Skip to content

Commit

Permalink
feat: parse mustache expressions inside attribute value strings
Browse files Browse the repository at this point in the history
Not supporting this was reported in #2

Note that we use a short-cut here to enable mustache statements inside
of a string that disallows a `{` to appear inside a "concat statement".

Technically this should be allowed, though I would say it's quite rare.

Issue #3 was files to keep track of eventually fixing that, though it
will likely require moving the scanner into C to pull that off.

The problem is that `{` alone is safe, but `{{` together is not, and I'm
not sure you can write a regex that TreeSitter supports that can capture
that correctly.
  • Loading branch information
alexlafroscia committed Mar 2, 2021
1 parent 1c3d01c commit 43d0dbd
Show file tree
Hide file tree
Showing 5 changed files with 1,208 additions and 776 deletions.
27 changes: 26 additions & 1 deletion grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,36 @@ module.exports = grammar({
optional(
seq(
"=",
choice($.string_literal, $.number_literal, $.mustache_statement)
choice(
alias($._mustache_safe_string_literal, $.string_literal),
$.concat_statement,
$.number_literal,
$.mustache_statement
)
)
)
),

// Special attribute-value strings that can embed a mustache statement
concat_statement: ($) =>
choice(
$._single_quote_concat_statement,
$._double_quote_concat_statement
),

_single_quote_concat_statement: ($) =>
prec(1, seq("'", $.mustache_statement, "'")),
_double_quote_concat_statement: ($) =>
prec(1, seq('"', $.mustache_statement, '"')),

_mustache_safe_string_literal: ($) =>
choice(
$._mustache_safe_single_quote_string_literal,
$._mustache_safe_double_quote_string_literal
),
_mustache_safe_single_quote_string_literal: () => seq("'", /[^'\\{]+/, "'"),
_mustache_safe_double_quote_string_literal: () => seq('"', /[^"\\{]+/, '"'),

block_params: ($) => seq("as", "|", repeat($.path_expression), "|"),

// Entering/existing Handlebars expressions
Expand Down
113 changes: 112 additions & 1 deletion src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,18 @@
{
"type": "CHOICE",
"members": [
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_mustache_safe_string_literal"
},
"named": true,
"value": "string_literal"
},
{
"type": "SYMBOL",
"name": "string_literal"
"name": "concat_statement"
},
{
"type": "SYMBOL",
Expand All @@ -224,6 +233,108 @@
}
]
},
"concat_statement": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_single_quote_concat_statement"
},
{
"type": "SYMBOL",
"name": "_double_quote_concat_statement"
}
]
},
"_single_quote_concat_statement": {
"type": "PREC",
"value": 1,
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "'"
},
{
"type": "SYMBOL",
"name": "mustache_statement"
},
{
"type": "STRING",
"value": "'"
}
]
}
},
"_double_quote_concat_statement": {
"type": "PREC",
"value": 1,
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "\""
},
{
"type": "SYMBOL",
"name": "mustache_statement"
},
{
"type": "STRING",
"value": "\""
}
]
}
},
"_mustache_safe_string_literal": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_mustache_safe_single_quote_string_literal"
},
{
"type": "SYMBOL",
"name": "_mustache_safe_double_quote_string_literal"
}
]
},
"_mustache_safe_single_quote_string_literal": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "'"
},
{
"type": "PATTERN",
"value": "[^'\\\\{]+"
},
{
"type": "STRING",
"value": "'"
}
]
},
"_mustache_safe_double_quote_string_literal": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "\""
},
{
"type": "PATTERN",
"value": "[^\"\\\\{]+"
},
{
"type": "STRING",
"value": "\""
}
]
},
"block_params": {
"type": "SEQ",
"members": [
Expand Down
19 changes: 19 additions & 0 deletions src/node-types.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
"type": "attribute_name",
"named": true
},
{
"type": "concat_statement",
"named": true
},
{
"type": "mustache_statement",
"named": true
Expand Down Expand Up @@ -46,6 +50,21 @@
"named": true,
"fields": {}
},
{
"type": "concat_statement",
"named": true,
"fields": {},
"children": {
"multiple": false,
"required": true,
"types": [
{
"type": "mustache_statement",
"named": true
}
]
}
},
{
"type": "element_node",
"named": true,
Expand Down
Loading

0 comments on commit 43d0dbd

Please sign in to comment.