Skip to content

Commit 52f79e4

Browse files
committed
fix: port javascript scanner changes
1 parent afd7339 commit 52f79e4

File tree

1 file changed

+49
-5
lines changed

1 file changed

+49
-5
lines changed

common/scanner.h

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ static bool scan_template_chars(TSLexer *lexer) {
3636
}
3737
}
3838

39-
static bool scan_whitespace_and_comments(TSLexer *lexer) {
39+
static bool scan_whitespace_and_comments(TSLexer *lexer, bool *scanned_comment) {
4040
for (;;) {
4141
while (iswspace(lexer->lookahead)) {
4242
skip(lexer);
@@ -50,6 +50,7 @@ static bool scan_whitespace_and_comments(TSLexer *lexer) {
5050
while (lexer->lookahead != 0 && lexer->lookahead != '\n') {
5151
skip(lexer);
5252
}
53+
*scanned_comment = true;
5354
} else if (lexer->lookahead == '*') {
5455
skip(lexer);
5556
while (lexer->lookahead != 0) {
@@ -72,7 +73,7 @@ static bool scan_whitespace_and_comments(TSLexer *lexer) {
7273
}
7374
}
7475

75-
static bool scan_automatic_semicolon(TSLexer *lexer, const bool *valid_symbols){
76+
static bool scan_automatic_semicolon(TSLexer *lexer, const bool *valid_symbols, bool *scanned_comment) {
7677
lexer->result_symbol = AUTOMATIC_SEMICOLON;
7778
lexer->mark_end(lexer);
7879

@@ -96,7 +97,7 @@ static bool scan_automatic_semicolon(TSLexer *lexer, const bool *valid_symbols){
9697

9798
skip(lexer);
9899

99-
if (!scan_whitespace_and_comments(lexer)) return false;
100+
if (!scan_whitespace_and_comments(lexer, scanned_comment)) return false;
100101

101102
switch (lexer->lookahead) {
102103
case ',':
@@ -198,6 +199,44 @@ static bool scan_ternary_qmark(TSLexer *lexer) {
198199
return false;
199200
}
200201

202+
static bool scan_closing_comment(TSLexer *lexer) {
203+
while (iswspace(lexer->lookahead) || lexer->lookahead == 0x2028 || lexer->lookahead == 0x2029) {
204+
skip(lexer);
205+
}
206+
207+
const char *comment_start = "<!--";
208+
const char *comment_end = "-->";
209+
210+
if (lexer->lookahead == '<') {
211+
for (unsigned i = 0; i < 4; i++) {
212+
if (lexer->lookahead != comment_start[i]) {
213+
return false;
214+
}
215+
advance(lexer);
216+
}
217+
} else if (lexer->lookahead == '-') {
218+
for (unsigned i = 0; i < 3; i++) {
219+
if (lexer->lookahead != comment_end[i]) {
220+
return false;
221+
}
222+
advance(lexer);
223+
}
224+
} else {
225+
return false;
226+
}
227+
228+
while (lexer->lookahead != 0 && lexer->lookahead != '\n' && lexer->lookahead != 0x2028 &&
229+
lexer->lookahead != 0x2029) {
230+
advance(lexer);
231+
}
232+
233+
lexer->result_symbol = HTML_COMMENT;
234+
lexer->mark_end(lexer);
235+
236+
return true;
237+
}
238+
239+
201240
static inline bool external_scanner_scan(void *payload, TSLexer *lexer, const bool *valid_symbols) {
202241
if (valid_symbols[TEMPLATE_CHARS]) {
203242
if (valid_symbols[AUTOMATIC_SEMICOLON]) return false;
@@ -206,15 +245,20 @@ static inline bool external_scanner_scan(void *payload, TSLexer *lexer, const bo
206245
valid_symbols[AUTOMATIC_SEMICOLON] ||
207246
valid_symbols[FUNCTION_SIGNATURE_AUTOMATIC_SEMICOLON]
208247
) {
209-
bool ret = scan_automatic_semicolon(lexer, valid_symbols);
210-
if (!ret && valid_symbols[TERNARY_QMARK] && lexer->lookahead == '?')
248+
bool scanned_comment = false;
249+
bool ret = scan_automatic_semicolon(lexer, valid_symbols, &scanned_comment);
250+
if (!ret && !scanned_comment && valid_symbols[TERNARY_QMARK] && lexer->lookahead == '?')
211251
return scan_ternary_qmark(lexer);
212252
return ret;
213253
}
214254
if (valid_symbols[TERNARY_QMARK]) {
215255
return scan_ternary_qmark(lexer);
216256
}
217257

258+
if (valid_symbols[HTML_COMMENT] && !valid_symbols[LOGICAL_OR] && !valid_symbols[ESCAPE_SEQUENCE]) {
259+
return scan_closing_comment(lexer);
260+
}
261+
218262
return false;
219263

220264
}

0 commit comments

Comments
 (0)