Skip to content

Commit

Permalink
fix: handle comment delimiters in strings
Browse files Browse the repository at this point in the history
  • Loading branch information
Nsidorenco committed Nov 10, 2023
1 parent fa69ca8 commit 5a41f3b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 35 deletions.
13 changes: 5 additions & 8 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ module.exports = grammar({
],

extras: $ => [
$.block_comment,
$.line_comment,
/[ \s\f\uFEFF\u2060\u200B]|\\\r?n/,
],

Expand Down Expand Up @@ -97,8 +99,6 @@ module.exports = grammar({

_module_elem: $ =>
choice(
$.block_comment,
$.line_comment,
$.value_declaration,
$.module_defn,
$.module_abbrev,
Expand Down Expand Up @@ -388,8 +388,6 @@ module.exports = grammar({

_expression_inner: $ =>
choice(
$.line_comment,
$.block_comment,
$.const,
$.paren_expression,
$.begin_end_expression,
Expand Down Expand Up @@ -1580,7 +1578,7 @@ module.exports = grammar({
),

// note: \n is allowed in strings
_simple_string_char: $ => /[^\t\r\u0008\a\f\v\\"]/,
_simple_string_char: $ => imm(prec(1, /[^\t\r\u0008\a\f\v\\"]/)),
_string_char: $ => choice(
$._simple_string_char,
$._escape_char,
Expand All @@ -1605,8 +1603,7 @@ module.exports = grammar({
bytechar: $ => seq("'", $._char_char, imm("'B")),
bytearray: $ => seq('"', repeat($._string_char), imm('"B')),
verbatim_bytearray: $ => seq('@"', repeat($._verbatim_string_char), imm('"B')),
_simple_or_escape_char: $ => choice($._escape_char, imm(/[^'\\]/)),
triple_quoted_string: $ => seq('"""', repeat($._simple_or_escape_char), imm('"""')),
triple_quoted_string: $ => seq('"""', repeat($._string_char), imm('"""')),
_newline: $ => /\r?\n/,

unit: $ => seq("(", optional(seq($._virtual_open_section, $._virtual_end_section)), ")"),
Expand Down Expand Up @@ -1733,7 +1730,7 @@ module.exports = grammar({
//

block_comment: $ => seq("(*", $.block_comment_content, "*)"),
line_comment: $ => token(seq("//", repeat(/[^\n\r]/))),
line_comment: $ => token(seq('//', /[^\n\r]*/)),

identifier: $ => choice(
/[_\p{XID_Start}][_'\p{XID_Continue}]*/,
Expand Down
36 changes: 12 additions & 24 deletions src/grammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -6061,8 +6061,15 @@
]
},
"_simple_string_char": {
"type": "PATTERN",
"value": "[^\\t\\r\\u0008\\a\\f\\v\\\\\"]"
"type": "IMMEDIATE_TOKEN",
"content": {
"type": "PREC",
"value": 1,
"content": {
"type": "PATTERN",
"value": "[^\\t\\r\\u0008\\a\\f\\v\\\\\"]"
}
}
},
"_string_char": {
"type": "CHOICE",
Expand Down Expand Up @@ -6264,22 +6271,6 @@
}
]
},
"_simple_or_escape_char": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "_escape_char"
},
{
"type": "IMMEDIATE_TOKEN",
"content": {
"type": "PATTERN",
"value": "[^'\\\\]"
}
}
]
},
"triple_quoted_string": {
"type": "SEQ",
"members": [
Expand All @@ -6291,7 +6282,7 @@
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "_simple_or_escape_char"
"name": "_string_char"
}
},
{
Expand Down Expand Up @@ -7359,11 +7350,8 @@
"value": "//"
},
{
"type": "REPEAT",
"content": {
"type": "PATTERN",
"value": "[^\\n\\r]"
}
"type": "PATTERN",
"value": "[^\\n\\r]*"
}
]
}
Expand Down
6 changes: 3 additions & 3 deletions test/corpus/comments.txt
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ let x = 1
(const (int)))))

================================================================================
coment in simple string
comment in simple string
================================================================================

let x = "//comment in string"
Expand All @@ -97,7 +97,7 @@ let x = "//comment in string"
(string)))))

================================================================================
coment in triple-quoted string
comment in triple-quoted string
================================================================================

let x = """
Expand All @@ -117,7 +117,7 @@ let x = """
(triple_quoted_string)))))

================================================================================
coment in verbatim string
comment in verbatim string
================================================================================

let x = @"//comment in string"
Expand Down

0 comments on commit 5a41f3b

Please sign in to comment.