@willbooster/tree-sitter-bash 1.1.0 → 1.1.2
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/README.md +2 -0
- package/grammar.js +13 -11
- package/package.json +3 -3
- package/prebuilds/darwin-arm64/tree-sitter-bash.node +0 -0
- package/prebuilds/darwin-x64/tree-sitter-bash.node +0 -0
- package/prebuilds/linux-arm64/tree-sitter-bash.node +0 -0
- package/prebuilds/linux-x64/tree-sitter-bash.node +0 -0
- package/src/grammar.json +13 -13
- package/src/parser.c +7624 -7775
- package/src/scanner.c +131 -74
- package/tree-sitter-bash.wasm +0 -0
- package/tree-sitter.json +1 -1
package/src/scanner.c
CHANGED
|
@@ -54,8 +54,11 @@ typedef struct {
|
|
|
54
54
|
uint8_t backtick_depth;
|
|
55
55
|
// The closer (`)`, `}`, or `]`) of each open substitution, innermost last.
|
|
56
56
|
Array(uint8_t) closers;
|
|
57
|
-
//
|
|
58
|
-
|
|
57
|
+
// How many closers were open when the backquotes opened; the ones after them are nested inside.
|
|
58
|
+
uint16_t backtick_closers;
|
|
59
|
+
// A heredoc body ended before the end of its line (at a `${ list; }` closer, or at a delimiter
|
|
60
|
+
// followed by more code); the newline that ended the command's line began the body, so a
|
|
61
|
+
// terminator is still owed before what follows.
|
|
59
62
|
bool terminator_owed;
|
|
60
63
|
// The last token of this scanner was a line continuation that ended a line; nothing concatenates at
|
|
61
64
|
// the start of the next one.
|
|
@@ -74,7 +77,11 @@ static inline bool is_metacharacter(int32_t c) {
|
|
|
74
77
|
return c == '|' || c == '&' || c == ';' || c == '(' || c == ')' || c == '<' || c == '>';
|
|
75
78
|
}
|
|
76
79
|
|
|
77
|
-
|
|
80
|
+
// Like bash, only a space and a tab are blanks; every other whitespace character, CR included, is part
|
|
81
|
+
// of a word.
|
|
82
|
+
static inline bool is_blank(int32_t c) { return c == ' ' || c == '\t'; }
|
|
83
|
+
|
|
84
|
+
static inline bool is_separator(int32_t c) { return is_blank(c) || c == '\n'; }
|
|
78
85
|
|
|
79
86
|
static inline bool is_name_start(int32_t c) { return iswalpha(c) || c == '_'; }
|
|
80
87
|
|
|
@@ -123,13 +130,14 @@ static unsigned decode_varint(const char *in, unsigned available, int32_t *c) {
|
|
|
123
130
|
return 0;
|
|
124
131
|
}
|
|
125
132
|
|
|
126
|
-
// Serialized layout: backtick depth, the
|
|
133
|
+
// Serialized layout: backtick depth, the closer count when the backquotes opened (uint16), the owed-terminator and
|
|
134
|
+
// line-break flags, the closers of the open substitutions
|
|
127
135
|
// (uint16 count, then one byte each), and heredoc count, then per heredoc its flags, depth (uint16),
|
|
128
136
|
// delimiter length (uint16), and the delimiter as varints.
|
|
129
137
|
#define HEREDOC_HEADER_SIZE (3 + 2 * sizeof(uint16_t))
|
|
130
138
|
|
|
131
139
|
static unsigned serialized_size(Scanner *scanner) {
|
|
132
|
-
unsigned size = 3 + sizeof(uint16_t) + scanner->closers.size;
|
|
140
|
+
unsigned size = 3 + 2 * sizeof(uint16_t) + scanner->closers.size;
|
|
133
141
|
for (uint32_t i = 0; i < scanner->heredocs.size; i++) {
|
|
134
142
|
Heredoc *heredoc = array_get(&scanner->heredocs, i);
|
|
135
143
|
size += HEREDOC_HEADER_SIZE;
|
|
@@ -149,6 +157,7 @@ static void scanner_reset(Scanner *scanner) {
|
|
|
149
157
|
array_clear(&scanner->heredocs);
|
|
150
158
|
scanner->backtick_depth = 0;
|
|
151
159
|
array_clear(&scanner->closers);
|
|
160
|
+
scanner->backtick_closers = 0;
|
|
152
161
|
scanner->terminator_owed = false;
|
|
153
162
|
scanner->after_line_break = false;
|
|
154
163
|
}
|
|
@@ -221,12 +230,23 @@ static bool scan_heredoc_content(Scanner *scanner, TSLexer *lexer, uint32_t inde
|
|
|
221
230
|
at_line_start = false;
|
|
222
231
|
lexer->mark_end(lexer);
|
|
223
232
|
// Like bash, compare the delimiter with the line after joining backslash-newlines and
|
|
224
|
-
// then stripping the leading tabs of `<<-`.
|
|
225
|
-
// newline.
|
|
233
|
+
// then stripping the leading tabs of `<<-`.
|
|
226
234
|
bool consumed = false;
|
|
227
235
|
bool escaped = false;
|
|
228
236
|
uint32_t matched = 0;
|
|
237
|
+
// Inside `$( )`, `<( )`, and `>( )`, bash also ends the body at a line that merely starts with
|
|
238
|
+
// the delimiter when a `)` follows anywhere on that line, and reads the rest of the line as
|
|
239
|
+
// code. Such a line ends the content token before it and the end token after the delimiter.
|
|
240
|
+
// Where the backquotes are innermost, bash fixes their extent before it reads the body, so
|
|
241
|
+
// the rule is off.
|
|
242
|
+
uint16_t outside_backquotes = scanner->backtick_depth > 0 ? scanner->backtick_closers : 0;
|
|
243
|
+
bool in_parenthesized =
|
|
244
|
+
scanner->closers.size > outside_backquotes && *array_back(&scanner->closers) == ')';
|
|
245
|
+
bool prefix_marked = false;
|
|
246
|
+
int32_t escaped_after_prefix = 0;
|
|
229
247
|
// At the end of input the lookahead is 0, which a delimiter holding NUL would otherwise match.
|
|
248
|
+
// Bash compares one line at a time, so a delimiter holding a newline (`<<"E` + newline +
|
|
249
|
+
// `OF"`) never matches.
|
|
230
250
|
for (;;) {
|
|
231
251
|
if (matched == 0 && heredoc->allows_indent && lexer->lookahead == '\t') {
|
|
232
252
|
advance(lexer);
|
|
@@ -234,31 +254,34 @@ static bool scan_heredoc_content(Scanner *scanner, TSLexer *lexer, uint32_t inde
|
|
|
234
254
|
} else if (lexer->lookahead == '\\' && !heredoc->is_raw) {
|
|
235
255
|
advance(lexer);
|
|
236
256
|
consumed = true;
|
|
237
|
-
bool escaped_cr = lexer->lookahead == '\r';
|
|
238
|
-
if (escaped_cr) {
|
|
239
|
-
advance(lexer);
|
|
240
|
-
}
|
|
241
257
|
if (lexer->lookahead != '\n' || lexer->eof(lexer)) {
|
|
242
|
-
// An escape rather than a line continuation: the escaped character
|
|
243
|
-
|
|
244
|
-
|
|
258
|
+
// An escape rather than a line continuation: the escaped character is content.
|
|
259
|
+
if (!lexer->eof(lexer)) {
|
|
260
|
+
if (prefix_marked) {
|
|
261
|
+
escaped_after_prefix = lexer->lookahead;
|
|
262
|
+
}
|
|
245
263
|
advance(lexer);
|
|
246
264
|
}
|
|
247
265
|
escaped = true;
|
|
248
266
|
break;
|
|
249
267
|
}
|
|
250
268
|
advance(lexer);
|
|
251
|
-
} else if (matched < heredoc->delimiter.size && !lexer->eof(lexer) &&
|
|
269
|
+
} else if (matched < heredoc->delimiter.size && !lexer->eof(lexer) && lexer->lookahead != '\n' &&
|
|
252
270
|
lexer->lookahead == *array_get(&heredoc->delimiter, matched)) {
|
|
253
271
|
advance(lexer);
|
|
254
272
|
matched++;
|
|
273
|
+
if (matched == heredoc->delimiter.size && in_parenthesized) {
|
|
274
|
+
if (did_advance) {
|
|
275
|
+
lexer->result_symbol = HEREDOC_CONTENT;
|
|
276
|
+
return true;
|
|
277
|
+
}
|
|
278
|
+
lexer->mark_end(lexer);
|
|
279
|
+
prefix_marked = true;
|
|
280
|
+
}
|
|
255
281
|
} else {
|
|
256
282
|
break;
|
|
257
283
|
}
|
|
258
284
|
}
|
|
259
|
-
if (!escaped && lexer->lookahead == '\r') {
|
|
260
|
-
advance(lexer);
|
|
261
|
-
}
|
|
262
285
|
// Inside a substitution, bash also ends the body at a delimiter directly followed by the
|
|
263
286
|
// innermost substitution's closer (`)` or `}`) or a closing backquote.
|
|
264
287
|
bool ends_line = lexer->lookahead == '\n' || lexer->eof(lexer) ||
|
|
@@ -275,6 +298,24 @@ static bool scan_heredoc_content(Scanner *scanner, TSLexer *lexer, uint32_t inde
|
|
|
275
298
|
remove_heredoc(scanner, index);
|
|
276
299
|
return true;
|
|
277
300
|
}
|
|
301
|
+
if (prefix_marked) {
|
|
302
|
+
// The line is read after joining its backslash-newlines, as for the delimiter.
|
|
303
|
+
while (!lexer->eof(lexer) && lexer->lookahead != '\n' && lexer->lookahead != ')') {
|
|
304
|
+
bool backslash = lexer->lookahead == '\\' && !heredoc->is_raw;
|
|
305
|
+
advance(lexer);
|
|
306
|
+
if (backslash && lexer->lookahead == '\n') {
|
|
307
|
+
advance(lexer);
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
if (escaped_after_prefix == ')' || lexer->lookahead == ')') {
|
|
311
|
+
lexer->result_symbol = HEREDOC_END;
|
|
312
|
+
scanner->terminator_owed = true;
|
|
313
|
+
remove_heredoc(scanner, index);
|
|
314
|
+
} else {
|
|
315
|
+
lexer->result_symbol = HEREDOC_CONTENT;
|
|
316
|
+
}
|
|
317
|
+
return true;
|
|
318
|
+
}
|
|
278
319
|
// The characters read while trying the delimiter are content; an empty attempt must not
|
|
279
320
|
// count, or a zero-width content token would repeat forever.
|
|
280
321
|
did_advance = did_advance || consumed || matched > 0;
|
|
@@ -299,20 +340,25 @@ static bool scan_heredoc_content(Scanner *scanner, TSLexer *lexer, uint32_t inde
|
|
|
299
340
|
case '\\':
|
|
300
341
|
advance(lexer);
|
|
301
342
|
did_advance = true;
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
advance(lexer);
|
|
309
|
-
}
|
|
310
|
-
} else if (!lexer->eof(lexer)) {
|
|
311
|
-
advance(lexer);
|
|
312
|
-
}
|
|
343
|
+
// Inside backquotes a backslash also hides a backquote from a quoted body's end.
|
|
344
|
+
if (!lexer->eof(lexer) &&
|
|
345
|
+
(!heredoc->is_raw || (scanner->backtick_depth > 0 && lexer->lookahead == '`'))) {
|
|
346
|
+
// A backslash-newline joins lines before the delimiter comparison, while a
|
|
347
|
+
// backslash-CR escapes only the CR, so the LF after it still ends the line.
|
|
348
|
+
advance(lexer);
|
|
313
349
|
}
|
|
314
350
|
break;
|
|
315
351
|
case '`':
|
|
352
|
+
// Bash ends backquotes at the first unescaped backquote, even inside a heredoc body,
|
|
353
|
+
// and ends the body there.
|
|
354
|
+
if (scanner->backtick_depth > 0) {
|
|
355
|
+
lexer->mark_end(lexer);
|
|
356
|
+
lexer->result_symbol = did_advance ? HEREDOC_CONTENT : HEREDOC_END;
|
|
357
|
+
if (!did_advance) {
|
|
358
|
+
remove_heredoc(scanner, index);
|
|
359
|
+
}
|
|
360
|
+
return true;
|
|
361
|
+
}
|
|
316
362
|
if (!heredoc->is_raw) {
|
|
317
363
|
lexer->mark_end(lexer);
|
|
318
364
|
lexer->result_symbol = HEREDOC_CONTENT;
|
|
@@ -446,16 +492,8 @@ static bool scan_heredoc_start(Scanner *scanner, TSLexer *lexer) {
|
|
|
446
492
|
continue;
|
|
447
493
|
}
|
|
448
494
|
// Inside double quotes a backslash-newline still only continues the line.
|
|
449
|
-
if (quote == '"' && c == '\\' &&
|
|
450
|
-
|
|
451
|
-
advance(lexer);
|
|
452
|
-
}
|
|
453
|
-
if (lexer->lookahead == '\n') {
|
|
454
|
-
advance(lexer);
|
|
455
|
-
continue;
|
|
456
|
-
}
|
|
457
|
-
array_push(&heredoc->delimiter, '\\');
|
|
458
|
-
array_push(&heredoc->delimiter, '\r');
|
|
495
|
+
if (quote == '"' && c == '\\' && lexer->lookahead == '\n') {
|
|
496
|
+
advance(lexer);
|
|
459
497
|
continue;
|
|
460
498
|
}
|
|
461
499
|
if (quote == '"' && c == '\\' &&
|
|
@@ -469,7 +507,7 @@ static bool scan_heredoc_start(Scanner *scanner, TSLexer *lexer) {
|
|
|
469
507
|
}
|
|
470
508
|
// Inside backquotes an unquoted backquote closes the substitution; at top level it is part of
|
|
471
509
|
// the word (`<<EO`true`F`).
|
|
472
|
-
if (
|
|
510
|
+
if (is_separator(c) || is_metacharacter(c) || (c == '`' && scanner->backtick_depth > 0)) {
|
|
473
511
|
break;
|
|
474
512
|
}
|
|
475
513
|
advance(lexer);
|
|
@@ -486,15 +524,8 @@ static bool scan_heredoc_start(Scanner *scanner, TSLexer *lexer) {
|
|
|
486
524
|
heredoc->is_raw = true;
|
|
487
525
|
} else if (c == '\\') {
|
|
488
526
|
// A backslash-newline only continues the line; any other backslash quotes the delimiter.
|
|
489
|
-
bool carriage_return = lexer->lookahead == '\r';
|
|
490
|
-
if (carriage_return) {
|
|
491
|
-
advance(lexer);
|
|
492
|
-
}
|
|
493
527
|
if (lexer->lookahead == '\n') {
|
|
494
528
|
advance(lexer);
|
|
495
|
-
} else if (carriage_return) {
|
|
496
|
-
heredoc->is_raw = true;
|
|
497
|
-
array_push(&heredoc->delimiter, '\r');
|
|
498
529
|
} else if (!lexer->eof(lexer)) {
|
|
499
530
|
heredoc->is_raw = true;
|
|
500
531
|
array_push(&heredoc->delimiter, lexer->lookahead);
|
|
@@ -520,8 +551,8 @@ static bool scan_heredoc_start(Scanner *scanner, TSLexer *lexer) {
|
|
|
520
551
|
return true;
|
|
521
552
|
}
|
|
522
553
|
|
|
523
|
-
|
|
524
|
-
|
|
554
|
+
// Scans `<<` or `<<-` after its first `<` has been read.
|
|
555
|
+
static bool scan_heredoc_arrow_after_first(Scanner *scanner, TSLexer *lexer, const bool *valid_symbols) {
|
|
525
556
|
if (lexer->lookahead != '<') {
|
|
526
557
|
return false;
|
|
527
558
|
}
|
|
@@ -553,7 +584,12 @@ static bool scan_heredoc_arrow(Scanner *scanner, TSLexer *lexer, const bool *val
|
|
|
553
584
|
return true;
|
|
554
585
|
}
|
|
555
586
|
|
|
556
|
-
|
|
587
|
+
static bool scan_heredoc_arrow(Scanner *scanner, TSLexer *lexer, const bool *valid_symbols) {
|
|
588
|
+
advance(lexer);
|
|
589
|
+
return scan_heredoc_arrow_after_first(scanner, lexer, valid_symbols);
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
// The right side of `=~` extends to an unquoted space, tab, or newline outside parentheses.
|
|
557
593
|
static bool scan_regex(TSLexer *lexer) {
|
|
558
594
|
while (is_blank(lexer->lookahead)) {
|
|
559
595
|
skip(lexer);
|
|
@@ -563,7 +599,7 @@ static bool scan_regex(TSLexer *lexer) {
|
|
|
563
599
|
while (!lexer->eof(lexer)) {
|
|
564
600
|
lexer->mark_end(lexer);
|
|
565
601
|
int32_t c = lexer->lookahead;
|
|
566
|
-
if (depth == 0 &&
|
|
602
|
+
if (depth == 0 && is_separator(c)) {
|
|
567
603
|
break;
|
|
568
604
|
}
|
|
569
605
|
// Like bash, treat `|` and parentheses as regex characters, while other metacharacters end
|
|
@@ -624,7 +660,7 @@ static bool scan_string_content(TSLexer *lexer) {
|
|
|
624
660
|
}
|
|
625
661
|
|
|
626
662
|
static inline bool is_word_break(int32_t c) {
|
|
627
|
-
return
|
|
663
|
+
return is_separator(c) || is_metacharacter(c) || c == '"' || c == '\'' || c == '`' || c == '$' || c == '\\';
|
|
628
664
|
}
|
|
629
665
|
|
|
630
666
|
static inline bool is_extglob_operator(int32_t c) { return c == '?' || c == '*' || c == '+' || c == '@' || c == '!'; }
|
|
@@ -647,7 +683,7 @@ static GroupKind scan_extglob_group(TSLexer *lexer) {
|
|
|
647
683
|
advance(lexer);
|
|
648
684
|
if (c == '\\') {
|
|
649
685
|
// The word token's literal groups cannot hold a line continuation.
|
|
650
|
-
if (lexer->eof(lexer) || lexer->lookahead == '\n'
|
|
686
|
+
if (lexer->eof(lexer) || lexer->lookahead == '\n') {
|
|
651
687
|
return lexer->eof(lexer) ? GROUP_UNSUPPORTED : GROUP_STRUCTURED;
|
|
652
688
|
}
|
|
653
689
|
advance(lexer);
|
|
@@ -683,7 +719,7 @@ static bool continue_extglob_prefix(TSLexer *lexer, bool consumed) {
|
|
|
683
719
|
} else if (c == '\\') {
|
|
684
720
|
advance(lexer);
|
|
685
721
|
// A backslash-newline continues the line; the word token handles that.
|
|
686
|
-
if (lexer->eof(lexer) || lexer->lookahead == '\n'
|
|
722
|
+
if (lexer->eof(lexer) || lexer->lookahead == '\n') {
|
|
687
723
|
return false;
|
|
688
724
|
}
|
|
689
725
|
advance(lexer);
|
|
@@ -814,9 +850,6 @@ static bool at_closing_reserved_word(TSLexer *lexer) {
|
|
|
814
850
|
if (lexer->lookahead == '\\') {
|
|
815
851
|
// Bash joins a backslash-newline before it checks where the word ends (`then\` + newline).
|
|
816
852
|
advance(lexer);
|
|
817
|
-
if (lexer->lookahead == '\r') {
|
|
818
|
-
advance(lexer);
|
|
819
|
-
}
|
|
820
853
|
if (lexer->lookahead != '\n') {
|
|
821
854
|
return false;
|
|
822
855
|
}
|
|
@@ -829,7 +862,7 @@ static bool at_closing_reserved_word(TSLexer *lexer) {
|
|
|
829
862
|
}
|
|
830
863
|
}
|
|
831
864
|
if (length == 0 || length == sizeof(word) ||
|
|
832
|
-
!(lexer->eof(lexer) ||
|
|
865
|
+
!(lexer->eof(lexer) || is_separator(lexer->lookahead) || is_metacharacter(lexer->lookahead))) {
|
|
833
866
|
return false;
|
|
834
867
|
}
|
|
835
868
|
for (size_t i = 0; i < sizeof(words) / sizeof(words[0]); i++) {
|
|
@@ -852,7 +885,7 @@ static bool scan(Scanner *scanner, TSLexer *lexer, const bool *valid_symbols) {
|
|
|
852
885
|
|
|
853
886
|
if (scanner->terminator_owed) {
|
|
854
887
|
scanner->terminator_owed = false;
|
|
855
|
-
if (valid_symbols[NEWLINE]
|
|
888
|
+
if (valid_symbols[NEWLINE]) {
|
|
856
889
|
lexer->result_symbol = NEWLINE;
|
|
857
890
|
return true;
|
|
858
891
|
}
|
|
@@ -879,7 +912,8 @@ static bool scan(Scanner *scanner, TSLexer *lexer, const bool *valid_symbols) {
|
|
|
879
912
|
if (scanner->heredocs.size > 0) {
|
|
880
913
|
int32_t active = active_heredoc_index(scanner);
|
|
881
914
|
if (active >= 0 && (valid_symbols[HEREDOC_CONTENT] || valid_symbols[HEREDOC_END]) &&
|
|
882
|
-
!(lexer->lookahead == '`' && !array_get(&scanner->heredocs, active)->is_raw
|
|
915
|
+
!(lexer->lookahead == '`' && !array_get(&scanner->heredocs, active)->is_raw &&
|
|
916
|
+
scanner->backtick_depth == 0)) {
|
|
883
917
|
return scan_heredoc_content(scanner, lexer, (uint32_t)active);
|
|
884
918
|
}
|
|
885
919
|
// A newline inside a string is part of it, so bodies start only outside strings.
|
|
@@ -918,16 +952,29 @@ static bool scan(Scanner *scanner, TSLexer *lexer, const bool *valid_symbols) {
|
|
|
918
952
|
// next line. The column costs a scan back to the line start, so it is computed only right after
|
|
919
953
|
// such a continuation and only where a concatenation could start; a concatenation then clears the
|
|
920
954
|
// flag, which bounds the scans to one per continuation.
|
|
921
|
-
|
|
955
|
+
// Like bash, `a<(cmd)` is one word. `<` and `>` end a word part otherwise, so the concatenation
|
|
956
|
+
// needs a look at the next character, and a `<<` read along the way is scanned as a heredoc arrow.
|
|
957
|
+
if (valid_symbols[CONCAT] && (first == '<' || first == '>') &&
|
|
958
|
+
!(after_line_break && lexer->get_column(lexer) == 0)) {
|
|
959
|
+
lexer->mark_end(lexer);
|
|
960
|
+
advance(lexer);
|
|
961
|
+
if (lexer->lookahead == '(') {
|
|
962
|
+
lexer->result_symbol = CONCAT;
|
|
963
|
+
return true;
|
|
964
|
+
}
|
|
965
|
+
if (first == '<' && (valid_symbols[HEREDOC_ARROW] || valid_symbols[HEREDOC_ARROW_DASH])) {
|
|
966
|
+
return scan_heredoc_arrow_after_first(scanner, lexer, valid_symbols);
|
|
967
|
+
}
|
|
968
|
+
return false;
|
|
969
|
+
}
|
|
970
|
+
|
|
971
|
+
if (valid_symbols[CONCAT] && !at_eof && !is_separator(first) && !is_metacharacter(first) &&
|
|
922
972
|
!(after_line_break && lexer->get_column(lexer) == 0)) {
|
|
923
973
|
int32_t c = first;
|
|
924
974
|
if (c == '\\' && lexer->lookahead == '\\') {
|
|
925
975
|
// A backslash-newline joins lines, so the word continues only if the next line does.
|
|
926
976
|
lexer->mark_end(lexer);
|
|
927
977
|
advance(lexer);
|
|
928
|
-
if (lexer->lookahead == '\r') {
|
|
929
|
-
advance(lexer);
|
|
930
|
-
}
|
|
931
978
|
// Bash drops a backslash at the end of input like a continuation.
|
|
932
979
|
if (lexer->eof(lexer)) {
|
|
933
980
|
lexer->mark_end(lexer);
|
|
@@ -948,9 +995,6 @@ static bool scan(Scanner *scanner, TSLexer *lexer, const bool *valid_symbols) {
|
|
|
948
995
|
break;
|
|
949
996
|
}
|
|
950
997
|
advance(lexer);
|
|
951
|
-
if (lexer->lookahead == '\r') {
|
|
952
|
-
advance(lexer);
|
|
953
|
-
}
|
|
954
998
|
if (lexer->eof(lexer)) {
|
|
955
999
|
lexer->mark_end(lexer);
|
|
956
1000
|
lexer->result_symbol = LINE_CONTINUATION;
|
|
@@ -966,7 +1010,16 @@ static bool scan(Scanner *scanner, TSLexer *lexer, const bool *valid_symbols) {
|
|
|
966
1010
|
}
|
|
967
1011
|
// The word continues only if the next line does; otherwise the continuations themselves are
|
|
968
1012
|
// the token, so that whatever follows them is lexed as it would be without them.
|
|
969
|
-
if (
|
|
1013
|
+
if ((c == '<' || c == '>')) {
|
|
1014
|
+
advance(lexer);
|
|
1015
|
+
if (lexer->lookahead == '(') {
|
|
1016
|
+
lexer->result_symbol = CONCAT;
|
|
1017
|
+
return true;
|
|
1018
|
+
}
|
|
1019
|
+
scanner->after_line_break = true;
|
|
1020
|
+
lexer->result_symbol = LINE_CONTINUATION;
|
|
1021
|
+
} else if (lexer->eof(lexer) || is_separator(c) || is_metacharacter(c) ||
|
|
1022
|
+
(c == '`' && scanner->backtick_depth > 0)) {
|
|
970
1023
|
scanner->after_line_break = true;
|
|
971
1024
|
lexer->result_symbol = LINE_CONTINUATION;
|
|
972
1025
|
} else {
|
|
@@ -993,7 +1046,7 @@ static bool scan(Scanner *scanner, TSLexer *lexer, const bool *valid_symbols) {
|
|
|
993
1046
|
lexer->result_symbol = EMPTY_VALUE;
|
|
994
1047
|
return true;
|
|
995
1048
|
}
|
|
996
|
-
if (at_eof ||
|
|
1049
|
+
if (at_eof || is_separator(c) || c == ';' || c == '&' || c == '|' || c == ')' ||
|
|
997
1050
|
(c == '`' && scanner->backtick_depth > 0)) {
|
|
998
1051
|
lexer->result_symbol = EMPTY_VALUE;
|
|
999
1052
|
return true;
|
|
@@ -1021,9 +1074,6 @@ static bool scan(Scanner *scanner, TSLexer *lexer, const bool *valid_symbols) {
|
|
|
1021
1074
|
// after one; a backslash escaping a word's first character can only start an extglob prefix
|
|
1022
1075
|
// (`\foo@($x)`), and the word token handles it otherwise.
|
|
1023
1076
|
advance(lexer);
|
|
1024
|
-
if (lexer->lookahead == '\r') {
|
|
1025
|
-
advance(lexer);
|
|
1026
|
-
}
|
|
1027
1077
|
if (lexer->lookahead == '\n' || lexer->eof(lexer)) {
|
|
1028
1078
|
advance(lexer);
|
|
1029
1079
|
lexer->mark_end(lexer);
|
|
@@ -1099,6 +1149,7 @@ static bool scan(Scanner *scanner, TSLexer *lexer, const bool *valid_symbols) {
|
|
|
1099
1149
|
advance(lexer);
|
|
1100
1150
|
lexer->mark_end(lexer);
|
|
1101
1151
|
scanner->backtick_depth++;
|
|
1152
|
+
scanner->backtick_closers = (uint16_t)scanner->closers.size;
|
|
1102
1153
|
lexer->result_symbol = BACKTICK_OPEN;
|
|
1103
1154
|
return true;
|
|
1104
1155
|
}
|
|
@@ -1117,7 +1168,9 @@ static bool scan(Scanner *scanner, TSLexer *lexer, const bool *valid_symbols) {
|
|
|
1117
1168
|
lexer->mark_end(lexer);
|
|
1118
1169
|
lexer->result_symbol = FILE_DESCRIPTOR;
|
|
1119
1170
|
if (lexer->lookahead == '<' || lexer->lookahead == '>') {
|
|
1120
|
-
|
|
1171
|
+
// Like bash, digits before `<(` or `>(` are a word joined with the process substitution.
|
|
1172
|
+
advance(lexer);
|
|
1173
|
+
return lexer->lookahead != '(';
|
|
1121
1174
|
}
|
|
1122
1175
|
return valid_symbols[EXTGLOB_PREFIX] && continue_extglob_prefix(lexer, true);
|
|
1123
1176
|
}
|
|
@@ -1174,6 +1227,8 @@ unsigned tree_sitter_bash_external_scanner_serialize(void *payload, char *buffer
|
|
|
1174
1227
|
Scanner *scanner = (Scanner *)payload;
|
|
1175
1228
|
unsigned size = 0;
|
|
1176
1229
|
buffer[size++] = (char)scanner->backtick_depth;
|
|
1230
|
+
memcpy(&buffer[size], &scanner->backtick_closers, sizeof(scanner->backtick_closers));
|
|
1231
|
+
size += sizeof(scanner->backtick_closers);
|
|
1177
1232
|
buffer[size++] = (char)(scanner->terminator_owed | scanner->after_line_break << 1);
|
|
1178
1233
|
uint16_t closer_count = (uint16_t)scanner->closers.size;
|
|
1179
1234
|
memcpy(&buffer[size], &closer_count, sizeof(closer_count));
|
|
@@ -1203,11 +1258,13 @@ unsigned tree_sitter_bash_external_scanner_serialize(void *payload, char *buffer
|
|
|
1203
1258
|
void tree_sitter_bash_external_scanner_deserialize(void *payload, const char *buffer, unsigned length) {
|
|
1204
1259
|
Scanner *scanner = (Scanner *)payload;
|
|
1205
1260
|
scanner_reset(scanner);
|
|
1206
|
-
if (length < 2 + sizeof(uint16_t)) {
|
|
1261
|
+
if (length < 2 + 2 * sizeof(uint16_t)) {
|
|
1207
1262
|
return;
|
|
1208
1263
|
}
|
|
1209
1264
|
unsigned size = 0;
|
|
1210
1265
|
scanner->backtick_depth = (uint8_t)buffer[size++];
|
|
1266
|
+
memcpy(&scanner->backtick_closers, &buffer[size], sizeof(scanner->backtick_closers));
|
|
1267
|
+
size += sizeof(scanner->backtick_closers);
|
|
1211
1268
|
scanner->terminator_owed = buffer[size] & 1;
|
|
1212
1269
|
scanner->after_line_break = (buffer[size++] & 2) != 0;
|
|
1213
1270
|
uint16_t closer_count;
|
package/tree-sitter-bash.wasm
CHANGED
|
Binary file
|