sitter

package module
v0.0.0-...-7a511b9 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2020 License: MIT Imports: 6 Imported by: 0

README

go tree-sitter

Build Status GoDoc

Golang bindings for tree-sitter

Usage

Create a parser with that grammar:

import (
	sitter "github.com/kiteco/go-tree-sitter"
	"github.com/kiteco/go-tree-sitter/javascript"
)

parser := sitter.NewParser()
parser.SetLanguage(javascript.GetLanguage())

Parse some code:

sourceCode = []byte("let a = 1")
tree := parser.Parse(sourceCode)

Inspect the syntax tree:

n := tree.RootNode()

fmt.Println(n) // (program (lexical_declaration (variable_declarator (identifier) (number))))

child := n.NamedChild(0)
fmt.Println(child.Type()) // lexical_declaration
fmt.Println(child.StartByte()) // 0
fmt.Println(child.EndByte()) // 9

If your source code changes, you can update the syntax tree. This will take less time than the first parse.

// change 1 -> true
newText := []byte("let a = true")
tree.Edit(sitter.EditInput{
    StartIndex:  8,
    OldEndIndex: 9,
    NewEndIndex: 12,
    StartPoint: sitter.Point{
        Row:    0,
        Column: 8,
    },
    OldEndPoint: sitter.Point{
        Row:    0,
        Column: 9,
    },
    NewEndPoint: sitter.Point{
        Row:    0,
        Column: 12,
    },
})

// check that it changed tree
assert.True(n.HasChanges())
assert.True(n.Child(0).HasChanges())
assert.False(n.Child(0).Child(0).HasChanges()) // left side of the tree didn't change
assert.True(n.Child(0).Child(1).HasChanges())

// generate new tree
newTree := parser.ParseWithTree(newText, tree)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type EditInput

type EditInput struct {
	StartIndex  uint32
	OldEndIndex uint32
	NewEndIndex uint32
	StartPoint  Point
	OldEndPoint Point
	NewEndPoint Point
}

type IterMode

type IterMode int
const (
	DFSMode IterMode = iota
	BFSMode
)

type Iterator

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

Iterator for a tree of nodes

func NewIterator

func NewIterator(n *Node, mode IterMode) *Iterator

NewIterator takes a node and mode (DFS/BFS) and returns iterator over children of the node

func NewNamedIterator

func NewNamedIterator(n *Node, mode IterMode) *Iterator

NewNamedIterator takes a node and mode (DFS/BFS) and returns iterator over named children of the node

func (*Iterator) ForEach

func (iter *Iterator) ForEach(fn func(*Node) error) error

func (*Iterator) Next

func (iter *Iterator) Next() (*Node, error)

type Language

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

Language defines how to parse a particular programming language

func NewLanguage

func NewLanguage(ptr unsafe.Pointer) *Language

NewLanguage creates new Language from c pointer

func (*Language) SymbolCount

func (l *Language) SymbolCount() uint32

SymbolCount returns the number of distinct field names in the language.

func (*Language) SymbolName

func (l *Language) SymbolName(s Symbol) string

SymbolName returns a node type string for the given Symbol.

func (*Language) SymbolType

func (l *Language) SymbolType(s Symbol) SymbolType

SymbolType returns named, anonymous, or a hidden type for a Symbol.

type Node

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

Node represents a single node in the syntax tree It tracks its start and end positions in the source code, as well as its relation to other nodes like its parent, siblings and children.

func Parse

func Parse(content []byte, lang *Language) *Node

Parse is a shortcut for parsing bytes of source code, returns root node. This should not be used outside quick prototypes or tests as it prevents closing the created Parser and Tree, it relies strictly on the Finalizer to free those resources.

func (*Node) Child

func (n *Node) Child(idx int) *Node

Child returns the node's child at the given index, where zero represents the first child.

func (*Node) ChildByFieldName

func (n *Node) ChildByFieldName(name string) *Node

ChildByFieldName returns the node's child with the given field name.

func (*Node) ChildCount

func (n *Node) ChildCount() uint32

ChildCount returns the node's number of children.

func (*Node) Content

func (n *Node) Content(input []byte) string

Content returns node's source code from input as a string

func (*Node) Edit

func (n *Node) Edit(i EditInput)

Edit the node to keep it in-sync with source code that has been edited.

func (*Node) EndByte

func (n *Node) EndByte() uint32

EndByte returns the node's end byte.

func (*Node) EndPoint

func (n *Node) EndPoint() Point

EndPoint returns the node's end position in terms of rows and columns.

