tree_sitter

package
v0.5.3 Latest Latest
Warning

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

Go to latest
Published: May 29, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DumpTree

func DumpTree(tree *sitter.Tree, source []byte, w io.Writer, format DumpFormat, options DumpOptions) error

DumpTree dumps a tree to the specified writer using the given format and options

func NodeToLispExpression

func NodeToLispExpression(node *sitter.Node, content []byte, includeAnonymous bool) pm.Expression

NodeToLispExpression converts a tree-sitter node into a Lisp-style S-expression represented using the patternmatcher Expression types.

The resulting expression has the structure:

(node_type [ (field_name child) | child ] ...)

- node_type is a pm.Symbol with the node's Type() - Fields are represented as 2-element lists: (field_name child) - Anonymous children without a field are included directly - Leaf nodes are represented as a single-element list: (node_type)

Types

type Capture

type Capture struct {
	// Name if the capture name from the query
	Name string
	// Text is the actual text that was captured
	Text string
	// Type is the Treesitter type of the captured node
	Type string

	// TODO(manuel, 2023-04-23): Add more information about the capture
	// for example: offset, line number, filename, query name, ...
	StartByte  uint32
	EndByte    uint32
	StartPoint sitter.Point
	EndPoint   sitter.Point
}

type DumpFormat

type DumpFormat = dump.Format

DumpFormat represents a tree output format type

const (
	FormatText DumpFormat = dump.FormatText
	FormatXML  DumpFormat = dump.FormatXML
	FormatJSON DumpFormat = dump.FormatJSON
	FormatYAML DumpFormat = dump.FormatYAML
)

Format constants

type DumpOptions

type DumpOptions = dump.Options

DumpOptions contains settings for tree dumping

type Dumper

type Dumper = dump.Dumper

Dumper is the interface that all tree dumpers must implement

func NewDumper

func NewDumper(format DumpFormat) Dumper

NewDumper creates a new tree dumper for the specified format

type Match

type Match map[string]Capture

type QueryResults

type QueryResults map[string]*Result

func ExecuteQueries

func ExecuteQueries(
	lang *sitter.Language,
	tree *sitter.Node,
	queries []SitterQuery,
	sourceCode []byte,
) (QueryResults, error)

ExecuteQueries runs the given queries on the given tree and returns the results. Individual names are resolved using the sourceCode string, so as to provide full identifier names when matched.

TODO(manuel, 2023-06-19) We only need the language from oc here, right?

type Result

type Result struct {
	QueryName string
	Matches   []Match
}

func (*Result) Clone

func (r *Result) Clone() *Result

type SitterQuery

type SitterQuery struct {
	// Name of the resulting variable after parsing
	Name string `yaml:"name"`
	// Query contains the tree-sitter query that will be applied to the source code
	Query string `yaml:"query"`
	// Rendered keeps track if the Query was Rendered with RenderQueries.
	// This is an ugly way of doing things, but at least we'll signal at runtime
	// if the code tries to render a query multiple times.
	// See the NOTEs in RenderQueries.
	Rendered bool
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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