forked from prataprc/goparsec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nonterminal.go
82 lines (72 loc) · 2.06 KB
/
nonterminal.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
package parsec
// NonTerminal will be used by AST methods to construct intermediate nodes.
// Note that user supplied ASTNodify callback can construct a different
// type of intermediate node that confirms to Queryable interface.
type NonTerminal struct {
Name string // contains terminal's token type
Children []Queryable // list of children to this node.
Attributes map[string][]string
}
// NewNonTerminal create and return a new NonTerminal instance.
func NewNonTerminal(name string) *NonTerminal {
nt := &NonTerminal{
Name: name,
Children: make([]Queryable, 0),
Attributes: make(map[string][]string),
}
nt.SetAttribute("class", "nonterm")
return nt
}
// GetName implement Queryable interface.
func (nt *NonTerminal) GetName() string {
return nt.Name
}
// IsTerminal implement Queryable interface.
func (nt *NonTerminal) IsTerminal() bool {
return false
}
// GetValue implement Queryable interface.
func (nt *NonTerminal) GetValue() string {
value := ""
for _, c := range nt.Children {
value += c.GetValue()
}
return value
}
// GetChildren implement Queryable interface.
func (nt *NonTerminal) GetChildren() []Queryable {
return nt.Children
}
// GetPosition implement Queryable interface.
func (nt *NonTerminal) GetPosition() int {
if nodes := nt.GetChildren(); len(nodes) > 0 {
return nodes[0].GetPosition()
}
return 0
}
// SetAttribute implement Queryable interface.
func (nt *NonTerminal) SetAttribute(attrname, value string) Queryable {
if nt.Attributes == nil {
nt.Attributes = make(map[string][]string)
}
values, ok := nt.Attributes[attrname]
if ok == false {
values = []string{}
}
values = append(values, value)
nt.Attributes[attrname] = values
return nt
}
// GetAttribute implement Queryable interface.
func (nt *NonTerminal) GetAttribute(attrname string) []string {
if nt.Attributes == nil {
return nil
} else if values, ok := nt.Attributes[attrname]; ok {
return values
}
return nil
}
// GetAttributes implement Queryable interface.
func (nt *NonTerminal) GetAttributes() map[string][]string {
return nt.Attributes
}