forked from jasonmf/wowlua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.go
280 lines (262 loc) · 6.37 KB
/
parse.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package wowlua
import (
"errors"
"fmt"
)
// Parser handles parsing tokens into Nodes
type Parser struct {
stack []*Node
}
// NewParser creates a new parser.
func NewParser() *Parser {
p := &Parser{
stack: make([]*Node, 0),
}
p.startTable() // Top level table
return p
}
// Push a node onto the current parse stack
func (p *Parser) Push(n *Node) {
p.stack = append(p.stack, n)
}
// Pop a node off the current parse stack. Returns nil if the stack is empty.
func (p *Parser) Pop() *Node {
sLen := len(p.stack)
if sLen == 0 {
return nil
}
n := p.stack[sLen-1]
p.stack = p.stack[0 : sLen-1]
return n
}
// Peek returns the node on top of the stack without removing it. It returns
// nil if the stack is empty.
func (p *Parser) Peek() *Node {
sLen := len(p.stack)
if sLen == 0 {
return nil
}
return p.stack[sLen-1]
}
func (p *Parser) startTable() {
t := NewTable()
n := &Node{nType: NodeTypeTable, value: t}
p.Push(n)
}
func (p *Parser) startKey() {
n := &Node{nType: NodeTypeTableEntry, value: &tableEntry{}}
p.Push(n)
}
func tokenToNode(t *Token) (*Node, error) {
var n *Node
switch t.Type {
case TokenTypeIdentifier:
switch t.Value {
case "true":
n = NewNode(NodeTypeBool, true)
case "false":
n = NewNode(NodeTypeBool, false)
default:
n = NewNode(NodeTypeString, t.Value)
}
case TokenTypeString:
n = NewNode(NodeTypeString, t.Value)
case TokenTypeNumber:
logger.Debugf("TokenToNode: %q", t)
n = NewNode(NodeTypeNumber, t.Value)
default:
return nil, fmt.Errorf("can't convert this token: %q", t)
}
return n, nil
}
// Next parses the next token
func (p *Parser) Next(t *Token) error {
logger.Debugf("Parsing Token: %v", t)
if t.Type == TokenTypeIgnore {
return nil
}
switch t.Type {
case TokenTypeIdentifier:
top := p.Peek()
switch top.nType {
case NodeTypeTableEntry:
n, err := tokenToNode(t)
if err != nil {
return err
}
p.Push(n)
default:
p.Next(tokenStartKey)
p.Next(NewToken(TokenTypeString, t.Value))
p.Next(tokenEndKey)
}
case TokenTypeEquals:
top := p.Peek()
if top.nType != NodeTypeTableEntry {
return p.bailout("Found equals with non table entry.")
}
if top.value == nil {
return p.bailout("Found equals with nil table entry.")
}
if _, ok := top.value.(*tableEntry); !ok {
return p.bailout("Key node contains non-table entry")
}
case TokenTypeStartTable:
p.startTable()
case TokenTypeEndTable:
top := p.Peek()
if top.nType != NodeTypeTable {
v := p.Pop()
top = p.Peek()
if top.nType == NodeTypeTableEntry {
if e, ok := top.value.(*tableEntry); ok {
p.Pop()
top = p.Peek()
if top.nType != NodeTypeTable {
return p.bailout("Key not in table, when closing the table!")
}
if table, ok := top.value.(*Table); ok {
table.Set(e.key, v)
} else {
return p.bailout("Expected table node on top of stack.")
}
} else {
return p.bailout("TableEntryValue not tableEntry!")
}
}
}
case TokenTypeStartKey:
if p.Peek().nType != NodeTypeTable {
return p.bailout(fmt.Sprintf("Found start of key under %q", p.Peek()))
}
p.startKey()
case TokenTypeEndKey:
key := p.Pop()
logger.Debugf("Popped Key: %v", key)
top := p.Peek()
if top.nType != NodeTypeTableEntry {
return p.bailout("Found end key without table entry.")
}
if top.value == nil {
return p.bailout("Found end key on nil entry.")
}
if e, ok := top.value.(*tableEntry); ok {
if e.key != nil {
return p.bailout("Found end key with key already set.")
}
e.key = key
} else {
return p.bailout("Found end key on non-table-entry")
}
case TokenTypeComma:
v := p.Pop()
top := p.Peek()
switch top.nType {
case NodeTypeTableEntry:
if e, ok := top.value.(*tableEntry); ok {
p.Pop()
top = p.Peek()
if top.nType != NodeTypeTable {
return p.bailout("Key not in table!")
}
if table, ok := top.value.(*Table); ok {
table.Set(e.key, v)
} else {
return p.bailout("Expected table node on top of stack.")
}
} else {
return p.bailout("TableEntryValue not tableEntry!")
}
case NodeTypeTable:
top.GetTable().AddIndexed(v)
default:
return p.bailout("Comma found outside table, table key")
}
case TokenTypeString:
return p.handleValueToken(t)
case TokenTypeNumber:
return p.handleValueToken(t)
default:
logger.Debugf("Unhandled token type: %q", t)
}
return nil
}
func (p *Parser) handleValueToken(t *Token) error {
top := p.Peek()
if top.nType != NodeTypeTable && top.nType != NodeTypeTableEntry {
return p.bailout(fmt.Sprintf("Found value %q outside of table/key!", t))
}
n, err := tokenToNode(t)
if err != nil {
return err
}
p.Push(n)
return nil
}
func (p *Parser) unwind() {
for n := p.Pop(); n != nil; n = p.Pop() {
logger.Debugf("Stack: %v", n)
}
}
func (p *Parser) dumpstack() {
for i := len(p.stack) - 1; i > -1; i-- {
logger.Debugf("Stack Dump: %v", p.stack[i])
}
}
func (p *Parser) bailout(msg string) error {
logger.Errorf("BAILING OUT!")
p.unwind()
return errors.New(msg)
}
// Finish parses all available tokens and returns the resulting table.
func (p *Parser) Finish() (*Table, error) {
for n := p.Pop(); n != nil; n = p.Pop() {
top := p.Peek()
switch n.nType {
case NodeTypeTableEntry:
if e, ok := n.value.(*tableEntry); ok {
if top.nType != NodeTypeTable {
return nil, p.bailout("Key not in table!")
}
if table, ok := top.value.(*Table); ok {
table.Set(e.key, e.value)
} else {
return nil, p.bailout("Expected table node on top of stack.")
}
} else {
logger.Debugf("Stack Popped: ", n)
return nil, p.bailout("TableEntryValue not tableEntry!")
}
default:
if top != nil {
switch top.nType {
case NodeTypeTableEntry:
if e, ok := top.value.(*tableEntry); ok {
e.value = n
} else {
return nil, p.bailout("Top is marked tableEntry but isn't a *tableEntry")
}
default:
return nil, p.bailout("SHIT BROKE SON")
}
} else {
logger.Debugf("--> TOP is NIL <--")
if t, ok := n.value.(*Table); ok {
return t, nil
}
}
}
}
err := errors.New("final result not a table")
logger.Errorf(err.Error())
return nil, err
}
// ParseLua handles end to end parsing of a string containing Lua table data
func ParseLua(data string) (*Table, error) {
p := NewParser()
t := NewTokenizer(data, p.Next)
if err := t.Tokenize(); err != nil {
return nil, err
}
return p.Finish()
}