{"id":13492619,"url":"https://github.com/smacker/go-tree-sitter","last_synced_at":"2025-05-14T19:08:25.567Z","repository":{"id":37431925,"uuid":"120343377","full_name":"smacker/go-tree-sitter","owner":"smacker","description":"Golang bindings for tree-sitter https://github.com/tree-sitter/tree-sitter","archived":false,"fork":false,"pushed_at":"2024-08-27T09:42:17.000Z","size":55984,"stargazers_count":483,"open_issues_count":35,"forks_count":133,"subscribers_count":11,"default_branch":"master","last_synced_at":"2025-05-14T19:08:22.513Z","etag":null,"topics":["binding","golang","golang-bindings","syntax-tree","tree-sitter"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/smacker.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-02-05T18:20:00.000Z","updated_at":"2025-05-12T02:56:29.000Z","dependencies_parsed_at":"2024-01-16T09:10:11.966Z","dependency_job_id":"c71a926d-393c-45a0-9d92-f5f8a1429d14","html_url":"https://github.com/smacker/go-tree-sitter","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/smacker%2Fgo-tree-sitter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/smacker%2Fgo-tree-sitter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/smacker%2Fgo-tree-sitter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/smacker%2Fgo-tree-sitter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/smacker","download_url":"https://codeload.github.com/smacker/go-tree-sitter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254209859,"owners_count":22032897,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["binding","golang","golang-bindings","syntax-tree","tree-sitter"],"created_at":"2024-07-31T19:01:07.585Z","updated_at":"2025-05-14T19:08:23.329Z","avatar_url":"https://github.com/smacker.png","language":"C","funding_links":[],"categories":["C","Language bindings"],"sub_categories":["Others"],"readme":"# go tree-sitter\n\n[![Build Status](https://github.com/smacker/go-tree-sitter/workflows/Test/badge.svg?branch=master)](https://github.com/smacker/go-tree-sitter/actions/workflows/test.yml?query=branch%3Amaster)\n[![GoDoc](https://godoc.org/github.com/smacker/go-tree-sitter?status.svg)](https://godoc.org/github.com/smacker/go-tree-sitter)\n\nGolang bindings for [tree-sitter](https://github.com/tree-sitter/tree-sitter)\n\n## Usage\n\nCreate a parser with a grammar:\n\n```go\nimport (\n\t\"context\"\n\t\"fmt\"\n\n\tsitter \"github.com/smacker/go-tree-sitter\"\n\t\"github.com/smacker/go-tree-sitter/javascript\"\n)\n\nparser := sitter.NewParser()\nparser.SetLanguage(javascript.GetLanguage())\n```\n\nParse some code:\n\n```go\nsourceCode := []byte(\"let a = 1\")\ntree, _ := parser.ParseCtx(context.Background(), nil, sourceCode)\n```\n\nInspect the syntax tree:\n\n```go\nn := tree.RootNode()\n\nfmt.Println(n) // (program (lexical_declaration (variable_declarator (identifier) (number))))\n\nchild := n.NamedChild(0)\nfmt.Println(child.Type()) // lexical_declaration\nfmt.Println(child.StartByte()) // 0\nfmt.Println(child.EndByte()) // 9\n```\n\n### Custom grammars\n\nThis repository provides grammars for many common languages out of the box.\n\nBut 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. \n\nSee explanation on how to create a grammar for go-tree-sitter [here](https://github.com/smacker/go-tree-sitter/issues/57).\n\nKnown external grammars:\n\n- [Salesforce grammars](https://github.com/aheber/tree-sitter-sfapex) - including Apex, SOQL, and SOSL languages.\n- [Ruby](https://github.com/shagabutdinov/go-tree-sitter-ruby) - Deprecated, grammar is provided by main repo instead\n- [Go Template](https://github.com/mrjosh/helm-ls/tree/master/internal/tree-sitter/gotemplate) - Used for helm\n\n### Editing\n\nIf your source code changes, you can update the syntax tree. This will take less time than the first parse.\n\n```go\n// change 1 -\u003e true\nnewText := []byte(\"let a = true\")\ntree.Edit(sitter.EditInput{\n    StartIndex:  8,\n    OldEndIndex: 9,\n    NewEndIndex: 12,\n    StartPoint: sitter.Point{\n        Row:    0,\n        Column: 8,\n    },\n    OldEndPoint: sitter.Point{\n        Row:    0,\n        Column: 9,\n    },\n    NewEndPoint: sitter.Point{\n        Row:    0,\n        Column: 12,\n    },\n})\n\n// check that it changed tree\nassert.True(n.HasChanges())\nassert.True(n.Child(0).HasChanges())\nassert.False(n.Child(0).Child(0).HasChanges()) // left side of the tree didn't change\nassert.True(n.Child(0).Child(1).HasChanges())\n\n// generate new tree\nnewTree := parser.Parse(tree, newText)\n```\n\n### Predicates\n\nYou can filter AST by using [predicate](https://tree-sitter.github.io/tree-sitter/using-parsers#predicates) S-expressions.\n\nSimilar 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:\n- `eq?`, `not-eq?`\n- `match?`, `not-match?`\n\nUsage [example](./_examples/predicates/main.go):\n\n```go\nfunc main() {\n\t// Javascript code\n\tsourceCode := []byte(`\n\t\tconst camelCaseConst = 1;\n\t\tconst SCREAMING_SNAKE_CASE_CONST = 2;\n\t\tconst lower_snake_case_const = 3;`)\n\t// Query with predicates\n\tscreamingSnakeCasePattern := `(\n\t\t(identifier) @constant\n\t\t(#match? @constant \"^[A-Z][A-Z_]+\")\n\t)`\n\n\t// Parse source code\n\tlang := javascript.GetLanguage()\n\tn, _ := sitter.ParseCtx(context.Background(), sourceCode, lang)\n\t// Execute the query\n\tq, _ := sitter.NewQuery([]byte(screamingSnakeCasePattern), lang)\n\tqc := sitter.NewQueryCursor()\n\tqc.Exec(q, n)\n\t// Iterate over query results\n\tfor {\n\t\tm, ok := qc.NextMatch()\n\t\tif !ok {\n\t\t\tbreak\n\t\t}\n\t\t// Apply predicates filtering\n\t\tm = qc.FilterPredicates(m, sourceCode)\n\t\tfor _, c := range m.Captures {\n\t\t\tfmt.Println(c.Node.Content(sourceCode))\n\t\t}\n\t}\n}\n\n// Output of this program:\n// SCREAMING_SNAKE_CASE_CONST\n```\n\n## Development\n\n### Updating a grammar\n\nCheck if any updates for vendored files are available:\n\n```\ngo run _automation/main.go check-updates\n```\n\nUpdate vendor files:\n\n- open `_automation/grammars.json`\n- modify `reference` (for tagged grammars) or `revision` (for grammars from a branch)\n- run `go run _automation/main.go update \u003cgrammar-name\u003e`\n\nIt is also possible to update all grammars in one go using\n\n```\ngo run _automation/main.go update-all\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsmacker%2Fgo-tree-sitter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsmacker%2Fgo-tree-sitter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsmacker%2Fgo-tree-sitter/lists"}