@konomanoasa/tree-sitter-posix-awk 0.18.0 → 0.20.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/scanner.c CHANGED
@@ -7,107 +7,98 @@
7
7
  #define ARRAY_LENGTH(array) (sizeof(array) / sizeof((array)[0]))
8
8
 
9
9
  enum TokenType {
10
- BEGIN_KEYWORD,
11
- END_KEYWORD,
12
- FUNCTION_KEYWORD,
13
- PRINT_KEYWORD,
14
- BREAK_KEYWORD,
15
- CONTINUE_KEYWORD,
16
- DELETE_KEYWORD,
17
- DO_KEYWORD,
18
- ELSE_KEYWORD,
19
- EXIT_KEYWORD,
20
- FOR_KEYWORD,
21
- IF_KEYWORD,
22
- NEXT_KEYWORD,
23
- NEXTFILE_KEYWORD,
24
- PRINTF_KEYWORD,
25
- RETURN_KEYWORD,
26
- WHILE_KEYWORD,
27
- NAME_WORD,
28
- FOR_IN_VARIABLE_WORD,
29
- GETLINE_WORD,
30
- IN_WORD,
31
- BUILTIN_FUNC_NAME_WORD,
32
- BUILTIN_CALL_WORD,
33
- FUNC_NAME_WORD,
34
- NUMBER,
35
- DIVISION_SLASH,
36
- ERE_OPENING_SLASH,
37
- DIV_ASSIGN_OPERATOR,
38
- ADD_ASSIGN_OPERATOR,
39
- SUB_ASSIGN_OPERATOR,
40
- MUL_ASSIGN_OPERATOR,
41
- MOD_ASSIGN_OPERATOR,
42
- POW_ASSIGN_OPERATOR,
43
- OR_OPERATOR,
44
- AND_OPERATOR,
45
- NO_MATCH_OPERATOR,
46
- EQ_OPERATOR,
47
- LE_OPERATOR,
48
- GE_OPERATOR,
49
- NE_OPERATOR,
50
- INCR_OPERATOR,
51
- DECR_OPERATOR,
52
- APPEND_OPERATOR,
53
- PLUS_OPERATOR,
54
- MINUS_OPERATOR,
55
- STAR_OPERATOR,
56
- PERCENT_OPERATOR,
57
- CARET_OPERATOR,
58
- BANG_OPERATOR,
59
- LESS_OPERATOR,
60
- GREATER_OPERATOR,
61
- EQUAL_OPERATOR,
62
- PIPE_OPERATOR,
63
- OUTPUT_GREATER,
64
- ERE_COMPOUND_OPENING,
65
- ERE_DOT_CLOSING,
66
- ERE_EQUAL_CLOSING,
67
- ERE_COLON_CLOSING,
68
- ERE_BRACKET_LITERAL_OPEN,
69
- ERE_COMPOUND_CONTENT,
70
- ERE_CLOSING_HYPHEN,
71
- ERE_CLOSING,
72
- STRING_OPENING,
73
- STRING_CLOSING,
74
- STRING_CONTENT,
75
- STRING_ESCAPE,
76
- ERE_NAMED_ESCAPE,
77
- ERE_QUOTED_ESCAPE,
78
- ERE_OCTAL_ESCAPE,
79
- ERE_UNDEFINED_ESCAPE,
80
- ERE_ESCAPED_DELIMITER,
81
- ERE_CLASS_NAME,
82
- ERE_DUP_COUNT,
83
- COMMENT,
84
- CONTINUATION_BACKSLASH,
85
- CONTINUATION_NEWLINE,
86
- STRAY_BACKSLASH,
87
- ERROR_SENTINEL,
10
+ ROOT = 0,
11
+ PRINT_ROOT = 1,
12
+ FOR_IN = 2,
13
+ PRINT_ARGUMENTS = 3,
14
+ BEGIN_CHILD = 4,
15
+ END_CHILD = 5,
16
+ RESUME = 6,
17
+ NEXT_ARGUMENT = 7,
18
+ SAVE_0 = 8,
19
+ SAVE_1 = 9,
20
+ SAVE_2 = 10,
21
+ SAVE_3 = 11,
22
+ RESTORE_0 = 12,
23
+ RESTORE_1 = 13,
24
+ RESTORE_2 = 14,
25
+ RESTORE_3 = 15,
26
+ CLASS_UNARY = 16,
27
+ CLASS_NON_UNARY = 17,
28
+ SELECT_ATOM = 18,
29
+ SELECT_NAME = 19,
30
+ SELECT_GROUP = 20,
31
+ SELECT_CALL = 21,
32
+ SELECT_SUBSCRIPT = 22,
33
+ SELECT_GET = 23,
34
+ SELECT_TARGET_GET = 24,
35
+ SELECT_FILE = 25,
36
+ SELECT_FIELD = 26,
37
+ SELECT_PREFIX = 27,
38
+ SELECT_POSTFIX = 28,
39
+ SELECT_BINARY = 29,
40
+ SELECT_CONCAT = 30,
41
+ SELECT_ASSIGNMENT = 31,
42
+ SELECT_CONDITIONAL = 32,
43
+ SELECT_BRANCHES = 33,
44
+ SELECT_PIPE = 34,
45
+ SELECT_MEMBERSHIP = 35,
46
+ SELECT_TUPLE = 36,
47
+ LOCAL_ROOT = 37,
48
+ LOCAL_PRINT_ROOT = 38,
49
+ GETLINE_WORD = 39,
50
+ FUNC_NAME_WORD = 40,
51
+ DIVISION_SLASH = 41,
52
+ ERE_OPENING_SLASH = 42,
53
+ INCR_OPERATOR = 43,
54
+ DECR_OPERATOR = 44,
55
+ PLUS_OPERATOR = 45,
56
+ MINUS_OPERATOR = 46,
57
+ FIELD_OPERATOR = 47,
58
+ NOT_OPERATOR = 48,
59
+ GETLINE_PIPE = 49,
60
+ ERE_COMPOUND_OPENING = 50,
61
+ ERE_ESCAPED_COMPOUND_OPENING = 51,
62
+ ERE_COMPOUND_SINGLE = 52,
63
+ ERE_COMPOUND_MULTI = 53,
64
+ ERE_DOT_CLOSING = 54,
65
+ ERE_EQUAL_CLOSING = 55,
66
+ ERE_COLON_CLOSING = 56,
67
+ ERE_BRACKET_LITERAL_OPEN = 57,
68
+ ERE_COMPOUND_CONTENT = 58,
69
+ ERE_CLOSING_HYPHEN = 59,
70
+ ERE_CLOSING = 60,
71
+ STRING_OPENING = 61,
72
+ STRING_CLOSING = 62,
73
+ STRING_ESCAPE = 63,
74
+ ERE_NAMED_ESCAPE = 64,
75
+ ERE_QUOTED_ESCAPE = 65,
76
+ ERE_OCTAL_ESCAPE = 66,
77
+ ERE_UNDEFINED_ESCAPE = 67,
78
+ ERE_ESCAPED_DELIMITER = 68,
79
+ COMMENT = 69,
80
+ CONTINUATION_BACKSLASH = 70,
81
+ CONTINUATION_NEWLINE = 71,
82
+ LITERAL_BREAK = 72,
83
+ STRAY_BACKSLASH = 73,
84
+ ERROR_SENTINEL = 74,
88
85
  TOKEN_TYPE_COUNT,
89
86
  };
90
87
 
91
- enum {
92
- MAX_RESERVED_WORD_LENGTH = 8,
93
- SERIALIZED_SCANNER_STATE_SIZE = 1,
94
- };
95
-
96
- typedef char SerializedScannerStateFitsTreeSitterBuffer
97
- [SERIALIZED_SCANNER_STATE_SIZE <= TREE_SITTER_SERIALIZATION_BUFFER_SIZE ? 1
98
- : -1];
88
+ typedef enum {
89
+ WORD_NONE,
90
+ WORD_NAME,
91
+ WORD_KEYWORD,
92
+ WORD_MEMBERSHIP,
93
+ WORD_GETLINE,
94
+ WORD_BUILTIN,
95
+ } WordKind;
99
96
 
100
97
  typedef struct {
101
98
  const char *spelling;
102
- enum TokenType token;
99
+ WordKind kind;
103
100
  } ReservedWord;
104
101
 
