Skip to content

Commit a18eda7

Browse files
committed
refactor: move list identifier to scanner
Close rescript-lang#189
1 parent 8f5c6de commit a18eda7

File tree

4 files changed

+31
-12
lines changed

4 files changed

+31
-12
lines changed

grammar.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ module.exports = grammar({
1010
$._template_chars,
1111
$._lparen,
1212
$._rparen,
13+
$._list_constructor
1314
],
1415

1516
extras: $ => [
@@ -637,7 +638,8 @@ module.exports = grammar({
637638
),
638639

639640
list: $ => seq(
640-
'list{',
641+
$._list_constructor,
642+
'{',
641643
optional(commaSep1t($._list_element)),
642644
'}'
643645
),
@@ -939,7 +941,8 @@ module.exports = grammar({
939941
),
940942

941943
list_pattern: $ => seq(
942-
'list{',
944+
$._list_constructor,
945+
'{',
943946
optional(commaSep1t($._collection_element_pattern)),
944947
'}',
945948
),

queries/highlights.scm

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,10 @@
1010
[
1111
(type_identifier)
1212
(unit_type)
13+
(list)
14+
(list_pattern)
1315
] @type
1416

15-
16-
(list "list{" @type)
17-
(list_pattern "list{" @type)
18-
19-
; To ensure that the closing curly bracket is the same color (scope) as the opening curly bracket
20-
(list "}" @type (#set! "priority" 105))
21-
(list_pattern "}" @type (#set! "priority" 105))
22-
2317
[
2418
(variant_identifier)
2519
(polyvar_identifier)

src/scanner.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ enum TokenType {
1111
TEMPLATE_CHARS,
1212
L_PAREN,
1313
R_PAREN,
14+
LIST_CONSTRUCTOR
1415
};
1516

1617
typedef struct ScannerState {
@@ -44,6 +45,7 @@ void tree_sitter_rescript_external_scanner_deserialize(void* state, const char *
4445
}
4546

4647
static void advance(TSLexer *lexer) { lexer->advance(lexer, false); }
48+
static void skip(TSLexer *lexer) { lexer->advance(lexer, true); }
4749

4850
static bool is_inline_whitespace(int32_t c) {
4951
return c == ' ' || c == '\t';
@@ -138,7 +140,7 @@ bool tree_sitter_rescript_external_scanner_scan(
138140
const in_string = state->in_quotes || state->in_backticks;
139141

140142
while (is_inline_whitespace(lexer->lookahead) && !in_string) {
141-
advance(lexer);
143+
skip(lexer);
142144
}
143145

144146
if (valid_symbols[TEMPLATE_CHARS]) {
@@ -285,6 +287,26 @@ bool tree_sitter_rescript_external_scanner_scan(
285287
return true;
286288
}
287289

290+
if (valid_symbols[LIST_CONSTRUCTOR]) {
291+
lexer->result_symbol = LIST_CONSTRUCTOR;
292+
if (lexer->lookahead == 'l') {
293+
advance(lexer);
294+
if (lexer->lookahead == 'i') {
295+
advance(lexer);
296+
if (lexer->lookahead == 's') {
297+
advance(lexer);
298+
if (lexer->lookahead == 't') {
299+
advance(lexer);
300+
if (lexer->lookahead == '{') {
301+
lexer->mark_end(lexer);
302+
return true;
303+
}
304+
}
305+
}
306+
}
307+
}
308+
}
309+
288310
lexer->advance(lexer, iswspace(lexer->lookahead));
289311
return false;
290312
}

test/highlight/expressions.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,6 @@ try {
5252

5353

5454
let c = list{a, ...list{b}}
55-
// ^ type
55+
// ^ type
5656
// ^ variable
5757
// ^ variable

0 commit comments

Comments
 (0)