treesitter

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package treesitter is strictcode's single parsing path: a thin, disciplined layer over the official tree-sitter CGo bindings (the binding-benchmark winner; see BUILDLOG.md). It owns the three responsibilities the rest of the codebase must never re-implement:

  • grammar selection for the language trio (Python, Go, TS/JS);
  • LF normalization before parsing, so every byte span in the system is a span over LF-normalized UTF-8 (schema/SPEC.md section 3);
  • CGo resource lifecycle (Close on parsers, trees, queries, cursors), so extractors cannot leak C memory.

There is no fallback parser and no regex path; a parse failure is an error.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NormalizeLF

func NormalizeLF(src []byte) []byte

NormalizeLF converts CRLF and lone CR line endings to LF. Canonical byte positions everywhere in strictcode are offsets into this normalized form (schema/SPEC.md section 3). The input slice is never modified; when no normalization is needed the input is returned as-is.

Types

type Capture

type Capture struct {
	Name string
	Node sitter.Node
}

Capture is one captured node with its capture name.

type Grammar

type Grammar int

Grammar identifies a concrete tree-sitter grammar. The TS/JS profile column ("ts") spans two grammar variants: the typescript grammar parses .ts and plain JavaScript; the tsx grammar parses .tsx/.jsx (JSX syntax conflicts with TS type assertions, so tree-sitter ships them as separate grammars).

const (
	GrammarPython Grammar = iota
	GrammarGo
	GrammarTypeScript
	GrammarTSX
)

func GrammarForFile

func GrammarForFile(filename string) (Grammar, bool)

GrammarForFile maps a filename to its grammar. The boolean is false for files strictcode does not parse. Extension mapping follows DESIGN.md section 6.2 (TS/JS resolution probes .ts/.tsx/.js/.jsx/.mjs/.cjs).

func (Grammar) String

func (g Grammar) String() string

type Match

type Match struct {
	PatternIndex uint
	Captures     []Capture
}

Match is one query match: the pattern index within the query and its captures in capture order.

type Query

type Query struct {
	Grammar Grammar
	// contains filtered or unexported fields
}

Query is a compiled tree-sitter query for one grammar. Callers must Close it. Queries are compiled once and reused across many trees.

func CompileQuery

func CompileQuery(g Grammar, pattern string) (*Query, error)

CompileQuery compiles a query pattern against a grammar. Pattern errors are hard errors carrying the tree-sitter diagnostic.

func (*Query) Close

func (q *Query) Close()

Close releases the underlying C query. Idempotent.

func (*Query) Matches

func (q *Query) Matches(t *Tree) []Match

Matches runs the query over the tree and returns all matches with text predicates (#eq?, #match?, #any-of?, ...) applied. The cursor is created and closed internally; returned nodes are valid until the tree is Closed. Matches panics if the query and tree grammars differ — that is a programming error in the caller, never an input condition.

type Tree

type Tree struct {
	// Source is the LF-normalized UTF-8 the tree was parsed from. All node
	// spans index into this slice, never into the raw file bytes.
	Source  []byte
	Grammar Grammar
	// contains filtered or unexported fields
}

Tree is a parsed file: the LF-normalized source and the syntax tree over it. Node byte offsets index Source. Callers must Close it.

func Parse

func Parse(g Grammar, src []byte) (*Tree, error)

Parse normalizes src to LF and parses it with the given grammar. The returned tree must be Closed. A nil tree from the runtime (the only failure mode of ts_parser_parse without timeouts/cancellation, e.g. a grammar/runtime version mismatch) is a hard error.

func (*Tree) Close

func (t *Tree) Close()

Close releases the underlying C tree. Idempotent.

func (*Tree) HasParseErrors

func (t *Tree) HasParseErrors() bool

HasParseErrors reports whether the tree contains ERROR or MISSING nodes. tree-sitter is error-tolerant; strictcode is honest about it — extractors consult this instead of silently analyzing a broken tree.

func (*Tree) Root

func (t *Tree) Root() *sitter.Node

Root returns the root node. Valid only until Close.

Jump to

Keyboard shortcuts

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