@willbooster/tree-sitter-bash 1.0.2 → 1.1.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/src/scanner.c CHANGED
@@ -227,6 +227,8 @@ static bool scan_heredoc_content(Scanner *scanner, TSLexer *lexer, uint32_t inde
227
227
  bool escaped = false;
228
228
  uint32_t matched = 0;
229
229
  // At the end of input the lookahead is 0, which a delimiter holding NUL would otherwise match.
230
+ // Bash compares one line at a time, so a delimiter holding a newline (`<<"E\` + CRLF +
231
+ // `OF"`) never matches.
230
232
  for (;;) {
231
233
  if (matched == 0 && heredoc->allows_indent && lexer->lookahead == '\t') {
232
234
  advance(lexer);
@@ -234,21 +236,17 @@ static bool scan_heredoc_content(Scanner *scanner, TSLexer *lexer, uint32_t inde
234
236
  } else if (lexer->lookahead == '\\' && !heredoc->is_raw) {
235
237
  advance(lexer);
236
238
  consumed = true;
237
- bool escaped_cr = lexer->lookahead == '\r';
238
- if (escaped_cr) {
239
- advance(lexer);
240
- }
241
239
  if (lexer->lookahead != '\n' || lexer->eof(lexer)) {
242
240
  // An escape rather than a line continuation: the escaped character, which is
243
- // the CR itself when no LF follows it, is content.
244
- if (!escaped_cr && !lexer->eof(lexer)) {
241
+ // the CR itself before a CRLF line ending as in bash, is content.
242
+ if (!lexer->eof(lexer)) {
245
243
  advance(lexer);
246
244
  }
247
245
  escaped = true;
248
246
  break;
249
247
  }
250
248
  advance(lexer);
251
- } else if (matched < heredoc->delimiter.size && !lexer->eof(lexer) &&
249
+ } else if (matched < heredoc->delimiter.size && !lexer->eof(lexer) && lexer->lookahead != '\n' &&
252
250
  lexer->lookahead == *array_get(&heredoc->delimiter, matched)) {
253
251
  advance(lexer);
254
252
  matched++;
@@ -300,16 +298,9 @@ static bool scan_heredoc_content(Scanner *scanner, TSLexer *lexer, uint32_t inde
300
298
  advance(lexer);
301
299
  did_advance = true;
302
300
  if (!heredoc->is_raw && !lexer->eof(lexer)) {
303
- // A backslash-newline joins lines before the delimiter comparison. A CR without an
304
- // LF after it is the escaped character itself.
305
- if (lexer->lookahead == '\r') {
306
- advance(lexer);
307
- if (lexer->lookahead == '\n') {
308
- advance(lexer);
309
- }
310
- } else if (!lexer->eof(lexer)) {
311
- advance(lexer);
312
- }
301
+ // A backslash-newline joins lines before the delimiter comparison, while a
302
+ // backslash-CR escapes only the CR, so the LF after it still ends the line.
303
+ advance(lexer);
313
304
  }
314
305
  break;
315
306
  case '`':
@@ -446,16 +437,8 @@ static bool scan_heredoc_start(Scanner *scanner, TSLexer *lexer) {
446
437
  continue;
447
438
  }
448
439
  // Inside double quotes a backslash-newline still only continues the line.
449
- if (quote == '"' && c == '\\' && (lexer->lookahead == '\n' || lexer->lookahead == '\r')) {
450
- if (lexer->lookahead == '\r') {
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');
440
+ if (quote == '"' && c == '\\' && lexer->lookahead == '\n') {
441
+ advance(lexer);
459
442
  continue;
460
443
  }
461
444
  if (quote == '"' && c == '\\' &&
@@ -486,15 +469,8 @@ static bool scan_heredoc_start(Scanner *scanner, TSLexer *lexer) {
486
469
  heredoc->is_raw = true;
487
470
  } else if (c == '\\') {
488
471
  // 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
472
  if (lexer->lookahead == '\n') {
494
473
  advance(lexer);
495
- } else if (carriage_return) {
496
- heredoc->is_raw = true;
497
- array_push(&heredoc->delimiter, '\r');
498
474
  } else if (!lexer->eof(lexer)) {
499
475
  heredoc->is_raw = true;
500
476
  array_push(&heredoc->delimiter, lexer->lookahead);
@@ -647,7 +623,7 @@ static GroupKind scan_extglob_group(TSLexer *lexer) {
647
623
  advance(lexer);
648
624
  if (c == '\\') {
649
625
  // The word token's literal groups cannot hold a line continuation.
650
- if (lexer->eof(lexer) || lexer->lookahead == '\n' || lexer->lookahead == '\r') {
626
+ if (lexer->eof(lexer) || lexer->lookahead == '\n') {
651
627
  return lexer->eof(lexer) ? GROUP_UNSUPPORTED : GROUP_STRUCTURED;
652
628
  }
653
629
  advance(lexer);
@@ -683,7 +659,7 @@ static bool continue_extglob_prefix(TSLexer *lexer, bool consumed) {
683
659
  } else if (c == '\\') {
684
660
  advance(lexer);
685
661
  // A backslash-newline continues the line; the word token handles that.
686
- if (lexer->eof(lexer) || lexer->lookahead == '\n' || lexer->lookahead == '\r') {
662
+ if (lexer->eof(lexer) || lexer->lookahead == '\n') {
687
663
  return false;
688
664
  }
689
665
  advance(lexer);
@@ -814,9 +790,6 @@ static bool at_closing_reserved_word(TSLexer *lexer) {
814
790
  if (lexer->lookahead == '\\') {
815
791
  // Bash joins a backslash-newline before it checks where the word ends (`then\` + newline).
816
792
  advance(lexer);
817
- if (lexer->lookahead == '\r') {
818
- advance(lexer);
819
- }
820
793
  if (lexer->lookahead != '\n') {
821
794
  return false;
822
795
  }
@@ -925,9 +898,6 @@ static bool scan(Scanner *scanner, TSLexer *lexer, const bool *valid_symbols) {
925
898
  // A backslash-newline joins lines, so the word continues only if the next line does.
926
899
  lexer->mark_end(lexer);
927
900
  advance(lexer);
928
- if (lexer->lookahead == '\r') {
929
- advance(lexer);
930
- }
931
901
  // Bash drops a backslash at the end of input like a continuation.
932
902
  if (lexer->eof(lexer)) {
933
903
  lexer->mark_end(lexer);
@@ -948,9 +918,6 @@ static bool scan(Scanner *scanner, TSLexer *lexer, const bool *valid_symbols) {
948
918
  break;
949
919
  }
950
920
  advance(lexer);
951
- if (lexer->lookahead == '\r') {
952
- advance(lexer);
953
- }
954
921
  if (lexer->eof(lexer)) {
955
922
  lexer->mark_end(lexer);
956
923
  lexer->result_symbol = LINE_CONTINUATION;
@@ -1021,9 +988,6 @@ static bool scan(Scanner *scanner, TSLexer *lexer, const bool *valid_symbols) {
1021
988
  // after one; a backslash escaping a word's first character can only start an extglob prefix
1022
989
  // (`\foo@($x)`), and the word token handles it otherwise.
1023
990
  advance(lexer);
1024
- if (lexer->lookahead == '\r') {
1025
- advance(lexer);
1026
- }
1027
991
  if (lexer->lookahead == '\n' || lexer->eof(lexer)) {
1028
992
  advance(lexer);
1029
993
  lexer->mark_end(lexer);
Binary file
package/tree-sitter.json CHANGED
@@ -18,7 +18,7 @@
18
18
  }
19
19
  ],
20
20
  "metadata": {
21
- "version": "1.0.2",
21
+ "version": "1.1.1",
22
22
  "license": "MIT",
23
23
  "description": "Bash grammar for tree-sitter",
24
24
  "authors": [