@konomanoasa/tree-sitter-lua 0.0.0-stage → 0.2.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.
@@ -0,0 +1,235 @@
1
+ ((identifier) @variable
2
+ (#not-eq? @variable ""))
3
+
4
+ ([
5
+ "0x"
6
+ "0X"
7
+ (decimal_digits)
8
+ (hex_digits)
9
+ ] @number
10
+ (#not-eq? @number ""))
11
+
12
+ ([
13
+ (true)
14
+ (false)
15
+ ] @boolean
16
+ (#not-eq? @boolean ""))
17
+
18
+ ((nil) @constant.builtin
19
+ (#not-eq? @constant.builtin ""))
20
+
21
+ ([
22
+ (hash_line)
23
+ (short_comment)
24
+ (long_comment_open)
25
+ (comment_content)
26
+ ] @comment
27
+ (#not-eq? @comment ""))
28
+
29
+ (long_comment
30
+ (long_bracket_close) @comment
31
+ (#not-eq? @comment ""))
32
+
33
+ ([
34
+ "'"
35
+ "\""
36
+ (string_content)
37
+ (long_bracket_open)
38
+ ] @string
39
+ (#not-eq? @string ""))
40
+
41
+ (long_string
42
+ (long_bracket_close) @string
43
+ (#not-eq? @string ""))
44
+
45
+ ([
46
+ (escape_sequence
47
+ !value)
48
+ (code_point)
49
+ ] @string.escape
50
+ (#not-eq? @string.escape ""))
51
+
52
+ ([
53
+ "do"
54
+ "end"
55
+ "global"
56
+ "goto"
57
+ "local"
58
+ ] @keyword
59
+ (#not-eq? @keyword ""))
60
+
61
+ ([
62
+ "else"
63
+ "elseif"
64
+ "if"
65
+ "then"
66
+ ] @keyword.conditional
67
+ (#not-eq? @keyword.conditional ""))
68
+
69
+ ([
70
+ "for"
71
+ "in"
72
+ "repeat"
73
+ "until"
74
+ "while"
75
+ (break_statement)
76
+ ] @keyword.repeat
77
+ (#not-eq? @keyword.repeat ""))
78
+
79
+ ("function" @keyword.function
80
+ (#not-eq? @keyword.function ""))
81
+
82
+ ("return" @keyword.return
83
+ (#not-eq? @keyword.return ""))
84
+
85
+ ([
86
+ "and"
87
+ "not"
88
+ "or"
89
+ ] @keyword.operator
90
+ (#not-eq? @keyword.operator ""))
91
+
92
+ ([
93
+ "+"
94
+ "-"
95
+ "*"
96
+ "/"
97
+ "//"
98
+ "%"
99
+ "^"
100
+ "#"
101
+ "&"
102
+ "|"
103
+ "~"
104
+ "<<"
105
+ ">>"
106
+ ".."
107
+ "<"
108
+ ">"
109
+ "<="
110
+ ">="
111
+ "~="
112
+ "=="
113
+ "="
114
+ ] @operator
115
+ (#not-eq? @operator ""))
116
+
117
+ ([
118
+ "("
119
+ ")"
120
+ "["
121
+ "]"
122
+ "{"
123
+ "}"
124
+ ] @punctuation.bracket
125
+ (#not-eq? @punctuation.bracket ""))
126
+
127
+ ([
128
+ ","
129
+ ";"
130
+ "."
131
+ ":"
132
+ "::"
133
+ ] @punctuation.delimiter
134
+ (#not-eq? @punctuation.delimiter ""))
135
+
136
+ (number
137
+ "." @number
138
+ (#not-eq? @number ""))
139
+
140
+ (exponent_part
141
+ [
142
+ "e"
143
+ "E"
144
+ "p"
145
+ "P"
146
+ "+"
147
+ "-"
148
+ ] @number
149
+ (#not-eq? @number ""))
150
+
151
+ (escape_sequence
152
+ [
153
+ "\\u"
154
+ "{"
155
+ "}"
156
+ ] @string.escape
157
+ (#not-eq? @string.escape ""))
158
+
159
+ (attribute
160
+ [
161
+ "<"
162
+ ">"
163
+ ] @punctuation.bracket
164
+ (#not-eq? @punctuation.bracket ""))
165
+
166
+ (attribute
167
+ name: (identifier) @attribute
168
+ (#not-eq? @attribute ""))
169
+
170
+ (vararg_expression
171
+ "..." @variable.builtin
172
+ (#not-eq? @variable.builtin ""))
173
+
174
+ (vararg_parameter
175
+ "..." @punctuation.special
176
+ (#not-eq? @punctuation.special ""))
177
+
178
+ (global_declaration
179
+ all: "*" @punctuation.special
180
+ (#not-eq? @punctuation.special ""))
181
+
182
+ (parameters
183
+ names: (name_list
184
+ (identifier) @variable.parameter)
185
+ (#not-eq? @variable.parameter ""))
186
+
187
+ (vararg_parameter
188
+ name: (identifier) @variable.parameter
189
+ (#not-eq? @variable.parameter ""))
190
+
191
+ (field_expression
192
+ field: (identifier) @variable.member
193
+ (#not-eq? @variable.member ""))
194
+
195
+ (table_field
196
+ key: (identifier) @variable.member
197
+ (#not-eq? @variable.member ""))
198
+
199
+ (table_field
200
+ "["
201
+ key: (identifier) @variable
202
+ (#not-eq? @variable ""))
203
+
204
+ (label_statement
205
+ name: (identifier) @label
206
+ (#not-eq? @label ""))
207
+
208
+ (goto_statement
209
+ name: (identifier) @label
210
+ (#not-eq? @label ""))
211
+
212
+ (function_declaration
213
+ name: (identifier) @function
214
+ (#not-eq? @function ""))
215
+
216
+ (function_name
217
+ name: (identifier) @function
218
+ (#not-eq? @function ""))
219
+
220
+ (function_name
221
+ method: (identifier) @function.method
222
+ (#not-eq? @function.method ""))
223
+
224
+ (function_call
225
+ !method
226
+ function: [
227
+ (identifier) @function.call
228
+ (field_expression
229
+ field: (identifier) @function.call)
230
+ ]
231
+ (#not-eq? @function.call ""))
232
+
233
+ (function_call
234
+ method: (identifier) @function.method.call
235
+ (#not-eq? @function.method.call ""))
@@ -0,0 +1,171 @@
1
+ import { spawn, spawnSync } from "node:child_process";
2
+ import { once } from "node:events";
3
+ import {
4
+ cpSync,
5
+ mkdirSync,
6
+ mkdtempDisposableSync,
7
+ readFileSync,
8
+ writeFileSync,
9
+ } from "node:fs";
10
+ import { tmpdir } from "node:os";
11
+ import { dirname, join, resolve } from "node:path";
12
+ import { createInterface } from "node:readline";
13
+ import { fileURLToPath } from "node:url";
14
+
15
+ export const root = resolve(import.meta.dirname, "..");
16
+ const cli = fileURLToPath(
17
+ new URL(
18
+ process.platform === "win32" ? "tree-sitter.exe" : "tree-sitter",
19
+ import.meta.resolve("tree-sitter-cli/package.json"),
20
+ ),
21
+ );
22
+ const libraryName = process.platform === "win32" ? "lua.dll" : "lua.so";
23
+
24
+ const queryTimeoutMs = 60_000;
25
+
26
+ export function runTreeSitter(args, options = {}) {
27
+ using scratch = mkdtempDisposableSync(join(tmpdir(), "lua-cli-"));
28
+ writeFileSync(
29
+ join(scratch.path, "config.json"),
30
+ JSON.stringify({ "parser-directories": [] }),
31
+ );
32
+ return spawnSync(cli, args, {
33
+ cwd: root,
34
+ encoding: "utf8",
35
+ ...options,
36
+ env: {
37
+ ...process.env,
38
+ TMPDIR: scratch.path,
39
+ TMP: scratch.path,
40
+ TEMP: scratch.path,
41
+ TREE_SITTER_DIR: scratch.path,
42
+ TREE_SITTER_LIBDIR: scratch.path,
43
+ ...(process.platform === "darwin"
44
+ ? { CC: process.env.CC ?? "/opt/homebrew/opt/llvm/bin/clang" }
45
+ : {}),
46
+ NO_COLOR: "1",
47
+ TREE_SITTER_SEED: process.env.TREE_SITTER_SEED ?? "1",
48
+ ...options.env,
49
+ },
50
+ });
51
+ }
52
+
53
+ export function buildParser(output) {
54
+ mkdirSync(dirname(output), { recursive: true });
55
+ const result = runTreeSitter(["build", "--output", output, root]);
56
+ if (result.error) throw result.error;
57
+ if (result.status !== 0)
58
+ throw new Error(result.stderr || result.stdout || "Parser build failed.");
59
+ }
60
+
61
+ function tokenHeader(externals) {
62
+ return [
63
+ "/* Generated by npm run generate. */",
64
+ "#ifndef TREE_SITTER_LUA_TOKENS_H",
65
+ "#define TREE_SITTER_LUA_TOKENS_H",
66
+ "",
67
+ "enum Token {",
68
+ ...externals.map(
69
+ ({ name }) => ` ${name.replace(/^_/, "").toUpperCase()},`,
70
+ ),
71
+ " TOKEN_COUNT",
72
+ "};",
73
+ "",
74
+ "#endif",
75
+ "",
76
+ ].join("\n");
77
+ }
78
+
79
+ export function generateParsers(outputRoot = root) {
80
+ const src = join(outputRoot, "src");
81
+ mkdirSync(src, { recursive: true });
82
+ const result = runTreeSitter(
83
+ ["generate", join(root, "grammar.js"), "--abi", "latest", "--output", src],
84
+ {
85
+ stdio: "inherit",
86
+ },
87
+ );
88
+ if (result.error) throw result.error;
89
+ if (result.status !== 0) throw new Error("Parser generation failed.");
90
+
91
+ const grammar = JSON.parse(readFileSync(join(src, "grammar.json"), "utf8"));
92
+ writeFileSync(join(src, "tokens.h"), tokenHeader(grammar.externals));
93
+ }
94
+
95
+ function testCorpus(args) {
96
+ using directory = mkdtempDisposableSync(join(tmpdir(), "lua-corpus-"));
97
+ const keepOutput = args.some(
98
+ (arg) =>
99
+ ["--update", "--debug-graph", "--open-log"].includes(arg) ||
100
+ /^-[d0rh]*[uD]/.test(arg),
101
+ );
102
+ const cwd = keepOutput ? root : directory.path;
103
+ if (!keepOutput) {
104
+ for (const path of [
105
+ "grammar.js",
106
+ "package.json",
107
+ "queries",
108
+ "src",
109
+ "test/corpus",
110
+ "tree-sitter.json",
111
+ ])
112
+ cpSync(join(root, path), join(cwd, path), { recursive: true });
113
+ }
114
+ const result = runTreeSitter(["test", ...args], { cwd, stdio: "inherit" });
115
+ if (result.error) throw result.error;
116
+ return result.status ?? 1;
117
+ }
118
+
119
+ async function fuzzParser(args) {
120
+ const child = spawn(cli, ["fuzz", ...args], {
121
+ stdio: ["inherit", "inherit", "pipe"],
122
+ env: {
123
+ ...process.env,
124
+ TREE_SITTER_SEED: process.env.TREE_SITTER_SEED ?? "1",
125
+ },
126
+ });
127
+ let failed = false;
128
+ child.stderr.pipe(process.stderr);
129
+ const lines = createInterface({ input: child.stderr, crlfDelay: Infinity });
130
+ lines.on("line", (line) => {
131
+ if (/^[1-9][0-9]* .+ corpus tests failed fuzzing$/.test(line))
132
+ failed = true;
133
+ });
134
+ const [status] = await once(child, "close");
135
+ return status === 0 && failed ? 1 : (status ?? 1);
136
+ }
137
+
138
+ if (import.meta.main) {
139
+ const args = process.argv.slice(2);
140
+ if (["generate-all", "build-all"].includes(args[0]) && args.length !== 1)
141
+ throw new Error(`Usage: node scripts/tree-sitter.js ${args[0]}`);
142
+ if (args[0] === "build-all") buildParser(join(root, "parsers", libraryName));
143
+ else if (args[0] === "generate-all") generateParsers();
144
+ else if (args[0] === "test-corpus")
145
+ process.exitCode = testCorpus(args.slice(1));
146
+ else if (args[0] === "fuzz-all")
147
+ process.exitCode = await fuzzParser([
148
+ "--grammar-path",
149
+ root,
150
+ ...args.slice(1),
151
+ ]);
152
+ else if (args[0] === "fuzz")
153
+ process.exitCode = await fuzzParser(args.slice(1));
154
+ else if (args[0] === "check-queries") {
155
+ const result = spawnSync("ts_query_ls", ["check", "--format", "queries"], {
156
+ cwd: root,
157
+ timeout: queryTimeoutMs,
158
+ killSignal: "SIGKILL",
159
+ stdio: "inherit",
160
+ });
161
+ if (result.error) throw result.error;
162
+ process.exitCode = result.status ?? 1;
163
+ } else {
164
+ const result = runTreeSitter(args, {
165
+ stdio: "inherit",
166
+ cwd: process.cwd(),
167
+ });
168
+ if (result.error) throw result.error;
169
+ process.exitCode = result.status ?? 1;
170
+ }
171
+ }
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_lua.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
+ )