1
0
treerack/nodehead.go
2026-01-18 22:52:27 +01:00

41 lines
1.2 KiB
Go

package treerack
import "fmt"
// Node represents a distinct element in the resulting Abstract Syntax Tree (AST) following a successful parse.
// Every named parser that is not an Alias or Whitespace yields a Node.
type Node struct {
// Name is the identifier of the parser that generated this node.
Name string
// Nodes contains the child nodes representing the substructures of this node.
Nodes []*Node
// From is the inclusive character offset of the starting position in the input stream.
From int
// To is the exclusive character offset of the ending position in the input stream.
To int
tokens []rune
}
// Tokens returns the raw slice of runes from the input stream represented by this node.
//
// Note: This returns a reference to the underlying buffer, not a copy. It should not be modified.
func (n *Node) Tokens() []rune {
return n.tokens
}
// String returns the string representation of the node, including its name, position range (From/To), and the
// captured text content.
func (n *Node) String() string {
return fmt.Sprintf("%s:%d:%d:%s", n.Name, n.From, n.To, n.Text())
}
// Text returns the actual string segment from the input stream represented by this node.
func (n *Node) Text() string {
return string(n.Tokens()[n.From:n.To])
}