@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/package.json
CHANGED
|
@@ -1,6 +1,72 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@konomanoasa/tree-sitter-c",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Tree-sitter grammar for C.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": "git+https://github.com/konomanoasa/tree-sitter-c.git",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./bindings/node/index.d.ts",
|
|
11
|
+
"default": "./bindings/node/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"PARSING_POLICY.md",
|
|
16
|
+
"Cargo.toml",
|
|
17
|
+
"binding.gyp",
|
|
18
|
+
"bindings/node/*.cc",
|
|
19
|
+
"bindings/node/*.js",
|
|
20
|
+
"bindings/node/*.ts",
|
|
21
|
+
"bindings/python/*.c",
|
|
22
|
+
"bindings/python/*.py",
|
|
23
|
+
"bindings/python/*.pyi",
|
|
24
|
+
"bindings/python/py.typed",
|
|
25
|
+
"bindings/rust/*.rs",
|
|
26
|
+
"grammar.js",
|
|
27
|
+
"pyproject.toml",
|
|
28
|
+
"queries/*.scm",
|
|
29
|
+
"scripts/tree-sitter.js",
|
|
30
|
+
"setup.py",
|
|
31
|
+
"src/*.c",
|
|
32
|
+
"src/*.h",
|
|
33
|
+
"src/*.json",
|
|
34
|
+
"src/tree_sitter/*.h",
|
|
35
|
+
"test/bindings.test.rs",
|
|
36
|
+
"tree-sitter.json"
|
|
37
|
+
],
|
|
38
|
+
"publishConfig": {
|
|
39
|
+
"access": "public"
|
|
40
|
+
},
|
|
41
|
+
"scripts": {
|
|
42
|
+
"build": "node-gyp rebuild && node-gyp configure -- -f compile_commands_json",
|
|
43
|
+
"build:parser": "npm run tree-sitter -- build-all",
|
|
44
|
+
"check": "biome check . && tsc",
|
|
45
|
+
"check:queries": "npm run tree-sitter -- check-queries",
|
|
46
|
+
"format": "biome format --write .",
|
|
47
|
+
"generate": "npm run tree-sitter -- generate-all",
|
|
48
|
+
"install": "node-gyp-build",
|
|
49
|
+
"test": "npm run tree-sitter -- test-corpus --rebuild && node --test --test-concurrency=1 test/bindings.test.js test/build.test.js test/highlight.test.js test/incremental.test.js test/parser.test.js && node scripts/check-c.js --test",
|
|
50
|
+
"test:fuzz": "npm run tree-sitter -- fuzz-all --iterations 100 --edits 5",
|
|
51
|
+
"test:sanitize": "node scripts/check-c.js --sanitize",
|
|
52
|
+
"tree-sitter": "node scripts/tree-sitter.js"
|
|
53
|
+
},
|
|
54
|
+
"dependencies": {
|
|
55
|
+
"node-addon-api": "8.9.2",
|
|
56
|
+
"node-gyp-build": "4.8.4"
|
|
57
|
+
},
|
|
58
|
+
"peerDependencies": {
|
|
59
|
+
"tree-sitter": "0.25.1"
|
|
60
|
+
},
|
|
61
|
+
"devDependencies": {
|
|
62
|
+
"@biomejs/biome": "2.6.9",
|
|
63
|
+
"@types/node": "26.6.11",
|
|
64
|
+
"node-gyp": "13.1.0",
|
|
65
|
+
"tree-sitter-cli": "0.27.2",
|
|
66
|
+
"typescript": "7.0.2"
|
|
67
|
+
},
|
|
68
|
+
"allowScripts": {
|
|
69
|
+
"tree-sitter": true,
|
|
70
|
+
"tree-sitter-cli": true
|
|
71
|
+
}
|
|
72
|
+
}
|
package/pyproject.toml
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "konomanoasa-tree-sitter-c"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Tree-sitter grammar for C."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
license-files = ["LICENSE"]
|
|
8
|
+
requires-python = ">=3.13"
|
|
9
|
+
|
|
10
|
+
[project.urls]
|
|
11
|
+
Repository = "https://github.com/konomanoasa/tree-sitter-c"
|
|
12
|
+
|
|
13
|
+
[dependency-groups]
|
|
14
|
+
dev = [
|
|
15
|
+
"poethepoet==0.49.0",
|
|
16
|
+
"ruff==0.17.6",
|
|
17
|
+
"setuptools==84.2.1",
|
|
18
|
+
"tree-sitter==0.27.0",
|
|
19
|
+
"twine==7.0.0",
|
|
20
|
+
"ty==0.0.112",
|
|
21
|
+
]
|
|
22
|
+
|
|
23
|
+
[build-system]
|
|
24
|
+
requires = ["setuptools==84.2.1"]
|
|
25
|
+
build-backend = "setuptools.build_meta"
|
|
26
|
+
|
|
27
|
+
[tool.setuptools]
|
|
28
|
+
include-package-data = false
|
|
29
|
+
packages = ["tree_sitter_c", "tree_sitter_c.queries"]
|
|
30
|
+
|
|
31
|
+
[tool.setuptools.package-dir]
|
|
32
|
+
tree_sitter_c = "bindings/python"
|
|
33
|
+
"tree_sitter_c.queries" = "queries"
|
|
34
|
+
|
|
35
|
+
[tool.setuptools.package-data]
|
|
36
|
+
tree_sitter_c = ["*.pyi", "py.typed"]
|
|
37
|
+
"tree_sitter_c.queries" = ["*.scm"]
|
|
38
|
+
|
|
39
|
+
[tool.poe]
|
|
40
|
+
executor.type = "simple"
|
|
41
|
+
|
|
42
|
+
[tool.poe.tasks]
|
|
43
|
+
build = "uv build --no-sources"
|
|
44
|
+
check = [
|
|
45
|
+
{ cmd = "ruff check setup.py bindings/python test/bindings.test.py" },
|
|
46
|
+
{ cmd = "ty check setup.py bindings/python test/bindings.test.py" },
|
|
47
|
+
{ cmd = "ruff format --check setup.py bindings/python test/bindings.test.py" },
|
|
48
|
+
]
|
|
49
|
+
format = "ruff format setup.py bindings/python test/bindings.test.py"
|
|
50
|
+
test = "python test/bindings.test.py"
|
|
51
|
+
|
|
52
|
+
[tool.ruff]
|
|
53
|
+
indent-width = 4
|
|
54
|
+
line-length = 80
|
|
55
|
+
|
|
56
|
+
[tool.ruff.format]
|
|
57
|
+
indent-style = "space"
|
|
58
|
+
skip-magic-trailing-comma = true
|
|
59
|
+
|
|
60
|
+
[tool.ruff.lint.isort]
|
|
61
|
+
split-on-trailing-comma = false
|
|
62
|
+
|
|
63
|
+
[tool.uv]
|
|
64
|
+
cache-keys = [
|
|
65
|
+
{ file = "LICENSE" },
|
|
66
|
+
{ file = "README.md" },
|
|
67
|
+
{ file = "bindings/python/*" },
|
|
68
|
+
{ file = "pyproject.toml" },
|
|
69
|
+
{ file = "queries/*.scm" },
|
|
70
|
+
{ file = "setup.py" },
|
|
71
|
+
{ file = "src/*.c" },
|
|
72
|
+
{ file = "src/*.h" },
|
|
73
|
+
{ file = "src/tree_sitter/*.h" },
|
|
74
|
+
]
|
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
(comment) @comment
|
|
2
|
+
|
|
3
|
+
(pp_number) @number
|
|
4
|
+
|
|
5
|
+
(integer_constant
|
|
6
|
+
[
|
|
7
|
+
(base_prefix)
|
|
8
|
+
(integer_digits)
|
|
9
|
+
(integer_suffix)
|
|
10
|
+
(line_splice)
|
|
11
|
+
] @number)
|
|
12
|
+
|
|
13
|
+
(floating_constant
|
|
14
|
+
[
|
|
15
|
+
(base_prefix)
|
|
16
|
+
(floating_suffix)
|
|
17
|
+
(line_splice)
|
|
18
|
+
] @number.float)
|
|
19
|
+
|
|
20
|
+
(significand
|
|
21
|
+
[
|
|
22
|
+
(significand_digits)
|
|
23
|
+
(decimal_point)
|
|
24
|
+
(line_splice)
|
|
25
|
+
] @number.float)
|
|
26
|
+
|
|
27
|
+
(exponent
|
|
28
|
+
[
|
|
29
|
+
(exponent_marker)
|
|
30
|
+
(exponent_sign)
|
|
31
|
+
(exponent_digits)
|
|
32
|
+
(line_splice)
|
|
33
|
+
] @number.float)
|
|
34
|
+
|
|
35
|
+
(character_constant
|
|
36
|
+
[
|
|
37
|
+
(encoding_prefix)
|
|
38
|
+
"'"
|
|
39
|
+
(character_content)
|
|
40
|
+
(line_splice)
|
|
41
|
+
] @character)
|
|
42
|
+
|
|
43
|
+
(string_literal
|
|
44
|
+
[
|
|
45
|
+
(encoding_prefix)
|
|
46
|
+
"\""
|
|
47
|
+
(string_content)
|
|
48
|
+
(line_splice)
|
|
49
|
+
] @string)
|
|
50
|
+
|
|
51
|
+
(escape_sequence) @string.escape
|
|
52
|
+
|
|
53
|
+
(header_name) @string
|
|
54
|
+
|
|
55
|
+
[
|
|
56
|
+
"("
|
|
57
|
+
")"
|
|
58
|
+
"["
|
|
59
|
+
"]"
|
|
60
|
+
"{"
|
|
61
|
+
"}"
|
|
62
|
+
] @punctuation.bracket
|
|
63
|
+
|
|
64
|
+
[
|
|
65
|
+
","
|
|
66
|
+
";"
|
|
67
|
+
":"
|
|
68
|
+
"..."
|
|
69
|
+
"."
|
|
70
|
+
] @punctuation.delimiter
|
|
71
|
+
|
|
72
|
+
[
|
|
73
|
+
"void"
|
|
74
|
+
"char"
|
|
75
|
+
"short"
|
|
76
|
+
"int"
|
|
77
|
+
"long"
|
|
78
|
+
"float"
|
|
79
|
+
"double"
|
|
80
|
+
"signed"
|
|
81
|
+
"unsigned"
|
|
82
|
+
"_Bool"
|
|
83
|
+
"_Complex"
|
|
84
|
+
"_Imaginary"
|
|
85
|
+
] @type.builtin
|
|
86
|
+
|
|
87
|
+
[
|
|
88
|
+
"struct"
|
|
89
|
+
"union"
|
|
90
|
+
"enum"
|
|
91
|
+
"typedef"
|
|
92
|
+
] @keyword.type
|
|
93
|
+
|
|
94
|
+
(atomic_type_specifier
|
|
95
|
+
"_Atomic" @keyword.type)
|
|
96
|
+
|
|
97
|
+
[
|
|
98
|
+
"auto"
|
|
99
|
+
"register"
|
|
100
|
+
"static"
|
|
101
|
+
"_Thread_local"
|
|
102
|
+
"extern"
|
|
103
|
+
"const"
|
|
104
|
+
"restrict"
|
|
105
|
+
"volatile"
|
|
106
|
+
"inline"
|
|
107
|
+
"_Noreturn"
|
|
108
|
+
"_Alignas"
|
|
109
|
+
] @keyword.modifier
|
|
110
|
+
|
|
111
|
+
(type_qualifier
|
|
112
|
+
"_Atomic" @keyword.modifier)
|
|
113
|
+
|
|
114
|
+
[
|
|
115
|
+
"if"
|
|
116
|
+
"else"
|
|
117
|
+
"switch"
|
|
118
|
+
"case"
|
|
119
|
+
"default"
|
|
120
|
+
"_Generic"
|
|
121
|
+
] @keyword.conditional
|
|
122
|
+
|
|
123
|
+
[
|
|
124
|
+
"for"
|
|
125
|
+
"while"
|
|
126
|
+
"do"
|
|
127
|
+
] @keyword.repeat
|
|
128
|
+
|
|
129
|
+
"return" @keyword.return
|
|
130
|
+
|
|
131
|
+
[
|
|
132
|
+
"goto"
|
|
133
|
+
"continue"
|
|
134
|
+
"break"
|
|
135
|
+
"_Static_assert"
|
|
136
|
+
] @keyword
|
|
137
|
+
|
|
138
|
+
[
|
|
139
|
+
"sizeof"
|
|
140
|
+
"_Alignof"
|
|
141
|
+
] @keyword.operator
|
|
142
|
+
|
|
143
|
+
(_
|
|
144
|
+
operator: _ @operator)
|
|
145
|
+
|
|
146
|
+
(pointer
|
|
147
|
+
"*" @operator)
|
|
148
|
+
|
|
149
|
+
(conditional_expression
|
|
150
|
+
[
|
|
151
|
+
"?"
|
|
152
|
+
":"
|
|
153
|
+
] @operator)
|
|
154
|
+
|
|
155
|
+
[
|
|
156
|
+
(init_declarator
|
|
157
|
+
"=" @operator)
|
|
158
|
+
(designation
|
|
159
|
+
"=" @operator)
|
|
160
|
+
(enumerator
|
|
161
|
+
"=" @operator)
|
|
162
|
+
]
|
|
163
|
+
|
|
164
|
+
(preprocessing_token
|
|
165
|
+
[
|
|
166
|
+
"#"
|
|
167
|
+
"##"
|
|
168
|
+
] @operator)
|
|
169
|
+
|
|
170
|
+
[
|
|
171
|
+
(primary_expression
|
|
172
|
+
(identifier) @variable)
|
|
173
|
+
(direct_declarator
|
|
174
|
+
(identifier) @variable)
|
|
175
|
+
]
|
|
176
|
+
|
|
177
|
+
(typedef_name
|
|
178
|
+
(identifier) @type)
|
|
179
|
+
|
|
180
|
+
(_
|
|
181
|
+
tag: (identifier) @type)
|
|
182
|
+
|
|
183
|
+
(enumerator
|
|
184
|
+
name: (identifier) @constant)
|
|
185
|
+
|
|
186
|
+
(_
|
|
187
|
+
label: (identifier) @label)
|
|
188
|
+
|
|
189
|
+
(_
|
|
190
|
+
member: (identifier) @property)
|
|
191
|
+
|
|
192
|
+
(designator
|
|
193
|
+
(identifier) @property)
|
|
194
|
+
|
|
195
|
+
(struct_declarator
|
|
196
|
+
(declarator
|
|
197
|
+
(direct_declarator
|
|
198
|
+
(identifier) @property)))
|
|
199
|
+
|
|
200
|
+
(parameter_declaration
|
|
201
|
+
declarator: (declarator
|
|
202
|
+
(direct_declarator
|
|
203
|
+
(identifier) @variable.parameter)))
|
|
204
|
+
|
|
205
|
+
(function_definition
|
|
206
|
+
declarator: (declarator
|
|
207
|
+
(direct_declarator
|
|
208
|
+
(direct_declarator
|
|
209
|
+
(identifier))
|
|
210
|
+
parameters: (parameter_identifiers
|
|
211
|
+
(identifier) @variable.parameter)))
|
|
212
|
+
declarations: (declaration_list))
|
|
213
|
+
|
|
214
|
+
(direct_declarator
|
|
215
|
+
(direct_declarator
|
|
216
|
+
(identifier) @function)
|
|
217
|
+
"(")
|
|
218
|
+
|
|
219
|
+
(postfix_expression
|
|
220
|
+
base: (primary_expression
|
|
221
|
+
(identifier) @function.call)
|
|
222
|
+
"(")
|
|
223
|
+
|
|
224
|
+
(macro_invocation_candidate
|
|
225
|
+
name: (identifier) @function.call)
|
|
226
|
+
|
|
227
|
+
[
|
|
228
|
+
(control_line
|
|
229
|
+
"#" @keyword.directive)
|
|
230
|
+
(non_directive
|
|
231
|
+
"#" @keyword.directive)
|
|
232
|
+
(if_group
|
|
233
|
+
[
|
|
234
|
+
"#"
|
|
235
|
+
"if"
|
|
236
|
+
"ifdef"
|
|
237
|
+
"ifndef"
|
|
238
|
+
] @keyword.directive)
|
|
239
|
+
(elif_group
|
|
240
|
+
[
|
|
241
|
+
"#"
|
|
242
|
+
"elif"
|
|
243
|
+
] @keyword.directive)
|
|
244
|
+
(else_group
|
|
245
|
+
[
|
|
246
|
+
"#"
|
|
247
|
+
"else"
|
|
248
|
+
] @keyword.directive)
|
|
249
|
+
(endif_line
|
|
250
|
+
[
|
|
251
|
+
"#"
|
|
252
|
+
"endif"
|
|
253
|
+
] @keyword.directive)
|
|
254
|
+
]
|
|
255
|
+
|
|
256
|
+
[
|
|
257
|
+
"line"
|
|
258
|
+
"error"
|
|
259
|
+
"pragma"
|
|
260
|
+
"_Pragma"
|
|
261
|
+
] @keyword.directive
|
|
262
|
+
|
|
263
|
+
[
|
|
264
|
+
"define"
|
|
265
|
+
"undef"
|
|
266
|
+
] @keyword.directive.define
|
|
267
|
+
|
|
268
|
+
"include" @keyword.import
|
|
269
|
+
|
|
270
|
+
(control_line
|
|
271
|
+
name: (identifier) @constant.macro)
|
|
272
|
+
|
|
273
|
+
(if_group
|
|
274
|
+
condition: (identifier) @constant.macro)
|
|
275
|
+
|
|
276
|
+
(control_line
|
|
277
|
+
name: (identifier) @function.macro
|
|
278
|
+
parameters: (macro_parameters))
|
|
279
|
+
|
|
280
|
+
(macro_parameters
|
|
281
|
+
(identifier) @variable.parameter)
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import { spawnSync } from "node:child_process";
|
|
2
|
+
import {
|
|
3
|
+
cpSync,
|
|
4
|
+
mkdirSync,
|
|
5
|
+
mkdtempSync,
|
|
6
|
+
readFileSync,
|
|
7
|
+
rmSync,
|
|
8
|
+
writeFileSync,
|
|
9
|
+
} from "node:fs";
|
|
10
|
+
import { tmpdir } from "node:os";
|
|
11
|
+
import { dirname, join, resolve } from "node:path";
|
|
12
|
+
import { fileURLToPath } from "node:url";
|
|
13
|
+
|
|
14
|
+
import tokens from "../src/tokens.json" with { type: "json" };
|
|
15
|
+
|
|
16
|
+
export const root = resolve(import.meta.dirname, "..");
|
|
17
|
+
const cli = fileURLToPath(
|
|
18
|
+
new URL(
|
|
19
|
+
process.platform === "win32" ? "tree-sitter.exe" : "tree-sitter",
|
|
20
|
+
import.meta.resolve("tree-sitter-cli/package.json"),
|
|
21
|
+
),
|
|
22
|
+
);
|
|
23
|
+
const libraryName = process.platform === "win32" ? "c.dll" : "c.so";
|
|
24
|
+
|
|
25
|
+
const fuzzOutputLimitBytes = 16 * 1024 * 1024;
|
|
26
|
+
const fuzzTimeoutMs = 600_000;
|
|
27
|
+
const queryTimeoutMs = 60_000;
|
|
28
|
+
|
|
29
|
+
function runTreeSitter(args, options = {}) {
|
|
30
|
+
return spawnSync(cli, args, {
|
|
31
|
+
cwd: root,
|
|
32
|
+
encoding: "utf8",
|
|
33
|
+
...options,
|
|
34
|
+
env: {
|
|
35
|
+
...process.env,
|
|
36
|
+
...(process.platform === "darwin"
|
|
37
|
+
? { CC: process.env.CC ?? "/opt/homebrew/opt/llvm/bin/clang" }
|
|
38
|
+
: {}),
|
|
39
|
+
NO_COLOR: "1",
|
|
40
|
+
TREE_SITTER_SEED: process.env.TREE_SITTER_SEED ?? "1",
|
|
41
|
+
...options.env,
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function buildParser(output) {
|
|
47
|
+
mkdirSync(dirname(output), { recursive: true });
|
|
48
|
+
const result = runTreeSitter(["build", "--output", output, root]);
|
|
49
|
+
if (result.error) throw result.error;
|
|
50
|
+
if (result.status !== 0)
|
|
51
|
+
throw new Error(result.stderr || result.stdout || "Parser build failed.");
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function tokenHeader(externals) {
|
|
55
|
+
const { directives, keywords, punctuators, typeArgumentMacros } = tokens;
|
|
56
|
+
const symbol = (name) => `TOK_${name.replace(/^_/, "").toUpperCase()}`;
|
|
57
|
+
const table = (name, entries) =>
|
|
58
|
+
`static const Spelling ${name}[] = {\n${entries.map(([text, id]) => ` { ${JSON.stringify(text)}, ${symbol(id)} },`).join("\n")}\n};\n`;
|
|
59
|
+
const punctuation = Object.entries(punctuators).map(([name, text]) => [
|
|
60
|
+
text,
|
|
61
|
+
`_p_${name}`,
|
|
62
|
+
]);
|
|
63
|
+
punctuation.push(
|
|
64
|
+
["<:", "_p_lbracket"],
|
|
65
|
+
[":>", "_p_rbracket"],
|
|
66
|
+
["<%", "_p_lbrace"],
|
|
67
|
+
["%>", "_p_rbrace"],
|
|
68
|
+
["%:", "_p_hash"],
|
|
69
|
+
["%:%:", "_p_paste"],
|
|
70
|
+
);
|
|
71
|
+
return [
|
|
72
|
+
"/* Generated by npm run generate. */",
|
|
73
|
+
"#ifndef TREE_SITTER_C_TOKENS_H\n#define TREE_SITTER_C_TOKENS_H",
|
|
74
|
+
`typedef enum {\n${externals.map(({ name }) => ` ${symbol(name)},`).join("\n")}\n TOKEN_COUNT\n} Token;\n`,
|
|
75
|
+
"typedef struct { const char *text; Token token; } Spelling;\n",
|
|
76
|
+
table("source_words", [
|
|
77
|
+
...keywords.map((word) => [word, `_kw_${word}`]),
|
|
78
|
+
...typeArgumentMacros.map((word) => [word, `_lib_${word}`]),
|
|
79
|
+
]),
|
|
80
|
+
table(
|
|
81
|
+
"directive_names",
|
|
82
|
+
directives.map((word) => [word, `_pp_${word}`]),
|
|
83
|
+
),
|
|
84
|
+
table("punctuators", punctuation),
|
|
85
|
+
"#endif\n",
|
|
86
|
+
].join("\n");
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function generateParsers(outputRoot = root) {
|
|
90
|
+
const src = join(outputRoot, "src");
|
|
91
|
+
mkdirSync(src, { recursive: true });
|
|
92
|
+
const result = runTreeSitter(
|
|
93
|
+
["generate", join(root, "grammar.js"), "--abi", "latest", "--output", src],
|
|
94
|
+
{
|
|
95
|
+
stdio: "inherit",
|
|
96
|
+
},
|
|
97
|
+
);
|
|
98
|
+
if (result.error) throw result.error;
|
|
99
|
+
if (result.status !== 0) throw new Error("Parser generation failed.");
|
|
100
|
+
|
|
101
|
+
const grammar = JSON.parse(readFileSync(join(src, "grammar.json"), "utf8"));
|
|
102
|
+
writeFileSync(join(src, "tokens.h"), tokenHeader(grammar.externals));
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function testCorpus(args) {
|
|
106
|
+
if (
|
|
107
|
+
args.some(
|
|
108
|
+
(arg) =>
|
|
109
|
+
["--update", "--debug-graph", "--open-log"].includes(arg) ||
|
|
110
|
+
/^-[d0rh]*[uD]/.test(arg),
|
|
111
|
+
)
|
|
112
|
+
)
|
|
113
|
+
throw new Error(
|
|
114
|
+
"Use tree-sitter test directly to keep updates or debug output.",
|
|
115
|
+
);
|
|
116
|
+
const directory = mkdtempSync(join(tmpdir(), "c-corpus-"));
|
|
117
|
+
try {
|
|
118
|
+
for (const path of [
|
|
119
|
+
"grammar.js",
|
|
120
|
+
"package.json",
|
|
121
|
+
"queries",
|
|
122
|
+
"src",
|
|
123
|
+
"test/corpus",
|
|
124
|
+
"tree-sitter.json",
|
|
125
|
+
])
|
|
126
|
+
cpSync(join(root, path), join(directory, path), { recursive: true });
|
|
127
|
+
const result = runTreeSitter(["test", ...args], {
|
|
128
|
+
cwd: directory,
|
|
129
|
+
env: {
|
|
130
|
+
TREE_SITTER_LIBDIR: join(directory, "lib"),
|
|
131
|
+
TREE_SITTER_DIR: join(directory, "config"),
|
|
132
|
+
},
|
|
133
|
+
stdio: "inherit",
|
|
134
|
+
});
|
|
135
|
+
if (result.error) throw result.error;
|
|
136
|
+
return result.status ?? 1;
|
|
137
|
+
} finally {
|
|
138
|
+
rmSync(directory, { recursive: true, force: true });
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function fuzzParser(args) {
|
|
143
|
+
const directory = mkdtempSync(join(tmpdir(), "c-fuzz-"));
|
|
144
|
+
try {
|
|
145
|
+
const library = join(directory, libraryName);
|
|
146
|
+
buildParser(library);
|
|
147
|
+
const result = runTreeSitter(
|
|
148
|
+
["fuzz", "--lib-path", library, "--lang-name", "c", ...args],
|
|
149
|
+
{
|
|
150
|
+
maxBuffer: fuzzOutputLimitBytes,
|
|
151
|
+
timeout: fuzzTimeoutMs,
|
|
152
|
+
killSignal: "SIGKILL",
|
|
153
|
+
},
|
|
154
|
+
);
|
|
155
|
+
process.stdout.write(result.stdout ?? "");
|
|
156
|
+
process.stderr.write(result.stderr ?? "");
|
|
157
|
+
if (result.error) throw result.error;
|
|
158
|
+
if (result.status !== 0) return result.status ?? 1;
|
|
159
|
+
return /^[1-9][0-9]* .+ corpus tests failed fuzzing$/m.test(
|
|
160
|
+
result.stdout + result.stderr,
|
|
161
|
+
)
|
|
162
|
+
? 1
|
|
163
|
+
: 0;
|
|
164
|
+
} finally {
|
|
165
|
+
rmSync(directory, { recursive: true, force: true });
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
if (import.meta.main) {
|
|
170
|
+
const args = process.argv.slice(2);
|
|
171
|
+
if (["generate-all", "build-all"].includes(args[0]) && args.length !== 1)
|
|
172
|
+
throw new Error(`Usage: node scripts/tree-sitter.js ${args[0]}`);
|
|
173
|
+
if (args[0] === "build-all") buildParser(join(root, "parsers", libraryName));
|
|
174
|
+
else if (args[0] === "generate-all") generateParsers();
|
|
175
|
+
else if (args[0] === "test-corpus")
|
|
176
|
+
process.exitCode = testCorpus(args.slice(1));
|
|
177
|
+
else if (args[0] === "fuzz-all") process.exitCode = fuzzParser(args.slice(1));
|
|
178
|
+
else if (args[0] === "check-queries") {
|
|
179
|
+
const result = spawnSync("ts_query_ls", ["check", "--format", "queries"], {
|
|
180
|
+
cwd: root,
|
|
181
|
+
timeout: queryTimeoutMs,
|
|
182
|
+
killSignal: "SIGKILL",
|
|
183
|
+
stdio: "inherit",
|
|
184
|
+
});
|
|
185
|
+
if (result.error) throw result.error;
|
|
186
|
+
process.exitCode = result.status ?? 1;
|
|
187
|
+
} else {
|
|
188
|
+
const result = runTreeSitter(args, {
|
|
189
|
+
stdio: "inherit",
|
|
190
|
+
cwd: process.cwd(),
|
|
191
|
+
});
|
|
192
|
+
if (result.error) throw result.error;
|
|
193
|
+
process.exitCode = result.status ?? 1;
|
|
194
|
+
}
|
|
195
|
+
}
|
package/setup.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import sys
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
|
|
4
|
+
from setuptools import Extension, setup
|
|
5
|
+
|
|
6
|
+
Path("dist/metadata").mkdir(parents=True, exist_ok=True)
|
|
7
|
+
|
|
8
|
+
setup(
|
|
9
|
+
options={
|
|
10
|
+
"build": {"build_base": "dist/build"},
|
|
11
|
+
"egg_info": {"egg_base": "dist/metadata"},
|
|
12
|
+
},
|
|
13
|
+
ext_modules=[
|
|
14
|
+
Extension(
|
|
15
|
+
"tree_sitter_c.binding",
|
|
16
|
+
sources=[
|
|
17
|
+
"bindings/python/binding.c",
|
|
18
|
+
"src/parser.c",
|
|
19
|
+
"src/scanner.c",
|
|
20
|
+
],
|
|
21
|
+
include_dirs=["src"],
|
|
22
|
+
depends=[
|
|
23
|
+
"src/tokens.h",
|
|
24
|
+
"src/tree_sitter/alloc.h",
|
|
25
|
+
"src/tree_sitter/parser.h",
|
|
26
|
+
],
|
|
27
|
+
extra_compile_args=["/std:c17", "/utf-8"]
|
|
28
|
+
if sys.platform == "win32"
|
|
29
|
+
else ["-std=c17"],
|
|
30
|
+
)
|
|
31
|
+
],
|
|
32
|
+
)
|