|
1 | 1 | # Kotlin Grammar for Tree-sitter |
2 | 2 |
|
3 | | -[](https://github.com/fwcd/tree-sitter-kotlin/actions/workflows/build.yml) |
4 | | -[](https://www.npmjs.com/package/tree-sitter-kotlin) |
5 | | -[](https://crates.io/crates/tree-sitter-kotlin) |
| 3 | +Gitar’s Kotlin grammar for Tree-sitter began as a fork of [tree-sitter-kotlin](https://github.com/fwcd/tree-sitter-kotlin). Our goal is to provide a Kotlin grammar tailored for **program analysis**, not just syntax highlighting. This requires correctness and adherence to the Kotlin compiler. Over time, our grammar has diverged so much from the fork that it effectively stands as a new grammar. |
6 | 4 |
|
7 | | -[Kotlin](https://kotlinlang.org) language grammar for [Tree-sitter](http://tree-sitter.github.io/tree-sitter/). You can try it out directly [on the web](https://fwcd.github.io/tree-sitter-kotlin). |
8 | | - |
9 | | - |
10 | | - |
11 | | -The grammar is based on the [official language grammar](https://kotlinlang.org/docs/reference/grammar.html). |
12 | | - |
13 | | -## Project Structure |
14 | | - |
15 | | -| File | Description | |
16 | | -| ---- | ----------- | |
17 | | -| grammar.js | The Tree-sitter grammar | |
18 | | -| grammar-reference.js | A direct translation of the Kotlin language grammar that is, however, ambiguous to Tree-sitter | |
19 | | -| src | The generated parser | |
| 5 | +- The original repo is based on the [Kotlin specification grammar](https://kotlinlang.org/docs/reference/grammar.html), which only loosely reflects the compiler’s actual grammar. Our grammar more closely matches the compiler. |
| 6 | +- We handle newline characters directly in the grammar rules to take advantage of full parser context. See our [blog post](https://gitar.ai/blog/parsing-kotlin) for details on newline handling in Kotlin. |
| 7 | +- Some lexer hacks are still needed for newline handling — for example, distinguishing newlines after binary operators (to avoid conflicts with operator precedence) and ignoring newlines inside brackets. |
| 8 | +- For program analysis, whitespace and comments often need to be properly attached to nodes. To support this, we’ve split some rules (such as class_declaration) so that whitespace and comments become part of the intended node. |
20 | 9 |
|
21 | 10 | ## Setup |
22 | 11 |
|
23 | | ->`npm install` |
24 | | -
|
25 | | -## Development |
26 | | - |
27 | | -### Compilation |
28 | | - |
29 | | -To (re-)compile the grammar, run: |
30 | | - |
31 | | ->`npm run generate` |
32 | | -
|
33 | | -Note that the grammar is written completely in JavaScript (`grammar.js`), the other source files are generated by `tree-sitter`. |
34 | | - |
35 | | -### Testing |
36 | | - |
37 | | -To run the unit tests, run: |
38 | | - |
39 | | ->`npm run test` |
40 | | -
|
41 | | -## WebAssembly |
42 | | - |
43 | | -### Compilation |
44 | | - |
45 | | -First make sure to have [Emscripten](https://emscripten.org/) installed. If you use Homebrew, you can `brew install emscripten`. Then run: |
46 | | - |
47 | | ->`npm run build-wasm` |
48 | | -
|
49 | | -### Playground |
50 | | - |
51 | | -After compiling the grammar to WebAssembly, you can invoke |
52 | | - |
53 | | ->`npm run playground` |
54 | | -
|
55 | | -to launch an interactive editing environment that displays the parsed syntax tree on-the-fly in the browser. You can also view a deployed version of this playground [on the web](https://fwcd.github.io/tree-sitter-kotlin). |
56 | | - |
57 | | - |
58 | | - |
59 | | -## Documentation |
60 | | - |
61 | | -More documentation on how to create Tree-sitter grammars [can be found here](https://tree-sitter.github.io/tree-sitter/creating-parsers). |
62 | | - |
63 | | -## See also |
| 12 | +To run the grammar, install the [tree-sitter-cli](https://crates.io/crates/tree-sitter-cli). From the project root: |
64 | 13 |
|
65 | | -* [Kotlin Language Server](https://github.com/fwcd/kotlin-language-server) for code completion, diagnostics and more |
66 | | -* [Kotlin Debug Adapter](https://github.com/fwcd/kotlin-debug-adapter) for JVM debugging support |
| 14 | +- `tree-sitter generate` → Generates the `parser.c` file. (Note: the file is currently quite large — a known Tree-sitter issue. We’re working on ways to reduce the size.) |
| 15 | +- `tree-sitter test` → Runs all tests in the `test/corpus` directory. |
| 16 | +- `tree-sitter build --wasm` → Builds the WebAssembly (wasm) file, which you can use to run the local playground with `tree-sitter play`. |
0 commit comments