@willbooster/tree-sitter-c 24.0.1 → 26.0.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/README.md CHANGED
@@ -18,8 +18,7 @@ This fork fixes parsing bugs and raises conformance with the ISO C standard
18
18
  ## Support Scope
19
19
 
20
20
  This fork maintains only full parsing: valid input yields a correct tree, and invalid input yields `ERROR` or `MISSING`
21
- nodes without crashing, hanging, or taking more than linear time. Incremental parsing works as in upstream tree-sitter
22
- but is not tested here, and no queries (highlights, locals, injections, tags) are shipped. Node types, field names, and
21
+ nodes without crashing, hanging, or taking more than linear time. No queries (highlights, locals, injections, tags) are shipped. Node types, field names, and
23
22
  the trees that error recovery produces may change; such changes are released as new major versions.
24
23
 
25
24
  ## Usage
@@ -67,9 +66,14 @@ parser.setLanguage(await Language.load(c));
67
66
  The package also ships `grammar.js`, scanner sources under `src/`,
68
67
  and the node types in `src/node-types.json` for grammars extending C (such as C++).
69
68
  The scanner sources include `scanner.c`, `pragma.h`, `identifier.h`, `keywords.h`, `functionMacro.h`,
70
- `conditionalMacro.h`, `macroProof.h`, and `gnuRawString.h`.
69
+ `conditionalMacro.h`, `emptyDirectives.h`, `macroProof.h`, and `gnuRawString.h`.
71
70
 
72
71
  Derived grammars must port the inherited external-token dispatch from `src/scanner.c`.
72
+ Append `_directive_context` and `preproc_empty` in their inherited external-token slots and retain them as extras.
73
+ Port the scanner lifecycle functions together with `EmptyDirectives` from `emptyDirectives.h`; the source-boundary
74
+ facts belong to one full parse and are freed by scanner destruction. Run `scan_directive_context` first,
75
+ then reject external scans at `empty_directives_at_line_end` before the other token dispatch. Pass `empty_directives_skip` and its state through `ConditionalMacroSymbols.skip_empty` and
76
+ `empty_context` so declaration lookahead ignores null directives without moving the opening token's range.
73
77
  Keep preprocessor argument/newline handling ahead of annotation lookahead.
74
78
  Port `preproc_argument_mode` selection together with `scan_preproc_arg` so tokenized pragmas retain raw bodies.
75
79
  Append `_gnu_raw_string_body` and `_raw_macro_type_tail` in their inherited enum slots and port their dispatch from `src/scanner.c`.
@@ -83,6 +87,9 @@ The hidden `_function_attribute_start`, `_post_type_function_attribute_start`,
83
87
  `_post_type_function_attribute_suffix`, and `_braced_function_attribute_start` guards look ahead before the ordinary
84
88
  identifier token. Keep them outside the name field so identifier leaves and ordinary C lexing retain their shape.
85
89
  Port `scan_function_attribute_guard`, its `MacroAttributeSymbols` context, and the name-range handling in `pragma.h`.
90
+ Set `MacroAttributeSymbols.trivia` to `{empty_directives_skip, state}` so annotation, argument-group, and declaration-tail proofs skip the same null directives.
91
+ Pass that `PragmaTrivia` context to the raw macro-type tail and balanced-group helpers, and use `scan_pragma_with` for the pragma-operator fallback.
92
+ A derived scanner that omits this context cannot recognize null directives inside those lookahead paths.
86
93
  Set `braced_start` to the inherited braced token and `allow_braced_start` from its valid-symbol entry.
87
94
  Omitting these fields disables braced handoff in the current argument group and in prototype-tail lookahead.
88
95
  Preserve the inherited external-token order before adding tokens specific to the derived grammar.
@@ -97,8 +104,8 @@ when porting this dispatch. Later pragma lookahead must preserve the zero-width
97
104
 
98
105
  The C scanner owns a `MacroProofMemo` for each full parse and frees it on scanner destruction.
99
106
  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.
107
+ The first-word dispatch stays uncached because it depends on the valid external tokens. The memo binds to one reserved-word table and trivia callback/context;
108
+ a different table or trivia context disables reuse. Derived scanners can leave `proof_memo` unset, or own a memo with the same lifetime.
102
109
  These source facts need no serialized bytes.
103
110
 
104
111
  When overriding declarations or function definitions, retain both guarded ordinary-type/declarator alternatives and
@@ -196,7 +203,7 @@ arrays, parenthesized/function declarators, initializers and multiple declarator
196
203
 
