parser

package
v1.7.2 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2025 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrFileTooLarge = fmt.Errorf("file too large")

ErrFileTooLarge is returned when a file exceeds the maximum size limit.

Functions

func FindNodes

func FindNodes(root *sitter.Node, source []byte, predicate func(*sitter.Node) bool) []*sitter.Node

FindNodes returns all nodes matching a predicate.

func FindNodesByType

func FindNodesByType(root *sitter.Node, source []byte, nodeType string) []*sitter.Node

FindNodesByType returns all nodes of a specific type.

func GetNodeText

func GetNodeText(node *sitter.Node, source []byte) string

GetNodeText extracts the source text for a node. Returns empty string if node is nil or byte offsets are out of bounds.

func GetTreeSitterLanguage

func GetTreeSitterLanguage(lang Language) (*sitter.Language, error)

GetTreeSitterLanguage returns the tree-sitter language for a Language enum.

func Walk

func Walk(node *sitter.Node, source []byte, visitor NodeVisitor)

Walk traverses the AST calling visitor for each node.

func WalkTyped

func WalkTyped(node *sitter.Node, source []byte, visitor TypedNodeVisitor)

WalkTyped traverses the AST with cached node types to reduce CGO overhead. Use this when you need to check node types frequently.

Types

type ClassNode

type ClassNode struct {
	Name      string
	StartLine uint32
	EndLine   uint32
	Methods   []FunctionNode
}

ClassNode represents a parsed class/struct.

func GetClasses

func GetClasses(result *ParseResult) []ClassNode

GetClasses extracts all class definitions from parsed code.

type FunctionNode

type FunctionNode struct {
	Name       string
	StartLine  uint32
	EndLine    uint32
	Parameters []string
	Body       *sitter.Node
}

FunctionNode represents a parsed function.

func GetFunctions

func GetFunctions(result *ParseResult) []FunctionNode

GetFunctions extracts all function definitions from parsed code.

type Language

type Language string

Language represents a supported programming language.

const (
	LangGo         Language = "go"
	LangRust       Language = "rust"
	LangPython     Language = "python"
	LangTypeScript Language = "typescript"
	LangJavaScript Language = "javascript"
	LangTSX        Language = "tsx"
	LangJava       Language = "java"
	LangC          Language = "c"
	LangCPP        Language = "cpp"
	LangCSharp     Language = "csharp"
	LangRuby       Language = "ruby"
	LangPHP        Language = "php"
	LangBash       Language = "bash"
	LangUnknown    Language = "unknown"
)

func DetectLanguage

func DetectLanguage(path string) Language

DetectLanguage determines the language from a file path.

type NodeVisitor

type NodeVisitor func(node *sitter.Node, source []byte) bool

NodeVisitor is a function that visits AST nodes.

type ParseResult

type ParseResult struct {
	Tree     *sitter.Tree
	Language Language
	Source   []byte
	Path     string
}

ParseResult contains the parsed AST and metadata.

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

Parser wraps tree-sitter for multi-language parsing.

func New

func New() *Parser

New creates a new parser instance.

func (*Parser) Close

func (p *Parser) Close()

Close releases parser resources.

func (*Parser) Parse

func (p *Parser) Parse(source []byte, lang Language, path string) (*ParseResult, error)

Parse parses source code with a specified language.

func (*Parser) ParseFile

func (p *Parser) ParseFile(path string) (*ParseResult, error)

ParseFile parses a source file and returns the AST.

func (*Parser) ParseFileWithLimit

func (p *Parser) ParseFileWithLimit(path string, maxSize int64) (*ParseResult, error)

ParseFileWithLimit parses a source file with a maximum size limit. If maxSize is 0, no limit is enforced.

type TypedNodeVisitor

type TypedNodeVisitor func(node *sitter.Node, nodeType string, source []byte) bool

TypedNodeVisitor visits AST nodes with pre-cached node type to avoid CGO overhead.

Jump to

Keyboard shortcuts

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