@willbooster/tree-sitter-c 23.0.0 → 24.0.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/README.md CHANGED
@@ -25,7 +25,7 @@ the trees that error recovery produces may change; such changes are released as
25
25
  ## Usage
26
26
 
27
27
  The npm package ships `tree-sitter-c.wasm` for
28
- [@willbooster/web-tree-sitter](https://www.npmjs.com/package/@willbooster/web-tree-sitter) version 15.0.3 or later within 15.x, which runs in Node.js, Bun, browsers, and Cloudflare Workers. The parser carries the ABI number of the WillBooster/tree-sitter generator (`0x5742000b`), so it loads only with that runtime; upstream tree-sitter runtimes reject it. In Node.js and Bun, load the grammar from its path:
28
+ [@willbooster/web-tree-sitter](https://www.npmjs.com/package/@willbooster/web-tree-sitter) version 15.0.3 or later within 15.x, or 16.x, which runs in Node.js, Bun, browsers, and Cloudflare Workers. The parser carries the ABI number of the WillBooster/tree-sitter generator (`0x5742000b`), so it loads only with that runtime; upstream tree-sitter runtimes reject it. In Node.js and Bun, load the grammar from its path:
29
29
 
30
30
  ```js
31
31
  import { fileURLToPath } from 'node:url';
@@ -67,7 +67,7 @@ parser.setLanguage(await Language.load(c));
67
67
  The package also ships `grammar.js`, scanner sources under `src/`,
68
68
  and the node types in `src/node-types.json` for grammars extending C (such as C++).
69
69
  The scanner sources include `scanner.c`, `pragma.h`, `identifier.h`, `keywords.h`, `functionMacro.h`,
70
- `conditionalMacro.h`, and `gnuRawString.h`.
70
+ `conditionalMacro.h`, `macroProof.h`, and `gnuRawString.h`.
71
71
 
72
72
  Derived grammars must port the inherited external-token dispatch from `src/scanner.c`.
73
73
  Keep preprocessor argument/newline handling ahead of annotation lookahead.
@@ -92,9 +92,15 @@ This separate zero-width guard proves marker words followed by a braced macro-ty
92
92
  consumes each marker and validates its type. Keep `MacroName.preserve_end` in `pragma.h` so scanning subsequent words
93
93
  does not move the guard's end. Its default is false for ordinary name scans. Derived grammars can supply reserved
94
94
  starter words and the same `MacroAttributeSymbols` context to the guard helper; the C scanner supplies an empty table.
95
- The helper shares pragma and scalar-object dispatch with annotation guards. Copy both `functionMacro.h` and `pragma.h`
95
+ The helper shares pragma and scalar-object dispatch with annotation guards. Copy `functionMacro.h`, `macroProof.h`, and `pragma.h`
96
96
  when porting this dispatch. Later pragma lookahead must preserve the zero-width token end.
97
97
 
98
+ The C scanner owns a `MacroProofMemo` for each full parse and frees it on scanner destruction.
99
+ Pass it as `MacroAttributeSymbols.proof_memo` to reuse continuation proofs by physical byte position and lexical state.
100
+ The first-word dispatch stays uncached because it depends on the valid external tokens. The memo binds to one reserved-word table;
101
+ a different table disables reuse. Derived scanners can leave `proof_memo` unset, or own a memo with the same lifetime.
102
+ These source facts need no serialized bytes.
103
+
98
104
  When overriding declarations or function definitions, retain both guarded ordinary-type/declarator alternatives and
99
105
  annotation alternatives in ordinary, block, and for-declaration builders. Accept the matching passive guard after calling conventions and before ordinary, builtin,
100
106
  and initialized declarators. Derived custom declaration builders also need `_guarded_type_specifiers` and
@@ -190,10 +196,10 @@ arrays, parenthesized/function declarators, initializers and multiple declarator
190
196
 
191
197
  In Rust, depend on the [crate](https://crates.io/crates/willbooster-tree-sitter-c) and on
192
198
  [willbooster-tree-sitter](https://crates.io/crates/willbooster-tree-sitter), the runtime this package is tested and
193
- fuzzed with, at version 15.0.3 or later within 15.x. No other runtime loads the parser:
199
+ fuzzed with, at version 15.0.3 or later within 15.x, or 16.x. No other runtime loads the parser:
194
200
 
195
201
  ```sh
196
- cargo add willbooster-tree-sitter@15.0.3 --rename tree-sitter
202
+ cargo add willbooster-tree-sitter@16.0.0 --rename tree-sitter
197
203
  cargo add willbooster-tree-sitter-c --rename tree-sitter-c
198
204
  ```
199
205
 
package/grammar.js CHANGED
@@ -62,6 +62,35 @@ const BINARY_OPERATORS = [
62
62
 
63
63
  const LINE_SPLICE = /\\(?:\r\n?|\n\r?)/;
64
64
  const LINE_COMMENT = seq('/', repeat(LINE_SPLICE), '/', /(\\+(.|\r?\n)|[^\\\n])*/);
65
+ const COMMENT_SPLICES = repeat(seq('\\', /[\r\n]/));
66
+ const COMMENT_ESCAPE = seq('\\', /[^\r\n*]/);
67
+ const BLOCK_COMMENT_STARS = seq(
68
+ '*',
69
+ repeat(
70
+ choice(
71
+ seq(COMMENT_SPLICES, choice('*', String.raw`\*`)),
72
+ seq(COMMENT_SPLICES, '\\\r', '\n'),
73
+ seq(COMMENT_SPLICES, '\\\n', '\r')
74
+ )
75
+ )
76
+ );
77
+ const BLOCK_COMMENT_NONCLOSING = choice(
78
+ /[^/*\\]/,
79
+ COMMENT_ESCAPE,
80
+ seq(COMMENT_SPLICES, '\\\r', choice(/[^/*\\\n]/, COMMENT_ESCAPE)),
81
+ seq(COMMENT_SPLICES, '\\\n', choice(/[^/*\\\r]/, COMMENT_ESCAPE))
82
+ );
83
+ const BLOCK_COMMENT = seq(
84
+ '/',
85
+ repeat(LINE_SPLICE),
86
+ '*',
87
+ /[^*]*/,
88
+ repeat(seq(BLOCK_COMMENT_STARS, BLOCK_COMMENT_NONCLOSING, /[^*]*/)),
89
+ BLOCK_COMMENT_STARS,
90
+ COMMENT_SPLICES,
91
+ '/'
92
+ );
93
+ const PREPROC_PREFIX = seq('#', repeat(choice(/[ \t\v\f]/, LINE_SPLICE, BLOCK_COMMENT)));
65
94
  const PRAGMA_SPACING = repeat(choice(/\s/, /\\(?:\r\n?|\n\r?)/));
66
95
  const PREPROC_ARGUMENT = /\S([^/\n]|\/[^*]|\\\r?\n)*/;
67
96
  const VA_ARG_KEYWORDS = choice('va_arg', '__builtin_va_arg');
@@ -627,7 +656,11 @@ module.exports = Object.assign(
627
656
 
628
657
  preproc_call: ($) =>
629
658
  choice(
630
- preprocCall($, alias(/#[ \t]*pragma/, $.preproc_directive), choice($._preproc_directive_arg, $.preproc_arg)),
659
+ preprocCall(
660
+ $,
661
+ alias(preprocessorToken('pragma'), $.preproc_directive),
662
+ choice($._preproc_directive_arg, $.preproc_arg)
663
+ ),
631
664
  preprocCall($, $.preproc_directive, $._preproc_directive_arg)
632
665
  ),
633
666
 
@@ -659,7 +692,7 @@ module.exports = Object.assign(
659
692
  preproc_arg: () => token(prec(-1, PREPROC_ARGUMENT)),
660
693
 
661
694
  _preproc_directive_arg: () => token(prec(-2, PREPROC_ARGUMENT)),
662
- preproc_directive: () => /#[ \t]*[a-zA-Z0-9]\w*/,
695
+ preproc_directive: () => preprocessorToken(/[a-zA-Z0-9]\w*/),
663
696
 
664
697
  _preproc_expression: ($) =>
665
698
  choice(
@@ -2281,7 +2314,7 @@ module.exports = Object.assign(
2281
2314
  _preproc_number: () =>
2282
2315
  /(\d|\.\d)([\p{XID_Continue}.]|[eEpP][+-]|'[_a-zA-Z0-9]|\\u[0-9a-fA-F]{4}|\\U[0-9a-fA-F]{8})*/u,
2283
2316
 
2284
- comment: () => token(choice(LINE_COMMENT, seq('/*', /[^*]*\*+([^/*][^*]*\*+)*/, '/'))),
2317
+ comment: () => token(choice(LINE_COMMENT, BLOCK_COMMENT)),
2285
2318
  },
2286
2319
  }),
2287
2320
  { PREC, preprocIf, preprocessor, commaSep, commaSep1, declarationSpecifiers }
@@ -2682,14 +2715,19 @@ function preprocIf(suffix, content, precedence = 0, repeatContent = true) {
2682
2715
  }
2683
2716
 
2684
2717
  /**
2685
- * Creates a preprocessor regex rule
2718
+ * Creates a preprocessing directive token.
2686
2719
  *
2687
2720
  * @param {string} command
2688
2721
  *
2689
2722
  * @returns {import('./types/treeSitterDsl').AliasRule}
2690
2723
  */
2691
2724
  function preprocessor(command) {
2692
- return alias(new RegExp('#[ \t]*' + command), '#' + command);
2725
+ return alias(preprocessorToken(command), '#' + command);
2726
+ }
2727
+
2728
+ /** @param {string | RegExp} command */
2729
+ function preprocessorToken(command) {
2730
+ return token(seq(PREPROC_PREFIX, command));
2693
2731
  }
2694
2732
 
2695
2733
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@willbooster/tree-sitter-c",
3
- "version": "23.0.0",
3
+ "version": "24.0.1",
4
4
  "description": "C grammar for tree-sitter",
5
5
  "keywords": [
6
6
  "c",
@@ -25,6 +25,7 @@
25
25
  "src/identifier.h",
26
26
  "src/keywords.h",
27
27
  "src/functionMacro.h",
28
+ "src/macroProof.h",
28
29
  "src/conditionalMacro.h",
29
30
  "src/gnuRawString.h",
30
31
  "src/scanner.c"
@@ -13,7 +13,8 @@ typedef struct {
13
13
 
14
14
  static bool conditional_macro_body(TSLexer *lexer, const ConditionalMacroSymbols *symbols, bool *escaped);
15
15
  static bool conditional_macro_word(TSLexer *lexer, MacroName *name, bool escaped);
16
- static bool conditional_macro_spacing(TSLexer *lexer, bool *separated, bool *escaped);
16
+ static bool conditional_macro_spacing(TSLexer *lexer, bool *separated, bool *escaped, bool directive);
17
+ static bool conditional_macro_space(int32_t c, bool directive);
17
18
  static bool conditional_macro_next_line(TSLexer *lexer, bool *escaped);
18
19
  static bool conditional_macro_directive(TSLexer *lexer, MacroName *name);
19
20
  static bool conditional_macro_marker_line(TSLexer *lexer, const ConditionalMacroSymbols *symbols, bool *escaped);
@@ -50,7 +51,7 @@ static bool scan_conditional_macro(TSLexer *lexer, const ConditionalMacroSymbols
50
51
  static bool conditional_macro_body(TSLexer *lexer, const ConditionalMacroSymbols *symbols, bool *escaped) {
51
52
  bool separated = false;
52
53
  MacroName condition = {.ascii = true};
53
- if (!conditional_macro_spacing(lexer, &separated, escaped) || !separated ||
54
+ if (!conditional_macro_spacing(lexer, &separated, escaped, false) || !separated ||
54
55
  !conditional_macro_word(lexer, &condition, *escaped) || !conditional_macro_next_line(lexer, escaped)) return false;
55
56
  if (!conditional_macro_marker_line(lexer, symbols, escaped) || !conditional_macro_closing(lexer)) return false;
56
57
  return conditional_macro_skip_lines(lexer, escaped) && !lexer->eof(lexer) && lexer->lookahead != '#';
@@ -63,7 +64,7 @@ static bool conditional_macro_closing(TSLexer *lexer) {
63
64
  return false;
64
65
  bool separated = false;
65
66
  bool escaped = false;
66
- if (!conditional_macro_spacing(lexer, &separated, &escaped)) return false;
67
+ if (!conditional_macro_spacing(lexer, &separated, &escaped, false)) return false;
67
68
  return !escaped && (lexer->eof(lexer) || lexer->lookahead == '\n' || lexer->lookahead == '\r');
68
69
  }
69
70
 
@@ -151,7 +152,7 @@ static bool conditional_macro_skip_lines(TSLexer *lexer, bool *escaped) {
151
152
  bool separated = false;
152
153
  for (;;) {
153
154
  *escaped = false;
154
- if (!conditional_macro_spacing(lexer, &separated, escaped)) return false;
155
+ if (!conditional_macro_spacing(lexer, &separated, escaped, false)) return false;
155
156
  if (*escaped || !scan_preproc_newline(lexer, false)) return true;
156
157
  }
157
158
  }
@@ -167,25 +168,27 @@ static bool conditional_macro_marker_line(TSLexer *lexer, const ConditionalMacro
167
168
  static bool conditional_macro_directive(TSLexer *lexer, MacroName *name) {
168
169
  if (lexer->lookahead != '#') return false;
169
170
  lexer->advance(lexer, false);
170
- while (lexer->lookahead == ' ' || lexer->lookahead == '\t') lexer->advance(lexer, false);
171
- return conditional_macro_word(lexer, name, false);
171
+ bool separated = false;
172
+ bool escaped = false;
173
+ return conditional_macro_spacing(lexer, &separated, &escaped, true) && !escaped &&
174
+ conditional_macro_word(lexer, name, false);
172
175
  }
173
176
 
174
177
  static bool conditional_macro_next_line(TSLexer *lexer, bool *escaped) {
175
178
  bool separated = false;
176
179
  *escaped = false;
177
- if (!conditional_macro_spacing(lexer, &separated, escaped) || *escaped ||
180
+ if (!conditional_macro_spacing(lexer, &separated, escaped, false) || *escaped ||
178
181
  !scan_preproc_newline(lexer, false)) return false;
179
182
  for (;;) {
180
- if (!conditional_macro_spacing(lexer, &separated, escaped) || lexer->eof(lexer)) return false;
183
+ if (!conditional_macro_spacing(lexer, &separated, escaped, false) || lexer->eof(lexer)) return false;
181
184
  if (*escaped || !scan_preproc_newline(lexer, false)) return true;
182
185
  }
183
186
  }
184
187
 
185
- static bool conditional_macro_spacing(TSLexer *lexer, bool *separated, bool *escaped) {
188
+ static bool conditional_macro_spacing(TSLexer *lexer, bool *separated, bool *escaped, bool directive) {
186
189
  while (lexer->lookahead != '\n' && lexer->lookahead != '\r') {
187
190
  int32_t c = lexer->lookahead;
188
- if (pragma_space(c)) {
191
+ if (conditional_macro_space(c, directive)) {
189
192
  *separated = true;
190
193
  lexer->advance(lexer, false);
191
194
  continue;
@@ -204,6 +207,10 @@ static bool conditional_macro_spacing(TSLexer *lexer, bool *separated, bool *esc
204
207
  return true;
205
208
  }
206
209
 
210
+ static bool conditional_macro_space(int32_t c, bool directive) {
211
+ return directive ? c == ' ' || c == '\t' || c == '\v' || c == '\f' : pragma_space(c);
212
+ }
213
+
207
214
  static bool conditional_macro_word(TSLexer *lexer, MacroName *name, bool escaped) {
208
215
  bool has_name = escaped;
209
216
  if (escaped) {
@@ -1,8 +1,10 @@
1
+ // code-gauge-allow-file fileNcss 579 -- Annotation and argument scanners share lexical helpers and keyword tables.
1
2
  #ifndef TREE_SITTER_C_FUNCTION_MACRO_H_
2
3
  #define TREE_SITTER_C_FUNCTION_MACRO_H_
3
4
 
4
5
  #include "pragma.h"
5
6
  #include "keywords.h"
7
+ #include "macroProof.h"
6
8
 
7
9
  typedef struct {
8
10
  bool identifier;
@@ -33,6 +35,7 @@ typedef struct {
33
35
  bool allow_object_start;
34
36
  TSSymbol braced_start;
35
37
  bool allow_braced_start;
38
+ MacroProofMemo *proof_memo;
36
39
  } MacroAttributeSymbols;
37
40
 
38
41
  typedef enum {
@@ -82,7 +85,6 @@ static inline bool scan_raw_macro_type_tail(TSLexer *lexer) {
82
85
  int32_t c = lexer->lookahead;
83
86
  if (c == ')' && depth == 1) break;
84
87
  if (function_macro_argument_token(lexer, &word, NULL) != MACRO_ARGUMENT_PLAIN) continue;
85
- // Like the attribute guard, leave a balanced group's first brace and later arguments to the grammar.
86
88
  if (c == '{' && balanced) return false;
87
89
  if (balanced) balanced = function_macro_group(c, '(', ')', &depth, &nested, &braces);
88
90
  else if (c == '(') depth++;
@@ -123,7 +125,6 @@ static bool scan_function_attribute_guard(TSLexer *lexer, bool prototype, bool r
123
125
  bool literal = false;
124
126
  bool unterminated_raw = false;
125
127
  MacroGroup group = function_macro_group_prefix(lexer, '(', ')', &literal, &unterminated_raw, false);
126
- // The grammar closes braced arguments without repeated lookahead through an incomplete body.
127
128
  if (group == MACRO_GROUP_BRACED && symbols->allow_braced_start) {
128
129
  lexer->result_symbol = symbols->braced_start;
129
130
  return true;
@@ -157,57 +158,92 @@ typedef enum {
157
158
 
158
159
  static MacroTypePrefix function_macro_type_prefix_item(TSLexer *lexer, const MacroName *name, const char *const *reserved_words, unsigned reserved_count, bool type_argument);
159
160
 
161
+ typedef struct {
162
+ bool marker, known_type, tag, body, started;
163
+ } MacroTypeState;
164
+
165
+ typedef enum { MACRO_TYPE_CONTINUE, MACRO_TYPE_ACCEPT, MACRO_TYPE_REJECT } MacroTypeResult;
166
+
167
+ static MacroTypeResult function_macro_type_transition(MacroTypeState *state, MacroTypePrefix kind);
168
+ static MacroTypeResult function_macro_type_group(TSLexer *lexer, MacroTypeState *state);
169
+ static MacroTypeResult function_macro_type_word(TSLexer *lexer, const MacroAttributeSymbols *symbols,
170
+ const char *const *reserved_words, unsigned reserved_count, MacroTypeState *state);
171
+
172
+ static bool function_macro_prove_braced_type(TSLexer *lexer, const MacroAttributeSymbols *symbols,
173
+ const char *const *reserved_words, unsigned reserved_count, MacroProofMemo *memo);
174
+
160
175
  static bool scan_braced_macro_type_start(TSLexer *lexer, const MacroAttributeSymbols *symbols, const char *const *reserved_words, unsigned reserved_count) {
161
- bool marker = false;
162
- bool known_type = false;
163
- bool tag = false;
164
- bool body = false;
165
- bool started = false;
176
+ MacroProofMemo *memo = symbols->proof_memo;
177
+ if (!macro_proof_begin(memo, reserved_words, reserved_count)) memo = NULL;
178
+ bool accepted = function_macro_prove_braced_type(lexer, symbols, reserved_words, reserved_count, memo);
179
+ macro_proof_finish(memo, accepted);
180
+ return accepted;
181
+ }
182
+
183
+ static bool function_macro_prove_braced_type(TSLexer *lexer, const MacroAttributeSymbols *symbols,
184
+ const char *const *reserved_words, unsigned reserved_count, MacroProofMemo *memo) {
185
+ MacroTypeState state = {0};
166
186
  for (;;) {
167
187
  while (pragma_space(lexer->lookahead)) lexer->advance(lexer, true);
168
- if (lexer->lookahead == '[') {
169
- if (!started) lexer->mark_end(lexer);
170
- started = true;
171
- if (!function_macro_bracket_attribute(lexer)) return false;
172
- continue;
173
- }
174
- if (lexer->lookahead == '{') {
175
- if (!body || !function_macro_modifier_balanced(lexer, '{', '}')) return false;
176
- body = tag = false;
177
- continue;
178
- }
179
- char word[FUNCTION_MACRO_KEYWORD_MAX_LENGTH + 1] = {0};
180
- MacroName name = {.word = word, .capacity = sizeof(word) - 1, .ascii = true, .lookahead_only = true, .preserve_end = started};
181
- if (!scan_macro_name(lexer, &name)) return false;
182
- if (!started) {
183
- bool handled;
184
- bool accepted = function_macro_first_word(lexer, &name, symbols, &handled);
185
- if (handled) return accepted;
186
- }
187
- started = true;
188
- MacroTypePrefix kind = function_macro_type_prefix_item(lexer, &name, reserved_words, reserved_count, !known_type);
189
- switch (kind) {
190
- case MACRO_TYPE_PREFIX_INVALID:
191
- return false;
192
- case MACRO_TYPE_PREFIX_TYPE:
193
- return marker;
194
- case MACRO_TYPE_PREFIX_MODIFIER:
195
- continue;
196
- case MACRO_TYPE_PREFIX_MARKER:
197
- if (known_type && !tag) return false;
198
- marker |= !known_type;
199
- body = tag;
200
- tag = false;
201
- break;
202
- case MACRO_TYPE_PREFIX_KNOWN_TYPE:
203
- case MACRO_TYPE_PREFIX_BALANCED_TYPE:
204
- case MACRO_TYPE_PREFIX_TAGGED_TYPE:
205
- if (!marker || (known_type && kind == MACRO_TYPE_PREFIX_BALANCED_TYPE)) return false;
206
- known_type = true;
207
- tag = body = kind == MACRO_TYPE_PREFIX_TAGGED_TYPE;
208
- break;
209
- }
188
+ uint8_t key = state.marker | (state.known_type << 1) | (state.tag << 2) | (state.body << 3);
189
+ unsigned cached = state.started ? macro_proof_step(memo, lexer, key) : 0;
190
+ if (cached) return cached == 2;
191
+ MacroTypeResult result = lexer->lookahead == '[' || lexer->lookahead == '{'
192
+ ? function_macro_type_group(lexer, &state)
193
+ : function_macro_type_word(lexer, symbols, reserved_words, reserved_count, &state);
194
+ if (result != MACRO_TYPE_CONTINUE) return result == MACRO_TYPE_ACCEPT;
195
+ }
196
+ }
197
+
198
+ static MacroTypeResult function_macro_type_group(TSLexer *lexer, MacroTypeState *state) {
199
+ if (lexer->lookahead == '[') {
200
+ if (!state->started) lexer->mark_end(lexer);
201
+ state->started = true;
202
+ return function_macro_bracket_attribute(lexer) ? MACRO_TYPE_CONTINUE : MACRO_TYPE_REJECT;
203
+ }
204
+ if (!state->body || !function_macro_modifier_balanced(lexer, '{', '}')) return MACRO_TYPE_REJECT;
205
+ state->body = state->tag = false;
206
+ return MACRO_TYPE_CONTINUE;
207
+ }
208
+
209
+ static MacroTypeResult function_macro_type_word(TSLexer *lexer, const MacroAttributeSymbols *symbols,
210
+ const char *const *reserved_words, unsigned reserved_count, MacroTypeState *state) {
211
+ char word[FUNCTION_MACRO_KEYWORD_MAX_LENGTH + 1] = {0};
212
+ MacroName name = {.word = word, .capacity = sizeof(word) - 1, .ascii = true, .lookahead_only = true, .preserve_end = state->started};
213
+ if (!scan_macro_name(lexer, &name)) return MACRO_TYPE_REJECT;
214
+ if (!state->started) {
215
+ bool handled;
216
+ bool accepted = function_macro_first_word(lexer, &name, symbols, &handled);
217
+ if (handled) return accepted ? MACRO_TYPE_ACCEPT : MACRO_TYPE_REJECT;
218
+ }
219
+ state->started = true;
220
+ MacroTypePrefix kind = function_macro_type_prefix_item(lexer, &name, reserved_words, reserved_count, !state->known_type);
221
+ return function_macro_type_transition(state, kind);
222
+ }
223
+
224
+ static MacroTypeResult function_macro_type_transition(MacroTypeState *state, MacroTypePrefix kind) {
225
+ switch (kind) {
226
+ case MACRO_TYPE_PREFIX_INVALID:
227
+ return MACRO_TYPE_REJECT;
228
+ case MACRO_TYPE_PREFIX_TYPE:
229
+ return state->marker ? MACRO_TYPE_ACCEPT : MACRO_TYPE_REJECT;
230
+ case MACRO_TYPE_PREFIX_MODIFIER:
231
+ return MACRO_TYPE_CONTINUE;
232
+ case MACRO_TYPE_PREFIX_MARKER:
233
+ if (state->known_type && !state->tag) return MACRO_TYPE_REJECT;
234
+ state->marker |= !state->known_type;
235
+ state->body = state->tag;
236
+ state->tag = false;
237
+ return MACRO_TYPE_CONTINUE;
238
+ case MACRO_TYPE_PREFIX_KNOWN_TYPE:
239
+ case MACRO_TYPE_PREFIX_BALANCED_TYPE:
240
+ case MACRO_TYPE_PREFIX_TAGGED_TYPE:
241
+ if (!state->marker || (state->known_type && kind == MACRO_TYPE_PREFIX_BALANCED_TYPE)) return MACRO_TYPE_REJECT;
242
+ state->known_type = true;
243
+ state->tag = state->body = kind == MACRO_TYPE_PREFIX_TAGGED_TYPE;
244
+ return MACRO_TYPE_CONTINUE;
210
245
  }
246
+ return MACRO_TYPE_REJECT;
211
247
  }
212
248
 
213
249
  static MacroTypePrefix function_macro_type_prefix_item(TSLexer *lexer, const MacroName *name, const char *const *reserved_words, unsigned reserved_count, bool type_argument) {
@@ -331,7 +367,6 @@ static MacroGroup function_macro_group_prefix(TSLexer *lexer, int32_t open, int3
331
367
  static MacroGroup function_macro_group_contents(TSLexer *lexer, int32_t open, int32_t close, bool *literal, bool *unterminated_raw, bool type_argument, bool modifier) {
332
368
  unsigned depth = 1;
333
369
  unsigned nested = 0;
334
- // A definition body's own brace permits its member semicolons.
335
370
  unsigned braces = open == '{';
336
371
  MacroArgumentWord word = {0};
337
372
  while (depth) {
@@ -0,0 +1,95 @@
1
+ #ifndef TREE_SITTER_C_MACRO_PROOF_H_
2
+ #define TREE_SITTER_C_MACRO_PROOF_H_
3
+
4
+ #include "tree_sitter/parser.h"
5
+ #include <stdlib.h>
6
+
7
+ typedef struct { void *children[256]; } MacroProofNode;
8
+ typedef struct { uint32_t values[256]; } MacroProofPage;
9
+ typedef struct { uint32_t byte; uint8_t state; } MacroProofKey;
10
+ typedef struct {
11
+ MacroProofNode root;
12
+ MacroProofKey *trace;
13
+ uint32_t length, capacity;
14
+ const char *const *reserved_words;
15
+ unsigned reserved_count;
16
+ bool bound;
17
+ } MacroProofMemo;
18
+
19
+ static MacroProofPage *macro_proof_page(MacroProofMemo *memo, uint32_t byte, bool create);
20
+ static void macro_proof_append(MacroProofMemo *memo, MacroProofKey key);
21
+ static void macro_proof_free_node(MacroProofNode *node, unsigned level);
22
+
23
+ static bool macro_proof_begin(MacroProofMemo *memo, const char *const *reserved_words, unsigned reserved_count) {
24
+ if (!memo) return false;
25
+ if (memo->bound && (memo->reserved_words != reserved_words || memo->reserved_count != reserved_count)) return false;
26
+ memo->bound = true;
27
+ memo->reserved_words = reserved_words;
28
+ memo->reserved_count = reserved_count;
29
+ memo->length = 0;
30
+ return true;
31
+ }
32
+
33
+ static unsigned macro_proof_step(MacroProofMemo *memo, TSLexer *lexer, uint8_t state) {
34
+ if (!memo) return 0;
35
+ TSLexerPosition position;
36
+ lexer->get_position(lexer, &position);
37
+ MacroProofPage *page = macro_proof_page(memo, position.bytes, false);
38
+ unsigned result = page ? (page->values[position.bytes & 255] >> (2 * state)) & 3 : 0;
39
+ if (!result) macro_proof_append(memo, (MacroProofKey){position.bytes, state});
40
+ return result;
41
+ }
42
+
43
+ static void macro_proof_finish(MacroProofMemo *memo, bool accepted) {
44
+ if (!memo) return;
45
+ for (uint32_t i = 0; i < memo->length; i++) {
46
+ MacroProofKey key = memo->trace[i];
47
+ MacroProofPage *page = macro_proof_page(memo, key.byte, true);
48
+ if (!page) continue;
49
+ unsigned shift = 2 * key.state;
50
+ uint32_t *value = &page->values[key.byte & 255];
51
+ *value = (*value & ~(3u << shift)) | ((accepted ? 2u : 1u) << shift);
52
+ }
53
+ }
54
+
55
+ static void macro_proof_destroy(MacroProofMemo *memo) {
56
+ if (!memo) return;
57
+ macro_proof_free_node(&memo->root, 3);
58
+ free(memo->trace);
59
+ }
60
+
61
+ static MacroProofPage *macro_proof_page(MacroProofMemo *memo, uint32_t byte, bool create) {
62
+ MacroProofNode *node = &memo->root;
63
+ for (unsigned level = 3; level > 0; level--) {
64
+ unsigned index = (byte >> (8 * level)) & 255;
65
+ if (!node->children[index] && create) {
66
+ node->children[index] = calloc(1, level == 1 ? sizeof(MacroProofPage) : sizeof(MacroProofNode));
67
+ }
68
+ if (!node->children[index]) return NULL;
69
+ if (level == 1) return (MacroProofPage *)node->children[index];
70
+ node = (MacroProofNode *)node->children[index];
71
+ }
72
+ return NULL;
73
+ }
74
+
75
+ static void macro_proof_append(MacroProofMemo *memo, MacroProofKey key) {
76
+ if (memo->length == memo->capacity) {
77
+ uint32_t next = memo->capacity ? memo->capacity * 2 : 64;
78
+ if (next < memo->capacity || (size_t)next > SIZE_MAX / sizeof(*memo->trace)) return;
79
+ void *trace = realloc(memo->trace, (size_t)next * sizeof(*memo->trace));
80
+ if (!trace) return;
81
+ memo->trace = (MacroProofKey *)trace;
82
+ memo->capacity = next;
83
+ }
84
+ memo->trace[memo->length++] = key;
85
+ }
86
+
87
+ static void macro_proof_free_node(MacroProofNode *node, unsigned level) {
88
+ for (unsigned i = 0; i < 256; i++) {
89
+ if (!node->children[i]) continue;
90
+ if (level > 1) macro_proof_free_node((MacroProofNode *)node->children[i], level - 1);
91
+ free(node->children[i]);
92
+ }
93
+ }
94
+
95
+ #endif
package/src/scanner.c CHANGED
@@ -4,13 +4,15 @@
4
4
  enum TokenType { PRAGMA_OPERATOR, PREPROC_ARG, PREPROC_NEWLINE, PREPROC_LPAREN, PREPROC_DIRECTIVE_ARG, PREPROC_FUNCTION_NAME, FUNCTION_ATTRIBUTE_START, POST_TYPE_FUNCTION_ATTRIBUTE_START, POST_TYPE_FUNCTION_ATTRIBUTE_SUFFIX, TRAILING_MACRO_ATTRIBUTE_NAME, TRAILING_OBJECT_DECLARATION_START, CONDITIONAL_MARKER_IFDEF, CONDITIONAL_MARKER_IFNDEF, LEADING_CONDITIONAL_MARKER_IFDEF, LEADING_CONDITIONAL_MARKER_IFNDEF, GNU_RAW_STRING_BODY, RAW_MACRO_TYPE_TAIL, BRACED_FUNCTION_ATTRIBUTE_START, BRACED_MACRO_TYPE_START };
5
5
 
6
6
  static bool scan_preproc_tokens(TSLexer *lexer, const bool *valid_symbols);
7
+ static bool scan_declaration_tokens(void *payload, TSLexer *lexer, const bool *valid_symbols);
7
8
 
8
9
  void *tree_sitter_c_external_scanner_create(void) {
9
- return NULL;
10
+ return calloc(1, sizeof(MacroProofMemo));
10
11
  }
11
12
 
12
13
  void tree_sitter_c_external_scanner_destroy(void *payload) {
13
- (void)payload;
14
+ macro_proof_destroy(payload);
15
+ free(payload);
14
16
  }
15
17
 
16
18
  unsigned tree_sitter_c_external_scanner_serialize(void *payload, char *buffer) {
@@ -26,7 +28,6 @@ void tree_sitter_c_external_scanner_deserialize(void *payload, const char *buffe
26
28
  }
27
29
 
28
30
  bool tree_sitter_c_external_scanner_scan(void *payload, TSLexer *lexer, const bool *valid_symbols) {
29
- (void)payload;
30
31
  if (valid_symbols[GNU_RAW_STRING_BODY] && !valid_symbols[PREPROC_LPAREN]) {
31
32
  lexer->result_symbol = GNU_RAW_STRING_BODY;
32
33
  return scan_gnu_raw_string_token(lexer);
@@ -45,14 +46,17 @@ bool tree_sitter_c_external_scanner_scan(void *payload, TSLexer *lexer, const bo
45
46
  }
46
47
  bool leading = valid_symbols[LEADING_CONDITIONAL_MARKER_IFDEF] || valid_symbols[LEADING_CONDITIONAL_MARKER_IFNDEF];
47
48
  bool conditional = leading || valid_symbols[CONDITIONAL_MARKER_IFDEF] || valid_symbols[CONDITIONAL_MARKER_IFNDEF];
48
- if (conditional) {
49
- while (pragma_space(lexer->lookahead)) lexer->advance(lexer, true);
50
- const ConditionalMacroSymbols symbols = {
51
- .ifdef = CONDITIONAL_MARKER_IFDEF, .ifndef = CONDITIONAL_MARKER_IFNDEF,
52
- .leading_ifdef = LEADING_CONDITIONAL_MARKER_IFDEF, .leading_ifndef = LEADING_CONDITIONAL_MARKER_IFNDEF,
53
- };
54
- if (lexer->lookahead == '#') return scan_conditional_macro(lexer, &symbols, valid_symbols);
55
- }
49
+ if (!conditional) return scan_declaration_tokens(payload, lexer, valid_symbols);
50
+ while (pragma_space(lexer->lookahead)) lexer->advance(lexer, true);
51
+ const ConditionalMacroSymbols symbols = {
52
+ .ifdef = CONDITIONAL_MARKER_IFDEF, .ifndef = CONDITIONAL_MARKER_IFNDEF,
53
+ .leading_ifdef = LEADING_CONDITIONAL_MARKER_IFDEF, .leading_ifndef = LEADING_CONDITIONAL_MARKER_IFNDEF,
54
+ };
55
+ if (lexer->lookahead == '#') return scan_conditional_macro(lexer, &symbols, valid_symbols);
56
+ return scan_declaration_tokens(payload, lexer, valid_symbols);
57
+ }
58
+
59
+ static bool scan_declaration_tokens(void *payload, TSLexer *lexer, const bool *valid_symbols) {
56
60
  static const TSSymbol attribute_tokens[] = {
57
61
  FUNCTION_ATTRIBUTE_START, POST_TYPE_FUNCTION_ATTRIBUTE_SUFFIX, POST_TYPE_FUNCTION_ATTRIBUTE_START,
58
62
  TRAILING_MACRO_ATTRIBUTE_NAME, TRAILING_OBJECT_DECLARATION_START, BRACED_FUNCTION_ATTRIBUTE_START,
@@ -64,6 +68,7 @@ bool tree_sitter_c_external_scanner_scan(void *payload, TSLexer *lexer, const bo
64
68
  .allow_object_start = valid_symbols[TRAILING_OBJECT_DECLARATION_START],
65
69
  .braced_start = BRACED_FUNCTION_ATTRIBUTE_START,
66
70
  .allow_braced_start = valid_symbols[BRACED_FUNCTION_ATTRIBUTE_START],
71
+ .proof_memo = payload,
67
72
  };
68
73
  if (valid_symbols[BRACED_MACRO_TYPE_START]) {
69
74
  lexer->result_symbol = BRACED_MACRO_TYPE_START;
Binary file
package/tree-sitter.json CHANGED
@@ -16,13 +16,14 @@
16
16
  "src/identifier.h",
17
17
  "src/keywords.h",
18
18
  "src/functionMacro.h",
19
+ "src/macroProof.h",
19
20
  "src/conditionalMacro.h",
20
21
  "src/gnuRawString.h"
21
22
  ]
22
23
  }
23
24
  ],
24
25
  "metadata": {
25
- "version": "23.0.0",
26
+ "version": "24.0.1",
26
27
  "license": "MIT",
27
28
  "description": "C grammar for tree-sitter",
28
29
  "authors": [