Skip to content

Commit b821fc8

Browse files
fix: add update tree-sitter dynamic library building command
fix #632
1 parent 89a14bd commit b821fc8

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

website/advanced/custom-language.md

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,21 +50,17 @@ git clone https://github.com/HerringtonDarkholme/tree-sitter-mojo.git
5050
## Compile the Parser as Dynamic Library
5151

5252
Once we have prepared the tool and the grammar, we can compile the parser as dynamic library.
53+
_`tree-sitter-cli` is the preferred way to compile dynamic library._
5354

54-
There are no official instructions on how to do this on the internet, but we can get some hints from Tree-sitter's [source code](https://github.com/tree-sitter/tree-sitter/blob/a62bac5370dc5c76c75935834ef083457a6dd0e1/cli/loader/src/lib.rs#L111).
55-
56-
One way is to set an environment variable called `TREE_SITTER_LIBDIR` to the path where you want to store the dynamic library, and then run `tree-sitter test` in the directory of your custom language parser.
57-
58-
This will generate a dynamic library at the `TREE_SITTER_LIBDIR` path.
59-
60-
For example:
55+
The [official way](https://tree-sitter.github.io/tree-sitter/cli/build.html) to compile a parser as a dynamic library is to use the `tree-sitter build` command.
6156

6257
```sh
63-
cd path/to/mojo/parser
64-
export TREE_SITTER_LIBDIR=path/to/your/dir
65-
tree-sitter test
58+
tree-sitter build --output mojo.so
6659
```
6760

61+
The build command compiles your parser into a dynamically-loadable library as a shared object (.so, .dylib, or .dll).
62+
63+
6864
Another way is to use the following [commands](https://github.com/tree-sitter/tree-sitter/blob/a62bac5370dc5c76c75935834ef083457a6dd0e1/cli/loader/src/lib.rs#L380-L410) to compile the parser manually:
6965

7066
```shell
@@ -79,8 +75,21 @@ For example, in mojo's case, the full command will be:
7975
gcc -shared -fPIC -fno-exceptions -g -I 'src' -o mojo.so -O2 src/scanner.cc -xc src/parser.c -lstdc++
8076
```
8177

82-
:::warning
83-
`tree-sitter-cli` is the preferred way to compile dynamic library.
78+
:::details Old tree-sitter does not have build command
79+
80+
[Previously](https://github.com/tree-sitter/tree-sitter/pull/3174) there are no official instructions on how to do this on the internet, but we can get some hints from Tree-sitter's [source code](https://github.com/tree-sitter/tree-sitter/blob/a62bac5370dc5c76c75935834ef083457a6dd0e1/cli/loader/src/lib.rs#L111).
81+
82+
One way is to set an environment variable called `TREE_SITTER_LIBDIR` to the path where you want to store the dynamic library, and then run `tree-sitter test` in the directory of your custom language parser.
83+
84+
This will generate a dynamic library at the `TREE_SITTER_LIBDIR` path.
85+
86+
For example:
87+
88+
```sh
89+
cd path/to/mojo/parser
90+
export TREE_SITTER_LIBDIR=path/to/your/dir
91+
tree-sitter test
92+
```
8493
:::
8594

8695
## Register Language in `sgconfig.yml`

0 commit comments

Comments
 (0)