Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrParseFailed = errors.New("bonsai: parse failed")
ErrParseFailed is returned by Parse when tree-sitter rejects the input outright (returns a NULL tree pointer). The grammar is permissive enough that this is rare. Most malformed source yields a tree containing error nodes rather than a failed parse.
Functions ¶
This section is empty.
Types ¶
type HostEnv ¶
type HostEnv struct {
Mem *Memory
}
HostEnv satisfies the Xenv interface that every tree-sitter wasm module imports. Across grammars these imports are identical (memory plus a few stubs we never reach at runtime) so one HostEnv works for every grammar without per-grammar wrappers.
func (*HostEnv) Xfputc ¶
Stdio stubs. tree-sitter's parser.c references these from debug-only branches we never trigger (no dot_graph_file is set from our exported API). Returning 0 satisfies the import.
func (*HostEnv) Xts_language_is_wasm ¶
Xts_language_is_wasm: every grammar is statically linked into its wasm module, not loaded from a runtime .wasm via wasm_store. Always false.
func (*HostEnv) Xts_wasm_store_delete ¶
Xts_wasm_store_delete: unreachable, never called when is_wasm is false.
type Memory ¶
Memory backs the WASM module's linear memory.
The portable implementation grows by appending Go-side, which can relocate the backing array. Every helper in marshal.go re-fetches the slice through Slice() before each access. Do not cache the byte slice across allocations.
type MemoryAPI ¶
MemoryAPI is the interface the wasm-module side calls when it needs linear memory. Declared as a type alias (note the `=`) so that, when HostEnv.Xmemory returns MemoryAPI, the return type unifies with each grammar's generated `Memory` alias and HostEnv structurally satisfies every grammar's Xenv interface.
type Module ¶
type Module interface {
X_initialize()
Xmalloc(int32) int32
Xfree(int32)
Xts_parser_new() int32
Xts_parser_delete(int32)
Xts_parser_set_language(parser, language int32) int32
Xts_parser_parse_string(parser, oldTree, src, length int32) int32
Xts_tree_delete(int32)
Xts_tree_root_node(sret, tree int32)
Xts_node_type(sret int32) int32
Xts_node_start_byte(sret int32) int32
Xts_node_end_byte(sret int32) int32
Xts_node_is_named(sret int32) int32
Xts_node_is_error(sret int32) int32
Xts_node_is_missing(sret int32) int32
Xts_node_has_error(sret int32) int32
Xts_node_start_point(sret, node int32)
Xts_node_end_point(sret, node int32)
Xts_tree_cursor_new(sret, node int32)
Xts_tree_cursor_delete(cursor int32)
Xts_tree_cursor_current_node(sret, cursor int32)
Xts_tree_cursor_current_field_name(cursor int32) int32
Xts_tree_cursor_goto_first_child(cursor int32) int32
Xts_tree_cursor_goto_next_sibling(cursor int32) int32
Xts_tree_cursor_goto_parent(cursor int32) int32
}
Module is the subset of every tree-sitter wasm module that the bonsai runtime invokes during parsing. Each grammar's generated *Module structurally implements it. There is no interface declaration on the generated side. Adding a method here requires every grammar's wasm to export the matching tree-sitter symbol.
The grammar-specific language pointer (e.g. Xtree_sitter_python) is NOT part of this interface. Each grammar's NewParser fetches it from its own *Module and passes it to NewFromModule as a plain int32.
type Node ¶
type Node struct {
Type string
Named bool
Field string // field name in parent ("" if unnamed slot)
IsError bool // this node is the ERROR sentinel
IsMissing bool // this node was inserted to recover from a parse error
StartByte uint32
EndByte uint32
StartPoint Point
EndPoint Point
Parent *Node // nil at root
Children []*Node
// contains filtered or unexported fields
}
Node is a fully-owned snapshot of a tree-sitter node. There is no live dependency on WASM memory: after Parse returns, the tree has been deleted and the Go GC owns every Node.
func (*Node) ChildByField ¶
ChildByField returns the first child whose Field name equals name, or nil.
func (*Node) Find ¶
Find yields, in preorder, every descendant of n whose Type == typ. n itself is included if it matches.
func (*Node) HasError ¶
HasError reports whether the subtree rooted at n contains any error or missing nodes. Equivalent to tree-sitter's ts_node_has_error.
type Parser ¶
type Parser struct {
// contains filtered or unexported fields
}
Parser owns one wasm module instance plus a TSParser*. NOT safe for concurrent use. Pool a Parser per goroutine. Reuse a Parser across files. Module instantiation is the expensive part, and once warm a Parser holds onto its linear-memory high-water mark.
Grammar-specific construction lives in each grammar's package (e.g. bonsai-python.NewParser).
func NewFromModule ¶
NewFromModule wires up a parser around an already-instantiated wasm module and the language pointer the grammar exposes. Called by each grammar package after constructing its module.
func (*Parser) Parse ¶
Parse builds an owned Node snapshot from src. After Parse returns, no wasm-side data backs the snapshot. The tree has been freed.
If p was configured with sub-parsers via With, Parse applies them automatically, returning the unified tree.
Parse is not concurrent-safe with itself on the same Parser.
func (*Parser) With ¶
With registers sub-parsers on p. After With returns, p.Parse(src) applies the rules automatically and returns one unified tree. Returns p for chaining.
Markdown is the canonical case (block grammar dispatching to inline grammar):
full := bonsaimarkdown.NewParser().With(bonsai.SubParser{
Match: func(n *bonsai.Node) bool { return n.Type == "inline" },
Parser: bonsaimarkdowninline.NewParser(),
})
root, _ := full.Parse(src) // tree contains block + inline nodes
Subtrees are themselves walked for further matches, so multi-layer parsing (e.g. HTML containing JS containing a template literal containing SQL) composes naturally. Beware a Match that fires on its own grammar's output: it would recurse forever. (Specifically, recursion only re-checks the CHILDREN of a replaced node, so a sub-parser whose root has the same Type as the original match is safe.)
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
bonsai-bash
module
|
|
|
bonsai-c
module
|
|
|
bonsai-dockerfile
module
|
|
|
bonsai-go
module
|
|
|
bonsai-gotemplate
module
|
|
|
bonsai-groovy
module
|
|
|
bonsai-java
module
|
|
|
bonsai-markdown
module
|
|
|
bonsai-markdown-inline
module
|
|
|
bonsai-python
module
|
|
|
bonsai-ruby
module
|
|
|
bonsai-terraform
module
|
|
|
bonsai-tsx
module
|
|
|
bonsai-yaml
module
|