Skip to content
Merged
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
8 changes: 6 additions & 2 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,9 +352,13 @@ module.exports = grammar({
),

// quoted-string = '"' { CHAR } '"'
// String content needs lexical precedence over the comment token:
// without it, a '#' in the content lets the comment (which runs to end
// of line) win the longest-match rule, so `"#fff"` lexed as a comment
// and corrupted the rest of the parse.
quoted_string: $ => seq(
'"',
optional(alias(token.immediate(/([^\\"\n]|\\["\\/bfnrt]|\\u[0-9a-fA-F]{4})+/), 'string_content')),
optional(alias(token.immediate(prec(1, /([^\\"\n]|\\["\\/bfnrt]|\\u[0-9a-fA-F]{4})+/)), 'string_content')),
'"',
),

Expand All @@ -363,7 +367,7 @@ module.exports = grammar({
seq(
'`',
repeat(
token.immediate(/[^`]+/),
token.immediate(prec(1, /[^`]+/)),
),
'`',
),
Expand Down
18 changes: 13 additions & 5 deletions src/grammar.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/node-types.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

100 changes: 50 additions & 50 deletions src/parser.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

75 changes: 75 additions & 0 deletions test/corpus/strings.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
================================================================================
hash inside quoted string
================================================================================

package test

color := "#fff"

--------------------------------------------------------------------------------
(source_file
(module
(package)
(ref
(var))
(policy
(rule
(rule_head
(var))
(rule_body
(assignment)
(term
(scalar
(string
(quoted_string)))))))))

================================================================================
hash inside raw string
================================================================================

package test

tag := `#hash`

--------------------------------------------------------------------------------
(source_file
(module
(package)
(ref
(var))
(policy
(rule
(rule_head
(var))
(rule_body
(assignment)
(term
(scalar
(string
(raw_string)))))))))

================================================================================
comment after string containing hash
================================================================================

package test

x := "#a" # real comment

--------------------------------------------------------------------------------
(source_file
(module
(package)
(ref
(var))
(policy
(rule
(rule_head
(var))
(rule_body
(assignment)
(term
(scalar
(string
(quoted_string))))))))
(comment))
Loading