Skip to content

Commit

Permalink
implement $ escape
Browse files Browse the repository at this point in the history
Treat `$` escapes specially so that the $ can be properly captured
apart from the escape symbols before it.
  • Loading branch information
Timothy DeHerrera committed Sep 7, 2022
1 parent 6b71a81 commit 04e5dca
Show file tree
Hide file tree
Showing 7 changed files with 6,601 additions and 6,321 deletions.
9 changes: 6 additions & 3 deletions corpus/string.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ ${bob} likes crisp $ bills. escape newline \
(variable_expression (identifier)))
(string_fragment)
(escape_sequence)
(escape_sequence)
(dollar_escape)
(string_fragment)
(string_fragment)))

================================================================================
Expand Down Expand Up @@ -63,12 +64,14 @@ This works, too: ''$
(string_fragment)
(escape_sequence)
(string_fragment)
(escape_sequence)
(dollar_escape)
(string_fragment)
(string_fragment)
(interpolation
(variable_expression (identifier)))
(string_fragment)
(escape_sequence)
(dollar_escape)
(string_fragment)
(string_fragment)))

================================================================================
Expand Down
16 changes: 11 additions & 5 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ module.exports = grammar({
$._indented_string_fragment,
$._path_start,
$.path_fragment,
$.dollar_escape,
$._indented_dollar_escape,
],

word: $ => $.keyword,
Expand Down Expand Up @@ -230,22 +232,26 @@ module.exports = grammar({
repeat(choice(
$.string_fragment,
$.interpolation,
$.escape_sequence
choice($.escape_sequence,
seq($.dollar_escape, alias('$', $.string_fragment))
)
)),
'"'
),
escape_sequence: $ => token.immediate(/\\(.|\s)/), // Can also escape newline.

escape_sequence: $ => token.immediate(/\\([^$]|\s)/), // Can also escape newline.

indented_string_expression: $ => seq(
"''",
repeat(choice(
alias($._indented_string_fragment, $.string_fragment),
$.interpolation,
alias($._indented_escape_sequence, $.escape_sequence),
)),
choice(alias($._indented_escape_sequence, $.escape_sequence),
seq(alias($._indented_dollar_escape, $.dollar_escape), alias('$', $.string_fragment))
))),
"''"
),
_indented_escape_sequence: $ => token.immediate(/'''|''\$|''\\(.|\s)/), // Can also escape newline.
_indented_escape_sequence: $ => token.immediate(/'''|''\\([^$]|\s)/), // Can also escape newline.

binding_set: $ => repeat1(field('binding', choice($.binding, $.inherit, $.inherit_from))),
binding: $ => seq(field('attrpath', $.attrpath), '=', field('expression', $._expression), ';'),
Expand Down
1 change: 1 addition & 0 deletions queries/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"}" @punctuation.special) @embedded

(escape_sequence) @escape
(dollar_escape) @escape

(function_expression
universal: (identifier) @variable.parameter
Expand Down
81 changes: 70 additions & 11 deletions src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -1521,8 +1521,31 @@
"name": "interpolation"
},
{
"type": "SYMBOL",
"name": "escape_sequence"
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "escape_sequence"
},
{
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "dollar_escape"
},
{
"type": "ALIAS",
"content": {
"type": "STRING",
"value": "$"
},
"named": true,
"value": "string_fragment"
}
]
}
]
}
]
}
Expand All @@ -1537,7 +1560,7 @@
"type": "IMMEDIATE_TOKEN",
"content": {
"type": "PATTERN",
"value": "\\\\(.|\\s)"
"value": "\\\\([^$]|\\s)"
}
},
"indented_string_expression": {
Expand Down Expand Up @@ -1566,13 +1589,41 @@
"name": "interpolation"
},
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_indented_escape_sequence"
},
"named": true,
"value": "escape_sequence"
"type": "CHOICE",
"members": [
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_indented_escape_sequence"
},
"named": true,
"value": "escape_sequence"
},
{
"type": "SEQ",
"members": [
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_indented_dollar_escape"
},
"named": true,
"value": "dollar_escape"
},
{
"type": "ALIAS",
"content": {
"type": "STRING",
"value": "$"
},
"named": true,
"value": "string_fragment"
}
]
}
]
}
]
}
Expand All @@ -1587,7 +1638,7 @@
"type": "IMMEDIATE_TOKEN",
"content": {
"type": "PATTERN",
"value": "'''|''\\$|''\\\\(.|\\s)"
"value": "'''|''\\\\([^$]|\\s)"
}
},
"binding_set": {
Expand Down Expand Up @@ -1920,6 +1971,14 @@
{
"type": "SYMBOL",
"name": "path_fragment"
},
{
"type": "SYMBOL",
"name": "dollar_escape"
},
{
"type": "SYMBOL",
"name": "_indented_dollar_escape"
}
],
"inline": [],
Expand Down
12 changes: 12 additions & 0 deletions src/node-types.json
Original file line number Diff line number Diff line change
Expand Up @@ -1037,6 +1037,10 @@
"multiple": true,
"required": false,
"types": [
{
"type": "dollar_escape",
"named": true
},
{
"type": "escape_sequence",
"named": true
Expand Down Expand Up @@ -1557,6 +1561,10 @@
"multiple": true,
"required": false,
"types": [
{
"type": "dollar_escape",
"named": true
},
{
"type": "escape_sequence",
"named": true
Expand Down Expand Up @@ -1932,6 +1940,10 @@
"type": "comment",
"named": true
},
{
"type": "dollar_escape",
"named": true
},
{
"type": "ellipses",
"named": true
Expand Down
Loading

0 comments on commit 04e5dca

Please sign in to comment.