@willbooster/tree-sitter-c 1.1.6 → 1.1.7
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 +45 -52
- package/package.json +2 -2
- package/tree-sitter-c.wasm +0 -0
- package/tree-sitter.json +1 -1
- package/types/LICENSE +21 -0
- package/types/treeSitterDsl.d.ts +451 -0
package/grammar.js
CHANGED
|
@@ -5,9 +5,12 @@
|
|
|
5
5
|
* @license MIT
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
/// <reference types="tree-sitter-cli/dsl" />
|
|
9
8
|
// @ts-check
|
|
10
9
|
|
|
10
|
+
// Tree-sitter injects its DSL before loading this CommonJS grammar.
|
|
11
|
+
const { grammar, alias, choice, field, optional, prec, repeat, repeat1, seq, sym, token } =
|
|
12
|
+
/** @type {typeof globalThis & typeof import('./types/treeSitterDsl')} */ (globalThis);
|
|
13
|
+
|
|
11
14
|
const PREC = {
|
|
12
15
|
PAREN_DECLARATOR: -10,
|
|
13
16
|
ASSIGNMENT: -2,
|
|
@@ -94,8 +97,8 @@ module.exports = Object.assign(
|
|
|
94
97
|
$.attributed_statement,
|
|
95
98
|
$.type_definition,
|
|
96
99
|
$._empty_declaration,
|
|
97
|
-
|
|
98
|
-
|
|
100
|
+
sym('preproc_if'),
|
|
101
|
+
sym('preproc_ifdef'),
|
|
99
102
|
$.preproc_include,
|
|
100
103
|
$.preproc_def,
|
|
101
104
|
$.preproc_function_def,
|
|
@@ -112,8 +115,8 @@ module.exports = Object.assign(
|
|
|
112
115
|
$.attributed_statement,
|
|
113
116
|
$.type_definition,
|
|
114
117
|
$._empty_declaration,
|
|
115
|
-
|
|
116
|
-
|
|
118
|
+
sym('preproc_if'),
|
|
119
|
+
sym('preproc_ifdef'),
|
|
117
120
|
$.preproc_include,
|
|
118
121
|
$.preproc_def,
|
|
119
122
|
$.preproc_function_def,
|
|
@@ -163,10 +166,10 @@ module.exports = Object.assign(
|
|
|
163
166
|
token.immediate(/\r?\n/)
|
|
164
167
|
),
|
|
165
168
|
|
|
166
|
-
...preprocIf('', (
|
|
167
|
-
...preprocIf('_in_field_declaration_list', (
|
|
168
|
-
...preprocIf('_in_enumerator_list', (
|
|
169
|
-
...preprocIf('_in_enumerator_list_no_comma', (
|
|
169
|
+
...preprocIf('', () => sym('_block_item')),
|
|
170
|
+
...preprocIf('_in_field_declaration_list', () => sym('_field_declaration_list_item')),
|
|
171
|
+
...preprocIf('_in_enumerator_list', () => seq(sym('enumerator'), ',')),
|
|
172
|
+
...preprocIf('_in_enumerator_list_no_comma', () => sym('enumerator'), -1),
|
|
170
173
|
|
|
171
174
|
preproc_arg: () => token(prec(-1, /\S([^/\n]|\/[^*]|\\\r?\n)*/)),
|
|
172
175
|
preproc_directive: () => /#[ \t]*[a-zA-Z0-9]\w*/,
|
|
@@ -203,6 +206,7 @@ module.exports = Object.assign(
|
|
|
203
206
|
preproc_argument_list: ($) => seq('(', commaSep($._preproc_expression), ')'),
|
|
204
207
|
|
|
205
208
|
preproc_binary_expression: ($) => {
|
|
209
|
+
/** @type {[string, number][]} */
|
|
206
210
|
const table = [
|
|
207
211
|
['+', PREC.ADD],
|
|
208
212
|
['-', PREC.ADD],
|
|
@@ -230,7 +234,6 @@ module.exports = Object.assign(
|
|
|
230
234
|
precedence,
|
|
231
235
|
seq(
|
|
232
236
|
field('left', $._preproc_expression),
|
|
233
|
-
// @ts-ignore
|
|
234
237
|
field('operator', operator),
|
|
235
238
|
field('right', $._preproc_expression)
|
|
236
239
|
)
|
|
@@ -646,8 +649,8 @@ module.exports = Object.assign(
|
|
|
646
649
|
repeat(
|
|
647
650
|
choice(
|
|
648
651
|
seq($.enumerator, ','),
|
|
649
|
-
alias(
|
|
650
|
-
alias(
|
|
652
|
+
alias(sym('preproc_if_in_enumerator_list'), sym('preproc_if')),
|
|
653
|
+
alias(sym('preproc_ifdef_in_enumerator_list'), sym('preproc_ifdef')),
|
|
651
654
|
seq($.preproc_call, ',')
|
|
652
655
|
)
|
|
653
656
|
),
|
|
@@ -655,8 +658,8 @@ module.exports = Object.assign(
|
|
|
655
658
|
seq(
|
|
656
659
|
choice(
|
|
657
660
|
$.enumerator,
|
|
658
|
-
alias(
|
|
659
|
-
alias(
|
|
661
|
+
alias(sym('preproc_if_in_enumerator_list_no_comma'), sym('preproc_if')),
|
|
662
|
+
alias(sym('preproc_ifdef_in_enumerator_list_no_comma'), sym('preproc_ifdef')),
|
|
660
663
|
$.preproc_call
|
|
661
664
|
)
|
|
662
665
|
)
|
|
@@ -699,8 +702,8 @@ module.exports = Object.assign(
|
|
|
699
702
|
$.preproc_def,
|
|
700
703
|
$.preproc_function_def,
|
|
701
704
|
$.preproc_call,
|
|
702
|
-
alias(
|
|
703
|
-
alias(
|
|
705
|
+
alias(sym('preproc_if_in_field_declaration_list'), sym('preproc_if')),
|
|
706
|
+
alias(sym('preproc_ifdef_in_field_declaration_list'), sym('preproc_ifdef'))
|
|
704
707
|
),
|
|
705
708
|
|
|
706
709
|
field_declaration: ($) =>
|
|
@@ -911,6 +914,7 @@ module.exports = Object.assign(
|
|
|
911
914
|
prec.left(PREC.UNARY, seq(field('operator', choice('!', '~', '-', '+')), field('argument', $.expression))),
|
|
912
915
|
|
|
913
916
|
binary_expression: ($) => {
|
|
917
|
+
/** @type {[string, number][]} */
|
|
914
918
|
const table = [
|
|
915
919
|
['+', PREC.ADD],
|
|
916
920
|
['-', PREC.ADD],
|
|
@@ -936,12 +940,7 @@ module.exports = Object.assign(
|
|
|
936
940
|
...table.map(([operator, precedence]) => {
|
|
937
941
|
return prec.left(
|
|
938
942
|
precedence,
|
|
939
|
-
seq(
|
|
940
|
-
field('left', $.expression),
|
|
941
|
-
// @ts-ignore
|
|
942
|
-
field('operator', operator),
|
|
943
|
-
field('right', $.expression)
|
|
944
|
-
)
|
|
943
|
+
seq(field('left', $.expression), field('operator', operator), field('right', $.expression))
|
|
945
944
|
);
|
|
946
945
|
})
|
|
947
946
|
);
|
|
@@ -1124,7 +1123,7 @@ module.exports = Object.assign(
|
|
|
1124
1123
|
char_literal: ($) =>
|
|
1125
1124
|
seq(
|
|
1126
1125
|
choice("L'", "u'", "U'", "u8'", "'"),
|
|
1127
|
-
repeat1(choice($.escape_sequence, alias(token.immediate(/[^\n']/),
|
|
1126
|
+
repeat1(choice($.escape_sequence, alias(token.immediate(/[^\n']/), sym('character')))),
|
|
1128
1127
|
"'"
|
|
1129
1128
|
),
|
|
1130
1129
|
|
|
@@ -1145,7 +1144,7 @@ module.exports = Object.assign(
|
|
|
1145
1144
|
string_literal: ($) =>
|
|
1146
1145
|
seq(
|
|
1147
1146
|
choice('L"', 'u"', 'U"', 'u8"', '"'),
|
|
1148
|
-
repeat(choice(alias(token.immediate(prec(1, /[^\\"\n]+/)),
|
|
1147
|
+
repeat(choice(alias(token.immediate(prec(1, /[^\\"\n]+/)), sym('string_content')), $.escape_sequence)),
|
|
1149
1148
|
'"'
|
|
1150
1149
|
),
|
|
1151
1150
|
|
|
@@ -1163,9 +1162,9 @@ module.exports = Object.assign(
|
|
|
1163
1162
|
identifier: () =>
|
|
1164
1163
|
/(\p{XID_Start}|\$|_|\\u[0-9A-Fa-f]{4}|\\U[0-9A-Fa-f]{8})(\p{XID_Continue}|\$|\\u[0-9A-Fa-f]{4}|\\U[0-9A-Fa-f]{8})*/u,
|
|
1165
1164
|
|
|
1166
|
-
_type_identifier: ($) => alias($.identifier,
|
|
1167
|
-
_field_identifier: ($) => alias($.identifier,
|
|
1168
|
-
_statement_identifier: ($) => alias($.identifier,
|
|
1165
|
+
_type_identifier: ($) => alias($.identifier, sym('type_identifier')),
|
|
1166
|
+
_field_identifier: ($) => alias($.identifier, sym('field_identifier')),
|
|
1167
|
+
_statement_identifier: ($) => alias($.identifier, sym('statement_identifier')),
|
|
1169
1168
|
|
|
1170
1169
|
_empty_declaration: ($) => seq($.type_specifier, ';'),
|
|
1171
1170
|
|
|
@@ -1183,24 +1182,18 @@ module.exports = Object.assign(
|
|
|
1183
1182
|
*
|
|
1184
1183
|
* @param {string} suffix
|
|
1185
1184
|
*
|
|
1186
|
-
* @param {RuleBuilder<string>} content
|
|
1185
|
+
* @param {import('./types/treeSitterDsl').RuleBuilder<string>} content
|
|
1187
1186
|
*
|
|
1188
1187
|
* @param {number} precedence
|
|
1189
1188
|
*
|
|
1190
|
-
* @returns {RuleBuilders<string, string>}
|
|
1189
|
+
* @returns {import('./types/treeSitterDsl').RuleBuilders<string, string>}
|
|
1191
1190
|
*/
|
|
1192
1191
|
function preprocIf(suffix, content, precedence = 0) {
|
|
1193
|
-
|
|
1194
|
-
*
|
|
1195
|
-
* @param {GrammarSymbols<string>} $
|
|
1196
|
-
*
|
|
1197
|
-
* @returns {ChoiceRule}
|
|
1198
|
-
*/
|
|
1199
|
-
function alternativeBlock($) {
|
|
1192
|
+
function alternativeBlock() {
|
|
1200
1193
|
return choice(
|
|
1201
|
-
suffix ? alias(
|
|
1202
|
-
suffix ? alias(
|
|
1203
|
-
suffix ? alias(
|
|
1194
|
+
suffix ? alias(sym('preproc_else' + suffix), sym('preproc_else')) : sym('preproc_else'),
|
|
1195
|
+
suffix ? alias(sym('preproc_elif' + suffix), sym('preproc_elif')) : sym('preproc_elif'),
|
|
1196
|
+
suffix ? alias(sym('preproc_elifdef' + suffix), sym('preproc_elifdef')) : sym('preproc_elifdef')
|
|
1204
1197
|
);
|
|
1205
1198
|
}
|
|
1206
1199
|
|
|
@@ -1210,10 +1203,10 @@ function preprocIf(suffix, content, precedence = 0) {
|
|
|
1210
1203
|
precedence,
|
|
1211
1204
|
seq(
|
|
1212
1205
|
preprocessor('if'),
|
|
1213
|
-
field('condition',
|
|
1206
|
+
field('condition', sym('_preproc_expression')),
|
|
1214
1207
|
'\n',
|
|
1215
1208
|
repeat(content($)),
|
|
1216
|
-
field('alternative', optional(alternativeBlock(
|
|
1209
|
+
field('alternative', optional(alternativeBlock())),
|
|
1217
1210
|
preprocessor('endif')
|
|
1218
1211
|
)
|
|
1219
1212
|
),
|
|
@@ -1223,9 +1216,9 @@ function preprocIf(suffix, content, precedence = 0) {
|
|
|
1223
1216
|
precedence,
|
|
1224
1217
|
seq(
|
|
1225
1218
|
choice(preprocessor('ifdef'), preprocessor('ifndef')),
|
|
1226
|
-
field('name',
|
|
1219
|
+
field('name', sym('identifier')),
|
|
1227
1220
|
repeat(content($)),
|
|
1228
|
-
field('alternative', optional(alternativeBlock(
|
|
1221
|
+
field('alternative', optional(alternativeBlock())),
|
|
1229
1222
|
preprocessor('endif')
|
|
1230
1223
|
)
|
|
1231
1224
|
),
|
|
@@ -1237,10 +1230,10 @@ function preprocIf(suffix, content, precedence = 0) {
|
|
|
1237
1230
|
precedence,
|
|
1238
1231
|
seq(
|
|
1239
1232
|
preprocessor('elif'),
|
|
1240
|
-
field('condition',
|
|
1233
|
+
field('condition', sym('_preproc_expression')),
|
|
1241
1234
|
'\n',
|
|
1242
1235
|
repeat(content($)),
|
|
1243
|
-
field('alternative', optional(alternativeBlock(
|
|
1236
|
+
field('alternative', optional(alternativeBlock()))
|
|
1244
1237
|
)
|
|
1245
1238
|
),
|
|
1246
1239
|
|
|
@@ -1249,9 +1242,9 @@ function preprocIf(suffix, content, precedence = 0) {
|
|
|
1249
1242
|
precedence,
|
|
1250
1243
|
seq(
|
|
1251
1244
|
choice(preprocessor('elifdef'), preprocessor('elifndef')),
|
|
1252
|
-
field('name',
|
|
1245
|
+
field('name', sym('identifier')),
|
|
1253
1246
|
repeat(content($)),
|
|
1254
|
-
field('alternative', optional(alternativeBlock(
|
|
1247
|
+
field('alternative', optional(alternativeBlock()))
|
|
1255
1248
|
)
|
|
1256
1249
|
),
|
|
1257
1250
|
};
|
|
@@ -1262,7 +1255,7 @@ function preprocIf(suffix, content, precedence = 0) {
|
|
|
1262
1255
|
*
|
|
1263
1256
|
* @param {string} command
|
|
1264
1257
|
*
|
|
1265
|
-
* @returns {AliasRule}
|
|
1258
|
+
* @returns {import('./types/treeSitterDsl').AliasRule}
|
|
1266
1259
|
*/
|
|
1267
1260
|
function preprocessor(command) {
|
|
1268
1261
|
return alias(new RegExp('#[ \t]*' + command), '#' + command);
|
|
@@ -1271,9 +1264,9 @@ function preprocessor(command) {
|
|
|
1271
1264
|
/**
|
|
1272
1265
|
* Creates a rule to optionally match one or more of the rules separated by a comma
|
|
1273
1266
|
*
|
|
1274
|
-
* @param {Rule} rule
|
|
1267
|
+
* @param {import('./types/treeSitterDsl').Rule} rule
|
|
1275
1268
|
*
|
|
1276
|
-
* @returns {ChoiceRule}
|
|
1269
|
+
* @returns {import('./types/treeSitterDsl').ChoiceRule}
|
|
1277
1270
|
*/
|
|
1278
1271
|
function commaSep(rule) {
|
|
1279
1272
|
return optional(commaSep1(rule));
|
|
@@ -1282,9 +1275,9 @@ function commaSep(rule) {
|
|
|
1282
1275
|
/**
|
|
1283
1276
|
* Creates a rule to match one or more of the rules separated by a comma
|
|
1284
1277
|
*
|
|
1285
|
-
* @param {Rule} rule
|
|
1278
|
+
* @param {import('./types/treeSitterDsl').Rule} rule
|
|
1286
1279
|
*
|
|
1287
|
-
* @returns {SeqRule}
|
|
1280
|
+
* @returns {import('./types/treeSitterDsl').SeqRule}
|
|
1288
1281
|
*/
|
|
1289
1282
|
function commaSep1(rule) {
|
|
1290
1283
|
return seq(rule, repeat(seq(',', rule)));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@willbooster/tree-sitter-c",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.7",
|
|
4
4
|
"description": "C grammar for tree-sitter",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"incremental",
|
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
"author": "WillBooster Inc.",
|
|
17
17
|
"files": [
|
|
18
18
|
"grammar.js",
|
|
19
|
+
"types/*",
|
|
19
20
|
"queries/*.scm",
|
|
20
21
|
"src/node-types.json",
|
|
21
22
|
"tree-sitter.json",
|
|
@@ -55,7 +56,6 @@
|
|
|
55
56
|
"playwright": "1.63.0",
|
|
56
57
|
"semantic-release": "25.0.9",
|
|
57
58
|
"sort-package-json": "4.0.0",
|
|
58
|
-
"tree-sitter-cli": "0.27.0",
|
|
59
59
|
"typescript": "7.0.2",
|
|
60
60
|
"vitest": "5.0.1",
|
|
61
61
|
"wrangler": "4.136.1"
|
package/tree-sitter-c.wasm
CHANGED
|
Binary file
|
package/tree-sitter.json
CHANGED
package/types/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2018 Max Brunsfeld
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,451 @@
|
|
|
1
|
+
// DSL declarations from tree-sitter-cli 0.27.0, matching the fork CLI's upstream grammar API.
|
|
2
|
+
export type AliasRule = { type: 'ALIAS'; named: boolean; content: Rule; value: string };
|
|
3
|
+
export type BlankRule = { type: 'BLANK' };
|
|
4
|
+
export type ChoiceRule = { type: 'CHOICE'; members: Rule[] };
|
|
5
|
+
export type FieldRule = { type: 'FIELD'; name: string; content: Rule };
|
|
6
|
+
export type ImmediateTokenRule = { type: 'IMMEDIATE_TOKEN'; content: Rule };
|
|
7
|
+
export type PatternRule = { type: 'PATTERN'; value: string; flags?: string };
|
|
8
|
+
export type PrecedenceValue = string | number;
|
|
9
|
+
export type PrecDynamicRule = { type: 'PREC_DYNAMIC'; content: Rule; value: number };
|
|
10
|
+
export type PrecLeftRule = { type: 'PREC_LEFT'; content: Rule; value: PrecedenceValue };
|
|
11
|
+
export type PrecRightRule = { type: 'PREC_RIGHT'; content: Rule; value: PrecedenceValue };
|
|
12
|
+
export type PrecRule = { type: 'PREC'; content: Rule; value: PrecedenceValue };
|
|
13
|
+
export type Repeat1Rule = { type: 'REPEAT1'; content: Rule };
|
|
14
|
+
export type RepeatRule = { type: 'REPEAT'; content: Rule };
|
|
15
|
+
export type ReservedRule = { type: 'RESERVED'; content: Rule; context_name: string };
|
|
16
|
+
export type SeqRule = { type: 'SEQ'; members: Rule[] };
|
|
17
|
+
export type StringRule = { type: 'STRING'; value: string };
|
|
18
|
+
export type SymbolRule<Name extends string> = { type: 'SYMBOL'; name: Name };
|
|
19
|
+
export type PrecedenceEntry = StringRule | SymbolRule<string>;
|
|
20
|
+
export type TokenRule = { type: 'TOKEN'; content: Rule };
|
|
21
|
+
export type EOFRule = { type: 'EOF' };
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
export type Rule =
|
|
25
|
+
| AliasRule
|
|
26
|
+
| BlankRule
|
|
27
|
+
| ChoiceRule
|
|
28
|
+
| FieldRule
|
|
29
|
+
| ImmediateTokenRule
|
|
30
|
+
| PatternRule
|
|
31
|
+
| PrecDynamicRule
|
|
32
|
+
| PrecLeftRule
|
|
33
|
+
| PrecRightRule
|
|
34
|
+
| PrecRule
|
|
35
|
+
| Repeat1Rule
|
|
36
|
+
| RepeatRule
|
|
37
|
+
| ReservedRule
|
|
38
|
+
| SeqRule
|
|
39
|
+
| StringRule
|
|
40
|
+
| SymbolRule<string>
|
|
41
|
+
| TokenRule
|
|
42
|
+
| EOFRule;
|
|
43
|
+
|
|
44
|
+
export declare class RustRegex {
|
|
45
|
+
value: string;
|
|
46
|
+
|
|
47
|
+
constructor(pattern: string);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export type RuleOrLiteral = Rule | RegExp | RustRegex | string;
|
|
51
|
+
|
|
52
|
+
export type GrammarSymbols<RuleName extends string> = {
|
|
53
|
+
[name in RuleName]: SymbolRule<name>;
|
|
54
|
+
} &
|
|
55
|
+
Record<string, SymbolRule<string>>;
|
|
56
|
+
|
|
57
|
+
export type RuleBuilder<RuleName extends string> = (
|
|
58
|
+
$: GrammarSymbols<RuleName>,
|
|
59
|
+
previous?: Rule,
|
|
60
|
+
) => RuleOrLiteral;
|
|
61
|
+
|
|
62
|
+
export type RuleBuilders<
|
|
63
|
+
RuleName extends string,
|
|
64
|
+
BaseGrammarRuleName extends string
|
|
65
|
+
> = {
|
|
66
|
+
[name in RuleName]: RuleBuilder<RuleName | BaseGrammarRuleName>;
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
export interface Grammar<
|
|
70
|
+
RuleName extends string,
|
|
71
|
+
BaseGrammarRuleName extends string = never,
|
|
72
|
+
Rules extends RuleBuilders<RuleName, BaseGrammarRuleName> = RuleBuilders<
|
|
73
|
+
RuleName,
|
|
74
|
+
BaseGrammarRuleName
|
|
75
|
+
>
|
|
76
|
+
> {
|
|
77
|
+
/**
|
|
78
|
+
* Name of the grammar language.
|
|
79
|
+
*/
|
|
80
|
+
name: string;
|
|
81
|
+
|
|
82
|
+
/** Mapping of grammar rule names to rule builder functions. */
|
|
83
|
+
rules: Rules;
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* An array of arrays of precedence names or rules. Each inner array represents
|
|
87
|
+
* a *descending* ordering. Names/rules listed earlier in one of these arrays
|
|
88
|
+
* have higher precedence than any names/rules listed later in the same array.
|
|
89
|
+
*
|
|
90
|
+
* Using rules is just a shorthand way for using a name then calling prec()
|
|
91
|
+
* with that name. It is just a convenience.
|
|
92
|
+
*/
|
|
93
|
+
precedences?: (
|
|
94
|
+
$: GrammarSymbols<RuleName | BaseGrammarRuleName>,
|
|
95
|
+
previous: PrecedenceEntry[][],
|
|
96
|
+
) => (string | PrecedenceEntry)[][],
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* An array of arrays of rule names. Each inner array represents a set of
|
|
100
|
+
* rules that's involved in an _LR(1) conflict_ that is _intended to exist_
|
|
101
|
+
* in the grammar. When these conflicts occur at runtime, Tree-sitter will
|
|
102
|
+
* use the GLR algorithm to explore all of the possible interpretations. If
|
|
103
|
+
* _multiple_ parses end up succeeding, Tree-sitter will pick the subtree
|
|
104
|
+
* whose corresponding rule has the highest total _dynamic precedence_.
|
|
105
|
+
*
|
|
106
|
+
* @param $ grammar rules
|
|
107
|
+
*/
|
|
108
|
+
conflicts?: (
|
|
109
|
+
$: GrammarSymbols<RuleName | BaseGrammarRuleName>,
|
|
110
|
+
previous: SymbolRule<string>[][],
|
|
111
|
+
) => SymbolRule<string>[][];
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* An array of token names which can be returned by an _external scanner_.
|
|
115
|
+
* External scanners allow you to write custom C code which runs during the
|
|
116
|
+
* lexing process in order to handle lexical rules (e.g. Python's indentation
|
|
117
|
+
* tokens) that cannot be described by regular expressions.
|
|
118
|
+
*
|
|
119
|
+
* @param $ grammar rules
|
|
120
|
+
* @param previous array of externals from the base schema, if any
|
|
121
|
+
*
|
|
122
|
+
* @see https://tree-sitter.github.io/tree-sitter/creating-parsers/4-external-scanners
|
|
123
|
+
*/
|
|
124
|
+
externals?: (
|
|
125
|
+
$: Record<string, SymbolRule<string>>,
|
|
126
|
+
previous: Rule[],
|
|
127
|
+
) => RuleOrLiteral[];
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* An array of tokens that may appear anywhere in the language. This
|
|
131
|
+
* is often used for whitespace and comments. The default value of
|
|
132
|
+
* extras is to accept whitespace. To control whitespace explicitly,
|
|
133
|
+
* specify extras: `$ => []` in your grammar.
|
|
134
|
+
*
|
|
135
|
+
* @param $ grammar rules
|
|
136
|
+
* @param previous array of extras from the base grammar
|
|
137
|
+
*/
|
|
138
|
+
extras?: (
|
|
139
|
+
$: GrammarSymbols<RuleName | BaseGrammarRuleName>,
|
|
140
|
+
previous: Rule[],
|
|
141
|
+
) => RuleOrLiteral[];
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* An array of rules that should be automatically removed from the
|
|
145
|
+
* grammar by replacing all of their usages with a copy of their definition.
|
|
146
|
+
* This is useful for rules that are used in multiple places but for which
|
|
147
|
+
* you don't want to create syntax tree nodes at runtime.
|
|
148
|
+
*
|
|
149
|
+
* @param $ grammar rules
|
|
150
|
+
*/
|
|
151
|
+
inline?: (
|
|
152
|
+
$: GrammarSymbols<RuleName | BaseGrammarRuleName>,
|
|
153
|
+
previous: SymbolRule<string>[],
|
|
154
|
+
) => SymbolRule<string>[];
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* A list of hidden rule names that should be considered supertypes in the
|
|
158
|
+
* generated node types file.
|
|
159
|
+
*
|
|
160
|
+
* @param $ grammar rules
|
|
161
|
+
*
|
|
162
|
+
* @see https://tree-sitter.github.io/tree-sitter/using-parsers/6-static-node-types
|
|
163
|
+
*/
|
|
164
|
+
supertypes?: (
|
|
165
|
+
$: GrammarSymbols<RuleName | BaseGrammarRuleName>,
|
|
166
|
+
previous: SymbolRule<string>[],
|
|
167
|
+
) => SymbolRule<string>[];
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* The name of a token that will match keywords for the purpose of the
|
|
171
|
+
* keyword extraction optimization.
|
|
172
|
+
*
|
|
173
|
+
* @param $ grammar rules
|
|
174
|
+
*
|
|
175
|
+
* @see https://tree-sitter.github.io/tree-sitter/creating-parsers/3-writing-the-grammar#keyword-extraction
|
|
176
|
+
*/
|
|
177
|
+
word?: (
|
|
178
|
+
$: GrammarSymbols<RuleName | BaseGrammarRuleName>,
|
|
179
|
+
) => SymbolRule<string>;
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Mapping of names to reserved word sets. The first reserved word set is the
|
|
184
|
+
* global word set, meaning it applies to every rule in every parse state.
|
|
185
|
+
* The other word sets can be used with the `reserved` function. Each callback
|
|
186
|
+
* receives the base grammar's reserved word set of the same name as its second
|
|
187
|
+
* argument, or `undefined` if no matching set exists.
|
|
188
|
+
*/
|
|
189
|
+
reserved?: Record<
|
|
190
|
+
string,
|
|
191
|
+
(
|
|
192
|
+
$: GrammarSymbols<RuleName | BaseGrammarRuleName>,
|
|
193
|
+
previous: Rule[] | undefined,
|
|
194
|
+
) => RuleOrLiteral[]
|
|
195
|
+
>;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Return type of grammar(). The runtime evaluates and normalizes the grammar
|
|
200
|
+
* beneath a "grammar" key. Optional input fields become required output fields
|
|
201
|
+
* with default values when not provided.
|
|
202
|
+
*/
|
|
203
|
+
export type GrammarSchema<RuleName extends string> = {
|
|
204
|
+
grammar: {
|
|
205
|
+
name: string;
|
|
206
|
+
/** Base grammar name when extending; undefined for root grammars. */
|
|
207
|
+
inherits: string | undefined;
|
|
208
|
+
rules: Record<RuleName, Rule>;
|
|
209
|
+
precedences: PrecedenceEntry[][];
|
|
210
|
+
conflicts: string[][];
|
|
211
|
+
externals: Rule[];
|
|
212
|
+
extras: Rule[];
|
|
213
|
+
inline: string[];
|
|
214
|
+
supertypes: string[];
|
|
215
|
+
word: string | undefined;
|
|
216
|
+
reserved: Record<string, Rule[]>;
|
|
217
|
+
};
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Causes the given rule to appear with an alternative name in the syntax tree.
|
|
222
|
+
* For instance with `alias($.foo, 'bar')`, the aliased rule will appear as an
|
|
223
|
+
* anonymous node, as if the rule had been written as the simple string.
|
|
224
|
+
*
|
|
225
|
+
* @param rule rule that will be aliased
|
|
226
|
+
* @param name target name for the alias
|
|
227
|
+
*/
|
|
228
|
+
export declare function alias(rule: RuleOrLiteral, name: string): AliasRule;
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Causes the given rule to appear as an alternative named node, for instance
|
|
232
|
+
* with `alias($.foo, $.bar)`, the aliased rule `foo` will appear as a named
|
|
233
|
+
* node called `bar`.
|
|
234
|
+
*
|
|
235
|
+
* @param rule rule that will be aliased
|
|
236
|
+
* @param symbol target symbol for the alias
|
|
237
|
+
*/
|
|
238
|
+
export declare function alias(
|
|
239
|
+
rule: RuleOrLiteral,
|
|
240
|
+
symbol: SymbolRule<string>,
|
|
241
|
+
): AliasRule;
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Creates a blank rule, matching nothing.
|
|
245
|
+
*/
|
|
246
|
+
export declare function blank(): BlankRule;
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Assigns a field name to the child node(s) matched by the given rule.
|
|
250
|
+
* In the resulting syntax tree, you can then use that field name to
|
|
251
|
+
* access specific children.
|
|
252
|
+
*
|
|
253
|
+
* @param name name of the field
|
|
254
|
+
* @param rule rule the field should match
|
|
255
|
+
*/
|
|
256
|
+
export declare function field(name: string, rule: RuleOrLiteral): FieldRule;
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Creates a rule that matches one of a set of possible rules. The order
|
|
260
|
+
* of the arguments does not matter. This is analogous to the `|` (pipe)
|
|
261
|
+
* operator in EBNF notation.
|
|
262
|
+
*
|
|
263
|
+
* @param options possible rule choices
|
|
264
|
+
*/
|
|
265
|
+
export declare function choice(...options: RuleOrLiteral[]): ChoiceRule;
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* Creates a rule that matches zero or one occurrence of a given rule.
|
|
269
|
+
* It is analogous to the `[x]` (square bracket) syntax in EBNF notation.
|
|
270
|
+
*
|
|
271
|
+
* @param value rule to be made optional
|
|
272
|
+
*/
|
|
273
|
+
export declare function optional(rule: RuleOrLiteral): ChoiceRule;
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* Marks the given rule with a precedence which will be used to resolve LR(1)
|
|
277
|
+
* conflicts at parser-generation time. When two rules overlap in a way that
|
|
278
|
+
* represents either a true ambiguity or a _local_ ambiguity given one token
|
|
279
|
+
* of lookahead, Tree-sitter will try to resolve the conflict by matching the
|
|
280
|
+
* rule with the higher precedence.
|
|
281
|
+
*
|
|
282
|
+
* Precedence values can either be strings or numbers. When comparing rules
|
|
283
|
+
* with numerical precedence, higher numbers indicate higher precedences. To
|
|
284
|
+
* compare rules with string precedence, Tree-sitter uses the grammar's `precedences`
|
|
285
|
+
* field.
|
|
286
|
+
*
|
|
287
|
+
* The default precedence of all rules is zero. This works similarly to the precedence directives in Yacc grammars.
|
|
288
|
+
*
|
|
289
|
+
* @param value precedence weight
|
|
290
|
+
* @param rule rule being weighted
|
|
291
|
+
*
|
|
292
|
+
* @see https://en.wikipedia.org/wiki/LR_parser#Conflicts_in_the_constructed_tables
|
|
293
|
+
* @see https://docs.oracle.com/cd/E19504-01/802-5880/6i9k05dh3/index.html
|
|
294
|
+
*/
|
|
295
|
+
export declare const prec: {
|
|
296
|
+
(value: string | number, rule: RuleOrLiteral): PrecRule;
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* Marks the given rule as left-associative (and optionally applies a
|
|
300
|
+
* numerical precedence). When an LR(1) conflict arises in which all of the
|
|
301
|
+
* rules have the same numerical precedence, Tree-sitter will consult the
|
|
302
|
+
* rules' associativity. If there is a left-associative rule, Tree-sitter
|
|
303
|
+
* will prefer matching a rule that ends _earlier_. This works similarly to
|
|
304
|
+
* associativity directives in Yacc grammars.
|
|
305
|
+
*
|
|
306
|
+
* @param value (optional) precedence weight
|
|
307
|
+
* @param rule rule to mark as left-associative
|
|
308
|
+
*
|
|
309
|
+
* @see https://docs.oracle.com/cd/E19504-01/802-5880/6i9k05dh3/index.html
|
|
310
|
+
*/
|
|
311
|
+
left(rule: RuleOrLiteral): PrecLeftRule;
|
|
312
|
+
left(value: string | number, rule: RuleOrLiteral): PrecLeftRule;
|
|
313
|
+
|
|
314
|
+
/**
|
|
315
|
+
* Marks the given rule as right-associative (and optionally applies a
|
|
316
|
+
* numerical precedence). When an LR(1) conflict arises in which all of the
|
|
317
|
+
* rules have the same numerical precedence, Tree-sitter will consult the
|
|
318
|
+
* rules' associativity. If there is a right-associative rule, Tree-sitter
|
|
319
|
+
* will prefer matching a rule that ends _later_. This works similarly to
|
|
320
|
+
* associativity directives in Yacc grammars.
|
|
321
|
+
*
|
|
322
|
+
* @param value (optional) precedence weight
|
|
323
|
+
* @param rule rule to mark as right-associative
|
|
324
|
+
*
|
|
325
|
+
* @see https://docs.oracle.com/cd/E19504-01/802-5880/6i9k05dh3/index.html
|
|
326
|
+
*/
|
|
327
|
+
right(rule: RuleOrLiteral): PrecRightRule;
|
|
328
|
+
right(value: string | number, rule: RuleOrLiteral): PrecRightRule;
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* Marks the given rule with a numerical precedence which will be used to
|
|
332
|
+
* resolve LR(1) conflicts at _runtime_ instead of parser-generation time.
|
|
333
|
+
* This is only necessary when handling a conflict dynamically using the
|
|
334
|
+
* `conflicts` field in the grammar, and when there is a genuine _ambiguity_:
|
|
335
|
+
* multiple rules correctly match a given piece of code. In that event,
|
|
336
|
+
* Tree-sitter compares the total dynamic precedence associated with each
|
|
337
|
+
* rule, and selects the one with the highest total. This is similar to
|
|
338
|
+
* dynamic precedence directives in Bison grammars.
|
|
339
|
+
*
|
|
340
|
+
* @param value precedence weight
|
|
341
|
+
* @param rule rule being weighted
|
|
342
|
+
*
|
|
343
|
+
* @see https://www.gnu.org/software/bison/manual/html_node/Generalized-LR-Parsing.html
|
|
344
|
+
*/
|
|
345
|
+
dynamic(value: number, rule: RuleOrLiteral): PrecDynamicRule;
|
|
346
|
+
};
|
|
347
|
+
|
|
348
|
+
/**
|
|
349
|
+
* Creates a rule that matches _zero-or-more_ occurrences of a given rule.
|
|
350
|
+
* It is analogous to the `{x}` (curly brace) syntax in EBNF notation. This
|
|
351
|
+
* rule is implemented in terms of `repeat1` but is included because it
|
|
352
|
+
* is very commonly used.
|
|
353
|
+
*
|
|
354
|
+
* @param rule rule to repeat, zero or more times
|
|
355
|
+
*/
|
|
356
|
+
export declare function repeat(rule: RuleOrLiteral): RepeatRule;
|
|
357
|
+
|
|
358
|
+
/**
|
|
359
|
+
* Creates a rule that matches one-or-more occurrences of a given rule.
|
|
360
|
+
*
|
|
361
|
+
* @param rule rule to repeat, one or more times
|
|
362
|
+
*/
|
|
363
|
+
export declare function repeat1(rule: RuleOrLiteral): Repeat1Rule;
|
|
364
|
+
|
|
365
|
+
/**
|
|
366
|
+
* Overrides the global reserved word set for a given rule. The word set name
|
|
367
|
+
* should be defined in the `reserved` field in the grammar.
|
|
368
|
+
*
|
|
369
|
+
* @param wordset name of the reserved word set
|
|
370
|
+
* @param rule rule that will use the reserved word set
|
|
371
|
+
*/
|
|
372
|
+
export declare function reserved(wordset: string, rule: RuleOrLiteral): ReservedRule;
|
|
373
|
+
|
|
374
|
+
/**
|
|
375
|
+
* Creates a rule that matches any number of other rules, one after another.
|
|
376
|
+
* It is analogous to simply writing multiple symbols next to each other
|
|
377
|
+
* in EBNF notation.
|
|
378
|
+
*
|
|
379
|
+
* @param rules ordered rules that comprise the sequence
|
|
380
|
+
*/
|
|
381
|
+
export declare function seq(...rules: RuleOrLiteral[]): SeqRule;
|
|
382
|
+
|
|
383
|
+
/**
|
|
384
|
+
* Creates a symbol rule, representing another rule in the grammar by name.
|
|
385
|
+
*
|
|
386
|
+
* @param name name of the target rule
|
|
387
|
+
*/
|
|
388
|
+
export declare function sym<Name extends string>(name: Name): SymbolRule<Name>;
|
|
389
|
+
|
|
390
|
+
/**
|
|
391
|
+
* Marks the given rule as producing only a single token. Tree-sitter's
|
|
392
|
+
* default is to treat each string or RegExp literal in the grammar as a
|
|
393
|
+
* separate token. Each token is matched separately by the lexer and
|
|
394
|
+
* returned as its own leaf node in the tree. The token function allows
|
|
395
|
+
* you to express a complex rule using the DSL functions (rather
|
|
396
|
+
* than as a single regular expression) but still have Tree-sitter treat
|
|
397
|
+
* it as a single token.
|
|
398
|
+
*
|
|
399
|
+
* @param rule rule to represent as a single token
|
|
400
|
+
*/
|
|
401
|
+
export declare const token: {
|
|
402
|
+
(rule: RuleOrLiteral): TokenRule;
|
|
403
|
+
|
|
404
|
+
/**
|
|
405
|
+
* Marks the given rule as producing an immediate token. This allows
|
|
406
|
+
* the parser to produce a different token based on whether or not
|
|
407
|
+
* there are `extras` preceding the token's main content. When there
|
|
408
|
+
* are _no_ leading `extras`, an immediate token is preferred over a
|
|
409
|
+
* normal token which would otherwise match.
|
|
410
|
+
*
|
|
411
|
+
* @param rule rule to represent as an immediate token
|
|
412
|
+
*/
|
|
413
|
+
immediate(rule: RuleOrLiteral): ImmediateTokenRule;
|
|
414
|
+
};
|
|
415
|
+
|
|
416
|
+
/**
|
|
417
|
+
* Matches the end of input. May only appear as the final symbol of a
|
|
418
|
+
* (possibly nested) sequence; a production ending in `eof()` reduces only
|
|
419
|
+
* when the lookahead is end-of-input, rather than shifting a token.
|
|
420
|
+
*
|
|
421
|
+
* Choice branches that continue past `eof()` are dropped as unreachable,
|
|
422
|
+
* and `eof()` is not allowed inside `token()`.
|
|
423
|
+
*
|
|
424
|
+
* Useful when a rule should match either an explicit terminator (e.g. a
|
|
425
|
+
* newline) or the end of the file.
|
|
426
|
+
*/
|
|
427
|
+
export declare function eof(): EOFRule;
|
|
428
|
+
|
|
429
|
+
/**
|
|
430
|
+
* Creates a new language grammar with the provided schema.
|
|
431
|
+
*
|
|
432
|
+
* @param options grammar options
|
|
433
|
+
*/
|
|
434
|
+
export declare function grammar<RuleName extends string>(
|
|
435
|
+
options: Grammar<RuleName>,
|
|
436
|
+
): GrammarSchema<RuleName>;
|
|
437
|
+
|
|
438
|
+
/**
|
|
439
|
+
* Extends an existing language grammar with the provided options,
|
|
440
|
+
* creating a new language.
|
|
441
|
+
*
|
|
442
|
+
* @param baseGrammar base grammar schema to extend from
|
|
443
|
+
* @param options grammar options for the new extended language
|
|
444
|
+
*/
|
|
445
|
+
export declare function grammar<
|
|
446
|
+
BaseGrammarRuleName extends string,
|
|
447
|
+
RuleName extends string
|
|
448
|
+
>(
|
|
449
|
+
baseGrammar: GrammarSchema<BaseGrammarRuleName>,
|
|
450
|
+
options: Grammar<RuleName, BaseGrammarRuleName>,
|
|
451
|
+
): GrammarSchema<RuleName | BaseGrammarRuleName>;
|