-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction.go
More file actions
52 lines (44 loc) · 1.02 KB
/
action.go
File metadata and controls
52 lines (44 loc) · 1.02 KB
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
package gojson
func action(stack []*stackElement) (*jsonElement, int) {
stackSize := len(stack)
var je *jsonElement
var offset int
for _, rule := range grammar {
for _, production := range rule.rhs {
size := len(production)
if size > stackSize {
continue
}
actual := topNOfStack(stack, size)
matches := compare(production, actual)
if matches && size > offset {
je = &jsonElement{
value: rule.toJson(stack[len(stack)-size:]...),
jsonElementType: rule.lhs,
}
offset = size
}
}
}
return je, offset
}
func topNOfStack(stack []*stackElement, count int) []elementType {
slice := stack[len(stack)-count:]
var elements []elementType
for _, el := range slice {
var value = el.value.tokenType
if el.rule != nil {
value = el.rule.jsonElementType
}
elements = append(elements, value)
}
return elements
}
func compare(expansion, actual []elementType) bool {
for i := 0; i < len(expansion); i++ {
if expansion[i] != actual[i] {
return false
}
}
return true
}