@konomanoasa/tree-sitter-posix-awk 0.20.0 → 0.20.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/Cargo.toml CHANGED
@@ -1,6 +1,6 @@
1
1
  [package]
2
2
  name = "konomanoasa-tree-sitter-posix-awk"
3
- version = "0.20.0"
3
+ version = "0.20.2"
4
4
  edition = "2024"
5
5
  description = "Tree-sitter grammar for POSIX awk."
6
6
  readme = "README.md"
@@ -27,10 +27,10 @@ name = "bindings"
27
27
  path = "test/bindings.test.rs"
28
28
 
29
29
  [dependencies]
30
- tree-sitter-language = "0.1.8"
30
+ tree-sitter-language = "=0.1.9"
31
31
 
32
32
  [dev-dependencies]
33
- tree-sitter = "0.27.0"
33
+ tree-sitter = "=0.27.1"
34
34
 
35
35
  [build-dependencies]
36
- cc = "1.4"
36
+ cc = "=1.10.0"
package/README.md CHANGED
@@ -7,6 +7,8 @@
7
7
  [Tree-sitter](https://tree-sitter.github.io/tree-sitter/) grammar for
8
8
  POSIX.1-2024 AWK.
9
9
 
10
+ For GNU awk, see [tree-sitter-awk](https://github.com/konomanoasa/tree-sitter-awk).
11
+
10
12
  ## Installation
11
13
 
12
14
  ```sh
@@ -15,8 +17,6 @@ npm install @konomanoasa/tree-sitter-posix-awk
15
17
 
16
18
  ## Development
17
19
 
18
- Development uses Node.js 24.21.0 or later.
19
-
20
20
  ```sh
21
21
  npm install
22
22
  npm run build
@@ -30,7 +30,7 @@ npm test
30
30
 
31
31
  ## Used By
32
32
 
33
- [awk-ts-mode](https://github.com/konomanoasa/awk-ts-mode): Emacs major mode.
33
+ [awk-ts-mode](https://github.com/konomanoasa/awk-ts-mode)
34
34
 
35
35
  ## License
36
36
 
package/grammar.js CHANGED
@@ -75,6 +75,18 @@ const continuationMarker = () => token.immediate("\\");
75
75
  const signOperator = ($, character) =>
76
76
  alias(character === "+" ? $._plus_operator : $._minus_operator, character);
77
77
 
78
+ const prefixOperator = ($, unary) =>
79
+ field(
80
+ "operator",
81
+ unary
82
+ ? choice(signOperator($, "+"), signOperator($, "-"))
83
+ : alias($._not_operator, "!"),
84
+ );
85
+
86
+ const updateOperator = ($) => field("operator", choice($.incr, $.decr));
87
+
88
+ const fieldOperator = ($) => field("operator", alias($._field_operator, "$"));
89
+
78
90
  const assignmentOperator = ($) =>
79
91
  choice(
80
92
  "=",
@@ -339,12 +351,7 @@ for (const print of [false, true]) {
339
351
  choice(
340
352
  seq(
341
353
  $._select_prefix,
342
- field(
343
- "operator",
344
- unary
345
- ? choice(signOperator($, "+"), signOperator($, "-"))
346
- : alias($._not_operator, "!"),
347
- ),
354
+ prefixOperator($, unary),
348
355
  field("operand", expr($)),
349
356
  ),
350
357
  seq(
@@ -411,11 +418,11 @@ for (const print of [false, true]) {
411
418
  seq(
412
419
  $._select_postfix,
413
420
  field("operand", $._child_lvalue),
414
- field("operator", choice($.incr, $.decr)),
421
+ updateOperator($),
415
422
  ),
416
423
  seq(
417
424
  $._select_prefix,
418
- field("operator", choice($.incr, $.decr)),
425
+ updateOperator($),
419
426
  field("operand", $._child_lvalue),
420
427
  ),
421
428
  seq(
@@ -450,11 +457,7 @@ Object.assign(boundaryRules, {
450
457
  alias($._boundary_arguments, $.expr_list),
451
458
  "]",
452
459
  ),
453
- seq(
454
- $._select_field,
455
- field("operator", alias($._field_operator, "$")),
456
- field("operand", $._child_expr),
457
- ),
460
+ seq($._select_field, fieldOperator($), field("operand", $._child_expr)),
458
461
  ),
459
462
  _boundary_arguments: ($) =>
460
463
  choice(
@@ -503,6 +506,10 @@ Object.assign(boundaryRules, {
503
506
 
504
507
  const localRules = {};
505
508
  const localNode = ($, name) => alias($[`local_${name}`], $[name]);
509
+ const localSubscriptable = ($) =>
510
+ seq($.name, optional(seq("[", localNode($, "expr_list"), "]")));
511
+ const localPrefixUpdate = ($) =>
512
+ seq(updateOperator($), field("operand", localNode($, "lvalue")));
506
513
  const localAtoms = ($, print, lvalue = localNode($, "lvalue")) =>
507
514
  choice(
508
515
  $._local_atom,
@@ -596,12 +603,7 @@ for (const print of [false, true]) {
596
603
  const operand = any(name);
597
604
  return ($) => [
598
605
  seq(
599
- field(
600
- "operator",
601
- classification === "unary"
602
- ? choice(signOperator($, "+"), signOperator($, "-"))
603
- : alias($._not_operator, "!"),
604
- ),
606
+ prefixOperator($, classification === "unary"),
605
607
  field("operand", operand($)),
606
608
  ),
607
609
  ];
@@ -610,12 +612,9 @@ for (const print of [false, true]) {
610
612
  return ($) => [
611
613
  seq(
612
614
  field("operand", alias($.local_postfix_lvalue, $.lvalue)),
613
- field("operator", choice($.incr, $.decr)),
614
- ),
615
- seq(
616
- field("operator", choice($.incr, $.decr)),
617
- field("operand", localNode($, "lvalue")),
615
+ updateOperator($),
618
616
  ),
617
+ localPrefixUpdate($),
619
618
  ];
620
619
  }
621
620
  const left = member(
@@ -693,9 +692,9 @@ Object.assign(localRules, {
693
692
  ),
694
693
  local_lvalue: ($) =>
695
694
  choice(
696
- seq($.name, optional(seq("[", localNode($, "expr_list"), "]"))),
695
+ localSubscriptable($),
697
696
  seq(
698
- field("operator", alias($._field_operator, "$")),
697
+ fieldOperator($),
699
698
  field("operand", alias($.local_field_expr, $.expr)),
700
699
  ),
701
700
  ),
@@ -703,9 +702,9 @@ Object.assign(localRules, {
703
702
  prec(
704
703
  1,
705
704
  choice(
706
- seq($.name, optional(seq("[", localNode($, "expr_list"), "]"))),
705
+ localSubscriptable($),
707
706
  seq(
708
- field("operator", alias($._field_operator, "$")),
707
+ fieldOperator($),
709
708
  field("operand", alias($.local_postfix_field_expr, $.expr)),
710
709
  ),
711
710
  ),
@@ -723,13 +722,10 @@ Object.assign(localRules, {
723
722
  choice(
724
723
  $._local_non_unary_atom,
725
724
  seq(
726
- field("operator", alias($._not_operator, "!")),
725
+ prefixOperator($, false),
727
726
  field("operand", alias($.local_unary, $.expr)),
728
727
  ),
729
- seq(
730
- field("operator", choice($.incr, $.decr)),
731
- field("operand", localNode($, "lvalue")),
732
- ),
728
+ localPrefixUpdate($),
733
729
  ),
734
730
  local_expr_list: ($) =>
735
731
  choice(localNode($, "expr"), localNode($, "multiple_expr_list")),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@konomanoasa/tree-sitter-posix-awk",
3
- "version": "0.20.0",
3
+ "version": "0.20.2",
4
4
  "type": "module",
5
5
  "description": "Tree-sitter grammar for POSIX awk.",
6
6
  "license": "MIT",
@@ -21,25 +21,24 @@
21
21
  },
22
22
  "scripts": {
23
23
  "build": "npm run tree-sitter -- build-all",
24
- "check": "biome check . && tsc --noEmit",
24
+ "check": "biome check . && tsc",
25
25
  "check:queries": "npm run tree-sitter -- check-queries",
26
26
  "format": "biome format --write .",
27
27
  "generate": "npm run tree-sitter -- generate-all",
28
- "test": "npm run tree-sitter -- test-corpus --rebuild && node --test --test-concurrency=1 --test-skip-pattern=\"posix_awk: stress:\" \"test/*.test.js\"",
28
+ "test": "npm run tree-sitter -- test-corpus --rebuild && node --test --test-concurrency=1 test/build.test.js test/cst.test.js test/highlight.test.js test/incremental.test.js test/parser.test.js",
29
29
  "test:fuzz": "node --test --test-name-pattern=\"fixed-seed generated histories\" test/incremental.test.js",
30
30
  "test:fuzz:corpus": "npm run tree-sitter -- fuzz-all --iterations 100 --edits 5",
31
31
  "test:highlights": "node --test test/highlight.test.js",
32
32
  "test:incremental": "node --test --test-concurrency=1 test/cst.test.js test/parser.test.js test/incremental.test.js",
33
- "test:performance": "node --test --test-skip-pattern=\"posix_awk: stress:\" test/performance.test.js",
33
+ "test:performance": "node --test test/performance.test.js",
34
34
  "test:sanitize": "node scripts/check-c.js --sanitize",
35
35
  "test:scanner": "node scripts/check-c.js",
36
- "test:stress": "node --test --test-name-pattern=\"posix_awk: stress:\" test/performance.test.js",
37
36
  "tree-sitter": "node scripts/tree-sitter.js"
38
37
  },
39
38
  "devDependencies": {
40
- "@biomejs/biome": "2.5.14",
41
- "@types/node": "26.5.1",
42
- "tree-sitter-cli": "0.27.0",
39
+ "@biomejs/biome": "2.6.2",
40
+ "@types/node": "26.6.5",
41
+ "tree-sitter-cli": "0.27.1",
43
42
  "typescript": "7.0.2"
44
43
  },
45
44
  "allowScripts": {
package/src/parser.c CHANGED
@@ -65894,7 +65894,7 @@ TS_PUBLIC const TSLanguage *tree_sitter_posix_awk(void) {
65894
65894
  .metadata = {
65895
65895
  .major_version = 0,
65896
65896
  .minor_version = 20,
65897
- .patch_version = 0,
65897
+ .patch_version = 2,
65898
65898
  },
65899
65899
  };
65900
65900
  return &language;
package/src/scanner.c CHANGED
@@ -140,7 +140,7 @@ typedef struct {
140
140
  size_t children[2];
141
141
  } BoundaryTree;
142
142
 
143
- enum BoundaryPhase { BOUNDARY_READY, BOUNDARY_SAVING, BOUNDARY_RESTORING };
143
+ enum BoundaryPhase { BOUNDARY_READY, BOUNDARY_SAVING };
144
144
  typedef struct {
145
145
  uint64_t active, pending[2], next, carry;
146
146
  enum BoundaryPhase phase;
@@ -152,14 +152,9 @@ typedef struct {
152
152
  BoundaryOutline cache[BOUNDARY_CACHE_CAPACITY + 1];
153
153
  } BoundaryState;
154
154
 
155
- static void boundary_reset(
156
- BoundaryState *state,
157
- uint64_t active,
158
- enum BoundaryPhase phase
159
- ) {
155
+ static void boundary_reset(BoundaryState *state, uint64_t active) {
160
156
  memset(state, 0, offsetof(BoundaryState, decision_hash));
161
157
  state->active = active;
162
- state->phase = phase;
163
158
  }
164
159
 
165
160
  typedef struct {
@@ -169,6 +164,7 @@ typedef struct {
169
164
 
170
165
  #define SAVE_FIRST SAVE_0
171
166
  #define RESTORE_FIRST RESTORE_0
167
+ enum { SAVED_DIGITS = RESTORE_FIRST - SAVE_FIRST };
172
168
  // BEGIN generated reserved words from grammar.js.
173
169
  enum {
174
170
  MAX_RESERVED_WORD_LENGTH = 8,
@@ -702,7 +698,6 @@ static bool skip_scope_layout(TSLexer *lexer, bool separated) {
702
698
  }
703
699
 
704
700
  static bool skip_group(TSLexer *lexer, bool *tuple) {
705
- if (tuple) *tuple = false;
706
701
  bool after_operand = false;
707
702
  bool separated = false;
708
703
  unsigned depth = 0;
@@ -736,7 +731,6 @@ static bool skip_group(TSLexer *lexer, bool *tuple) {
736
731
  depth--;
737
732
  after_operand = true;
738
733
  } else if (character_in(character, "+-") && lexer->lookahead == character) {
739
- // A slash after an update divides: prefix updates require an lvalue.
740
734
  lexer->advance(lexer, false);
741
735
  after_operand = true;
742
736
  } else if (character_in(character, "&|") && lexer->lookahead == character) {
@@ -744,6 +738,8 @@ static bool skip_group(TSLexer *lexer, bool *tuple) {
744
738
  after_operand = false;
745
739
  separated = true;
746
740
  } else {
741
+ if (character == '{' || character == '}' || character == ';')
742
+ return false;
747
743
  after_operand = is_word_continue(character) || character == '.';
748
744
  separated = character == ',';
749
745
  }
@@ -762,7 +758,6 @@ static WordAtom word_atom(TSLexer *lexer, WordKind word) {
762
758
  return ATOM_PLAIN;
763
759
  }
764
760
 
765
- // Preserve the source lexer's furthest lookahead when restoring this cursor.
766
761
  typedef struct {
767
762
  TSLexer lexer;
768
763
  TSLexer *source;
@@ -830,6 +825,12 @@ static InputProbe probe_open(TSLexer *source) {
830
825
  };
831
826
  }
832
827
 
828
+ static bool probe_number(InputProbe *probe) {
829
+ if (!skip_number(&probe->lexer)) return false;
830
+ probe_restore(probe, probe->marked_end);
831
+ return true;
832
+ }
833
+
833
834
  enum ProbeToken {
834
835
  PROBE_END = 0,
835
836
  PROBE_VALUE = 256,
@@ -852,11 +853,7 @@ static int probe_token(InputProbe *probe, bool expect_operand) {
852
853
  }
853
854
  const int32_t character = lexer->lookahead;
854
855
  if (is_ascii_digit(character) || character == '.') {
855
- if (!skip_number(lexer)) {
856
- return PROBE_END;
857
- }
858
- probe_restore(probe, probe->marked_end);
859
- return PROBE_VALUE;
856
+ return probe_number(probe) ? PROBE_VALUE : PROBE_END;
860
857
  }
861
858
  if (character == '"' || (expect_operand && character == '/')) {
862
859
  return skip_literal(lexer) ? PROBE_VALUE : PROBE_END;
@@ -1009,13 +1006,6 @@ static int probe_precedence(int token) {
1009
1006
  }
1010
1007
  }
1011
1008
 
1012
- typedef struct {
1013
- size_t end;
1014
- int ceiling, kind;
1015
- bool lvalue, postfix, unary;
1016
- uint64_t children[2];
1017
- } RecognizedExpression;
1018
-
1019
1009
  typedef struct {
1020
1010
  int *tokens;
1021
1011
  size_t length, capacity;
@@ -1081,7 +1071,6 @@ static int descriptor_kind(uint64_t descriptor) {
1081
1071
  return signature_kind(descriptor_signature(descriptor));
1082
1072
  }
1083
1073
 
1084
- // Store the short signature in low bits to reduce base-4 stack digits.
1085
1074
  enum { SAVED_SIGNATURE_BITS = 17 };
1086
1075
  static const uint64_t SAVED_SIGNATURE_MASK =
1087
1076
  (UINT64_C(1) << SAVED_SIGNATURE_BITS) - 1;
@@ -1101,19 +1090,6 @@ static uint64_t restored_descriptor(uint64_t saved) {
1101
1090
  );
1102
1091
  }
1103
1092
 
1104
- static RecognizedExpression outline_expression(BoundaryOutline outline) {
1105
- const uint32_t signature = descriptor_signature(outline.descriptor);
1106
- return (RecognizedExpression){
1107
- .end = descriptor_length(outline.descriptor),
1108
- .kind = descriptor_kind(outline.descriptor),
1109
- .ceiling = signature_ceiling(signature),
1110
- .lvalue = signature & SIGNATURE_LVALUE,
1111
- .postfix = signature & SIGNATURE_POSTFIX,
1112
- .unary = signature & SIGNATURE_UNARY,
1113
- .children = {outline.children[0], outline.children[1]},
1114
- };
1115
- }
1116
-
1117
1093
  static void recognizer_destroy(RecognizerInput *parser) {
1118
1094
  ts_free(parser->tokens);
1119
1095
  ts_free(parser->tree);
@@ -1182,7 +1158,7 @@ typedef struct {
1182
1158
  bool *alternatives;
1183
1159
  size_t depth, context_depth;
1184
1160
  RecognizedValue value;
1185
- bool valid, exhausted;
1161
+ bool valid, exhausted, checked, feasible;
1186
1162
  size_t remaining;
1187
1163
  RecognizerContext *contexts;
1188
1164
  size_t context_count, context_capacity, *context_slots, context_slot_capacity;
@@ -1669,10 +1645,6 @@ static void recognizer_step(Recognizer *parser) {
1669
1645
  frame->stage = RECOGNIZER_BINARY;
1670
1646
  break;
1671
1647
  case RECOGNIZER_BINARY: {
1672
- if (frame->precedence == EXPR_CONCATENATION && parser->value.unary) {
1673
- parser->valid = false;
1674
- break;
1675
- }
1676
1648
  const bool unary = frame->left.unary;
1677
1649
  frame->left = recognizer_node(
1678
1650
  parser,
@@ -1703,7 +1675,7 @@ typedef struct {
1703
1675
  typedef struct {
1704
1676
  RecognizerInput *input;
1705
1677
  SharedNode *nodes;
1706
- size_t length, capacity;
1678
+ size_t length, capacity, generated;
1707
1679
  SharedSet *memo;
1708
1680
  bool *needed;
1709
1681
  uint32_t *ranks[2], *stack;
@@ -1732,6 +1704,7 @@ static SharedNode shared_node(
1732
1704
  bool postfix,
1733
1705
  bool unary
1734
1706
  ) {
1707
+ if (parser->generated != SIZE_MAX) parser->generated++;
1735
1708
  SharedNode node = {
1736
1709
  .descriptor = make_descriptor(
1737
1710
  boundary_signature(kind, ceiling, lvalue, postfix, unary),
@@ -1743,7 +1716,7 @@ static SharedNode shared_node(
1743
1716
  };
1744
1717
  if (kind == RECOGNIZED_GET) {
1745
1718
  node.decision = start;
1746
- node.rank = left || right ? 1 : 2;
1719
+ node.rank = right ? 1 : 2;
1747
1720
  } else if (kind == RECOGNIZED_FILE || kind == '<') {
1748
1721
  node.decision = shared_end(parser->nodes[left - 1]);
1749
1722
  node.rank = kind == '<' ? (uint32_t)parser->input->length + 2
@@ -2144,11 +2117,6 @@ shared_compute(SharedRecognition *parser, uint32_t start, int minimum) {
2144
2117
  for (size_t i = 0; i < rights->length && !parser->failed; i++) {
2145
2118
  const uint32_t right = rights->items[i];
2146
2119
  const SharedNode child = parser->nodes[right - 1];
2147
- if (
2148
- next.precedence == EXPR_CONCATENATION &&
2149
- (shared_shape(child) & SIGNATURE_UNARY)
2150
- )
2151
- continue;
2152
2120
  shared_add(
2153
2121
  parser,
2154
2122
  &work,
@@ -2199,7 +2167,8 @@ static bool shared_tree(SharedRecognition *parser, uint32_t root) {
2199
2167
  return true;
2200
2168
  }
2201
2169
 
2202
- static bool shared_recognize(RecognizerInput *input, bool select) {
2170
+ static bool
2171
+ shared_recognize(RecognizerInput *input, bool select, size_t *generated) {
2203
2172
  if (
2204
2173
  input->length > UINT32_MAX - 2 ||
2205
2174
  input->length > SIZE_MAX / RECOGNIZER_LEVELS / sizeof(SharedSet) - 1
@@ -2267,6 +2236,7 @@ static bool shared_recognize(RecognizerInput *input, bool select) {
2267
2236
  if (best && !parser.failed) accepted = !select || shared_tree(&parser, best);
2268
2237
  shared_set_destroy(&local);
2269
2238
  done:
2239
+ if (generated) *generated = parser.generated;
2270
2240
  if (parser.memo)
2271
2241
  for (size_t i = 0; i < count; i++) shared_set_destroy(&parser.memo[i]);
2272
2242
  ts_free(parser.memo);
@@ -2278,14 +2248,26 @@ done:
2278
2248
  return accepted;
2279
2249
  }
2280
2250
 
2281
- static bool shared_parse(RecognizerInput *input) {
2282
- return shared_recognize(input, false) && shared_recognize(input, true);
2283
- }
2284
-
2285
2251
  #ifndef RECOGNIZER_WORK_FACTOR
2286
2252
  #define RECOGNIZER_WORK_FACTOR 8
2287
2253
  #endif
2288
2254
 
2255
+ enum { RECOGNIZER_RESUME_FACTOR = 32 };
2256
+
2257
+ static bool recognizer_extend(Recognizer *parser) {
2258
+ if (parser->checked) return false;
2259
+ parser->checked = true;
2260
+ size_t generated = 0;
2261
+ parser->feasible = shared_recognize(parser->input, false, &generated);
2262
+ if (!parser->feasible) return false;
2263
+ // Stored nodes omit the alternatives that dominate selection work.
2264
+ parser->remaining = generated > SIZE_MAX / RECOGNIZER_RESUME_FACTOR
2265
+ ? SIZE_MAX
2266
+ : generated * RECOGNIZER_RESUME_FACTOR;
2267
+ parser->exhausted = false;
2268
+ return true;
2269
+ }
2270
+
2289
2271
  static bool recognizer_parse(RecognizerInput *input) {
2290
2272
  if (input->length > (SIZE_MAX / sizeof(RecognizerFrame)) - 2) return false;
2291
2273
  Recognizer parser = {.input = input};
@@ -2293,7 +2275,7 @@ static bool recognizer_parse(RecognizerInput *input) {
2293
2275
  parser.choices = ts_calloc(input->length, sizeof(*parser.choices));
2294
2276
  parser.sources = ts_calloc(input->length, sizeof(*parser.sources));
2295
2277
  parser.alternatives = ts_calloc(input->length, sizeof(*parser.alternatives));
2296
- bool result = false, shared = false;
2278
+ bool result = false;
2297
2279
  size_t limit = input->length > SIZE_MAX / (input->length ? input->length : 1)
2298
2280
  ? SIZE_MAX
2299
2281
  : input->length * input->length;
@@ -2306,10 +2288,11 @@ static bool recognizer_parse(RecognizerInput *input) {
2306
2288
  )
2307
2289
  goto done;
2308
2290
  for (;;) {
2309
- if (input->length > parser.remaining) {
2310
- shared = true;
2291
+ if (
2292
+ input->length > parser.remaining &&
2293
+ (!recognizer_extend(&parser) || input->length > parser.remaining)
2294
+ )
2311
2295
  goto done;
2312
- }
2313
2296
  parser.remaining -= input->length;
2314
2297
  memset(parser.sources, 0, input->length * sizeof(*parser.sources));
2315
2298
  memset(
@@ -2329,17 +2312,14 @@ static bool recognizer_parse(RecognizerInput *input) {
2329
2312
  recognizer_push(&parser, 0, EXPR_ASSIGNMENT);
2330
2313
  }
2331
2314
  while (parser.valid && parser.depth) {
2332
- if (!parser.remaining) {
2333
- shared = true;
2334
- goto done;
2335
- }
2315
+ if (!parser.remaining && !recognizer_extend(&parser)) goto done;
2336
2316
  parser.remaining--;
2337
2317
  if (!recognizer_checkpoint(&parser)) {
2338
2318
  parser.valid = false;
2339
2319
  break;
2340
2320
  }
2341
2321
  if (parser.exhausted) {
2342
- shared = true;
2322
+ if (recognizer_extend(&parser)) continue;
2343
2323
  goto done;
2344
2324
  }
2345
2325
  if (parser.context_depth >= parser.depth)
@@ -2359,7 +2339,6 @@ static bool recognizer_parse(RecognizerInput *input) {
2359
2339
  size_t position = input->length;
2360
2340
  while (position && !parser.alternatives[position - 1]) position--;
2361
2341
  if (!position) break;
2362
- // Only positions past the backtrack point have exhausted all choices.
2363
2342
  for (size_t i = 0; i < parser.history_count; i++) {
2364
2343
  if (parser.history[i].position > position - 1)
2365
2344
  recognizer_failed(&parser, parser.history[i], true);
@@ -2380,7 +2359,8 @@ done:
2380
2359
  ts_free(parser.choices);
2381
2360
  ts_free(parser.sources);
2382
2361
  ts_free(parser.alternatives);
2383
- return shared ? shared_parse(input) : result;
2362
+ return !result && parser.feasible ? shared_recognize(input, true, NULL)
2363
+ : result;
2384
2364
  }
2385
2365
 
2386
2366
  static const uint64_t UNCONSTRAINED = UINT64_MAX;
@@ -2399,7 +2379,11 @@ boundary_emit(TSLexer *lexer, BoundaryState *state, unsigned token) {
2399
2379
  return true;
2400
2380
  }
2401
2381
 
2402
- // Periodic caches store shift flags in the repeat field.
2382
+ static void cache_clear(BoundaryState *state) {
2383
+ state->cache_count = state->cache_index = state->cache_repeats =
2384
+ state->cache_delta = 0;
2385
+ }
2386
+
2403
2387
  enum {
2404
2388
  SHIFT_DESCRIPTOR = 1,
2405
2389
  SHIFT_LEFT = 2,
@@ -2535,9 +2519,8 @@ static bool cache_period(
2535
2519
 
2536
2520
  static void
2537
2521
  cache_boundary(BoundaryState *state, const RecognizerInput *parser) {
2538
- state->cache_count = state->cache_index = state->cache_repeats =
2539
- state->cache_delta = 0;
2540
- if (!BOUNDARY_CACHE_CAPACITY || !parser->tree_root) return;
2522
+ cache_clear(state);
2523
+ if (!BOUNDARY_CACHE_CAPACITY) return;
2541
2524
  if (parser->tree_count > SIZE_MAX / 2 / sizeof(size_t) - 1) return;
2542
2525
  size_t *order = ts_calloc(parser->tree_count * 2 + 2, sizeof(*order));
2543
2526
  if (!order) return;
@@ -2576,17 +2559,13 @@ cache_boundary(BoundaryState *state, const RecognizerInput *parser) {
2576
2559
  ts_free(order);
2577
2560
  }
2578
2561
 
2579
- static bool take_boundary(
2580
- BoundaryState *state,
2581
- uint64_t requested,
2582
- RecognizedExpression *node
2583
- ) {
2562
+ static bool
2563
+ take_boundary(BoundaryState *state, uint64_t requested, BoundaryOutline *node) {
2584
2564
  if (
2585
2565
  !state->cache_count ||
2586
2566
  state->cache[state->cache_index].descriptor != requested
2587
2567
  ) {
2588
- state->cache_count = state->cache_index = state->cache_repeats =
2589
- state->cache_delta = 0;
2568
+ cache_clear(state);
2590
2569
  return false;
2591
2570
  }
2592
2571
  BoundaryOutline outline = state->cache[state->cache_index];
@@ -2603,7 +2582,7 @@ static bool take_boundary(
2603
2582
  if (cached->repeat & SHIFT_RIGHT)
2604
2583
  cached->children[1] -= state->cache_delta;
2605
2584
  }
2606
- } else state->cache_count = state->cache_delta = 0;
2585
+ } else cache_clear(state);
2607
2586
  }
2608
2587
  } else if (--state->cache[0].repeat) {
2609
2588
  uint32_t delta = descriptor_length(outline.descriptor) -
@@ -2618,7 +2597,7 @@ static bool take_boundary(
2618
2597
  state->cache_count * sizeof(*state->cache)
2619
2598
  );
2620
2599
  }
2621
- *node = outline_expression(outline);
2600
+ *node = outline;
2622
2601
  return true;
2623
2602
  }
2624
2603
 
@@ -2635,7 +2614,7 @@ static void collect_expression(
2635
2614
  const int token = probe_token(input, expect_operand);
2636
2615
  if (
2637
2616
  !token ||
2638
- character_in(token, "\n;,) ]}{#") ||
2617
+ character_in(token, "\n;,)]}{#") ||
2639
2618
  (token == ':' && !questions && !branches) ||
2640
2619
  (print && !count && !questions && (token == '>' || token == '|'))
2641
2620
  )
@@ -2672,11 +2651,12 @@ atom_token(InputProbe *input, BoundaryState *state, bool lvalue) {
2672
2651
  }
2673
2652
 
2674
2653
  static unsigned selection_token(
2675
- RecognizedExpression selected,
2654
+ BoundaryOutline selected,
2676
2655
  InputProbe *input,
2677
2656
  BoundaryState *state
2678
2657
  ) {
2679
- switch (selected.kind) {
2658
+ const uint32_t signature = descriptor_signature(selected.descriptor);
2659
+ switch (signature_kind(signature)) {
2680
2660
  case RECOGNIZED_GET:
2681
2661
  return selected.children[0] ? SELECT_TARGET_GET : SELECT_GET;
2682
2662
  case RECOGNIZED_FILE:
@@ -2691,7 +2671,7 @@ static unsigned selection_token(
2691
2671
  return SELECT_CONCAT;
2692
2672
  case RECOGNIZED_TUPLE:
2693
2673
  case 0:
2694
- return atom_token(input, state, selected.lvalue);
2674
+ return atom_token(input, state, signature & SIGNATURE_LVALUE);
2695
2675
  case RECOGNIZED_BRANCHES:
2696
2676
  return SELECT_BRANCHES;
2697
2677
  case '?':
@@ -2718,7 +2698,7 @@ static bool select_boundary(TSLexer *lexer, BoundaryState *state) {
2718
2698
  RecognizerInput parser = {
2719
2699
  .expected = requested == UNCONSTRAINED ? 0 : descriptor_signature(requested)
2720
2700
  };
2721
- RecognizedExpression selected = {0};
2701
+ BoundaryOutline selected = {0};
2722
2702
  if (!take_boundary(state, requested, &selected)) {
2723
2703
  collect_expression(&input, &parser, count, state->print);
2724
2704
  if (
@@ -2728,15 +2708,14 @@ static bool select_boundary(TSLexer *lexer, BoundaryState *state) {
2728
2708
  !parser.failed &&
2729
2709
  recognizer_parse(&parser)
2730
2710
  ) {
2731
- const BoundaryOutline root = parser.tree[parser.tree_root - 1].outline;
2732
- selected = outline_expression(root);
2711
+ selected = parser.tree[parser.tree_root - 1].outline;
2733
2712
  cache_boundary(state, &parser);
2734
- take_boundary(state, root.descriptor, &selected);
2713
+ take_boundary(state, selected.descriptor, &selected);
2735
2714
  }
2736
2715
  }
2737
2716
  unsigned token = ERROR_SENTINEL;
2738
- if (selected.end) {
2739
- state->unary = selected.unary;
2717
+ if (descriptor_length(selected.descriptor)) {
2718
+ state->unary = descriptor_signature(selected.descriptor) & SIGNATURE_UNARY;
2740
2719
  state->pending[0] = selected.children[0];
2741
2720
  state->pending[1] = selected.children[1];
2742
2721
  token = selection_token(selected, &input, state);
@@ -2748,13 +2727,22 @@ static bool select_boundary(TSLexer *lexer, BoundaryState *state) {
2748
2727
  return token != ERROR_SENTINEL;
2749
2728
  }
2750
2729
 
2751
- // Close children before trailing blanks to preserve enclosing node ranges.
2752
2730
  static bool restoring_requested(const bool *valid) {
2753
2731
  bool restoring = valid[END_CHILD] || valid[RESUME];
2754
- for (unsigned i = 0; i < 4; i++) restoring |= valid[RESTORE_FIRST + i];
2732
+ for (unsigned digit = 0; digit < SAVED_DIGITS; digit++)
2733
+ restoring |= valid[RESTORE_FIRST + digit];
2755
2734
  return restoring;
2756
2735
  }
2757
2736
 
2737
+ static bool root_requested(const bool *valid) {
2738
+ return valid[ROOT] ||
2739
+ valid[PRINT_ROOT] ||
2740
+ valid[FOR_IN] ||
2741
+ valid[PRINT_ARGUMENTS] ||
2742
+ valid[LOCAL_ROOT] ||
2743
+ valid[LOCAL_PRINT_ROOT];
2744
+ }
2745
+
2758
2746
  static bool boundary_requested(const bool *valid) {
2759
2747
  for (unsigned i = ROOT; i <= LOCAL_PRINT_ROOT; i++)
2760
2748
  if (valid[i]) return true;
@@ -2765,8 +2753,9 @@ static bool boundary_requested(const bool *valid) {
2765
2753
  #define LOCAL_EXPRESSIONS 1
2766
2754
  #endif
2767
2755
 
2768
- static bool local_expression(TSLexer *lexer, bool print) {
2756
+ static bool local_expression(InputProbe *probe, bool print) {
2769
2757
  enum { GETLINE_SEEN = 1, AMBIGUOUS_SEEN = 2 };
2758
+ TSLexer *lexer = &probe->lexer;
2770
2759
  size_t depth = 0, questions = 0, capacity = 0;
2771
2760
  bool after_operand = false, separated = false, result = false;
2772
2761
  unsigned char flags = 0;
@@ -2794,6 +2783,9 @@ static bool local_expression(TSLexer *lexer, bool print) {
2794
2783
  if (word == WORD_KEYWORD) break;
2795
2784
  if (word == WORD_GETLINE) flags |= GETLINE_SEEN;
2796
2785
  after_operand = word != WORD_MEMBERSHIP;
2786
+ } else if (is_ascii_digit(character) || character == '.') {
2787
+ probe_number(probe);
2788
+ after_operand = true;
2797
2789
  } else {
2798
2790
  lexer->advance(lexer, false);
2799
2791
  if (
@@ -2833,7 +2825,7 @@ static bool local_expression(TSLexer *lexer, bool print) {
2833
2825
  if (character == '|' || (character == '<' && lexer->lookahead != '='))
2834
2826
  flags |= AMBIGUOUS_SEEN;
2835
2827
  if (character == ',') flags = 0;
2836
- after_operand = is_word_continue(character) || character == '.';
2828
+ after_operand = false;
2837
2829
  separated = character == ',';
2838
2830
  if (!depth && character == '?') questions++;
2839
2831
  if (!depth && character == ':' && questions) questions--;
@@ -2859,22 +2851,21 @@ static unsigned root_token(InputProbe *probe, const bool *valid) {
2859
2851
  probe_restore(probe, 0);
2860
2852
  if (valid[PRINT_ARGUMENTS] && probe_print_arguments(probe))
2861
2853
  return PRINT_ARGUMENTS;
2862
- probe_restore(probe, 0);
2863
2854
  const int32_t first = probe->lexer.lookahead;
2864
2855
  if (is_word_start(first)) {
2865
2856
  const WordKind word = scan_word_spelling(&probe->lexer);
2866
2857
  if (word == WORD_KEYWORD || word == WORD_MEMBERSHIP) return ERROR_SENTINEL;
2858
+ probe_restore(probe, 0);
2867
2859
  }
2868
2860
  const bool print = valid[PRINT_ROOT] || valid[LOCAL_PRINT_ROOT];
2869
- probe_restore(probe, 0);
2870
2861
  const bool local = (valid[LOCAL_ROOT] || valid[LOCAL_PRINT_ROOT]) &&
2871
- local_expression(&probe->lexer, print) &&
2862
+ local_expression(probe, print) &&
2872
2863
  !probe->failed;
2873
2864
  if (
2874
2865
  (!valid[ROOT] && !valid[LOCAL_ROOT] && !print) ||
2875
2866
  (print && (first == '>' || first == '|')) ||
2876
2867
  !first ||
2877
- character_in(first, "\n;,) ]}{:#")
2868
+ character_in(first, "\n;,)]}{:#")
2878
2869
  )
2879
2870
  return ERROR_SENTINEL;
2880
2871
  if (local) return print ? LOCAL_PRINT_ROOT : LOCAL_ROOT;
@@ -2898,18 +2889,9 @@ static bool scan_root(TSLexer *lexer, BoundaryState *state, const bool *valid) {
2898
2889
  static bool
2899
2890
  scan_boundary(TSLexer *lexer, BoundaryState *state, const bool *valid) {
2900
2891
  lexer->mark_end(lexer);
2901
- if (valid[NEXT_ARGUMENT] || valid[BEGIN_CHILD]) {
2902
- if (lexer->lookahead == '\n') return false;
2903
- }
2904
- if (
2905
- valid[ROOT] ||
2906
- valid[PRINT_ROOT] ||
2907
- valid[FOR_IN] ||
2908
- valid[PRINT_ARGUMENTS] ||
2909
- valid[LOCAL_ROOT] ||
2910
- valid[LOCAL_PRINT_ROOT]
2911
- )
2912
- return scan_root(lexer, state, valid);
2892
+ if ((valid[NEXT_ARGUMENT] || valid[BEGIN_CHILD]) && lexer->lookahead == '\n')
2893
+ return false;
2894
+ if (root_requested(valid)) return scan_root(lexer, state, valid);
2913
2895
  if (valid[NEXT_ARGUMENT]) {
2914
2896
  state->pending[0] = UNCONSTRAINED;
2915
2897
  return boundary_emit(lexer, state, NEXT_ARGUMENT);
@@ -2922,12 +2904,12 @@ scan_boundary(TSLexer *lexer, BoundaryState *state, const bool *valid) {
2922
2904
  return boundary_emit(lexer, state, RESUME);
2923
2905
  }
2924
2906
  if (valid[END_CHILD]) {
2925
- boundary_reset(state, 0, BOUNDARY_RESTORING);
2907
+ boundary_reset(state, 0);
2926
2908
  return boundary_emit(lexer, state, END_CHILD);
2927
2909
  }
2928
- for (unsigned digit = 0; digit < 4; digit++)
2910
+ for (unsigned digit = 0; digit < SAVED_DIGITS; digit++)
2929
2911
  if (valid[RESTORE_FIRST + digit]) {
2930
- state->carry = state->carry * 4 + digit;
2912
+ state->carry = state->carry * SAVED_DIGITS + digit;
2931
2913
  return boundary_emit(lexer, state, RESTORE_FIRST + digit);
2932
2914
  }
2933
2915
  if (valid[BEGIN_CHILD]) {
@@ -2938,15 +2920,13 @@ scan_boundary(TSLexer *lexer, BoundaryState *state, const bool *valid) {
2938
2920
  state->phase = BOUNDARY_SAVING;
2939
2921
  }
2940
2922
  if (state->carry) {
2941
- unsigned digit = state->carry & 3;
2942
- state->carry >>= 2;
2923
+ const unsigned digit = state->carry % SAVED_DIGITS;
2924
+ state->carry /= SAVED_DIGITS;
2943
2925
  return boundary_emit(lexer, state, SAVE_FIRST + digit);
2944
2926
  }
2945
2927
  uint64_t active = state->next;
2946
- boundary_reset(state, active, BOUNDARY_READY);
2947
- if (active == UNCONSTRAINED)
2948
- state->cache_count = state->cache_index = state->cache_repeats =
2949
- state->cache_delta = 0;
2928
+ boundary_reset(state, active);
2929
+ if (active == UNCONSTRAINED) cache_clear(state);
2950
2930
  return boundary_emit(lexer, state, BEGIN_CHILD);
2951
2931
  }
2952
2932
  if (state->active && !select_boundary(lexer, state)) return false;
@@ -2990,7 +2970,6 @@ static bool scan_outside(
2990
2970
  if (!recovering && restoring_requested(valid_symbols))
2991
2971
  return scan_boundary(lexer, &state->boundary, valid_symbols);
2992
2972
  while (is_ascii_blank(lexer->lookahead)) lexer->advance(lexer, true);
2993
- lexer->mark_end(lexer);
2994
2973
  if (lexer->lookahead == '\\') return scan_backslash(state, lexer);
2995
2974
  if (lexer->lookahead == '#' && valid_symbols[COMMENT])
2996
2975
  return scan_comment(lexer);
@@ -3010,7 +2989,6 @@ static bool scan_outside(
3010
2989
  return scan_sign(lexer, valid_symbols);
3011
2990
  }
3012
2991
 
3013
- // Tree-sitter passes NULL to every entry point after allocation failure.
3014
2992
  void *tree_sitter_posix_awk_external_scanner_create(void) {
3015
2993
  return ts_calloc(1, sizeof(ScannerState));
3016
2994
  }
@@ -3076,7 +3054,7 @@ void tree_sitter_posix_awk_external_scanner_deserialize(
3076
3054
  if (
3077
3055
  length < STATE_HEADER_BYTES ||
3078
3056
  (unsigned char)buffer[0] > LEXICAL_MODE_CONTINUED_NEWLINE ||
3079
- (unsigned char)buffer[1] > BOUNDARY_RESTORING
3057
+ (unsigned char)buffer[1] > BOUNDARY_SAVING
3080
3058
  )
3081
3059
  return;
3082
3060
  const unsigned count = (unsigned char)buffer[STATE_CACHE_COUNT_OFFSET];
package/tree-sitter.json CHANGED
@@ -12,7 +12,7 @@
12
12
  }
13
13
  ],
14
14
  "metadata": {
15
- "version": "0.20.0",
15
+ "version": "0.20.2",
16
16
  "license": "MIT",
17
17
  "description": "Tree-sitter grammar for POSIX awk.",
18
18
  "links": {