Skip to content

Commit 9b38c6b

Browse files
authored
update again (#522)
1 parent 0610ec8 commit 9b38c6b

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

lang/semgrep-grammars/src/semgrep-kotlin/src/scanner.c

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -239,12 +239,23 @@ static bool scan_whitespace_and_comments(TSLexer *lexer) {
239239
return lexer->lookahead != '/';
240240
}
241241

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

@@ -288,10 +299,8 @@ static bool scan_automatic_semicolon(TSLexer *lexer) {
288299

289300
if (sameline) {
290301
switch (lexer->lookahead) {
291-
// Don't insert a semicolon before an else
292-
case 'e':
293-
return !scan_for_word(lexer, "lse", 3);
294-
302+
// Insert imaginary semicolon before an 'import' but not in front
303+
// of other words or keywords starting with 'i'
295304
case 'i':
296305
return scan_for_word(lexer, "mport", 5);
297306

@@ -300,6 +309,7 @@ static bool scan_automatic_semicolon(TSLexer *lexer) {
300309
lexer->mark_end(lexer);
301310
return true;
302311

312+
// Don't insert a semicolon in other cases
303313
default:
304314
return false;
305315
}
@@ -538,4 +548,4 @@ void tree_sitter_kotlin_external_scanner_deserialize(void *payload, const char *
538548
} else {
539549
array_clear(stack);
540550
}
541-
}
551+
}

0 commit comments

Comments
 (0)