-
Notifications
You must be signed in to change notification settings - Fork 5
/
types.go
91 lines (76 loc) · 1.88 KB
/
types.go
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package rd
import "github.com/shivamMg/ppds/tree"
// Token represents a token received after tokenization.
type Token interface{}
// Tree is a parse tree node. Symbol can either be a terminal (Token) or a non-terminal
// (see Builder's Enter method). Tokens matched using Builder's Match method or added
// using Builder's Add method, can be retrieved by type asserting Symbol.
// Subtrees are child nodes of the current node.
type Tree struct {
Symbol interface{}
Subtrees []*Tree
}
func NewTree(symbol interface{}, subtrees ...*Tree) *Tree {
t := Tree{Symbol: symbol}
for _, subtree := range subtrees {
if subtree != nil {
t.Subtrees = append(t.Subtrees, subtree)
}
}
return &t
}
func (t *Tree) Data() interface{} {
if t == nil {
return ""
}
return t.Symbol
}
func (t *Tree) Children() (c []tree.Node) {
for _, subtree := range t.Subtrees {
c = append(c, subtree)
}
return
}
// Add adds a subtree as a child to t.
func (t *Tree) Add(subtree *Tree) {
t.Subtrees = append(t.Subtrees, subtree)
}
// Detach removes a subtree as a child of t.
func (t *Tree) Detach(subtree *Tree) {
for i, st := range t.Subtrees {
if st == subtree {
t.Subtrees = append(t.Subtrees[:i], t.Subtrees[i+1:]...)
break
}
}
}
func (t *Tree) String() string {
return tree.SprintHrn(t)
}
// DebugTree is a debug tree node. Can be printed to help tracing the
// parsing flow.
type DebugTree struct {
data string
subtrees []*DebugTree
}
func newDebugTree(data string) *DebugTree {
return &DebugTree{
data: data,
subtrees: []*DebugTree{},
}
}
func (dt *DebugTree) add(subtree *DebugTree) {
dt.subtrees = append(dt.subtrees, subtree)
}
func (dt *DebugTree) Data() interface{} {
return dt.data
}
func (dt *DebugTree) Children() (c []tree.Node) {
for _, child := range dt.subtrees {
c = append(c, child)
}
return
}
func (dt *DebugTree) String() string {
return tree.SprintHrn(dt)
}