diff options
| author | Mitja Felicijan <mitja.felicijan@gmail.com> | 2026-01-21 20:22:09 +0100 |
|---|---|---|
| committer | Mitja Felicijan <mitja.felicijan@gmail.com> | 2026-01-21 20:22:09 +0100 |
| commit | 5a8dbc6347b3541e84fe669b22c17ad3b715e258 (patch) | |
| tree | b148c450939688caaaeb4adac6f2faa1eaffe649 /vendor/github.com/mitjafelicijan/go-tree-sitter/README.md | |
| download | qwe-editor-5a8dbc6347b3541e84fe669b22c17ad3b715e258.tar.gz | |
Engage!
Diffstat (limited to 'vendor/github.com/mitjafelicijan/go-tree-sitter/README.md')
| -rw-r--r-- | vendor/github.com/mitjafelicijan/go-tree-sitter/README.md | 162 |
1 files changed, 162 insertions, 0 deletions
diff --git a/vendor/github.com/mitjafelicijan/go-tree-sitter/README.md b/vendor/github.com/mitjafelicijan/go-tree-sitter/README.md new file mode 100644 index 0000000..333b92b --- /dev/null +++ b/vendor/github.com/mitjafelicijan/go-tree-sitter/README.md | |||
| @@ -0,0 +1,162 @@ | |||
| 1 | # go tree-sitter | ||
| 2 | |||
| 3 | [](https://github.com/mitjafelicijan/go-tree-sitter/actions/workflows/test.yml?query=branch%3Amaster) | ||
| 4 | [](https://godoc.org/github.com/mitjafelicijan/go-tree-sitter) | ||
| 5 | |||
| 6 | Golang bindings for [tree-sitter](https://github.com/tree-sitter/tree-sitter) | ||
| 7 | |||
| 8 | ## Usage | ||
| 9 | |||
| 10 | Create a parser with a grammar: | ||
| 11 | |||
| 12 | ```go | ||
| 13 | import ( | ||
| 14 | "context" | ||
| 15 | "fmt" | ||
| 16 | |||
| 17 | sitter "github.com/mitjafelicijan/go-tree-sitter" | ||
| 18 | "github.com/mitjafelicijan/go-tree-sitter/javascript" | ||
| 19 | ) | ||
| 20 | |||
| 21 | parser := sitter.NewParser() | ||
| 22 | parser.SetLanguage(javascript.GetLanguage()) | ||
| 23 | ``` | ||
| 24 | |||
| 25 | Parse some code: | ||
| 26 | |||
| 27 | ```go | ||
| 28 | sourceCode := []byte("let a = 1") | ||
| 29 | tree, _ := parser.ParseCtx(context.Background(), nil, sourceCode) | ||
| 30 | ``` | ||
| 31 | |||
| 32 | Inspect the syntax tree: | ||
| 33 | |||
| 34 | ```go | ||
| 35 | n := tree.RootNode() | ||
| 36 | |||
| 37 | fmt.Println(n) // (program (lexical_declaration (variable_declarator (identifier) (number)))) | ||
| 38 | |||
| 39 | child := n.NamedChild(0) | ||
| 40 | fmt.Println(child.Type()) // lexical_declaration | ||
| 41 | fmt.Println(child.StartByte()) // 0 | ||
| 42 | fmt.Println(child.EndByte()) // 9 | ||
| 43 | ``` | ||
| 44 | |||
| 45 | ### Custom grammars | ||
| 46 | |||
| 47 | This repository provides grammars for many common languages out of the box. | ||
| 48 | |||
| 49 | But if you need support for any other language you can keep it inside your own project or publish it as a separate repository to share with the community. | ||
| 50 | |||
| 51 | See explanation on how to create a grammar for go-tree-sitter [here](https://github.com/mitjafelicijan/go-tree-sitter/issues/57). | ||
| 52 | |||
| 53 | Known external grammars: | ||
| 54 | |||
| 55 | - [Salesforce grammars](https://github.com/aheber/tree-sitter-sfapex) - including Apex, SOQL, and SOSL languages. | ||
| 56 | - [Ruby](https://github.com/shagabutdinov/go-tree-sitter-ruby) - Deprecated, grammar is provided by main repo instead | ||
| 57 | - [Go Template](https://github.com/mrjosh/helm-ls/tree/master/internal/tree-sitter/gotemplate) - Used for helm | ||
| 58 | |||
| 59 | ### Editing | ||
| 60 | |||
| 61 | If your source code changes, you can update the syntax tree. This will take less time than the first parse. | ||
| 62 | |||
| 63 | ```go | ||
| 64 | // change 1 -> true | ||
| 65 | newText := []byte("let a = true") | ||
| 66 | tree.Edit(sitter.EditInput{ | ||
| 67 | StartIndex: 8, | ||
| 68 | OldEndIndex: 9, | ||
| 69 | NewEndIndex: 12, | ||
| 70 | StartPoint: sitter.Point{ | ||
| 71 | Row: 0, | ||
| 72 | Column: 8, | ||
| 73 | }, | ||
| 74 | OldEndPoint: sitter.Point{ | ||
| 75 | Row: 0, | ||
| 76 | Column: 9, | ||
| 77 | }, | ||
| 78 | NewEndPoint: sitter.Point{ | ||
| 79 | Row: 0, | ||
| 80 | Column: 12, | ||
| 81 | }, | ||
| 82 | }) | ||
| 83 | |||
| 84 | // check that it changed tree | ||
| 85 | assert.True(n.HasChanges()) | ||
| 86 | assert.True(n.Child(0).HasChanges()) | ||
| 87 | assert.False(n.Child(0).Child(0).HasChanges()) // left side of the tree didn't change | ||
| 88 | assert.True(n.Child(0).Child(1).HasChanges()) | ||
| 89 | |||
| 90 | // generate new tree | ||
| 91 | newTree := parser.Parse(tree, newText) | ||
| 92 | ``` | ||
| 93 | |||
| 94 | ### Predicates | ||
| 95 | |||
| 96 | You can filter AST by using [predicate](https://tree-sitter.github.io/tree-sitter/using-parsers#predicates) S-expressions. | ||
| 97 | |||
| 98 | Similar to [Rust](https://github.com/tree-sitter/tree-sitter/tree/master/lib/binding_rust) or [WebAssembly](https://github.com/tree-sitter/tree-sitter/blob/master/lib/binding_web) bindings we support filtering on a few common predicates: | ||
| 99 | - `eq?`, `not-eq?` | ||
| 100 | - `match?`, `not-match?` | ||
| 101 | |||
| 102 | Usage [example](./_examples/predicates/main.go): | ||
| 103 | |||
| 104 | ```go | ||
| 105 | func main() { | ||
| 106 | // Javascript code | ||
| 107 | sourceCode := []byte(` | ||
| 108 | const camelCaseConst = 1; | ||
| 109 | const SCREAMING_SNAKE_CASE_CONST = 2; | ||
| 110 | const lower_snake_case_const = 3;`) | ||
| 111 | // Query with predicates | ||
| 112 | screamingSnakeCasePattern := `( | ||
| 113 | (identifier) @constant | ||
| 114 | (#match? @constant "^[A-Z][A-Z_]+") | ||
| 115 | )` | ||
| 116 | |||
| 117 | // Parse source code | ||
| 118 | lang := javascript.GetLanguage() | ||
| 119 | n, _ := sitter.ParseCtx(context.Background(), sourceCode, lang) | ||
| 120 | // Execute the query | ||
| 121 | q, _ := sitter.NewQuery([]byte(screamingSnakeCasePattern), lang) | ||
| 122 | qc := sitter.NewQueryCursor() | ||
| 123 | qc.Exec(q, n) | ||
| 124 | // Iterate over query results | ||
| 125 | for { | ||
| 126 | m, ok := qc.NextMatch() | ||
| 127 | if !ok { | ||
| 128 | break | ||
| 129 | } | ||
| 130 | // Apply predicates filtering | ||
| 131 | m = qc.FilterPredicates(m, sourceCode) | ||
| 132 | for _, c := range m.Captures { | ||
| 133 | fmt.Println(c.Node.Content(sourceCode)) | ||
| 134 | } | ||
| 135 | } | ||
| 136 | } | ||
| 137 | |||
| 138 | // Output of this program: | ||
| 139 | // SCREAMING_SNAKE_CASE_CONST | ||
| 140 | ``` | ||
| 141 | |||
| 142 | ## Development | ||
| 143 | |||
| 144 | ### Updating a grammar | ||
| 145 | |||
| 146 | Check if any updates for vendored files are available: | ||
| 147 | |||
| 148 | ``` | ||
| 149 | go run _automation/main.go check-updates | ||
| 150 | ``` | ||
| 151 | |||
| 152 | Update vendor files: | ||
| 153 | |||
| 154 | - open `_automation/grammars.json` | ||
| 155 | - modify `reference` (for tagged grammars) or `revision` (for grammars from a branch) | ||
| 156 | - run `go run _automation/main.go update <grammar-name>` | ||
| 157 | |||
| 158 | It is also possible to update all grammars in one go using | ||
| 159 | |||
| 160 | ``` | ||
| 161 | go run _automation/main.go update-all | ||
| 162 | ``` | ||
