elvish/pkg/parse/node.go

59 lines
1.2 KiB
Go
Raw Normal View History

2016-01-25 02:04:15 +08:00
package parse
2019-12-24 04:00:59 +08:00
import "github.com/elves/elvish/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 {
parse(*parser)
2018-10-14 00:08:15 +08:00
diag.Ranger
2016-01-25 02:04:15 +08:00
SourceText() string
2018-10-14 00:08:15 +08:00
Parent() Node
2016-01-25 02:04:15 +08:00
Children() []Node
2018-10-14 00:08:15 +08:00
setFrom(int)
setTo(int)
setSourceText(string)
2018-10-14 00:08:15 +08:00
setParent(Node)
addChild(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
}
2018-10-14 00:08:15 +08:00
func (n *node) setFrom(begin int) { n.From = begin }
2016-01-25 02:04:15 +08:00
2018-10-14 00:08:15 +08:00
func (n *node) setTo(end int) { n.To = end }
2018-10-14 00:08:15 +08:00
func (n *node) setSourceText(source string) { n.sourceText = source }
2018-10-14 00:08:15 +08:00
func (n *node) setParent(p Node) { n.parent = p }
func (n *node) addChild(ch Node) { n.children = append(n.children, ch) }
2018-10-13 21:03:58 +08:00
// Parent returns the parent node. If the node is the root of the syntax tree,
// the parent is nil.
2016-01-25 02:04:15 +08:00
func (n *node) Parent() Node {
return n.parent
}
// Range returns the range within the original (full) source text that parses
2018-10-13 21:03:58 +08:00
// to the node.
func (n *node) Range() diag.Ranging {
return diag.Ranging{n.From, n.To}
}
2018-10-13 21:03:58 +08:00
// SourceText returns the part of the source text that parses to the node.
2016-01-25 02:04:15 +08:00
func (n *node) SourceText() string {
return n.sourceText
}
2018-10-13 21:03:58 +08:00
// Children returns all children of the node in the parse tree.
2016-01-25 02:04:15 +08:00
func (n *node) Children() []Node {
return n.children
}