@konomanoasa/tree-sitter-posix-awk 0.12.0 → 0.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/scanner.c CHANGED
@@ -65,6 +65,7 @@ enum TokenType {
65
65
  STRING_OPENING,
66
66
  STRING_END,
67
67
  COMMENT,
68
+ SPLIT_TOKEN,
68
69
  ERROR_SENTINEL,
69
70
  TOKEN_TYPE_COUNT,
70
71
  };
@@ -251,20 +252,26 @@ static bool is_word_continue(int32_t character) {
251
252
  return is_word_start(character) || is_ascii_digit(character);
252
253
  }
253
254
 
254
- static bool advance_line_continuations(TSLexer *lexer) {
255
- bool found = false;
255
+ typedef enum {
256
+ CONTINUATION_NONE,
257
+ CONTINUATION_PAIRS,
258
+ CONTINUATION_INVALID,
259
+ } ContinuationResult;
260
+
261
+ static ContinuationResult advance_line_continuations(TSLexer *lexer) {
262
+ ContinuationResult result = CONTINUATION_NONE;
256
263
 
257
264
  while (lexer->lookahead == '\\') {
258
265
  lexer->advance(lexer, false);
259
266
  if (lexer->lookahead != '\n') {
260
- return false;
267
+ return CONTINUATION_INVALID;
261
268
  }
262
269
 
263
270
  lexer->advance(lexer, false);
264
- found = true;
271
+ result = CONTINUATION_PAIRS;
265
272
  }
266
273
 
267
- return found;
274
+ return result;
268
275
  }
269
276
 
270
277
  // Skipping during lookahead would move the token start past its marked end.
@@ -295,7 +302,7 @@ static bool advance_boundary_gap_remainder(TSLexer *lexer) {
295
302
  if (lexer->lookahead != '\\') {
296
303
  return true;
297
304
  }
298
- if (!advance_line_continuations(lexer)) {
305
+ if (advance_line_continuations(lexer) == CONTINUATION_INVALID) {
299
306
  return false;
300
307
  }
301
308
  }
@@ -372,9 +379,6 @@ static WordKind
372
379
  promote_word_kind(TSLexer *lexer, const bool *valid_symbols, WordKind kind) {
373
380
  switch (kind) {
374
381
  case WORD_KIND_NAME:
375
- if (lexer->lookahead == '\\' && !advance_line_continuations(lexer)) {
376
- return kind;
377
- }
378
382
  if (lexer->lookahead == '(') {
379
383
  return WORD_KIND_FUNC_NAME;
380
384
  }
@@ -431,11 +435,23 @@ static bool word_spelling_is_valid(const bool *valid_symbols, WordKind kind) {
431
435
 
432
436
  static bool
433
437
  scan_word_token(TSLexer *lexer, const bool *valid_symbols, WordKind kind) {
438
+ lexer->mark_end(lexer);
439
+ const ContinuationResult continuation = advance_line_continuations(lexer);
440
+ if (continuation == CONTINUATION_INVALID) {
441
+ return emit_word_kind(lexer, valid_symbols, kind);
442
+ }
443
+ if (
444
+ continuation == CONTINUATION_PAIRS && is_word_continue(lexer->lookahead)
445
+ ) {
446
+ if (!valid_symbols[SPLIT_TOKEN]) {
447
+ return false;
448
+ }
449
+ lexer->mark_end(lexer);
450
+ return emit(lexer, SPLIT_TOKEN);
451
+ }
434
452
  if (!word_spelling_is_valid(valid_symbols, kind)) {
435
453
  return false;
436
454
  }
437
-
438
- lexer->mark_end(lexer);
439
455
  return emit_word_kind(
440
456
  lexer,
441
457
  valid_symbols,
@@ -463,26 +479,46 @@ static enum TokenType number_kind_token(NumberKind kind) {
463
479
  }
464
480
  }
465
481
 
466
- static NumberKind accept_number(TSLexer *lexer, NumberKind kind) {
482
+ static bool peek_number_continuations(TSLexer *lexer, bool *crossed) {
483
+ const ContinuationResult continuation = advance_line_continuations(lexer);
484
+ if (continuation == CONTINUATION_PAIRS) {
485
+ *crossed = true;
486
+ }
487
+ return continuation != CONTINUATION_INVALID;
488
+ }
489
+
490
+ static NumberKind
491
+ accept_number(TSLexer *lexer, NumberKind kind, bool crossed, bool *split) {
467
492
  lexer->mark_end(lexer);
493
+ *split = crossed;
468
494
  return kind;
469
495
  }
470
496
 
471
- static NumberKind scan_number_kind(TSLexer *lexer) {
497
+ static NumberKind scan_number_kind(TSLexer *lexer, bool *split) {
472
498
  NumberKind kind = NUMBER_KIND_NONE;
499
+ bool crossed = false;
473
500
 
474
501
  while (is_ascii_digit(lexer->lookahead)) {
475
502
  lexer->advance(lexer, false);
476
- kind = accept_number(lexer, NUMBER_KIND_INTEGER);
503
+ kind = accept_number(lexer, NUMBER_KIND_INTEGER, crossed, split);
504
+ if (!peek_number_continuations(lexer, &crossed)) {
505
+ return kind;
506
+ }
477
507
  }
478
508
  if (lexer->lookahead == '.') {
479
509
  lexer->advance(lexer, false);
480
510
  if (kind != NUMBER_KIND_NONE) {
481
- kind = accept_number(lexer, NUMBER_KIND_FRACTION);
511
+ kind = accept_number(lexer, NUMBER_KIND_FRACTION, crossed, split);
512
+ }
513
+ if (!peek_number_continuations(lexer, &crossed)) {
514
+ return kind;
482
515
  }
483
516
  while (is_ascii_digit(lexer->lookahead)) {
484
517
  lexer->advance(lexer, false);
485
- kind = accept_number(lexer, NUMBER_KIND_FRACTION);
518
+ kind = accept_number(lexer, NUMBER_KIND_FRACTION, crossed, split);
519
+ if (!peek_number_continuations(lexer, &crossed)) {
520
+ return kind;
521
+ }
486
522
  }
487
523
  }
488
524
  if (kind == NUMBER_KIND_NONE) {
@@ -491,15 +527,24 @@ static NumberKind scan_number_kind(TSLexer *lexer) {
491
527
 
492
528
  if (lexer->lookahead == 'e' || lexer->lookahead == 'E') {
493
529
  lexer->advance(lexer, false);
530
+ if (!peek_number_continuations(lexer, &crossed)) {
531
+ return kind;
532
+ }
494
533
  if (lexer->lookahead == '+' || lexer->lookahead == '-') {
495
534
  lexer->advance(lexer, false);
535
+ if (!peek_number_continuations(lexer, &crossed)) {
536
+ return kind;
537
+ }
496
538
  }
497
539
  if (!is_ascii_digit(lexer->lookahead)) {
498
540
  return kind;
499
541
  }
500
542
  while (is_ascii_digit(lexer->lookahead)) {
501
543
  lexer->advance(lexer, false);
502
- kind = accept_number(lexer, NUMBER_KIND_EXPONENT);
544
+ kind = accept_number(lexer, NUMBER_KIND_EXPONENT, crossed, split);
545
+ if (!peek_number_continuations(lexer, &crossed)) {
546
+ return kind;
547
+ }
503
548
  }
504
549
  }
505
550
  if (
@@ -514,7 +559,7 @@ static NumberKind scan_number_kind(TSLexer *lexer) {
514
559
  lexer->lookahead == 'L')
515
560
  ) {
516
561
  lexer->advance(lexer, false);
517
- kind = accept_number(lexer, kind);
562
+ kind = accept_number(lexer, kind, crossed, split);
518
563
  }
519
564
  return kind;
520
565
  }
@@ -533,7 +578,8 @@ has_valid_composite_operator_start(int32_t first, const bool *valid_symbols) {
533
578
  if (
534
579
  COMPOSITE_OPERATORS[i].first ==
535
580
  first &&
536
- valid_symbols[COMPOSITE_OPERATORS[i].token]
581
+ (valid_symbols[COMPOSITE_OPERATORS[i].token] ||
582
+ valid_symbols[SPLIT_TOKEN])
537
583
  ) {
538
584
  return true;
539
585
  }
@@ -559,9 +605,24 @@ static bool
559
605
  scan_composite_operator_start(TSLexer *lexer, const bool *valid_symbols) {
560
606
  const int32_t first = lexer->lookahead;
561
607
  lexer->advance(lexer, false);
608
+ const ContinuationResult continuation = advance_line_continuations(lexer);
609
+ if (continuation == CONTINUATION_INVALID) {
610
+ return false;
611
+ }
562
612
  const CompositeOperator *composite =
563
613
  find_composite_operator(first, lexer->lookahead);
564
- if (composite == NULL || !valid_symbols[composite->token]) {
614
+ if (composite == NULL) {
615
+ return false;
616
+ }
617
+ if (continuation == CONTINUATION_PAIRS) {
618
+ if (!valid_symbols[SPLIT_TOKEN]) {
619
+ return false;
620
+ }
621
+ lexer->advance(lexer, false);
622
+ lexer->mark_end(lexer);
623
+ return emit(lexer, SPLIT_TOKEN);
624
+ }
625
+ if (!valid_symbols[composite->token]) {
565
626
  return false;
566
627
  }
567
628
 
@@ -572,7 +633,8 @@ scan_composite_operator_start(TSLexer *lexer, const bool *valid_symbols) {
572
633
 
573
634
  static bool
574
635
  has_greater_start(const bool *valid_symbols, bool allow_zero_width_guard) {
575
- return valid_symbols[GE_OPERATOR] ||
636
+ return valid_symbols[SPLIT_TOKEN] ||
637
+ valid_symbols[GE_OPERATOR] ||
576
638
  valid_symbols[APPEND_OPERATOR] ||
577
639
  (allow_zero_width_guard && valid_symbols[OUTPUT_GREATER_GUARD]);
578
640
  }
@@ -585,7 +647,20 @@ static bool scan_greater_start(
585
647
  bool allow_zero_width_guard
586
648
  ) {
587
649
  lexer->advance(lexer, false);
588
- if (lexer->lookahead == '=') {
650
+ const ContinuationResult continuation = advance_line_continuations(lexer);
651
+ if (
652
+ continuation ==
653
+ CONTINUATION_PAIRS &&
654
+ (lexer->lookahead == '=' || lexer->lookahead == '>')
655
+ ) {
656
+ if (!valid_symbols[SPLIT_TOKEN]) {
657
+ return false;
658
+ }
659
+ lexer->advance(lexer, false);
660
+ lexer->mark_end(lexer);
661
+ return emit(lexer, SPLIT_TOKEN);
662
+ }
663
+ if (continuation == CONTINUATION_NONE && lexer->lookahead == '=') {
589
664
  if (!valid_symbols[GE_OPERATOR]) {
590
665
  return false;
591
666
  }
@@ -593,7 +668,13 @@ static bool scan_greater_start(
593
668
  lexer->mark_end(lexer);
594
669
  return emit(lexer, GE_OPERATOR);
595
670
  }
596
- if (lexer->lookahead == '>' && valid_symbols[APPEND_OPERATOR]) {
671
+ if (
672
+ continuation ==
673
+ CONTINUATION_NONE &&
674
+ lexer->lookahead ==
675
+ '>' &&
676
+ valid_symbols[APPEND_OPERATOR]
677
+ ) {
597
678
  lexer->advance(lexer, false);
598
679
  lexer->mark_end(lexer);
599
680
  return emit(lexer, APPEND_OPERATOR);
@@ -611,7 +692,19 @@ static bool scan_slash_start(
611
692
  ) {
612
693
  lexer->advance(lexer, false);
613
694
  lexer->mark_end(lexer);
614
- if (lexer->lookahead == '=') {
695
+ const ContinuationResult continuation = advance_line_continuations(lexer);
696
+ if (
697
+ continuation ==
698
+ CONTINUATION_PAIRS &&
699
+ lexer->lookahead ==
700
+ '=' &&
701
+ valid_symbols[SPLIT_TOKEN]
702
+ ) {
703
+ lexer->advance(lexer, false);
704
+ lexer->mark_end(lexer);
705
+ return emit(lexer, SPLIT_TOKEN);
706
+ }
707
+ if (continuation == CONTINUATION_NONE && lexer->lookahead == '=') {
615
708
  if (valid_symbols[DIV_ASSIGN_OPERATOR]) {
616
709
  lexer->advance(lexer, false);
617
710
  lexer->mark_end(lexer);
@@ -640,6 +733,11 @@ static bool scan_ere_backslash_context(
640
733
  }
641
734
 
642
735
  lexer->advance(lexer, false);
736
+ if (lexer->lookahead == '\n' && valid_symbols[SPLIT_TOKEN]) {
737
+ lexer->advance(lexer, false);
738
+ lexer->mark_end(lexer);
739
+ return emit(lexer, SPLIT_TOKEN);
740
+ }
643
741
  if (valid_symbols[ERROR_SENTINEL]) {
644
742
  return false;
645
743
  }
@@ -785,6 +883,15 @@ static bool scan_string_context(
785
883
  const bool *valid_symbols
786
884
  ) {
787
885
  lexer->mark_end(lexer);
886
+ if (lexer->lookahead == '\\') {
887
+ lexer->advance(lexer, false);
888
+ if (lexer->lookahead == '\n' && valid_symbols[SPLIT_TOKEN]) {
889
+ lexer->advance(lexer, false);
890
+ lexer->mark_end(lexer);
891
+ return emit(lexer, SPLIT_TOKEN);
892
+ }
893
+ return false;
894
+ }
788
895
  const bool at_string_end =
789
896
  lexer->lookahead == '"' || lexer->lookahead == '\n' || lexer->eof(lexer);
790
897
  if (at_string_end && valid_symbols[STRING_END]) {
@@ -841,15 +948,22 @@ bool tree_sitter_posix_awk_external_scanner_scan(
841
948
  return scan_string_opening(state, lexer);
842
949
  }
843
950
 
844
- if (is_word_start(lexer->lookahead) && has_word_token(valid_symbols)) {
951
+ if (
952
+ is_word_start(lexer->lookahead) &&
953
+ (has_word_token(valid_symbols) || valid_symbols[SPLIT_TOKEN])
954
+ ) {
845
955
  const WordKind kind = scan_word_spelling(lexer);
846
956
  return scan_word_token(lexer, valid_symbols, kind);
847
957
  }
848
958
  if (
849
959
  (is_ascii_digit(lexer->lookahead) || lexer->lookahead == '.') &&
850
- has_number_token(valid_symbols)
960
+ (has_number_token(valid_symbols) || valid_symbols[SPLIT_TOKEN])
851
961
  ) {
852
- const NumberKind kind = scan_number_kind(lexer);
962
+ bool split = false;
963
+ const NumberKind kind = scan_number_kind(lexer, &split);
964
+ if (split) {
965
+ return valid_symbols[SPLIT_TOKEN] && emit(lexer, SPLIT_TOKEN);
966
+ }
853
967
  return emit_number_kind(lexer, valid_symbols, kind);
854
968
  }
855
969
  if (
@@ -857,7 +971,8 @@ bool tree_sitter_posix_awk_external_scanner_scan(
857
971
  '/' &&
858
972
  (valid_symbols[DIVISION_SLASH] ||
859
973
  valid_symbols[ERE_OPENING_SLASH] ||
860
- valid_symbols[DIV_ASSIGN_OPERATOR])
974
+ valid_symbols[DIV_ASSIGN_OPERATOR] ||
975
+ valid_symbols[SPLIT_TOKEN])
861
976
  ) {
862
977
  return scan_slash_start(state, lexer, valid_symbols);
863
978
  }
package/tree-sitter.json CHANGED
@@ -12,7 +12,7 @@
12
12
  }
13
13
  ],
14
14
  "metadata": {
15
- "version": "0.12.0",
15
+ "version": "0.13.0",
16
16
  "license": "MIT",
17
17
  "description": "Tree-sitter grammar for POSIX awk.",
18
18
  "authors": [