Skip to content

Commit 0662afb

Browse files
committed
Merge pull request #157
2 parents e30769c + 3bd75c5 commit 0662afb

File tree

2 files changed

+92
-4
lines changed

2 files changed

+92
-4
lines changed

src/scanner.c

+14-4
Original file line numberDiff line numberDiff line change
@@ -236,12 +236,23 @@ static bool scan_whitespace_and_comments(TSLexer *lexer) {
236236
return lexer->lookahead != '/';
237237
}
238238

239+
// Test for any identifier character other than the first character.
240+
// This is meant to match the regexp [\p{L}_\p{Nd}]
241+
// as found in '_alpha_identifier' (see grammar.js).
242+
static bool is_word_char(int32_t c) {
243+
return (iswalnum(c) || c == '_');
244+
}
245+
246+
// Scan for [the end of] a nonempty alphanumeric identifier or
247+
// alphanumeric keyword (including '_').
239248
static bool scan_for_word(TSLexer *lexer, const char* word, unsigned len) {
240249
skip(lexer);
241250
for (unsigned i = 0; i < len; ++i) {
242251
if (lexer->lookahead != word[i]) return false;
243252
skip(lexer);
244253
}
254+
// check that the identifier stops here
255+
if (is_word_char(lexer->lookahead)) return false;
245256
return true;
246257
}
247258

@@ -285,10 +296,8 @@ static bool scan_automatic_semicolon(TSLexer *lexer) {
285296

286297
if (sameline) {
287298
switch (lexer->lookahead) {
288-
// Don't insert a semicolon before an else
289-
case 'e':
290-
return !scan_for_word(lexer, "lse", 3);
291-
299+
// Insert imaginary semicolon before an 'import' but not in front
300+
// of other words or keywords starting with 'i'
292301
case 'i':
293302
return scan_for_word(lexer, "mport", 5);
294303

@@ -297,6 +306,7 @@ static bool scan_automatic_semicolon(TSLexer *lexer) {
297306
lexer->mark_end(lexer);
298307
return true;
299308

309+
// Don't insert a semicolon in other cases
300310
default:
301311
return false;
302312
}

test/corpus/expressions.txt

+78
Original file line numberDiff line numberDiff line change
@@ -631,3 +631,81 @@ if (cond1) {
631631
(value_argument
632632
(string_literal
633633
(string_content))))))))))))
634+
635+
================================================================================
636+
If-else without braces or semicolons or newlines
637+
================================================================================
638+
639+
if (true) a else if (true) b else c
640+
641+
---
642+
643+
(source_file
644+
(if_expression
645+
(boolean_literal)
646+
(control_structure_body
647+
(simple_identifier))
648+
(control_structure_body
649+
(if_expression
650+
(boolean_literal)
651+
(control_structure_body
652+
(simple_identifier))
653+
(control_structure_body
654+
(simple_identifier))))))
655+
656+
================================================================================
657+
Infix syntax for function application
658+
================================================================================
659+
660+
a mul b
661+
662+
---
663+
664+
(source_file
665+
(infix_expression
666+
(simple_identifier)
667+
(simple_identifier)
668+
(simple_identifier)))
669+
670+
================================================================================
671+
Infix syntax, edge case 1
672+
================================================================================
673+
674+
a exxx b
675+
a ixxx b
676+
677+
---
678+
679+
(source_file
680+
(infix_expression
681+
(simple_identifier)
682+
(simple_identifier)
683+
(simple_identifier))
684+
(infix_expression
685+
(simple_identifier)
686+
(simple_identifier)
687+
(simple_identifier)))
688+
689+
================================================================================
690+
Infix syntax, edge case 2
691+
================================================================================
692+
693+
a else_ b
694+
a import_ b
695+
a imports b
696+
697+
---
698+
699+
(source_file
700+
(infix_expression
701+
(simple_identifier)
702+
(simple_identifier)
703+
(simple_identifier))
704+
(infix_expression
705+
(simple_identifier)
706+
(simple_identifier)
707+
(simple_identifier))
708+
(infix_expression
709+
(simple_identifier)
710+
(simple_identifier)
711+
(simple_identifier)))

0 commit comments

Comments
 (0)