https://github.com/devlights/try-treesitter
This is my TUTORIAL project for tree-sitter.
https://github.com/devlights/try-treesitter
Last synced: 4 months ago
JSON representation
This is my TUTORIAL project for tree-sitter.
- Host: GitHub
- URL: https://github.com/devlights/try-treesitter
- Owner: devlights
- License: mit
- Created: 2025-07-25T08:34:47.000Z (6 months ago)
- Default Branch: main
- Last Pushed: 2025-08-12T03:02:00.000Z (6 months ago)
- Last Synced: 2025-09-07T08:21:20.550Z (5 months ago)
- Language: Go
- Size: 34.2 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# try-treesitter
This is my TUTORIAL project for tree-sitter.
## tree-sitterとは
[tree-sitter-doc](https://tree-sitter.github.io/tree-sitter/)にライブラリの紹介が書いてあります。
> 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. Tree-sitter aims to be:
> (Tree-sitterは構文解析ツールであり、インクリメンタル構文解析ライブラリである。ソースファイルの具体的な構文ツリーを構築し、ソースファイルの編集に応じて構文ツリーを効率的に更新することができます。Tree-sitterは次のようなことを目指しています:)
> General enough to parse any programming language (あらゆるプログラミング言語を解析できる汎用性)
> Fast enough to parse on every keystroke in a text editor (テキストエディタでのすべてのキーストロークを解析するのに十分な速度)
> Robust enough to provide useful results even in the presence of syntax errors (構文エラーがあっても有用な結果を提供できるほど頑健である。)
> Dependency-free so that the runtime library (which is written in pure C11) can be embedded in any application (依存性がないため、ランタイムライブラリ(純粋なC11で書かれている)をあらゆるアプリケーションに組み込むことができる。)
## tree-sitterを使ったソースファイルの解析
[tree-sitter](https://github.com/tree-sitter/tree-sitter)は、インクリメンタル(増分)な構文解析が可能なパーサジェネレータ・ライブラリです。
[tree-sitter-doc](https://tree-sitter.github.io/tree-sitter/)に公式ドキュメントがあります。
いろいろな言語の構文にデフォルトで対応しており、独自の構文解析を構築することも出来ます。
また、様々なプログラム言語に対してのバインディングも存在します。(Go,Python,C#,Javaのバインディングは公式対応)
## tree-sitterの用意
例題としてC言語のソースファイルを解析する場合は以下のようにバインディングライブラリを選択します。
```go
$ go get github.com/tree-sitter/go-tree-sitter@latest
$ go get github.com/tree-sitter/tree-sitter-c/bindings/go@latest
```
Goのバインディングは、これで完了です。
## tree-sitter-cliの用意
```sh
$ cargo install tree-sitter-cli
```
### CLIのためにC言語バインディングを配置
```sh
$ cd
$ tree-sitter init-config
$ mkdir -p ~/tree-sitter-parsers
$ cd ~/tree-sitter-parsers
$ git clone https://github.com/tree-sitter/tree-sitter-c.git
```
```~/.config/tree-sitter/config.json```のparser-directoriesを以下のように設定します。。
```json
"parser-directories": [
"/home/dev/tree-sitter-parsers"
],
```
これでCLI側で構文木が出力できるようになる。デバッグ時などに便利。
```sh
$ tree-sitter parse app.c
```