@willbooster/tree-sitter-c 33.0.0 → 34.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/grammar.js +155 -23
- package/package.json +1 -1
- package/src/emptyDirectives.h +180 -34
- package/src/identifier.h +3 -0
- package/src/node-types.json +4 -0
- package/src/pragma.h +11 -5
- package/tree-sitter-c.wasm +0 -0
- package/tree-sitter.json +1 -1
package/grammar.js
CHANGED
|
@@ -462,6 +462,16 @@ module.exports = Object.assign(
|
|
|
462
462
|
[$.parameter_list, $._old_style_parameter_list],
|
|
463
463
|
[$.function_declarator, $._function_declaration_declarator],
|
|
464
464
|
[$.function_declarator, $._parameter_function_declarator],
|
|
465
|
+
[$._parameter_macro_argument_list],
|
|
466
|
+
[$._later_macro_call_expression, $.macro_specifier],
|
|
467
|
+
[$._later_macro_call_expression, $._macro_case_macro_specifier],
|
|
468
|
+
[$._macro_case_attributed_declarator],
|
|
469
|
+
[$._macro_case_parameter_declarator, $._macro_case_attributed_declarator],
|
|
470
|
+
[$._declarator, $._later_macro_call_expression, $.macro_specifier],
|
|
471
|
+
[$._macro_case_macro_specifier, $._macro_case_unattributed_declarator],
|
|
472
|
+
[$._later_macro_call_expression, $._macro_case_macro_specifier, $._macro_case_unattributed_declarator],
|
|
473
|
+
[$._parameter_pointer_declarator, $.pointer_declarator],
|
|
474
|
+
[$._declarator, $._leading_macro_call_expression, $.macro_specifier],
|
|
465
475
|
[$._parameter_pointer_declarator, $.pointer_declarator, $.abstract_pointer_declarator],
|
|
466
476
|
[$._parameter_declarator, $.macro_specifier],
|
|
467
477
|
[$.pointer_declarator],
|
|
@@ -1134,6 +1144,14 @@ module.exports = Object.assign(
|
|
|
1134
1144
|
seq($._declaration_specifiers, field('declarator', $._declarator), repeat($.attribute_specifier)),
|
|
1135
1145
|
|
|
1136
1146
|
_macro_attribute_argument_list: ($) => macroAttributeArgumentList(macroAttributeArgument($)),
|
|
1147
|
+
// A list with a type ranks two below one without, once however many types it has, so a function declarator
|
|
1148
|
+
// reading of the same parentheses outranks a trailing annotation only with a type: `int *CC FOO(int)` declares a
|
|
1149
|
+
// function, while `char *buf __counted_by(len)` is annotated.
|
|
1150
|
+
_parameter_macro_argument_list: ($) =>
|
|
1151
|
+
choice(
|
|
1152
|
+
macroAttributeArgumentList(macroAttributeArgument($, false)),
|
|
1153
|
+
prec.dynamic(-2, macroAttributeArgumentList(macroAttributeArgument($, $.type_descriptor)))
|
|
1154
|
+
),
|
|
1137
1155
|
_braced_macro_attribute_argument_list: ($) =>
|
|
1138
1156
|
macroAttributeArgumentList(
|
|
1139
1157
|
choice(macroAttributeArgument($), alias(sym('_macro_attribute_parameter'), $.parameter_declaration))
|
|
@@ -1192,8 +1210,8 @@ module.exports = Object.assign(
|
|
|
1192
1210
|
|
|
1193
1211
|
_macro_declaration_declarator: ($) => declarationDeclaratorRule($, $.function_declarator),
|
|
1194
1212
|
|
|
1195
|
-
// A
|
|
1196
|
-
//
|
|
1213
|
+
// A function declarator under pointers, as in `int *callback(int) ATTR(1)`, leaves the annotations after it to
|
|
1214
|
+
// the parameter, unless its name and parentheses read as a leading call marker (see `_parameter_pointer_declarator`).
|
|
1197
1215
|
_parameter_declarator: ($) =>
|
|
1198
1216
|
choice(
|
|
1199
1217
|
$.attributed_declarator,
|
|
@@ -1205,7 +1223,87 @@ module.exports = Object.assign(
|
|
|
1205
1223
|
alias(VA_ARG_KEYWORDS, $.identifier)
|
|
1206
1224
|
),
|
|
1207
1225
|
|
|
1208
|
-
|
|
1226
|
+
// After a first leading marker, a macro-cased name lexes as `_macro_case_identifier`, since the marker may also
|
|
1227
|
+
// be the declarator and the name a trailing annotation, so later markers and the pointee may be named by it too,
|
|
1228
|
+
// as `FOO` in `int *CC FOO[3]`. It is not valid right after `*`, where it would take the operand of an
|
|
1229
|
+
// expression reading such as `foo(width * HEIGHT)`.
|
|
1230
|
+
_parameter_pointer_declarator: ($) => {
|
|
1231
|
+
const modifier = pointerDeclaratorModifier($);
|
|
1232
|
+
return prec.right(
|
|
1233
|
+
seq(
|
|
1234
|
+
...pointerDeclaratorHead($),
|
|
1235
|
+
repeat(modifier),
|
|
1236
|
+
choice(
|
|
1237
|
+
field('declarator', $._parameter_declarator),
|
|
1238
|
+
seq(
|
|
1239
|
+
choice($.macro_specifier, alias($._leading_macro_call_expression, $.call_expression)),
|
|
1240
|
+
repeat(
|
|
1241
|
+
choice(
|
|
1242
|
+
modifier,
|
|
1243
|
+
$.macro_specifier,
|
|
1244
|
+
alias($._macro_case_macro_specifier, $.macro_specifier),
|
|
1245
|
+
alias($._later_macro_call_expression, $.call_expression)
|
|
1246
|
+
)
|
|
1247
|
+
),
|
|
1248
|
+
field('declarator', choice($._parameter_declarator, $._macro_case_parameter_declarator))
|
|
1249
|
+
)
|
|
1250
|
+
)
|
|
1251
|
+
)
|
|
1252
|
+
);
|
|
1253
|
+
},
|
|
1254
|
+
|
|
1255
|
+
// A first leading marker with arguments, as in `const void *__sized_by(n) buffer`, outranks the reading of its
|
|
1256
|
+
// name and arguments as a function declarator whose following name is a trailing annotation (-1), whatever the
|
|
1257
|
+
// arguments are; system headers write this marker, while a function-typed parameter returning a pointer and
|
|
1258
|
+
// annotated after its parameter list is rare.
|
|
1259
|
+
_leading_macro_call_expression: ($) => leadingMacroCallRule($, 2, $.identifier),
|
|
1260
|
+
|
|
1261
|
+
// A later one, as `FOO(int)` in `int *CC FOO(int) UNUSED`, ranks below that reading.
|
|
1262
|
+
_later_macro_call_expression: ($) =>
|
|
1263
|
+
leadingMacroCallRule($, -2, choice($.identifier, alias($._macro_case_identifier, $.identifier))),
|
|
1264
|
+
|
|
1265
|
+
_macro_case_macro_specifier: ($) =>
|
|
1266
|
+
prec.dynamic(-2, field('name', alias($._macro_case_identifier, $.identifier))),
|
|
1267
|
+
|
|
1268
|
+
// The declarator forms that start with a name, split as `_declarator` is.
|
|
1269
|
+
_macro_case_parameter_declarator: ($) =>
|
|
1270
|
+
choice(
|
|
1271
|
+
alias($._macro_case_attributed_declarator, $.attributed_declarator),
|
|
1272
|
+
$._macro_case_unattributed_declarator
|
|
1273
|
+
),
|
|
1274
|
+
|
|
1275
|
+
_macro_case_unattributed_declarator: ($) =>
|
|
1276
|
+
choice(
|
|
1277
|
+
alias($._macro_case_identifier, $.identifier),
|
|
1278
|
+
alias($._macro_case_array_declarator, $.array_declarator),
|
|
1279
|
+
alias($._macro_case_function_declarator, $.function_declarator)
|
|
1280
|
+
),
|
|
1281
|
+
|
|
1282
|
+
_macro_case_attributed_declarator: ($) =>
|
|
1283
|
+
seq($._macro_case_unattributed_declarator, repeat1($.attribute_declaration)),
|
|
1284
|
+
|
|
1285
|
+
_macro_case_array_declarator: ($) =>
|
|
1286
|
+
prec(
|
|
1287
|
+
1,
|
|
1288
|
+
seq(
|
|
1289
|
+
field('declarator', $._macro_case_parameter_declarator),
|
|
1290
|
+
'[',
|
|
1291
|
+
repeat(choice($.type_qualifier, 'static')),
|
|
1292
|
+
field('size', optional(choice($.expression, '*'))),
|
|
1293
|
+
']'
|
|
1294
|
+
)
|
|
1295
|
+
),
|
|
1296
|
+
|
|
1297
|
+
_macro_case_function_declarator: ($) =>
|
|
1298
|
+
prec.right(
|
|
1299
|
+
2,
|
|
1300
|
+
seq(
|
|
1301
|
+
field('declarator', $._macro_case_parameter_declarator),
|
|
1302
|
+
field('parameters', $.parameter_list),
|
|
1303
|
+
optional($.gnu_asm_expression),
|
|
1304
|
+
repeat($.attribute_specifier)
|
|
1305
|
+
)
|
|
1306
|
+
),
|
|
1209
1307
|
|
|
1210
1308
|
_braced_macro_declarator: ($) => alias(sym('_braced_macro_function_declarator'), $.function_declarator),
|
|
1211
1309
|
_braced_macro_function_declarator: ($) =>
|
|
@@ -2398,7 +2496,7 @@ module.exports = Object.assign(
|
|
|
2398
2496
|
|
|
2399
2497
|
_trailing_macro_call_expression: ($) =>
|
|
2400
2498
|
trailingAnnotationRule($, (name) =>
|
|
2401
|
-
seq(field('function', name), field('arguments', alias($.
|
|
2499
|
+
seq(field('function', name), field('arguments', alias($._parameter_macro_argument_list, $.argument_list)))
|
|
2402
2500
|
),
|
|
2403
2501
|
|
|
2404
2502
|
_conditional_macro_specifier: () => alias(sym('_marker_preproc_ifdef'), sym('preproc_ifdef')),
|
|
@@ -2506,8 +2604,8 @@ function* keywordStrings(node) {
|
|
|
2506
2604
|
|
|
2507
2605
|
/**
|
|
2508
2606
|
* Ranks a trailing parameter annotation against a leading marker (-2) that reads `int *A B` the other way: an
|
|
2509
|
-
* annotation named like a macro (`char **argv UNUSED
|
|
2510
|
-
*
|
|
2607
|
+
* annotation named like a macro (`char **argv UNUSED`) outranks it, and any other name ranks below it
|
|
2608
|
+
* (`int *CC pointer`), since macro definitions are unknown.
|
|
2511
2609
|
* @param {import('./types/treeSitterDsl').GrammarSymbols<'identifier' | '_macro_case_identifier'>} $
|
|
2512
2610
|
* @param {(name: import('./types/treeSitterDsl').Rule) => import('./types/treeSitterDsl').Rule} annotation
|
|
2513
2611
|
*/
|
|
@@ -2518,6 +2616,19 @@ function trailingAnnotationRule($, annotation) {
|
|
|
2518
2616
|
);
|
|
2519
2617
|
}
|
|
2520
2618
|
|
|
2619
|
+
/**
|
|
2620
|
+
* A leading marker with arguments in a parameter's pointer declarator.
|
|
2621
|
+
* @param {import('./types/treeSitterDsl').GrammarSymbols<'_parameter_macro_argument_list' | 'argument_list'>} $
|
|
2622
|
+
* @param {number} precedence
|
|
2623
|
+
* @param {import('./types/treeSitterDsl').Rule} name
|
|
2624
|
+
*/
|
|
2625
|
+
function leadingMacroCallRule($, precedence, name) {
|
|
2626
|
+
return prec.dynamic(
|
|
2627
|
+
precedence,
|
|
2628
|
+
seq(field('function', name), field('arguments', alias($._parameter_macro_argument_list, $.argument_list)))
|
|
2629
|
+
);
|
|
2630
|
+
}
|
|
2631
|
+
|
|
2521
2632
|
/**
|
|
2522
2633
|
* Returns a regular expression source that matches every string of a character in `first` followed by characters in
|
|
2523
2634
|
* `rest`, the empty string included, except the given words.
|
|
@@ -2607,25 +2718,43 @@ function parenthesizedDeclaratorRule($, declarator) {
|
|
|
2607
2718
|
function pointerDeclaratorRule($, declarator) {
|
|
2608
2719
|
return prec.right(
|
|
2609
2720
|
seq(
|
|
2610
|
-
|
|
2611
|
-
|
|
2612
|
-
'*',
|
|
2613
|
-
repeat($.attribute_declaration),
|
|
2614
|
-
repeat($.ms_pointer_modifier),
|
|
2615
|
-
repeat(
|
|
2616
|
-
choice(
|
|
2617
|
-
$.type_qualifier,
|
|
2618
|
-
$.attribute_specifier,
|
|
2619
|
-
prec(-1, $.ms_call_modifier),
|
|
2620
|
-
$.macro_specifier,
|
|
2621
|
-
$._conditional_macro_specifier
|
|
2622
|
-
)
|
|
2623
|
-
),
|
|
2721
|
+
...pointerDeclaratorHead($),
|
|
2722
|
+
repeat(pointerDeclaratorModifier($, $.macro_specifier)),
|
|
2624
2723
|
field('declarator', declarator)
|
|
2625
2724
|
)
|
|
2626
2725
|
);
|
|
2627
2726
|
}
|
|
2628
2727
|
|
|
2728
|
+
/**
|
|
2729
|
+
* The tokens of a pointer declarator up to its `*` and the modifiers that only follow it.
|
|
2730
|
+
* @param {import('./types/treeSitterDsl').GrammarSymbols<'ms_based_modifier' | 'ms_call_modifier' | 'attribute_declaration' | 'ms_pointer_modifier'>} $
|
|
2731
|
+
*/
|
|
2732
|
+
function pointerDeclaratorHead($) {
|
|
2733
|
+
return [
|
|
2734
|
+
optional($.ms_based_modifier),
|
|
2735
|
+
optional(prec.dynamic(2, $.ms_call_modifier)),
|
|
2736
|
+
'*',
|
|
2737
|
+
repeat($.attribute_declaration),
|
|
2738
|
+
repeat($.ms_pointer_modifier),
|
|
2739
|
+
];
|
|
2740
|
+
}
|
|
2741
|
+
|
|
2742
|
+
/**
|
|
2743
|
+
* A pointer declarator's modifier between its `*` and its declarator: a qualifier, attribute, calling convention or
|
|
2744
|
+
* conditional marker, or one of `markers`.
|
|
2745
|
+
* @param {import('./types/treeSitterDsl').GrammarSymbols<'type_qualifier' | 'attribute_specifier' | 'ms_call_modifier' | '_conditional_macro_specifier'>} $
|
|
2746
|
+
* @param {...import('./types/treeSitterDsl').Rule} markers
|
|
2747
|
+
*/
|
|
2748
|
+
function pointerDeclaratorModifier($, ...markers) {
|
|
2749
|
+
return choice(
|
|
2750
|
+
$.type_qualifier,
|
|
2751
|
+
$.attribute_specifier,
|
|
2752
|
+
prec(-1, $.ms_call_modifier),
|
|
2753
|
+
...markers,
|
|
2754
|
+
$._conditional_macro_specifier
|
|
2755
|
+
);
|
|
2756
|
+
}
|
|
2757
|
+
|
|
2629
2758
|
/**
|
|
2630
2759
|
* @param {import('./types/treeSitterDsl').GrammarSymbols<'_guarded_type_specifiers' | '_marked_macro_type_specifiers' | '_macro_declaration_specifiers'>} $
|
|
2631
2760
|
* @param {import('./types/treeSitterDsl').Rule} ordinary
|
|
@@ -2818,11 +2947,14 @@ function macroAttributeArgumentList(argument) {
|
|
|
2818
2947
|
return seq('(', optional(argument), repeat(seq(',', optional(argument))), ')');
|
|
2819
2948
|
}
|
|
2820
2949
|
|
|
2821
|
-
/**
|
|
2822
|
-
|
|
2950
|
+
/**
|
|
2951
|
+
* @param {import('./types/treeSitterDsl').GrammarSymbols<'expression' | 'parameter_declaration' | 'type_descriptor' | '_preproc_number' | 'number_literal' | 'compound_statement' | 'initializer_pair' | 'initializer_list'>} $
|
|
2952
|
+
* @param {import('./types/treeSitterDsl').Rule | false} [type] The type argument form, or `false` for none.
|
|
2953
|
+
*/
|
|
2954
|
+
function macroAttributeArgument($, type = prec.dynamic(-1, $.type_descriptor)) {
|
|
2823
2955
|
return choice(
|
|
2824
2956
|
$.expression,
|
|
2825
|
-
|
|
2957
|
+
...(type ? [type] : []),
|
|
2826
2958
|
alias($._preproc_number, $.number_literal),
|
|
2827
2959
|
$.compound_statement,
|
|
2828
2960
|
$.initializer_pair,
|
package/package.json
CHANGED
package/src/emptyDirectives.h
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// code-gauge-allow-file fileNcss
|
|
1
|
+
// code-gauge-allow-file fileNcss 695 -- One source-fact pass shares compact character storage, directive lexing and physical-position conversion.
|
|
2
2
|
#ifndef TREE_SITTER_C_EMPTY_DIRECTIVES_H_
|
|
3
3
|
#define TREE_SITTER_C_EMPTY_DIRECTIVES_H_
|
|
4
4
|
|
|
@@ -35,7 +35,8 @@ typedef struct {
|
|
|
35
35
|
} DirectiveLine;
|
|
36
36
|
|
|
37
37
|
static void directive_prefixes(EmptyDirectives *state, DirectiveInput *input);
|
|
38
|
-
static
|
|
38
|
+
static uint32_t directive_skip_inert(const DirectiveInput *input, uint32_t end, uint32_t position, uint32_t *byte);
|
|
39
|
+
static bool directive_append(DirectiveInput *input, uint32_t *capacity, uint32_t *byte, TSLexer *lexer);
|
|
39
40
|
static bool directive_reserve(DirectiveInput *input, uint32_t *capacity, unsigned width);
|
|
40
41
|
static bool directive_position(DirectiveInput *input, uint32_t index, uint32_t byte, uint32_t width);
|
|
41
42
|
static void directive_encode(DirectiveInput *input, uint32_t c);
|
|
@@ -50,6 +51,8 @@ static bool directive_requires_operand(DirectiveKind kind);
|
|
|
50
51
|
static void directive_line_character(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line, int32_t c);
|
|
51
52
|
static void directive_code(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line, int32_t c);
|
|
52
53
|
static bool directive_pragma_part(DirectiveLine *line, int32_t c);
|
|
54
|
+
static bool directive_neutral(const DirectiveLine *line);
|
|
55
|
+
static void directive_neutral_code(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line, int32_t c);
|
|
53
56
|
static bool directive_header_ready(DirectiveLine *line, int32_t c);
|
|
54
57
|
static bool directive_header(DirectiveInput *input, int32_t close);
|
|
55
58
|
static void directive_line_comment(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line);
|
|
@@ -59,7 +62,13 @@ static bool empty_directives_build(EmptyDirectives *state, TSLexer *lexer);
|
|
|
59
62
|
static void empty_directives_find(EmptyDirectives *state, DirectiveInput *input);
|
|
60
63
|
static int32_t directive_next(DirectiveInput *input);
|
|
61
64
|
static int32_t directive_physical_next(DirectiveInput *input);
|
|
65
|
+
static uint32_t directive_expected_width(const DirectiveInput *input, uint32_t index, int32_t *c);
|
|
62
66
|
static int32_t directive_peek(const DirectiveInput *input);
|
|
67
|
+
static int32_t directive_plain(const DirectiveInput *input);
|
|
68
|
+
static void directive_skip_plain(DirectiveInput *input);
|
|
69
|
+
typedef enum { DIRECTIVE_RUN_WORD, DIRECTIVE_RUN_CODE, DIRECTIVE_RUN_SPACE, DIRECTIVE_RUN_LINE_COMMENT, DIRECTIVE_RUN_BLOCK_COMMENT } DirectiveRun;
|
|
70
|
+
static inline uint32_t directive_plain_run(DirectiveInput *input, DirectiveRun run);
|
|
71
|
+
static bool directive_run_continues(DirectiveRun run, uint16_t c);
|
|
63
72
|
static void directive_pragma_name(DirectiveLine *line, const DirectiveInput *input);
|
|
64
73
|
static bool directive_pragma_operator(const DirectiveInput *input);
|
|
65
74
|
static int32_t directive_pragma_token(DirectiveInput *input);
|
|
@@ -74,7 +83,8 @@ typedef struct {
|
|
|
74
83
|
|
|
75
84
|
static uint32_t directive_keyword_end(const DirectiveInput *input);
|
|
76
85
|
static DirectiveKind directive_keyword(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line, const DirectiveWord *word, uint32_t end);
|
|
77
|
-
static DirectiveWord directive_word(DirectiveInput *input, int32_t first, bool raw);
|
|
86
|
+
static DirectiveWord directive_word(DirectiveInput *input, int32_t first, bool raw, bool classify);
|
|
87
|
+
static void directive_word_run(DirectiveInput *input, DirectiveWord *word, bool classify);
|
|
78
88
|
static DirectiveKind directive_word_kind(const DirectiveWord *word);
|
|
79
89
|
static void directive_word_character(DirectiveWord *word, int32_t c);
|
|
80
90
|
static void directive_word_role(DirectiveLine *line, DirectiveKind kind, bool name, bool pragma, bool had_operand, bool identifier);
|
|
@@ -141,14 +151,12 @@ static bool empty_directives_build(EmptyDirectives *state, TSLexer *lexer) {
|
|
|
141
151
|
TSLexerPosition start;
|
|
142
152
|
lexer->get_position(lexer, &start);
|
|
143
153
|
DirectiveInput input = {.byte = start.bytes};
|
|
144
|
-
uint32_t capacity = 0;
|
|
154
|
+
uint32_t capacity = 0, byte = start.bytes;
|
|
145
155
|
while (!lexer->eof(lexer)) {
|
|
146
|
-
if (!directive_append(&input, &capacity, lexer)) goto failed;
|
|
156
|
+
if (!directive_append(&input, &capacity, &byte, lexer)) goto failed;
|
|
147
157
|
}
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
input.end = end.bytes;
|
|
151
|
-
state->bytes = end.bytes;
|
|
158
|
+
input.end = byte;
|
|
159
|
+
state->bytes = byte;
|
|
152
160
|
state->starts = calloc((size_t)state->bytes / 8 + 1, 1);
|
|
153
161
|
state->ends = calloc((size_t)state->bytes / 8 + 1, 1);
|
|
154
162
|
state->directive_starts = calloc((size_t)state->bytes / 8 + 1, 1);
|
|
@@ -171,23 +179,22 @@ failed:
|
|
|
171
179
|
return false;
|
|
172
180
|
}
|
|
173
181
|
|
|
174
|
-
// code-gauge-allow functionCognitiveComplexity
|
|
182
|
+
// code-gauge-allow functionCognitiveComplexity 29 -- The reverse lexical pass applies sparse physical-position corrections while carrying null-prefix and line-end facts.
|
|
175
183
|
static void directive_prefixes(EmptyDirectives *state, DirectiveInput *input) {
|
|
176
184
|
bool prefix = false, line_end = false;
|
|
177
185
|
uint32_t byte = input->end, position = input->position_count;
|
|
178
186
|
for (uint32_t i = input->length; i > 0;) {
|
|
187
|
+
if (!prefix && !line_end) i = directive_skip_inert(input, i, position, &byte);
|
|
188
|
+
if (!i) break;
|
|
179
189
|
i--;
|
|
180
190
|
if (i && directive_character(input, i) >= 0xdc00 && directive_character(input, i) <= 0xdfff &&
|
|
181
191
|
directive_character(input, i - 1) >= 0xd800 && directive_character(input, i - 1) <= 0xdbff) continue;
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
character.byte = 0;
|
|
185
|
-
character.position_count = 0;
|
|
186
|
-
int32_t c = directive_physical_next(&character);
|
|
192
|
+
int32_t c;
|
|
193
|
+
uint32_t width = directive_expected_width(input, i, &c);
|
|
187
194
|
if (position && input->positions[position - 1].index == i) byte = input->positions[--position].byte;
|
|
188
195
|
else {
|
|
189
|
-
if (
|
|
190
|
-
byte -=
|
|
196
|
+
if (width > byte) return;
|
|
197
|
+
byte -= width;
|
|
191
198
|
}
|
|
192
199
|
if (byte > state->bytes) return;
|
|
193
200
|
bool hash = c == '#' && (state->starts[byte / 8] & (1u << (byte % 8)));
|
|
@@ -199,22 +206,39 @@ static void directive_prefixes(EmptyDirectives *state, DirectiveInput *input) {
|
|
|
199
206
|
}
|
|
200
207
|
}
|
|
201
208
|
|
|
202
|
-
|
|
203
|
-
|
|
209
|
+
// Steps back from the character before `end` over characters that cannot change the reverse pass's state while no
|
|
210
|
+
// prefix or line end is carried: plain-width characters that are neither `#` nor barriers. Returns the new end.
|
|
211
|
+
static uint32_t directive_skip_inert(const DirectiveInput *input, uint32_t end, uint32_t position, uint32_t *byte) {
|
|
212
|
+
uint32_t stop = position ? input->positions[position - 1].index + 1 : 0, ascii_width = input->utf16 ? 2 : 1;
|
|
213
|
+
while (end > stop) {
|
|
214
|
+
uint32_t c = directive_character(input, end - 1);
|
|
215
|
+
uint32_t width = c < 0x80 ? ascii_width : input->utf16 && (c < 0xd800 || c > 0xdfff) ? 2 : 0;
|
|
216
|
+
if (!width || c == '#' || width > *byte) break;
|
|
217
|
+
uint32_t previous = *byte - width;
|
|
218
|
+
if (input->prefix_barriers[previous / 8] & (1u << (previous % 8))) break;
|
|
219
|
+
*byte = previous;
|
|
220
|
+
end--;
|
|
221
|
+
}
|
|
222
|
+
return end;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// `*byte` is the lexer's position before the character and is advanced past it.
|
|
226
|
+
static bool directive_append(DirectiveInput *input, uint32_t *capacity, uint32_t *byte, TSLexer *lexer) {
|
|
227
|
+
uint32_t c = lexer->lookahead, index = input->length, start = *byte;
|
|
204
228
|
unsigned width = directive_utf8_width(c), utf16_width = c > 0xffff && c <= 0x10ffff ? 4 : 2;
|
|
205
229
|
if (!directive_reserve(input, capacity, c > 0xffff && c <= 0x10ffff ? 2 : 1)) return false;
|
|
206
230
|
directive_encode(input, c);
|
|
207
|
-
TSLexerPosition
|
|
208
|
-
lexer->get_position(lexer, &start);
|
|
231
|
+
TSLexerPosition end;
|
|
209
232
|
lexer->advance(lexer, false);
|
|
210
233
|
lexer->get_position(lexer, &end);
|
|
211
|
-
|
|
234
|
+
*byte = end.bytes;
|
|
235
|
+
uint32_t measured = end.bytes - start;
|
|
212
236
|
if (!input->encoding_known && c <= 0x10ffff && width != utf16_width && !lexer->is_at_included_range_start(lexer)) {
|
|
213
237
|
input->utf16 = measured == utf16_width;
|
|
214
238
|
input->encoding_known = true;
|
|
215
239
|
}
|
|
216
240
|
if (c > 0x10ffff || measured != (input->utf16 ? utf16_width : width)) {
|
|
217
|
-
return directive_position(input, index, start
|
|
241
|
+
return directive_position(input, index, start, measured);
|
|
218
242
|
}
|
|
219
243
|
return true;
|
|
220
244
|
}
|
|
@@ -283,6 +307,8 @@ static unsigned directive_utf8_width(uint32_t c) {
|
|
|
283
307
|
static void empty_directives_find(EmptyDirectives *state, DirectiveInput *input) {
|
|
284
308
|
DirectiveLine line = {.beginning = true};
|
|
285
309
|
while (input->index < input->length) {
|
|
310
|
+
// Horizontal spaces never change the line state.
|
|
311
|
+
directive_plain_run(input, DIRECTIVE_RUN_SPACE);
|
|
286
312
|
int32_t c = directive_next(input);
|
|
287
313
|
if (c < 0) break;
|
|
288
314
|
directive_line_character(state, input, &line, c);
|
|
@@ -312,7 +338,8 @@ static void directive_line_character(EmptyDirectives *state, DirectiveInput *inp
|
|
|
312
338
|
line->pending = true;
|
|
313
339
|
return;
|
|
314
340
|
}
|
|
315
|
-
|
|
341
|
+
if (directive_neutral(line)) directive_neutral_code(state, input, line, c);
|
|
342
|
+
else directive_code(state, input, line, c);
|
|
316
343
|
}
|
|
317
344
|
|
|
318
345
|
// code-gauge-allow functionCognitiveComplexity 19 -- Header names, quoted tokens and distinct keyword boundaries update one logical directive's lexical state.
|
|
@@ -333,12 +360,38 @@ static void directive_code(EmptyDirectives *state, DirectiveInput *input, Direct
|
|
|
333
360
|
}
|
|
334
361
|
if (!preproc_word(c, false) && !(c >= '0' && c <= '9')) return;
|
|
335
362
|
uint32_t end = name ? directive_keyword_end(input) : 0;
|
|
336
|
-
DirectiveWord word = directive_word(input, c, line->kind != DIRECTIVE_DIAGNOSTIC);
|
|
363
|
+
DirectiveWord word = directive_word(input, c, line->kind != DIRECTIVE_DIAGNOSTIC, true);
|
|
337
364
|
DirectiveKind kind = name ? directive_keyword(state, input, line, &word, end) : directive_word_kind(&word);
|
|
338
365
|
if (!name && !pragma && kind == DIRECTIVE_PRAGMA) directive_pragma_name(line, input);
|
|
339
366
|
directive_word_role(line, kind, name, pragma, operand, preproc_word(c, false));
|
|
340
367
|
}
|
|
341
368
|
|
|
369
|
+
// Outside a directive name, a line of no directive kind with no `_Pragma` or header operand in progress keeps that
|
|
370
|
+
// state through every token, so its words need no classification.
|
|
371
|
+
static bool directive_neutral(const DirectiveLine *line) {
|
|
372
|
+
return !line->pending && line->kind == DIRECTIVE_ORDINARY && !line->pragma_depth && !line->pragma_pending && !line->header;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
// Also consumes the following characters that leave the line state as it is, which is every plain character but `/`.
|
|
376
|
+
static void directive_neutral_code(EmptyDirectives *state, DirectiveInput *input, DirectiveLine *line, int32_t c) {
|
|
377
|
+
line->operand = true;
|
|
378
|
+
line->beginning = false;
|
|
379
|
+
for (;;) {
|
|
380
|
+
if (c == '"' || c == '\'') {
|
|
381
|
+
if (directive_quoted(input, c)) {
|
|
382
|
+
directive_line_end(state, input, line);
|
|
383
|
+
return;
|
|
384
|
+
}
|
|
385
|
+
} else if (preproc_word(c, false) || (c >= '0' && c <= '9')) {
|
|
386
|
+
directive_word(input, c, true, false);
|
|
387
|
+
}
|
|
388
|
+
directive_plain_run(input, DIRECTIVE_RUN_CODE);
|
|
389
|
+
c = directive_plain(input);
|
|
390
|
+
if (c < 0 || c == '/') return;
|
|
391
|
+
directive_skip_plain(input);
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
|
|
342
395
|
static void directive_word_role(DirectiveLine *line, DirectiveKind kind, bool name, bool pragma, bool had_operand, bool identifier) {
|
|
343
396
|
if (name) {
|
|
344
397
|
line->kind = kind;
|
|
@@ -444,6 +497,11 @@ static void directive_barrier(DirectiveInput *input) {
|
|
|
444
497
|
}
|
|
445
498
|
|
|
446
499
|
static int32_t directive_next(DirectiveInput *input) {
|
|
500
|
+
int32_t plain = directive_plain(input);
|
|
501
|
+
if (plain >= 0) {
|
|
502
|
+
directive_skip_plain(input);
|
|
503
|
+
return plain;
|
|
504
|
+
}
|
|
447
505
|
while (input->index < input->length) {
|
|
448
506
|
int32_t c = directive_physical_next(input);
|
|
449
507
|
uint32_t after = c == '\\' ? directive_splice_end(input) : input->index;
|
|
@@ -462,20 +520,36 @@ static int32_t directive_next(DirectiveInput *input) {
|
|
|
462
520
|
}
|
|
463
521
|
|
|
464
522
|
static int32_t directive_physical_next(DirectiveInput *input) {
|
|
465
|
-
uint32_t index = input->index
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
c = 0x10000 + ((c - 0xd800) << 10) + (directive_character(input, input->index++) - 0xdc00);
|
|
470
|
-
}
|
|
523
|
+
uint32_t index = input->index;
|
|
524
|
+
int32_t c;
|
|
525
|
+
uint32_t width = directive_expected_width(input, index, &c);
|
|
526
|
+
input->index += c > 0xffff ? 2 : 1;
|
|
471
527
|
input->before_previous_byte = input->previous_byte;
|
|
472
528
|
input->previous_byte = input->byte;
|
|
473
529
|
if (input->position_index < input->position_count && input->positions[input->position_index].index == index) {
|
|
474
530
|
DirectivePosition position = input->positions[input->position_index++];
|
|
475
531
|
input->previous_byte = position.byte;
|
|
476
532
|
input->byte = position.byte + position.width;
|
|
477
|
-
} else input->byte +=
|
|
478
|
-
return
|
|
533
|
+
} else input->byte += width;
|
|
534
|
+
return c;
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
// Decodes the character at `index` into `*c` and returns its byte width when it has no recorded position.
|
|
538
|
+
static uint32_t directive_expected_width(const DirectiveInput *input, uint32_t index, int32_t *c) {
|
|
539
|
+
uint32_t unit = directive_character(input, index);
|
|
540
|
+
if (unit == 0xdfff) {
|
|
541
|
+
*c = 0xfffd;
|
|
542
|
+
return input->utf16 ? 2 : 1;
|
|
543
|
+
}
|
|
544
|
+
if (unit >= 0xd800 && unit <= 0xdbff && index + 1 < input->length) {
|
|
545
|
+
uint32_t low = directive_character(input, index + 1);
|
|
546
|
+
if (low >= 0xdc00 && low <= 0xdfff) {
|
|
547
|
+
*c = (int32_t)(0x10000 + ((unit - 0xd800) << 10) + (low - 0xdc00));
|
|
548
|
+
return 4;
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
*c = (int32_t)unit;
|
|
552
|
+
return input->utf16 ? 2 : directive_utf8_width(unit);
|
|
479
553
|
}
|
|
480
554
|
|
|
481
555
|
static uint32_t directive_splice_end(const DirectiveInput *input) {
|
|
@@ -489,10 +563,68 @@ static uint32_t directive_splice_end(const DirectiveInput *input) {
|
|
|
489
563
|
}
|
|
490
564
|
|
|
491
565
|
static int32_t directive_peek(const DirectiveInput *input) {
|
|
566
|
+
int32_t plain = directive_plain(input);
|
|
567
|
+
if (plain >= 0) return plain;
|
|
492
568
|
DirectiveInput copy = *input;
|
|
493
569
|
return directive_next(©);
|
|
494
570
|
}
|
|
495
571
|
|
|
572
|
+
// Consumes the longest run of plain characters that `run` accepts and returns its length.
|
|
573
|
+
static inline uint32_t directive_plain_run(DirectiveInput *input, DirectiveRun run) {
|
|
574
|
+
uint32_t index = input->index, end = input->length;
|
|
575
|
+
if (input->position_index < input->position_count && input->positions[input->position_index].index < end) {
|
|
576
|
+
end = input->positions[input->position_index].index;
|
|
577
|
+
}
|
|
578
|
+
while (index < end) {
|
|
579
|
+
uint16_t c = directive_character(input, index);
|
|
580
|
+
if (c >= 0x80 || c == '\\' || directive_newline(c) || !directive_run_continues(run, c)) break;
|
|
581
|
+
index++;
|
|
582
|
+
}
|
|
583
|
+
uint32_t length = index - input->index, width = input->utf16 ? 2 : 1;
|
|
584
|
+
if (!length) return 0;
|
|
585
|
+
input->before_previous_byte = length > 1 ? input->byte + (length - 2) * width : input->previous_byte;
|
|
586
|
+
input->previous_byte = input->byte + (length - 1) * width;
|
|
587
|
+
input->byte += length * width;
|
|
588
|
+
input->index = index;
|
|
589
|
+
return length;
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
// `c` is ASCII.
|
|
593
|
+
static bool directive_run_continues(DirectiveRun run, uint16_t c) {
|
|
594
|
+
bool identifier = (preproc_identifier_continue_ascii[c / 64] >> (c % 64)) & 1;
|
|
595
|
+
switch (run) {
|
|
596
|
+
case DIRECTIVE_RUN_WORD:
|
|
597
|
+
return identifier;
|
|
598
|
+
case DIRECTIVE_RUN_CODE:
|
|
599
|
+
// The rest of a neutral line: characters that start no token.
|
|
600
|
+
return !identifier && c != '"' && c != '\'' && c != '/';
|
|
601
|
+
case DIRECTIVE_RUN_SPACE:
|
|
602
|
+
return directive_horizontal(c);
|
|
603
|
+
case DIRECTIVE_RUN_LINE_COMMENT:
|
|
604
|
+
return true;
|
|
605
|
+
case DIRECTIVE_RUN_BLOCK_COMMENT:
|
|
606
|
+
return c != '*';
|
|
607
|
+
}
|
|
608
|
+
return false;
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
static void directive_skip_plain(DirectiveInput *input) {
|
|
612
|
+
input->index++;
|
|
613
|
+
input->before_previous_byte = input->previous_byte;
|
|
614
|
+
input->previous_byte = input->byte;
|
|
615
|
+
input->byte += input->utf16 ? 2 : 1;
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
// The next character when it is ASCII, cannot start a line splice or a newline, and has its expected width; otherwise -1.
|
|
619
|
+
static int32_t directive_plain(const DirectiveInput *input) {
|
|
620
|
+
if (input->index >= input->length) return -1;
|
|
621
|
+
uint16_t c = directive_character(input, input->index);
|
|
622
|
+
if (c >= 0x80 || c == '\\' || directive_newline(c)) return -1;
|
|
623
|
+
bool positioned = input->position_index < input->position_count &&
|
|
624
|
+
input->positions[input->position_index].index == input->index;
|
|
625
|
+
return positioned ? -1 : c;
|
|
626
|
+
}
|
|
627
|
+
|
|
496
628
|
// `_Pragma` is the macro name of `#define _Pragma(x)` or `#ifdef _Pragma`, but an operator before a string literal.
|
|
497
629
|
static void directive_pragma_name(DirectiveLine *line, const DirectiveInput *input) {
|
|
498
630
|
if (line->kind == DIRECTIVE_UNDEF || (line->kind == DIRECTIVE_REQUIRED && !directive_pragma_operator(input))) line->pragma_name = true;
|
|
@@ -525,7 +657,12 @@ static int32_t directive_pragma_token(DirectiveInput *input) {
|
|
|
525
657
|
static bool directive_comment(DirectiveInput *input, bool *line) {
|
|
526
658
|
*line = directive_next(input) == '/';
|
|
527
659
|
int32_t previous = 0;
|
|
660
|
+
DirectiveRun run = *line ? DIRECTIVE_RUN_LINE_COMMENT : DIRECTIVE_RUN_BLOCK_COMMENT;
|
|
528
661
|
while (input->index < input->length) {
|
|
662
|
+
if (previous != '*') {
|
|
663
|
+
uint32_t length = directive_plain_run(input, run);
|
|
664
|
+
if (length) previous = directive_character(input, input->index - 1);
|
|
665
|
+
}
|
|
529
666
|
int32_t c = directive_next(input);
|
|
530
667
|
if (*line && c == '\n') return true;
|
|
531
668
|
if (!*line && previous == '*' && c == '/') return true;
|
|
@@ -565,11 +702,13 @@ static DirectiveKind directive_keyword(EmptyDirectives *state, DirectiveInput *i
|
|
|
565
702
|
return kind;
|
|
566
703
|
}
|
|
567
704
|
|
|
568
|
-
|
|
705
|
+
// Without `classify`, only `raw` of the result is complete.
|
|
706
|
+
static DirectiveWord directive_word(DirectiveInput *input, int32_t first, bool raw, bool classify) {
|
|
569
707
|
DirectiveWord word = {0};
|
|
570
708
|
directive_word_character(&word, first);
|
|
571
709
|
bool number = first >= '0' && first <= '9';
|
|
572
710
|
int32_t previous = first;
|
|
711
|
+
if (!number) directive_word_run(input, &word, classify);
|
|
573
712
|
for (;;) {
|
|
574
713
|
int32_t c = directive_peek(input);
|
|
575
714
|
bool exponent_sign = number && (c == '+' || c == '-') &&
|
|
@@ -582,6 +721,13 @@ static DirectiveWord directive_word(DirectiveInput *input, int32_t first, bool r
|
|
|
582
721
|
return word;
|
|
583
722
|
}
|
|
584
723
|
|
|
724
|
+
static void directive_word_run(DirectiveInput *input, DirectiveWord *word, bool classify) {
|
|
725
|
+
uint32_t start = input->index, length = directive_plain_run(input, DIRECTIVE_RUN_WORD);
|
|
726
|
+
// Both parts of the word stop changing after these many characters.
|
|
727
|
+
uint32_t significant = classify ? sizeof word->directive : sizeof word->raw.value;
|
|
728
|
+
for (uint32_t i = 0; i < length && i < significant; i++) directive_word_character(word, directive_character(input, start + i));
|
|
729
|
+
}
|
|
730
|
+
|
|
585
731
|
static DirectiveKind directive_word_kind(const DirectiveWord *word) {
|
|
586
732
|
if (word->length >= sizeof word->directive) return DIRECTIVE_ORDINARY;
|
|
587
733
|
if (strcmp(word->directive, "if") == 0 || strcmp(word->directive, "elif") == 0) return DIRECTIVE_CONDITION;
|
package/src/identifier.h
CHANGED
|
@@ -197,4 +197,7 @@ static const TSCharacterRange preproc_identifier_continue[] = {
|
|
|
197
197
|
{0x31350, 0x323af}, {0xe0100, 0xe01ef},
|
|
198
198
|
};
|
|
199
199
|
|
|
200
|
+
static const uint64_t preproc_identifier_start_ascii[] = {0x0000001000000000ull, 0x07fffffe97fffffeull};
|
|
201
|
+
static const uint64_t preproc_identifier_continue_ascii[] = {0x03ff001000000000ull, 0x07fffffe97fffffeull};
|
|
202
|
+
|
|
200
203
|
#endif
|
package/src/node-types.json
CHANGED
package/src/pragma.h
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// code-gauge-allow-file fileNcss
|
|
1
|
+
// code-gauge-allow-file fileNcss 489 -- Preprocessor tokens and pragma operators share names, spacing, and literal readers.
|
|
2
2
|
#ifndef TREE_SITTER_C_PRAGMA_H_
|
|
3
3
|
#define TREE_SITTER_C_PRAGMA_H_
|
|
4
4
|
|
|
@@ -57,6 +57,7 @@ static bool scan_preproc_newline(TSLexer *lexer, bool skip);
|
|
|
57
57
|
static bool scan_preproc_splices(TSLexer *lexer);
|
|
58
58
|
static bool scan_preproc_ucn(TSLexer *lexer, bool *in_number);
|
|
59
59
|
static bool preproc_word(int32_t c, bool continuation);
|
|
60
|
+
static bool preproc_identifier_character(int32_t c, bool continuation);
|
|
60
61
|
static bool scan_preproc_quote(TSLexer *lexer, int32_t quote, bool escaped, bool directive_text);
|
|
61
62
|
static bool scan_preproc_raw_quote(TSLexer *lexer);
|
|
62
63
|
|
|
@@ -401,6 +402,14 @@ static bool scan_preproc_ucn(TSLexer *lexer, bool *in_number) {
|
|
|
401
402
|
static bool preproc_word(int32_t c, bool continuation) {
|
|
402
403
|
if (c == '\\') return false;
|
|
403
404
|
if (continuation && c >= 0x80 && !pragma_space(c)) return true;
|
|
405
|
+
return preproc_identifier_character(c, continuation);
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
static bool preproc_identifier_character(int32_t c, bool continuation) {
|
|
409
|
+
if (c >= 0 && c < 0x80) {
|
|
410
|
+
const uint64_t *ascii = continuation ? preproc_identifier_continue_ascii : preproc_identifier_start_ascii;
|
|
411
|
+
return (ascii[c / 64] >> (c % 64)) & 1;
|
|
412
|
+
}
|
|
404
413
|
return continuation
|
|
405
414
|
? set_contains(preproc_identifier_continue, sizeof(preproc_identifier_continue) / sizeof(TSCharacterRange), c)
|
|
406
415
|
: set_contains(preproc_identifier_start, sizeof(preproc_identifier_start) / sizeof(TSCharacterRange), c);
|
|
@@ -481,10 +490,7 @@ static MacroNameCharacter scan_macro_name_character(TSLexer *lexer, bool continu
|
|
|
481
490
|
name->ascii = false;
|
|
482
491
|
return scan_macro_name_ucn(lexer) ? MACRO_NAME_CHARACTER : MACRO_NAME_INVALID;
|
|
483
492
|
}
|
|
484
|
-
|
|
485
|
-
? set_contains(preproc_identifier_continue, sizeof(preproc_identifier_continue) / sizeof(TSCharacterRange), c)
|
|
486
|
-
: set_contains(preproc_identifier_start, sizeof(preproc_identifier_start) / sizeof(TSCharacterRange), c);
|
|
487
|
-
if (!accepted) return MACRO_NAME_END;
|
|
493
|
+
if (!preproc_identifier_character(c, continuation)) return MACRO_NAME_END;
|
|
488
494
|
if (name->ascii) {
|
|
489
495
|
if (c < 128 && name->length < name->capacity) name->word[name->length++] = (char)c;
|
|
490
496
|
else name->ascii = false;
|
package/tree-sitter-c.wasm
CHANGED
|
Binary file
|