Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions src/scanner.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ typedef struct {
Array(Heredoc) open_heredocs;
} Scanner;

static const int32_t ASCII_C0_CONTROL_MAX = 0x1f;
static const int32_t ASCII_MAX = 0x7f;

const char NON_IDENTIFIER_CHARS[] = {
'\0', '\n', '\r', '\t', ' ', ':', ';', '`', '"', '\'', '@', '$', '#', '.', ',', '|', '^', '&',
'<', '=', '>', '+', '-', '*', '/', '\\', '%', '?', '!', '~', '(', ')', '[', ']', '{', '}',
Expand Down Expand Up @@ -336,8 +339,14 @@ static inline bool scan_operator(TSLexer *lexer) {
}
}

static inline bool is_iden_char(char c) {
return memchr(&NON_IDENTIFIER_CHARS, c, sizeof(NON_IDENTIFIER_CHARS)) == NULL;
static inline bool is_iden_char(int32_t c) {
if (c <= ASCII_C0_CONTROL_MAX || iswspace(c)) {
return false;
}
if (c > ASCII_MAX) {
return true;
}
return memchr(&NON_IDENTIFIER_CHARS, (char)c, sizeof(NON_IDENTIFIER_CHARS)) == NULL;
}

static inline bool scan_symbol_identifier(TSLexer *lexer) {
Expand All @@ -350,13 +359,13 @@ static inline bool scan_symbol_identifier(TSLexer *lexer) {
advance(lexer);
}

if (is_iden_char((char)lexer->lookahead)) {
if (is_iden_char(lexer->lookahead)) {
advance(lexer);
} else if (!scan_operator(lexer)) {
return false;
}

while (is_iden_char((char)lexer->lookahead)) {
while (is_iden_char(lexer->lookahead)) {
advance(lexer);
}

Expand Down Expand Up @@ -656,7 +665,7 @@ static inline bool scan_short_interpolation(TSLexer *lexer, const bool has_conte
if (lexer->lookahead == '@') {
advance(lexer);
}
is_short_interpolation = is_iden_char((char)lexer->lookahead) && !iswdigit(lexer->lookahead);
is_short_interpolation = is_iden_char(lexer->lookahead) && !iswdigit(lexer->lookahead);
}

if (is_short_interpolation) {
Expand Down
15 changes: 15 additions & 0 deletions test/corpus/literals.txt
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,9 @@ interpolation

"#{foo}"
"foo#@foo"
"foo#@言"
"#@@foo"
"#@@言"
"#$foo"
"#$%"
"#$1"
Expand All @@ -694,6 +696,13 @@ interpolation
(string_content)
(interpolation
(instance_variable)))
(string
(string_content)
(interpolation
(instance_variable)))
(string
(interpolation
(class_variable)))
(string
(interpolation
(class_variable)))
Expand Down Expand Up @@ -2290,6 +2299,9 @@ Cß
@äö
@@äö
:äö
:言
:你
:일
äö

---
Expand All @@ -2299,4 +2311,7 @@ Cß
(instance_variable)
(class_variable)
(simple_symbol)
(simple_symbol)
(simple_symbol)
(simple_symbol)
(identifier))