@konomanoasa/tree-sitter-c 0.0.0-stage → 0.1.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/Cargo.toml +38 -0
- package/LICENSE +21 -0
- package/PARSING_POLICY.md +76 -0
- package/README.md +31 -2
- package/binding.gyp +34 -0
- package/bindings/node/binding.cc +25 -0
- package/bindings/node/index.d.ts +5 -0
- package/bindings/node/index.js +15 -0
- package/bindings/python/__init__.py +9 -0
- package/bindings/python/binding.c +27 -0
- package/bindings/python/binding.pyi +3 -0
- package/bindings/python/py.typed +0 -0
- package/bindings/rust/build.rs +23 -0
- package/bindings/rust/lib.rs +9 -0
- package/grammar.js +1479 -0
- package/package.json +70 -4
- package/pyproject.toml +74 -0
- package/queries/highlights.scm +281 -0
- package/scripts/tree-sitter.js +195 -0
- package/setup.py +32 -0
- package/src/grammar.json +76212 -0
- package/src/node-types.json +7137 -0
- package/src/parser.c +953096 -0
- package/src/scanner.c +1558 -0
- package/src/tokens.h +287 -0
- package/src/tokens.json +162 -0
- package/src/tree_sitter/alloc.h +54 -0
- package/src/tree_sitter/array.h +336 -0
- package/src/tree_sitter/parser.h +286 -0
- package/test/bindings.test.rs +29 -0
- package/tree-sitter.json +31 -0
package/grammar.js
ADDED
|
@@ -0,0 +1,1479 @@
|
|
|
1
|
+
import tokens from "./src/tokens.json" with { type: "json" };
|
|
2
|
+
|
|
3
|
+
const { categories, directives, keywords, punctuators, typeArgumentMacros } =
|
|
4
|
+
tokens;
|
|
5
|
+
const externals = [
|
|
6
|
+
...categories,
|
|
7
|
+
...typeArgumentMacros.map((word) => `_lib_${word}`),
|
|
8
|
+
...directives.map((word) => `_pp_${word}`),
|
|
9
|
+
...keywords.map((word) => `_kw_${word}`),
|
|
10
|
+
...Object.keys(punctuators).map((name) => `_p_${name}`),
|
|
11
|
+
];
|
|
12
|
+
|
|
13
|
+
const identifierSpelling = ($) =>
|
|
14
|
+
choice($._identifier, ...typeArgumentMacros.map((name) => $[`_lib_${name}`]));
|
|
15
|
+
|
|
16
|
+
const punctuationNames = new Map(
|
|
17
|
+
Object.entries(punctuators).map(([name, text]) => [text, name]),
|
|
18
|
+
);
|
|
19
|
+
const p = ($, text) => {
|
|
20
|
+
const token = alias($[`_p_${punctuationNames.get(text)}`], text);
|
|
21
|
+
return text === "("
|
|
22
|
+
? choice(token, alias($._identifier_list_open, "("))
|
|
23
|
+
: token;
|
|
24
|
+
};
|
|
25
|
+
const kw = ($, text) => alias($[`_kw_${text}`], text);
|
|
26
|
+
const pp = ($, text) => alias($[`_pp_${text}`], text);
|
|
27
|
+
const branchHash = ($) => alias($._branch_hash, "#");
|
|
28
|
+
const separated = (item, separator) => seq(item, repeat(seq(separator, item)));
|
|
29
|
+
|
|
30
|
+
const oneOf = ($, spellings) => choice(...spellings.map((text) => p($, text)));
|
|
31
|
+
const binary = (left, operator, right) =>
|
|
32
|
+
seq(field("left", left), field("operator", operator), field("right", right));
|
|
33
|
+
|
|
34
|
+
const groupRef = ($, context, name) =>
|
|
35
|
+
context ? alias($[`_${context}_${name}`], $[name]) : $[name];
|
|
36
|
+
const controlDirective = ($) =>
|
|
37
|
+
choice(
|
|
38
|
+
alias($._structural_control_line, $.control_line),
|
|
39
|
+
alias($._structural_non_directive, $.non_directive),
|
|
40
|
+
);
|
|
41
|
+
const directive = ($, context) =>
|
|
42
|
+
choice(groupRef($, context, "if_section"), controlDirective($));
|
|
43
|
+
|
|
44
|
+
function sectionBody($, context) {
|
|
45
|
+
return seq(
|
|
46
|
+
groupRef($, context, "if_group"),
|
|
47
|
+
repeat(groupRef($, context, "elif_group")),
|
|
48
|
+
optional(groupRef($, context, "else_group")),
|
|
49
|
+
$.endif_line,
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function conditionalRules(context, branch) {
|
|
54
|
+
return {
|
|
55
|
+
[`_${context}_if_section`]: ($) =>
|
|
56
|
+
seq($._structural_start, sectionBody($, context)),
|
|
57
|
+
...branchRules(context, branch),
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function branchRules(context, branch) {
|
|
62
|
+
const name = (base) => (context ? `_${context}_${base}` : base);
|
|
63
|
+
// A terminal after trailing extras keeps them inside their source branch.
|
|
64
|
+
const body = ($) => seq(branch($), optional($._branch_trivia_end));
|
|
65
|
+
return {
|
|
66
|
+
[name("if_group")]: ($) => seq($._if_header, body($)),
|
|
67
|
+
[name("elif_group")]: ($) => seq($._elif_header, body($)),
|
|
68
|
+
[name("else_group")]: ($) => seq($._else_header, body($)),
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const singleValue = ($, value) => {
|
|
73
|
+
const surrounding = repeat(controlDirective($));
|
|
74
|
+
return seq(surrounding, optional(seq(value, surrounding)));
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
const specifiers = ($, context, item) =>
|
|
78
|
+
repeat1(choice(item, prec.dynamic(-1, groupRef($, context, "if_section"))));
|
|
79
|
+
|
|
80
|
+
function declarationSpecifiers($, context, modifier, item) {
|
|
81
|
+
// C17 6.7.2 requires a type specifier when both readings fit the source.
|
|
82
|
+
const section = prec.dynamic(-1, groupRef($, context, "if_section"));
|
|
83
|
+
return choice(
|
|
84
|
+
seq(
|
|
85
|
+
repeat(modifier),
|
|
86
|
+
choice($.type_specifier, section),
|
|
87
|
+
repeat(choice(item, section)),
|
|
88
|
+
),
|
|
89
|
+
prec.dynamic(-2, repeat1(modifier)),
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const listItems = {
|
|
94
|
+
init_declarator_list: "init_declarator",
|
|
95
|
+
initializer_list: "_initializer_entry",
|
|
96
|
+
enumerator_list: "enumerator",
|
|
97
|
+
struct_declarator_list: "struct_declarator",
|
|
98
|
+
generic_assoc_list: "generic_association",
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
const braceLists = new Set(["initializer_list", "enumerator_list"]);
|
|
102
|
+
const listStates = ["start", "item", "comma"];
|
|
103
|
+
|
|
104
|
+
// States name the last token on one path through the list. An empty branch
|
|
105
|
+
// preserves its incoming state.
|
|
106
|
+
const reachable = (from, to) => from === to || to !== "start";
|
|
107
|
+
const listName = (list, from, to) => `_${list}_${from}_${to}`;
|
|
108
|
+
const listNode = ($, list, from, to) =>
|
|
109
|
+
alias($[listName(list, from, to)], $[list]);
|
|
110
|
+
const uncheckedGroup = ($, list, kind) => groupRef($, `any_${list}`, kind);
|
|
111
|
+
|
|
112
|
+
function uncheckedListRules(list, item) {
|
|
113
|
+
const name = `_any_${list}`;
|
|
114
|
+
return {
|
|
115
|
+
...conditionalRules(`any_${list}`, ($) => optional($[`${name}_body`])),
|
|
116
|
+
[`${name}_body`]: ($) => {
|
|
117
|
+
const commas = repeat1(p($, ","));
|
|
118
|
+
return choice(
|
|
119
|
+
commas,
|
|
120
|
+
seq(optional(commas), alias($[name], $[list]), optional(commas)),
|
|
121
|
+
);
|
|
122
|
+
},
|
|
123
|
+
[name]: ($) => {
|
|
124
|
+
const unit = choice($[item], uncheckedGroup($, list, "if_section"));
|
|
125
|
+
return choice(
|
|
126
|
+
unit,
|
|
127
|
+
seq($[name], repeat1(p($, ",")), unit),
|
|
128
|
+
list === "parameter_identifiers"
|
|
129
|
+
? seq($[name], unit)
|
|
130
|
+
: prec.dynamic(-2, seq($[name], unit)),
|
|
131
|
+
);
|
|
132
|
+
},
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function listSequence($, list, states, from, to) {
|
|
137
|
+
const prefix = (start, end) => $[listName(list, start, end)];
|
|
138
|
+
const unit = (start, end) => $[`${listName(list, start, end)}_unit`];
|
|
139
|
+
const paths = [unit(from, to)];
|
|
140
|
+
for (const middle of states) {
|
|
141
|
+
if (reachable(from, middle) && reachable(middle, to))
|
|
142
|
+
paths.push(seq(prefix(from, middle), unit(middle, to)));
|
|
143
|
+
}
|
|
144
|
+
if (reachable(from, "item") && reachable("comma", to))
|
|
145
|
+
paths.push(seq(prefix(from, "item"), p($, ","), unit("comma", to)));
|
|
146
|
+
return choice(...paths);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function listBranch($, list, from, to) {
|
|
150
|
+
const comma = p($, ",");
|
|
151
|
+
const node = (start, end) => listNode($, list, start, end);
|
|
152
|
+
const paths = [];
|
|
153
|
+
paths.push(node(from, to));
|
|
154
|
+
if (from === "item") paths.push(seq(comma, node("comma", to)));
|
|
155
|
+
if (to === "comma") {
|
|
156
|
+
paths.push(seq(node(from, "item"), comma));
|
|
157
|
+
if (from === "item")
|
|
158
|
+
paths.push(comma, seq(comma, node("comma", "item"), comma));
|
|
159
|
+
}
|
|
160
|
+
return choice(...paths);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function listSection($, list, context, canSkip) {
|
|
164
|
+
const first = uncheckedGroup($, list, "if_group");
|
|
165
|
+
const elif = uncheckedGroup($, list, "elif_group");
|
|
166
|
+
const last = uncheckedGroup($, list, "else_group");
|
|
167
|
+
const selected = (kind) => groupRef($, context, kind);
|
|
168
|
+
const otherElse = canSkip ? last : optional(last);
|
|
169
|
+
const paths = [
|
|
170
|
+
seq(selected("if_group"), repeat(elif), otherElse),
|
|
171
|
+
seq(first, repeat(elif), selected("elif_group"), repeat(elif), otherElse),
|
|
172
|
+
seq(first, repeat(elif), selected("else_group")),
|
|
173
|
+
];
|
|
174
|
+
if (canSkip) paths.push(seq(first, repeat(elif)));
|
|
175
|
+
return seq($._structural_start, choice(...paths), $.endif_line);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function listRules(list, item) {
|
|
179
|
+
const states = listStates;
|
|
180
|
+
const rules = {
|
|
181
|
+
...uncheckedListRules(list, item),
|
|
182
|
+
[list]: ($) => {
|
|
183
|
+
const paths = [$[listName(list, "start", "item")]];
|
|
184
|
+
if (!braceLists.has(list) && list !== "generic_assoc_list")
|
|
185
|
+
paths.push($[listName(list, "start", "start")]);
|
|
186
|
+
return choice(...paths);
|
|
187
|
+
},
|
|
188
|
+
};
|
|
189
|
+
for (const from of states) {
|
|
190
|
+
for (const to of states) {
|
|
191
|
+
if (!reachable(from, to)) continue;
|
|
192
|
+
const context = `${list}_${from}_${to}`;
|
|
193
|
+
Object.assign(
|
|
194
|
+
rules,
|
|
195
|
+
branchRules(context, ($) =>
|
|
196
|
+
from === to ? optional($[`_${context}_body`]) : $[`_${context}_body`],
|
|
197
|
+
),
|
|
198
|
+
{
|
|
199
|
+
[`_${context}_body`]: ($) => listBranch($, list, from, to),
|
|
200
|
+
[`_${context}_unit`]: ($) => {
|
|
201
|
+
const section = groupRef($, context, "if_section");
|
|
202
|
+
return to === "item" && (from === "start" || from === "comma")
|
|
203
|
+
? choice($[item], section)
|
|
204
|
+
: section;
|
|
205
|
+
},
|
|
206
|
+
[`_${context}`]: ($) => listSequence($, list, states, from, to),
|
|
207
|
+
[`_${context}_if_section`]: ($) =>
|
|
208
|
+
listSection($, list, context, from === to),
|
|
209
|
+
},
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
return rules;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function listConflicts($, list) {
|
|
217
|
+
const unchecked = $[`_any_${list}`];
|
|
218
|
+
const states = listStates;
|
|
219
|
+
const conflicts = ["start", "comma"].map((from) => [
|
|
220
|
+
unchecked,
|
|
221
|
+
$[`_${list}_${from}_item_unit`],
|
|
222
|
+
]);
|
|
223
|
+
if (braceLists.has(list))
|
|
224
|
+
conflicts.push([
|
|
225
|
+
$[list],
|
|
226
|
+
$[listName(list, "start", "item")],
|
|
227
|
+
$[listName(list, "start", "comma")],
|
|
228
|
+
]);
|
|
229
|
+
const specifier = {
|
|
230
|
+
generic_assoc_list: $._specifier_qualifiers_if_group,
|
|
231
|
+
initializer_list: $._initializer_if_group,
|
|
232
|
+
}[list];
|
|
233
|
+
for (const kind of ["if_group", "elif_group", "else_group", "if_section"]) {
|
|
234
|
+
const any = $[`_any_${list}_${kind}`];
|
|
235
|
+
const branch = (from, to) => $[`${listName(list, from, to)}_${kind}`];
|
|
236
|
+
// Both parses may still be viable when the checked and unchecked branches end.
|
|
237
|
+
for (const state of states) {
|
|
238
|
+
const same = branch(state, state);
|
|
239
|
+
if (kind === "if_group" && specifier) {
|
|
240
|
+
conflicts.push([specifier, any, same]);
|
|
241
|
+
if (state === "item") conflicts.push([any, same]);
|
|
242
|
+
} else conflicts.push([any, same]);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
const body = $[`_any_${list}_body`];
|
|
246
|
+
const checked = (from, to) => $[`${listName(list, from, to)}_body`];
|
|
247
|
+
const afterItem = [body, checked("item", "item"), checked("item", "comma")];
|
|
248
|
+
conflicts.push([body, checked("item", "comma")]);
|
|
249
|
+
conflicts.push(afterItem);
|
|
250
|
+
return conflicts;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// A branch returns all legal next tokens on its projections. Joining the
|
|
254
|
+
// branches before continuing lets the parser reuse a completed if_section.
|
|
255
|
+
// Zero retains a branch whose separators cannot extend the current prefix.
|
|
256
|
+
function callListGrammar(parameters) {
|
|
257
|
+
const item = 1;
|
|
258
|
+
const comma = 2;
|
|
259
|
+
const end = 4;
|
|
260
|
+
const ellipsis = parameters ? 8 : 0;
|
|
261
|
+
const initial = item | end;
|
|
262
|
+
const afterItem = comma | end;
|
|
263
|
+
const afterComma = item | ellipsis;
|
|
264
|
+
const listName = parameters ? "parameter_list" : "argument_expression_list";
|
|
265
|
+
const itemName = parameters ? "parameter_declaration" : "_assignment";
|
|
266
|
+
const reachableSets = (from) => {
|
|
267
|
+
const states = new Set([from]);
|
|
268
|
+
for (const state of states) {
|
|
269
|
+
states.add(state & item ? afterItem : 0);
|
|
270
|
+
states.add(state & comma ? afterComma : 0);
|
|
271
|
+
if (parameters) states.add(state & ellipsis ? end : 0);
|
|
272
|
+
for (const other of states) states.add(state | other);
|
|
273
|
+
}
|
|
274
|
+
return [...states];
|
|
275
|
+
};
|
|
276
|
+
const states = reachableSets(initial);
|
|
277
|
+
const transitions = Object.fromEntries(
|
|
278
|
+
states.map((state) => [state, reachableSets(state)]),
|
|
279
|
+
);
|
|
280
|
+
const context = (from, to) =>
|
|
281
|
+
`${parameters ? "parameters" : "arguments"}_${from}_${to}`;
|
|
282
|
+
const span = ($, from, to) => $[`_${context(from, to)}`];
|
|
283
|
+
const group = ($, kind, from, to) => groupRef($, context(from, to), kind);
|
|
284
|
+
const branches = ($, from, to) => $[`_${context(from, to)}_branches`];
|
|
285
|
+
const gaps = ($, from) => [
|
|
286
|
+
{ tokens: [], state: from },
|
|
287
|
+
{ tokens: [p($, ",")], state: from & comma ? afterComma : 0 },
|
|
288
|
+
{ tokens: [$._repeated_list_commas], state: 0 },
|
|
289
|
+
];
|
|
290
|
+
const unit = ($, from, to, adjacent = false) => {
|
|
291
|
+
const section = group($, "if_section", from, to);
|
|
292
|
+
if ((from & item ? afterItem : 0) !== to) return section;
|
|
293
|
+
return choice(
|
|
294
|
+
section,
|
|
295
|
+
adjacent && !to ? prec.dynamic(-2, $[itemName]) : $[itemName],
|
|
296
|
+
);
|
|
297
|
+
};
|
|
298
|
+
const fragment = ($, from, to) => {
|
|
299
|
+
const paths = [];
|
|
300
|
+
for (const left of gaps($, from)) {
|
|
301
|
+
if (left.tokens.length && left.state === to) paths.push(...left.tokens);
|
|
302
|
+
for (const middle of transitions[left.state]) {
|
|
303
|
+
const list = alias(span($, left.state, middle), $[listName]);
|
|
304
|
+
for (const right of gaps($, middle))
|
|
305
|
+
if (right.state === to)
|
|
306
|
+
paths.push(seq(...left.tokens, list, ...right.tokens));
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
return choice(...paths);
|
|
310
|
+
};
|
|
311
|
+
// After an explicit ellipsis a path can only remain complete or become
|
|
312
|
+
// invalid. Keep every following section directly in the branch that wrote it.
|
|
313
|
+
const variadicTail = ($, from, to) => {
|
|
314
|
+
const staying = (state) =>
|
|
315
|
+
choice(controlDirective($), group($, "if_section", state, state));
|
|
316
|
+
const invalidItem = choice($.parameter_declaration, p($, ","), p($, "..."));
|
|
317
|
+
const dead = repeat(choice(staying(0), invalidItem));
|
|
318
|
+
if (!from) return dead;
|
|
319
|
+
const complete = repeat(staying(end));
|
|
320
|
+
return to === end
|
|
321
|
+
? complete
|
|
322
|
+
: seq(
|
|
323
|
+
complete,
|
|
324
|
+
choice(group($, "if_section", end, 0), invalidItem),
|
|
325
|
+
dead,
|
|
326
|
+
);
|
|
327
|
+
};
|
|
328
|
+
const body = ($, from, to) => {
|
|
329
|
+
const part = (middle) => $[`_${context(from, middle)}_fragment`];
|
|
330
|
+
const paths = [part(to)];
|
|
331
|
+
if (to === end || !to) {
|
|
332
|
+
for (const middle of transitions[from]) {
|
|
333
|
+
const post = middle & ellipsis ? end : 0;
|
|
334
|
+
if (to && !post) continue;
|
|
335
|
+
const prefix = part(middle);
|
|
336
|
+
paths.push(
|
|
337
|
+
seq(
|
|
338
|
+
from === middle ? optional(prefix) : prefix,
|
|
339
|
+
$[`_variadic_${post}_${to}`],
|
|
340
|
+
),
|
|
341
|
+
);
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
return choice(...paths);
|
|
345
|
+
};
|
|
346
|
+
const accepted = states.filter((state) => state & end);
|
|
347
|
+
const rules = parameters
|
|
348
|
+
? {
|
|
349
|
+
parameter_type_list: ($) =>
|
|
350
|
+
choice(...accepted.map((to) => $[`_${context(initial, to)}_body`])),
|
|
351
|
+
}
|
|
352
|
+
: {
|
|
353
|
+
argument_expression_list: ($) =>
|
|
354
|
+
choice(...accepted.map((to) => span($, initial, to))),
|
|
355
|
+
};
|
|
356
|
+
if (parameters) {
|
|
357
|
+
for (const [from, to] of [
|
|
358
|
+
[end, end],
|
|
359
|
+
[end, 0],
|
|
360
|
+
[0, 0],
|
|
361
|
+
])
|
|
362
|
+
rules[`_variadic_${from}_${to}`] = ($) =>
|
|
363
|
+
seq(
|
|
364
|
+
p($, "..."),
|
|
365
|
+
variadicTail($, from, to),
|
|
366
|
+
optional($._ellipsis_trivia_end),
|
|
367
|
+
);
|
|
368
|
+
}
|
|
369
|
+
for (const from of states) {
|
|
370
|
+
const ends = transitions[from];
|
|
371
|
+
for (const to of ends) {
|
|
372
|
+
const name = context(from, to);
|
|
373
|
+
if (parameters) {
|
|
374
|
+
rules[`_${name}_fragment`] = ($) => fragment($, from, to);
|
|
375
|
+
rules[`_${name}_body`] = ($) => body($, from, to);
|
|
376
|
+
}
|
|
377
|
+
rules[`_${name}`] = ($) => {
|
|
378
|
+
const paths = [];
|
|
379
|
+
for (const middle of ends)
|
|
380
|
+
for (const gap of gaps($, middle)) {
|
|
381
|
+
if (transitions[gap.state].includes(to))
|
|
382
|
+
paths.push(
|
|
383
|
+
seq(
|
|
384
|
+
span($, from, middle),
|
|
385
|
+
...gap.tokens,
|
|
386
|
+
unit($, gap.state, to, !gap.tokens.length),
|
|
387
|
+
),
|
|
388
|
+
);
|
|
389
|
+
}
|
|
390
|
+
return choice(unit($, from, to), ...paths);
|
|
391
|
+
};
|
|
392
|
+
Object.assign(
|
|
393
|
+
rules,
|
|
394
|
+
branchRules(name, ($) => {
|
|
395
|
+
const contents = parameters
|
|
396
|
+
? $[`_${name}_body`]
|
|
397
|
+
: fragment($, from, to);
|
|
398
|
+
return from === to ? optional(contents) : contents;
|
|
399
|
+
}),
|
|
400
|
+
);
|
|
401
|
+
const joined = ($, kind) =>
|
|
402
|
+
ends.flatMap((left) =>
|
|
403
|
+
ends
|
|
404
|
+
.filter((right) => (left | right) === to)
|
|
405
|
+
.map((right) =>
|
|
406
|
+
seq(branches($, from, left), group($, kind, from, right)),
|
|
407
|
+
),
|
|
408
|
+
);
|
|
409
|
+
rules[`_${name}_branches`] = ($) =>
|
|
410
|
+
choice(group($, "if_group", from, to), ...joined($, "elif_group"));
|
|
411
|
+
rules[`_${name}_if_section`] = ($) =>
|
|
412
|
+
seq(
|
|
413
|
+
$._structural_start,
|
|
414
|
+
choice(
|
|
415
|
+
...ends
|
|
416
|
+
.filter((left) => (left | from) === to)
|
|
417
|
+
.map((left) => branches($, from, left)),
|
|
418
|
+
...joined($, "else_group"),
|
|
419
|
+
),
|
|
420
|
+
$.endif_line,
|
|
421
|
+
);
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
return {
|
|
425
|
+
rules,
|
|
426
|
+
specifierConflicts: ($) =>
|
|
427
|
+
states.map((state) => [
|
|
428
|
+
$._specifiers_if_group,
|
|
429
|
+
$[`_${context(state, state)}_if_group`],
|
|
430
|
+
]),
|
|
431
|
+
};
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
const parameterGrammar = callListGrammar(true);
|
|
435
|
+
|
|
436
|
+
const keywordChoice = ($, words) =>
|
|
437
|
+
choice(...words.split(" ").map((word) => kw($, word)));
|
|
438
|
+
const parens = ($, item) => seq(p($, "("), item, p($, ")"));
|
|
439
|
+
|
|
440
|
+
function typeDependentRules() {
|
|
441
|
+
const star = ($) => field("operator", p($, "*"));
|
|
442
|
+
const stars = ($) => repeat(star($));
|
|
443
|
+
const group = ($, rule) => alias(rule, $.parenthesized_group);
|
|
444
|
+
const namedGroup = ($) => group($, $._shared_named_group);
|
|
445
|
+
const suffixes = ($) => repeat($._shared_declarator_suffix);
|
|
446
|
+
const initializer = ($) =>
|
|
447
|
+
optional(seq(field("operator", p($, "=")), $._assignment));
|
|
448
|
+
return {
|
|
449
|
+
_shared_named_group: ($) => parens($, $.declarator_or_expression),
|
|
450
|
+
_shared_array: ($) => seq(p($, "["), $._assignment, p($, "]")),
|
|
451
|
+
_shared_parameters: ($) =>
|
|
452
|
+
separated(
|
|
453
|
+
choice($.type_or_expression, $._shared_named_parameter),
|
|
454
|
+
p($, ","),
|
|
455
|
+
),
|
|
456
|
+
_shared_function: ($) => parens($, optional($._shared_parameters)),
|
|
457
|
+
_shared_declarator_suffix: ($) =>
|
|
458
|
+
choice(
|
|
459
|
+
alias($._shared_array, $.bracketed_group),
|
|
460
|
+
group($, $._shared_function),
|
|
461
|
+
),
|
|
462
|
+
declarator_or_expression: ($) =>
|
|
463
|
+
prec.dynamic(
|
|
464
|
+
1,
|
|
465
|
+
seq(stars($), choice($.identifier, namedGroup($)), suffixes($)),
|
|
466
|
+
),
|
|
467
|
+
_shared_named_tail: ($) =>
|
|
468
|
+
choice(
|
|
469
|
+
seq(star($), $.declarator_or_expression),
|
|
470
|
+
seq(namedGroup($), suffixes($)),
|
|
471
|
+
),
|
|
472
|
+
_shared_named_parameter: ($) => seq($.identifier, $._shared_named_tail),
|
|
473
|
+
_shared_abstract_group: ($) =>
|
|
474
|
+
parens($, choice($._shared_parameters, $._shared_abstract_operand)),
|
|
475
|
+
_shared_empty_group: ($) => seq(p($, "("), p($, ")")),
|
|
476
|
+
_shared_abstract_operand: ($) =>
|
|
477
|
+
seq(stars($), group($, $._shared_abstract_group), suffixes($)),
|
|
478
|
+
type_or_expression: ($) =>
|
|
479
|
+
prec.dynamic(
|
|
480
|
+
1,
|
|
481
|
+
seq(
|
|
482
|
+
$.identifier,
|
|
483
|
+
optional(
|
|
484
|
+
choice(
|
|
485
|
+
seq(
|
|
486
|
+
choice(
|
|
487
|
+
alias($._shared_array, $.bracketed_group),
|
|
488
|
+
group($, $._shared_empty_group),
|
|
489
|
+
group($, $._shared_abstract_group),
|
|
490
|
+
),
|
|
491
|
+
suffixes($),
|
|
492
|
+
),
|
|
493
|
+
seq(star($), $._shared_abstract_operand),
|
|
494
|
+
),
|
|
495
|
+
),
|
|
496
|
+
),
|
|
497
|
+
),
|
|
498
|
+
declaration_or_expression: ($) =>
|
|
499
|
+
prec.dynamic(
|
|
500
|
+
1,
|
|
501
|
+
seq(
|
|
502
|
+
$.identifier,
|
|
503
|
+
optional(
|
|
504
|
+
seq(
|
|
505
|
+
choice(
|
|
506
|
+
seq(star($), $.declarator_or_expression),
|
|
507
|
+
seq(namedGroup($), suffixes($), initializer($)),
|
|
508
|
+
),
|
|
509
|
+
repeat(
|
|
510
|
+
seq(p($, ","), $.declarator_or_expression, initializer($)),
|
|
511
|
+
),
|
|
512
|
+
),
|
|
513
|
+
),
|
|
514
|
+
),
|
|
515
|
+
),
|
|
516
|
+
declaration_or_expression_statement: ($) =>
|
|
517
|
+
seq($.declaration_or_expression, p($, ";")),
|
|
518
|
+
_cast_operand_group: ($) => parens($, separated($._assignment, p($, ","))),
|
|
519
|
+
_parenthesized_cast_operand: ($) =>
|
|
520
|
+
choice(
|
|
521
|
+
group($, $._cast_operand_group),
|
|
522
|
+
alias($._postfix_cast_operand, $.postfix_expression),
|
|
523
|
+
),
|
|
524
|
+
_postfix_cast_operand: ($) =>
|
|
525
|
+
seq(field("base", $._parenthesized_cast_operand), $._postfix_suffix),
|
|
526
|
+
cast_or_expression: ($) =>
|
|
527
|
+
prec.dynamic(
|
|
528
|
+
1,
|
|
529
|
+
seq(
|
|
530
|
+
parens($, $.type_or_expression),
|
|
531
|
+
repeat(field("operator", oneOf($, ["++", "--"]))),
|
|
532
|
+
choice(
|
|
533
|
+
seq(
|
|
534
|
+
field("operator", oneOf($, ["+", "-", "*", "&"])),
|
|
535
|
+
field("operand", $._cast),
|
|
536
|
+
),
|
|
537
|
+
field("operand", $._parenthesized_cast_operand),
|
|
538
|
+
),
|
|
539
|
+
),
|
|
540
|
+
),
|
|
541
|
+
named_or_abstract_declarator: ($) =>
|
|
542
|
+
prec.dynamic(
|
|
543
|
+
1,
|
|
544
|
+
seq(
|
|
545
|
+
optional($.pointer),
|
|
546
|
+
group($, $._parameter_declarator_group),
|
|
547
|
+
repeat($._declarator_suffix),
|
|
548
|
+
),
|
|
549
|
+
),
|
|
550
|
+
_parameter_declarator_group: ($) =>
|
|
551
|
+
parens(
|
|
552
|
+
$,
|
|
553
|
+
choice(
|
|
554
|
+
seq($.identifier, repeat($._declarator_suffix)),
|
|
555
|
+
$.named_or_abstract_declarator,
|
|
556
|
+
),
|
|
557
|
+
),
|
|
558
|
+
};
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
function conditionalOperandRules(context, expression) {
|
|
562
|
+
return conditionalRules(context, ($) =>
|
|
563
|
+
singleValue(
|
|
564
|
+
$,
|
|
565
|
+
choice(
|
|
566
|
+
$.type_name,
|
|
567
|
+
$.type_or_expression,
|
|
568
|
+
$[expression],
|
|
569
|
+
groupRef($, context, "if_section"),
|
|
570
|
+
),
|
|
571
|
+
),
|
|
572
|
+
);
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
function quotedLiteral($, quote, opening, content) {
|
|
576
|
+
const unit = choice(content, $.escape_sequence);
|
|
577
|
+
return seq(
|
|
578
|
+
optional(seq($.encoding_prefix, repeat($.line_splice))),
|
|
579
|
+
alias(opening, quote),
|
|
580
|
+
...(quote === "'" ? [repeat($.line_splice), unit] : []),
|
|
581
|
+
repeat(choice(unit, $.line_splice)),
|
|
582
|
+
alias($._literal_close, quote),
|
|
583
|
+
);
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
function macroCandidate($, start) {
|
|
587
|
+
return seq(
|
|
588
|
+
field("name", $._candidate_base),
|
|
589
|
+
p($, "("),
|
|
590
|
+
start,
|
|
591
|
+
optional(field("arguments", $.macro_argument_list)),
|
|
592
|
+
alias($._candidate_close, ")"),
|
|
593
|
+
);
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
function numberRules() {
|
|
597
|
+
const following = ($, part) =>
|
|
598
|
+
seq(repeat(alias($._number_splice, $.line_splice)), part);
|
|
599
|
+
const body = ($, digits) =>
|
|
600
|
+
choice(digits, seq($.base_prefix, following($, digits)));
|
|
601
|
+
return {
|
|
602
|
+
integer_constant: ($) =>
|
|
603
|
+
seq(body($, $.integer_digits), optional(following($, $.integer_suffix))),
|
|
604
|
+
floating_constant: ($) =>
|
|
605
|
+
seq(
|
|
606
|
+
body($, $.significand),
|
|
607
|
+
optional(following($, $.exponent)),
|
|
608
|
+
optional(following($, $.floating_suffix)),
|
|
609
|
+
),
|
|
610
|
+
significand: ($) => {
|
|
611
|
+
// A new pp-number can start after whitespace inside a macro argument.
|
|
612
|
+
// Only continuation tokens may extend the current significand.
|
|
613
|
+
const splices = repeat(alias($._significand_splice, $.line_splice));
|
|
614
|
+
const fraction = seq(
|
|
615
|
+
splices,
|
|
616
|
+
alias($._fraction_digits, $.significand_digits),
|
|
617
|
+
);
|
|
618
|
+
return choice(
|
|
619
|
+
seq(
|
|
620
|
+
$.significand_digits,
|
|
621
|
+
optional(seq(splices, $.decimal_point, optional(fraction))),
|
|
622
|
+
),
|
|
623
|
+
seq(alias($._leading_decimal_point, $.decimal_point), fraction),
|
|
624
|
+
);
|
|
625
|
+
},
|
|
626
|
+
exponent: ($) =>
|
|
627
|
+
seq(
|
|
628
|
+
$.exponent_marker,
|
|
629
|
+
optional(following($, $.exponent_sign)),
|
|
630
|
+
following($, $.exponent_digits),
|
|
631
|
+
),
|
|
632
|
+
};
|
|
633
|
+
}
|
|
634
|
+
const braceList = ($, list, label = "") => {
|
|
635
|
+
const owner = (rule) => (label ? field(label, rule) : rule);
|
|
636
|
+
return seq(
|
|
637
|
+
p($, "{"),
|
|
638
|
+
choice(
|
|
639
|
+
seq(owner($[list]), optional(p($, ","))),
|
|
640
|
+
owner(listNode($, list, "start", "comma")),
|
|
641
|
+
),
|
|
642
|
+
p($, "}"),
|
|
643
|
+
);
|
|
644
|
+
};
|
|
645
|
+
function arraySuffix($, abstract) {
|
|
646
|
+
return seq(
|
|
647
|
+
p($, "["),
|
|
648
|
+
choice(
|
|
649
|
+
seq(optional($.type_qualifier_list), optional($._assignment)),
|
|
650
|
+
seq(kw($, "static"), optional($.type_qualifier_list), $._assignment),
|
|
651
|
+
seq($.type_qualifier_list, kw($, "static"), $._assignment),
|
|
652
|
+
abstract ? p($, "*") : seq(optional($.type_qualifier_list), p($, "*")),
|
|
653
|
+
),
|
|
654
|
+
p($, "]"),
|
|
655
|
+
);
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
function binaryRules() {
|
|
659
|
+
const levels = Object.entries({
|
|
660
|
+
multiplicative: ["*", "/", "%"],
|
|
661
|
+
additive: ["+", "-"],
|
|
662
|
+
shift: ["<<", ">>"],
|
|
663
|
+
relational: ["<", ">", "<=", ">="],
|
|
664
|
+
equality: ["==", "!="],
|
|
665
|
+
and: ["&"],
|
|
666
|
+
exclusive_or: ["^"],
|
|
667
|
+
inclusive_or: ["|"],
|
|
668
|
+
logical_and: ["&&"],
|
|
669
|
+
logical_or: ["||"],
|
|
670
|
+
});
|
|
671
|
+
return Object.fromEntries(
|
|
672
|
+
levels.flatMap(([level, operators], index) => {
|
|
673
|
+
const name = `${level}_expression`;
|
|
674
|
+
const hidden = `_${level}`;
|
|
675
|
+
const tighter = index === 0 ? "_cast" : `_${levels[index - 1][0]}`;
|
|
676
|
+
return [
|
|
677
|
+
[hidden, ($) => choice($[tighter], $[name])],
|
|
678
|
+
[name, ($) => binary($[hidden], oneOf($, operators), $[tighter])],
|
|
679
|
+
];
|
|
680
|
+
}),
|
|
681
|
+
);
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
// An open statement ends in an if that can still take an else (C17 6.8.4.1).
|
|
685
|
+
function statementRules(open) {
|
|
686
|
+
const name = (base) => (open ? `_open_${base}` : base);
|
|
687
|
+
const subordinate = ($, role) =>
|
|
688
|
+
field(role, open ? $._open_statement : $._closed_body);
|
|
689
|
+
return {
|
|
690
|
+
[name("labeled_statement")]: ($) =>
|
|
691
|
+
seq(
|
|
692
|
+
choice(
|
|
693
|
+
field("label", $.identifier),
|
|
694
|
+
seq(kw($, "case"), field("label", $.constant_expression)),
|
|
695
|
+
kw($, "default"),
|
|
696
|
+
),
|
|
697
|
+
p($, ":"),
|
|
698
|
+
subordinate($, "body"),
|
|
699
|
+
),
|
|
700
|
+
[name("selection_statement")]: ($) => {
|
|
701
|
+
const head = seq(kw($, "if"), $._parenthesized_condition);
|
|
702
|
+
const paired = seq(
|
|
703
|
+
head,
|
|
704
|
+
field("consequence", $._closed_body),
|
|
705
|
+
repeat(controlDirective($)),
|
|
706
|
+
kw($, "else"),
|
|
707
|
+
subordinate($, "alternative"),
|
|
708
|
+
);
|
|
709
|
+
const selection = seq(
|
|
710
|
+
kw($, "switch"),
|
|
711
|
+
$._parenthesized_condition,
|
|
712
|
+
subordinate($, "body"),
|
|
713
|
+
);
|
|
714
|
+
return open
|
|
715
|
+
? choice(paired, selection, seq(head, field("consequence", $._body)))
|
|
716
|
+
: choice(paired, selection);
|
|
717
|
+
},
|
|
718
|
+
[name("iteration_statement")]: ($) => {
|
|
719
|
+
const loop = seq($._iteration_header, subordinate($, "body"));
|
|
720
|
+
if (open) return loop;
|
|
721
|
+
return choice(
|
|
722
|
+
loop,
|
|
723
|
+
seq(
|
|
724
|
+
kw($, "do"),
|
|
725
|
+
field("body", $._body),
|
|
726
|
+
repeat(controlDirective($)),
|
|
727
|
+
kw($, "while"),
|
|
728
|
+
$._parenthesized_condition,
|
|
729
|
+
p($, ";"),
|
|
730
|
+
),
|
|
731
|
+
);
|
|
732
|
+
},
|
|
733
|
+
};
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
function controlLine($, marker) {
|
|
737
|
+
return seq(
|
|
738
|
+
alias(marker, "#"),
|
|
739
|
+
optional(
|
|
740
|
+
choice(
|
|
741
|
+
seq(
|
|
742
|
+
pp($, "define"),
|
|
743
|
+
field("name", $.identifier),
|
|
744
|
+
optional(field("parameters", $.macro_parameters)),
|
|
745
|
+
optional(field("replacement", $.replacement_list)),
|
|
746
|
+
),
|
|
747
|
+
seq(
|
|
748
|
+
pp($, "include"),
|
|
749
|
+
field("argument", choice($.header_name, $.pp_tokens)),
|
|
750
|
+
),
|
|
751
|
+
seq(pp($, "undef"), field("name", $.identifier)),
|
|
752
|
+
seq(pp($, "line"), $.pp_tokens),
|
|
753
|
+
seq(choice(pp($, "pragma"), pp($, "error")), optional($.pp_tokens)),
|
|
754
|
+
),
|
|
755
|
+
),
|
|
756
|
+
$._directive_end,
|
|
757
|
+
);
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
const nonDirective = ($, marker) =>
|
|
761
|
+
seq(alias(marker, "#"), $.pp_tokens, $._directive_end);
|
|
762
|
+
|
|
763
|
+
export default grammar({
|
|
764
|
+
name: "c",
|
|
765
|
+
externals: ($) => externals.map((id) => $[id]),
|
|
766
|
+
extras: ($) => [
|
|
767
|
+
$._layout,
|
|
768
|
+
$.comment,
|
|
769
|
+
$.pragma_operator,
|
|
770
|
+
$.control_line,
|
|
771
|
+
$.non_directive,
|
|
772
|
+
$.if_section,
|
|
773
|
+
$._unmatchable,
|
|
774
|
+
],
|
|
775
|
+
conflicts: ($) => [
|
|
776
|
+
...Object.keys(listItems).flatMap((list) => listConflicts($, list)),
|
|
777
|
+
[$.macro_invocation_candidate, $.postfix_expression],
|
|
778
|
+
[$._candidate_expressions, $.macro_argument],
|
|
779
|
+
[$.unary_expression, $.postfix_expression],
|
|
780
|
+
...parameterGrammar.specifierConflicts($),
|
|
781
|
+
[$.identifier, $._candidate_name],
|
|
782
|
+
[
|
|
783
|
+
$.type_or_expression,
|
|
784
|
+
$.typedef_name,
|
|
785
|
+
$._macro_argument_token,
|
|
786
|
+
$.primary_expression,
|
|
787
|
+
],
|
|
788
|
+
[$.typedef_name, $._macro_argument_token],
|
|
789
|
+
[$.struct_or_union_specifier, $._macro_argument_token],
|
|
790
|
+
[$.enum_specifier, $._macro_argument_token],
|
|
791
|
+
[$.atomic_type_specifier, $._macro_argument_token],
|
|
792
|
+
[$.type_qualifier, $._macro_argument_token],
|
|
793
|
+
[$.type_specifier, $._macro_argument_token],
|
|
794
|
+
[$._macro_argument_token, $.primary_expression],
|
|
795
|
+
[$._macro_argument_token, $.generic_selection],
|
|
796
|
+
[$.unary_expression, $._macro_argument_token],
|
|
797
|
+
[$.and_expression, $.exclusive_or_expression],
|
|
798
|
+
[$.additive_expression, $.shift_expression],
|
|
799
|
+
[$.multiplicative_expression, $.additive_expression],
|
|
800
|
+
[$.declarator, $.abstract_declarator],
|
|
801
|
+
[$.primary_expression],
|
|
802
|
+
[$.and_expression, $._exclusive_or],
|
|
803
|
+
[$.additive_expression, $._shift],
|
|
804
|
+
[$._unary, $.postfix_expression],
|
|
805
|
+
[$.multiplicative_expression, $._additive],
|
|
806
|
+
[$.parameter_declaration],
|
|
807
|
+
[$.struct_declarator],
|
|
808
|
+
[$.declarator, $.direct_declarator],
|
|
809
|
+
...typeArgumentMacros.map((name) => [
|
|
810
|
+
$.identifier,
|
|
811
|
+
$._candidate_name,
|
|
812
|
+
$[`_${name}_callee`],
|
|
813
|
+
]),
|
|
814
|
+
[$.specifier_qualifier_list, $._specifier_qualifier],
|
|
815
|
+
[$.declaration_specifiers, $._declaration_specifier],
|
|
816
|
+
[$._postfix_cast_operand, $.cast_or_expression],
|
|
817
|
+
[$._expression_if_group, $._sizeof_operand_if_group],
|
|
818
|
+
[$._type_argument_if_group, $._specifier_qualifiers_if_group],
|
|
819
|
+
[
|
|
820
|
+
$._expression_if_group,
|
|
821
|
+
$._sizeof_operand_if_group,
|
|
822
|
+
$._specifier_qualifiers_if_group,
|
|
823
|
+
],
|
|
824
|
+
[$._alignment_operand_if_group, $._specifier_qualifiers_if_group],
|
|
825
|
+
[$._parameter_declarator_group, $.typedef_name, $.direct_declarator],
|
|
826
|
+
[$._shared_function, $._shared_abstract_group],
|
|
827
|
+
[$._shared_function, $._shared_empty_group],
|
|
828
|
+
[$._shared_declarator_suffix, $.type_or_expression],
|
|
829
|
+
[$.type_or_expression, $.typedef_name],
|
|
830
|
+
[$.declarator_or_expression, $.type_or_expression],
|
|
831
|
+
[$.declaration_or_expression, $.typedef_name, $.primary_expression],
|
|
832
|
+
[$.type_or_expression, $.typedef_name, $.primary_expression],
|
|
833
|
+
[$.typedef_name, $.primary_expression],
|
|
834
|
+
[$.declaration_specifiers],
|
|
835
|
+
[$.specifier_qualifier_list],
|
|
836
|
+
[$.type_qualifier_list],
|
|
837
|
+
[$.pointer],
|
|
838
|
+
[$.init_declarator, $.function_definition],
|
|
839
|
+
[$._body, $.selection_statement, $._open_selection_statement],
|
|
840
|
+
[$._external_if_group, $._specifiers_if_group],
|
|
841
|
+
[$._external_item, $._specifiers_if_group],
|
|
842
|
+
[$.declaration_specifiers, $._specifiers_if_group],
|
|
843
|
+
[$._specifiers_if_group, $._old_declarations_if_group],
|
|
844
|
+
[$.specifier_qualifier_list, $._specifier_qualifiers_if_group],
|
|
845
|
+
[$._specifier_qualifiers_if_group, $._member_if_group],
|
|
846
|
+
[$._specifier_qualifiers_if_group, $._any_generic_assoc_list_if_group],
|
|
847
|
+
[$._initializer_entry, $._initializer_if_group],
|
|
848
|
+
[$._initializer_if_group, $._any_initializer_list_if_group],
|
|
849
|
+
[$._statement_if_group, $._expression_if_group],
|
|
850
|
+
[$._expression_if_group, $._specifier_qualifiers_if_group],
|
|
851
|
+
[$._expression_if_group, $._specifiers_if_group],
|
|
852
|
+
[$._expression_if_group, $._specifiers_if_group, $._block_if_group],
|
|
853
|
+
[$._block_item, $._expression_if_group],
|
|
854
|
+
[$._block_item, $._specifiers_if_group],
|
|
855
|
+
[$._block_item, $._expression_if_group, $._specifiers_if_group],
|
|
856
|
+
],
|
|
857
|
+
|
|
858
|
+
rules: {
|
|
859
|
+
translation_unit: ($) => repeat($._external_item),
|
|
860
|
+
identifier: identifierSpelling,
|
|
861
|
+
...typeDependentRules(),
|
|
862
|
+
...numberRules(),
|
|
863
|
+
_unmatchable: () => token(seq(/[\s\S]/, /[^\s\S]/)),
|
|
864
|
+
_external_item: ($) =>
|
|
865
|
+
choice(
|
|
866
|
+
$.external_declaration,
|
|
867
|
+
seq(
|
|
868
|
+
alias($._external_candidate, $.macro_invocation_candidate),
|
|
869
|
+
optional(p($, ";")),
|
|
870
|
+
),
|
|
871
|
+
directive($, "external"),
|
|
872
|
+
),
|
|
873
|
+
external_declaration: ($) => choice($.declaration, $.function_definition),
|
|
874
|
+
declaration: ($) =>
|
|
875
|
+
choice(
|
|
876
|
+
seq(
|
|
877
|
+
field("specifiers", $.declaration_specifiers),
|
|
878
|
+
optional(field("declarators", $.init_declarator_list)),
|
|
879
|
+
p($, ";"),
|
|
880
|
+
),
|
|
881
|
+
$.static_assert_declaration,
|
|
882
|
+
),
|
|
883
|
+
declaration_specifiers: ($) =>
|
|
884
|
+
declarationSpecifiers(
|
|
885
|
+
$,
|
|
886
|
+
"specifiers",
|
|
887
|
+
$._declaration_modifier,
|
|
888
|
+
$._declaration_specifier,
|
|
889
|
+
),
|
|
890
|
+
_declaration_specifier: ($) =>
|
|
891
|
+
choice($.type_specifier, $._declaration_modifier),
|
|
892
|
+
_declaration_modifier: ($) =>
|
|
893
|
+
choice(
|
|
894
|
+
$.type_qualifier,
|
|
895
|
+
$.storage_class_specifier,
|
|
896
|
+
$.function_specifier,
|
|
897
|
+
$.alignment_specifier,
|
|
898
|
+
),
|
|
899
|
+
storage_class_specifier: ($) =>
|
|
900
|
+
keywordChoice($, "typedef extern static _Thread_local auto register"),
|
|
901
|
+
type_specifier: ($) =>
|
|
902
|
+
choice(
|
|
903
|
+
keywordChoice(
|
|
904
|
+
$,
|
|
905
|
+
"void char short int long float double signed unsigned _Bool _Complex _Imaginary",
|
|
906
|
+
),
|
|
907
|
+
$.struct_or_union_specifier,
|
|
908
|
+
$.enum_specifier,
|
|
909
|
+
$.atomic_type_specifier,
|
|
910
|
+
$.typedef_name,
|
|
911
|
+
),
|
|
912
|
+
typedef_name: ($) => prec.dynamic(-1, $.identifier),
|
|
913
|
+
type_qualifier: ($) => keywordChoice($, "const restrict volatile _Atomic"),
|
|
914
|
+
type_qualifier_list: ($) => specifiers($, "qualifiers", $.type_qualifier),
|
|
915
|
+
function_specifier: ($) => keywordChoice($, "inline _Noreturn"),
|
|
916
|
+
alignment_specifier: ($) =>
|
|
917
|
+
seq(
|
|
918
|
+
kw($, "_Alignas"),
|
|
919
|
+
parens(
|
|
920
|
+
$,
|
|
921
|
+
choice(
|
|
922
|
+
$.type_name,
|
|
923
|
+
$.constant_expression,
|
|
924
|
+
$.type_or_expression,
|
|
925
|
+
groupRef($, "alignment_operand", "if_section"),
|
|
926
|
+
),
|
|
927
|
+
),
|
|
928
|
+
),
|
|
929
|
+
atomic_type_specifier: ($) =>
|
|
930
|
+
seq(kw($, "_Atomic"), alias($._atomic_open, "("), $.type_name, p($, ")")),
|
|
931
|
+
struct_or_union_specifier: ($) =>
|
|
932
|
+
seq(
|
|
933
|
+
keywordChoice($, "struct union"),
|
|
934
|
+
choice(
|
|
935
|
+
field("tag", $.identifier),
|
|
936
|
+
seq(
|
|
937
|
+
optional(field("tag", $.identifier)),
|
|
938
|
+
p($, "{"),
|
|
939
|
+
$.struct_declaration_list,
|
|
940
|
+
p($, "}"),
|
|
941
|
+
),
|
|
942
|
+
),
|
|
943
|
+
),
|
|
944
|
+
struct_declaration_list: ($) =>
|
|
945
|
+
repeat1(choice($.struct_declaration, directive($, "member"))),
|
|
946
|
+
struct_declaration: ($) =>
|
|
947
|
+
choice(
|
|
948
|
+
$.static_assert_declaration,
|
|
949
|
+
seq(
|
|
950
|
+
$.specifier_qualifier_list,
|
|
951
|
+
optional($.struct_declarator_list),
|
|
952
|
+
p($, ";"),
|
|
953
|
+
),
|
|
954
|
+
),
|
|
955
|
+
struct_declarator: ($) =>
|
|
956
|
+
choice(
|
|
957
|
+
$.declarator,
|
|
958
|
+
seq(
|
|
959
|
+
optional($.declarator),
|
|
960
|
+
p($, ":"),
|
|
961
|
+
field("width", $.constant_expression),
|
|
962
|
+
),
|
|
963
|
+
),
|
|
964
|
+
enum_specifier: ($) =>
|
|
965
|
+
seq(
|
|
966
|
+
kw($, "enum"),
|
|
967
|
+
choice(
|
|
968
|
+
field("tag", $.identifier),
|
|
969
|
+
seq(
|
|
970
|
+
optional(field("tag", $.identifier)),
|
|
971
|
+
braceList($, "enumerator_list"),
|
|
972
|
+
),
|
|
973
|
+
),
|
|
974
|
+
),
|
|
975
|
+
enumerator: ($) =>
|
|
976
|
+
seq(
|
|
977
|
+
field("name", $.identifier),
|
|
978
|
+
optional(seq(p($, "="), field("value", $.constant_expression))),
|
|
979
|
+
),
|
|
980
|
+
static_assert_declaration: ($) =>
|
|
981
|
+
seq(
|
|
982
|
+
kw($, "_Static_assert"),
|
|
983
|
+
parens(
|
|
984
|
+
$,
|
|
985
|
+
seq($.constant_expression, p($, ","), repeat1($.string_literal)),
|
|
986
|
+
),
|
|
987
|
+
p($, ";"),
|
|
988
|
+
),
|
|
989
|
+
init_declarator: ($) =>
|
|
990
|
+
seq(
|
|
991
|
+
field("declarator", $.declarator),
|
|
992
|
+
optional(seq(p($, "="), field("initializer", $.initializer))),
|
|
993
|
+
),
|
|
994
|
+
declarator: ($) => seq(optional($.pointer), $.direct_declarator),
|
|
995
|
+
pointer: ($) =>
|
|
996
|
+
seq(p($, "*"), optional($.type_qualifier_list), optional($.pointer)),
|
|
997
|
+
direct_declarator: ($) =>
|
|
998
|
+
choice(
|
|
999
|
+
$.identifier,
|
|
1000
|
+
parens($, $.declarator),
|
|
1001
|
+
seq($.direct_declarator, $._declarator_suffix),
|
|
1002
|
+
),
|
|
1003
|
+
_declarator_suffix: ($) =>
|
|
1004
|
+
choice(
|
|
1005
|
+
arraySuffix($, false),
|
|
1006
|
+
seq(
|
|
1007
|
+
alias($._p_lparen, "("),
|
|
1008
|
+
optional(field("parameters", $.parameter_type_list)),
|
|
1009
|
+
p($, ")"),
|
|
1010
|
+
),
|
|
1011
|
+
seq(
|
|
1012
|
+
alias($._identifier_list_open, "("),
|
|
1013
|
+
field("parameters", $.parameter_identifiers),
|
|
1014
|
+
p($, ")"),
|
|
1015
|
+
),
|
|
1016
|
+
),
|
|
1017
|
+
...uncheckedListRules("parameter_identifiers", "identifier"),
|
|
1018
|
+
parameter_identifiers: ($) => $._any_parameter_identifiers,
|
|
1019
|
+
parameter_declaration: ($) =>
|
|
1020
|
+
seq(
|
|
1021
|
+
field("specifiers", $.declaration_specifiers),
|
|
1022
|
+
optional(
|
|
1023
|
+
field(
|
|
1024
|
+
"declarator",
|
|
1025
|
+
choice(
|
|
1026
|
+
$.declarator,
|
|
1027
|
+
$.abstract_declarator,
|
|
1028
|
+
$.named_or_abstract_declarator,
|
|
1029
|
+
),
|
|
1030
|
+
),
|
|
1031
|
+
),
|
|
1032
|
+
),
|
|
1033
|
+
abstract_declarator: ($) =>
|
|
1034
|
+
choice($.pointer, seq(optional($.pointer), $.direct_abstract_declarator)),
|
|
1035
|
+
direct_abstract_declarator: ($) =>
|
|
1036
|
+
choice(
|
|
1037
|
+
parens($, $.abstract_declarator),
|
|
1038
|
+
seq(
|
|
1039
|
+
optional($.direct_abstract_declarator),
|
|
1040
|
+
choice(
|
|
1041
|
+
arraySuffix($, true),
|
|
1042
|
+
parens($, optional(field("parameters", $.parameter_type_list))),
|
|
1043
|
+
),
|
|
1044
|
+
),
|
|
1045
|
+
),
|
|
1046
|
+
type_name: ($) =>
|
|
1047
|
+
seq($.specifier_qualifier_list, optional($.abstract_declarator)),
|
|
1048
|
+
specifier_qualifier_list: ($) =>
|
|
1049
|
+
declarationSpecifiers(
|
|
1050
|
+
$,
|
|
1051
|
+
"specifier_qualifiers",
|
|
1052
|
+
$.type_qualifier,
|
|
1053
|
+
$._specifier_qualifier,
|
|
1054
|
+
),
|
|
1055
|
+
_specifier_qualifier: ($) => choice($.type_specifier, $.type_qualifier),
|
|
1056
|
+
initializer: ($) =>
|
|
1057
|
+
choice(
|
|
1058
|
+
$._assignment,
|
|
1059
|
+
braceList($, "initializer_list"),
|
|
1060
|
+
prec.dynamic(-1, groupRef($, "initializer", "if_section")),
|
|
1061
|
+
),
|
|
1062
|
+
_initializer_entry: ($) => seq(optional($.designation), $.initializer),
|
|
1063
|
+
designation: ($) => seq($.designator_list, p($, "=")),
|
|
1064
|
+
designator_list: ($) => repeat1($.designator),
|
|
1065
|
+
designator: ($) =>
|
|
1066
|
+
choice(
|
|
1067
|
+
seq(p($, "["), $.constant_expression, p($, "]")),
|
|
1068
|
+
seq(p($, "."), $.identifier),
|
|
1069
|
+
),
|
|
1070
|
+
function_definition: ($) =>
|
|
1071
|
+
seq(
|
|
1072
|
+
field("specifiers", $.declaration_specifiers),
|
|
1073
|
+
field("declarator", $.declarator),
|
|
1074
|
+
optional(field("declarations", $.declaration_list)),
|
|
1075
|
+
field("body", $.compound_statement),
|
|
1076
|
+
),
|
|
1077
|
+
declaration_list: ($) =>
|
|
1078
|
+
repeat1(
|
|
1079
|
+
choice($.declaration, groupRef($, "old_declarations", "if_section")),
|
|
1080
|
+
),
|
|
1081
|
+
compound_statement: ($) => seq(p($, "{"), repeat($._block_item), p($, "}")),
|
|
1082
|
+
_block_item: ($) =>
|
|
1083
|
+
choice(
|
|
1084
|
+
$.declaration,
|
|
1085
|
+
$.declaration_or_expression_statement,
|
|
1086
|
+
$._statement,
|
|
1087
|
+
directive($, "block"),
|
|
1088
|
+
),
|
|
1089
|
+
_statement: ($) => choice($._closed_statement, $._open_statement),
|
|
1090
|
+
_closed_statement: ($) =>
|
|
1091
|
+
choice(
|
|
1092
|
+
$.labeled_statement,
|
|
1093
|
+
$.compound_statement,
|
|
1094
|
+
$.expression_statement,
|
|
1095
|
+
$.selection_statement,
|
|
1096
|
+
$.iteration_statement,
|
|
1097
|
+
$.jump_statement,
|
|
1098
|
+
),
|
|
1099
|
+
_open_statement: ($) =>
|
|
1100
|
+
choice(
|
|
1101
|
+
alias($._open_labeled_statement, $.labeled_statement),
|
|
1102
|
+
alias($._open_selection_statement, $.selection_statement),
|
|
1103
|
+
alias($._open_iteration_statement, $.iteration_statement),
|
|
1104
|
+
),
|
|
1105
|
+
_closed_body: ($) =>
|
|
1106
|
+
choice($._closed_statement, groupRef($, "statement", "if_section")),
|
|
1107
|
+
_body: ($) => choice($._closed_body, $._open_statement),
|
|
1108
|
+
_parenthesized_condition: ($) =>
|
|
1109
|
+
parens($, field("condition", $._expression)),
|
|
1110
|
+
_iteration_header: ($) =>
|
|
1111
|
+
choice(
|
|
1112
|
+
seq(kw($, "while"), $._parenthesized_condition),
|
|
1113
|
+
seq(
|
|
1114
|
+
kw($, "for"),
|
|
1115
|
+
parens(
|
|
1116
|
+
$,
|
|
1117
|
+
seq(
|
|
1118
|
+
choice(
|
|
1119
|
+
field("initializer", $.declaration),
|
|
1120
|
+
seq(
|
|
1121
|
+
field("initializer", $.declaration_or_expression),
|
|
1122
|
+
p($, ";"),
|
|
1123
|
+
),
|
|
1124
|
+
seq(optional(field("initializer", $._expression)), p($, ";")),
|
|
1125
|
+
),
|
|
1126
|
+
optional(field("condition", $._expression)),
|
|
1127
|
+
p($, ";"),
|
|
1128
|
+
optional(field("update", $._expression)),
|
|
1129
|
+
),
|
|
1130
|
+
),
|
|
1131
|
+
),
|
|
1132
|
+
),
|
|
1133
|
+
...statementRules(false),
|
|
1134
|
+
...statementRules(true),
|
|
1135
|
+
jump_statement: ($) =>
|
|
1136
|
+
seq(
|
|
1137
|
+
choice(
|
|
1138
|
+
seq(kw($, "goto"), field("label", $.identifier)),
|
|
1139
|
+
kw($, "continue"),
|
|
1140
|
+
kw($, "break"),
|
|
1141
|
+
seq(kw($, "return"), optional(field("value", $._expression))),
|
|
1142
|
+
),
|
|
1143
|
+
p($, ";"),
|
|
1144
|
+
),
|
|
1145
|
+
expression_statement: ($) => seq(optional($._expression), p($, ";")),
|
|
1146
|
+
_expression: ($) =>
|
|
1147
|
+
choice(
|
|
1148
|
+
$._assignment,
|
|
1149
|
+
$.comma_expression,
|
|
1150
|
+
prec.dynamic(-1, groupRef($, "expression", "if_section")),
|
|
1151
|
+
),
|
|
1152
|
+
comma_expression: ($) => binary($._expression, p($, ","), $._assignment),
|
|
1153
|
+
_assignment: ($) => choice($._conditional, $.assignment_expression),
|
|
1154
|
+
assignment_expression: ($) =>
|
|
1155
|
+
binary(
|
|
1156
|
+
$._unary,
|
|
1157
|
+
oneOf($, [
|
|
1158
|
+
"=",
|
|
1159
|
+
"*=",
|
|
1160
|
+
"/=",
|
|
1161
|
+
"%=",
|
|
1162
|
+
"+=",
|
|
1163
|
+
"-=",
|
|
1164
|
+
"<<=",
|
|
1165
|
+
">>=",
|
|
1166
|
+
"&=",
|
|
1167
|
+
"^=",
|
|
1168
|
+
"|=",
|
|
1169
|
+
]),
|
|
1170
|
+
$._assignment,
|
|
1171
|
+
),
|
|
1172
|
+
_conditional: ($) => choice($._logical_or, $.conditional_expression),
|
|
1173
|
+
conditional_expression: ($) =>
|
|
1174
|
+
seq(
|
|
1175
|
+
field("condition", $._logical_or),
|
|
1176
|
+
p($, "?"),
|
|
1177
|
+
field("consequence", $._expression),
|
|
1178
|
+
p($, ":"),
|
|
1179
|
+
field("alternative", $._conditional),
|
|
1180
|
+
),
|
|
1181
|
+
constant_expression: ($) => $._conditional,
|
|
1182
|
+
...binaryRules(),
|
|
1183
|
+
_cast: ($) => choice($._unary, $.cast_expression, $.cast_or_expression),
|
|
1184
|
+
cast_expression: ($) =>
|
|
1185
|
+
seq(parens($, field("type", $.type_name)), field("operand", $._cast)),
|
|
1186
|
+
_unary: ($) => choice($._postfix, $.unary_expression),
|
|
1187
|
+
unary_expression: ($) =>
|
|
1188
|
+
choice(
|
|
1189
|
+
seq(
|
|
1190
|
+
field("operator", oneOf($, ["++", "--"])),
|
|
1191
|
+
field("operand", $._unary),
|
|
1192
|
+
),
|
|
1193
|
+
seq(
|
|
1194
|
+
field("operator", oneOf($, ["&", "*", "+", "-", "~", "!"])),
|
|
1195
|
+
field("operand", $._cast),
|
|
1196
|
+
),
|
|
1197
|
+
seq(kw($, "sizeof"), field("operand", $._unary)),
|
|
1198
|
+
seq(
|
|
1199
|
+
kw($, "sizeof"),
|
|
1200
|
+
parens(
|
|
1201
|
+
$,
|
|
1202
|
+
field(
|
|
1203
|
+
"argument",
|
|
1204
|
+
choice(
|
|
1205
|
+
$.type_or_expression,
|
|
1206
|
+
groupRef($, "sizeof_operand", "if_section"),
|
|
1207
|
+
),
|
|
1208
|
+
),
|
|
1209
|
+
),
|
|
1210
|
+
),
|
|
1211
|
+
seq(
|
|
1212
|
+
keywordChoice($, "sizeof _Alignof"),
|
|
1213
|
+
parens($, field("type", $.type_name)),
|
|
1214
|
+
),
|
|
1215
|
+
),
|
|
1216
|
+
_postfix: ($) =>
|
|
1217
|
+
choice(
|
|
1218
|
+
$.primary_expression,
|
|
1219
|
+
$.postfix_expression,
|
|
1220
|
+
$.macro_invocation_candidate,
|
|
1221
|
+
),
|
|
1222
|
+
// Keep the preprocessing reading alive through a shared prefix. A complete
|
|
1223
|
+
// expression or a dedicated standard macro has priority at the closing paren.
|
|
1224
|
+
_candidate_name: ($) => prec.dynamic(1, identifierSpelling($)),
|
|
1225
|
+
macro_invocation_candidate: ($) =>
|
|
1226
|
+
prec.dynamic(-4, macroCandidate($, $._candidate_start)),
|
|
1227
|
+
_external_candidate: ($) => macroCandidate($, $._external_candidate_start),
|
|
1228
|
+
_candidate_base: ($) => alias($._candidate_name, $.identifier),
|
|
1229
|
+
_candidate_expressions: ($) =>
|
|
1230
|
+
separated($._assignment, alias($._candidate_comma, ",")),
|
|
1231
|
+
macro_argument_list: ($) => {
|
|
1232
|
+
const comma = alias($._candidate_comma, ",");
|
|
1233
|
+
return choice(
|
|
1234
|
+
$.macro_argument,
|
|
1235
|
+
seq(
|
|
1236
|
+
optional($.macro_argument),
|
|
1237
|
+
repeat1(seq(comma, optional($.macro_argument))),
|
|
1238
|
+
),
|
|
1239
|
+
);
|
|
1240
|
+
},
|
|
1241
|
+
macro_argument: ($) =>
|
|
1242
|
+
choice($._assignment, prec.dynamic(-3, repeat1($._macro_argument_token))),
|
|
1243
|
+
_macro_argument_token: ($) =>
|
|
1244
|
+
prec.dynamic(
|
|
1245
|
+
-2,
|
|
1246
|
+
choice(
|
|
1247
|
+
$.identifier,
|
|
1248
|
+
$.integer_constant,
|
|
1249
|
+
$.floating_constant,
|
|
1250
|
+
$.pp_number,
|
|
1251
|
+
alias($._invalid_macro_number, $.pp_number),
|
|
1252
|
+
$.other_preprocessing_token,
|
|
1253
|
+
$.string_literal,
|
|
1254
|
+
$.character_constant,
|
|
1255
|
+
$.macro_parenthesized_tokens,
|
|
1256
|
+
...keywords.map((name) => alias($[`_kw_${name}`], $.identifier)),
|
|
1257
|
+
...Object.values(punctuators)
|
|
1258
|
+
.filter((s) => !["(", ")", ","].includes(s))
|
|
1259
|
+
.map((s) => p($, s)),
|
|
1260
|
+
),
|
|
1261
|
+
),
|
|
1262
|
+
_invalid_macro_number: ($) => seq($._raw_number_head, $._raw_number_tail),
|
|
1263
|
+
macro_parenthesized_tokens: ($) =>
|
|
1264
|
+
seq(
|
|
1265
|
+
choice(p($, "("), alias($._atomic_open, "(")),
|
|
1266
|
+
repeat(choice($._macro_argument_token, p($, ","))),
|
|
1267
|
+
p($, ")"),
|
|
1268
|
+
),
|
|
1269
|
+
...Object.fromEntries(
|
|
1270
|
+
typeArgumentMacros.map((name) => [
|
|
1271
|
+
`_${name}_callee`,
|
|
1272
|
+
($) => prec.dynamic(2, alias($[`_lib_${name}`], $.identifier)),
|
|
1273
|
+
]),
|
|
1274
|
+
),
|
|
1275
|
+
_type_argument: ($) =>
|
|
1276
|
+
choice(
|
|
1277
|
+
$.type_name,
|
|
1278
|
+
$.type_or_expression,
|
|
1279
|
+
groupRef($, "type_argument", "if_section"),
|
|
1280
|
+
),
|
|
1281
|
+
_alignof_arguments: ($) => $._type_argument,
|
|
1282
|
+
_va_arg_arguments: ($) => seq($._assignment, p($, ","), $._type_argument),
|
|
1283
|
+
_offsetof_arguments: ($) =>
|
|
1284
|
+
choice(
|
|
1285
|
+
seq($.type_name, p($, ","), $.member_designator),
|
|
1286
|
+
seq(
|
|
1287
|
+
choice(
|
|
1288
|
+
$.type_or_expression,
|
|
1289
|
+
groupRef($, "type_argument", "if_section"),
|
|
1290
|
+
),
|
|
1291
|
+
p($, ","),
|
|
1292
|
+
$.member_designator_or_expression,
|
|
1293
|
+
),
|
|
1294
|
+
),
|
|
1295
|
+
member_designator: ($) =>
|
|
1296
|
+
seq(field("member", $.identifier), repeat($._member_designator_suffix)),
|
|
1297
|
+
member_designator_or_expression: ($) =>
|
|
1298
|
+
seq($.identifier, repeat($._member_designator_suffix)),
|
|
1299
|
+
_member_designator_suffix: ($) =>
|
|
1300
|
+
choice(
|
|
1301
|
+
seq(
|
|
1302
|
+
field("operator", oneOf($, [".", "->"])),
|
|
1303
|
+
field("member", $.identifier),
|
|
1304
|
+
),
|
|
1305
|
+
seq(p($, "["), field("index", $._expression), p($, "]")),
|
|
1306
|
+
),
|
|
1307
|
+
_postfix_suffix: ($) =>
|
|
1308
|
+
choice(
|
|
1309
|
+
$._member_designator_suffix,
|
|
1310
|
+
parens(
|
|
1311
|
+
$,
|
|
1312
|
+
seq(
|
|
1313
|
+
$._call_start,
|
|
1314
|
+
optional(field("arguments", $.argument_expression_list)),
|
|
1315
|
+
),
|
|
1316
|
+
),
|
|
1317
|
+
field("operator", oneOf($, ["++", "--"])),
|
|
1318
|
+
),
|
|
1319
|
+
postfix_expression: ($) =>
|
|
1320
|
+
choice(
|
|
1321
|
+
prec.dynamic(
|
|
1322
|
+
-2,
|
|
1323
|
+
seq(
|
|
1324
|
+
field("base", alias($._candidate_base, $.primary_expression)),
|
|
1325
|
+
p($, "("),
|
|
1326
|
+
$._candidate_start,
|
|
1327
|
+
optional(
|
|
1328
|
+
field(
|
|
1329
|
+
"arguments",
|
|
1330
|
+
alias($._candidate_expressions, $.argument_expression_list),
|
|
1331
|
+
),
|
|
1332
|
+
),
|
|
1333
|
+
alias($._candidate_close, ")"),
|
|
1334
|
+
),
|
|
1335
|
+
),
|
|
1336
|
+
|
|
1337
|
+
seq(field("base", $._postfix), $._postfix_suffix),
|
|
1338
|
+
...typeArgumentMacros.map((name) =>
|
|
1339
|
+
seq(
|
|
1340
|
+
field("base", alias($[`_${name}_callee`], $.primary_expression)),
|
|
1341
|
+
parens(
|
|
1342
|
+
$,
|
|
1343
|
+
field(
|
|
1344
|
+
"arguments",
|
|
1345
|
+
alias($[`_${name}_arguments`], $.argument_expression_list),
|
|
1346
|
+
),
|
|
1347
|
+
),
|
|
1348
|
+
),
|
|
1349
|
+
),
|
|
1350
|
+
seq(
|
|
1351
|
+
parens($, field("type", $.type_name)),
|
|
1352
|
+
braceList($, "initializer_list", "initializer"),
|
|
1353
|
+
),
|
|
1354
|
+
),
|
|
1355
|
+
character_constant: ($) =>
|
|
1356
|
+
quotedLiteral($, "'", $._character_open, $.character_content),
|
|
1357
|
+
character_content: ($) => prec.right(repeat1($._literal_text)),
|
|
1358
|
+
string_literal: ($) =>
|
|
1359
|
+
quotedLiteral($, '"', $._string_open, $.string_content),
|
|
1360
|
+
string_content: ($) => prec.right(repeat1($._literal_text)),
|
|
1361
|
+
primary_expression: ($) =>
|
|
1362
|
+
choice(
|
|
1363
|
+
$.identifier,
|
|
1364
|
+
$.integer_constant,
|
|
1365
|
+
$.floating_constant,
|
|
1366
|
+
$.character_constant,
|
|
1367
|
+
repeat1($.string_literal),
|
|
1368
|
+
parens($, $._expression),
|
|
1369
|
+
$.generic_selection,
|
|
1370
|
+
),
|
|
1371
|
+
generic_selection: ($) =>
|
|
1372
|
+
seq(
|
|
1373
|
+
kw($, "_Generic"),
|
|
1374
|
+
parens(
|
|
1375
|
+
$,
|
|
1376
|
+
seq(field("control", $._assignment), p($, ","), $.generic_assoc_list),
|
|
1377
|
+
),
|
|
1378
|
+
),
|
|
1379
|
+
generic_association: ($) =>
|
|
1380
|
+
seq(
|
|
1381
|
+
choice(field("type", $.type_name), kw($, "default")),
|
|
1382
|
+
p($, ":"),
|
|
1383
|
+
field("value", $._assignment),
|
|
1384
|
+
),
|
|
1385
|
+
|
|
1386
|
+
control_line: ($) => controlLine($, $._extra_directive_hash),
|
|
1387
|
+
non_directive: ($) => nonDirective($, $._extra_directive_hash),
|
|
1388
|
+
_structural_control_line: ($) => controlLine($, $._directive_hash),
|
|
1389
|
+
_structural_non_directive: ($) => nonDirective($, $._directive_hash),
|
|
1390
|
+
macro_parameters: ($) =>
|
|
1391
|
+
seq(
|
|
1392
|
+
alias($._macro_open, "("),
|
|
1393
|
+
optional(
|
|
1394
|
+
choice(
|
|
1395
|
+
p($, "..."),
|
|
1396
|
+
seq(
|
|
1397
|
+
separated($.identifier, p($, ",")),
|
|
1398
|
+
optional(seq(p($, ","), p($, "..."))),
|
|
1399
|
+
),
|
|
1400
|
+
),
|
|
1401
|
+
),
|
|
1402
|
+
p($, ")"),
|
|
1403
|
+
),
|
|
1404
|
+
replacement_list: ($) => repeat1($.preprocessing_token),
|
|
1405
|
+
pp_tokens: ($) => repeat1($.preprocessing_token),
|
|
1406
|
+
preprocessing_token: ($) =>
|
|
1407
|
+
choice(
|
|
1408
|
+
prec(-1, $.header_name),
|
|
1409
|
+
$.identifier,
|
|
1410
|
+
$.pp_number,
|
|
1411
|
+
$.character_constant,
|
|
1412
|
+
$.string_literal,
|
|
1413
|
+
$.other_preprocessing_token,
|
|
1414
|
+
...Object.values(punctuators).map((text) => p($, text)),
|
|
1415
|
+
),
|
|
1416
|
+
_if_header: ($) =>
|
|
1417
|
+
seq(
|
|
1418
|
+
alias($._if_hash, "#"),
|
|
1419
|
+
choice(
|
|
1420
|
+
seq(pp($, "if"), field("condition", $.pp_tokens)),
|
|
1421
|
+
seq(
|
|
1422
|
+
choice(pp($, "ifdef"), pp($, "ifndef")),
|
|
1423
|
+
field("condition", $.identifier),
|
|
1424
|
+
),
|
|
1425
|
+
),
|
|
1426
|
+
$._directive_end,
|
|
1427
|
+
),
|
|
1428
|
+
_elif_header: ($) =>
|
|
1429
|
+
seq(
|
|
1430
|
+
branchHash($),
|
|
1431
|
+
pp($, "elif"),
|
|
1432
|
+
field("condition", $.pp_tokens),
|
|
1433
|
+
$._directive_end,
|
|
1434
|
+
),
|
|
1435
|
+
_else_header: ($) => seq(branchHash($), pp($, "else"), $._directive_end),
|
|
1436
|
+
...conditionalRules("external", ($) => repeat($._external_item)),
|
|
1437
|
+
if_section: ($) => seq($._transparent_start, sectionBody($, "")),
|
|
1438
|
+
...branchRules("", ($) => repeat(controlDirective($))),
|
|
1439
|
+
...conditionalRules("statement", ($) => singleValue($, $._body)),
|
|
1440
|
+
...conditionalRules("initializer", ($) => singleValue($, $.initializer)),
|
|
1441
|
+
...conditionalRules("expression", ($) => singleValue($, $._expression)),
|
|
1442
|
+
...conditionalOperandRules("alignment_operand", "constant_expression"),
|
|
1443
|
+
...conditionalOperandRules("sizeof_operand", "_expression"),
|
|
1444
|
+
...conditionalOperandRules("type_argument", "_assignment"),
|
|
1445
|
+
...conditionalRules("specifiers", ($) =>
|
|
1446
|
+
repeat(choice($._declaration_specifier, directive($, "specifiers"))),
|
|
1447
|
+
),
|
|
1448
|
+
...conditionalRules("specifier_qualifiers", ($) =>
|
|
1449
|
+
repeat(
|
|
1450
|
+
choice($._specifier_qualifier, directive($, "specifier_qualifiers")),
|
|
1451
|
+
),
|
|
1452
|
+
),
|
|
1453
|
+
...conditionalRules("qualifiers", ($) =>
|
|
1454
|
+
repeat(choice($.type_qualifier, directive($, "qualifiers"))),
|
|
1455
|
+
),
|
|
1456
|
+
...conditionalRules("old_declarations", ($) =>
|
|
1457
|
+
repeat(choice($.declaration, directive($, "old_declarations"))),
|
|
1458
|
+
),
|
|
1459
|
+
...conditionalRules("block", ($) => repeat($._block_item)),
|
|
1460
|
+
...conditionalRules("member", ($) =>
|
|
1461
|
+
repeat(choice($.struct_declaration, directive($, "member"))),
|
|
1462
|
+
),
|
|
1463
|
+
_repeated_list_commas: ($) => seq(p($, ","), repeat1(p($, ","))),
|
|
1464
|
+
...callListGrammar(false).rules,
|
|
1465
|
+
...parameterGrammar.rules,
|
|
1466
|
+
...Object.assign(
|
|
1467
|
+
{},
|
|
1468
|
+
...Object.entries(listItems).map(([list, item]) => listRules(list, item)),
|
|
1469
|
+
),
|
|
1470
|
+
endif_line: ($) => seq(branchHash($), pp($, "endif"), $._directive_end),
|
|
1471
|
+
pragma_operator: ($) =>
|
|
1472
|
+
seq(
|
|
1473
|
+
kw($, "_Pragma"),
|
|
1474
|
+
p($, "("),
|
|
1475
|
+
$.string_literal,
|
|
1476
|
+
alias($._pragma_close, ")"),
|
|
1477
|
+
),
|
|
1478
|
+
},
|
|
1479
|
+
});
|