diff --git a/grammar.js b/grammar.js index 6b2c3cc2..e5a32474 100644 --- a/grammar.js +++ b/grammar.js @@ -19,9 +19,10 @@ module.exports = grammar({ $._ternary_qmark, $.html_comment, '||', - // We use escape sequence to tell the scanner if we're currently inside a string or template string, in which case + // We use escape sequence and regex pattern to tell the scanner if we're currently inside a string or template string, in which case // it should NOT parse html comments. $.escape_sequence, + $.regex_pattern, ], extras: $ => [ diff --git a/src/grammar.json b/src/grammar.json index fb83f44f..62128c4e 100644 --- a/src/grammar.json +++ b/src/grammar.json @@ -6861,6 +6861,10 @@ { "type": "SYMBOL", "name": "escape_sequence" + }, + { + "type": "SYMBOL", + "name": "regex_pattern" } ], "inline": [ @@ -6888,4 +6892,3 @@ "pattern" ] } - diff --git a/src/parser.c b/src/parser.c index f48fd50d..aa43320c 100644 --- a/src/parser.c +++ b/src/parser.c @@ -19,7 +19,7 @@ #define SYMBOL_COUNT 267 #define ALIAS_COUNT 4 #define TOKEN_COUNT 136 -#define EXTERNAL_TOKEN_COUNT 6 +#define EXTERNAL_TOKEN_COUNT 7 #define FIELD_COUNT 38 #define MAX_ALIAS_SEQUENCE_LENGTH 7 #define PRODUCTION_ID_COUNT 106 @@ -13499,12 +13499,12 @@ static const TSLexMode ts_lex_modes[STATE_COUNT] = { [2729] = {.lex_state = 252, .external_lex_state = 2}, [2730] = {.lex_state = 252, .external_lex_state = 2}, [2731] = {.lex_state = 34, .external_lex_state = 2}, - [2732] = {.lex_state = 4, .external_lex_state = 2}, + [2732] = {.lex_state = 4, .external_lex_state = 8}, [2733] = {.lex_state = 34, .external_lex_state = 2}, [2734] = {.lex_state = 252, .external_lex_state = 2}, [2735] = {.lex_state = 34, .external_lex_state = 2}, [2736] = {.lex_state = 34, .external_lex_state = 2}, - [2737] = {.lex_state = 4, .external_lex_state = 2}, + [2737] = {.lex_state = 4, .external_lex_state = 8}, [2738] = {.lex_state = 39, .external_lex_state = 2}, [2739] = {.lex_state = 252, .external_lex_state = 2}, [2740] = {.lex_state = 252, .external_lex_state = 2}, @@ -13514,7 +13514,7 @@ static const TSLexMode ts_lex_modes[STATE_COUNT] = { [2744] = {.lex_state = 252, .external_lex_state = 2}, [2745] = {.lex_state = 34, .external_lex_state = 2}, [2746] = {.lex_state = 34, .external_lex_state = 2}, - [2747] = {.lex_state = 4, .external_lex_state = 2}, + [2747] = {.lex_state = 4, .external_lex_state = 8}, [2748] = {.lex_state = 252, .external_lex_state = 2}, [2749] = {.lex_state = 30, .external_lex_state = 2}, [2750] = {.lex_state = 252, .external_lex_state = 2}, @@ -13526,7 +13526,7 @@ static const TSLexMode ts_lex_modes[STATE_COUNT] = { [2756] = {.lex_state = 252, .external_lex_state = 2}, [2757] = {.lex_state = 34, .external_lex_state = 2}, [2758] = {.lex_state = 252, .external_lex_state = 2}, - [2759] = {.lex_state = 4, .external_lex_state = 2}, + [2759] = {.lex_state = 4, .external_lex_state = 8}, [2760] = {.lex_state = 252, .external_lex_state = 2}, [2761] = {.lex_state = 34, .external_lex_state = 2}, [2762] = {.lex_state = 34, .external_lex_state = 2}, @@ -149066,6 +149066,7 @@ enum ts_external_scanner_symbol_identifiers { ts_external_token_html_comment = 3, ts_external_token_PIPE_PIPE = 4, ts_external_token_escape_sequence = 5, + ts_external_token_regex_pattern = 6, }; static const TSSymbol ts_external_scanner_symbol_map[EXTERNAL_TOKEN_COUNT] = { @@ -149075,9 +149076,10 @@ static const TSSymbol ts_external_scanner_symbol_map[EXTERNAL_TOKEN_COUNT] = { [ts_external_token_html_comment] = sym_html_comment, [ts_external_token_PIPE_PIPE] = anon_sym_PIPE_PIPE, [ts_external_token_escape_sequence] = sym_escape_sequence, + [ts_external_token_regex_pattern] = sym_regex_pattern, }; -static const bool ts_external_scanner_states[8][EXTERNAL_TOKEN_COUNT] = { +static const bool ts_external_scanner_states[9][EXTERNAL_TOKEN_COUNT] = { [1] = { [ts_external_token__automatic_semicolon] = true, [ts_external_token__template_chars] = true, @@ -149113,6 +149115,10 @@ static const bool ts_external_scanner_states[8][EXTERNAL_TOKEN_COUNT] = { [ts_external_token_html_comment] = true, [ts_external_token_escape_sequence] = true, }, + [8] = { + [ts_external_token_html_comment] = true, + [ts_external_token_regex_pattern] = true, + }, }; #ifdef __cplusplus diff --git a/src/scanner.c b/src/scanner.c index a25af2fe..9bea9685 100644 --- a/src/scanner.c +++ b/src/scanner.c @@ -9,6 +9,7 @@ enum TokenType { HTML_COMMENT, LOGICAL_OR, ESCAPE_SEQUENCE, + REGEX_PATTERN, }; void *tree_sitter_javascript_external_scanner_create() { return NULL; } @@ -283,7 +284,8 @@ bool tree_sitter_javascript_external_scanner_scan(void *payload, TSLexer *lexer, return scan_ternary_qmark(lexer); } - if (valid_symbols[HTML_COMMENT] && !valid_symbols[LOGICAL_OR] && !valid_symbols[ESCAPE_SEQUENCE]) { + if (valid_symbols[HTML_COMMENT] && !valid_symbols[LOGICAL_OR] && !valid_symbols[ESCAPE_SEQUENCE] && + !valid_symbols[REGEX_PATTERN]) { return scan_html_comment(lexer); } diff --git a/test/corpus/statements.txt b/test/corpus/statements.txt index 8e6f23c4..9567f105 100644 --- a/test/corpus/statements.txt +++ b/test/corpus/statements.txt @@ -1022,6 +1022,7 @@ Comments within expressions y // comment * z; +var a = /