@konomanoasa/tree-sitter-posix-awk 0.20.1 → 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.1"
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.1"
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,7 +7,7 @@
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-gawk](https://github.com/konomanoasa/tree-sitter-gawk).
10
+ For GNU awk, see [tree-sitter-awk](https://github.com/konomanoasa/tree-sitter-awk).
11
11
 
12
12
  ## Installation
13
13
 
@@ -17,8 +17,6 @@ npm install @konomanoasa/tree-sitter-posix-awk
17
17
 
18
18
  ## Development
19
19
 
20
- Development uses Node.js 24.21.0 or later.
21
-
22
20
  ```sh
23
21
  npm install
24
22
  npm run build
@@ -32,7 +30,7 @@ npm test
32
30
 
33
31
  ## Used By
34
32
 
35
- [awk-ts-mode](https://github.com/konomanoasa/awk-ts-mode): Emacs major mode.
33
+ [awk-ts-mode](https://github.com/konomanoasa/awk-ts-mode)
36
34
 
37
35
  ## License
38
36
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@konomanoasa/tree-sitter-posix-awk",
3
- "version": "0.20.1",
3
+ "version": "0.20.2",
4
4
  "type": "module",
5
5
  "description": "Tree-sitter grammar for POSIX awk.",
6
6
  "license": "MIT",
@@ -21,11 +21,11 @@
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/build.test.js test/cst.test.js test/highlight.test.js test/incremental.test.js test/parser.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",
@@ -33,11 +33,10 @@
33
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/parser.test.js",
37
36
  "tree-sitter": "node scripts/tree-sitter.js"
38
37
  },
39
38
  "devDependencies": {
40
- "@biomejs/biome": "2.5.15",
39
+ "@biomejs/biome": "2.6.2",
41
40
  "@types/node": "26.6.5",
42
41
  "tree-sitter-cli": "0.27.1",
43
42
  "typescript": "7.0.2"
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 = 1,
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 {
@@ -703,7 +698,6 @@ static bool skip_scope_layout(TSLexer *lexer, bool separated) {
703
698
  }
704
699
 
705
700
  static bool skip_group(TSLexer *lexer, bool *tuple) {
706
- if (tuple) *tuple = false;
707
701
  bool after_operand = false;
708
702
  bool separated = false;
709
703
  unsigned depth = 0;
@@ -737,7 +731,6 @@ static bool skip_group(TSLexer *lexer, bool *tuple) {
737
731
  depth--;
738
732
  after_operand = true;
739
733
  } else if (character_in(character, "+-") && lexer->lookahead == character) {
740
- // Updates require lvalues, so a following slash divides.
741
734
  lexer->advance(lexer, false);
742
735
  after_operand = true;
743
736
  } else if (character_in(character, "&|") && lexer->lookahead == character) {
@@ -765,7 +758,6 @@ static WordAtom word_atom(TSLexer *lexer, WordKind word) {
765
758
  return ATOM_PLAIN;
766
759
  }
767
760
 
768
- // Restoring the probe must preserve furthest lookahead.
769
761
  typedef struct {
770
762
  TSLexer lexer;
771
763
  TSLexer *source;
@@ -833,6 +825,12 @@ static InputProbe probe_open(TSLexer *source) {
833
825
  };
834
826
  }
835
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
+
836
834
  enum ProbeToken {
837
835
  PROBE_END = 0,
838
836
  PROBE_VALUE = 256,
@@ -855,11 +853,7 @@ static int probe_token(InputProbe *probe, bool expect_operand) {
855
853
  }
856
854
  const int32_t character = lexer->lookahead;
857
855
  if (is_ascii_digit(character) || character == '.') {
858
- if (!skip_number(lexer)) {
859
- return PROBE_END;
860
- }
861
- probe_restore(probe, probe->marked_end);
862
- return PROBE_VALUE;
856
+ return probe_number(probe) ? PROBE_VALUE : PROBE_END;
863
857
  }
864
858
  if (character == '"' || (expect_operand && character == '/')) {
865
859
  return skip_literal(lexer) ? PROBE_VALUE : PROBE_END;
@@ -1164,7 +1158,7 @@ typedef struct {
1164
1158
  bool *alternatives;
1165
1159
  size_t depth, context_depth;
1166
1160
  RecognizedValue value;
1167
- bool valid, exhausted;
1161
+ bool valid, exhausted, checked, feasible;
1168
1162
  size_t remaining;
1169
1163
  RecognizerContext *contexts;
1170
1164
  size_t context_count, context_capacity, *context_slots, context_slot_capacity;
@@ -1681,7 +1675,7 @@ typedef struct {
1681
1675
  typedef struct {
1682
1676
  RecognizerInput *input;
1683
1677
  SharedNode *nodes;
1684
- size_t length, capacity;
1678
+ size_t length, capacity, generated;
1685
1679
  SharedSet *memo;
1686
1680
  bool *needed;
1687
1681
  uint32_t *ranks[2], *stack;
@@ -1710,6 +1704,7 @@ static SharedNode shared_node(
1710
1704
  bool postfix,
1711
1705
  bool unary
1712
1706
  ) {
1707
+ if (parser->generated != SIZE_MAX) parser->generated++;
1713
1708
  SharedNode node = {
1714
1709
  .descriptor = make_descriptor(
1715
1710
  boundary_signature(kind, ceiling, lvalue, postfix, unary),
@@ -1721,7 +1716,7 @@ static SharedNode shared_node(
1721
1716
  };
1722
1717
  if (kind == RECOGNIZED_GET) {
1723
1718
  node.decision = start;
1724
- node.rank = left || right ? 1 : 2;
1719
+ node.rank = right ? 1 : 2;
1725
1720
  } else if (kind == RECOGNIZED_FILE || kind == '<') {
1726
1721
  node.decision = shared_end(parser->nodes[left - 1]);
1727
1722
  node.rank = kind == '<' ? (uint32_t)parser->input->length + 2
@@ -2172,7 +2167,8 @@ static bool shared_tree(SharedRecognition *parser, uint32_t root) {
2172
2167
  return true;
2173
2168
  }
2174
2169
 
2175
- static bool shared_recognize(RecognizerInput *input, bool select) {
2170
+ static bool
2171
+ shared_recognize(RecognizerInput *input, bool select, size_t *generated) {
2176
2172
  if (
2177
2173
  input->length > UINT32_MAX - 2 ||
2178
2174
  input->length > SIZE_MAX / RECOGNIZER_LEVELS / sizeof(SharedSet) - 1
@@ -2240,6 +2236,7 @@ static bool shared_recognize(RecognizerInput *input, bool select) {
2240
2236
  if (best && !parser.failed) accepted = !select || shared_tree(&parser, best);
2241
2237
  shared_set_destroy(&local);
2242
2238
  done:
2239
+ if (generated) *generated = parser.generated;
2243
2240
  if (parser.memo)
2244
2241
  for (size_t i = 0; i < count; i++) shared_set_destroy(&parser.memo[i]);
2245
2242
  ts_free(parser.memo);
@@ -2251,14 +2248,26 @@ done:
2251
2248
  return accepted;
2252
2249
  }
2253
2250
 
2254
- static bool shared_parse(RecognizerInput *input) {
2255
- return shared_recognize(input, false) && shared_recognize(input, true);
2256
- }
2257
-
2258
2251
  #ifndef RECOGNIZER_WORK_FACTOR
2259
2252
  #define RECOGNIZER_WORK_FACTOR 8
2260
2253
  #endif
2261
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
+
2262
2271
  static bool recognizer_parse(RecognizerInput *input) {
2263
2272
  if (input->length > (SIZE_MAX / sizeof(RecognizerFrame)) - 2) return false;
2264
2273
  Recognizer parser = {.input = input};
@@ -2266,7 +2275,7 @@ static bool recognizer_parse(RecognizerInput *input) {
2266
2275
  parser.choices = ts_calloc(input->length, sizeof(*parser.choices));
2267
2276
  parser.sources = ts_calloc(input->length, sizeof(*parser.sources));
2268
2277
  parser.alternatives = ts_calloc(input->length, sizeof(*parser.alternatives));
2269
- bool result = false, shared = false;
2278
+ bool result = false;
2270
2279
  size_t limit = input->length > SIZE_MAX / (input->length ? input->length : 1)
2271
2280
  ? SIZE_MAX
2272
2281
  : input->length * input->length;
@@ -2279,10 +2288,11 @@ static bool recognizer_parse(RecognizerInput *input) {
2279
2288
  )
2280
2289
  goto done;
2281
2290
  for (;;) {
2282
- if (input->length > parser.remaining) {
2283
- shared = true;
2291
+ if (
2292
+ input->length > parser.remaining &&
2293
+ (!recognizer_extend(&parser) || input->length > parser.remaining)
2294
+ )
2284
2295
  goto done;
2285
- }
2286
2296
  parser.remaining -= input->length;
2287
2297
  memset(parser.sources, 0, input->length * sizeof(*parser.sources));
2288
2298
  memset(
@@ -2302,17 +2312,14 @@ static bool recognizer_parse(RecognizerInput *input) {
2302
2312
  recognizer_push(&parser, 0, EXPR_ASSIGNMENT);
2303
2313
  }
2304
2314
  while (parser.valid && parser.depth) {
2305
- if (!parser.remaining) {
2306
- shared = true;
2307
- goto done;
2308
- }
2315
+ if (!parser.remaining && !recognizer_extend(&parser)) goto done;
2309
2316
  parser.remaining--;
2310
2317
  if (!recognizer_checkpoint(&parser)) {
2311
2318
  parser.valid = false;
2312
2319
  break;
2313
2320
  }
2314
2321
  if (parser.exhausted) {
2315
- shared = true;
2322
+ if (recognizer_extend(&parser)) continue;
2316
2323
  goto done;
2317
2324
  }
2318
2325
  if (parser.context_depth >= parser.depth)
@@ -2332,7 +2339,6 @@ static bool recognizer_parse(RecognizerInput *input) {
2332
2339
  size_t position = input->length;
2333
2340
  while (position && !parser.alternatives[position - 1]) position--;
2334
2341
  if (!position) break;
2335
- // Only positions beyond the backtrack point are exhausted.
2336
2342
  for (size_t i = 0; i < parser.history_count; i++) {
2337
2343
  if (parser.history[i].position > position - 1)
2338
2344
  recognizer_failed(&parser, parser.history[i], true);
@@ -2353,7 +2359,8 @@ done:
2353
2359
  ts_free(parser.choices);
2354
2360
  ts_free(parser.sources);
2355
2361
  ts_free(parser.alternatives);
2356
- return shared ? shared_parse(input) : result;
2362
+ return !result && parser.feasible ? shared_recognize(input, true, NULL)
2363
+ : result;
2357
2364
  }
2358
2365
 
2359
2366
  static const uint64_t UNCONSTRAINED = UINT64_MAX;
@@ -2513,7 +2520,7 @@ static bool cache_period(
2513
2520
  static void
2514
2521
  cache_boundary(BoundaryState *state, const RecognizerInput *parser) {
2515
2522
  cache_clear(state);
2516
- if (!BOUNDARY_CACHE_CAPACITY || !parser->tree_root) return;
2523
+ if (!BOUNDARY_CACHE_CAPACITY) return;
2517
2524
  if (parser->tree_count > SIZE_MAX / 2 / sizeof(size_t) - 1) return;
2518
2525
  size_t *order = ts_calloc(parser->tree_count * 2 + 2, sizeof(*order));
2519
2526
  if (!order) return;
@@ -2746,8 +2753,9 @@ static bool boundary_requested(const bool *valid) {
2746
2753
  #define LOCAL_EXPRESSIONS 1
2747
2754
  #endif
2748
2755
 
2749
- static bool local_expression(TSLexer *lexer, bool print) {
2756
+ static bool local_expression(InputProbe *probe, bool print) {
2750
2757
  enum { GETLINE_SEEN = 1, AMBIGUOUS_SEEN = 2 };
2758
+ TSLexer *lexer = &probe->lexer;
2751
2759
  size_t depth = 0, questions = 0, capacity = 0;
2752
2760
  bool after_operand = false, separated = false, result = false;
2753
2761
  unsigned char flags = 0;
@@ -2775,6 +2783,9 @@ static bool local_expression(TSLexer *lexer, bool print) {
2775
2783
  if (word == WORD_KEYWORD) break;
2776
2784
  if (word == WORD_GETLINE) flags |= GETLINE_SEEN;
2777
2785
  after_operand = word != WORD_MEMBERSHIP;
2786
+ } else if (is_ascii_digit(character) || character == '.') {
2787
+ probe_number(probe);
2788
+ after_operand = true;
2778
2789
  } else {
2779
2790
  lexer->advance(lexer, false);
2780
2791
  if (
@@ -2814,7 +2825,7 @@ static bool local_expression(TSLexer *lexer, bool print) {
2814
2825
  if (character == '|' || (character == '<' && lexer->lookahead != '='))
2815
2826
  flags |= AMBIGUOUS_SEEN;
2816
2827
  if (character == ',') flags = 0;
2817
- after_operand = is_word_continue(character) || character == '.';
2828
+ after_operand = false;
2818
2829
  separated = character == ',';
2819
2830
  if (!depth && character == '?') questions++;
2820
2831
  if (!depth && character == ':' && questions) questions--;
@@ -2840,16 +2851,15 @@ static unsigned root_token(InputProbe *probe, const bool *valid) {
2840
2851
  probe_restore(probe, 0);
2841
2852
  if (valid[PRINT_ARGUMENTS] && probe_print_arguments(probe))
2842
2853
  return PRINT_ARGUMENTS;
2843
- probe_restore(probe, 0);
2844
2854
  const int32_t first = probe->lexer.lookahead;
2845
2855
  if (is_word_start(first)) {
2846
2856
  const WordKind word = scan_word_spelling(&probe->lexer);
2847
2857
  if (word == WORD_KEYWORD || word == WORD_MEMBERSHIP) return ERROR_SENTINEL;
2858
+ probe_restore(probe, 0);
2848
2859
  }
2849
2860
  const bool print = valid[PRINT_ROOT] || valid[LOCAL_PRINT_ROOT];
2850
- probe_restore(probe, 0);
2851
2861
  const bool local = (valid[LOCAL_ROOT] || valid[LOCAL_PRINT_ROOT]) &&
2852
- local_expression(&probe->lexer, print) &&
2862
+ local_expression(probe, print) &&
2853
2863
  !probe->failed;
2854
2864
  if (
2855
2865
  (!valid[ROOT] && !valid[LOCAL_ROOT] && !print) ||
@@ -2894,7 +2904,7 @@ scan_boundary(TSLexer *lexer, BoundaryState *state, const bool *valid) {
2894
2904
  return boundary_emit(lexer, state, RESUME);
2895
2905
  }
2896
2906
  if (valid[END_CHILD]) {
2897
- boundary_reset(state, 0, BOUNDARY_RESTORING);
2907
+ boundary_reset(state, 0);
2898
2908
  return boundary_emit(lexer, state, END_CHILD);
2899
2909
  }
2900
2910
  for (unsigned digit = 0; digit < SAVED_DIGITS; digit++)
@@ -2915,7 +2925,7 @@ scan_boundary(TSLexer *lexer, BoundaryState *state, const bool *valid) {
2915
2925
  return boundary_emit(lexer, state, SAVE_FIRST + digit);
2916
2926
  }
2917
2927
  uint64_t active = state->next;
2918
- boundary_reset(state, active, BOUNDARY_READY);
2928
+ boundary_reset(state, active);
2919
2929
  if (active == UNCONSTRAINED) cache_clear(state);
2920
2930
  return boundary_emit(lexer, state, BEGIN_CHILD);
2921
2931
  }
@@ -2960,7 +2970,6 @@ static bool scan_outside(
2960
2970
  if (!recovering && restoring_requested(valid_symbols))
2961
2971
  return scan_boundary(lexer, &state->boundary, valid_symbols);
2962
2972
  while (is_ascii_blank(lexer->lookahead)) lexer->advance(lexer, true);
2963
- lexer->mark_end(lexer);
2964
2973
  if (lexer->lookahead == '\\') return scan_backslash(state, lexer);
2965
2974
  if (lexer->lookahead == '#' && valid_symbols[COMMENT])
2966
2975
  return scan_comment(lexer);
@@ -2980,7 +2989,6 @@ static bool scan_outside(
2980
2989
  return scan_sign(lexer, valid_symbols);
2981
2990
  }
2982
2991
 
2983
- // Tree-sitter passes NULL to every entry point after allocation failure.
2984
2992
  void *tree_sitter_posix_awk_external_scanner_create(void) {
2985
2993
  return ts_calloc(1, sizeof(ScannerState));
2986
2994
  }
@@ -3046,7 +3054,7 @@ void tree_sitter_posix_awk_external_scanner_deserialize(
3046
3054
  if (
3047
3055
  length < STATE_HEADER_BYTES ||
3048
3056
  (unsigned char)buffer[0] > LEXICAL_MODE_CONTINUED_NEWLINE ||
3049
- (unsigned char)buffer[1] > BOUNDARY_RESTORING
3057
+ (unsigned char)buffer[1] > BOUNDARY_SAVING
3050
3058
  )
3051
3059
  return;
3052
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.1",
15
+ "version": "0.20.2",
16
16
  "license": "MIT",
17
17
  "description": "Tree-sitter grammar for POSIX awk.",
18
18
  "links": {