Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support embedded expressions/braces in double quoted strings #26

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 39 additions & 6 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -406,14 +406,47 @@ const rules = {

// prettier-ignore
string: $ =>
token(
choice(
/'(\\'|\\\\|\\?[^'\\])*'/,
/"(\\"|\\\\|\\?[^"\\])*"/,
),
choice(
$._single_quoted_string,
$._double_quoted_string,
),

// Breakout single quoted string for symmetry with double quoted strings.
_single_quoted_string: $ => /'(\\'|\\\\|\\?[^'\\])*'/,

// Reference: https://docs.hhvm.com/hack/source-code-fundamentals/literals#string-literals__double-quoted-string-literals
_double_quoted_string: $ =>
seq(
'"',
choice.rep($._string_character, $._escape_sequence, $.embedded_expression, $.embedded_brace_expression),
'"',
),

_string_character: $ => choice(token(/([^"\\])/), token(prec(1, choice('#', '//', '/*')))),
Comment on lines +421 to +425
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From last time we talked, did you try putting repeating $._string_character in it's own rule, so we can just have a $.string_body rule that encompass a full consecutive string?

string_body: $ => rep1($._string_character)

or

string_body: $ => rep1(choice(token(/([^"\\])/), token(prec(1, choice('#', '//', '/*')))))

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, this is a nit, but I don't think you need token around /([^"\\])/. And that rule also doesn't have to use a regex group anymore () since there's only one option.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I try that it creates a conflict. Any suggestions for resolving it?

Unresolved conflict for symbol sequence:

  '"'  string_body_repeat1  •  '_string_character_token1'  …

Possible interpretations:

  1:  '"'  (string_body  string_body_repeat1)  •  '_string_character_token1'  …
  2:  '"'  (string_body_repeat1  string_body_repeat1  •  string_body_repeat1)

Possible resolutions:

  1:  Specify a left or right associativity in `string_body`
  2:  Add a conflict for these rules: `string_body`

I suspect that adding this rule as is might lead some problems. Tree-sitter prioritizes matching based on length. Say for example we have:

"sometext$var"

I think the $ will be captured as part of the string body instead of an embedded expression, and therefore the whole thing will be parsed as a string body. We might need to make some modifications to accommodate this.

Copy link

@aosq aosq Aug 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you'd want right associativity for $.string_body since that'll capture the largest node. Left associativity would capture the $.string_body on first match so it'd end up just capturing individual characters like it does without the repeat1.

If we run into problems with $ and {, we could do something like we do for $.xhp_comment where we exclude problematic token characters and only allow them in specific sequences. Something like,

  string_body: $ =>
    repeat1(
      choice(
        seq(opt('\\'), choice(/[^"\\${]/, token(prec(1, choice('#', '//', '/*'))))),
        seq(
          // $ is allowed only if not followed by an identifier character
          repeat1('$'),
          opt('\\'),
          choice(/[^"\\${a-zA-Z_\x80-\xff]/, token(prec(1, choice('#', '//', '/*')))),
        ),
        seq(
          // { is allowed only if not followed by $
          repeat1('{'),
          opt('\\'),
          choice(/[^"\\${]/, token(prec(1, choice('#', '//', '/*')))),
        ),
      ),
    ),

I checked out your branch and tried adding right associativity to repeating $. _string_character and it doesn't seem conflict with $.variable (honestly surprised it doesn't cause issues (at least not immediate ones)).


// These are the relevant escape sequences that will affect parsing an embedded expression in the string
_escape_sequence: $ => token(seq('\\', opt(choice('\\', '"', '$', '{',)))),

embedded_expression: $ => seq($._embedded_expression),

_embedded_expression: $ =>
choice(
$.variable,
alias($._embedded_subscript_expression, $.subscript_expression),
alias($._embedded_selection_expression, $.selection_expression),
),

_embedded_subscript_expression: $ =>
seq($.variable, '[', opt($._expression), ']'),

_embedded_selection_expression: $ =>
seq(
$.variable,
field('selection_operator', choice('?->', '->')),
$._variablish,
),

prefixed_string: $ => seq(field('prefix', $.identifier), $.string),
prefixed_string: $ => prec(-1, seq(field('prefix', $.identifier), $.string)),

// Types

Expand Down
234 changes: 224 additions & 10 deletions src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -1941,38 +1941,252 @@
]
},
"string": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_single_quoted_string"
},
{
"type": "SYMBOL",
"name": "_double_quoted_string"
}
]
},
"_single_quoted_string": {
"type": "PATTERN",
"value": "'(\\\\'|\\\\\\\\|\\\\?[^'\\\\])*'"
},
"_double_quoted_string": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "\""
},
{
"type": "REPEAT",
"content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_string_character"
},
{
"type": "SYMBOL",
"name": "_escape_sequence"
},
{
"type": "SYMBOL",
"name": "embedded_expression"
},
{
"type": "SYMBOL",
"name": "embedded_brace_expression"
}
]
}
},
{
"type": "STRING",
"value": "\""
}
]
},
"_string_character": {
"type": "CHOICE",
"members": [
{
"type": "TOKEN",
"content": {
"type": "PATTERN",
"value": "([^\"\\\\])"
}
},
{
"type": "TOKEN",
"content": {
"type": "PREC",
"value": 1,
"content": {
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "#"
},
{
"type": "STRING",
"value": "//"
},
{
"type": "STRING",
"value": "/*"
}
]
}
}
}
]
},
"_escape_sequence": {
"type": "TOKEN",
"content": {
"type": "CHOICE",
"type": "SEQ",
"members": [
{
"type": "PATTERN",
"value": "'(\\\\'|\\\\\\\\|\\\\?[^'\\\\])*'"
"type": "STRING",
"value": "\\"
},
{
"type": "PATTERN",
"value": "\"(\\\\\"|\\\\\\\\|\\\\?[^\"\\\\])*\""
"type": "CHOICE",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "\\"
},
{
"type": "STRING",
"value": "\""
},
{
"type": "STRING",
"value": "$"
},
{
"type": "STRING",
"value": "{"
}
]
},
{
"type": "BLANK"
}
]
}
]
}
},
"prefixed_string": {
"embedded_expression": {
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "prefix",
"type": "SYMBOL",
"name": "_embedded_expression"
}
]
},
"_embedded_expression": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "variable"
},
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "identifier"
"name": "_embedded_subscript_expression"
},
"named": true,
"value": "subscript_expression"
},
{
"type": "ALIAS",
"content": {
"type": "SYMBOL",
"name": "_embedded_selection_expression"
},
"named": true,
"value": "selection_expression"
}
]
},
"_embedded_subscript_expression": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "variable"
},
{
"type": "STRING",
"value": "["
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_expression"
},
{
"type": "BLANK"
}
]
},
{
"type": "STRING",
"value": "]"
}
]
},
"_embedded_selection_expression": {
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "variable"
},
{
"type": "FIELD",
"name": "selection_operator",
"content": {
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "?->"
},
{
"type": "STRING",
"value": "->"
}
]
}
},
{
"type": "SYMBOL",
"name": "string"
"name": "_variablish"
}
]
},
"prefixed_string": {
"type": "PREC",
"value": -1,
"content": {
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "prefix",
"content": {
"type": "SYMBOL",
"name": "identifier"
}
},
{
"type": "SYMBOL",
"name": "string"
}
]
}
},
"_type": {
"type": "CHOICE",
"members": [
Expand Down
Loading