-
Notifications
You must be signed in to change notification settings - Fork 4
/
helper.go
449 lines (356 loc) · 9.98 KB
/
helper.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/*
* ECAL
*
* Copyright 2020 Matthias Ladkau. All rights reserved.
*
* This Source Code Form is subject to the terms of the MIT
* License, If a copy of the MIT License was not distributed with this
* file, You can obtain one at https://opensource.org/licenses/MIT.
*/
package parser
import (
"bytes"
"fmt"
"strconv"
"github.com/krotik/common/datautil"
"github.com/krotik/common/stringutil"
)
// AST Nodes
// =========
/*
MetaData is auxiliary data which can be attached to ASTs.
*/
type MetaData interface {
/*
Type returns the type of the meta data.
*/
Type() string
/*
Value returns the value of the meta data.
*/
Value() string
}
/*
metaData is a minimal MetaData implementation.
*/
type metaData struct {
metatype string
metavalue string
}
/*
Type returns the type of the meta data.
*/
func (m *metaData) Type() string {
return m.metatype
}
/*
Value returns the value of the meta data.
*/
func (m *metaData) Value() string {
return m.metavalue
}
/*
ASTNode models a node in the AST
*/
type ASTNode struct {
Name string // Name of the node
Token *LexToken // Lexer token of this ASTNode
Meta []MetaData // Meta data for this ASTNode (e.g. comments)
Children []*ASTNode // Child nodes
Runtime Runtime // Runtime component for this ASTNode
binding int // Binding power of this node
nullDenotation func(p *parser, self *ASTNode) (*ASTNode, error) // Configure token as beginning node
leftDenotation func(p *parser, self *ASTNode, left *ASTNode) (*ASTNode, error) // Configure token as left node
}
/*
Create a new instance of this ASTNode which is connected to a concrete lexer token.
*/
func (n *ASTNode) instance(p *parser, t *LexToken) *ASTNode {
ret := &ASTNode{n.Name, t, nil, make([]*ASTNode, 0, 2), nil, n.binding, n.nullDenotation, n.leftDenotation}
if p.rp != nil {
ret.Runtime = p.rp.Runtime(ret)
}
return ret
}
/*
Equals checks if this AST data equals another AST data. Returns also a message describing
what is the found difference.
*/
func (n *ASTNode) Equals(other *ASTNode, ignoreTokenPosition bool) (bool, string) {
return n.equalsPath(n.Name, other, ignoreTokenPosition)
}
/*
equalsPath checks if this AST data equals another AST data while preserving the search path.
Returns also a message describing what is the found difference.
*/
func (n *ASTNode) equalsPath(path string, other *ASTNode, ignoreTokenPosition bool) (bool, string) {
var res = true
var msg = ""
if n.Name != other.Name {
res = false
msg = fmt.Sprintf("Name is different %v vs %v\n", n.Name, other.Name)
}
if n.Token != nil && other.Token != nil {
if ok, tokenMSG := n.Token.Equals(*other.Token, ignoreTokenPosition); !ok {
res = false
msg += fmt.Sprintf("Token is different:\n%v\n", tokenMSG)
}
}
if len(n.Meta) != len(other.Meta) {
res = false
msg = fmt.Sprintf("Number of meta data entries is different %v vs %v\n",
len(n.Meta), len(other.Meta))
} else {
for i, meta := range n.Meta {
// Check for different in meta entries
if meta.Type() != other.Meta[i].Type() {
res = false
msg += fmt.Sprintf("Meta data type is different %v vs %v\n", meta.Type(), other.Meta[i].Type())
} else if meta.Value() != other.Meta[i].Value() {
res = false
msg += fmt.Sprintf("Meta data value is different %v vs %v\n", meta.Value(), other.Meta[i].Value())
}
}
}
if len(n.Children) != len(other.Children) {
res = false
msg = fmt.Sprintf("Number of children is different %v vs %v\n",
len(n.Children), len(other.Children))
} else {
for i, child := range n.Children {
// Check for different in children
if ok, childMSG := child.equalsPath(fmt.Sprintf("%v > %v", path, child.Name),
other.Children[i], ignoreTokenPosition); !ok {
return ok, childMSG
}
}
}
if msg != "" {
var buf bytes.Buffer
buf.WriteString("AST Nodes:\n")
n.levelString(0, &buf, 1)
buf.WriteString("vs\n")
other.levelString(0, &buf, 1)
msg = fmt.Sprintf("Path to difference: %v\n\n%v\n%v", path, msg, buf.String())
}
return res, msg
}
/*
String returns a string representation of this token.
*/
func (n *ASTNode) String() string {
var buf bytes.Buffer
n.levelString(0, &buf, -1)
return buf.String()
}
/*
levelString function to recursively print the tree.
*/
func (n *ASTNode) levelString(indent int, buf *bytes.Buffer, printChildren int) {
// Print current level
buf.WriteString(stringutil.GenerateRollingString(" ", indent*2))
if n.Name == NodeSTRING {
buf.WriteString(fmt.Sprintf("%v: '%v'", n.Name, n.Token.Val))
} else if n.Name == NodeNUMBER {
buf.WriteString(fmt.Sprintf("%v: %v", n.Name, n.Token.Val))
} else if n.Name == NodeIDENTIFIER {
buf.WriteString(fmt.Sprintf("%v: %v", n.Name, n.Token.Val))
} else {
buf.WriteString(n.Name)
}
if len(n.Meta) > 0 {
buf.WriteString(" # ")
for i, c := range n.Meta {
buf.WriteString(c.Value())
if i < len(n.Meta)-1 {
buf.WriteString(" ")
}
}
}
buf.WriteString("\n")
if printChildren == -1 || printChildren > 0 {
if printChildren != -1 {
printChildren--
}
// Print children
for _, child := range n.Children {
child.levelString(indent+1, buf, printChildren)
}
}
}
/*
ToJSONObject returns this ASTNode and all its children as a JSON object.
*/
func (n *ASTNode) ToJSONObject() map[string]interface{} {
ret := make(map[string]interface{})
ret["name"] = n.Name
lenMeta := len(n.Meta)
if lenMeta > 0 {
meta := make([]map[string]interface{}, lenMeta)
for i, metaChild := range n.Meta {
meta[i] = map[string]interface{}{
"type": metaChild.Type(),
"value": metaChild.Value(),
}
}
ret["meta"] = meta
}
lenChildren := len(n.Children)
if lenChildren > 0 {
children := make([]map[string]interface{}, lenChildren)
for i, child := range n.Children {
children[i] = child.ToJSONObject()
}
ret["children"] = children
}
// The value is what the lexer found in the source
if n.Token != nil {
ret["id"] = n.Token.ID
if n.Token.Val != "" {
ret["value"] = n.Token.Val
}
ret["identifier"] = n.Token.Identifier
ret["allowescapes"] = n.Token.AllowEscapes
ret["pos"] = n.Token.Pos
ret["source"] = n.Token.Lsource
ret["line"] = n.Token.Lline
ret["linepos"] = n.Token.Lpos
}
return ret
}
/*
ASTFromJSONObject creates an AST from a JSON Object.
The following nested map structure is expected:
{
name : <name of node>
// Optional node information
value : <value of node>
children : [ <child nodes> ]
// Optional token information
id : <token id>
}
*/
func ASTFromJSONObject(jsonAST map[string]interface{}) (*ASTNode, error) {
var astMeta []MetaData
var astChildren []*ASTNode
nodeID := TokenANY
name, ok := jsonAST["name"]
if !ok {
return nil, fmt.Errorf("Found json ast node without a name: %v", jsonAST)
}
if nodeIDString, ok := jsonAST["id"]; ok {
if nodeIDInt, err := strconv.Atoi(fmt.Sprint(nodeIDString)); err == nil && IsValidTokenID(nodeIDInt) {
nodeID = LexTokenID(nodeIDInt)
}
}
getVal := func(k string, d interface{}) (interface{}, int) {
value, ok := jsonAST[k]
if !ok {
value = d
}
numVal, _ := strconv.Atoi(fmt.Sprint(value))
return value, numVal
}
value, _ := getVal("value", "")
identifier, _ := getVal("identifier", false)
allowescapes, _ := getVal("allowescapes", false)
_, prefixnl := getVal("prefixnewlines", "")
_, pos := getVal("pos", "")
_, line := getVal("line", "")
_, linepos := getVal("linepos", "")
source, _ := getVal("source", "")
// Create meta data
if meta, ok := jsonAST["meta"]; ok {
if ic, ok := meta.([]interface{}); ok {
// Do a list conversion if necessary - this is necessary when we parse
// JSON with map[string]interface{}
metaList := make([]map[string]interface{}, len(ic))
for i := range ic {
metaList[i] = ic[i].(map[string]interface{})
}
meta = metaList
}
for _, metaChild := range meta.([]map[string]interface{}) {
astMeta = append(astMeta, &metaData{
fmt.Sprint(metaChild["type"]), fmt.Sprint(metaChild["value"])})
}
}
// Create children
if children, ok := jsonAST["children"]; ok {
if ic, ok := children.([]interface{}); ok {
// Do a list conversion if necessary - this is necessary when we parse
// JSON with map[string]interface{}
childrenList := make([]map[string]interface{}, len(ic))
for i := range ic {
childrenList[i] = ic[i].(map[string]interface{})
}
children = childrenList
}
for _, child := range children.([]map[string]interface{}) {
astChild, err := ASTFromJSONObject(child)
if err != nil {
return nil, err
}
astChildren = append(astChildren, astChild)
}
}
token := &LexToken{
nodeID, // ID
pos, // Pos
fmt.Sprint(value), // Val
identifier == true, // Identifier
allowescapes == true, // AllowEscapes
prefixnl, // PrefixNewlines
fmt.Sprint(source), // Lsource
line, // Lline
linepos, // Lpos
}
return &ASTNode{fmt.Sprint(name), token, astMeta, astChildren, nil, 0, nil, nil}, nil
}
// Look ahead buffer
// =================
/*
LABuffer models a look-ahead buffer.
*/
type LABuffer struct {
tokens chan LexToken
buffer *datautil.RingBuffer
}
/*
NewLABuffer creates a new NewLABuffer instance.
*/
func NewLABuffer(c chan LexToken, size int) *LABuffer {
if size < 1 {
size = 1
}
ret := &LABuffer{c, datautil.NewRingBuffer(size)}
v, more := <-ret.tokens
ret.buffer.Add(v)
for ret.buffer.Size() < size && more && v.ID != TokenEOF {
v, more = <-ret.tokens
ret.buffer.Add(v)
}
return ret
}
/*
Next returns the next item.
*/
func (b *LABuffer) Next() (LexToken, bool) {
ret := b.buffer.Poll()
if v, more := <-b.tokens; more {
b.buffer.Add(v)
}
if ret == nil {
return LexToken{ID: TokenEOF}, false
}
return ret.(LexToken), true
}
/*
Peek looks inside the buffer starting with 0 as the next item.
*/
func (b *LABuffer) Peek(pos int) (LexToken, bool) {
if pos >= b.buffer.Size() {
return LexToken{ID: TokenEOF}, false
}
return b.buffer.Get(pos).(LexToken), true
}