forked from prataprc/goparsec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
terminal.go
125 lines (105 loc) · 3.02 KB
/
terminal.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package parsec
// Terminal type can be used to construct a terminal ParsecNode.
// It implements Queryable interface, hence can be used with
// AST object.
type Terminal struct {
Name string // contains terminal's token type
Value string // value of the terminal
Position int // Offset into the text stream where token was identified
Attributes map[string][]string
}
// NewTerminal create a new Terminal instance. Supply the name of the
// terminal, its matching text from i/p stream as value. And its position
// within the i/p stream.
func NewTerminal(name, value string, position int) *Terminal {
t := &Terminal{
Name: name,
Value: value,
Position: position,
Attributes: make(map[string][]string),
}
t.SetAttribute("class", "term")
return t
}
// GetName implement Queryable interface.
func (t *Terminal) GetName() string {
return t.Name
}
// IsTerminal implement Queryable interface.
func (t *Terminal) IsTerminal() bool {
return true
}
// GetValue implement Queryable interface.
func (t *Terminal) GetValue() string {
return t.Value
}
// GetChildren implement Queryable interface.
func (t *Terminal) GetChildren() []Queryable {
return nil
}
// GetPosition implement Queryable interface.
func (t *Terminal) GetPosition() int {
return t.Position
}
// SetAttribute implement Queryable interface.
func (t *Terminal) SetAttribute(attrname, value string) Queryable {
if t.Attributes == nil {
t.Attributes = make(map[string][]string)
}
values, ok := t.Attributes[attrname]
if ok == false {
values = []string{}
}
values = append(values, value)
t.Attributes[attrname] = values
return t
}
// GetAttribute implement Queryable interface.
func (t *Terminal) GetAttribute(attrname string) []string {
if t.Attributes == nil {
return nil
} else if values, ok := t.Attributes[attrname]; ok {
return values
}
return nil
}
// GetAttributes implement Queryable interface.
func (t *Terminal) GetAttributes() map[string][]string {
return t.Attributes
}
// MaybeNone is a placeholder type, similar to Terminal type, used by
// Maybe combinator if parser does not match the input text.
type MaybeNone string
//---- implement Queryable interface
// GetName implement Queryable interface.
func (mn MaybeNone) GetName() string {
return string(mn)
}
// IsTerminal implement Queryable interface.
func (mn MaybeNone) IsTerminal() bool {
return true
}
// GetValue implement Queryable interface.
func (mn MaybeNone) GetValue() string {
return ""
}
// GetChildren implement Queryable interface.
func (mn MaybeNone) GetChildren() []Queryable {
return nil
}
// GetPosition implement Queryable interface.
func (mn MaybeNone) GetPosition() int {
return -1
}
// SetAttribute implement Queryable interface.
func (mn MaybeNone) SetAttribute(attrname, value string) Queryable {
return mn
}
// GetAttribute implement Queryable interface.
func (mn MaybeNone) GetAttribute(attrname string) []string {
return nil
}
// GetAttributes implement Queryable interface.
func (mn MaybeNone) GetAttributes() map[string][]string {
return nil
}