Skip to content

Commit ebf6259

Browse files
authored
Merge pull request rescript-lang#190 from aspeddro/refactor-list-constructor
refactor: move list identifier to scanner
2 parents 7ae8e63 + 389c409 commit ebf6259

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: $ => [
@@ -652,7 +653,8 @@ module.exports = grammar({
652653
),
653654

654655
list: $ => seq(
655-
'list{',
656+
$._list_constructor,
657+
'{',
656658
optional(
657659
commaSep1t(
658660
seq(repeat($.decorator), $._list_element)
@@ -951,7 +953,8 @@ module.exports = grammar({
951953
),
952954

953955
list_pattern: $ => seq(
954-
'list{',
956+
$._list_constructor,
957+
'{',
955958
optional(commaSep1t(
956959
seq(repeat($.decorator), $._collection_element_pattern)
957960
)),

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)