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