105
- typedef struct {
106
- int32_t first;
107
- int32_t second;
108
- enum TokenType token;
109
- } CompositeOperator;
110
-
111
102
  typedef enum {
112
103
  LEXICAL_MODE_OUTSIDE,
113
104
  LEXICAL_MODE_ERE_BODY,
@@ -118,91 +109,115 @@ typedef enum {
118
109
  LEXICAL_MODE_CONTINUED_NEWLINE,
119
110
  } LexicalMode;
120
111
 
112
+ #ifndef BOUNDARY_CACHE_CAPACITY
113
+ #define BOUNDARY_CACHE_CAPACITY 32
114
+ #endif
115
+
116
+ enum {
117
+ STATE_VALUES_OFFSET = 5,
118
+ STATE_CACHE_COUNT_OFFSET = 45,
119
+ STATE_CACHE_INDEX_OFFSET = 46,
120
+ STATE_CACHE_REPEATS_OFFSET = 47,
121
+ STATE_CACHE_DELTA_OFFSET = 51,
122
+ STATE_DECISION_HASH_OFFSET = 55,
123
+ STATE_HEADER_BYTES = 63,
124
+ STATE_OUTLINE_BYTES = 28,
125
+ };
126
+
127
+ typedef char BoundarySerializationFits
128
+ [(STATE_HEADER_BYTES + BOUNDARY_CACHE_CAPACITY * STATE_OUTLINE_BYTES <=
129
+ TREE_SITTER_SERIALIZATION_BUFFER_SIZE)
130
+ ? 1
131
+ : -1];
132
+
133
+ typedef struct {
134
+ uint64_t descriptor, children[2];
135
+ uint32_t repeat;
136
+ } BoundaryOutline;
137
+
138
+ typedef struct {
139
+ BoundaryOutline outline;
140
+ size_t children[2];
141
+ } BoundaryTree;
142
+
143
+ enum BoundaryPhase { BOUNDARY_READY, BOUNDARY_SAVING, BOUNDARY_RESTORING };
144
+ typedef struct {
145
+ uint64_t active, pending[2], next, carry;
146
+ enum BoundaryPhase phase;
147
+ unsigned selected;
148
+ bool unary, print;
149
+ uint64_t decision_hash;
150
+ unsigned cache_count, cache_index;
151
+ uint32_t cache_repeats, cache_delta;
152
+ BoundaryOutline cache[BOUNDARY_CACHE_CAPACITY + 1];
153
+ } BoundaryState;
154
+
155
+ static void boundary_reset(
156
+ BoundaryState *state,
157
+ uint64_t active,
158
+ enum BoundaryPhase phase
159
+ ) {
160
+ memset(state, 0, offsetof(BoundaryState, decision_hash));
161
+ state->active = active;
162
+ state->phase = phase;
163
+ }
164
+
121
165
  typedef struct {
122
166
  LexicalMode mode;
167
+ BoundaryState boundary;
123
168
  } ScannerState;
124
169
 
125
- static const ReservedWord RESERVED_WORDS[] = {
126
- {"BEGIN", BEGIN_KEYWORD},
127
- {"break", BREAK_KEYWORD},
128
- {"continue", CONTINUE_KEYWORD},
129
- {"delete", DELETE_KEYWORD},
130
- {"do", DO_KEYWORD},
131
- {"else", ELSE_KEYWORD},
132
- {"END", END_KEYWORD},
133
- {"exit", EXIT_KEYWORD},
134
- {"for", FOR_KEYWORD},
135
- {"function", FUNCTION_KEYWORD},
136
- {"getline", GETLINE_WORD},
137
- {"if", IF_KEYWORD},
138
- {"in", IN_WORD},
139
- {"next", NEXT_KEYWORD},
140
- {"nextfile", NEXTFILE_KEYWORD},
141
- {"print", PRINT_KEYWORD},
142
- {"printf", PRINTF_KEYWORD},
143
- {"return", RETURN_KEYWORD},
144
- {"while", WHILE_KEYWORD},
145
- {"atan2", BUILTIN_FUNC_NAME_WORD},
146
- {"close", BUILTIN_FUNC_NAME_WORD},
147
- {"cos", BUILTIN_FUNC_NAME_WORD},
148
- {"exp", BUILTIN_FUNC_NAME_WORD},
149
- {"fflush", BUILTIN_FUNC_NAME_WORD},
150
- {"gsub", BUILTIN_FUNC_NAME_WORD},
151
- {"index", BUILTIN_FUNC_NAME_WORD},
152
- {"int", BUILTIN_FUNC_NAME_WORD},
153
- {"length", BUILTIN_FUNC_NAME_WORD},
154
- {"log", BUILTIN_FUNC_NAME_WORD},
155
- {"match", BUILTIN_FUNC_NAME_WORD},
156
- {"rand", BUILTIN_FUNC_NAME_WORD},
157
- {"sin", BUILTIN_FUNC_NAME_WORD},
158
- {"split", BUILTIN_FUNC_NAME_WORD},
159
- {"sprintf", BUILTIN_FUNC_NAME_WORD},
160
- {"sqrt", BUILTIN_FUNC_NAME_WORD},
161
- {"srand", BUILTIN_FUNC_NAME_WORD},
162
- {"sub", BUILTIN_FUNC_NAME_WORD},
163
- {"substr", BUILTIN_FUNC_NAME_WORD},
164
- {"system", BUILTIN_FUNC_NAME_WORD},
165
- {"tolower", BUILTIN_FUNC_NAME_WORD},
166
- {"toupper", BUILTIN_FUNC_NAME_WORD},
167
- };
168
-
169
- static const CompositeOperator COMPOSITE_OPERATORS[] = {
170
- {'+', '=', ADD_ASSIGN_OPERATOR},
171
- {'-', '=', SUB_ASSIGN_OPERATOR},
172
- {'*', '=', MUL_ASSIGN_OPERATOR},
173
- {'%', '=', MOD_ASSIGN_OPERATOR},
174
- {'^', '=', POW_ASSIGN_OPERATOR},
175
- {'|', '|', OR_OPERATOR},
176
- {'&', '&', AND_OPERATOR},
177
- {'!', '~', NO_MATCH_OPERATOR},
178
- {'=', '=', EQ_OPERATOR},
179
- {'<', '=', LE_OPERATOR},
180
- {'>', '=', GE_OPERATOR},
181
- {'!', '=', NE_OPERATOR},
182
- {'+', '+', INCR_OPERATOR},
183
- {'-', '-', DECR_OPERATOR},
184
- {'>', '>', APPEND_OPERATOR},
170
+ #define SAVE_FIRST SAVE_0
171
+ #define RESTORE_FIRST RESTORE_0
172
+ // BEGIN generated reserved words from grammar.js.
173
+ enum {
174
+ MAX_RESERVED_WORD_LENGTH = 8,
185
175
  };
186
176
 
187
- typedef struct {
188
- int32_t character;
189
- enum TokenType token;
190
- } SingleOperator;
191
-
192
- // Redirection precedes comparison where the parser accepts both.
193
- static const SingleOperator SINGLE_OPERATORS[] = {
194
- {'+', PLUS_OPERATOR},
195
- {'-', MINUS_OPERATOR},
196
- {'*', STAR_OPERATOR},
197
- {'%', PERCENT_OPERATOR},
198
- {'^', CARET_OPERATOR},
199
- {'!', BANG_OPERATOR},
200
- {'<', LESS_OPERATOR},
201
- {'>', OUTPUT_GREATER},
202
- {'>', GREATER_OPERATOR},
203
- {'=', EQUAL_OPERATOR},
204
- {'|', PIPE_OPERATOR},
177
+ static const ReservedWord RESERVED_WORDS[] = {
178
+ {"BEGIN", WORD_KEYWORD},
179
+ {"END", WORD_KEYWORD},
180
+ {"break", WORD_KEYWORD},
181
+ {"continue", WORD_KEYWORD},
182
+ {"delete", WORD_KEYWORD},
183
+ {"do", WORD_KEYWORD},
184
+ {"else", WORD_KEYWORD},
185
+ {"exit", WORD_KEYWORD},
186
+ {"for", WORD_KEYWORD},
187
+ {"function", WORD_KEYWORD},
188
+ {"if", WORD_KEYWORD},
189
+ {"next", WORD_KEYWORD},
190
+ {"nextfile", WORD_KEYWORD},
191
+ {"print", WORD_KEYWORD},
192
+ {"printf", WORD_KEYWORD},
193
+ {"return", WORD_KEYWORD},
194
+ {"while", WORD_KEYWORD},
195
+ {"in", WORD_MEMBERSHIP},
196
+ {"getline", WORD_GETLINE},
197
+ {"atan2", WORD_BUILTIN},
198
+ {"close", WORD_BUILTIN},
199
+ {"cos", WORD_BUILTIN},
200
+ {"exp", WORD_BUILTIN},
201
+ {"fflush", WORD_BUILTIN},
202
+ {"gsub", WORD_BUILTIN},
203
+ {"index", WORD_BUILTIN},
204
+ {"int", WORD_BUILTIN},
205
+ {"length", WORD_BUILTIN},
206
+ {"log", WORD_BUILTIN},
207
+ {"match", WORD_BUILTIN},
208
+ {"rand", WORD_BUILTIN},
209
+ {"sin", WORD_BUILTIN},
210
+ {"split", WORD_BUILTIN},
211
+ {"sprintf", WORD_BUILTIN},
212
+ {"sqrt", WORD_BUILTIN},
213
+ {"srand", WORD_BUILTIN},
214
+ {"sub", WORD_BUILTIN},
215
+ {"substr", WORD_BUILTIN},
216
+ {"system", WORD_BUILTIN},
217
+ {"tolower", WORD_BUILTIN},
218
+ {"toupper", WORD_BUILTIN},
205
219
  };
220
+ // END generated reserved words.
206
221
 
207
222
  static bool is_ascii_blank(int32_t character) {
208
223
  return character == ' ' || character == '\t';
@@ -255,23 +270,48 @@ static bool emit_mode(
255
270
  return emit(lexer, token);
256
271
  }
257
272
 
258
- static enum TokenType classify_word(const char *word, size_t length) {
273
+ static bool emit_delimiter(
274
+ ScannerState *state,
275
+ TSLexer *lexer,
276
+ LexicalMode mode,
277
+ enum TokenType token
278
+ ) {
279
+ lexer->advance(lexer, false);
280
+ lexer->mark_end(lexer);
281
+ return emit_mode(state, lexer, mode, token);
282
+ }
283
+
284
+ static size_t doubled_capacity(size_t capacity, size_t size, size_t initial) {
285
+ const size_t count = capacity ? capacity * 2 : initial;
286
+ return count <= capacity || count > SIZE_MAX / size ? 0 : count;
287
+ }
288
+
289
+ static void *
290
+ grow_array(void *items, size_t *capacity, size_t size, size_t initial) {
291
+ const size_t count = doubled_capacity(*capacity, size, initial);
292
+ void *grown = count ? ts_realloc(items, count * size) : NULL;
293
+ if (grown) *capacity = count;
294
+ return grown;
295
+ }
296
+
297
+ static WordKind classify_word(const char *word, size_t length) {
259
298
  if (length > MAX_RESERVED_WORD_LENGTH) {
260
- return NAME_WORD;
299
+ return WORD_NAME;
261
300
  }
262
301
  for (size_t i = 0; i < ARRAY_LENGTH(RESERVED_WORDS); i++) {
263
- if (strcmp(RESERVED_WORDS[i].spelling, word) == 0) {
264
- return RESERVED_WORDS[i].token;
302
+ const char *spelling = RESERVED_WORDS[i].spelling;
303
+ if (spelling[0] == word[0] && strcmp(spelling, word) == 0) {
304
+ return RESERVED_WORDS[i].kind;
265
305
  }
266
306
  }
267
- return NAME_WORD;
307
+ return WORD_NAME;
268
308
  }
269
309
 
270
- static enum TokenType scan_word_spelling(TSLexer *lexer) {
310
+ static WordKind scan_word_spelling(TSLexer *lexer) {
271
311
  char spelling[MAX_RESERVED_WORD_LENGTH + 1] = {0};
272
312
  size_t length = 0;
273
313
  if (!is_word_start(lexer->lookahead)) {
274
- return TOKEN_TYPE_COUNT;
314
+ return WORD_NONE;
275
315
  }
276
316
  do {
277
317
  if (length < MAX_RESERVED_WORD_LENGTH) {
@@ -301,121 +341,59 @@ static bool skip_token_layout(TSLexer *lexer) {
301
341
  }
302
342
  }
303
343
 
304
- static bool scan_for_in_shape(TSLexer *lexer) {
305
- if (!skip_token_layout(lexer) || scan_word_spelling(lexer) != IN_WORD) {
306
- return false;
307
- }
308
- if (!skip_token_layout(lexer) || scan_word_spelling(lexer) != NAME_WORD) {
344
+ static bool scan_sign(TSLexer *lexer, const bool *valid_symbols) {
345
+ const int32_t first = lexer->lookahead;
346
+ if (first != '+' && first != '-') {
309
347
  return false;
310
348
  }
311
- return skip_token_layout(lexer) && lexer->lookahead == ')';
312
- }
313
-
314
- static enum TokenType
315
- promote_word(TSLexer *lexer, const bool *valid_symbols, enum TokenType token) {
316
- switch (token) {
317
- case NAME_WORD:
318
- if (lexer->lookahead == '(') {
319
- return FUNC_NAME_WORD;
320
- }
321
- if (valid_symbols[FOR_IN_VARIABLE_WORD] && scan_for_in_shape(lexer)) {
322
- return FOR_IN_VARIABLE_WORD;
323
- }
324
- return token;
325
- case BUILTIN_FUNC_NAME_WORD:
326
- return skip_token_layout(lexer) && lexer->lookahead == '('
327
- ? BUILTIN_CALL_WORD
328
- : token;
329
- default:
330
- return token;
331
- }
332
- }
333
-
334
- static bool scan_word_token(TSLexer *lexer, const bool *valid_symbols) {
335
- enum TokenType token = scan_word_spelling(lexer);
349
+ lexer->advance(lexer, false);
336
350
  lexer->mark_end(lexer);
337
- token = promote_word(lexer, valid_symbols, token);
338
- return valid_symbols[token] && emit(lexer, token);
339
- }
340
-
341
- static bool scan_number_token(TSLexer *lexer) {
342
- bool digits = false;
343
- bool floating = false;
344
- while (is_ascii_digit(lexer->lookahead)) {
345
- digits = true;
346
- lexer->advance(lexer, false);
347
- }
348
- if (lexer->lookahead == '.') {
349
- floating = true;
350
- lexer->advance(lexer, false);
351
- while (is_ascii_digit(lexer->lookahead)) {
352
- digits = true;
353
- lexer->advance(lexer, false);
354
- }
355
- }
356
- if (!digits) {
351
+ if (lexer->lookahead == '=') {
357
352
  return false;
358
353
  }
359
- lexer->mark_end(lexer);
360
- if (lexer->lookahead == 'e' || lexer->lookahead == 'E') {
361
- lexer->advance(lexer, false);
362
- if (lexer->lookahead == '+' || lexer->lookahead == '-') {
363
- lexer->advance(lexer, false);
364
- }
365
- if (!is_ascii_digit(lexer->lookahead)) {
366
- return emit(lexer, NUMBER);
367
- }
368
- floating = true;
369
- do {
370
- lexer->advance(lexer, false);
371
- } while (is_ascii_digit(lexer->lookahead));
372
- lexer->mark_end(lexer);
373
- }
374
- if (floating && character_in(lexer->lookahead, "fFlL")) {
354
+ if (lexer->lookahead == first) {
375
355
  lexer->advance(lexer, false);
376
356
  lexer->mark_end(lexer);
357
+ const enum TokenType token = first == '+' ? INCR_OPERATOR : DECR_OPERATOR;
358
+ return valid_symbols[token] && emit(lexer, token);
377
359
  }
378
- return emit(lexer, NUMBER);
360
+ const enum TokenType token = first == '+' ? PLUS_OPERATOR : MINUS_OPERATOR;
361
+ return valid_symbols[token] && emit(lexer, token);
379
362
  }
380
363
 
381
- static const CompositeOperator *
382
- find_composite_operator(int32_t first, int32_t second) {
383
- for (size_t i = 0; i < ARRAY_LENGTH(COMPOSITE_OPERATORS); i++) {
384
- const CompositeOperator *entry = &COMPOSITE_OPERATORS[i];
385
- if (entry->first == first && entry->second == second) {
386
- return entry;
387
- }
388
- }
389
- return NULL;
364
+ static bool scan_contextual_word(TSLexer *lexer, bool recovering) {
365
+ WordKind word = scan_word_spelling(lexer);
366
+ lexer->mark_end(lexer);
367
+ if (word == WORD_NAME && lexer->lookahead == '(')
368
+ return emit(lexer, FUNC_NAME_WORD);
369
+ if (recovering && (word == WORD_NAME || word == WORD_GETLINE))
370
+ return emit(lexer, ERROR_SENTINEL);
371
+ return word == WORD_GETLINE && emit(lexer, GETLINE_WORD);
390
372
  }
391
373
 
392
- static const SingleOperator *
393
- find_single_operator(int32_t character, const bool *valid_symbols) {
394
- for (size_t i = 0; i < ARRAY_LENGTH(SINGLE_OPERATORS); i++) {
395
- const SingleOperator *entry = &SINGLE_OPERATORS[i];
396
- if (entry->character == character && valid_symbols[entry->token]) {
397
- return entry;
398
- }
399
- }
400
- return NULL;
374
+ static bool
375
+ scan_prefix_operator(TSLexer *lexer, const bool *valid, bool recovering) {
376
+ int32_t first = lexer->lookahead;
377
+ lexer->advance(lexer, false);
378
+ lexer->mark_end(lexer);
379
+ if (first == '!' && (lexer->lookahead == '=' || lexer->lookahead == '~'))
380
+ return false;
381
+ if (recovering) return emit(lexer, ERROR_SENTINEL);
382
+ enum TokenType token = first == '$' ? FIELD_OPERATOR : NOT_OPERATOR;
383
+ return valid[token] && emit(lexer, token);
401
384
  }
402
385
 
403
- static bool scan_operator(TSLexer *lexer, const bool *valid_symbols) {
404
- const int32_t first = lexer->lookahead;
386
+ // Read past output pipes so edits invalidate reused nodes.
387
+ static bool
388
+ scan_getline_pipe(TSLexer *lexer, const bool *valid_symbols, bool recovering) {
405
389
  lexer->advance(lexer, false);
406
390
  lexer->mark_end(lexer);
407
- const CompositeOperator *composite =
408
- find_composite_operator(first, lexer->lookahead);
409
- if (composite != NULL) {
410
- if (!valid_symbols[composite->token]) {
411
- return false;
412
- }
413
- lexer->advance(lexer, false);
414
- lexer->mark_end(lexer);
415
- return emit(lexer, composite->token);
416
- }
417
- const SingleOperator *single = find_single_operator(first, valid_symbols);
418
- return single != NULL && emit(lexer, single->token);
391
+ const bool getline =
392
+ skip_token_layout(lexer) && scan_word_spelling(lexer) == WORD_GETLINE;
393
+ return getline &&
394
+ !recovering &&
395
+ valid_symbols[GETLINE_PIPE] &&
396
+ emit(lexer, GETLINE_PIPE);
419
397
  }
420
398
 
421
399
  static bool scan_slash_start(
@@ -425,25 +403,19 @@ static bool scan_slash_start(
425
403
  ) {
426
404
  lexer->advance(lexer, false);
427
405
  lexer->mark_end(lexer);
428
- if (lexer->lookahead == '=') {
429
- if (valid_symbols[DIV_ASSIGN_OPERATOR]) {
430
- lexer->advance(lexer, false);
431
- lexer->mark_end(lexer);
432
- return emit(lexer, DIV_ASSIGN_OPERATOR);
433
- }
434
- if (valid_symbols[DIVISION_SLASH]) {
435
- return false;
436
- }
437
- }
438
406
  if (valid_symbols[DIVISION_SLASH]) {
439
- return emit(lexer, DIVISION_SLASH);
407
+ return lexer->lookahead != '=' && emit(lexer, DIVISION_SLASH);
440
408
  }
441
409
  return valid_symbols[ERE_OPENING_SLASH] &&
442
410
  emit_mode(state, lexer, LEXICAL_MODE_ERE_BODY, ERE_OPENING_SLASH);
443
411
  }
444
412
 
445
413
  static bool scan_escape(const ScannerState *state, TSLexer *lexer) {
446
- if (lexer->eof(lexer) || lexer->lookahead == '\n') {
414
+ if (
415
+ lexer->eof(lexer) ||
416
+ lexer->lookahead == '\n' ||
417
+ (lexer->lookahead == 0 && is_ere_mode(state->mode))
418
+ ) {
447
419
  return emit(lexer, STRAY_BACKSLASH);
448
420
  }
449
421
  const int32_t character = lexer->lookahead;
@@ -473,9 +445,8 @@ static bool scan_escape(const ScannerState *state, TSLexer *lexer) {
473
445
  return emit(lexer, token);
474
446
  }
475
447
 
476
- // Every backslash must be emitted to prevent fallback to the internal marker.
477
- static bool
478
- scan_backslash(ScannerState *state, TSLexer *lexer, bool recovering) {
448
+ // Emit every backslash to prevent fallback to the internal marker.
449
+ static bool scan_backslash(ScannerState *state, TSLexer *lexer) {
479
450
  lexer->advance(lexer, false);
480
451
  lexer->mark_end(lexer);
481
452
  if (state->mode == LEXICAL_MODE_OUTSIDE) {
@@ -489,28 +460,7 @@ scan_backslash(ScannerState *state, TSLexer *lexer, bool recovering) {
489
460
  CONTINUATION_BACKSLASH
490
461
  );
491
462
  }
492
- return !recovering && scan_escape(state, lexer);
493
- }
494
-
495
- static bool scan_string_content(TSLexer *lexer) {
496
- bool consumed = false;
497
- while (!lexer->eof(lexer) && !character_in(lexer->lookahead, "\"\\\n")) {
498
- consumed = true;
499
- lexer->advance(lexer, false);
500
- }
501
- lexer->mark_end(lexer);
502
- return consumed && emit(lexer, STRING_CONTENT);
503
- }
504
-
505
- static bool scan_ere_spelling(TSLexer *lexer, enum TokenType token) {
506
- do {
507
- lexer->advance(lexer, false);
508
- } while (
509
- is_ascii_digit(lexer->lookahead) ||
510
- (token == ERE_CLASS_NAME && is_ascii_letter(lexer->lookahead))
511
- );
512
- lexer->mark_end(lexer);
513
- return emit(lexer, token);
463
+ return scan_escape(state, lexer);
514
464
  }
515
465
 
516
466
  typedef struct {
@@ -527,6 +477,75 @@ static const EreCompoundToken ERE_COMPOUND_TOKENS[] = {
527
477
  {ERE_COLON_CLOSING, ERE_COMPOUND_CONTENT, ':', LEXICAL_MODE_ERE_CLASS},
528
478
  };
529
479
 
480
+ static bool compound_has_escape(TSLexer *lexer, int32_t marker) {
481
+ lexer->advance(lexer, false);
482
+ while (
483
+ !lexer->eof(lexer) && lexer->lookahead != '\n' && lexer->lookahead != '/'
484
+ ) {
485
+ const int32_t character = lexer->lookahead;
486
+ if (character == '\\') {
487
+ return true;
488
+ }
489
+ lexer->advance(lexer, false);
490
+ if (character == marker && lexer->lookahead == ']') {
491
+ break;
492
+ }
493
+ }
494
+ return false;
495
+ }
496
+
497
+ static bool skip_blanks_before_newline(TSLexer *lexer, unsigned *count) {
498
+ while (is_ascii_blank(lexer->lookahead)) {
499
+ lexer->advance(lexer, false);
500
+ if (count) (*count)++;
501
+ }
502
+ return lexer->lookahead == '\n';
503
+ }
504
+
505
+ static bool emit_literal_break(
506
+ ScannerState *state,
507
+ TSLexer *lexer,
508
+ const bool *valid_symbols
509
+ ) {
510
+ return valid_symbols[LITERAL_BREAK] &&
511
+ emit_mode(state, lexer, LEXICAL_MODE_OUTSIDE, LITERAL_BREAK);
512
+ }
513
+
514
+ static bool scan_ere_collating_element(
515
+ ScannerState *state,
516
+ TSLexer *lexer,
517
+ const bool *valid_symbols
518
+ ) {
519
+ const int32_t marker = state->mode == LEXICAL_MODE_ERE_COLLATING ? '.' : '=';
520
+ const int32_t first = lexer->lookahead;
521
+ unsigned count = 0;
522
+ if (skip_blanks_before_newline(lexer, &count)) {
523
+ return emit_literal_break(state, lexer, valid_symbols);
524
+ }
525
+ while (
526
+ lexer->lookahead != 0 &&
527
+ lexer->lookahead != '\n' &&
528
+ lexer->lookahead != '/' &&
529
+ lexer->lookahead != '\\'
530
+ ) {
531
+ lexer->mark_end(lexer);
532
+ const int32_t character = lexer->lookahead;
533
+ lexer->advance(lexer, false);
534
+ if (character == marker && lexer->lookahead == ']') {
535
+ if (count == 0 || (count == 1 && character_in(first, "-]"))) {
536
+ return false;
537
+ }
538
+ const enum TokenType token =
539
+ count == 1 ? ERE_COMPOUND_SINGLE : ERE_COMPOUND_MULTI;
540
+ return valid_symbols[token] && emit(lexer, token);
541
+ }
542
+ if (count < 2) {
543
+ count++;
544
+ }
545
+ }
546
+ return false;
547
+ }
548
+
530
549
  static bool scan_ere_compound_token(
531
550
  ScannerState *state,
532
551
  TSLexer *lexer,
@@ -542,10 +561,14 @@ static bool scan_ere_compound_token(
542
561
  if (!terminates) {
543
562
  return valid_symbols[token->content] && emit(lexer, token->content);
544
563
  }
545
- if (!valid_symbols[token->delimiter]) {
564
+ if (
565
+ !valid_symbols[token->delimiter] &&
566
+ !(opening && valid_symbols[ERE_ESCAPED_COMPOUND_OPENING])
567
+ ) {
546
568
  return false;
547
569
  }
548
570
  LexicalMode mode = LEXICAL_MODE_ERE_BODY;
571
+ enum TokenType delimiter = token->delimiter;
549
572
  if (opening) {
550
573
  for (size_t i = 1; i < ARRAY_LENGTH(ERE_COMPOUND_TOKENS); i++) {
551
574
  if (ERE_COMPOUND_TOKENS[i].first == lexer->lookahead) {
@@ -553,8 +576,14 @@ static bool scan_ere_compound_token(
553
576
  break;
554
577
  }
555
578
  }
579
+ if (
580
+ mode != LEXICAL_MODE_ERE_CLASS &&
581
+ compound_has_escape(lexer, lexer->lookahead)
582
+ ) {
583
+ delimiter = ERE_ESCAPED_COMPOUND_OPENING;
584
+ }
556
585
  }
557
- return emit_mode(state, lexer, mode, token->delimiter);
586
+ return valid_symbols[delimiter] && emit_mode(state, lexer, mode, delimiter);
558
587
  }
559
588
 
560
589
  static bool scan_ere_context(
@@ -564,30 +593,29 @@ static bool scan_ere_context(
564
593
  bool recovering
565
594
  ) {
566
595
  if (!recovering) {
596
+ if (
597
+ (state->mode == LEXICAL_MODE_ERE_COLLATING ||
598
+ state->mode == LEXICAL_MODE_ERE_EQUIVALENCE) &&
599
+ (valid_symbols[ERE_COMPOUND_SINGLE] || valid_symbols[ERE_COMPOUND_MULTI])
600
+ ) {
601
+ return scan_ere_collating_element(state, lexer, valid_symbols);
602
+ }
567
603
  for (size_t i = 0; i < ARRAY_LENGTH(ERE_COMPOUND_TOKENS); i++) {
568
604
  const EreCompoundToken *token = &ERE_COMPOUND_TOKENS[i];
569
605
  if (lexer->lookahead == token->first) {
570
606
  return scan_ere_compound_token(state, lexer, token, valid_symbols);
571
607
  }
572
608
  }
573
- if (valid_symbols[ERE_CLASS_NAME] && is_ascii_letter(lexer->lookahead)) {
574
- return scan_ere_spelling(lexer, ERE_CLASS_NAME);
575
- }
576
- if (valid_symbols[ERE_DUP_COUNT] && is_ascii_digit(lexer->lookahead)) {
577
- return scan_ere_spelling(lexer, ERE_DUP_COUNT);
578
- }
579
- }
580
- if (lexer->lookahead == '/' && valid_symbols[ERE_CLOSING]) {
581
- lexer->advance(lexer, false);
582
- lexer->mark_end(lexer);
583
- return emit_mode(state, lexer, LEXICAL_MODE_OUTSIDE, ERE_CLOSING);
584
609
  }
610
+ if (lexer->lookahead == '/' && valid_symbols[ERE_CLOSING])
611
+ return emit_delimiter(state, lexer, LEXICAL_MODE_OUTSIDE, ERE_CLOSING);
585
612
  if (lexer->lookahead == '-' && valid_symbols[ERE_CLOSING_HYPHEN]) {
586
613
  lexer->advance(lexer, false);
587
614
  lexer->mark_end(lexer);
588
615
  return lexer->lookahead == ']' && emit(lexer, ERE_CLOSING_HYPHEN);
589
616
  }
590
- return false;
617
+ return skip_blanks_before_newline(lexer, NULL) &&
618
+ emit_literal_break(state, lexer, valid_symbols);
591
619
  }
592
620
 
593
621
  static bool scan_comment(TSLexer *lexer) {
@@ -598,99 +626,2520 @@ static bool scan_comment(TSLexer *lexer) {
598
626
  return emit(lexer, COMMENT);
599
627
  }
600
628
 
601
- void *tree_sitter_posix_awk_external_scanner_create(void) {
602
- return ts_calloc(1, sizeof(ScannerState));
603
- }
604
-
605
- void tree_sitter_posix_awk_external_scanner_destroy(void *payload) {
606
- ts_free(payload);
607
- }
608
-
609
- unsigned
610
- tree_sitter_posix_awk_external_scanner_serialize(void *payload, char *buffer) {
611
- const ScannerState *state = payload;
612
- if (state->mode == LEXICAL_MODE_OUTSIDE) {
613
- return 0;
614
- }
615
- buffer[0] = (char)state->mode;
616
- return SERIALIZED_SCANNER_STATE_SIZE;
617
- }
618
-
619
- void tree_sitter_posix_awk_external_scanner_deserialize(
620
- void *payload,
621
- const char *buffer,
622
- unsigned length
623
- ) {
624
- ScannerState *state = payload;
625
- *state = (ScannerState){0};
626
- if (length != SERIALIZED_SCANNER_STATE_SIZE) {
627
- return;
628
- }
629
- const unsigned char mode = (unsigned char)buffer[0];
630
- if (mode <= LEXICAL_MODE_CONTINUED_NEWLINE) {
631
- state->mode = (LexicalMode)mode;
629
+ static bool skip_literal(TSLexer *lexer) {
630
+ const int32_t delimiter = lexer->lookahead;
631
+ for (;;) {
632
+ lexer->advance(lexer, false);
633
+ if (lexer->lookahead == '\\') {
634
+ lexer->advance(lexer, false);
635
+ } else if (lexer->lookahead == delimiter) {
636
+ lexer->advance(lexer, false);
637
+ return true;
638
+ }
639
+ if (lexer->eof(lexer) || lexer->lookahead == '\n') {
640
+ return false;
641
+ }
632
642
  }
633
643
  }
634
644
 
635
- bool tree_sitter_posix_awk_external_scanner_scan(
636
- void *payload,
637
- TSLexer *lexer,
638
- const bool *valid_symbols
639
- ) {
640
- ScannerState *state = payload;
641
- const bool recovering = valid_symbols[ERROR_SENTINEL];
642
- if (state->mode == LEXICAL_MODE_CONTINUED_NEWLINE) {
643
- if (lexer->lookahead != '\n' || !valid_symbols[CONTINUATION_NEWLINE]) {
644
- return false;
645
- }
645
+ static bool skip_number(TSLexer *lexer) {
646
+ bool digits = false;
647
+ bool floating = false;
648
+ while (is_ascii_digit(lexer->lookahead)) {
649
+ digits = true;
646
650
  lexer->advance(lexer, false);
647
- lexer->mark_end(lexer);
648
- return emit_mode(state, lexer, LEXICAL_MODE_OUTSIDE, CONTINUATION_NEWLINE);
649
651
  }
650
- if (state->mode == LEXICAL_MODE_OUTSIDE) {
651
- while (is_ascii_blank(lexer->lookahead)) {
652
- lexer->advance(lexer, true);
652
+ if (lexer->lookahead == '.') {
653
+ floating = true;
654
+ lexer->advance(lexer, false);
655
+ while (is_ascii_digit(lexer->lookahead)) {
656
+ digits = true;
657
+ lexer->advance(lexer, false);
653
658
  }
654
659
  }
655
- lexer->mark_end(lexer);
656
- if (lexer->lookahead == '\\') {
657
- return scan_backslash(state, lexer, recovering);
658
- }
659
- if (is_ere_mode(state->mode)) {
660
- return scan_ere_context(state, lexer, valid_symbols, recovering);
660
+ if (!digits) {
661
+ return false;
661
662
  }
662
- if (state->mode == LEXICAL_MODE_STRING) {
663
- if (lexer->lookahead == '"' && valid_symbols[STRING_CLOSING]) {
663
+ lexer->mark_end(lexer);
664
+ if (lexer->lookahead == 'e' || lexer->lookahead == 'E') {
665
+ lexer->advance(lexer, false);
666
+ if (lexer->lookahead == '+' || lexer->lookahead == '-') {
664
667
  lexer->advance(lexer, false);
665
- lexer->mark_end(lexer);
666
- return emit_mode(state, lexer, LEXICAL_MODE_OUTSIDE, STRING_CLOSING);
667
668
  }
668
- return !recovering &&
669
- valid_symbols[STRING_CONTENT] &&
670
- scan_string_content(lexer);
671
- }
672
- if (lexer->lookahead == '#' && valid_symbols[COMMENT]) {
673
- return scan_comment(lexer);
669
+ if (!is_ascii_digit(lexer->lookahead)) {
670
+ return true;
671
+ }
672
+ floating = true;
673
+ do {
674
+ lexer->advance(lexer, false);
675
+ } while (is_ascii_digit(lexer->lookahead));
676
+ lexer->mark_end(lexer);
674
677
  }
675
- if (lexer->lookahead == '"' && valid_symbols[STRING_OPENING]) {
678
+ if (floating && character_in(lexer->lookahead, "fFlL")) {
676
679
  lexer->advance(lexer, false);
677
680
  lexer->mark_end(lexer);
678
- return emit_mode(state, lexer, LEXICAL_MODE_STRING, STRING_OPENING);
679
- }
680
- if (recovering) {
681
- return false;
682
- }
683
- if (is_word_start(lexer->lookahead)) {
684
- return scan_word_token(lexer, valid_symbols);
685
- }
686
- if (
687
- (is_ascii_digit(lexer->lookahead) || lexer->lookahead == '.') &&
688
- valid_symbols[NUMBER]
689
- ) {
690
- return scan_number_token(lexer);
691
681
  }
692
- if (lexer->lookahead == '/') {
693
- return scan_slash_start(state, lexer, valid_symbols);
682
+ return true;
683
+ }
684
+
685
+ static bool skip_scope_layout(TSLexer *lexer, bool separated) {
686
+ for (;;) {
687
+ const int32_t character = lexer->lookahead;
688
+ if (character == '#') {
689
+ do {
690
+ lexer->advance(lexer, false);
691
+ } while (!lexer->eof(lexer) && lexer->lookahead != '\n');
692
+ } else if (is_ascii_blank(character) || (character == '\n' && separated)) {
693
+ lexer->advance(lexer, false);
694
+ } else if (character == '\\') {
695
+ lexer->advance(lexer, false);
696
+ if (lexer->lookahead != '\n') return false;
697
+ lexer->advance(lexer, false);
698
+ } else {
699
+ return true;
700
+ }
701
+ }
702
+ }
703
+
704
+ static bool skip_group(TSLexer *lexer, bool *tuple) {
705
+ if (tuple) *tuple = false;
706
+ bool after_operand = false;
707
+ bool separated = false;
708
+ unsigned depth = 0;
709
+ do {
710
+ if (
711
+ !skip_scope_layout(lexer, separated) ||
712
+ lexer->eof(lexer) ||
713
+ lexer->lookahead == '\n'
714
+ )
715
+ return false;
716
+ const int32_t character = lexer->lookahead;
717
+ separated = false;
718
+ if (tuple && character == ',' && depth == 1) *tuple = true;
719
+ if (character == '"' || (character == '/' && !after_operand)) {
720
+ if (!skip_literal(lexer)) {
721
+ return false;
722
+ }
723
+ after_operand = true;
724
+ continue;
725
+ }
726
+ if (is_word_start(character)) {
727
+ scan_word_spelling(lexer);
728
+ after_operand = true;
729
+ continue;
730
+ }
731
+ lexer->advance(lexer, false);
732
+ if (character_in(character, "([")) {
733
+ depth++;
734
+ after_operand = false;
735
+ } else if (character_in(character, ")]")) {
736
+ depth--;
737
+ after_operand = true;
738
+ } else if (character_in(character, "+-") && lexer->lookahead == character) {
739
+ // A slash after an update divides: prefix updates require an lvalue.
740
+ lexer->advance(lexer, false);
741
+ after_operand = true;
742
+ } else if (character_in(character, "&|") && lexer->lookahead == character) {
743
+ lexer->advance(lexer, false);
744
+ after_operand = false;
745
+ separated = true;
746
+ } else {
747
+ after_operand = is_word_continue(character) || character == '.';
748
+ separated = character == ',';
749
+ }
750
+ } while (depth > 0);
751
+ return true;
752
+ }
753
+
754
+ typedef enum { ATOM_PLAIN, ATOM_CALL, ATOM_SUBSCRIPT } WordAtom;
755
+
756
+ static WordAtom word_atom(TSLexer *lexer, WordKind word) {
757
+ const bool adjacent = lexer->lookahead == '(';
758
+ if (!skip_token_layout(lexer)) return ATOM_PLAIN;
759
+ if (lexer->lookahead == '(' && (adjacent || word == WORD_BUILTIN))
760
+ return ATOM_CALL;
761
+ if (lexer->lookahead == '[' && word == WORD_NAME) return ATOM_SUBSCRIPT;
762
+ return ATOM_PLAIN;
763
+ }
764
+
765
+ // Preserve the source lexer's furthest lookahead when restoring this cursor.
766
+ typedef struct {
767
+ TSLexer lexer;
768
+ TSLexer *source;
769
+ int32_t *characters;
770
+ size_t length;
771
+ size_t capacity;
772
+ size_t position;
773
+ size_t marked_end;
774
+ bool failed;
775
+ } InputProbe;
776
+
777
+ static void probe_restore(InputProbe *probe, size_t position) {
778
+ probe->position = position;
779
+ probe->lexer.lookahead = probe->failed ? 0
780
+ : position < probe->length ? probe->characters[position]
781
+ : probe->source->lookahead;
782
+ }
783
+
784
+ static bool probe_eof(const TSLexer *lexer) {
785
+ const InputProbe *probe = (const InputProbe *)lexer;
786
+ return probe->failed ||
787
+ (probe->position == probe->length && probe->source->eof(probe->source));
788
+ }
789
+
790
+ static void probe_advance(TSLexer *lexer, bool skip) {
791
+ InputProbe *probe = (InputProbe *)lexer;
792
+ (void)skip;
793
+ if (probe_eof(lexer)) {
794
+ return;
795
+ }
796
+ if (probe->position == probe->length) {
797
+ if (probe->length == probe->capacity) {
798
+ int32_t *characters = grow_array(
799
+ probe->characters,
800
+ &probe->capacity,
801
+ sizeof(*characters),
802
+ 64
803
+ );
804
+ if (!characters) {
805
+ probe->failed = true;
806
+ lexer->lookahead = 0;
807
+ return;
808
+ }
809
+ probe->characters = characters;
810
+ }
811
+ probe->characters[probe->length++] = lexer->lookahead;
812
+ probe->source->advance(probe->source, false);
813
+ }
814
+ probe_restore(probe, probe->position + 1);
815
+ }
816
+
817
+ static void probe_mark_end(TSLexer *lexer) {
818
+ InputProbe *probe = (InputProbe *)lexer;
819
+ probe->marked_end = probe->position;
820
+ }
821
+
822
+ static InputProbe probe_open(TSLexer *source) {
823
+ return (InputProbe){
824
+ .lexer =
825
+ {.lookahead = source->lookahead,
826
+ .advance = probe_advance,
827
+ .mark_end = probe_mark_end,
828
+ .eof = probe_eof},
829
+ .source = source,
830
+ };
831
+ }
832
+
833
+ enum ProbeToken {
834
+ PROBE_END = 0,
835
+ PROBE_VALUE = 256,
836
+ PROBE_TUPLE,
837
+ PROBE_NAME,
838
+ PROBE_GETLINE,
839
+ PROBE_UPDATE,
840
+ PROBE_ASSIGNMENT,
841
+ PROBE_COMPARISON,
842
+ PROBE_MATCH,
843
+ PROBE_MEMBERSHIP,
844
+ PROBE_AND,
845
+ PROBE_OR,
846
+ };
847
+
848
+ static int probe_token(InputProbe *probe, bool expect_operand) {
849
+ TSLexer *lexer = &probe->lexer;
850
+ if (!skip_token_layout(lexer) || probe_eof(lexer)) {
851
+ return PROBE_END;
852
+ }
853
+ const int32_t character = lexer->lookahead;
854
+ if (is_ascii_digit(character) || character == '.') {
855
+ if (!skip_number(lexer)) {
856
+ return PROBE_END;
857
+ }
858
+ probe_restore(probe, probe->marked_end);
859
+ return PROBE_VALUE;
860
+ }
861
+ if (character == '"' || (expect_operand && character == '/')) {
862
+ return skip_literal(lexer) ? PROBE_VALUE : PROBE_END;
863
+ }
864
+ if (character == '(') {
865
+ bool tuple = false;
866
+ return skip_group(lexer, &tuple) ? (tuple ? PROBE_TUPLE : PROBE_VALUE)
867
+ : PROBE_END;
868
+ }
869
+ if (is_word_start(character)) {
870
+ const WordKind word = scan_word_spelling(lexer);
871
+ if (word == WORD_GETLINE) {
872
+ return PROBE_GETLINE;
873
+ }
874
+ if (word == WORD_MEMBERSHIP) {
875
+ return PROBE_MEMBERSHIP;
876
+ }
877
+ if (word != WORD_NAME && word != WORD_BUILTIN) {
878
+ return PROBE_END;
879
+ }
880
+ const WordAtom atom = word_atom(lexer, word);
881
+ if (atom != ATOM_PLAIN && !skip_group(lexer, NULL)) {
882
+ return PROBE_END;
883
+ }
884
+ return atom == ATOM_SUBSCRIPT || (atom == ATOM_PLAIN && word == WORD_NAME)
885
+ ? PROBE_NAME
886
+ : PROBE_VALUE;
887
+ }
888
+ lexer->advance(lexer, false);
889
+ if (character_in(character, "+-") && lexer->lookahead == character) {
890
+ lexer->advance(lexer, false);
891
+ return PROBE_UPDATE;
892
+ }
893
+ if (character_in(character, "&|") && lexer->lookahead == character) {
894
+ lexer->advance(lexer, false);
895
+ if (!skip_scope_layout(lexer, true)) {
896
+ return PROBE_END;
897
+ }
898
+ return character == '&' ? PROBE_AND : PROBE_OR;
899
+ }
900
+ if (character_in(character, "+-*/%^=<>!") && lexer->lookahead == '=') {
901
+ lexer->advance(lexer, false);
902
+ return character_in(character, "=<>!") ? PROBE_COMPARISON
903
+ : PROBE_ASSIGNMENT;
904
+ }
905
+ if (character == '!' && lexer->lookahead == '~') {
906
+ lexer->advance(lexer, false);
907
+ return PROBE_MATCH;
908
+ }
909
+ return character;
910
+ }
911
+
912
+ static bool probe_print_arguments(InputProbe *input) {
913
+ const size_t start = input->position;
914
+ bool tuple = false;
915
+ const bool arguments = input->lexer.lookahead == '(' &&
916
+ skip_group(&input->lexer, &tuple) &&
917
+ tuple &&
918
+ probe_token(input, false) != PROBE_MEMBERSHIP;
919
+ probe_restore(input, start);
920
+ return arguments;
921
+ }
922
+
923
+ // BEGIN generated expression rules from grammar.js.
924
+ enum ExpressionPrecedence {
925
+ EXPR_ASSIGNMENT = 2,
926
+ EXPR_CONDITIONAL = 3,
927
+ EXPR_LOGICAL_OR = 4,
928
+ EXPR_LOGICAL_AND = 6,
929
+ EXPR_MEMBERSHIP = 8,
930
+ EXPR_MATCH = 10,
931
+ EXPR_COMPARISON = 12,
932
+ EXPR_CONCATENATION = 14,
933
+ EXPR_ADDITIVE = 16,
934
+ EXPR_MULTIPLICATIVE = 18,
935
+ EXPR_UNARY = 20,
936
+ EXPR_EXPONENTIATION = 21,
937
+ EXPR_POSTFIX = 23,
938
+ EXPR_FIELD = 24,
939
+ EXPR_FILE_SOURCE = EXPR_ADDITIVE,
940
+ };
941
+
942
+ static int expression_right_precedence(int precedence) {
943
+ switch (precedence) {
944
+ case EXPR_ASSIGNMENT:
945
+ return EXPR_ASSIGNMENT;
946
+ case EXPR_CONDITIONAL:
947
+ return EXPR_CONDITIONAL;
948
+ case EXPR_UNARY:
949
+ return EXPR_UNARY;
950
+ case EXPR_EXPONENTIATION:
951
+ return EXPR_UNARY;
952
+ case EXPR_FIELD:
953
+ return EXPR_FIELD;
954
+ default:
955
+ return precedence + 1;
956
+ }
957
+ }
958
+
959
+ static bool expression_non_associative(int precedence) {
960
+ switch (precedence) {
961
+ case EXPR_MATCH:
962
+ case EXPR_COMPARISON:
963
+ return true;
964
+ default:
965
+ return false;
966
+ }
967
+ }
968
+ // END generated expression rules.
969
+
970
+ static int probe_precedence(int token) {
971
+ switch (token) {
972
+ case '=':
973
+ case PROBE_ASSIGNMENT:
974
+ return EXPR_ASSIGNMENT;
975
+ case '?':
976
+ return EXPR_CONDITIONAL;
977
+ case PROBE_OR:
978
+ return EXPR_LOGICAL_OR;
979
+ case PROBE_AND:
980
+ return EXPR_LOGICAL_AND;
981
+ case PROBE_MEMBERSHIP:
982
+ return EXPR_MEMBERSHIP;
983
+ case '~':
984
+ case PROBE_MATCH:
985
+ return EXPR_MATCH;
986
+ case '<':
987
+ case '>':
988
+ case PROBE_COMPARISON:
989
+ return EXPR_COMPARISON;
990
+ case '+':
991
+ case '-':
992
+ return EXPR_ADDITIVE;
993
+ case '*':
994
+ case '/':
995
+ case '%':
996
+ return EXPR_MULTIPLICATIVE;
997
+ case '^':
998
+ return EXPR_EXPONENTIATION;
999
+ case PROBE_UPDATE:
1000
+ case PROBE_NAME:
1001
+ case PROBE_VALUE:
1002
+ case PROBE_TUPLE:
1003
+ case PROBE_GETLINE:
1004
+ case '$':
1005
+ case '!':
1006
+ return EXPR_CONCATENATION;
1007
+ default:
1008
+ return 0;
1009
+ }
1010
+ }
1011
+
1012
+ typedef struct {
1013
+ size_t end;
1014
+ int ceiling, kind;
1015
+ bool lvalue, postfix, unary;
1016
+ uint64_t children[2];
1017
+ } RecognizedExpression;
1018
+
1019
+ typedef struct {
1020
+ int *tokens;
1021
+ size_t length, capacity;
1022
+ bool failed;
1023
+ uint32_t expected;
1024
+ BoundaryTree *tree;
1025
+ size_t tree_count, tree_capacity, tree_root;
1026
+ } RecognizerInput;
1027
+
1028
+ enum {
1029
+ RECOGNIZED_LVALUE = EXPR_FIELD + 1,
1030
+ RECOGNIZED_GET = 1,
1031
+ RECOGNIZED_FILE,
1032
+ RECOGNIZED_FIELD,
1033
+ RECOGNIZED_PREFIX,
1034
+ RECOGNIZED_POSTFIX,
1035
+ RECOGNIZED_CONCAT,
1036
+ RECOGNIZED_BRANCHES,
1037
+ RECOGNIZED_TUPLE,
1038
+ };
1039
+
1040
+ enum {
1041
+ SIGNATURE_UNARY = 1,
1042
+ SIGNATURE_POSTFIX = 2,
1043
+ SIGNATURE_LVALUE = 4,
1044
+ };
1045
+
1046
+ static uint32_t boundary_signature(
1047
+ int kind,
1048
+ int ceiling,
1049
+ bool lvalue,
1050
+ bool postfix,
1051
+ bool unary
1052
+ ) {
1053
+ return ((uint32_t)kind << 8) |
1054
+ ((uint32_t)ceiling << 3) |
1055
+ (lvalue ? SIGNATURE_LVALUE : 0u) |
1056
+ (postfix ? SIGNATURE_POSTFIX : 0u) |
1057
+ (unary ? SIGNATURE_UNARY : 0u);
1058
+ }
1059
+
1060
+ static int signature_ceiling(uint32_t signature) {
1061
+ return (signature >> 3) & 31;
1062
+ }
1063
+
1064
+ static int signature_kind(uint32_t signature) {
1065
+ return (int)(signature >> 8);
1066
+ }
1067
+
1068
+ static uint64_t make_descriptor(uint32_t signature, uint32_t length) {
1069
+ return (uint64_t)signature << 32 | length;
1070
+ }
1071
+
1072
+ static uint32_t descriptor_signature(uint64_t descriptor) {
1073
+ return (uint32_t)(descriptor >> 32);
1074
+ }
1075
+
1076
+ static uint32_t descriptor_length(uint64_t descriptor) {
1077
+ return (uint32_t)descriptor;
1078
+ }
1079
+
1080
+ static int descriptor_kind(uint64_t descriptor) {
1081
+ return signature_kind(descriptor_signature(descriptor));
1082
+ }
1083
+
1084
+ // Store the short signature in low bits to reduce base-4 stack digits.
1085
+ enum { SAVED_SIGNATURE_BITS = 17 };
1086
+ static const uint64_t SAVED_SIGNATURE_MASK =
1087
+ (UINT64_C(1) << SAVED_SIGNATURE_BITS) - 1;
1088
+ static const uint64_t SAVED_HIGH_MASK =
1089
+ ~((UINT64_C(1) << (32 + SAVED_SIGNATURE_BITS)) - 1);
1090
+ static uint64_t saved_descriptor(uint64_t descriptor) {
1091
+ return (descriptor & SAVED_HIGH_MASK) |
1092
+ (uint64_t)descriptor_length(descriptor) << SAVED_SIGNATURE_BITS |
1093
+ (descriptor_signature(descriptor) & SAVED_SIGNATURE_MASK);
1094
+ }
1095
+
1096
+ static uint64_t restored_descriptor(uint64_t saved) {
1097
+ return (saved & SAVED_HIGH_MASK) |
1098
+ make_descriptor(
1099
+ (uint32_t)(saved & SAVED_SIGNATURE_MASK),
1100
+ (uint32_t)(saved >> SAVED_SIGNATURE_BITS)
1101
+ );
1102
+ }
1103
+
1104
+ static RecognizedExpression outline_expression(BoundaryOutline outline) {
1105
+ const uint32_t signature = descriptor_signature(outline.descriptor);
1106
+ return (RecognizedExpression){
1107
+ .end = descriptor_length(outline.descriptor),
1108
+ .kind = descriptor_kind(outline.descriptor),
1109
+ .ceiling = signature_ceiling(signature),
1110
+ .lvalue = signature & SIGNATURE_LVALUE,
1111
+ .postfix = signature & SIGNATURE_POSTFIX,
1112
+ .unary = signature & SIGNATURE_UNARY,
1113
+ .children = {outline.children[0], outline.children[1]},
1114
+ };
1115
+ }
1116
+
1117
+ static void recognizer_destroy(RecognizerInput *parser) {
1118
+ ts_free(parser->tokens);
1119
+ ts_free(parser->tree);
1120
+ }
1121
+
1122
+ static void recognizer_add_token(RecognizerInput *parser, int token) {
1123
+ if (parser->failed) return;
1124
+ if (parser->length == parser->capacity) {
1125
+ int *tokens =
1126
+ grow_array(parser->tokens, &parser->capacity, sizeof(*tokens), 32);
1127
+ if (!tokens) {
1128
+ parser->failed = true;
1129
+ return;
1130
+ }
1131
+ parser->tokens = tokens;
1132
+ }
1133
+ parser->tokens[parser->length++] = token;
1134
+ }
1135
+
1136
+ static int recognizer_token(const RecognizerInput *parser, size_t position) {
1137
+ return position < parser->length ? parser->tokens[position] : PROBE_END;
1138
+ }
1139
+
1140
+ typedef struct {
1141
+ size_t start, end;
1142
+ int kind, ceiling;
1143
+ bool lvalue, postfix, unary;
1144
+ uint64_t children[2];
1145
+ size_t child_indices[2];
1146
+ } RecognizedValue;
1147
+
1148
+ typedef enum {
1149
+ RECOGNIZER_PREFIX,
1150
+ RECOGNIZER_FIELD,
1151
+ RECOGNIZER_UNARY,
1152
+ RECOGNIZER_TARGET,
1153
+ RECOGNIZER_SOURCE,
1154
+ RECOGNIZER_GET,
1155
+ RECOGNIZER_TAIL,
1156
+ RECOGNIZER_BINARY,
1157
+ RECOGNIZER_CONSEQUENCE,
1158
+ RECOGNIZER_ALTERNATIVE,
1159
+ } RecognizerStage;
1160
+
1161
+ typedef struct {
1162
+ size_t start, operator_position, context;
1163
+ int minimum, token, precedence;
1164
+ RecognizerStage stage;
1165
+ bool get_only;
1166
+ RecognizedValue left, consequence;
1167
+ } RecognizerFrame;
1168
+
1169
+ typedef struct {
1170
+ size_t parent;
1171
+ uint64_t control, left, consequence;
1172
+ } RecognizerContext;
1173
+
1174
+ typedef struct {
1175
+ size_t position, context;
1176
+ } RecognizerFailure;
1177
+
1178
+ typedef struct {
1179
+ RecognizerInput *input;
1180
+ RecognizerFrame *frames;
1181
+ size_t *choices, *sources;
1182
+ bool *alternatives;
1183
+ size_t depth, context_depth;
1184
+ RecognizedValue value;
1185
+ bool valid, exhausted;
1186
+ size_t remaining;
1187
+ RecognizerContext *contexts;
1188
+ size_t context_count, context_capacity, *context_slots, context_slot_capacity;
1189
+ RecognizerFailure *failures, *history;
1190
+ size_t failure_count, failure_capacity, history_count, history_capacity;
1191
+ } Recognizer;
1192
+
1193
+ static uint32_t value_signature(RecognizedValue value) {
1194
+ return boundary_signature(
1195
+ value.kind,
1196
+ value.ceiling,
1197
+ value.lvalue,
1198
+ value.postfix,
1199
+ value.unary
1200
+ );
1201
+ }
1202
+
1203
+ static size_t recognizer_context_hash(RecognizerContext value) {
1204
+ return value.parent * 0x9e3779b1u ^
1205
+ (size_t)value.control * 0x85ebca6bu ^
1206
+ (size_t)value.left * 0xc2b2ae35u ^
1207
+ (size_t)value.consequence;
1208
+ }
1209
+
1210
+ static size_t recognizer_context(Recognizer *parser) {
1211
+ size_t parent = parser->context_depth
1212
+ ? parser->frames[parser->context_depth - 1].context
1213
+ : 0;
1214
+ for (size_t i = parser->context_depth; i < parser->depth; i++) {
1215
+ if (!parser->remaining) {
1216
+ parser->exhausted = true;
1217
+ return 0;
1218
+ }
1219
+ parser->remaining--;
1220
+ RecognizerFrame *frame = &parser->frames[i];
1221
+ RecognizerContext value = {
1222
+ .parent = parent,
1223
+ .control = (uint64_t)frame->stage |
1224
+ ((uint64_t)(unsigned)frame->minimum << 8) |
1225
+ ((uint64_t)(unsigned)frame->precedence << 16) |
1226
+ ((uint64_t)(unsigned)frame->token << 24) |
1227
+ ((uint64_t)frame->get_only << 40) |
1228
+ ((uint64_t)(frame->operator_position == SIZE_MAX) << 41),
1229
+ .left = value_signature(frame->left),
1230
+ .consequence = value_signature(frame->consequence)
1231
+ };
1232
+ if (parser->context_count >= parser->context_slot_capacity / 2) {
1233
+ const size_t capacity =
1234
+ doubled_capacity(parser->context_slot_capacity, sizeof(size_t), 64);
1235
+ size_t *slots = capacity ? ts_calloc(capacity, sizeof(*slots)) : NULL;
1236
+ if (!slots) return 0;
1237
+ for (size_t j = 0; j < parser->context_count; j++) {
1238
+ size_t slot =
1239
+ recognizer_context_hash(parser->contexts[j]) & (capacity - 1);
1240
+ while (slots[slot]) slot = (slot + 1) & (capacity - 1);
1241
+ slots[slot] = j + 1;
1242
+ }
1243
+ ts_free(parser->context_slots);
1244
+ parser->context_slots = slots;
1245
+ parser->context_slot_capacity = capacity;
1246
+ }
1247
+ size_t slot =
1248
+ recognizer_context_hash(value) & (parser->context_slot_capacity - 1);
1249
+ while (parser->context_slots[slot]) {
1250
+ RecognizerContext old = parser->contexts[parser->context_slots[slot] - 1];
1251
+ if (
1252
+ old.parent == value.parent &&
1253
+ old.control == value.control &&
1254
+ old.left == value.left &&
1255
+ old.consequence == value.consequence
1256
+ )
1257
+ break;
1258
+ slot = (slot + 1) & (parser->context_slot_capacity - 1);
1259
+ }
1260
+ if (!parser->context_slots[slot]) {
1261
+ if (parser->context_count == parser->context_capacity) {
1262
+ RecognizerContext *contexts = grow_array(
1263
+ parser->contexts,
1264
+ &parser->context_capacity,
1265
+ sizeof(*contexts),
1266
+ 32
1267
+ );
1268
+ if (!contexts) return 0;
1269
+ parser->contexts = contexts;
1270
+ }
1271
+ parser->contexts[parser->context_count++] = value;
1272
+ parser->context_slots[slot] = parser->context_count;
1273
+ }
1274
+ parent = parser->context_slots[slot];
1275
+ frame->context = parent;
1276
+ parser->context_depth = i + 1;
1277
+ }
1278
+ return parent;
1279
+ }
1280
+
1281
+ static size_t recognizer_failure_hash(RecognizerFailure value) {
1282
+ return value.position * 0x9e3779b1u ^ value.context * 0x85ebca6bu;
1283
+ }
1284
+
1285
+ static bool
1286
+ recognizer_failed(Recognizer *parser, RecognizerFailure value, bool add) {
1287
+ if (!value.context) return false;
1288
+ if (add && parser->failure_count >= parser->failure_capacity / 2) {
1289
+ const size_t capacity =
1290
+ doubled_capacity(parser->failure_capacity, sizeof(*parser->failures), 64);
1291
+ RecognizerFailure *failures =
1292
+ capacity ? ts_calloc(capacity, sizeof(*failures)) : NULL;
1293
+ if (!failures) return false;
1294
+ for (size_t i = 0; i < parser->failure_capacity; i++)
1295
+ if (parser->failures[i].context) {
1296
+ size_t slot =
1297
+ recognizer_failure_hash(parser->failures[i]) & (capacity - 1);
1298
+ while (failures[slot].context) slot = (slot + 1) & (capacity - 1);
1299
+ failures[slot] = parser->failures[i];
1300
+ }
1301
+ ts_free(parser->failures);
1302
+ parser->failures = failures;
1303
+ parser->failure_capacity = capacity;
1304
+ }
1305
+ if (!parser->failure_capacity) return false;
1306
+ size_t slot = recognizer_failure_hash(value) & (parser->failure_capacity - 1);
1307
+ while (parser->failures[slot].context) {
1308
+ if (
1309
+ parser->failures[slot].position == value.position &&
1310
+ parser->failures[slot].context == value.context
1311
+ )
1312
+ return true;
1313
+ slot = (slot + 1) & (parser->failure_capacity - 1);
1314
+ }
1315
+ if (add) {
1316
+ parser->failures[slot] = value;
1317
+ parser->failure_count++;
1318
+ }
1319
+ return false;
1320
+ }
1321
+
1322
+ static bool recognizer_checkpoint(Recognizer *parser) {
1323
+ if (parser->frames[parser->depth - 1].stage != RECOGNIZER_PREFIX) return true;
1324
+ RecognizerFailure value = {
1325
+ parser->frames[parser->depth - 1].start,
1326
+ recognizer_context(parser)
1327
+ };
1328
+ if (recognizer_failed(parser, value, false)) return false;
1329
+ if (!value.context) return true;
1330
+ if (parser->history_count == parser->history_capacity) {
1331
+ RecognizerFailure *history = grow_array(
1332
+ parser->history,
1333
+ &parser->history_capacity,
1334
+ sizeof(*history),
1335
+ 32
1336
+ );
1337
+ if (!history) return true;
1338
+ parser->history = history;
1339
+ }
1340
+ parser->history[parser->history_count++] = value;
1341
+ return true;
1342
+ }
1343
+
1344
+ static void recognizer_push(Recognizer *parser, size_t start, int minimum) {
1345
+ if (parser->depth > parser->input->length) {
1346
+ parser->valid = false;
1347
+ return;
1348
+ }
1349
+ parser->frames[parser->depth++] = (RecognizerFrame){
1350
+ .start = start,
1351
+ .minimum = minimum,
1352
+ .get_only = minimum == 0,
1353
+ };
1354
+ }
1355
+
1356
+ static void recognizer_return(Recognizer *parser, RecognizedValue value) {
1357
+ parser->value = value;
1358
+ parser->depth--;
1359
+ }
1360
+
1361
+ static uint64_t recognizer_descriptor(RecognizedValue node) {
1362
+ if (node.start == node.end) return 0;
1363
+ return make_descriptor(
1364
+ value_signature(node),
1365
+ (uint32_t)(node.end - node.start)
1366
+ );
1367
+ }
1368
+
1369
+ static size_t recognizer_store(Recognizer *parser, RecognizedValue value) {
1370
+ if (!value.end) return 0;
1371
+ RecognizerInput *input = parser->input;
1372
+ if (input->tree_count == input->tree_capacity) {
1373
+ BoundaryTree *tree =
1374
+ grow_array(input->tree, &input->tree_capacity, sizeof(*tree), 64);
1375
+ if (!tree) {
1376
+ parser->valid = false;
1377
+ return 0;
1378
+ }
1379
+ input->tree = tree;
1380
+ }
1381
+ input->tree[input->tree_count++] = (BoundaryTree){
1382
+ .outline =
1383
+ {.descriptor = recognizer_descriptor(value),
1384
+ .children = {value.children[0], value.children[1]},
1385
+ .repeat = 1},
1386
+ .children = {value.child_indices[0], value.child_indices[1]},
1387
+ };
1388
+ return input->tree_count;
1389
+ }
1390
+
1391
+ static RecognizedValue recognizer_node(
1392
+ Recognizer *parser,
1393
+ int kind,
1394
+ size_t start,
1395
+ size_t end,
1396
+ RecognizedValue left,
1397
+ RecognizedValue right
1398
+ ) {
1399
+ return (RecognizedValue){
1400
+ .child_indices =
1401
+ {recognizer_store(parser, left.end ? left : right),
1402
+ left.end ? recognizer_store(parser, right) : 0},
1403
+ .kind = kind,
1404
+ .children =
1405
+ {recognizer_descriptor(left.end ? left : right),
1406
+ left.end ? recognizer_descriptor(right) : 0},
1407
+ .start = start,
1408
+ .end = end,
1409
+ .ceiling = EXPR_FIELD,
1410
+ };
1411
+ }
1412
+
1413
+ typedef struct {
1414
+ int token, precedence;
1415
+ bool pipe, postfix, eligible;
1416
+ } Continuation;
1417
+
1418
+ static Continuation continuation(
1419
+ const RecognizerInput *input,
1420
+ size_t end,
1421
+ int minimum,
1422
+ uint32_t left
1423
+ ) {
1424
+ const int token = recognizer_token(input, end);
1425
+ const int ceiling = signature_ceiling(left);
1426
+ const bool lvalue = left & SIGNATURE_LVALUE;
1427
+ const bool pipe = token == '|' && minimum <= EXPR_ASSIGNMENT;
1428
+ const int precedence = pipe ? EXPR_ASSIGNMENT : probe_precedence(token);
1429
+ return (Continuation){
1430
+ .token = token,
1431
+ .precedence = precedence,
1432
+ .pipe = pipe,
1433
+ .postfix = token == PROBE_UPDATE &&
1434
+ lvalue &&
1435
+ (left & SIGNATURE_POSTFIX) &&
1436
+ minimum <= EXPR_POSTFIX &&
1437
+ EXPR_POSTFIX <= ceiling,
1438
+ .eligible = precedence > 0 &&
1439
+ minimum <= precedence &&
1440
+ precedence <= ceiling &&
1441
+ (pipe || precedence != EXPR_ASSIGNMENT || lvalue),
1442
+ };
1443
+ }
1444
+
1445
+ static size_t right_start(Continuation next, size_t end) {
1446
+ return next.precedence == EXPR_CONCATENATION ? end : end + 1;
1447
+ }
1448
+
1449
+ static int right_minimum(Continuation next) {
1450
+ return next.pipe ? 0
1451
+ : next.token == '?' ? EXPR_ASSIGNMENT
1452
+ : expression_right_precedence(next.precedence);
1453
+ }
1454
+
1455
+ static int operator_kind(int token, int precedence) {
1456
+ return precedence == EXPR_CONCATENATION ? RECOGNIZED_CONCAT : token;
1457
+ }
1458
+
1459
+ static int operator_ceiling(int token, int precedence) {
1460
+ return token == '|' ? EXPR_FIELD
1461
+ : precedence - expression_non_associative(precedence);
1462
+ }
1463
+
1464
+ static void recognizer_step(Recognizer *parser) {
1465
+ RecognizerFrame *frame = &parser->frames[parser->depth - 1];
1466
+ const int token = recognizer_token(parser->input, frame->start);
1467
+ const RecognizedValue empty = {0};
1468
+ switch (frame->stage) {
1469
+ case RECOGNIZER_PREFIX:
1470
+ if (
1471
+ token == PROBE_NAME ||
1472
+ ((token == PROBE_VALUE || token == PROBE_TUPLE) &&
1473
+ frame->minimum != RECOGNIZED_LVALUE)
1474
+ ) {
1475
+ frame->left = recognizer_node(
1476
+ parser,
1477
+ token == PROBE_TUPLE ? RECOGNIZED_TUPLE : 0,
1478
+ frame->start,
1479
+ frame->start + 1,
1480
+ empty,
1481
+ empty
1482
+ );
1483
+ frame->left.lvalue = token == PROBE_NAME;
1484
+ frame->left.postfix = true;
1485
+ frame->stage = RECOGNIZER_TAIL;
1486
+ } else if (token == '$') {
1487
+ frame->stage = RECOGNIZER_FIELD;
1488
+ recognizer_push(parser, frame->start + 1, EXPR_FIELD);
1489
+ } else if (
1490
+ token == PROBE_GETLINE && frame->minimum != RECOGNIZED_LVALUE
1491
+ ) {
1492
+ frame->left = recognizer_node(
1493
+ parser,
1494
+ RECOGNIZED_GET,
1495
+ frame->start,
1496
+ frame->start + 1,
1497
+ empty,
1498
+ empty
1499
+ );
1500
+ frame->left.postfix = true;
1501
+ const int next = recognizer_token(parser->input, frame->start + 1);
1502
+ if (next == PROBE_NAME || next == '$') {
1503
+ parser->alternatives[frame->start] =
1504
+ parser->choices[frame->start] == 0;
1505
+ if (!parser->choices[frame->start]) {
1506
+ frame->stage = RECOGNIZER_TARGET;
1507
+ recognizer_push(parser, frame->start + 1, RECOGNIZED_LVALUE);
1508
+ break;
1509
+ }
1510
+ }
1511
+ frame->stage = RECOGNIZER_GET;
1512
+ } else if (
1513
+ frame->minimum != RECOGNIZED_LVALUE &&
1514
+ (token == '+' || token == '-' || token == '!' || token == PROBE_UPDATE)
1515
+ ) {
1516
+ frame->stage = RECOGNIZER_UNARY;
1517
+ recognizer_push(
1518
+ parser,
1519
+ frame->start + 1,
1520
+ token == PROBE_UPDATE ? RECOGNIZED_LVALUE : EXPR_UNARY
1521
+ );
1522
+ } else {
1523
+ parser->valid = false;
1524
+ }
1525
+ break;
1526
+ case RECOGNIZER_FIELD:
1527
+ frame->left = recognizer_node(
1528
+ parser,
1529
+ RECOGNIZED_FIELD,
1530
+ frame->start,
1531
+ parser->value.end,
1532
+ empty,
1533
+ parser->value
1534
+ );
1535
+ frame->left.lvalue = true;
1536
+ frame->left.postfix = parser->value.postfix;
1537
+ frame->stage = RECOGNIZER_TAIL;
1538
+ break;
1539
+ case RECOGNIZER_UNARY:
1540
+ frame->left = recognizer_node(
1541
+ parser,
1542
+ RECOGNIZED_PREFIX,
1543
+ frame->start,
1544
+ parser->value.end,
1545
+ empty,
1546
+ parser->value
1547
+ );
1548
+ frame->left.ceiling = token == PROBE_UPDATE ? EXPR_POSTFIX : EXPR_UNARY;
1549
+ frame->left.unary = token == '+' || token == '-';
1550
+ frame->stage = RECOGNIZER_TAIL;
1551
+ break;
1552
+ case RECOGNIZER_TARGET:
1553
+ frame->left = recognizer_node(
1554
+ parser,
1555
+ RECOGNIZED_GET,
1556
+ frame->start,
1557
+ parser->value.end,
1558
+ empty,
1559
+ parser->value
1560
+ );
1561
+ frame->left.postfix = parser->value.postfix;
1562
+ frame->stage = RECOGNIZER_GET;
1563
+ break;
1564
+ case RECOGNIZER_GET:
1565
+ if (
1566
+ !frame->get_only &&
1567
+ recognizer_token(parser->input, frame->left.end) == '<'
1568
+ ) {
1569
+ const size_t position = frame->left.end;
1570
+ const size_t owner = parser->sources[position]++;
1571
+ if (parser->choices[position] == owner) {
1572
+ parser->alternatives[position] = true;
1573
+ frame->stage = RECOGNIZER_SOURCE;
1574
+ recognizer_push(parser, position + 1, EXPR_FILE_SOURCE);
1575
+ break;
1576
+ }
1577
+ }
1578
+ frame->stage = RECOGNIZER_TAIL;
1579
+ break;
1580
+ case RECOGNIZER_SOURCE:
1581
+ frame->left = recognizer_node(
1582
+ parser,
1583
+ RECOGNIZED_FILE,
1584
+ frame->start,
1585
+ parser->value.end,
1586
+ frame->left,
1587
+ parser->value
1588
+ );
1589
+ frame->left.postfix = true;
1590
+ frame->stage = RECOGNIZER_TAIL;
1591
+ break;
1592
+ case RECOGNIZER_TAIL: {
1593
+ if (frame->minimum == RECOGNIZED_LVALUE || frame->get_only) {
1594
+ recognizer_return(parser, frame->left);
1595
+ break;
1596
+ }
1597
+ const size_t end = frame->left.end;
1598
+ const Continuation next = continuation(
1599
+ parser->input,
1600
+ end,
1601
+ frame->minimum,
1602
+ value_signature(frame->left)
1603
+ );
1604
+ if (next.postfix && (!next.eligible || !parser->choices[end])) {
1605
+ parser->alternatives[end] = next.eligible;
1606
+ const RecognizedValue left = frame->left;
1607
+ frame->left = recognizer_node(
1608
+ parser,
1609
+ RECOGNIZED_POSTFIX,
1610
+ frame->start,
1611
+ end + 1,
1612
+ left,
1613
+ empty
1614
+ );
1615
+ frame->left.ceiling = EXPR_POSTFIX;
1616
+ break;
1617
+ }
1618
+ if (!next.eligible) {
1619
+ recognizer_return(parser, frame->left);
1620
+ break;
1621
+ }
1622
+ frame->token = next.token;
1623
+ frame->operator_position = end;
1624
+ frame->precedence = next.precedence;
1625
+ frame->stage =
1626
+ next.token == '?' ? RECOGNIZER_CONSEQUENCE : RECOGNIZER_BINARY;
1627
+ if (next.token == PROBE_MEMBERSHIP) {
1628
+ if (recognizer_token(parser->input, end + 1) != PROBE_NAME) {
1629
+ parser->valid = false;
1630
+ break;
1631
+ }
1632
+ parser->value =
1633
+ recognizer_node(parser, 0, end + 1, end + 2, empty, empty);
1634
+ parser->value.lvalue = parser->value.postfix = true;
1635
+ break;
1636
+ }
1637
+ if (
1638
+ next.pipe && recognizer_token(parser->input, end + 1) != PROBE_GETLINE
1639
+ ) {
1640
+ parser->valid = false;
1641
+ break;
1642
+ }
1643
+ if (next.token == '<') parser->alternatives[end] = false;
1644
+ recognizer_push(parser, right_start(next, end), right_minimum(next));
1645
+ break;
1646
+ }
1647
+ case RECOGNIZER_CONSEQUENCE:
1648
+ if (recognizer_token(parser->input, parser->value.end) != ':') {
1649
+ parser->valid = false;
1650
+ break;
1651
+ }
1652
+ frame->consequence = parser->value;
1653
+ frame->stage = RECOGNIZER_ALTERNATIVE;
1654
+ recognizer_push(parser, parser->value.end + 1, EXPR_CONDITIONAL);
1655
+ break;
1656
+ case RECOGNIZER_ALTERNATIVE:
1657
+ parser->value = recognizer_node(
1658
+ parser,
1659
+ RECOGNIZED_BRANCHES,
1660
+ frame->operator_position + 1,
1661
+ parser->value.end,
1662
+ frame->consequence,
1663
+ parser->value
1664
+ );
1665
+ if (frame->operator_position == SIZE_MAX) {
1666
+ recognizer_return(parser, parser->value);
1667
+ break;
1668
+ }
1669
+ frame->stage = RECOGNIZER_BINARY;
1670
+ break;
1671
+ case RECOGNIZER_BINARY: {
1672
+ if (frame->precedence == EXPR_CONCATENATION && parser->value.unary) {
1673
+ parser->valid = false;
1674
+ break;
1675
+ }
1676
+ const bool unary = frame->left.unary;
1677
+ frame->left = recognizer_node(
1678
+ parser,
1679
+ operator_kind(frame->token, frame->precedence),
1680
+ frame->start,
1681
+ parser->value.end,
1682
+ frame->left,
1683
+ parser->value
1684
+ );
1685
+ frame->left.unary = unary;
1686
+ frame->left.ceiling = operator_ceiling(frame->token, frame->precedence);
1687
+ frame->stage = RECOGNIZER_TAIL;
1688
+ break;
1689
+ }
1690
+ }
1691
+ }
1692
+
1693
+ typedef struct {
1694
+ uint64_t descriptor;
1695
+ uint32_t start, children[2], decision, rank;
1696
+ } SharedNode;
1697
+
1698
+ typedef struct {
1699
+ uint32_t *items, *slots, *heap;
1700
+ size_t length, capacity, slot_capacity, heap_length;
1701
+ } SharedSet;
1702
+
1703
+ typedef struct {
1704
+ RecognizerInput *input;
1705
+ SharedNode *nodes;
1706
+ size_t length, capacity;
1707
+ SharedSet *memo;
1708
+ bool *needed;
1709
+ uint32_t *ranks[2], *stack;
1710
+ bool failed, select;
1711
+ } SharedRecognition;
1712
+
1713
+ enum { RECOGNIZER_LEVELS = RECOGNIZED_LVALUE + 1 };
1714
+
1715
+ static uint32_t shared_end(SharedNode node) {
1716
+ return node.start + descriptor_length(node.descriptor);
1717
+ }
1718
+
1719
+ static uint32_t shared_shape(SharedNode node) {
1720
+ return descriptor_signature(node.descriptor);
1721
+ }
1722
+
1723
+ static SharedNode shared_node(
1724
+ SharedRecognition *parser,
1725
+ int kind,
1726
+ uint32_t start,
1727
+ uint32_t end,
1728
+ uint32_t left,
1729
+ uint32_t right,
1730
+ int ceiling,
1731
+ bool lvalue,
1732
+ bool postfix,
1733
+ bool unary
1734
+ ) {
1735
+ SharedNode node = {
1736
+ .descriptor = make_descriptor(
1737
+ boundary_signature(kind, ceiling, lvalue, postfix, unary),
1738
+ end - start
1739
+ ),
1740
+ .start = start,
1741
+ .children = {left ? left : right, left ? right : 0},
1742
+ .decision = UINT32_MAX,
1743
+ };
1744
+ if (kind == RECOGNIZED_GET) {
1745
+ node.decision = start;
1746
+ node.rank = left || right ? 1 : 2;
1747
+ } else if (kind == RECOGNIZED_FILE || kind == '<') {
1748
+ node.decision = shared_end(parser->nodes[left - 1]);
1749
+ node.rank = kind == '<' ? (uint32_t)parser->input->length + 2
1750
+ : (uint32_t)parser->input->length - start + 1;
1751
+ } else if (kind == RECOGNIZED_POSTFIX) {
1752
+ node.decision = end - 1;
1753
+ node.rank = 1;
1754
+ } else if (
1755
+ kind == RECOGNIZED_PREFIX && parser->input->tokens[start] == PROBE_UPDATE
1756
+ ) {
1757
+ node.decision = start;
1758
+ node.rank = 2;
1759
+ }
1760
+ return node;
1761
+ }
1762
+
1763
+ static bool
1764
+ shared_better(SharedRecognition *parser, SharedNode a, SharedNode b) {
1765
+ if (!parser->select) return false;
1766
+ const SharedNode roots[] = {a, b};
1767
+ for (unsigned side = 0; side < 2; side++) {
1768
+ memset(parser->ranks[side], 0, parser->input->length * sizeof(uint32_t));
1769
+ size_t depth = 0;
1770
+ SharedNode node = roots[side];
1771
+ for (;;) {
1772
+ if (node.decision != UINT32_MAX)
1773
+ parser->ranks[side][node.decision] = node.rank;
1774
+ for (unsigned child = 0; child < 2; child++)
1775
+ if (node.children[child]) parser->stack[depth++] = node.children[child];
1776
+ if (!depth) break;
1777
+ node = parser->nodes[parser->stack[--depth] - 1];
1778
+ }
1779
+ }
1780
+ for (size_t i = a.start; i < shared_end(a); i++) {
1781
+ if (parser->ranks[0][i] != parser->ranks[1][i])
1782
+ return parser->ranks[0][i] < parser->ranks[1][i];
1783
+ }
1784
+ return false;
1785
+ }
1786
+
1787
+ static size_t shared_hash(SharedNode node) {
1788
+ return (size_t)shared_end(node) * 0x9e3779b1u ^ shared_shape(node);
1789
+ }
1790
+
1791
+ static void shared_add(
1792
+ SharedRecognition *parser,
1793
+ SharedSet *set,
1794
+ SharedNode node,
1795
+ uint32_t existing
1796
+ ) {
1797
+ if (parser->failed) return;
1798
+ if (set->length >= set->slot_capacity / 2) {
1799
+ const size_t capacity =
1800
+ doubled_capacity(set->slot_capacity, sizeof(uint32_t), 8);
1801
+ uint32_t *slots = capacity ? ts_calloc(capacity, sizeof(*slots)) : NULL;
1802
+ if (!slots) {
1803
+ parser->failed = true;
1804
+ return;
1805
+ }
1806
+ for (size_t i = 0; i < set->length; i++) {
1807
+ size_t slot =
1808
+ shared_hash(parser->nodes[set->items[i] - 1]) & (capacity - 1);
1809
+ while (slots[slot]) slot = (slot + 1) & (capacity - 1);
1810
+ slots[slot] = (uint32_t)i + 1;
1811
+ }
1812
+ ts_free(set->slots);
1813
+ set->slots = slots;
1814
+ set->slot_capacity = capacity;
1815
+ }
1816
+ size_t slot = shared_hash(node) & (set->slot_capacity - 1);
1817
+ while (set->slots[slot]) {
1818
+ uint32_t index = set->items[set->slots[slot] - 1];
1819
+ SharedNode old = parser->nodes[index - 1];
1820
+ if (
1821
+ shared_end(old) == shared_end(node) &&
1822
+ shared_shape(old) == shared_shape(node)
1823
+ ) {
1824
+ if (shared_better(parser, node, old)) parser->nodes[index - 1] = node;
1825
+ return;
1826
+ }
1827
+ slot = (slot + 1) & (set->slot_capacity - 1);
1828
+ }
1829
+ if (set->length == set->capacity) {
1830
+ size_t capacity = set->capacity;
1831
+ uint32_t *items = set->capacity <= UINT32_MAX / 2
1832
+ ? grow_array(set->items, &capacity, sizeof(*items), 4)
1833
+ : NULL;
1834
+ if (!items) {
1835
+ parser->failed = true;
1836
+ return;
1837
+ }
1838
+ set->items = items;
1839
+ uint32_t *heap = grow_array(set->heap, &set->capacity, sizeof(*heap), 4);
1840
+ if (!heap) {
1841
+ parser->failed = true;
1842
+ return;
1843
+ }
1844
+ set->heap = heap;
1845
+ }
1846
+ if (!existing) {
1847
+ if (parser->length == parser->capacity) {
1848
+ SharedNode *nodes = parser->capacity <= UINT32_MAX / 2
1849
+ ? grow_array(parser->nodes, &parser->capacity, sizeof(*nodes), 64)
1850
+ : NULL;
1851
+ if (!nodes) {
1852
+ parser->failed = true;
1853
+ return;
1854
+ }
1855
+ parser->nodes = nodes;
1856
+ }
1857
+ parser->nodes[parser->length++] = node;
1858
+ existing = (uint32_t)parser->length;
1859
+ }
1860
+ set->items[set->length] = existing;
1861
+ set->slots[slot] = (uint32_t)++set->length;
1862
+ size_t position = set->heap_length++;
1863
+ while (
1864
+ position &&
1865
+ shared_end(parser->nodes[set->heap[(position - 1) / 2] - 1]) >
1866
+ shared_end(node)
1867
+ ) {
1868
+ set->heap[position] = set->heap[(position - 1) / 2];
1869
+ position = (position - 1) / 2;
1870
+ }
1871
+ set->heap[position] = existing;
1872
+ }
1873
+
1874
+ static uint32_t shared_pop(SharedRecognition *parser, SharedSet *set) {
1875
+ uint32_t result = set->heap[0], last = set->heap[--set->heap_length];
1876
+ size_t position = 0;
1877
+ while (position * 2 + 1 < set->heap_length) {
1878
+ size_t child = position * 2 + 1;
1879
+ if (
1880
+ child + 1 < set->heap_length &&
1881
+ shared_end(parser->nodes[set->heap[child + 1] - 1]) <
1882
+ shared_end(parser->nodes[set->heap[child] - 1])
1883
+ )
1884
+ child++;
1885
+ if (
1886
+ shared_end(parser->nodes[last - 1]) <=
1887
+ shared_end(parser->nodes[set->heap[child] - 1])
1888
+ )
1889
+ break;
1890
+ set->heap[position] = set->heap[child];
1891
+ position = child;
1892
+ }
1893
+ set->heap[position] = last;
1894
+ return result;
1895
+ }
1896
+
1897
+ static void shared_set_destroy(SharedSet *set) {
1898
+ ts_free(set->items);
1899
+ ts_free(set->slots);
1900
+ ts_free(set->heap);
1901
+ }
1902
+
1903
+ static SharedSet *
1904
+ shared_expr(SharedRecognition *parser, size_t start, int minimum) {
1905
+ return &parser->memo[start * RECOGNIZER_LEVELS + minimum];
1906
+ }
1907
+
1908
+ static void shared_get(
1909
+ SharedRecognition *parser,
1910
+ SharedSet *result,
1911
+ uint32_t start,
1912
+ bool redirected
1913
+ ) {
1914
+ shared_add(
1915
+ parser,
1916
+ result,
1917
+ shared_node(
1918
+ parser,
1919
+ RECOGNIZED_GET,
1920
+ start,
1921
+ start + 1,
1922
+ 0,
1923
+ 0,
1924
+ EXPR_FIELD,
1925
+ false,
1926
+ true,
1927
+ false
1928
+ ),
1929
+ 0
1930
+ );
1931
+ SharedSet *targets = shared_expr(parser, start + 1, RECOGNIZED_LVALUE);
1932
+ for (size_t i = 0; i < targets->length && !parser->failed; i++) {
1933
+ uint32_t target = targets->items[i];
1934
+ SharedNode child = parser->nodes[target - 1];
1935
+ shared_add(
1936
+ parser,
1937
+ result,
1938
+ shared_node(
1939
+ parser,
1940
+ RECOGNIZED_GET,
1941
+ start,
1942
+ shared_end(child),
1943
+ 0,
1944
+ target,
1945
+ EXPR_FIELD,
1946
+ false,
1947
+ shared_shape(child) & SIGNATURE_POSTFIX,
1948
+ false
1949
+ ),
1950
+ 0
1951
+ );
1952
+ }
1953
+ const size_t count = result->length;
1954
+ for (size_t i = 0; redirected && i < count && !parser->failed; i++) {
1955
+ uint32_t get = result->items[i];
1956
+ uint32_t end = shared_end(parser->nodes[get - 1]);
1957
+ if (recognizer_token(parser->input, end) != '<') continue;
1958
+ SharedSet *sources = shared_expr(parser, end + 1, EXPR_FILE_SOURCE);
1959
+ for (size_t j = 0; j < sources->length && !parser->failed; j++) {
1960
+ uint32_t source = sources->items[j];
1961
+ shared_add(
1962
+ parser,
1963
+ result,
1964
+ shared_node(
1965
+ parser,
1966
+ RECOGNIZED_FILE,
1967
+ start,
1968
+ shared_end(parser->nodes[source - 1]),
1969
+ get,
1970
+ source,
1971
+ EXPR_FIELD,
1972
+ false,
1973
+ true,
1974
+ false
1975
+ ),
1976
+ 0
1977
+ );
1978
+ }
1979
+ }
1980
+ }
1981
+
1982
+ static void shared_prefix(
1983
+ SharedRecognition *parser,
1984
+ SharedSet *result,
1985
+ uint32_t start,
1986
+ int minimum
1987
+ ) {
1988
+ int token = recognizer_token(parser->input, start);
1989
+ bool lvalue = minimum == RECOGNIZED_LVALUE;
1990
+ if (
1991
+ token == PROBE_NAME ||
1992
+ (!lvalue && (token == PROBE_VALUE || token == PROBE_TUPLE))
1993
+ ) {
1994
+ shared_add(
1995
+ parser,
1996
+ result,
1997
+ shared_node(
1998
+ parser,
1999
+ token == PROBE_TUPLE ? RECOGNIZED_TUPLE : 0,
2000
+ start,
2001
+ start + 1,
2002
+ 0,
2003
+ 0,
2004
+ EXPR_FIELD,
2005
+ token == PROBE_NAME,
2006
+ true,
2007
+ false
2008
+ ),
2009
+ 0
2010
+ );
2011
+ } else if (
2012
+ token == '$' ||
2013
+ (!lvalue &&
2014
+ (token == '+' || token == '-' || token == '!' || token == PROBE_UPDATE))
2015
+ ) {
2016
+ SharedSet *operands = shared_expr(
2017
+ parser,
2018
+ start + 1,
2019
+ token == '$' ? EXPR_FIELD
2020
+ : token == PROBE_UPDATE ? RECOGNIZED_LVALUE
2021
+ : EXPR_UNARY
2022
+ );
2023
+ for (size_t i = 0; i < operands->length && !parser->failed; i++) {
2024
+ uint32_t operand = operands->items[i];
2025
+ SharedNode child = parser->nodes[operand - 1];
2026
+ shared_add(
2027
+ parser,
2028
+ result,
2029
+ shared_node(
2030
+ parser,
2031
+ token == '$' ? RECOGNIZED_FIELD : RECOGNIZED_PREFIX,
2032
+ start,
2033
+ shared_end(child),
2034
+ 0,
2035
+ operand,
2036
+ token == '$' ? EXPR_FIELD
2037
+ : token == PROBE_UPDATE ? EXPR_POSTFIX
2038
+ : EXPR_UNARY,
2039
+ token == '$',
2040
+ token == '$' && (shared_shape(child) & SIGNATURE_POSTFIX),
2041
+ token == '+' || token == '-'
2042
+ ),
2043
+ 0
2044
+ );
2045
+ }
2046
+ } else if (!lvalue && token == PROBE_GETLINE) {
2047
+ shared_get(parser, result, start, minimum != 0);
2048
+ }
2049
+ }
2050
+
2051
+ static void
2052
+ shared_branches(SharedRecognition *parser, SharedSet *result, uint32_t start) {
2053
+ SharedSet *consequences = shared_expr(parser, start, EXPR_ASSIGNMENT);
2054
+ for (size_t i = 0; i < consequences->length && !parser->failed; i++) {
2055
+ uint32_t consequence = consequences->items[i];
2056
+ uint32_t end = shared_end(parser->nodes[consequence - 1]);
2057
+ if (recognizer_token(parser->input, end) != ':') continue;
2058
+ SharedSet *alternatives = shared_expr(parser, end + 1, EXPR_CONDITIONAL);
2059
+ for (size_t j = 0; j < alternatives->length && !parser->failed; j++) {
2060
+ uint32_t alternative = alternatives->items[j];
2061
+ shared_add(
2062
+ parser,
2063
+ result,
2064
+ shared_node(
2065
+ parser,
2066
+ RECOGNIZED_BRANCHES,
2067
+ start,
2068
+ shared_end(parser->nodes[alternative - 1]),
2069
+ consequence,
2070
+ alternative,
2071
+ EXPR_FIELD,
2072
+ false,
2073
+ false,
2074
+ false
2075
+ ),
2076
+ 0
2077
+ );
2078
+ }
2079
+ }
2080
+ }
2081
+
2082
+ static void
2083
+ shared_compute(SharedRecognition *parser, uint32_t start, int minimum) {
2084
+ SharedSet *result = shared_expr(parser, start, minimum), work = {0};
2085
+ shared_prefix(parser, &work, start, minimum);
2086
+ if (minimum == RECOGNIZED_LVALUE || minimum == 0) {
2087
+ *result = work;
2088
+ return;
2089
+ }
2090
+ while (work.heap_length && !parser->failed) {
2091
+ uint32_t index = shared_pop(parser, &work);
2092
+ SharedNode left = parser->nodes[index - 1];
2093
+ const uint32_t end = shared_end(left), shape = shared_shape(left);
2094
+ const Continuation next = continuation(parser->input, end, minimum, shape);
2095
+ if (next.postfix)
2096
+ shared_add(
2097
+ parser,
2098
+ &work,
2099
+ shared_node(
2100
+ parser,
2101
+ RECOGNIZED_POSTFIX,
2102
+ start,
2103
+ end + 1,
2104
+ index,
2105
+ 0,
2106
+ EXPR_POSTFIX,
2107
+ false,
2108
+ false,
2109
+ false
2110
+ ),
2111
+ 0
2112
+ );
2113
+ if (!next.eligible) {
2114
+ if (!next.postfix) shared_add(parser, result, left, index);
2115
+ continue;
2116
+ }
2117
+ SharedSet local = {0}, *rights = &local;
2118
+ if (next.token == PROBE_MEMBERSHIP) {
2119
+ if (recognizer_token(parser->input, end + 1) == PROBE_NAME)
2120
+ shared_add(
2121
+ parser,
2122
+ &local,
2123
+ shared_node(
2124
+ parser,
2125
+ 0,
2126
+ end + 1,
2127
+ end + 2,
2128
+ 0,
2129
+ 0,
2130
+ EXPR_FIELD,
2131
+ true,
2132
+ true,
2133
+ false
2134
+ ),
2135
+ 0
2136
+ );
2137
+ } else if (next.token == '?') {
2138
+ shared_branches(parser, &local, end + 1);
2139
+ } else if (
2140
+ !next.pipe || recognizer_token(parser->input, end + 1) == PROBE_GETLINE
2141
+ ) {
2142
+ rights = shared_expr(parser, right_start(next, end), right_minimum(next));
2143
+ }
2144
+ for (size_t i = 0; i < rights->length && !parser->failed; i++) {
2145
+ const uint32_t right = rights->items[i];
2146
+ const SharedNode child = parser->nodes[right - 1];
2147
+ if (
2148
+ next.precedence == EXPR_CONCATENATION &&
2149
+ (shared_shape(child) & SIGNATURE_UNARY)
2150
+ )
2151
+ continue;
2152
+ shared_add(
2153
+ parser,
2154
+ &work,
2155
+ shared_node(
2156
+ parser,
2157
+ operator_kind(next.token, next.precedence),
2158
+ start,
2159
+ shared_end(child),
2160
+ index,
2161
+ right,
2162
+ operator_ceiling(next.token, next.precedence),
2163
+ false,
2164
+ false,
2165
+ shape & SIGNATURE_UNARY
2166
+ ),
2167
+ 0
2168
+ );
2169
+ }
2170
+ shared_set_destroy(&local);
2171
+ }
2172
+ shared_set_destroy(&work);
2173
+ }
2174
+
2175
+ static bool shared_tree(SharedRecognition *parser, uint32_t root) {
2176
+ RecognizerInput *input = parser->input;
2177
+ if (input->length > (SIZE_MAX / sizeof(BoundaryTree) - 1) / 2) return false;
2178
+ const size_t capacity = input->length * 2 + 1;
2179
+ BoundaryTree *tree = ts_realloc(input->tree, capacity * sizeof(*tree));
2180
+ if (!tree) return false;
2181
+ input->tree = tree;
2182
+ input->tree_capacity = capacity;
2183
+ input->tree_count = 1;
2184
+ input->tree_root = 1;
2185
+ parser->stack[0] = root;
2186
+ for (size_t i = 0; i < input->tree_count; i++) {
2187
+ SharedNode node = parser->nodes[parser->stack[i] - 1];
2188
+ input->tree[i] =
2189
+ (BoundaryTree){.outline = {.descriptor = node.descriptor, .repeat = 1}};
2190
+ for (unsigned child = 0; child < 2; child++) {
2191
+ uint32_t index = node.children[child];
2192
+ if (!index) continue;
2193
+ input->tree[i].outline.children[child] =
2194
+ parser->nodes[index - 1].descriptor;
2195
+ parser->stack[input->tree_count++] = index;
2196
+ input->tree[i].children[child] = input->tree_count;
2197
+ }
2198
+ }
2199
+ return true;
2200
+ }
2201
+
2202
+ static bool shared_recognize(RecognizerInput *input, bool select) {
2203
+ if (
2204
+ input->length > UINT32_MAX - 2 ||
2205
+ input->length > SIZE_MAX / RECOGNIZER_LEVELS / sizeof(SharedSet) - 1
2206
+ )
2207
+ return false;
2208
+ SharedRecognition parser = {.input = input, .select = select};
2209
+ const size_t count = (input->length + 1) * RECOGNIZER_LEVELS;
2210
+ parser.memo = ts_calloc(count, sizeof(*parser.memo));
2211
+ parser.needed = ts_calloc(count, sizeof(*parser.needed));
2212
+ parser.ranks[0] = ts_calloc(input->length, sizeof(uint32_t));
2213
+ parser.ranks[1] = ts_calloc(input->length, sizeof(uint32_t));
2214
+ parser.stack = ts_calloc(input->length * 2 + 1, sizeof(uint32_t));
2215
+ bool accepted = false;
2216
+ if (
2217
+ !parser.memo ||
2218
+ !parser.needed ||
2219
+ !parser.ranks[0] ||
2220
+ !parser.ranks[1] ||
2221
+ !parser.stack
2222
+ )
2223
+ goto done;
2224
+ parser.needed[EXPR_ASSIGNMENT] = true;
2225
+ for (size_t i = 0; i < input->length; i++) {
2226
+ const int token = input->tokens[i];
2227
+ size_t next = (i + 1) * RECOGNIZER_LEVELS;
2228
+ if (token == '$') parser.needed[next + EXPR_FIELD] = true;
2229
+ if (token == PROBE_GETLINE || token == PROBE_UPDATE)
2230
+ parser.needed[next + RECOGNIZED_LVALUE] = true;
2231
+ if (token == '<') parser.needed[next + EXPR_FILE_SOURCE] = true;
2232
+ if (token == '+' || token == '-' || token == '!')
2233
+ parser.needed[next + EXPR_UNARY] = true;
2234
+ if (token == '?') parser.needed[next + EXPR_ASSIGNMENT] = true;
2235
+ if (token == ':') parser.needed[next + EXPR_CONDITIONAL] = true;
2236
+ if (token == '|') parser.needed[next] = true;
2237
+ const int precedence = probe_precedence(token);
2238
+ if (precedence == EXPR_CONCATENATION)
2239
+ parser.needed[i * RECOGNIZER_LEVELS + EXPR_CONCATENATION + 1] = true;
2240
+ else if (precedence)
2241
+ parser.needed[next + expression_right_precedence(precedence)] = true;
2242
+ }
2243
+ for (size_t start = input->length + 1; start && !parser.failed;) {
2244
+ start--;
2245
+ for (
2246
+ int minimum = 0; minimum < RECOGNIZER_LEVELS && !parser.failed; minimum++
2247
+ )
2248
+ if (parser.needed[start * RECOGNIZER_LEVELS + minimum])
2249
+ shared_compute(&parser, (uint32_t)start, minimum);
2250
+ }
2251
+ SharedSet local = {0}, *result = shared_expr(&parser, 0, EXPR_ASSIGNMENT);
2252
+ if (signature_kind(input->expected) == RECOGNIZED_BRANCHES) {
2253
+ shared_branches(&parser, &local, 0);
2254
+ result = &local;
2255
+ }
2256
+ uint32_t best = 0;
2257
+ for (size_t i = 0; i < result->length && !parser.failed; i++) {
2258
+ uint32_t index = result->items[i];
2259
+ SharedNode node = parser.nodes[index - 1];
2260
+ if (
2261
+ shared_end(node) == input->length &&
2262
+ (!input->expected || shared_shape(node) == input->expected) &&
2263
+ (!best || shared_better(&parser, node, parser.nodes[best - 1]))
2264
+ )
2265
+ best = index;
2266
+ }
2267
+ if (best && !parser.failed) accepted = !select || shared_tree(&parser, best);
2268
+ shared_set_destroy(&local);
2269
+ done:
2270
+ if (parser.memo)
2271
+ for (size_t i = 0; i < count; i++) shared_set_destroy(&parser.memo[i]);
2272
+ ts_free(parser.memo);
2273
+ ts_free(parser.needed);
2274
+ ts_free(parser.nodes);
2275
+ ts_free(parser.ranks[0]);
2276
+ ts_free(parser.ranks[1]);
2277
+ ts_free(parser.stack);
2278
+ return accepted;
2279
+ }
2280
+
2281
+ static bool shared_parse(RecognizerInput *input) {
2282
+ return shared_recognize(input, false) && shared_recognize(input, true);
2283
+ }
2284
+
2285
+ #ifndef RECOGNIZER_WORK_FACTOR
2286
+ #define RECOGNIZER_WORK_FACTOR 8
2287
+ #endif
2288
+
2289
+ static bool recognizer_parse(RecognizerInput *input) {
2290
+ if (input->length > (SIZE_MAX / sizeof(RecognizerFrame)) - 2) return false;
2291
+ Recognizer parser = {.input = input};
2292
+ parser.frames = ts_calloc(input->length + 2, sizeof(*parser.frames));
2293
+ parser.choices = ts_calloc(input->length, sizeof(*parser.choices));
2294
+ parser.sources = ts_calloc(input->length, sizeof(*parser.sources));
2295
+ parser.alternatives = ts_calloc(input->length, sizeof(*parser.alternatives));
2296
+ bool result = false, shared = false;
2297
+ size_t limit = input->length > SIZE_MAX / (input->length ? input->length : 1)
2298
+ ? SIZE_MAX
2299
+ : input->length * input->length;
2300
+ limit = RECOGNIZER_WORK_FACTOR && limit > SIZE_MAX / RECOGNIZER_WORK_FACTOR
2301
+ ? SIZE_MAX
2302
+ : limit * RECOGNIZER_WORK_FACTOR;
2303
+ parser.remaining = limit;
2304
+ if (
2305
+ !parser.frames || !parser.choices || !parser.sources || !parser.alternatives
2306
+ )
2307
+ goto done;
2308
+ for (;;) {
2309
+ if (input->length > parser.remaining) {
2310
+ shared = true;
2311
+ goto done;
2312
+ }
2313
+ parser.remaining -= input->length;
2314
+ memset(parser.sources, 0, input->length * sizeof(*parser.sources));
2315
+ memset(
2316
+ parser.alternatives,
2317
+ 0,
2318
+ input->length * sizeof(*parser.alternatives)
2319
+ );
2320
+ parser.depth = 0;
2321
+ parser.context_depth = 0;
2322
+ parser.history_count = 0;
2323
+ input->tree_count = 0;
2324
+ parser.valid = true;
2325
+ recognizer_push(&parser, 0, EXPR_ASSIGNMENT);
2326
+ if (signature_kind(input->expected) == RECOGNIZED_BRANCHES) {
2327
+ parser.frames[0].stage = RECOGNIZER_CONSEQUENCE;
2328
+ parser.frames[0].operator_position = SIZE_MAX;
2329
+ recognizer_push(&parser, 0, EXPR_ASSIGNMENT);
2330
+ }
2331
+ while (parser.valid && parser.depth) {
2332
+ if (!parser.remaining) {
2333
+ shared = true;
2334
+ goto done;
2335
+ }
2336
+ parser.remaining--;
2337
+ if (!recognizer_checkpoint(&parser)) {
2338
+ parser.valid = false;
2339
+ break;
2340
+ }
2341
+ if (parser.exhausted) {
2342
+ shared = true;
2343
+ goto done;
2344
+ }
2345
+ if (parser.context_depth >= parser.depth)
2346
+ parser.context_depth = parser.depth - 1;
2347
+ recognizer_step(&parser);
2348
+ }
2349
+ if (
2350
+ parser.valid &&
2351
+ !parser.depth &&
2352
+ parser.value.end == input->length &&
2353
+ (!input->expected || input->expected == value_signature(parser.value))
2354
+ ) {
2355
+ input->tree_root = recognizer_store(&parser, parser.value);
2356
+ result = input->tree_root != 0;
2357
+ break;
2358
+ }
2359
+ size_t position = input->length;
2360
+ while (position && !parser.alternatives[position - 1]) position--;
2361
+ if (!position) break;
2362
+ // Only positions past the backtrack point have exhausted all choices.
2363
+ for (size_t i = 0; i < parser.history_count; i++) {
2364
+ if (parser.history[i].position > position - 1)
2365
+ recognizer_failed(&parser, parser.history[i], true);
2366
+ }
2367
+ parser.choices[position - 1]++;
2368
+ memset(
2369
+ parser.choices + position,
2370
+ 0,
2371
+ (input->length - position) * sizeof(*parser.choices)
2372
+ );
2373
+ }
2374
+ done:
2375
+ ts_free(parser.contexts);
2376
+ ts_free(parser.context_slots);
2377
+ ts_free(parser.failures);
2378
+ ts_free(parser.history);
2379
+ ts_free(parser.frames);
2380
+ ts_free(parser.choices);
2381
+ ts_free(parser.sources);
2382
+ ts_free(parser.alternatives);
2383
+ return shared ? shared_parse(input) : result;
2384
+ }
2385
+
2386
+ static const uint64_t UNCONSTRAINED = UINT64_MAX;
2387
+
2388
+ static bool
2389
+ boundary_emit(TSLexer *lexer, BoundaryState *state, unsigned token) {
2390
+ if (
2391
+ (token >= CLASS_UNARY && token <= SELECT_TUPLE) ||
2392
+ token == BEGIN_CHILD ||
2393
+ token == END_CHILD ||
2394
+ token == NEXT_ARGUMENT
2395
+ )
2396
+ state->decision_hash =
2397
+ (state->decision_hash ^ token) * UINT64_C(0x100000001b3);
2398
+ lexer->result_symbol = (TSSymbol)token;
2399
+ return true;
2400
+ }
2401
+
2402
+ // Periodic caches store shift flags in the repeat field.
2403
+ enum {
2404
+ SHIFT_DESCRIPTOR = 1,
2405
+ SHIFT_LEFT = 2,
2406
+ SHIFT_RIGHT = 4,
2407
+ };
2408
+
2409
+ static bool period_shift(
2410
+ uint64_t first,
2411
+ uint64_t next,
2412
+ uint32_t delta,
2413
+ unsigned flag,
2414
+ unsigned *shifts
2415
+ ) {
2416
+ if (first == next) return true;
2417
+ if (
2418
+ descriptor_signature(first) != descriptor_signature(next) ||
2419
+ descriptor_length(first) < descriptor_length(next) ||
2420
+ descriptor_length(first) - descriptor_length(next) != delta
2421
+ )
2422
+ return false;
2423
+ *shifts |= flag;
2424
+ return true;
2425
+ }
2426
+
2427
+ static bool shifted_field(
2428
+ uint64_t pattern,
2429
+ uint64_t current,
2430
+ bool shifted,
2431
+ uint64_t shift
2432
+ ) {
2433
+ if (!shifted) return current == pattern;
2434
+ return shift < descriptor_length(pattern) && current == pattern - shift;
2435
+ }
2436
+
2437
+ static bool cache_period(
2438
+ BoundaryState *state,
2439
+ const RecognizerInput *parser,
2440
+ const size_t *order,
2441
+ size_t length
2442
+ ) {
2443
+ size_t best_period = 0, best_repeats = 0, best_covered = 0;
2444
+ uint32_t best_delta = 0;
2445
+ unsigned best_shifts[BOUNDARY_CACHE_CAPACITY + 1] = {0};
2446
+ for (
2447
+ size_t period = 1;
2448
+ period <= BOUNDARY_CACHE_CAPACITY && period * 2 <= length;
2449
+ period++
2450
+ ) {
2451
+ const uint64_t head = parser->tree[order[0] - 1].outline.descriptor;
2452
+ const uint64_t next = parser->tree[order[period] - 1].outline.descriptor;
2453
+ if (
2454
+ descriptor_signature(head) != descriptor_signature(next) ||
2455
+ descriptor_length(head) <= descriptor_length(next)
2456
+ )
2457
+ continue;
2458
+ const uint32_t delta = descriptor_length(head) - descriptor_length(next);
2459
+ unsigned shifts[BOUNDARY_CACHE_CAPACITY + 1] = {0};
2460
+ bool periodic = true;
2461
+ for (size_t i = 0; i < period && periodic; i++) {
2462
+ const BoundaryOutline a = parser->tree[order[i] - 1].outline;
2463
+ const BoundaryOutline b = parser->tree[order[i + period] - 1].outline;
2464
+ periodic = period_shift(
2465
+ a.descriptor,
2466
+ b.descriptor,
2467
+ delta,
2468
+ SHIFT_DESCRIPTOR,
2469
+ &shifts[i]
2470
+ ) &&
2471
+ period_shift(
2472
+ a.children[0],
2473
+ b.children[0],
2474
+ delta,
2475
+ SHIFT_LEFT,
2476
+ &shifts[i]
2477
+ ) &&
2478
+ period_shift(
2479
+ a.children[1],
2480
+ b.children[1],
2481
+ delta,
2482
+ SHIFT_RIGHT,
2483
+ &shifts[i]
2484
+ );
2485
+ }
2486
+ if (!periodic) continue;
2487
+ size_t matched = period * 2;
2488
+ while (matched < length) {
2489
+ const size_t i = matched % period;
2490
+ const uint64_t shift = (uint64_t)delta * (matched / period);
2491
+ const BoundaryOutline pattern = parser->tree[order[i] - 1].outline;
2492
+ const BoundaryOutline current = parser->tree[order[matched] - 1].outline;
2493
+ if (
2494
+ !shifted_field(
2495
+ pattern.descriptor,
2496
+ current.descriptor,
2497
+ shifts[i] & SHIFT_DESCRIPTOR,
2498
+ shift
2499
+ ) ||
2500
+ !shifted_field(
2501
+ pattern.children[0],
2502
+ current.children[0],
2503
+ shifts[i] & SHIFT_LEFT,
2504
+ shift
2505
+ ) ||
2506
+ !shifted_field(
2507
+ pattern.children[1],
2508
+ current.children[1],
2509
+ shifts[i] & SHIFT_RIGHT,
2510
+ shift
2511
+ )
2512
+ )
2513
+ break;
2514
+ matched++;
2515
+ }
2516
+ const size_t repeats = matched / period;
2517
+ if (repeats * period > best_covered) {
2518
+ best_period = period;
2519
+ best_repeats = repeats;
2520
+ best_covered = repeats * period;
2521
+ best_delta = delta;
2522
+ memcpy(best_shifts, shifts, sizeof(best_shifts));
2523
+ }
2524
+ }
2525
+ if (best_covered <= BOUNDARY_CACHE_CAPACITY) return false;
2526
+ state->cache_count = (unsigned)best_period;
2527
+ state->cache_repeats = (uint32_t)best_repeats;
2528
+ state->cache_delta = best_delta;
2529
+ for (unsigned i = 0; i < state->cache_count; i++) {
2530
+ state->cache[i] = parser->tree[order[i] - 1].outline;
2531
+ state->cache[i].repeat = best_shifts[i];
2532
+ }
2533
+ return true;
2534
+ }
2535
+
2536
+ static void
2537
+ cache_boundary(BoundaryState *state, const RecognizerInput *parser) {
2538
+ state->cache_count = state->cache_index = state->cache_repeats =
2539
+ state->cache_delta = 0;
2540
+ if (!BOUNDARY_CACHE_CAPACITY || !parser->tree_root) return;
2541
+ if (parser->tree_count > SIZE_MAX / 2 / sizeof(size_t) - 1) return;
2542
+ size_t *order = ts_calloc(parser->tree_count * 2 + 2, sizeof(*order));
2543
+ if (!order) return;
2544
+ size_t *stack = order + parser->tree_count + 1, length = 0, depth = 0;
2545
+ stack[depth++] = parser->tree_root;
2546
+ while (depth) {
2547
+ const size_t index = stack[--depth];
2548
+ order[length++] = index;
2549
+ const BoundaryTree node = parser->tree[index - 1];
2550
+ if (node.children[1]) stack[depth++] = node.children[1];
2551
+ if (node.children[0]) stack[depth++] = node.children[0];
2552
+ }
2553
+ if (!cache_period(state, parser, order, length)) {
2554
+ for (size_t i = 0; i < length; i++) {
2555
+ const BoundaryOutline outline = parser->tree[order[i] - 1].outline;
2556
+ if (state->cache_count) {
2557
+ BoundaryOutline *last = &state->cache[state->cache_count - 1];
2558
+ const uint32_t count = descriptor_length(last->descriptor);
2559
+ const uint32_t left = descriptor_length(last->children[0]);
2560
+ const uint64_t delta = count > left ? count - left : 0;
2561
+ if (
2562
+ delta &&
2563
+ delta * last->repeat < left &&
2564
+ outline.descriptor == last->descriptor - delta * last->repeat &&
2565
+ outline.children[0] == last->children[0] - delta * last->repeat &&
2566
+ outline.children[1] == last->children[1]
2567
+ ) {
2568
+ last->repeat++;
2569
+ continue;
2570
+ }
2571
+ }
2572
+ if (state->cache_count == BOUNDARY_CACHE_CAPACITY) break;
2573
+ state->cache[state->cache_count++] = outline;
2574
+ }
2575
+ }
2576
+ ts_free(order);
2577
+ }
2578
+
2579
+ static bool take_boundary(
2580
+ BoundaryState *state,
2581
+ uint64_t requested,
2582
+ RecognizedExpression *node
2583
+ ) {
2584
+ if (
2585
+ !state->cache_count ||
2586
+ state->cache[state->cache_index].descriptor != requested
2587
+ ) {
2588
+ state->cache_count = state->cache_index = state->cache_repeats =
2589
+ state->cache_delta = 0;
2590
+ return false;
2591
+ }
2592
+ BoundaryOutline outline = state->cache[state->cache_index];
2593
+ if (state->cache_delta) {
2594
+ if (++state->cache_index == state->cache_count) {
2595
+ state->cache_index = 0;
2596
+ if (--state->cache_repeats) {
2597
+ for (unsigned i = 0; i < state->cache_count; i++) {
2598
+ BoundaryOutline *cached = &state->cache[i];
2599
+ if (cached->repeat & SHIFT_DESCRIPTOR)
2600
+ cached->descriptor -= state->cache_delta;
2601
+ if (cached->repeat & SHIFT_LEFT)
2602
+ cached->children[0] -= state->cache_delta;
2603
+ if (cached->repeat & SHIFT_RIGHT)
2604
+ cached->children[1] -= state->cache_delta;
2605
+ }
2606
+ } else state->cache_count = state->cache_delta = 0;
2607
+ }
2608
+ } else if (--state->cache[0].repeat) {
2609
+ uint32_t delta = descriptor_length(outline.descriptor) -
2610
+ descriptor_length(outline.children[0]);
2611
+ state->cache[0].descriptor -= delta;
2612
+ state->cache[0].children[0] -= delta;
2613
+ } else {
2614
+ state->cache_count--;
2615
+ memmove(
2616
+ state->cache,
2617
+ state->cache + 1,
2618
+ state->cache_count * sizeof(*state->cache)
2619
+ );
2620
+ }
2621
+ *node = outline_expression(outline);
2622
+ return true;
2623
+ }
2624
+
2625
+ static void collect_expression(
2626
+ InputProbe *input,
2627
+ RecognizerInput *parser,
2628
+ size_t count,
2629
+ bool print
2630
+ ) {
2631
+ const bool branches = signature_kind(parser->expected) == RECOGNIZED_BRANCHES;
2632
+ bool expect_operand = true;
2633
+ int questions = 0;
2634
+ while (!parser->failed && (!count || parser->length < count)) {
2635
+ const int token = probe_token(input, expect_operand);
2636
+ if (
2637
+ !token ||
2638
+ character_in(token, "\n;,) ]}{#") ||
2639
+ (token == ':' && !questions && !branches) ||
2640
+ (print && !count && !questions && (token == '>' || token == '|'))
2641
+ )
2642
+ break;
2643
+ if (token == '?') questions++;
2644
+ if (token == ':' && questions) questions--;
2645
+ recognizer_add_token(parser, token);
2646
+ if (token != PROBE_UPDATE)
2647
+ expect_operand = token != PROBE_NAME &&
2648
+ token != PROBE_VALUE &&
2649
+ token != PROBE_TUPLE &&
2650
+ token != PROBE_GETLINE;
2651
+ }
2652
+ }
2653
+
2654
+ static unsigned
2655
+ atom_token(InputProbe *input, BoundaryState *state, bool lvalue) {
2656
+ unsigned token = lvalue ? SELECT_NAME : SELECT_ATOM;
2657
+ probe_restore(input, 0);
2658
+ TSLexer *probe = &input->lexer;
2659
+ if (is_word_start(probe->lookahead)) {
2660
+ const WordAtom atom = word_atom(probe, scan_word_spelling(probe));
2661
+ if (atom == ATOM_CALL) token = SELECT_CALL;
2662
+ if (atom == ATOM_SUBSCRIPT) token = SELECT_SUBSCRIPT;
2663
+ } else if (probe->lookahead == '(') token = SELECT_GROUP;
2664
+ if (
2665
+ token == SELECT_GROUP || token == SELECT_CALL || token == SELECT_SUBSCRIPT
2666
+ ) {
2667
+ probe->advance(probe, false);
2668
+ skip_token_layout(probe);
2669
+ state->pending[0] = probe->lookahead == ')' ? 0 : UNCONSTRAINED;
2670
+ }
2671
+ return token;
2672
+ }
2673
+
2674
+ static unsigned selection_token(
2675
+ RecognizedExpression selected,
2676
+ InputProbe *input,
2677
+ BoundaryState *state
2678
+ ) {
2679
+ switch (selected.kind) {
2680
+ case RECOGNIZED_GET:
2681
+ return selected.children[0] ? SELECT_TARGET_GET : SELECT_GET;
2682
+ case RECOGNIZED_FILE:
2683
+ return SELECT_FILE;
2684
+ case RECOGNIZED_FIELD:
2685
+ return SELECT_FIELD;
2686
+ case RECOGNIZED_PREFIX:
2687
+ return SELECT_PREFIX;
2688
+ case RECOGNIZED_POSTFIX:
2689
+ return SELECT_POSTFIX;
2690
+ case RECOGNIZED_CONCAT:
2691
+ return SELECT_CONCAT;
2692
+ case RECOGNIZED_TUPLE:
2693
+ case 0:
2694
+ return atom_token(input, state, selected.lvalue);
2695
+ case RECOGNIZED_BRANCHES:
2696
+ return SELECT_BRANCHES;
2697
+ case '?':
2698
+ return SELECT_CONDITIONAL;
2699
+ case '|':
2700
+ return SELECT_PIPE;
2701
+ case '=':
2702
+ case PROBE_ASSIGNMENT:
2703
+ return SELECT_ASSIGNMENT;
2704
+ case PROBE_MEMBERSHIP:
2705
+ return descriptor_kind(selected.children[0]) == RECOGNIZED_TUPLE
2706
+ ? SELECT_TUPLE
2707
+ : SELECT_MEMBERSHIP;
2708
+ default:
2709
+ return SELECT_BINARY;
2710
+ }
2711
+ }
2712
+
2713
+ static bool select_boundary(TSLexer *lexer, BoundaryState *state) {
2714
+ const uint64_t requested = state->active;
2715
+ const size_t count =
2716
+ requested == UNCONSTRAINED ? 0 : descriptor_length(requested);
2717
+ InputProbe input = probe_open(lexer);
2718
+ RecognizerInput parser = {
2719
+ .expected = requested == UNCONSTRAINED ? 0 : descriptor_signature(requested)
2720
+ };
2721
+ RecognizedExpression selected = {0};
2722
+ if (!take_boundary(state, requested, &selected)) {
2723
+ collect_expression(&input, &parser, count, state->print);
2724
+ if (
2725
+ parser.length &&
2726
+ (!count || parser.length == count) &&
2727
+ !input.failed &&
2728
+ !parser.failed &&
2729
+ recognizer_parse(&parser)
2730
+ ) {
2731
+ const BoundaryOutline root = parser.tree[parser.tree_root - 1].outline;
2732
+ selected = outline_expression(root);
2733
+ cache_boundary(state, &parser);
2734
+ take_boundary(state, root.descriptor, &selected);
2735
+ }
2736
+ }
2737
+ unsigned token = ERROR_SENTINEL;
2738
+ if (selected.end) {
2739
+ state->unary = selected.unary;
2740
+ state->pending[0] = selected.children[0];
2741
+ state->pending[1] = selected.children[1];
2742
+ token = selection_token(selected, &input, state);
2743
+ }
2744
+ recognizer_destroy(&parser);
2745
+ ts_free(input.characters);
2746
+ state->active = 0;
2747
+ state->selected = token;
2748
+ return token != ERROR_SENTINEL;
2749
+ }
2750
+
2751
+ // Close children before trailing blanks to preserve enclosing node ranges.
2752
+ static bool restoring_requested(const bool *valid) {
2753
+ bool restoring = valid[END_CHILD] || valid[RESUME];
2754
+ for (unsigned i = 0; i < 4; i++) restoring |= valid[RESTORE_FIRST + i];
2755
+ return restoring;
2756
+ }
2757
+
2758
+ static bool boundary_requested(const bool *valid) {
2759
+ for (unsigned i = ROOT; i <= LOCAL_PRINT_ROOT; i++)
2760
+ if (valid[i]) return true;
2761
+ return false;
2762
+ }
2763
+
2764
+ #ifndef LOCAL_EXPRESSIONS
2765
+ #define LOCAL_EXPRESSIONS 1
2766
+ #endif
2767
+
2768
+ static bool local_expression(TSLexer *lexer, bool print) {
2769
+ enum { GETLINE_SEEN = 1, AMBIGUOUS_SEEN = 2 };
2770
+ size_t depth = 0, questions = 0, capacity = 0;
2771
+ bool after_operand = false, separated = false, result = false;
2772
+ unsigned char flags = 0;
2773
+ unsigned char *scopes = NULL;
2774
+ for (;;) {
2775
+ if (!skip_scope_layout(lexer, separated)) goto done;
2776
+ const int32_t character = lexer->lookahead;
2777
+ if (
2778
+ lexer->eof(lexer) ||
2779
+ character == '\n' ||
2780
+ character_in(character, "{};") ||
2781
+ (!depth && character_in(character, ",)]")) ||
2782
+ (!depth && character == ':' && !questions) ||
2783
+ (print && !depth && !questions && character == '>')
2784
+ )
2785
+ break;
2786
+ separated = false;
2787
+ if (character == '"' || (character == '/' && !after_operand)) {
2788
+ if (!skip_literal(lexer)) goto done;
2789
+ after_operand = true;
2790
+ continue;
2791
+ }
2792
+ if (is_word_start(character)) {
2793
+ const WordKind word = scan_word_spelling(lexer);
2794
+ if (word == WORD_KEYWORD) break;
2795
+ if (word == WORD_GETLINE) flags |= GETLINE_SEEN;
2796
+ after_operand = word != WORD_MEMBERSHIP;
2797
+ } else {
2798
+ lexer->advance(lexer, false);
2799
+ if (
2800
+ print &&
2801
+ !depth &&
2802
+ !questions &&
2803
+ character == '|' &&
2804
+ lexer->lookahead != '|'
2805
+ )
2806
+ break;
2807
+ if (character_in(character, "([")) {
2808
+ if (depth == capacity) {
2809
+ unsigned char *grown =
2810
+ grow_array(scopes, &capacity, sizeof(*scopes), 32);
2811
+ if (!grown) goto done;
2812
+ scopes = grown;
2813
+ }
2814
+ scopes[depth++] = flags;
2815
+ flags = 0;
2816
+ after_operand = false;
2817
+ } else if (character_in(character, ")]")) {
2818
+ flags = scopes[--depth];
2819
+ after_operand = true;
2820
+ } else if (
2821
+ character_in(character, "+-") && lexer->lookahead == character
2822
+ ) {
2823
+ lexer->advance(lexer, false);
2824
+ flags |= AMBIGUOUS_SEEN;
2825
+ after_operand = true;
2826
+ } else if (
2827
+ character_in(character, "&|") && lexer->lookahead == character
2828
+ ) {
2829
+ lexer->advance(lexer, false);
2830
+ separated = true;
2831
+ after_operand = false;
2832
+ } else {
2833
+ if (character == '|' || (character == '<' && lexer->lookahead != '='))
2834
+ flags |= AMBIGUOUS_SEEN;
2835
+ if (character == ',') flags = 0;
2836
+ after_operand = is_word_continue(character) || character == '.';
2837
+ separated = character == ',';
2838
+ if (!depth && character == '?') questions++;
2839
+ if (!depth && character == ':' && questions) questions--;
2840
+ }
2841
+ }
2842
+ if (flags == (GETLINE_SEEN | AMBIGUOUS_SEEN)) goto done;
2843
+ }
2844
+ result = depth == 0 && LOCAL_EXPRESSIONS;
2845
+ done:
2846
+ ts_free(scopes);
2847
+ return result;
2848
+ }
2849
+
2850
+ static bool is_for_in_header(InputProbe *probe) {
2851
+ return probe_token(probe, true) == PROBE_NAME &&
2852
+ probe_token(probe, false) == PROBE_MEMBERSHIP &&
2853
+ probe_token(probe, true) == PROBE_NAME &&
2854
+ probe_token(probe, false) == ')';
2855
+ }
2856
+
2857
+ static unsigned root_token(InputProbe *probe, const bool *valid) {
2858
+ if (valid[FOR_IN] && is_for_in_header(probe)) return FOR_IN;
2859
+ probe_restore(probe, 0);
2860
+ if (valid[PRINT_ARGUMENTS] && probe_print_arguments(probe))
2861
+ return PRINT_ARGUMENTS;
2862
+ probe_restore(probe, 0);
2863
+ const int32_t first = probe->lexer.lookahead;
2864
+ if (is_word_start(first)) {
2865
+ const WordKind word = scan_word_spelling(&probe->lexer);
2866
+ if (word == WORD_KEYWORD || word == WORD_MEMBERSHIP) return ERROR_SENTINEL;
2867
+ }
2868
+ const bool print = valid[PRINT_ROOT] || valid[LOCAL_PRINT_ROOT];
2869
+ probe_restore(probe, 0);
2870
+ const bool local = (valid[LOCAL_ROOT] || valid[LOCAL_PRINT_ROOT]) &&
2871
+ local_expression(&probe->lexer, print) &&
2872
+ !probe->failed;
2873
+ if (
2874
+ (!valid[ROOT] && !valid[LOCAL_ROOT] && !print) ||
2875
+ (print && (first == '>' || first == '|')) ||
2876
+ !first ||
2877
+ character_in(first, "\n;,) ]}{:#")
2878
+ )
2879
+ return ERROR_SENTINEL;
2880
+ if (local) return print ? LOCAL_PRINT_ROOT : LOCAL_ROOT;
2881
+ return valid[PRINT_ROOT] ? PRINT_ROOT : ROOT;
2882
+ }
2883
+
2884
+ static bool scan_root(TSLexer *lexer, BoundaryState *state, const bool *valid) {
2885
+ InputProbe probe = probe_open(lexer);
2886
+ const unsigned token = root_token(&probe, valid);
2887
+ ts_free(probe.characters);
2888
+ if (token == LOCAL_ROOT || token == LOCAL_PRINT_ROOT)
2889
+ *state = (BoundaryState){0};
2890
+ else if (token == ROOT || token == PRINT_ROOT)
2891
+ *state = (BoundaryState){
2892
+ .active = UNCONSTRAINED,
2893
+ .print = token == PRINT_ROOT,
2894
+ };
2895
+ return token != ERROR_SENTINEL && boundary_emit(lexer, state, token);
2896
+ }
2897
+
2898
+ static bool
2899
+ scan_boundary(TSLexer *lexer, BoundaryState *state, const bool *valid) {
2900
+ lexer->mark_end(lexer);
2901
+ if (valid[NEXT_ARGUMENT] || valid[BEGIN_CHILD]) {
2902
+ if (lexer->lookahead == '\n') return false;
2903
+ }
2904
+ if (
2905
+ valid[ROOT] ||
2906
+ valid[PRINT_ROOT] ||
2907
+ valid[FOR_IN] ||
2908
+ valid[PRINT_ARGUMENTS] ||
2909
+ valid[LOCAL_ROOT] ||
2910
+ valid[LOCAL_PRINT_ROOT]
2911
+ )
2912
+ return scan_root(lexer, state, valid);
2913
+ if (valid[NEXT_ARGUMENT]) {
2914
+ state->pending[0] = UNCONSTRAINED;
2915
+ return boundary_emit(lexer, state, NEXT_ARGUMENT);
2916
+ }
2917
+ if (valid[RESUME]) {
2918
+ state->pending[0] = restored_descriptor(state->carry);
2919
+ state->pending[1] = 0;
2920
+ state->carry = 0;
2921
+ state->phase = BOUNDARY_READY;
2922
+ return boundary_emit(lexer, state, RESUME);
2923
+ }
2924
+ if (valid[END_CHILD]) {
2925
+ boundary_reset(state, 0, BOUNDARY_RESTORING);
2926
+ return boundary_emit(lexer, state, END_CHILD);
2927
+ }
2928
+ for (unsigned digit = 0; digit < 4; digit++)
2929
+ if (valid[RESTORE_FIRST + digit]) {
2930
+ state->carry = state->carry * 4 + digit;
2931
+ return boundary_emit(lexer, state, RESTORE_FIRST + digit);
2932
+ }
2933
+ if (valid[BEGIN_CHILD]) {
2934
+ if (state->phase != BOUNDARY_SAVING) {
2935
+ if (!state->pending[0]) return false;
2936
+ state->next = state->pending[0];
2937
+ state->carry = saved_descriptor(state->pending[1]);
2938
+ state->phase = BOUNDARY_SAVING;
2939
+ }
2940
+ if (state->carry) {
2941
+ unsigned digit = state->carry & 3;
2942
+ state->carry >>= 2;
2943
+ return boundary_emit(lexer, state, SAVE_FIRST + digit);
2944
+ }
2945
+ uint64_t active = state->next;
2946
+ boundary_reset(state, active, BOUNDARY_READY);
2947
+ if (active == UNCONSTRAINED)
2948
+ state->cache_count = state->cache_index = state->cache_repeats =
2949
+ state->cache_delta = 0;
2950
+ return boundary_emit(lexer, state, BEGIN_CHILD);
2951
+ }
2952
+ if (state->active && !select_boundary(lexer, state)) return false;
2953
+ if (valid[CLASS_UNARY] || valid[CLASS_NON_UNARY])
2954
+ return boundary_emit(
2955
+ lexer,
2956
+ state,
2957
+ state->unary ? CLASS_UNARY : CLASS_NON_UNARY
2958
+ );
2959
+ const unsigned token = state->selected;
2960
+ if (token < SELECT_ATOM || token > SELECT_TUPLE || !valid[token])
2961
+ return false;
2962
+ state->selected = 0;
2963
+ return boundary_emit(lexer, state, token);
2964
+ }
2965
+
2966
+ static bool scan_literal(
2967
+ ScannerState *state,
2968
+ TSLexer *lexer,
2969
+ const bool *valid_symbols,
2970
+ bool recovering
2971
+ ) {
2972
+ lexer->mark_end(lexer);
2973
+ if (lexer->lookahead == '\\') return scan_backslash(state, lexer);
2974
+ if (lexer->lookahead == '\n')
2975
+ return emit_literal_break(state, lexer, valid_symbols);
2976
+ if (is_ere_mode(state->mode))
2977
+ return scan_ere_context(state, lexer, valid_symbols, recovering);
2978
+ if (lexer->lookahead == '"' && valid_symbols[STRING_CLOSING])
2979
+ return emit_delimiter(state, lexer, LEXICAL_MODE_OUTSIDE, STRING_CLOSING);
2980
+ return skip_blanks_before_newline(lexer, NULL) &&
2981
+ emit_literal_break(state, lexer, valid_symbols);
2982
+ }
2983
+
2984
+ static bool scan_outside(
2985
+ ScannerState *state,
2986
+ TSLexer *lexer,
2987
+ const bool *valid_symbols,
2988
+ bool recovering
2989
+ ) {
2990
+ if (!recovering && restoring_requested(valid_symbols))
2991
+ return scan_boundary(lexer, &state->boundary, valid_symbols);
2992
+ while (is_ascii_blank(lexer->lookahead)) lexer->advance(lexer, true);
2993
+ lexer->mark_end(lexer);
2994
+ if (lexer->lookahead == '\\') return scan_backslash(state, lexer);
2995
+ if (lexer->lookahead == '#' && valid_symbols[COMMENT])
2996
+ return scan_comment(lexer);
2997
+ if (!recovering && boundary_requested(valid_symbols))
2998
+ return scan_boundary(lexer, &state->boundary, valid_symbols);
2999
+ if (lexer->lookahead == '"' && valid_symbols[STRING_OPENING])
3000
+ return emit_delimiter(state, lexer, LEXICAL_MODE_STRING, STRING_OPENING);
3001
+ if (is_word_start(lexer->lookahead))
3002
+ return scan_contextual_word(lexer, recovering);
3003
+ if (lexer->lookahead == '|')
3004
+ return scan_getline_pipe(lexer, valid_symbols, recovering);
3005
+ if (lexer->lookahead == '$' || lexer->lookahead == '!')
3006
+ return scan_prefix_operator(lexer, valid_symbols, recovering);
3007
+ if (recovering) return false;
3008
+ if (lexer->lookahead == '/')
3009
+ return scan_slash_start(state, lexer, valid_symbols);
3010
+ return scan_sign(lexer, valid_symbols);
3011
+ }
3012
+
3013
+ // Tree-sitter passes NULL to every entry point after allocation failure.
3014
+ void *tree_sitter_posix_awk_external_scanner_create(void) {
3015
+ return ts_calloc(1, sizeof(ScannerState));
3016
+ }
3017
+
3018
+ void tree_sitter_posix_awk_external_scanner_destroy(void *payload) {
3019
+ ts_free(payload);
3020
+ }
3021
+
3022
+ static void write_uint(char *buffer, uint64_t value, unsigned bytes) {
3023
+ for (unsigned i = 0; i < bytes; i++) buffer[i] = (char)(value >> (8 * i));
3024
+ }
3025
+
3026
+ static uint64_t read_uint(const char *buffer, unsigned bytes) {
3027
+ uint64_t value = 0;
3028
+ for (unsigned i = 0; i < bytes; i++)
3029
+ value |= (uint64_t)(unsigned char)buffer[i] << (8 * i);
3030
+ return value;
3031
+ }
3032
+
3033
+ unsigned
3034
+ tree_sitter_posix_awk_external_scanner_serialize(void *payload, char *buffer) {
3035
+ ScannerState *scanner = payload;
3036
+ if (!scanner) return 0;
3037
+ BoundaryState *state = &scanner->boundary;
3038
+ const uint64_t values[] = {
3039
+ state->active,
3040
+ state->pending[0],
3041
+ state->pending[1],
3042
+ state->next,
3043
+ state->carry
3044
+ };
3045
+ buffer[0] = (char)scanner->mode;
3046
+ buffer[1] = (char)state->phase;
3047
+ buffer[2] = (char)state->selected;
3048
+ buffer[3] = (char)state->unary;
3049
+ buffer[4] = (char)state->print;
3050
+ for (size_t i = 0; i < ARRAY_LENGTH(values); i++)
3051
+ write_uint(buffer + STATE_VALUES_OFFSET + i * 8, values[i], 8);
3052
+ buffer[STATE_CACHE_COUNT_OFFSET] = (char)state->cache_count;
3053
+ buffer[STATE_CACHE_INDEX_OFFSET] = (char)state->cache_index;
3054
+ write_uint(buffer + STATE_CACHE_REPEATS_OFFSET, state->cache_repeats, 4);
3055
+ write_uint(buffer + STATE_CACHE_DELTA_OFFSET, state->cache_delta, 4);
3056
+ write_uint(buffer + STATE_DECISION_HASH_OFFSET, state->decision_hash, 8);
3057
+ for (unsigned i = 0; i < state->cache_count; i++) {
3058
+ const BoundaryOutline *node = &state->cache[i];
3059
+ char *entry = buffer + STATE_HEADER_BYTES + i * STATE_OUTLINE_BYTES;
3060
+ write_uint(entry, node->descriptor, 8);
3061
+ write_uint(entry + 8, node->children[0], 8);
3062
+ write_uint(entry + 16, node->children[1], 8);
3063
+ write_uint(entry + 24, node->repeat, 4);
3064
+ }
3065
+ return STATE_HEADER_BYTES + state->cache_count * STATE_OUTLINE_BYTES;
3066
+ }
3067
+
3068
+ void tree_sitter_posix_awk_external_scanner_deserialize(
3069
+ void *payload,
3070
+ const char *buffer,
3071
+ unsigned length
3072
+ ) {
3073
+ ScannerState *scanner = payload;
3074
+ if (!scanner) return;
3075
+ *scanner = (ScannerState){0};
3076
+ if (
3077
+ length < STATE_HEADER_BYTES ||
3078
+ (unsigned char)buffer[0] > LEXICAL_MODE_CONTINUED_NEWLINE ||
3079
+ (unsigned char)buffer[1] > BOUNDARY_RESTORING
3080
+ )
3081
+ return;
3082
+ const unsigned count = (unsigned char)buffer[STATE_CACHE_COUNT_OFFSET];
3083
+ if (
3084
+ count > BOUNDARY_CACHE_CAPACITY ||
3085
+ length != STATE_HEADER_BYTES + count * STATE_OUTLINE_BYTES
3086
+ )
3087
+ return;
3088
+ uint64_t values[5] = {0};
3089
+ for (size_t i = 0; i < ARRAY_LENGTH(values); i++)
3090
+ values[i] = read_uint(buffer + STATE_VALUES_OFFSET + i * 8, 8);
3091
+ scanner->mode = (LexicalMode)buffer[0];
3092
+ scanner->boundary = (BoundaryState){
3093
+ .active = values[0],
3094
+ .pending = {values[1], values[2]},
3095
+ .next = values[3],
3096
+ .carry = values[4],
3097
+ .phase = (enum BoundaryPhase)buffer[1],
3098
+ .selected = (unsigned char)buffer[2],
3099
+ .unary = buffer[3] != 0,
3100
+ .print = buffer[4] != 0,
3101
+ .decision_hash = read_uint(buffer + STATE_DECISION_HASH_OFFSET, 8),
3102
+ .cache_count = count,
3103
+ .cache_index = (unsigned char)buffer[STATE_CACHE_INDEX_OFFSET],
3104
+ .cache_repeats =
3105
+ (uint32_t)read_uint(buffer + STATE_CACHE_REPEATS_OFFSET, 4),
3106
+ .cache_delta = (uint32_t)read_uint(buffer + STATE_CACHE_DELTA_OFFSET, 4)
3107
+ };
3108
+ if (scanner->boundary.cache_index >= (count ? count : 1)) {
3109
+ *scanner = (ScannerState){0};
3110
+ return;
3111
+ }
3112
+ for (unsigned i = 0; i < count; i++) {
3113
+ const char *entry = buffer + STATE_HEADER_BYTES + i * STATE_OUTLINE_BYTES;
3114
+ scanner->boundary.cache[i] = (BoundaryOutline){
3115
+ .descriptor = read_uint(entry, 8),
3116
+ .children = {read_uint(entry + 8, 8), read_uint(entry + 16, 8)},
3117
+ .repeat = (uint32_t)read_uint(entry + 24, 4)
3118
+ };
3119
+ }
3120
+ }
3121
+
3122
+ bool tree_sitter_posix_awk_external_scanner_scan(
3123
+ void *payload,
3124
+ TSLexer *lexer,
3125
+ const bool *valid_symbols
3126
+ ) {
3127
+ ScannerState *state = payload;
3128
+ if (!state) return false;
3129
+ const bool recovering = valid_symbols[ERROR_SENTINEL];
3130
+ switch (state->mode) {
3131
+ case LEXICAL_MODE_OUTSIDE:
3132
+ return scan_outside(state, lexer, valid_symbols, recovering);
3133
+ case LEXICAL_MODE_CONTINUED_NEWLINE:
3134
+ return lexer->lookahead == '\n' &&
3135
+ valid_symbols[CONTINUATION_NEWLINE] &&
3136
+ emit_delimiter(
3137
+ state,
3138
+ lexer,
3139
+ LEXICAL_MODE_OUTSIDE,
3140
+ CONTINUATION_NEWLINE
3141
+ );
3142
+ default:
3143
+ return scan_literal(state, lexer, valid_symbols, recovering);
694
3144
  }
695
- return scan_operator(lexer, valid_symbols);
696
3145
  }