@willbooster/tree-sitter-c 24.0.0 → 25.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 +20 -7
- package/grammar.js +9 -1
- package/package.json +3 -1
- package/src/conditionalMacro.h +23 -19
- package/src/emptyDirectives.h +439 -0
- package/src/functionMacro.h +159 -100
- package/src/macroProof.h +100 -0
- package/src/node-types.json +5 -0
- package/src/pragma.h +33 -5
- package/src/scanner.c +73 -23
- package/tree-sitter-c.wasm +0 -0
- package/tree-sitter.json +3 -1
|
@@ -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(©);
|
|
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
|