elvish/pkg/parse/node.go

35 lines
908 B
Go
Raw Normal View History

2016-01-25 02:04:15 +08:00
package parse
import "src.elv.sh/pkg/diag"
2018-09-29 03:55:00 +08:00
2016-02-08 06:23:16 +08:00
// Node represents a parse tree as well as an AST.
2016-01-25 02:04:15 +08:00
type Node interface {
diag.Ranger
parse(*parser)
n() *node
2016-01-25 02:04:15 +08:00
}
type node struct {
diag.Ranging
2016-01-25 02:04:15 +08:00
sourceText string
parent Node
2016-01-25 02:04:15 +08:00
children []Node
}
func (n *node) n() *node { return n }
2018-10-14 00:08:15 +08:00
func (n *node) addChild(ch Node) { n.children = append(n.children, ch) }
// Range returns the range within the full source text that parses to the node.
func (n *node) Range() diag.Ranging { return n.Ranging }
2016-01-25 02:04:15 +08:00
// Parent returns the parent of a node. It returns nil if the node is the root
// of the parse tree.
func Parent(n Node) Node { return n.n().parent }
2018-10-13 21:03:58 +08:00
// SourceText returns the part of the source text that parses to the node.
func SourceText(n Node) string { return n.n().sourceText }
2016-01-25 02:04:15 +08:00
2018-10-13 21:03:58 +08:00
// Children returns all children of the node in the parse tree.
func Children(n Node) []Node { return n.n().children }