@konomanoasa/tree-sitter-lua 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/grammar.js ADDED
@@ -0,0 +1,465 @@
1
+ function binaryLevels($) {
2
+ return [
3
+ { level: 1, operator: "or" },
4
+ { level: 2, operator: "and" },
5
+ {
6
+ level: 3,
7
+ operator: choice("<", alias($._greater, ">"), "<=", ">=", "~=", "=="),
8
+ },
9
+ { level: 4, operator: "|" },
10
+ { level: 5, operator: "~" },
11
+ { level: 6, operator: "&" },
12
+ { level: 7, operator: choice("<<", ">>") },
13
+ { level: 8, right: true, operator: alias($._concat, "..") },
14
+ { level: 9, operator: choice("+", "-") },
15
+ { level: 10, operator: choice("*", "/", "//", "%") },
16
+ { level: 12, right: true, operator: "^" },
17
+ ];
18
+ }
19
+ const unaryLevel = 11;
20
+ const prefixLevel = 13;
21
+
22
+ function separated(rule, separator) {
23
+ return seq(rule, repeat(seq(separator, rule)));
24
+ }
25
+
26
+ function mantissa(integer, digits) {
27
+ return choice(
28
+ seq(integer, optional(seq(".", optional(digits)))),
29
+ seq(".", digits),
30
+ );
31
+ }
32
+
33
+ function exponent(marker, digits) {
34
+ return seq(marker, optional(choice("+", "-")), digits);
35
+ }
36
+
37
+ function quoted($, quote, text) {
38
+ return seq(
39
+ quote,
40
+ repeat(choice(alias(text, $.string_content), $._escape)),
41
+ quote,
42
+ );
43
+ }
44
+
45
+ function namesWithAtMostOneClose(ordinary, closing) {
46
+ return seq(
47
+ repeat(seq(ordinary, ",")),
48
+ choice(ordinary, seq(closing, repeat(seq(",", ordinary)))),
49
+ );
50
+ }
51
+
52
+ function attribute($, name) {
53
+ return seq(
54
+ "<",
55
+ field("name", alias(name, $.identifier)),
56
+ alias($._greater, ">"),
57
+ );
58
+ }
59
+
60
+ function body($) {
61
+ return optional(field("body", $.block));
62
+ }
63
+
64
+ export default grammar({
65
+ name: "lua",
66
+ reserved: {
67
+ global: ($) => [
68
+ "and",
69
+ $.break_statement,
70
+ "do",
71
+ "else",
72
+ "elseif",
73
+ "end",
74
+ $.false,
75
+ "for",
76
+ "function",
77
+ "global",
78
+ "goto",
79
+ "if",
80
+ "in",
81
+ "local",
82
+ $.nil,
83
+ "not",
84
+ "or",
85
+ "repeat",
86
+ "return",
87
+ "then",
88
+ $.true,
89
+ "until",
90
+ "while",
91
+ ],
92
+ },
93
+ conflicts: ($) => [[$.name_list]],
94
+ word: ($) => $.identifier,
95
+ extras: ($) => [$._space, $.short_comment, $.long_comment],
96
+ externals: ($) => [
97
+ $._space,
98
+ $.decimal_digits,
99
+ $.hex_digits,
100
+ $._exponent_lower_e,
101
+ $._exponent_upper_e,
102
+ $._exponent_lower_p,
103
+ $._exponent_upper_p,
104
+ $._number_end,
105
+ $.short_comment,
106
+ $.long_comment_open,
107
+ $.long_bracket_open,
108
+ $._long_content,
109
+ $.long_bracket_close,
110
+ $._single_text,
111
+ $._double_text,
112
+ $._simple_escape,
113
+ $._unicode_escape_start,
114
+ $.code_point,
115
+ $._concat,
116
+ $._greater,
117
+ $._file_start,
118
+ $.hash_line,
119
+ $._error_sentinel,
120
+ ],
121
+ rules: {
122
+ chunk: ($) => seq(choice($._file_start, $.hash_line), optional($._block)),
123
+ _block: ($) =>
124
+ choice(
125
+ seq(repeat1($._statement), optional($.return_statement)),
126
+ $.return_statement,
127
+ ),
128
+ block: ($) => $._block,
129
+ _statement: ($) =>
130
+ choice(
131
+ $.empty_statement,
132
+ $.assignment_statement,
133
+ $.function_call,
134
+ $.label_statement,
135
+ $.break_statement,
136
+ $.goto_statement,
137
+ $.do_statement,
138
+ $.while_statement,
139
+ $.repeat_statement,
140
+ $.if_statement,
141
+ $.numeric_for_statement,
142
+ $.generic_for_statement,
143
+ $.function_declaration,
144
+ $.local_declaration,
145
+ $.global_declaration,
146
+ ),
147
+ empty_statement: () => ";",
148
+ assignment_statement: ($) =>
149
+ seq(
150
+ field("variables", $.variable_list),
151
+ "=",
152
+ field("values", $.expression_list),
153
+ ),
154
+ variable_list: ($) => separated($._variable, ","),
155
+ label_statement: ($) => seq("::", field("name", $.identifier), "::"),
156
+ break_statement: () => "break",
157
+ goto_statement: ($) => seq("goto", field("name", $.identifier)),
158
+ do_statement: ($) => seq("do", body($), "end"),
159
+ while_statement: ($) =>
160
+ seq("while", field("condition", $._expression), "do", body($), "end"),
161
+ repeat_statement: ($) =>
162
+ seq("repeat", body($), "until", field("condition", $._expression)),
163
+ if_statement: ($) =>
164
+ seq(
165
+ "if",
166
+ field("condition", $._expression),
167
+ "then",
168
+ body($),
169
+ repeat($.elseif_clause),
170
+ optional($.else_clause),
171
+ "end",
172
+ ),
173
+ elseif_clause: ($) =>
174
+ seq("elseif", field("condition", $._expression), "then", body($)),
175
+ else_clause: ($) => seq("else", body($)),
176
+ numeric_for_statement: ($) =>
177
+ seq(
178
+ "for",
179
+ field("name", $.identifier),
180
+ "=",
181
+ field("start", $._expression),
182
+ ",",
183
+ field("limit", $._expression),
184
+ optional(seq(",", field("step", $._expression))),
185
+ "do",
186
+ body($),
187
+ "end",
188
+ ),
189
+ generic_for_statement: ($) =>
190
+ seq(
191
+ "for",
192
+ field("names", $.name_list),
193
+ "in",
194
+ field("values", $.expression_list),
195
+ "do",
196
+ body($),
197
+ "end",
198
+ ),
199
+ name_list: ($) => separated($.identifier, ","),
200
+ local_declaration: ($) =>
201
+ seq(
202
+ "local",
203
+ field("names", $.attribute_name_list),
204
+ optional($._initializer),
205
+ ),
206
+ global_declaration: ($) =>
207
+ seq(
208
+ "global",
209
+ choice(
210
+ seq(
211
+ field("names", alias($._global_names, $.attribute_name_list)),
212
+ optional($._initializer),
213
+ ),
214
+ seq(
215
+ optional(
216
+ field("attribute", alias($._const_attribute, $.attribute)),
217
+ ),
218
+ field("all", "*"),
219
+ ),
220
+ ),
221
+ ),
222
+ _initializer: ($) => seq("=", field("values", $.expression_list)),
223
+ attribute_name_list: ($) => {
224
+ const plain = alias($._plain_name, $.attributed_name);
225
+ const constant = alias($._const_name, $.attributed_name);
226
+ const closing = alias($._close_name, $.attributed_name);
227
+ return choice(
228
+ seq(
229
+ optional(field("attribute", alias($._const_attribute, $.attribute))),
230
+ namesWithAtMostOneClose(choice(plain, constant), closing),
231
+ ),
232
+ seq(
233
+ field("attribute", alias($._close_attribute, $.attribute)),
234
+ namesWithAtMostOneClose(constant, choice(plain, closing)),
235
+ ),
236
+ );
237
+ },
238
+ _global_names: ($) =>
239
+ seq(
240
+ optional(field("attribute", alias($._const_attribute, $.attribute))),
241
+ separated(
242
+ choice(
243
+ alias($._plain_name, $.attributed_name),
244
+ alias($._const_name, $.attributed_name),
245
+ ),
246
+ ",",
247
+ ),
248
+ ),
249
+ _plain_name: ($) => field("name", $.identifier),
250
+ _const_name: ($) =>
251
+ seq(
252
+ field("name", $.identifier),
253
+ field("attribute", alias($._const_attribute, $.attribute)),
254
+ ),
255
+ _close_name: ($) =>
256
+ seq(
257
+ field("name", $.identifier),
258
+ field("attribute", alias($._close_attribute, $.attribute)),
259
+ ),
260
+ _const_attribute: ($) => attribute($, "const"),
261
+ _close_attribute: ($) => attribute($, "close"),
262
+ function_declaration: ($) =>
263
+ seq(
264
+ choice(
265
+ seq("function", field("name", $.function_name)),
266
+ seq(
267
+ field("scope", choice("local", "global")),
268
+ "function",
269
+ field("name", $.identifier),
270
+ ),
271
+ ),
272
+ field("body", $.function_body),
273
+ ),
274
+ function_name: ($) =>
275
+ seq(
276
+ repeat(seq(field("table", $.identifier), ".")),
277
+ choice(
278
+ field("name", $.identifier),
279
+ seq(field("table", $.identifier), ":", field("method", $.identifier)),
280
+ ),
281
+ ),
282
+ function_expression: ($) => seq("function", field("body", $.function_body)),
283
+ function_body: ($) =>
284
+ seq(field("parameters", $.parameters), body($), "end"),
285
+ parameters: ($) =>
286
+ seq(
287
+ "(",
288
+ optional(
289
+ choice(
290
+ seq(
291
+ field("names", $.name_list),
292
+ optional(seq(",", field("vararg", $.vararg_parameter))),
293
+ ),
294
+ field("vararg", $.vararg_parameter),
295
+ ),
296
+ ),
297
+ ")",
298
+ ),
299
+ vararg_parameter: ($) => seq("...", optional(field("name", $.identifier))),
300
+ return_statement: ($) =>
301
+ seq(
302
+ "return",
303
+ optional(field("values", $.expression_list)),
304
+ optional(";"),
305
+ ),
306
+ expression_list: ($) => separated($._expression, ","),
307
+ _expression: ($) =>
308
+ choice(
309
+ $._prefix_expression,
310
+ $.unary_expression,
311
+ $.binary_expression,
312
+ $.table_constructor,
313
+ $.vararg_expression,
314
+ $.function_expression,
315
+ $.number,
316
+ $.short_string,
317
+ $.long_string,
318
+ $.nil,
319
+ $.true,
320
+ $.false,
321
+ ),
322
+ unary_expression: ($) =>
323
+ prec(
324
+ unaryLevel,
325
+ seq(
326
+ field("operator", choice("-", "not", "#", "~")),
327
+ field("operand", $._expression),
328
+ ),
329
+ ),
330
+ binary_expression: ($) =>
331
+ choice(
332
+ ...binaryLevels($).map(({ level, right, operator }) =>
333
+ (right ? prec.right : prec.left)(
334
+ level,
335
+ seq(
336
+ field("left", $._expression),
337
+ field("operator", operator),
338
+ field("right", $._expression),
339
+ ),
340
+ ),
341
+ ),
342
+ ),
343
+ vararg_expression: () => "...",
344
+ _prefix_expression: ($) =>
345
+ prec(
346
+ prefixLevel,
347
+ choice($._variable, $.parenthesized_expression, $.function_call),
348
+ ),
349
+ _variable: ($) =>
350
+ choice($.identifier, $.field_expression, $.index_expression),
351
+ parenthesized_expression: ($) => seq("(", $._expression, ")"),
352
+ field_expression: ($) =>
353
+ prec.left(
354
+ prefixLevel,
355
+ seq(
356
+ field("table", $._prefix_expression),
357
+ ".",
358
+ field("field", $.identifier),
359
+ ),
360
+ ),
361
+ index_expression: ($) =>
362
+ prec.left(
363
+ prefixLevel,
364
+ seq(
365
+ field("table", $._prefix_expression),
366
+ "[",
367
+ field("index", $._expression),
368
+ "]",
369
+ ),
370
+ ),
371
+ function_call: ($) =>
372
+ prec.left(
373
+ prefixLevel,
374
+ seq(
375
+ field("function", $._prefix_expression),
376
+ optional(seq(":", field("method", $.identifier))),
377
+ field("arguments", $.arguments),
378
+ ),
379
+ ),
380
+ arguments: ($) =>
381
+ choice(
382
+ seq("(", optional($.expression_list), ")"),
383
+ $.table_constructor,
384
+ $.short_string,
385
+ $.long_string,
386
+ ),
387
+ table_constructor: ($) => seq("{", optional($.field_list), "}"),
388
+ field_list: ($) =>
389
+ seq(
390
+ separated($.table_field, choice(",", ";")),
391
+ optional(choice(",", ";")),
392
+ ),
393
+ table_field: ($) =>
394
+ seq(
395
+ optional(
396
+ seq(
397
+ choice(
398
+ field("key", $.identifier),
399
+ seq("[", field("key", $._expression), "]"),
400
+ ),
401
+ "=",
402
+ ),
403
+ ),
404
+ field("value", $._expression),
405
+ ),
406
+ identifier: () => /[a-zA-Z_][a-zA-Z0-9_]*/,
407
+ nil: () => "nil",
408
+ true: () => "true",
409
+ false: () => "false",
410
+ number: ($) =>
411
+ seq(
412
+ choice(
413
+ seq(
414
+ mantissa(alias(/[0-9]+/, $.decimal_digits), $.decimal_digits),
415
+ optional($.exponent_part),
416
+ ),
417
+ seq(
418
+ choice("0x", "0X"),
419
+ mantissa($.hex_digits, $.hex_digits),
420
+ optional(alias($._hex_exponent_part, $.exponent_part)),
421
+ ),
422
+ ),
423
+ $._number_end,
424
+ ),
425
+ exponent_part: ($) =>
426
+ exponent(
427
+ choice(
428
+ alias($._exponent_lower_e, "e"),
429
+ alias($._exponent_upper_e, "E"),
430
+ ),
431
+ $.decimal_digits,
432
+ ),
433
+ _hex_exponent_part: ($) =>
434
+ exponent(
435
+ choice(
436
+ alias($._exponent_lower_p, "p"),
437
+ alias($._exponent_upper_p, "P"),
438
+ ),
439
+ $.decimal_digits,
440
+ ),
441
+ short_string: ($) =>
442
+ choice(quoted($, "'", $._single_text), quoted($, '"', $._double_text)),
443
+ _escape: ($) =>
444
+ choice(alias($._simple_escape, $.escape_sequence), $.escape_sequence),
445
+ escape_sequence: ($) =>
446
+ seq(
447
+ alias($._unicode_escape_start, "\\u"),
448
+ token.immediate("{"),
449
+ field("value", $.code_point),
450
+ token.immediate("}"),
451
+ ),
452
+ long_string: ($) =>
453
+ seq(
454
+ $.long_bracket_open,
455
+ optional(alias($._long_content, $.string_content)),
456
+ $.long_bracket_close,
457
+ ),
458
+ long_comment: ($) =>
459
+ seq(
460
+ $.long_comment_open,
461
+ optional(alias($._long_content, $.comment_content)),
462
+ $.long_bracket_close,
463
+ ),
464
+ },
465
+ });
package/package.json CHANGED
@@ -1,6 +1,71 @@
1
1
  {
2
2
  "name": "@konomanoasa/tree-sitter-lua",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "Tree-sitter grammar for Lua.",
5
+ "license": "MIT",
6
+ "repository": "git+https://github.com/konomanoasa/tree-sitter-lua.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
+ "Cargo.toml",
16
+ "binding.gyp",
17
+ "bindings/node/*.cc",
18
+ "bindings/node/*.js",
19
+ "bindings/node/*.ts",
20
+ "bindings/python/*.c",
21
+ "bindings/python/*.py",
22
+ "bindings/python/*.pyi",
23
+ "bindings/python/py.typed",
24
+ "bindings/rust/*.rs",
25
+ "grammar.js",
26
+ "pyproject.toml",
27
+ "queries/*.scm",
28
+ "scripts/tree-sitter.js",
29
+ "setup.py",
30
+ "src/*.c",
31
+ "src/*.h",
32
+ "src/*.json",
33
+ "src/tree_sitter/*.h",
34
+ "test/bindings.test.rs",
35
+ "tree-sitter.json"
36
+ ],
37
+ "publishConfig": {
38
+ "access": "public"
39
+ },
40
+ "scripts": {
41
+ "build": "node-gyp rebuild && node-gyp configure -- -f compile_commands_json",
42
+ "build:parser": "npm run tree-sitter -- build-all",
43
+ "check": "biome check . && tsc",
44
+ "check:queries": "npm run tree-sitter -- check-queries",
45
+ "format": "biome format --write .",
46
+ "generate": "npm run tree-sitter -- generate-all",
47
+ "install": "node-gyp-build",
48
+ "test": "npm run tree-sitter -- test-corpus --rebuild && node --test --test-concurrency=1 test/bindings.test.js test/build.test.js test/cst.test.js test/highlight.test.js test/incremental.test.js && node scripts/check-c.js --test",
49
+ "test:fuzz": "npm run tree-sitter -- fuzz-all --iterations 100 --edits 5",
50
+ "test:sanitize": "node scripts/check-c.js --sanitize",
51
+ "tree-sitter": "node scripts/tree-sitter.js"
52
+ },
53
+ "dependencies": {
54
+ "node-addon-api": "8.9.2",
55
+ "node-gyp-build": "4.8.4"
56
+ },
57
+ "peerDependencies": {
58
+ "tree-sitter": "0.25.1"
59
+ },
60
+ "devDependencies": {
61
+ "@biomejs/biome": "2.6.9",
62
+ "@types/node": "26.6.11",
63
+ "node-gyp": "13.1.0",
64
+ "tree-sitter-cli": "0.27.2",
65
+ "typescript": "7.0.2"
66
+ },
67
+ "allowScripts": {
68
+ "tree-sitter": true,
69
+ "tree-sitter-cli": true
70
+ }
71
+ }
package/pyproject.toml ADDED
@@ -0,0 +1,74 @@
1
+ [project]
2
+ name = "konomanoasa-tree-sitter-lua"
3
+ version = "0.1.0"
4
+ description = "Tree-sitter grammar for Lua."
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-lua"
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_lua", "tree_sitter_lua.queries"]
30
+
31
+ [tool.setuptools.package-dir]
32
+ tree_sitter_lua = "bindings/python"
33
+ "tree_sitter_lua.queries" = "queries"
34
+
35
+ [tool.setuptools.package-data]
36
+ tree_sitter_lua = ["*.pyi", "py.typed"]
37
+ "tree_sitter_lua.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
+ ]