taproot

package
v0.20.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 6, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package taproot is the common front-end harness shared by M31 DSLs that use the gotreesitter runtime. It provides:

  • Language: grammar generation + per-name caching (sync.Mutex + map).
  • Walker: a bundle of *Language + source bytes with CST cursor helpers.
  • Parse: the one-stop flow (Language → parse → error check).
  • LanguageFromBlob/ParseFromBlob: the same cache/parse flow, but preferring an embedded pre-generated grammar blob when one is available.

The error-leaf finder in Walker.SyntaxError is lifted from ~/work/elio/parse/tree.go (treeWalker.syntaxError): it collects every ERROR/MISSING leaf, picks the latest-positioned one — preferring MISSING nodes because they pinpoint expected-but-absent tokens — and formats either "expected <type>" (MISSING) or "near <text>" (ERROR).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Language

func Language(name string, build func() *grammargen.Grammar) (*gts.Language, error)

Language generates and caches (once per name) the tree-sitter Language for a grammar. build is called only on a cache miss; subsequent calls for the same name return the cached result without calling build again.

func LanguageFromBlob

func LanguageFromBlob(name string, blob []byte, build func() *grammargen.Grammar) (*gts.Language, error)

LanguageFromBlob loads and caches (once per name) the tree-sitter Language from blob when possible, falling back to build when blob is empty or corrupt. This is the fast path for DSLs that embed a generated grammar blob but still want a source grammar fallback during development.

Types

type Walker

type Walker struct {
	Lang *gts.Language
	Src  []byte
}

Walker bundles a parse's language and source bytes with CST cursor helpers. Lifted from elio/parse/tree.go (treeWalker) and confirmed against selena/parse/parse.go (walker) and eos/syntax/cst.go (cstLowerer).

func NewWalker

func NewWalker(lang *gts.Language, src []byte) *Walker

NewWalker constructs a Walker for the given language and source.

func Parse

func Parse(name string, build func() *grammargen.Grammar, src []byte) (*gts.Node, *Walker, error)

Parse runs the full common DSL parse flow:

  1. Obtain (or generate+cache) the Language for name.
  2. Parse src with a new Parser.
  3. If the root HasError, return (root, walker, walker.SyntaxError(root)).
  4. Otherwise return (root, walker, nil).

root and walker are always non-nil when the language step and parse step succeed (even when a syntax error is present), so callers can inspect the partial tree.

func ParseFromBlob

func ParseFromBlob(name string, blob []byte, build func() *grammargen.Grammar, src []byte) (*gts.Node, *Walker, error)

ParseFromBlob is the blob-aware variant of Parse. It obtains the language via LanguageFromBlob, then follows the same parse/error flow as Parse.

func (*Walker) ChildByType

func (w *Walker) ChildByType(n *gts.Node, typ string) *gts.Node

ChildByType returns the first direct child of n with grammar type typ, or nil.

func (*Walker) Field

func (w *Walker) Field(n *gts.Node, field string) *gts.Node

Field returns the child of n that is bound to the named field, or nil.

func (*Walker) Pos

func (w *Walker) Pos(n *gts.Node) (line, col int)

Pos returns the 1-based line and column where n begins.

func (*Walker) SyntaxError

func (w *Walker) SyntaxError(root *gts.Node) error

SyntaxError walks the subtree rooted at root to find the best error or missing leaf and returns a formatted error.

Strategy (lifted from elio/parse/tree.go treeWalker.syntaxError):

  1. Collect every leaf that is an ERROR node or a MISSING node.
  2. Among those, prefer MISSING (which pinpoints an expected-but-absent token). Among peers of the same kind, pick the latest start byte (where the LR parser got stuck).
  3. Format as "line:col: syntax error: expected <type>" for MISSING, or "line:col: syntax error near <text>" for ERROR.

func (*Walker) Text

func (w *Walker) Text(n *gts.Node) string

Text returns the source text spanned by n.

func (*Walker) Type

func (w *Walker) Type(n *gts.Node) string

Type returns the grammar type name of n.

Directories

Path Synopsis
Package diag provides a generic structured diagnostic type and a source-quoting renderer.
Package diag provides a generic structured diagnostic type and a source-quoting renderer.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL