Add isolated_sequence helper function
@@ -48,6 +48,18 @@ struct Scanner {
lexer->advance(lexer, false);
}
+ bool isolated_sequence(TSLexer *lexer, const char *sequence) {
+ for (const char *c = sequence; *c; c++) {
+ if (lexer->lookahead == *c) {
+ advance(lexer);
+ } else {
+ return false;
+ }
+ }
+
+ return iswspace(lexer->lookahead);
+ }
+
bool scan(TSLexer *lexer, const bool *valid_symbols) {
if (valid_symbols[LAYOUT_CLOSE_BRACE] && queued_close_brace_count > 0) {
queued_close_brace_count--;
@@ -87,11 +99,8 @@ struct Scanner {
}
lexer->mark_end(lexer);
- if (lexer->lookahead == 'i') {
- advance(lexer);
- if (lexer->lookahead == 'n') {
- advance(lexer);
- if (iswspace(lexer->lookahead)) {
+
+ if (isolated_sequence(lexer, "in")) {
indent_length_stack.pop_back();
if (valid_symbols[LAYOUT_CLOSE_BRACE]) {
lexer->result_symbol = LAYOUT_CLOSE_BRACE;
@@ -104,14 +113,8 @@ struct Scanner {
}
}
}
- }
- }
- if (!valid_symbols[ARROW]) {
- if (lexer->lookahead == '-') {
- advance(lexer);
- if (lexer->lookahead == '>') {
- advance(lexer);
+ if (!valid_symbols[ARROW] && isolated_sequence(lexer, "->")) {
if (iswspace(lexer->lookahead)) {
indent_length_stack.pop_back();
if (valid_symbols[LAYOUT_CLOSE_BRACE]) {
@@ -126,8 +129,6 @@ struct Scanner {
}
}
}
- }
- }
if (lexer->lookahead != '\n') return false;
advance(lexer);
@@ -167,11 +168,7 @@ struct Scanner {
}
if (!next_token_is_comment) {
- if (lexer->lookahead == 'i') {
- advance(lexer);
- if (lexer->lookahead == 'n') {
- advance(lexer);
- if (iswspace(lexer->lookahead)) {
+ if (isolated_sequence(lexer, "in")) {
indent_length_stack.pop_back();
if (valid_symbols[LAYOUT_CLOSE_BRACE]) {
lexer->result_symbol = LAYOUT_CLOSE_BRACE;
@@ -184,8 +181,6 @@ struct Scanner {
}
}
}
- }
- }
if (indent_length < indent_length_stack.back()) {
indent_length_stack.pop_back();