func (*Node) Equal

func (n *Node) Equal(other *Node) bool

Equal checks if two nodes are identical.

func (*Node) HasChanges

func (n *Node) HasChanges() bool

HasChanges checks if a syntax node has been edited.

func (*Node) HasError

func (n *Node) HasError() bool

HasError check if the node is a syntax error or contains any syntax errors.

func (*Node) IsMissing

func (n *Node) IsMissing() bool

IsMissing checks if the node is *missing*. Missing nodes are inserted by the parser in order to recover from certain kinds of syntax errors.

func (*Node) IsNamed

func (n *Node) IsNamed() bool

IsNamed checks if the node is *named*. Named nodes correspond to named rules in the grammar, whereas *anonymous* nodes correspond to string literals in the grammar.

func (*Node) IsNull

func (n *Node) IsNull() bool

IsNull checks if the node is null.

func (*Node) NamedChild

func (n *Node) NamedChild(idx int) *Node

NamedChild returns the node's *named* child at the given index.

func (*Node) NamedChildCount

func (n *Node) NamedChildCount() uint32

NamedChildCount returns the node's number of *named* children.

func (*Node) NextNamedSibling

func (n *Node) NextNamedSibling() *Node

NextNamedSibling returns the node's next *named* sibling.

func (*Node) NextSibling

func (n *Node) NextSibling() *Node

NextSibling returns the node's next sibling.

func (*Node) Parent

func (n *Node) Parent() *Node

Parent returns the node's immediate parent.

func (*Node) PrevNamedSibling

func (n *Node) PrevNamedSibling() *Node

PrevNamedSibling returns the node's previous *named* sibling.

func (*Node) PrevSibling

func (n *Node) PrevSibling() *Node

PrevSibling returns the node's previous sibling.

func (*Node) StartByte

func (n *Node) StartByte() uint32

StartByte returns the node's start byte.

func (*Node) StartPoint

func (n *Node) StartPoint() Point

StartPoint returns the node's start position in terms of rows and columns.

func (*Node) String

func (n *Node) String() string

String returns an S-expression representing the node as a string.

func (*Node) Symbol

func (n *Node) Symbol() Symbol

Symbol returns the node's type as a Symbol.

func (*Node) Type

func (n *Node) Type() string

Type returns the node's type as a string.

type Parser

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

Parser produces concrete syntax tree based on source code using Language

func NewParser

func NewParser() *Parser

NewParser creates new Parser

func (*Parser) Close

func (p *Parser) Close()

Close releases the resources used by the parser. Once called, the parser shouldn't be used anymore. All trees generated by this Parser should be closed before closing the parser.

func (*Parser) Debug

func (p *Parser) Debug()

Debug enables debug output to stderr

func (*Parser) OperationLimit

func (p *Parser) OperationLimit() int

OperationLimit returns the duration in microseconds that parsing is allowed to take

func (*Parser) Parse

func (p *Parser) Parse(content []byte) *Tree

Parse produces new Tree from content

func (*Parser) ParseWithTree

func (p *Parser) ParseWithTree(content []byte, t *Tree) *Tree

ParseWithTree produces new Tree from content using old tree

func (*Parser) Reset

func (p *Parser) Reset()

Reset causes the parser to parse from scratch on the next call to parse, instead of resuming so that it sees the changes to the beginning of the source code.

func (*Parser) SetIncludedRanges

func (p *Parser) SetIncludedRanges(ranges []Range)

SetIncludedRanges sets text ranges of a file

func (*Parser) SetLanguage

func (p *Parser) SetLanguage(lang *Language)

SetLanguage assignes Language to a parser

func (*Parser) SetOperationLimit

func (p *Parser) SetOperationLimit(limit int)

SetOperationLimit limits the maximum duration in microseconds that parsing should be allowed to take before halting

type Point

type Point struct {
	Row    uint32
	Column uint32
}

type Query

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

Query API

func NewQuery

func NewQuery(pattern []byte, lang *Language) (*Query, error)

NewQuery creates a query by specifying a string containing one or more patterns. In case of error returns QueryError.

func (*Query) Close

func (q *Query) Close()

Close releases the resources used by the query. Once called, the query shouldn't be used anymore.

type QueryCapture

type QueryCapture struct {
	Index uint32
	Node  *Node
}

QueryCapture is a captured node by a query with an index

type QueryCursor

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

QueryCursor carries the state needed for processing the queries.

func NewQueryCursor

func NewQueryCursor() *QueryCursor

NewQueryCursor creates a query cursor.

func (*QueryCursor) Close

