Documentation
¶
Overview ¶
Package gositter is a Go port of the tree-sitter parser runtime v0.25.0 (ABI 15). This file is the frozen contract between the runtime, the grammar generator and the hand-written scanners. See CONTRACT.md. Changes go through scratch/CONTRACT_CHANGES.md.
Example ¶
package main
import (
"fmt"
"strings"
"github.com/dobbo-ca/gositter"
"github.com/dobbo-ca/gositter/grammars/python"
)
func main() {
src := []byte("def add(a, b):\n return a + b\n")
p := gositter.NewParser()
if err := p.SetLanguage(python.Language()); err != nil {
panic(err)
}
tree := p.Parse(src)
var walk func(n gositter.Node, depth int)
walk = func(n gositter.Node, depth int) {
fmt.Printf("%s%s [%d, %d)\n", strings.Repeat(" ", depth), n.Kind(), n.StartByte(), n.EndByte())
for _, c := range n.NamedChildren(nil) {
walk(c, depth+1)
}
}
walk(tree.RootNode(), 0)
}
Output: module [0, 32) function_definition [0, 31) identifier [4, 7) parameters [7, 13) identifier [8, 9) identifier [11, 12) block [19, 31) return_statement [19, 31) binary_operator [26, 31) identifier [26, 27) identifier [30, 31)
Index ¶
- Constants
- func SetContains(ranges []CharacterRange, lookahead int32) bool
- func SetContainsASCII(ascii *[128]bool, ranges []CharacterRange, lookahead int32) bool
- type CharacterRange
- type ExternalScanner
- type ExternalScannerInfo
- type FieldID
- type FieldMapEntry
- type Language
- type LanguageMetadata
- type LexFunc
- type LexMode
- type Lexer
- type LogType
- type Logger
- type MapSlice
- type Node
- func (n Node) Child(i uint32) Node
- func (n Node) ChildByFieldID(fieldID FieldID) Node
- func (n Node) ChildByFieldName(name string) Node
- func (n Node) ChildCount() uint32
- func (n Node) Children(buf []Node) []Node
- func (n Node) ChildrenByFieldName(name string, c *TreeCursor) []Node
- func (n Node) EndByte() uint32
- func (n Node) EndPosition() Point
- func (n Node) Equals(other Node) bool
- func (n Node) FieldNameForChild(childIndex uint32) string
- func (n Node) HasError() bool
- func (n Node) IsError() bool
- func (n Node) IsExtra() bool
- func (n Node) IsMissing() bool
- func (n Node) IsNamed() bool
- func (n Node) IsNull() bool
- func (n Node) Kind() string
- func (n Node) Language() *Language
- func (n Node) NamedChild(i uint32) Node
- func (n Node) NamedChildCount() uint32
- func (n Node) NamedChildren(buf []Node) []Node
- func (n Node) Parent() Node
- func (n Node) Preorder(fn func(Node) bool)
- func (n Node) PreorderMask(mask uint32, fn func(Node) bool)
- func (n Node) StartByte() uint32
- func (n Node) StartPosition() Point
- func (n Node) String() string
- func (n Node) Symbol() Symbol
- func (n Node) Utf8Text(src []byte) string
- func (n Node) Walk() *TreeCursor
- type ParseAction
- type Parser
- type Point
- type StateID
- type Symbol
- type SymbolMetadata
- type Tree
- type TreeCursor
- func (c *TreeCursor) Close()
- func (c *TreeCursor) FieldID() FieldID
- func (c *TreeCursor) FieldName() string
- func (c *TreeCursor) GotoFirstChild() bool
- func (c *TreeCursor) GotoNextSibling() bool
- func (c *TreeCursor) GotoParent() bool
- func (c *TreeCursor) Node() Node
- func (c *TreeCursor) Reset(n Node)
Examples ¶
Constants ¶
const ( SymbolEnd Symbol = 0 // ts_builtin_sym_end SymbolError Symbol = 0xFFFF // ts_builtin_sym_error SymbolErrorRepeat Symbol = 0xFFFE // ts_builtin_sym_error_repeat // TREE_SITTER_SERIALIZATION_BUFFER_SIZE SerializationBufferSize = 1024 LanguageVersion = 15 // TREE_SITTER_LANGUAGE_VERSION MinCompatibleLanguageVersion = 13 // TREE_SITTER_MIN_COMPATIBLE_LANGUAGE_VERSION LanguageVersionWithReservedWords = 15 LanguageVersionWithPrimaryStates = 14 )
const ( ParseActionTypeShift uint8 = 0 ParseActionTypeReduce uint8 = 1 ParseActionTypeAccept uint8 = 2 ParseActionTypeRecover uint8 = 3 )
TSParseActionType.
Variables ¶
This section is empty.
Functions ¶
func SetContainsASCII ¶
func SetContainsASCII(ascii *[128]bool, ranges []CharacterRange, lookahead int32) bool
SetContainsASCII is set_contains with the answers for code points below 128 precomputed by the generator: ascii[c] is SetContains(ranges, c).
Types ¶
type CharacterRange ¶
TSCharacterRange, inclusive on both ends.
type ExternalScanner ¶
type ExternalScanner interface {
// scan. validSymbols has ExternalTokenCount elements and is read-only.
// On success the scanner sets l.ResultSymbol to the external token index.
Scan(l *Lexer, validSymbols []bool) bool
// serialize. len(buf) == SerializationBufferSize. Returns bytes written.
Serialize(buf []byte) int
// deserialize. len(buf) == 0 means reset to the initial state. buf is
// only valid during the call.
Deserialize(buf []byte)
}
ExternalScanner is one instance of a grammar's external scanner. The runtime creates it where C calls create and drops it where C calls destroy.
type ExternalScannerInfo ¶
type ExternalScannerInfo struct {
States []bool // [external state][ExternalTokenCount], row 0 unused
SymbolMap []Symbol // external token index -> symbol
New func() ExternalScanner
}
TSLanguage.external_scanner. destroy has no Go counterpart.
type FieldMapEntry ¶
TSFieldMapEntry (4 bytes).
type Language ¶
type Language struct {
ABIVersion uint32
SymbolCount uint32
AliasCount uint32
TokenCount uint32
ExternalTokenCount uint32
StateCount uint32
LargeStateCount uint32
ProductionIDCount uint32
FieldCount uint32
MaxAliasSequenceLength uint16
ParseTable []uint16 // [LargeStateCount][SymbolCount]
SmallParseTable []uint16
SmallParseTableMap []uint32
ParseActions []ParseAction
SymbolNames []string // SymbolCount + AliasCount entries
FieldNames []string // FieldCount + 1 entries, [0] == ""
FieldMapSlices []MapSlice
FieldMapEntries []FieldMapEntry
SymbolMetadata []SymbolMetadata
PublicSymbolMap []Symbol
AliasMap []uint16 // zero-terminated, as in C
AliasSequences []Symbol // [ProductionIDCount][MaxAliasSequenceLength]
LexModes []LexMode
LexFn LexFunc
KeywordLexFn LexFunc // nil if the grammar has no word token
KeywordCaptureToken Symbol
ExternalScanner ExternalScannerInfo
PrimaryStateIDs []StateID
Name string
ReservedWords []Symbol // [set][MaxReservedWordSetSize]
MaxReservedWordSetSize uint16
SupertypeCount uint32
SupertypeSymbols []Symbol
SupertypeMapSlices []MapSlice
SupertypeMapEntries []Symbol
Metadata LanguageMetadata
// contains filtered or unexported fields
}
Language mirrors TSLanguage (ABI 15) field for field. C pointers become slices; two-dimensional C arrays are flattened row-major. A Language is shared by all parsers and, KindMask before the first parse aside, immutable after construction.
func (*Language) FieldIDForName ¶
FieldIDForName is ts_language_field_id_for_name. The scan's answer for each field name is kept; any other name is scanned.
func (*Language) FieldNameForID ¶
ts_language_field_name_for_id. Returns "" for 0 or out of range.
func (*Language) KindMask ¶
KindMask returns the bits that stand for the named node kinds in Node.PreorderMask, giving each new name the next of 32 bits. A tree knows the kinds named before it was parsed, and the language is not locked: name every kind before the first Parse. No C counterpart.
func (*Language) SymbolName ¶
ts_language_symbol_name
type LanguageMetadata ¶
TSLanguageMetadata.
type LexMode ¶
TSLexerMode. ABI 14 grammars (TSLexMode) leave ReservedWordSetID zero. LexState 0xFFFF is the C (uint16_t)-1 marker and must be kept.
type Lexer ¶
type Lexer struct {
// Current code point: 0 at end of input, -1 for invalid UTF-8.
Lookahead int32
// Set by lex functions (a symbol) and scanners (an external token index).
ResultSymbol Symbol
// contains filtered or unexported fields
}
Lexer is the C Lexer with its embedded TSLexer. Generated lex functions and scanners use only the two exported fields and the exported methods.
func (*Lexer) Advance ¶
ts_lexer__advance, with ts_lexer__do_advance folded in: the lex functions call this once per character, and one call is cheaper than two.
func (*Lexer) AdvanceRun ¶
AdvanceRun is Advance(skip) repeated for as long as Lookahead is an ASCII character in set. It has no C counterpart: generated lex functions call it in states that loop on themselves (cmd/gengrammar/run.go). set[0] and set[128:] are false.
func (*Lexer) IsAtIncludedRangeStart ¶
ts_lexer__is_at_included_range_start
type Node ¶
type Node struct {
// contains filtered or unexported fields
}
TSNode, packed into four fields so the compiler keeps it in registers. A small comparable value; the zero Node is the null node. id is the slot of the subtree in the tree's children slab, where C has a const Subtree *.
func (Node) ChildByFieldID ¶
ts_node_child_by_field_id
func (Node) ChildByFieldName ¶
ts_node_child_by_field_name
func (Node) Children ¶
Children appends the children of n to buf: the nodes Child(0) to Child(ChildCount()-1), found in one pass. ts_node_child rescans from the first child on every call, so a Child(i) loop is quadratic. No C counterpart.
func (Node) ChildrenByFieldName ¶
func (n Node) ChildrenByFieldName(name string, c *TreeCursor) []Node
Same loop as go-tree-sitter's Node.ChildrenByFieldName.
func (Node) FieldNameForChild ¶
ts_node_field_name_for_child. Returns "" where C returns NULL.
func (Node) NamedChildren ¶
NamedChildren is Children for NamedChild.
func (Node) Preorder ¶
Preorder calls fn for n and, where fn returns true, for the nodes below it in document order: the walk a recursive Children loop makes, with no slice per node. No C counterpart.
func (Node) PreorderMask ¶
PreorderMask is Preorder for an fn that returns true and does nothing else on a node whose kind is not in mask (see Language.KindMask): fn is called for the nodes of those kinds only, and a subtree that holds none of them is not entered. No C counterpart.
type ParseAction ¶
type ParseAction struct {
Type uint8
ChildCount uint8
Symbol Symbol
DynamicPrecedence int16
ProductionID uint16
}
ParseAction is one 8-byte, pointer-free slot of Language.ParseActions. It stands for both C unions, TSParseActionEntry and TSParseAction, and keeps the C byte layout. Which variant a slot holds follows from its position: a header slot is followed by Count() action slots.
slot Type ChildCount Symbol DynamicPrecedence ProductionID header count reusable 0 0 0 shift 0 0 state bit0 extra, bit1 rep. 0 reduce 1 child_count symbol dynamic_precedence production_id accept 2 0 0 0 0 recover 3 0 0 0 0
The fields are named for the reduce variant; use the methods for the rest.
type Parser ¶
type Parser struct {
// contains filtered or unexported fields
}
TSParser. Not safe for concurrent use; one per goroutine.
func (*Parser) Parse ¶
ts_parser_parse with old_tree NULL and UTF-8 input. Returns nil if no language is set. The tree is valid until the next Parse, Reset or SetLanguage on p.
func (*Parser) ParseBudget ¶
ParseBudget is Parse, but returns nil once advance has been called more than maxStall times in a row without passing the furthest byte reached. 0 is no limit. The count depends only on the language and src.
func (*Parser) Reset ¶
func (p *Parser) Reset()
ts_parser_reset. C releases the stack, the token cache and the finished tree one reference at a time. Here everything lives in the parser's slabs and nothing outlives the reset, so they are truncated instead. The tree of the last Parse dies with them.
func (*Parser) SetLanguage ¶
ts_parser_set_language. Errors on an incompatible ABIVersion.
type SymbolMetadata ¶
TSSymbolMetadata.
type Tree ¶
type Tree struct {
// contains filtered or unexported fields
}
TSTree. It borrows the slabs of the parser that made it.
type TreeCursor ¶
type TreeCursor struct {
// contains filtered or unexported fields
}
TSTreeCursor.
func (*TreeCursor) FieldName ¶
func (c *TreeCursor) FieldName() string
ts_tree_cursor_current_field_name
func (*TreeCursor) GotoFirstChild ¶
func (c *TreeCursor) GotoFirstChild() bool
ts_tree_cursor_goto_first_child, with the internal variant and the child_count guard of ts_tree_cursor_iterate_children folded in.
func (*TreeCursor) GotoNextSibling ¶
func (c *TreeCursor) GotoNextSibling() bool
ts_tree_cursor_goto_next_sibling
func (*TreeCursor) Node ¶
func (c *TreeCursor) Node() Node
ts_tree_cursor_current_node, alias read from the entry.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
gengrammar
command
Command gengrammar turns a tree-sitter parser.c into a Go package for gositter: tables.go, lex.go and language.go.
|
Command gengrammar turns a tree-sitter parser.c into a Go package for gositter: tables.go, lex.go and language.go. |
|
grammars
|
|
|
internal/cchar
Package cchar gives scanners the plain char of the C compiler that the matching cgo build uses, so a port can follow C where a scanner stores a code point in a char and reads it back.
|
Package cchar gives scanners the plain char of the C compiler that the matching cgo build uses, so a port can follow C where a scanner stores a code point in a char and reads it back. |
|
internal
|
|