@willbooster/tree-sitter-kotlin 1.3.2 → 1.4.1
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/README.md +38 -5
- package/grammar.js +50 -26
- package/package.json +13 -7
- package/tree-sitter-kotlin.wasm +0 -0
- package/tree-sitter.json +1 -1
package/README.md
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
# @willbooster/tree-sitter-kotlin
|
|
2
2
|
|
|
3
3
|
[](https://www.npmjs.com/package/@willbooster/tree-sitter-kotlin)
|
|
4
|
-
[](https://crates.io/crates/willbooster-tree-sitter-kotlin)
|
|
5
4
|
[](https://www.npmjs.com/package/@willbooster/tree-sitter-kotlin)
|
|
6
5
|
[](https://github.com/WillBooster/tree-sitter-kotlin/actions/workflows/test.yml)
|
|
7
6
|
[](https://github.com/WillBooster/tree-sitter-kotlin/actions/workflows/test-rust.yml)
|
|
8
7
|
[](https://github.com/semantic-release/semantic-release)
|
|
9
|
-
[](https://github.com/WillBooster/shared/tree/main/packages/wbfy)
|
|
9
|
+
[](https://crates.io/crates/willbooster-tree-sitter-kotlin)
|
|
10
10
|
|
|
11
11
|
Kotlin grammar for [tree-sitter](https://github.com/tree-sitter/tree-sitter), forked from
|
|
12
12
|
[tree-sitter-grammars/tree-sitter-kotlin](https://github.com/tree-sitter-grammars/tree-sitter-kotlin). We are grateful
|
|
@@ -16,11 +16,15 @@ This fork fixes parsing bugs and raises conformance with the Kotlin grammar.
|
|
|
16
16
|
|
|
17
17
|
## Usage
|
|
18
18
|
|
|
19
|
-
The npm package ships `tree-sitter-kotlin.wasm` for
|
|
19
|
+
The npm package ships `tree-sitter-kotlin.wasm` for
|
|
20
|
+
[@willbooster/web-tree-sitter](https://www.npmjs.com/package/@willbooster/web-tree-sitter), which runs in Node.js, Bun,
|
|
21
|
+
browsers, and Cloudflare Workers.
|
|
22
|
+
|
|
23
|
+
In Node.js and Bun:
|
|
20
24
|
|
|
21
25
|
```js
|
|
22
26
|
import { fileURLToPath } from 'node:url';
|
|
23
|
-
import { Language, Parser } from 'web-tree-sitter';
|
|
27
|
+
import { Language, Parser } from '@willbooster/web-tree-sitter';
|
|
24
28
|
|
|
25
29
|
await Parser.init();
|
|
26
30
|
const parser = new Parser();
|
|
@@ -29,6 +33,31 @@ parser.setLanguage(await Language.load(wasmPath));
|
|
|
29
33
|
const tree = parser.parse('fun main() = println("Hello")\n');
|
|
30
34
|
```
|
|
31
35
|
|
|
36
|
+
In browsers, load both `.wasm` files by URL. With Vite:
|
|
37
|
+
|
|
38
|
+
```js
|
|
39
|
+
import { Language, Parser } from '@willbooster/web-tree-sitter';
|
|
40
|
+
import runtimeUrl from '@willbooster/web-tree-sitter/web-tree-sitter.wasm?url';
|
|
41
|
+
import kotlinUrl from '@willbooster/tree-sitter-kotlin/tree-sitter-kotlin.wasm?url';
|
|
42
|
+
|
|
43
|
+
await Parser.init({ locateFile: () => runtimeUrl });
|
|
44
|
+
const parser = new Parser();
|
|
45
|
+
parser.setLanguage(await Language.load(kotlinUrl));
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
In Cloudflare Workers, which do not allow compiling Wasm at run time, import both `.wasm` files as modules (with or
|
|
49
|
+
without Node.js compatibility):
|
|
50
|
+
|
|
51
|
+
```js
|
|
52
|
+
import { Language, Parser } from '@willbooster/web-tree-sitter';
|
|
53
|
+
import runtime from '@willbooster/web-tree-sitter/web-tree-sitter.wasm';
|
|
54
|
+
import kotlin from '@willbooster/tree-sitter-kotlin/tree-sitter-kotlin.wasm';
|
|
55
|
+
|
|
56
|
+
await Parser.init({ wasmModule: runtime });
|
|
57
|
+
const parser = new Parser();
|
|
58
|
+
parser.setLanguage(await Language.load(kotlin));
|
|
59
|
+
```
|
|
60
|
+
|
|
32
61
|
The package also ships the node types in `src/node-types.json`.
|
|
33
62
|
|
|
34
63
|
In Rust, depend on the [crate](https://crates.io/crates/willbooster-tree-sitter-kotlin):
|
|
@@ -49,6 +78,7 @@ parser.set_language(&tree_sitter_kotlin::LANGUAGE.into())?;
|
|
|
49
78
|
```sh
|
|
50
79
|
mise install
|
|
51
80
|
bun install --frozen-lockfile
|
|
81
|
+
bun run test/ci-setup
|
|
52
82
|
bun run build/ci
|
|
53
83
|
bun run test
|
|
54
84
|
script/parse-examples
|
|
@@ -67,7 +97,10 @@ cargo test
|
|
|
67
97
|
it; review its diff before committing;
|
|
68
98
|
- a performance check (`test/unit/performance.test.ts`) that recovering from an error on each of 10,000 lines takes
|
|
69
99
|
linear time, since consumers parse files while they are being edited. It loads the Wasm build through
|
|
70
|
-
web-tree-sitter, which `bun run build/ci` rebuilds after regenerating the parser
|
|
100
|
+
@willbooster/web-tree-sitter, which `bun run build/ci` rebuilds after regenerating the parser;
|
|
101
|
+
- checks that the Wasm build parses Kotlin through @willbooster/web-tree-sitter in Chromium
|
|
102
|
+
(`test/unit/browser.test.ts`) and in Cloudflare Workers with and without
|
|
103
|
+
Node.js compatibility (`test/unit/workers.test.ts`).
|
|
71
104
|
|
|
72
105
|
CI also runs these tests on Linux arm64 and macOS, where the Rust binding compiles the parser natively, and fuzzes the parser with libFuzzer and sanitizers
|
|
73
106
|
(`.github/workflows/robustness.yml`).
|
package/grammar.js
CHANGED
|
@@ -49,6 +49,8 @@ module.exports = grammar({
|
|
|
49
49
|
[$.parameter_modifiers, $.type_modifiers],
|
|
50
50
|
[$.function_modifier, $.type_modifiers],
|
|
51
51
|
[$.function_modifier, $._reserved_identifier],
|
|
52
|
+
[$.function_modifier, $.type_modifiers, $._reserved_identifier],
|
|
53
|
+
[$.type_modifiers, $._reserved_identifier],
|
|
52
54
|
[$.variable_declaration, $.type_modifiers],
|
|
53
55
|
[$.variable_declaration, $.type_modifiers, $.modifiers, $.annotated_expression],
|
|
54
56
|
[$.variable_declaration, $.type_modifiers, $.annotated_expression],
|
|
@@ -61,6 +63,9 @@ module.exports = grammar({
|
|
|
61
63
|
[$.class_modifier, $._reserved_identifier],
|
|
62
64
|
[$.platform_modifier, $._reserved_identifier],
|
|
63
65
|
[$.property_modifier, $._reserved_identifier],
|
|
66
|
+
[$.inheritance_modifier, $._reserved_identifier],
|
|
67
|
+
[$.visibility_modifier, $._reserved_identifier],
|
|
68
|
+
[$.member_modifier, $._reserved_identifier],
|
|
64
69
|
|
|
65
70
|
[$.explicit_delegation, $.expression],
|
|
66
71
|
[$.qualified_identifier],
|
|
@@ -87,6 +92,8 @@ module.exports = grammar({
|
|
|
87
92
|
'set',
|
|
88
93
|
// used to check if we can parse a comment
|
|
89
94
|
'$',
|
|
95
|
+
// used to check if a modifier alone on its line belongs to a declaration on the next line
|
|
96
|
+
'val',
|
|
90
97
|
],
|
|
91
98
|
|
|
92
99
|
inline: ($) => [$._statements, $._identifier, $._control_structure_body],
|
|
@@ -371,17 +378,22 @@ module.exports = grammar({
|
|
|
371
378
|
)
|
|
372
379
|
),
|
|
373
380
|
|
|
374
|
-
|
|
381
|
+
// A modifier keyword that `_reserved_identifier` also accepts as a name makes `@A\nsealed interface B` parse as
|
|
382
|
+
// an infix call as well, since `interface` lexes as an identifier where no keyword is valid, so the modifier
|
|
383
|
+
// reading takes dynamic precedence. The name takes none: it would outweigh the modifiers again wherever a
|
|
384
|
+
// reading has more names, such as `get` calls in a class body misread as a trailing lambda.
|
|
385
|
+
class_modifier: () => prec.dynamic(1, choice('enum', 'sealed', 'annotation', 'data', 'inner', 'value')),
|
|
375
386
|
|
|
376
|
-
function_modifier: () =>
|
|
387
|
+
function_modifier: () =>
|
|
388
|
+
prec.dynamic(1, prec.right(choice('tailrec', 'operator', 'infix', 'inline', 'external', 'suspend'))),
|
|
377
389
|
|
|
378
|
-
property_modifier: () => 'const',
|
|
390
|
+
property_modifier: () => prec.dynamic(1, 'const'),
|
|
379
391
|
|
|
380
|
-
visibility_modifier: () => choice('public', 'private', 'protected', 'internal'),
|
|
392
|
+
visibility_modifier: () => prec.dynamic(1, choice('public', 'private', 'protected', 'internal')),
|
|
381
393
|
|
|
382
|
-
inheritance_modifier: () => choice('abstract', 'final', 'open'),
|
|
394
|
+
inheritance_modifier: () => prec.dynamic(1, choice('abstract', 'final', 'open')),
|
|
383
395
|
|
|
384
|
-
member_modifier: () => choice('override', 'lateinit'),
|
|
396
|
+
member_modifier: () => prec.dynamic(1, choice('override', 'lateinit')),
|
|
385
397
|
|
|
386
398
|
parameter_modifiers: ($) => repeat1(choice($.annotation, $.parameter_modifier)),
|
|
387
399
|
|
|
@@ -389,7 +401,7 @@ module.exports = grammar({
|
|
|
389
401
|
|
|
390
402
|
reification_modifier: () => 'reified',
|
|
391
403
|
|
|
392
|
-
platform_modifier: () => choice('expect', 'actual'),
|
|
404
|
+
platform_modifier: () => prec.dynamic(1, choice('expect', 'actual')),
|
|
393
405
|
|
|
394
406
|
type_modifiers: ($) => prec.right(repeat1(choice($.annotation, 'suspend'))),
|
|
395
407
|
|
|
@@ -746,25 +758,37 @@ module.exports = grammar({
|
|
|
746
758
|
identifier: () => token(choice(/[\p{L}_][\p{L}_\p{Nd}]*/u, /`[^\r\n`]+`/)),
|
|
747
759
|
|
|
748
760
|
_reserved_identifier: ($) =>
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
761
|
+
alias(
|
|
762
|
+
choice(
|
|
763
|
+
'abstract',
|
|
764
|
+
'actual',
|
|
765
|
+
'annotation',
|
|
766
|
+
'const',
|
|
767
|
+
'constructor',
|
|
768
|
+
'data',
|
|
769
|
+
'enum',
|
|
770
|
+
'expect',
|
|
771
|
+
'external',
|
|
772
|
+
'final',
|
|
773
|
+
'get',
|
|
774
|
+
'infix',
|
|
775
|
+
'inline',
|
|
776
|
+
'inner',
|
|
777
|
+
'internal',
|
|
778
|
+
'lateinit',
|
|
779
|
+
'open',
|
|
780
|
+
'operator',
|
|
781
|
+
'override',
|
|
782
|
+
'private',
|
|
783
|
+
'protected',
|
|
784
|
+
'public',
|
|
785
|
+
'sealed',
|
|
786
|
+
'set',
|
|
787
|
+
'suspend',
|
|
788
|
+
'tailrec',
|
|
789
|
+
'value'
|
|
790
|
+
),
|
|
791
|
+
$.identifier
|
|
768
792
|
),
|
|
769
793
|
|
|
770
794
|
shebang: () => /#!.*/,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@willbooster/tree-sitter-kotlin",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.1",
|
|
4
4
|
"description": "Kotlin grammar for tree-sitter",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"incremental",
|
|
@@ -33,6 +33,7 @@
|
|
|
33
33
|
"start": "tree-sitter playground",
|
|
34
34
|
"test": "bun wb test",
|
|
35
35
|
"test/ci": "bun wb test-on-ci",
|
|
36
|
+
"test/ci-setup": "playwright install --with-deps chromium",
|
|
36
37
|
"typecheck": "bun wb typecheck",
|
|
37
38
|
"verify": "bun wb verify",
|
|
38
39
|
"verify-full": "bun wb verify --full"
|
|
@@ -41,25 +42,30 @@
|
|
|
41
42
|
"@semantic-release/exec": "7.1.0",
|
|
42
43
|
"@tsconfig/bun": "1.0.11",
|
|
43
44
|
"@types/bun": "1.4.2",
|
|
45
|
+
"@vitest/browser-playwright": "5.0.1",
|
|
44
46
|
"@willbooster/oxfmt-config": "1.2.2",
|
|
45
47
|
"@willbooster/oxlint-config": "1.4.8",
|
|
46
|
-
"@willbooster/wb": "22.
|
|
48
|
+
"@willbooster/wb": "22.11.5",
|
|
49
|
+
"@willbooster/web-tree-sitter": "1.0.0",
|
|
47
50
|
"conventional-changelog-conventionalcommits": "10.4.0",
|
|
48
51
|
"lefthook": "2.1.14",
|
|
49
|
-
"oxfmt": "0.
|
|
50
|
-
"oxlint": "1.
|
|
52
|
+
"oxfmt": "0.70.0",
|
|
53
|
+
"oxlint": "1.85.0",
|
|
51
54
|
"oxlint-tsgolint": "7.0.2002",
|
|
55
|
+
"playwright": "1.63.0",
|
|
52
56
|
"semantic-release": "25.0.9",
|
|
53
57
|
"sort-package-json": "4.0.0",
|
|
54
58
|
"tree-sitter-cli": "0.27.0",
|
|
55
59
|
"typescript": "7.0.2",
|
|
56
|
-
"
|
|
60
|
+
"vite": "8.3.0",
|
|
61
|
+
"vitest": "5.0.1",
|
|
62
|
+
"wrangler": "4.136.2"
|
|
57
63
|
},
|
|
58
64
|
"peerDependencies": {
|
|
59
|
-
"web-tree-sitter": "^0.
|
|
65
|
+
"@willbooster/web-tree-sitter": "^1.0.0"
|
|
60
66
|
},
|
|
61
67
|
"peerDependenciesMeta": {
|
|
62
|
-
"web-tree-sitter": {
|
|
68
|
+
"@willbooster/web-tree-sitter": {
|
|
63
69
|
"optional": true
|
|
64
70
|
}
|
|
65
71
|
},
|
package/tree-sitter-kotlin.wasm
CHANGED
|
Binary file
|