func (qc *QueryCursor) Close()

Close releases the resources used by the query cursor. Once called, the cursor shouldn't be used anymore.

func (*QueryCursor) Exec

func (qc *QueryCursor) Exec(q *Query, n *Node)

Exec executes the query on a given syntax node.

func (*QueryCursor) NextMatch

func (qc *QueryCursor) NextMatch() (*QueryMatch, bool)

NextMatch iterates over matches. This function will return (nil, false) when there are no more matches. Otherwise, it will populate the QueryMatch with data about which pattern matched and which nodes were captured.

type QueryError

type QueryError struct {
	Offset uint32
	Type   QueryErrorType
}

QueryError - if there is an error in the query, then the Offset argument will be set to the byte offset of the error, and the Type argument will be set to a value that indicates the type of error.

func (*QueryError) Error

func (qe *QueryError) Error() string

type QueryErrorType

type QueryErrorType int

QueryErrorType - value that indicates the type of QueryError.

const (
	QueryErrorNone QueryErrorType = iota
	QueryErrorSyntax
	QueryErrorNodeType
	QueryErrorField
	QueryErrorCapture
)

type QueryMatch

type QueryMatch struct {
	ID           uint32
	PatternIndex uint16
	Captures     []QueryCapture
}

QueryMatch - you can then iterate over the matches.

type Range

type Range struct {
	StartPoint Point
	EndPoint   Point
	StartByte  uint32
	EndByte    uint32
}

type Symbol

type Symbol = C.TSSymbol

type SymbolType

type SymbolType int
const (
	SymbolTypeRegular SymbolType = iota
	SymbolTypeAnonymous
	SymbolTypeAuxiliary
)

func (SymbolType) String

func (t SymbolType) String() string

type Tree

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

Tree represents the syntax tree of an entire source code file Note: Tree instances are not thread safe; you must copy a tree if you want to use it on multiple threads simultaneously.

func (*Tree) Close

func (t *Tree) Close()

Close releases the resources used by the tree. Once called, the tree shouldn't be used anymore.

func (*Tree) Copy

func (t *Tree) Copy() *Tree

Copy returns a new copy of a tree

func (*Tree) Edit

func (t *Tree) Edit(i EditInput)

Edit the syntax tree to keep it in sync with source code that has been edited.

func (*Tree) RootNode

func (t *Tree) RootNode() *Node

RootNode returns root node of a tree

type TreeCursor

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

TreeCursor allows you to walk a syntax tree more efficiently than is possible using the `Node` functions. It is a mutable object that is always on a certain syntax node, and can be moved imperatively to different nodes.

func NewTreeCursor

func NewTreeCursor(n *Node) *TreeCursor

NewTreeCursor creates a new tree cursor starting from the given node.

func (*TreeCursor) Close

func (c *TreeCursor) Close()

Close releases the resources used by the tree cursor. Once called, the cursor shouldn't be used anymore.

func (*TreeCursor) CurrentFieldName

func (c *TreeCursor) CurrentFieldName() string

CurrentFieldName gets the field name of the tree cursor's current node.

This returns empty string if the current node doesn't have a field.

func (*TreeCursor) CurrentNode

func (c *TreeCursor) CurrentNode() *Node

CurrentNode of the tree cursor.

func (*TreeCursor) GoToFirstChild

func (c *TreeCursor) GoToFirstChild() bool

GoToFirstChild moves the cursor to the first child of its current node.

This returns `true` if the cursor successfully moved, and returns `false` if there were no children.

func (*TreeCursor) GoToFirstChildForByte

func (c *TreeCursor) GoToFirstChildForByte(b uint32) int64

GoToFirstChildForByte moves the cursor to the first child of its current node that extends beyond the given byte offset.

This returns the index of the child node if one was found, and returns -1 if no such child was found.

func (*TreeCursor) GoToNextSibling

func (c *TreeCursor) GoToNextSibling() bool

GoToNextSibling moves the cursor to the next sibling of its current node.

This returns `true` if the cursor successfully moved, and returns `false` if there was no next sibling node.

func (*TreeCursor) GoToParent

func (c *TreeCursor) GoToParent() bool

GoToParent moves the cursor to the parent of its current node.

This returns `true` if the cursor successfully moved, and returns `false` if there was no parent node (the cursor was already on the root node).

func (*TreeCursor) Reset

func (c *TreeCursor) Reset(n *Node)

Reset re-initializes a tree cursor to start at a different node.

Directories

Path Synopsis
c
tsx

Jump to

Keyboard shortcuts

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