github.com/markusbkk/elvish@v0.0.0-20231204143114-91dc52438621/pkg/parse/node.go (about) 1 package parse 2 3 import "github.com/markusbkk/elvish/pkg/diag" 4 5 // Node represents a parse tree as well as an AST. 6 type Node interface { 7 diag.Ranger 8 parse(*parser) 9 n() *node 10 } 11 12 type node struct { 13 diag.Ranging 14 sourceText string 15 parent Node 16 children []Node 17 } 18 19 func (n *node) n() *node { return n } 20 21 func (n *node) addChild(ch Node) { n.children = append(n.children, ch) } 22 23 // Range returns the range within the full source text that parses to the node. 24 func (n *node) Range() diag.Ranging { return n.Ranging } 25 26 // Parent returns the parent of a node. It returns nil if the node is the root 27 // of the parse tree. 28 func Parent(n Node) Node { return n.n().parent } 29 30 // SourceText returns the part of the source text that parses to the node. 31 func SourceText(n Node) string { return n.n().sourceText } 32 33 // Children returns all children of the node in the parse tree. 34 func Children(n Node) []Node { return n.n().children }