treesitter

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2026 License: MIT Imports: 11 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DetectGrammarEntry added in v0.4.0

func DetectGrammarEntry(filename string) *grammars.LangEntry

func DetectLanguage

func DetectLanguage(filename string) (*gotreesitter.Language, error)

func IsWordChar added in v0.4.0

func IsWordChar(c byte) bool

IsWordChar checks if c is an ASCII letter, digit, or underscore.

Types

type ASTIndex added in v0.4.0

type ASTIndex struct {
	Nodes      []*ASTNode
	PostOrder  []*ASTNode
	LabelIndex map[string][]int32
}

ASTIndex holds pre-order and post-order arrays for fast subtree lookups.

func EnsureIndex added in v0.4.0

func EnsureIndex(root *ASTNode) *ASTIndex

EnsureIndex indexes the tree if it hasn't been indexed yet.

type ASTNode

type ASTNode struct {
	Type            string
	Label           string
	Children        []*ASTNode
	Parent          *ASTNode
	StartByte       uint32
	EndByte         uint32
	StartRow        uint32 // 0-indexed
	StartCol        uint32
	EndRow          uint32
	EndCol          uint32
	Language        string // Set on root node only
	HasError        bool   // True if parse tree contained any ERROR nodes
	ParseErrorCount int    // Total count of ERROR nodes in parse tree
	IsKeyword       bool   // True if node is a keyword token
	IsUnordered     bool   // True if children of this container node are order-insensitive

	// Hash is the combined hash of node type, label, and children.
	Hash uint64
	// StructureHash is the shape hash, ignoring leaf labels.
	StructureHash uint64

	ID        int32
	PreSize   int32
	PostStart int32
	Index     *ASTIndex
}

func BuildAST

func BuildAST(n *gotreesitter.Node, src []byte, lang *gotreesitter.Language, parent *ASTNode) *ASTNode

func Parse

func Parse(src []byte, filename string) (*ASTNode, error)

func ParseWithLanguage added in v0.4.0

func ParseWithLanguage(src []byte, lang *gotreesitter.Language) (*ASTNode, error)

func (*ASTNode) ChildIndex added in v0.4.0

func (n *ASTNode) ChildIndex() int

ChildIndex returns the node's position among its parent's children, or -1 if it has no parent.

func (*ASTNode) ComputeHashes added in v0.4.0

func (n *ASTNode) ComputeHashes()

ComputeHashes fills Hash and StructureHash for the node and its children.

func (*ASTNode) Contains added in v0.4.0

func (n *ASTNode) Contains(candidate *ASTNode) bool

Contains returns true if candidate is a descendant of n.

func (*ASTNode) DepthTo added in v0.5.0

func (n *ASTNode) DepthTo(ancestor *ASTNode) int

DepthTo returns the number of parent edges between n and ancestor (or 0 if n is ancestor or ancestor is nil).

func (*ASTNode) Descendants added in v0.4.0

func (n *ASTNode) Descendants() []*ASTNode

Descendants returns all child nodes under n in pre-order.

func (*ASTNode) FrequencyInSubtree added in v0.4.0

func (n *ASTNode) FrequencyInSubtree(label string) int

FrequencyInSubtree returns how many times a leaf label appears in n's subtree.

func (*ASTNode) GetLanguage

func (n *ASTNode) GetLanguage() string

GetLanguage walks up to the root to retrieve the AST's language.

func (*ASTNode) IsBracketOrParen added in v0.4.0

func (n *ASTNode) IsBracketOrParen() bool

IsBracketOrParen reports whether the node label is a bracket, brace, or parenthesis.

func (*ASTNode) IsLeafOrStringLiteral added in v0.4.0

func (n *ASTNode) IsLeafOrStringLiteral() bool

IsLeafOrStringLiteral checks if n is a leaf node or string literal.

func (*ASTNode) IsScaffolding

func (n *ASTNode) IsScaffolding() bool

IsScaffolding checks rules.yml to see if this node type is a variable-arity container.

func (*ASTNode) LeafLabels added in v0.4.0

func (n *ASTNode) LeafLabels() map[string]int

LeafLabels returns counts of each leaf label in the subtree.

func (*ASTNode) Leaves added in v0.4.0

func (n *ASTNode) Leaves() []*ASTNode

Leaves returns all leaf nodes (and atomic string literals) in pre-order under n.

func (*ASTNode) LevelOrder added in v0.4.0

func (n *ASTNode) LevelOrder() []*ASTNode

LevelOrder returns all nodes in the subtree level by level (breadth-first).

func (*ASTNode) PostOrder added in v0.4.0

func (n *ASTNode) PostOrder() []*ASTNode

PostOrder returns all nodes in children-first order.

func (*ASTNode) PreOrder added in v0.4.0

func (n *ASTNode) PreOrder() []*ASTNode

PreOrder returns all nodes in parent-first order.

func (*ASTNode) Root added in v0.4.0

func (n *ASTNode) Root() *ASTNode

Root walks up to the root node of the AST.

func (*ASTNode) Size

func (n *ASTNode) Size() int

Size returns the total number of nodes in the subtree rooted at n.

type Rules

type Rules struct {
	Flattened       []string          `yaml:"flattened"`
	Ignored         []string          `yaml:"ignored"`
	Aliased         map[string]string `yaml:"aliased"`
	LabelIgnored    []string          `yaml:"label_ignored"`
	Scaffolding     []string          `yaml:"scaffolding"`
	Keywords        []string          `yaml:"keywords"`
	Declarations    []string          `yaml:"declarations"`
	Identifiers     []string          `yaml:"identifiers"`
	Blocks          []string          `yaml:"blocks"`
	Pairs           []string          `yaml:"pairs"`
	Unordered       []string          `yaml:"unordered"`
	EquivalentTypes [][]string        `yaml:"equivalent_types"`
	// contains filtered or unexported fields
}

Rules configures language-specific AST transformations and node matching.

func GetRules

func GetRules(lang string) *Rules

GetRules returns the compiled AST rules for a language, or nil if none exist.

func (*Rules) Alias added in v0.6.0

func (r *Rules) Alias(nodeType, label string) (string, bool)

Alias returns the replacement node type if one exists for the label or node type.

func (*Rules) AreTypesEquivalent added in v0.6.0

func (r *Rules) AreTypesEquivalent(t1, t2 string) bool

AreTypesEquivalent checks if t1 and t2 belong to the same equivalence group.

func (*Rules) IsBlock added in v0.6.0

func (r *Rules) IsBlock(nodeType string) bool

IsBlock checks if this node type is a code block.

func (*Rules) IsFlattened added in v0.6.0

func (r *Rules) IsFlattened(nodeType string) bool

IsFlattened checks if intermediate nodes of this type should merge into their parent.

func (*Rules) IsIgnored added in v0.6.0

func (r *Rules) IsIgnored(nodeType, label string) bool

IsIgnored checks if a node type or label is filtered out when building the AST.

func (*Rules) IsKeyword added in v0.6.0

func (r *Rules) IsKeyword(nodeType, label string) bool

IsKeyword checks if a node type or label is a language keyword.

func (*Rules) IsLabelIgnored added in v0.6.0

func (r *Rules) IsLabelIgnored(nodeType string) bool

IsLabelIgnored checks if node labels should be dropped for this type.

func (*Rules) IsUnordered added in v0.6.0

func (r *Rules) IsUnordered(nodeType string) bool

IsUnordered checks if child order doesn't matter for this container.

Jump to

Keyboard shortcuts

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