-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
192 lines (138 loc) · 3.44 KB
/
parser.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
package main
import (
"strconv"
)
func parseNumber(tokens []Token, i int) (node *Node, tokensConsumed int) {
if tokens[i].Type == TokenTypeNumber {
value, e := strconv.ParseFloat(tokens[i].Value, 64)
if e == nil {
var node Node = NumberNode{value}
return &node, 1
}
}
return
}
func parseBoolean(tokens []Token, i int) (node *Node, tokensConsumed int) {
if tokens[i].Type == TokenTypeBoolean {
if tokens[i].Value == "true" {
var node Node = BooleanNode{true}
return &node, 1
}
if tokens[i].Value == "false" {
var node Node = BooleanNode{false}
return &node, 1
}
}
return
}
func parseString(tokens []Token, i int) (node *Node, tokensConsumed int) {
if tokens[i].Type == TokenTypeString {
value := tokens[i].Value
// Remove the leading and trailing quotes
var node Node = StringNode{value[1 : len(value)-1]}
return &node, 1
}
return
}
func parseArray(tokens []Token, i int) (node *Node, tokensConsumed int) {
if tokens[i].Type != TokenTypeOpenSquareBracket {
return
}
arrayNode := ArrayNode{[]*Node{}}
for j := i + 1; j < len(tokens); {
if tokens[j].Type == TokenTypeCloseSquareBracket {
var nodeToReturn Node = arrayNode
return &nodeToReturn, j - i + 1
}
if j != i+1 {
if tokens[j].Type == TokenTypeComma {
j++
} else {
return
}
}
elementNode, elementTokensConsumed := parseAny(tokens, j)
if elementNode == nil || elementTokensConsumed == 0 {
return
}
arrayNode.Elements = append(arrayNode.Elements, elementNode)
j += elementTokensConsumed
}
return
}
// Properties are only allowed to be direct children of objects
func parseProperty(tokens []Token, i int) (node *PropertyNode, tokensConsumed int) {
j := i
keyNode, keyTokensConsumed := parseString(tokens, j)
if keyNode == nil || keyTokensConsumed == 0 {
return
}
key := (*keyNode).(StringNode).Value
j += keyTokensConsumed
if j >= len(tokens) || tokens[j].Type != TokenTypeColon {
return
}
j++
valueNode, valueTokensConsumed := parseAny(tokens, j)
j += valueTokensConsumed
return &PropertyNode{key, valueNode}, j - i
}
func parseObject(tokens []Token, i int) (node *Node, tokensConsumed int) {
if tokens[i].Type != TokenTypeOpenCurlyBrace {
return
}
objectNode := ObjectNode{[]*PropertyNode{}}
for j := i + 1; j < len(tokens); {
if tokens[j].Type == TokenTypeCloseCurlyBrace {
var nodeToReturn Node = objectNode
return &nodeToReturn, j - i + 1
}
if j != i+1 {
if tokens[j].Type == TokenTypeComma {
j++
} else {
return
}
}
propertyNode, propertyTokensConsumed := parseProperty(tokens, j)
if propertyNode == nil || propertyTokensConsumed == 0 {
return
}
objectNode.Properties = append(objectNode.Properties, propertyNode)
j += propertyTokensConsumed
}
return
}
func parseAny(tokens []Token, i int) (node *Node, tokensConsumed int) {
check := func(_node *Node, _tokensConsumed int) bool {
if _node == nil || _tokensConsumed == 0 {
return false
}
node = _node
tokensConsumed = _tokensConsumed
return true
}
if check(parseNumber(tokens, i)) {
return
}
if check(parseBoolean(tokens, i)) {
return
}
if check(parseString(tokens, i)) {
return
}
if check(parseArray(tokens, i)) {
return
}
if check(parseObject(tokens, i)) {
return
}
return
}
func Parse(tokens []Token) (ast *Node, ok bool) {
ast, tokensConsumed := parseAny(tokens, 0)
if ast != nil && tokensConsumed == len(tokens) {
return ast, true
}
return
}