Learn something about the tree-sitter.
-
Create a new parser project
mkdir tree-sitter-{language}
. -
Initialize the project with dependencies.
cd tree-sitter-{language} pnpm init pnpm add nan node-gyp pnpm add tree-sitter-cli -D
-
Initialize global config for tree-sitter, add
Paths
../node_modules/.bin/tree-sitter init-config ^ this would print out the path to config file # add the parent folder of `tree-sitter-{language}` to the field "parser-directories" # so that tree-sitter can find the parser project
-
Write some grammar in
grammar.js
. Here is an example. -
Call
./node_modules/.bin/tree-sitter generate path-to-grammar.js
to generate several new files important. -
Run
node-gyp configure && node-gyp build
to build the final parser.
-
Add test files in the directory
corpus
. Check out the example. -
Run
./node_modules/.bin/tree-sitter test
to verify. Add--update
option to update the syntax.
-
Add
highlights.scm
in the directoryqueries
. Check out the example. -
Run
./node_modules/.bin/tree-sitter highlight --html ./examples/source.imp
to generate a result in html. -
Open that html to view the grammar highlight output.
Watch the video: Tree-sitter: a new parsing system for programming tools - GitHub Universe 2017
Read some blogs:
-
Guide to your first Tree-sitter grammar: A detailed post on writing a parser for tree-sitter.
-
Lightweight linting with tree-sitter: A post on using tree-sitter to write a linter. Manipulate the S-expression is interesting but difficult.
Here is another lint tools with JavaScript bindings.
-
How to write a tree-sitter grammar in an afternoon: A good article on how to write a parser in action.