feat: improve language bindings

Co-authored-by: ObserverOfTime <chronobserver@disroot.org>
This commit is contained in:
Amaan Qureshi 2024-02-21 11:47:59 -05:00
parent d0d349c02b
commit 9e5bf6591f
32 changed files with 1132 additions and 195 deletions

View file

@ -51,7 +51,7 @@ Here's an example of a simple C program that uses the Tree-sitter [JSON parser](
// Declare the `tree_sitter_json` function, which is
// implemented by the `tree-sitter-json` library.
TSLanguage *tree_sitter_json();
extern const TSLanguage *tree_sitter_json();
int main() {
// Create a parser.
@ -326,9 +326,9 @@ Conceptually, it can be represented by three syntax trees with overlapping range
#include <tree_sitter/api.h>
// These functions are each implemented in their own repo.
const TSLanguage *tree_sitter_embedded_template();
const TSLanguage *tree_sitter_html();
const TSLanguage *tree_sitter_ruby();
extern const TSLanguage *tree_sitter_embedded_template();
extern const TSLanguage *tree_sitter_html();
extern const TSLanguage *tree_sitter_ruby();
int main(int argc, const char **argv) {
const char *text = argv[1];

View file

@ -104,14 +104,46 @@ Let's go over all of the functionality of the `tree-sitter` command line tool.
The most important command you'll use is `tree-sitter generate`. This command reads the `grammar.js` file in your current working directory and creates a file called `src/parser.c`, which implements the parser. After making changes to your grammar, just run `tree-sitter generate` again.
The first time you run `tree-sitter generate`, it will also generate a few other files:
The first time you run `tree-sitter generate`, it will also generate a few other files for bindings for the following languages:
#### C/C++
* `Makefile` - This file tells `make` how to compile your language.
* `bindings/c/tree-sitter-language.h` - This file provides the C interface of your language.
* `bindings/c/tree-sitter-language.pc` - This file provides pkg-config metadata about your language's C library.
* `src/tree_sitter/parser.h` - This file provides some basic C definitions that are used in your generated `parser.c` file.
#### Go
* `bindings/go/binding.go` - This file wraps your language in a Go module.
* `bindings/go/binding_test.go` - This file contains a test for the Go package.
#### Node
* `binding.gyp` - This file tells Node.js how to compile your language.
* `bindings/node/index.js` - This is the file that Node.js initially loads when using your language.
* `bindings/node/binding.cc` - This file wraps your language in a JavaScript object when used in Node.js.
* `bindings/node/binding.cc` - This file wraps your language in a JavaScript module for Node.js.
#### Python
* `pyproject.toml` - This file is the manifest of the Python package.
* `setup.py` - This file tells Python how to compile your language.
* `bindings/python/binding.c` - This file wraps your language in a Python module.
* `bindings/python/tree_sitter_language/__init__.py` - This file tells Python how to load your language.
* `bindings/python/tree_sitter_language/__init__.pyi` - This file provides type hints for your parser when used in Python.
* `bindings/python/tree_sitter_language/py.typed` - This file provides type hints for your parser when used in Python.
#### Rust
* `Cargo.toml` - This file is the manifest of the Rust package.
* `bindings/rust/lib.rs` - This file wraps your language in a Rust crate when used in Rust.
* `bindings/rust/build.rs` - This file wraps the building process for the Rust crate.
* `src/tree_sitter/parser.h` - This file provides some basic C definitions that are used in your generated `parser.c` file.
#### Swift
* `Package.swift` - This file tells Swift how to compile your language.
* `bindings/swift/TreeSitterLanguage/language.h` - This file wraps your language in a Swift module when used in Swift.
If there is an ambiguity or *local ambiguity* in your grammar, Tree-sitter will detect it during parser generation, and it will exit with a `Unresolved conflict` error message. See below for more information on these errors.