WebGPU Shading Language WD-WGSL-20240205 grammar for tree-sitter.
inspired and guided by szebniok/tree-sitter-wgsl and tree-sitter-rust.
At first, what is WGSL, and what is tree-sitter?
WebGPU Shading Language (WGSL) is the shader language for [WebGPU]. That is, an application using the WebGPU API uses WGSL to express the programs, known as shaders, that run on the GPU.
Introduction | WebGPU Shading Language
Tree-sitter is a parser generator tool and an incremental parsing library. It can build a concrete syntax tree for a source file and efficiently update the syntax tree as the source file is edited.
The goal of this repo is to provide a grammar for WGSL that would enable tree-sitter to efficiently build a concrete syntax tree for a source file. The generated parser can then be used with emacs’s builtin treesit support to provide better major mode experience and more convenient emacs addons development experience.
Currently this grammar is not mature enough, ToDo list below shows things need to be done.
To use this grammar, you first need to download it from github:
git clone https://github.com/include-yy/tree-sitter-wgsl --depth 1
If you want to do further development, node.js should be installed, run the npm commands below to download necessary devtools:
npm i
You can use npx
to activate dev environment, and then use
tree-sitter
command to generate and test grammar. See Creating parsers
for more information.
If you just want to use the existing parser, you can compile like this
in src
directory:
# on Linux
gcc -fPIC -c -I. parser.c
gcc -fPIC -c -I. scanner.c
gcc -fPIC -shared *.o -o "libtree-sitter-wgsl.so"
# with MSYS2 on Windows
gcc -fPIC -c -I. parser.c
gcc -fPIC -c -I. scanner.c
gcc -fPIC -shared *.o -o "libtree-sitter-wgsl.dll"
Also, if you want to try it in Emacs, put this DLL in the correct place and enable treesit-explore-mode
in one buffer.
I also provide a very basic wgsl-ts-mode.el file, you can load it and enable wgsl-ts-mode
to test its highlight and indentation. Here is a demo:
issue and pr are welcomed. Whether it’s correcting mistakes, synchronizing with the latest standards, or pointing out errors in the grammar, all are acceptable. If you wish to submit a pull request, please make sure it passes all tests under test/corpus (of course, you may modify the test files as needed).
Here are some learning materials about writing grammars:
- Tree-sitter official documentation
- Tips and Tricks for a grammar author
- sogaiu/ts-questions: Tree-sitter Questions
- repo’s issues, pulls and discussions
- tree_sitter - Rust
Also something from emacs side:
Finished:
- [X] add basic test files for grammar
- [X] rename some unconvenient name
- [X] use code in WGSL’s spec as example
TODO:
- [ ] improve parser’s state count and parser’s size
- [ ] adjust code order and comments for easier understanding
- [ ] provide fully covered test
- [ ] add more examples
- [ ] add hightlight.scm (but I am not a nvim user)