2018-01-05 19:06:10 +01:00
|
|
|
package treerack
|
|
|
|
|
|
|
|
|
|
import "fmt"
|
|
|
|
|
|
2026-01-18 22:52:27 +01:00
|
|
|
// 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.
|
2018-01-05 19:06:10 +01:00
|
|
|
type Node struct {
|
2026-01-18 22:52:27 +01:00
|
|
|
|
|
|
|
|
// 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
|
2018-01-05 19:06:10 +01:00
|
|
|
}
|
|
|
|
|
|
2026-01-18 22:52:27 +01:00
|
|
|
// 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.
|
2018-01-05 19:06:10 +01:00
|
|
|
func (n *Node) Tokens() []rune {
|
|
|
|
|
return n.tokens
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-18 22:52:27 +01:00
|
|
|
// String returns the string representation of the node, including its name, position range (From/To), and the
|
|
|
|
|
// captured text content.
|
2018-01-05 19:06:10 +01:00
|
|
|
func (n *Node) String() string {
|
|
|
|
|
return fmt.Sprintf("%s:%d:%d:%s", n.Name, n.From, n.To, n.Text())
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-18 22:52:27 +01:00
|
|
|
// Text returns the actual string segment from the input stream represented by this node.
|
2018-01-05 19:06:10 +01:00
|
|
|
func (n *Node) Text() string {
|
|
|
|
|
return string(n.Tokens()[n.From:n.To])
|
|
|
|
|
}
|