197
204
  In Rust, depend on the [crate](https://crates.io/crates/willbooster-tree-sitter-c) and on
198
205
  [willbooster-tree-sitter](https://crates.io/crates/willbooster-tree-sitter), the runtime this package is tested and
199
- fuzzed with, at version 15.0.3 or later within 15.x, or 16.x. No other runtime loads the parser:
206
+ fuzzed with, at the version recorded in `Cargo.lock`:
200
207
 
201
208
  ```sh
202
209
  cargo add willbooster-tree-sitter@16.0.0 --rename tree-sitter
package/grammar.js CHANGED
@@ -61,7 +61,7 @@ const BINARY_OPERATORS = [
61
61
  ];
62
62
 
63
63
  const LINE_SPLICE = /\\(?:\r\n?|\n\r?)/;
64
- const LINE_COMMENT = seq('/', repeat(LINE_SPLICE), '/', /(\\+(.|\r?\n)|[^\\\n])*/);
64
+ const LINE_COMMENT = seq('/', repeat(LINE_SPLICE), '/', repeat(choice(/[^\\\r\n]/, '\\', LINE_SPLICE)));
65
65
  const COMMENT_SPLICES = repeat(seq('\\', /[\r\n]/));
66
66
  const COMMENT_ESCAPE = seq('\\', /[^\r\n*]/);
67
67
  const BLOCK_COMMENT_STARS = seq(
@@ -454,9 +454,17 @@ module.exports = Object.assign(
454
454
  sym('_raw_macro_type_tail'),
455
455
  sym('_braced_function_attribute_start'),
456
456
  sym('_braced_macro_type_start'),
457
+ sym('_directive_context'),
458
+ sym('preproc_empty'),
457
459
  ],
458
460
 
459
- extras: ($) => [$.pragma_operator, /\s|\\(?:\r\n?|\n\r?)/, $.comment],
461
+ extras: ($) => [
462
+ sym('_directive_context'),
463
+ sym('preproc_empty'),
464
+ $.pragma_operator,
465
+ /\s|\\(?:\r\n?|\n\r?)/,
466
+ $.comment,
467
+ ],
460
468
 
461
469
  inline: ($) => [
462
470
  $._bare_marker_initializer_declaration,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@willbooster/tree-sitter-c",
3
- "version": "24.0.1",
3
+ "version": "26.0.0",
4
4
  "description": "C grammar for tree-sitter",
5
5
  "keywords": [
6
6
  "c",
@@ -27,6 +27,7 @@
27
27
  "src/functionMacro.h",
28
28
  "src/macroProof.h",
29
29
  "src/conditionalMacro.h",
30
+ "src/emptyDirectives.h",
30
31
  "src/gnuRawString.h",
31
32
  "src/scanner.c"
32
33
  ],
@@ -9,21 +9,23 @@ typedef struct {
9
9
  // Excludes unknown type aliases and marker names; recognized C type/modifier prefixes are unchanged.
10
10
  const char *const *reserved_words;
11
11
  unsigned reserved_word_count;
12
+ bool (*skip_empty)(TSLexer *, void *);
13
+ void *empty_context;
12
14
  } ConditionalMacroSymbols;
13
15
 
14
16
  static bool conditional_macro_body(TSLexer *lexer, const ConditionalMacroSymbols *symbols, bool *escaped);
15
17
  static bool conditional_macro_word(TSLexer *lexer, MacroName *name, bool escaped);
16
18
  static bool conditional_macro_spacing(TSLexer *lexer, bool *separated, bool *escaped, bool directive);
17
19
  static bool conditional_macro_space(int32_t c, bool directive);
18
- static bool conditional_macro_next_line(TSLexer *lexer, bool *escaped);
20
+ static bool conditional_macro_next_line(TSLexer *lexer, const ConditionalMacroSymbols *symbols, bool *escaped);
19
21
  static bool conditional_macro_directive(TSLexer *lexer, MacroName *name);
20
22
  static bool conditional_macro_marker_line(TSLexer *lexer, const ConditionalMacroSymbols *symbols, bool *escaped);
21
23
  static bool conditional_macro_closing(TSLexer *lexer);
22
24
  static bool conditional_macro_type_prefix(TSLexer *lexer, const ConditionalMacroSymbols *symbols, bool escaped);
23
- static bool conditional_macro_skip_lines(TSLexer *lexer, bool *escaped);
25
+ static bool conditional_macro_skip_lines(TSLexer *lexer, const ConditionalMacroSymbols *symbols, bool *escaped);
24
26
  static bool conditional_macro_reserved_word(const ConditionalMacroSymbols *symbols, const char *word);
25
- static bool conditional_macro_modifier_group(TSLexer *lexer, const char *word, bool *escaped);
26
- static bool conditional_macro_attribute_declaration(TSLexer *lexer, bool *escaped);
27
+ static bool conditional_macro_modifier_group(TSLexer *lexer, const ConditionalMacroSymbols *symbols, const char *word, bool *escaped);
28
+ static bool conditional_macro_attribute_declaration(TSLexer *lexer, const ConditionalMacroSymbols *symbols, bool *escaped);
27
29
 
28
30
  static bool scan_conditional_macro(TSLexer *lexer, const ConditionalMacroSymbols *symbols, const bool *valid_symbols) {
29
31
  char directive[7] = {0};
@@ -52,9 +54,9 @@ static bool conditional_macro_body(TSLexer *lexer, const ConditionalMacroSymbols
52
54
  bool separated = false;
53
55
  MacroName condition = {.ascii = true};
54
56
  if (!conditional_macro_spacing(lexer, &separated, escaped, false) || !separated ||
55
- !conditional_macro_word(lexer, &condition, *escaped) || !conditional_macro_next_line(lexer, escaped)) return false;
57
+ !conditional_macro_word(lexer, &condition, *escaped) || !conditional_macro_next_line(lexer, symbols, escaped)) return false;
56
58
  if (!conditional_macro_marker_line(lexer, symbols, escaped) || !conditional_macro_closing(lexer)) return false;
57
- return conditional_macro_skip_lines(lexer, escaped) && !lexer->eof(lexer) && lexer->lookahead != '#';
59
+ return conditional_macro_skip_lines(lexer, symbols, escaped) && !lexer->eof(lexer) && lexer->lookahead != '#';
58
60
  }
59
61
 
60
62
  static bool conditional_macro_closing(TSLexer *lexer) {
@@ -92,17 +94,17 @@ static bool conditional_macro_type_prefix(TSLexer *lexer, const ConditionalMacro
92
94
 
93
95
  static ConditionalMacroPrefix conditional_macro_prefix(TSLexer *lexer, const ConditionalMacroSymbols *symbols, bool *escaped) {
94
96
  if (lexer->lookahead == '[') {
95
- return conditional_macro_attribute_declaration(lexer, escaped) ? CONDITIONAL_MODIFIER : CONDITIONAL_INVALID;
97
+ return conditional_macro_attribute_declaration(lexer, symbols, escaped) ? CONDITIONAL_MODIFIER : CONDITIONAL_INVALID;
96
98
  }
97
99
  char word[FUNCTION_MACRO_KEYWORD_MAX_LENGTH + 1] = {0};
98
100
  MacroName type = {.word = word, .capacity = sizeof(word) - 1, .ascii = true};
99
101
  if (!conditional_macro_word(lexer, &type, *escaped)) return CONDITIONAL_INVALID;
100
102
  ConditionalMacroPrefix kind = conditional_macro_prefix_kind(symbols, &type, word);
101
103
  if (kind == CONDITIONAL_TYPE) return kind;
102
- if (!conditional_macro_skip_lines(lexer, escaped)) return CONDITIONAL_INVALID;
104
+ if (!conditional_macro_skip_lines(lexer, symbols, escaped)) return CONDITIONAL_INVALID;
103
105
  if (kind == CONDITIONAL_TYPE_MODIFIER && lexer->lookahead == '(') return CONDITIONAL_TYPE;
104
106
  if (kind == CONDITIONAL_MODIFIER || kind == CONDITIONAL_TYPE_MODIFIER) {
105
- return conditional_macro_modifier_group(lexer, word, escaped) ? CONDITIONAL_MODIFIER : CONDITIONAL_INVALID;
107
+ return conditional_macro_modifier_group(lexer, symbols, word, escaped) ? CONDITIONAL_MODIFIER : CONDITIONAL_INVALID;
106
108
  }
107
109
  return kind;
108
110
  }
@@ -130,29 +132,30 @@ static bool conditional_macro_reserved_word(const ConditionalMacroSymbols *symbo
130
132
  return false;
131
133
  }
132
134
 
133
- static bool conditional_macro_modifier_group(TSLexer *lexer, const char *word, bool *escaped) {
135
+ static bool conditional_macro_modifier_group(TSLexer *lexer, const ConditionalMacroSymbols *symbols, const char *word, bool *escaped) {
134
136
  bool grouped = strcmp(word, pragma_word) == 0 || function_macro_word_in(word, function_macro_group_modifiers,
135
137
  sizeof(function_macro_group_modifiers) / sizeof(function_macro_group_modifiers[0]));
136
138
  if (!grouped || lexer->lookahead != '(') return true;
137
139
  bool literal = false;
138
- return function_macro_balanced(lexer, '(', ')', &literal, NULL) && conditional_macro_skip_lines(lexer, escaped);
140
+ return function_macro_balanced(lexer, '(', ')', &literal, NULL, &(PragmaTrivia){symbols->skip_empty, symbols->empty_context}) && conditional_macro_skip_lines(lexer, symbols, escaped);
139
141
  }
140
142
 
141
- static bool conditional_macro_attribute_declaration(TSLexer *lexer, bool *escaped) {
143
+ static bool conditional_macro_attribute_declaration(TSLexer *lexer, const ConditionalMacroSymbols *symbols, bool *escaped) {
142
144
  lexer->advance(lexer, false);
143
- if (!conditional_macro_skip_lines(lexer, escaped) || lexer->lookahead != '[') return false;
145
+ if (!conditional_macro_skip_lines(lexer, symbols, escaped) || lexer->lookahead != '[') return false;
144
146
  bool literal = false;
145
- if (!function_macro_balanced(lexer, '[', ']', &literal, NULL) ||
146
- !conditional_macro_skip_lines(lexer, escaped) || lexer->lookahead != ']') return false;
147
+ if (!function_macro_balanced(lexer, '[', ']', &literal, NULL, &(PragmaTrivia){symbols->skip_empty, symbols->empty_context}) ||
148
+ !conditional_macro_skip_lines(lexer, symbols, escaped) || lexer->lookahead != ']') return false;
147
149
  lexer->advance(lexer, false);
148
- return conditional_macro_skip_lines(lexer, escaped);
150
+ return conditional_macro_skip_lines(lexer, symbols, escaped);
149
151
  }
150
152
 
151
- static bool conditional_macro_skip_lines(TSLexer *lexer, bool *escaped) {
153
+ static bool conditional_macro_skip_lines(TSLexer *lexer, const ConditionalMacroSymbols *symbols, bool *escaped) {
152
154
  bool separated = false;
153
155
  for (;;) {
154
156
  *escaped = false;
155
157
  if (!conditional_macro_spacing(lexer, &separated, escaped, false)) return false;
158
+ if (symbols->skip_empty && symbols->skip_empty(lexer, symbols->empty_context)) continue;
156
159
  if (*escaped || !scan_preproc_newline(lexer, false)) return true;
157
160
  }
158
161
  }
@@ -162,7 +165,7 @@ static bool conditional_macro_marker_line(TSLexer *lexer, const ConditionalMacro
162
165
  MacroName marker = {.word = word, .capacity = sizeof(word) - 1, .ascii = true};
163
166
  if (!conditional_macro_word(lexer, &marker, *escaped) ||
164
167
  (marker.ascii && (function_macro_keyword(word) || conditional_macro_reserved_word(symbols, word)))) return false;
165
- return conditional_macro_next_line(lexer, escaped);
168
+ return conditional_macro_next_line(lexer, symbols, escaped);
166
169
  }
167
170
 
168
171
  static bool conditional_macro_directive(TSLexer *lexer, MacroName *name) {
@@ -174,13 +177,14 @@ static bool conditional_macro_directive(TSLexer *lexer, MacroName *name) {
174
177
  conditional_macro_word(lexer, name, false);
175
178
  }
176
179
 
177
- static bool conditional_macro_next_line(TSLexer *lexer, bool *escaped) {
180
+ static bool conditional_macro_next_line(TSLexer *lexer, const ConditionalMacroSymbols *symbols, bool *escaped) {
178
181
  bool separated = false;
179
182
  *escaped = false;
180
183
  if (!conditional_macro_spacing(lexer, &separated, escaped, false) || *escaped ||
181
184
  !scan_preproc_newline(lexer, false)) return false;
182
185
  for (;;) {
183
186
  if (!conditional_macro_spacing(lexer, &separated, escaped, false) || lexer->eof(lexer)) return false;
187
+ if (symbols->skip_empty && symbols->skip_empty(lexer, symbols->empty_context)) continue;
184
188
  if (*escaped || !scan_preproc_newline(lexer, false)) return true;
185
189
  }
186
190
  }
@@ -0,0 +1,439 @@
1
+ #ifndef TREE_SITTER_C_EMPTY_DIRECTIVES_H_
2
+ #define TREE_SITTER_C_EMPTY_DIRECTIVES_H_
3
+
4
+ #include "pragma.h"
5
+ #include <stdlib.h>
6
+
7
+ typedef struct {
8
+ int32_t value;
9
+ uint32_t byte;
10
+ } DirectiveCharacter;
11
+
12
+ typedef struct {
13
+ DirectiveCharacter *characters;
14
+ uint8_t *prefix_barriers;
15
+ uint32_t length, index, end;
16
+ } DirectiveInput;
17
+
18
+ typedef struct {
19
+ uint8_t *starts, *ends, *line_ends;
20
+ uint32_t bytes;
21
+ bool built, ready;
22
+ } EmptyDirectives;
23
+
24
+ typedef enum { DIRECTIVE_ORDINARY, DIRECTIVE_CONDITION, DIRECTIVE_DIAGNOSTIC, DIRECTIVE_REQUIRED, DIRECTIVE_PRAGMA, DIRECTIVE_HEADER } DirectiveKind;
25
+
26
+ typedef struct {
27
+ bool beginning, pending, operand, pragma_pending, pragma_newline;
28
+ unsigned pragma_depth, header;
29
+ DirectiveKind kind;
30
+ uint32_t start;
31
+ } DirectiveLine;
32
+
33
+ static void directive_prefixes(EmptyDirectives *state, DirectiveInput *input);
34
+ static bool directive_append(DirectiveInput *input, uint32_t *capacity, TSLexer *lexer);
35
+ static void directive_line_end(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line);
36
+ static void directive_line_character(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line, int32_t c);
37
+ static void directive_code(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line, int32_t c);
38
+ static bool directive_pragma_part(DirectiveLine *line, int32_t c);
39
+ static bool directive_header_ready(DirectiveLine *line, int32_t c);
40
+ static bool directive_header(DirectiveInput *input, int32_t close);
41
+ static void directive_line_comment(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line);
42
+ static uint32_t directive_splice_end(const DirectiveInput *input);
43
+
44
+ static bool empty_directives_build(EmptyDirectives *state, TSLexer *lexer);
45
+ static void empty_directives_find(EmptyDirectives *state, DirectiveInput *input);
46
+ static int32_t directive_next(DirectiveInput *input);
47
+ static int32_t directive_peek(const DirectiveInput *input);
48
+ static bool directive_comment(DirectiveInput *input, bool *line);
49
+ static bool directive_quoted(DirectiveInput *input, int32_t quote);
50
+ static bool directive_raw(DirectiveInput *input);
51
+ typedef struct {
52
+ GnuRawStringPrefix raw;
53
+ char directive[13];
54
+ unsigned length;
55
+ } DirectiveWord;
56
+
57
+ static DirectiveKind directive_word(DirectiveInput *input, int32_t first, bool raw);
58
+ static DirectiveKind directive_word_kind(const DirectiveWord *word);
59
+ static void directive_word_character(DirectiveWord *word, int32_t c);
60
+ static void directive_word_role(DirectiveLine *line, DirectiveKind kind, bool name, bool pragma, bool had_operand, bool identifier);
61
+ static bool directive_horizontal(int32_t c);
62
+ static bool directive_newline(int32_t c);
63
+ static uint32_t directive_byte(const DirectiveInput *input);
64
+ static void directive_record(EmptyDirectives *state, uint32_t start, uint32_t end);
65
+
66
+ static bool empty_directives_initialize(EmptyDirectives *state, TSLexer *lexer) {
67
+ TSLexerCheckpoint start;
68
+ lexer->get_checkpoint(lexer, &start);
69
+ if (!state->built) {
70
+ state->built = true;
71
+ empty_directives_build(state, lexer);
72
+ }
73
+ state->ready = true;
74
+ lexer->mark_end_at(lexer, &start);
75
+ return true;
76
+ }
77
+
78
+ static bool empty_directives_at_line_end(const EmptyDirectives *state, TSLexer *lexer) {
79
+ if (!state || !state->line_ends) return false;
80
+ TSLexerPosition position;
81
+ lexer->get_position(lexer, &position);
82
+ return position.bytes < state->bytes && (state->line_ends[position.bytes / 8] & (1u << (position.bytes % 8)));
83
+ }
84
+
85
+ static bool empty_directives_consume(TSLexer *lexer, EmptyDirectives *state, bool skip_prefix);
86
+
87
+ static bool empty_directives_skip(TSLexer *lexer, void *payload) {
88
+ return empty_directives_consume(lexer, payload, false);
89
+ }
90
+
91
+ static bool empty_directives_consume(TSLexer *lexer, EmptyDirectives *state, bool skip_prefix) {
92
+ if (!state || !state->starts) return false;
93
+ TSLexerPosition position;
94
+ lexer->get_position(lexer, &position);
95
+ if (position.bytes >= state->bytes || !(state->starts[position.bytes / 8] & (1u << (position.bytes % 8)))) return false;
96
+ while (lexer->lookahead != '#' && !lexer->eof(lexer)) lexer->advance(lexer, skip_prefix);
97
+ do {
98
+ lexer->advance(lexer, false);
99
+ lexer->get_position(lexer, &position);
100
+ } while (!lexer->eof(lexer) && position.bytes < state->bytes &&
101
+ !(state->ends[position.bytes / 8] & (1u << (position.bytes % 8))));
102
+ return true;
103
+ }
104
+
105
+ static bool empty_directives_build(EmptyDirectives *state, TSLexer *lexer) {
106
+ DirectiveInput input = {0};
107
+ uint32_t capacity = 0;
108
+ while (!lexer->eof(lexer)) {
109
+ if (!directive_append(&input, &capacity, lexer)) goto failed;
110
+ lexer->advance(lexer, false);
111
+ }
112
+ TSLexerPosition end;
113
+ lexer->get_position(lexer, &end);
114
+ input.end = end.bytes;
115
+ state->bytes = end.bytes;
116
+ state->starts = calloc((size_t)state->bytes / 8 + 1, 1);
117
+ state->ends = calloc((size_t)state->bytes / 8 + 1, 1);
118
+ input.prefix_barriers = calloc((size_t)state->bytes / 8 + 1, 1);
119
+ if (!state->starts || !state->ends || !input.prefix_barriers) goto failed;
120
+ empty_directives_find(state, &input);
121
+ directive_prefixes(state, &input);
122
+ state->line_ends = input.prefix_barriers;
123
+ free(input.characters);
124
+ return true;
125
+ failed:
126
+ free(input.prefix_barriers);
127
+ free(input.characters);
128
+ free(state->starts);
129
+ free(state->ends);
130
+ state->starts = state->ends = NULL;
131
+ return false;
132
+ }
133
+
134
+ static void directive_prefixes(EmptyDirectives *state, DirectiveInput *input) {
135
+ bool prefix = false, line_end = false;
136
+ for (uint32_t i = input->length; i > 0; i--) {
137
+ DirectiveCharacter character = input->characters[i - 1];
138
+ bool hash = character.value == '#' && (state->starts[character.byte / 8] & (1u << (character.byte % 8)));
139
+ bool condition_end = input->prefix_barriers[character.byte / 8] & (1u << (character.byte % 8));
140
+ line_end = condition_end || (line_end && directive_horizontal(character.value));
141
+ if (line_end) input->prefix_barriers[character.byte / 8] |= 1u << (character.byte % 8);
142
+ prefix = !condition_end && (hash || (prefix && (pragma_space(character.value) || character.value == '\\')));
143
+ if (prefix) state->starts[character.byte / 8] |= 1u << (character.byte % 8);
144
+ }
145
+ }
146
+
147
+ static bool directive_append(DirectiveInput *input, uint32_t *capacity, TSLexer *lexer) {
148
+ if (input->length == *capacity) {
149
+ uint32_t next = *capacity ? *capacity * 2 : 1024;
150
+ if (next < *capacity || (size_t)next > SIZE_MAX / sizeof(*input->characters)) return false;
151
+ void *characters = realloc(input->characters, (size_t)next * sizeof(*input->characters));
152
+ if (!characters) return false;
153
+ input->characters = characters;
154
+ *capacity = next;
155
+ }
156
+ TSLexerPosition position;
157
+ lexer->get_position(lexer, &position);
158
+ input->characters[input->length++] = (DirectiveCharacter){lexer->lookahead, position.bytes};
159
+ return true;
160
+ }
161
+
162
+ static void empty_directives_find(EmptyDirectives *state, DirectiveInput *input) {
163
+ DirectiveLine line = {.beginning = true};
164
+ while (input->index < input->length) {
165
+ int32_t c = directive_next(input);
166
+ if (c < 0) break;
167
+ directive_line_character(state, input, &line, c);
168
+ }
169
+ if (line.pending) directive_record(state, line.start, input->end);
170
+ }
171
+
172
+ static void directive_line_character(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line, int32_t c) {
173
+ if (c == '\n') {
174
+ directive_line_end(state, input, line);
175
+ return;
176
+ }
177
+ if (c == '/' && (directive_peek(input) == '/' || directive_peek(input) == '*')) {
178
+ directive_line_comment(state, input, line);
179
+ return;
180
+ }
181
+ if (directive_horizontal(c) || (line->beginning && pragma_space(c))) return;
182
+ if (c == '#' && line->beginning) {
183
+ line->pragma_pending = line->pragma_newline = false;
184
+ line->pragma_depth = 0;
185
+ line->kind = DIRECTIVE_ORDINARY;
186
+ line->operand = false;
187
+ line->start = input->characters[input->index - 1].byte;
188
+ line->beginning = false;
189
+ line->pending = true;
190
+ return;
191
+ }
192
+ directive_code(state, input, line, c);
193
+ }
194
+
195
+ static void directive_code(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line, int32_t c) {
196
+ bool name = line->pending;
197
+ bool header = directive_header_ready(line, c);
198
+ bool pragma = directive_pragma_part(line, c);
199
+ bool operand = line->operand;
200
+ if (!name && !pragma) line->operand = true;
201
+ line->beginning = line->pending = false;
202
+ if (header) {
203
+ if (directive_header(input, c == '<' ? '>' : '"')) directive_line_end(state, input, line);
204
+ } else if (c == '"' || c == '\'') {
205
+ if (directive_quoted(input, c)) directive_line_end(state, input, line);
206
+ } else if (preproc_word(c, false) || (c >= '0' && c <= '9')) {
207
+ DirectiveKind kind = directive_word(input, c, line->kind != DIRECTIVE_DIAGNOSTIC);
208
+ directive_word_role(line, kind, name, pragma, operand, preproc_word(c, false));
209
+ }
210
+ }
211
+
212
+ static void directive_word_role(DirectiveLine *line, DirectiveKind kind, bool name, bool pragma, bool had_operand, bool identifier) {
213
+ if (name) {
214
+ line->kind = kind;
215
+ return;
216
+ }
217
+ if (pragma) return;
218
+ if (kind == DIRECTIVE_PRAGMA) {
219
+ bool required = line->kind == DIRECTIVE_REQUIRED || line->kind == DIRECTIVE_HEADER;
220
+ if (required && !had_operand) {
221
+ line->operand = false;
222
+ line->pragma_pending = true;
223
+ }
224
+ return;
225
+ }
226
+ if (identifier && line->kind == DIRECTIVE_CONDITION) line->header = 1;
227
+ }
228
+
229
+ static bool directive_header_ready(DirectiveLine *line, int32_t c) {
230
+ if (line->header == 1) {
231
+ line->header = c == '(' ? 2 : 0;
232
+ return false;
233
+ }
234
+ bool argument = line->header == 2;
235
+ line->header = 0;
236
+ return (argument || (line->kind == DIRECTIVE_HEADER && !line->operand)) && (c == '<' || c == '"');
237
+ }
238
+
239
+ static bool directive_header(DirectiveInput *input, int32_t close) {
240
+ while (input->index < input->length) {
241
+ int32_t c = directive_next(input);
242
+ if (c == close) return false;
243
+ if (c == '\n') return true;
244
+ }
245
+ return false;
246
+ }
247
+
248
+ static bool directive_pragma_part(DirectiveLine *line, int32_t c) {
249
+ if (line->pragma_depth) {
250
+ if (c == '(') line->pragma_depth++;
251
+ if (c == ')') line->pragma_depth--;
252
+ return true;
253
+ }
254
+ if (!line->pragma_pending) return false;
255
+ line->pragma_pending = false;
256
+ if (c == '(') {
257
+ line->pragma_depth = 1;
258
+ line->pragma_newline = false;
259
+ return true;
260
+ }
261
+ if (line->pragma_newline) line->kind = DIRECTIVE_ORDINARY;
262
+ line->pragma_newline = false;
263
+ line->operand = true;
264
+ return false;
265
+ }
266
+
267
+ static void directive_line_comment(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line) {
268
+ bool single = false;
269
+ if (!directive_comment(input, &single)) line->pending = false;
270
+ if (single) directive_line_end(state, input, line);
271
+ }
272
+
273
+ static void directive_line_end(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line) {
274
+ if (line->pending) directive_record(state, line->start, directive_byte(input));
275
+ line->beginning = true;
276
+ line->pending = false;
277
+ bool required = line->kind == DIRECTIVE_CONDITION ||
278
+ ((line->kind == DIRECTIVE_REQUIRED || line->kind == DIRECTIVE_HEADER) && !line->operand && !line->pragma_pending);
279
+ if (required && input->index &&
280
+ directive_newline(input->characters[input->index - 1].value)) {
281
+ uint32_t byte = input->characters[input->index - 1].byte;
282
+ input->prefix_barriers[byte / 8] |= 1u << (byte % 8);
283
+ if (input->index > 1) {
284
+ DirectiveCharacter previous = input->characters[input->index - 2];
285
+ if (directive_newline(previous.value) && previous.value != input->characters[input->index - 1].value) {
286
+ input->prefix_barriers[previous.byte / 8] |= 1u << (previous.byte % 8);
287
+ }
288
+ }
289
+ }
290
+ line->header = 0;
291
+ if (line->pragma_pending) line->pragma_newline = true;
292
+ if (!line->pragma_depth && !line->pragma_pending) {
293
+ line->kind = DIRECTIVE_ORDINARY;
294
+ line->operand = false;
295
+ }
296
+ }
297
+
298
+ static int32_t directive_next(DirectiveInput *input) {
299
+ while (input->index < input->length) {
300
+ int32_t c = input->characters[input->index++].value;
301
+ if (c == '\\') {
302
+ uint32_t after = directive_splice_end(input);
303
+ if (after != input->index) {
304
+ input->index = after;
305
+ continue;
306
+ }
307
+ }
308
+ if (directive_newline(c)) {
309
+ if (input->index < input->length && directive_newline(input->characters[input->index].value) &&
310
+ input->characters[input->index].value != c) input->index++;
311
+ return '\n';
312
+ }
313
+ return c;
314
+ }
315
+ return -1;
316
+ }
317
+
318
+ static uint32_t directive_splice_end(const DirectiveInput *input) {
319
+ uint32_t after = input->index;
320
+ while (after < input->length && directive_horizontal(input->characters[after].value)) after++;
321
+ if (after == input->length || !directive_newline(input->characters[after].value)) return input->index;
322
+ int32_t newline = input->characters[after++].value;
323
+ if (after < input->length && directive_newline(input->characters[after].value) &&
324
+ input->characters[after].value != newline) after++;
325
+ return after;
326
+ }
327
+
328
+ static int32_t directive_peek(const DirectiveInput *input) {
329
+ DirectiveInput copy = *input;
330
+ return directive_next(&copy);
331
+ }
332
+
333
+ static bool directive_comment(DirectiveInput *input, bool *line) {
334
+ *line = directive_next(input) == '/';
335
+ int32_t previous = 0;
336
+ while (input->index < input->length) {
337
+ int32_t c = directive_next(input);
338
+ if (*line && c == '\n') return true;
339
+ if (!*line && previous == '*' && c == '/') return true;
340
+ previous = c;
341
+ }
342
+ return *line;
343
+ }
344
+
345
+ static bool directive_quoted(DirectiveInput *input, int32_t quote) {
346
+ while (input->index < input->length) {
347
+ int32_t c = directive_next(input);
348
+ if (c == quote) return false;
349
+ if (c == '\\') c = directive_next(input);
350
+ if (c == '\n') return true;
351
+ if (c < 0) return false;
352
+ }
353
+ return false;
354
+ }
355
+
356
+ static DirectiveKind directive_word(DirectiveInput *input, int32_t first, bool raw) {
357
+ DirectiveWord word = {0};
358
+ directive_word_character(&word, first);
359
+ bool number = first >= '0' && first <= '9';
360
+ int32_t previous = first;
361
+ for (;;) {
362
+ int32_t c = directive_peek(input);
363
+ bool exponent_sign = number && (c == '+' || c == '-') &&
364
+ (previous == 'e' || previous == 'E' || previous == 'p' || previous == 'P');
365
+ if (!preproc_word(c, true) && !(number && (c == '.' || c == '\'' || exponent_sign))) break;
366
+ previous = directive_next(input);
367
+ directive_word_character(&word, previous);
368
+ }
369
+ if (raw && gnu_raw_string_prefix(&word.raw) && directive_peek(input) == '"') directive_raw(input);
370
+ return directive_word_kind(&word);
371
+ }
372
+
373
+ static DirectiveKind directive_word_kind(const DirectiveWord *word) {
374
+ if (word->length >= sizeof word->directive) return DIRECTIVE_ORDINARY;
375
+ if (strcmp(word->directive, "if") == 0 || strcmp(word->directive, "elif") == 0) return DIRECTIVE_CONDITION;
376
+ if (strcmp(word->directive, pragma_word) == 0) return DIRECTIVE_PRAGMA;
377
+ if (strcmp(word->directive, "include") == 0 || strcmp(word->directive, "include_next") == 0 ||
378
+ strcmp(word->directive, "embed") == 0) return DIRECTIVE_HEADER;
379
+ if (strcmp(word->directive, "warning") == 0 || strcmp(word->directive, "error") == 0) return DIRECTIVE_DIAGNOSTIC;
380
+ static const char *const required[] = {
381
+ "define", "ifdef", "ifndef", "elifdef", "elifndef", "undef", "line",
382
+ };
383
+ for (unsigned i = 0; i < sizeof required / sizeof *required; i++) {
384
+ if (strcmp(word->directive, required[i]) == 0) return DIRECTIVE_REQUIRED;
385
+ }
386
+ return DIRECTIVE_ORDINARY;
387
+ }
388
+
389
+ static void directive_word_character(DirectiveWord *word, int32_t c) {
390
+ gnu_raw_string_prefix_character(&word->raw, c);
391
+ if (word->length < sizeof word->directive - 1 && c > 0 && c < 128) {
392
+ word->directive[word->length++] = (char)c;
393
+ } else {
394
+ word->length = sizeof word->directive;
395
+ }
396
+ }
397
+
398
+ static bool directive_raw(DirectiveInput *input) {
399
+ DirectiveInput candidate = *input;
400
+ directive_next(&candidate);
401
+ int32_t delimiter[16];
402
+ unsigned length = 0;
403
+ int32_t c;
404
+ while ((c = directive_next(&candidate)) != '(') {
405
+ if (length == 16 || c < 0x21 || c > 0x7e || c == ')' || c == '\\') return false;
406
+ delimiter[length++] = c;
407
+ }
408
+ while (candidate.index < candidate.length) {
409
+ uint32_t index = candidate.index++;
410
+ if (candidate.characters[index].value != ')' || candidate.length - index < length + 2) continue;
411
+ unsigned matched = 0;
412
+ while (matched < length && candidate.characters[index + 1 + matched].value == delimiter[matched]) matched++;
413
+ if (matched == length && candidate.characters[index + 1 + length].value == '"') {
414
+ candidate.index = index + length + 2;
415
+ break;
416
+ }
417
+ }
418
+ *input = candidate;
419
+ return true;
420
+ }
421
+
422
+ static bool directive_horizontal(int32_t c) {
423
+ return c == ' ' || c == '\t' || c == '\v' || c == '\f';
424
+ }
425
+
426
+ static bool directive_newline(int32_t c) {
427
+ return c == '\n' || c == '\r';
428
+ }
429
+
430
+ static uint32_t directive_byte(const DirectiveInput *input) {
431
+ return input->index < input->length ? input->characters[input->index].byte : input->end;
432
+ }
433
+
434
+ static void directive_record(EmptyDirectives *state, uint32_t start, uint32_t end) {
435
+ state->starts[start / 8] |= 1u << (start % 8);
436
+ state->ends[end / 8] |= 1u << (end % 8);
437
+ }
438
+
439
+ #endif