File: node.go

package info (click to toggle)
elvish 0.21.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,372 kB
  • sloc: javascript: 236; sh: 130; python: 104; makefile: 88; xml: 9
file content (34 lines) | stat: -rw-r--r-- 908 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package parse

import "src.elv.sh/pkg/diag"

// Node represents a parse tree as well as an AST.
type Node interface {
	diag.Ranger
	parse(*parser)
	n() *node
}

type node struct {
	diag.Ranging
	sourceText string
	parent     Node
	children   []Node
}

func (n *node) n() *node { return n }

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 }

// 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 }

// SourceText returns the part of the source text that parses to the node.
func SourceText(n Node) string { return n.n().sourceText }

// Children returns all children of the node in the parse tree.
func Children(n Node) []Node { return n.n().children }