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
|
package main
import (
"strings"
"strconv"
"fmt"
)
type parser struct {
tokenStream chan Token
rewinds []Token
}
func (p *parser) next() Token {
var token Token
if len(p.rewinds) == 0 {
token = <- p.tokenStream
} else {
token = p.rewinds[len(p.rewinds)-1]
p.rewinds = p.rewinds[:len(p.rewinds)-1]
}
if token.typ == TokenErr {
fmt.Println(token)
panic("Lexing error")
}
return token
}
func (p *parser) rewind(token Token) {
p.rewinds = append(p.rewinds, token)
}
func (p *parser) peek() Token {
token := p.next()
p.rewind(token)
return token
}
var segmentTokens map[TokenType]bool = map[TokenType]bool {
TokenHash: true,
TokenAt: true,
TokenDot: true,
TokenLBrack: true,
}
func (p *parser) parsePathPatternFilter(minPower int) PathFilterAST {
var lhs PathFilterAST
token := p.next()
switch token.typ {
case TokenHash:
stringIndex := p.next()
if stringIndex.typ != TokenPatternStringIndex {
panic("Expected string index after # in pattern")
}
lhs = StringSegmentPathFilterAST{stringIndex.val}
case TokenAt:
intIndex := p.next()
if intIndex.typ != TokenPatternIntegerIndex {
panic("Expected integer index after @ in pattern")
}
index, err := strconv.Atoi(intIndex.val)
if err != nil {
panic("Expected integer index after @ in pattern")
}
lhs = IntegerSegmentPathFilterAST{index}
case TokenDot:
lhs = AnySegmentPathFilterAST{}
case TokenLBrack:
lhs = p.parsePathPatternFilter(0)
if p.next().typ != TokenRBrack {
panic("Expected ] in path filter")
}
default:
panic("Expected path pattern filter segment")
}
loop: for {
token = p.next()
switch {
case token.typ == TokenAst && 10 >= minPower:
lhs = RepeatPathFilterAST {lhs}
case token.typ == TokenQuestion && 10 >= minPower:
lhs = OrPathFilterAST{lhs, NonePathFilterAST{}}
case token.typ == TokenBar && 0 >= minPower:
lhs = OrPathFilterAST{lhs, p.parsePathPatternFilter(1)}
case segmentTokens[token.typ] && 2 >= minPower:
p.rewind(token)
lhs = SequencePathFilterAST {lhs, p.parsePathPatternFilter(3)}
default:
p.rewind(token)
break loop
}
}
return lhs
}
func (p *parser) parseFilter(minPower int) Filter {
var lhs Filter
token := p.next()
switch token.typ {
case TokenHash, TokenAt, TokenDot, TokenLBrack:
p.rewind(token)
filterAst := p.parsePathPatternFilter(0)
lhs = compilePathFilterAST(filterAst)
case TokenHat:
lhs = BeginTerminalFilter{}
case TokenDollar:
lhs = EndTerminalFilter{}
case TokenHatDollar:
lhs = TerminalFilter{}
case TokenTilde:
lhs = RootFilter{}
case TokenLParen:
lhs = p.parseFilter(0)
rParen := p.next()
if rParen.typ != TokenRParen {
panic("Missing ) in filter")
}
default:
panic("Expected filter")
}
loop: for {
token = p.next()
switch {
case token.typ == TokenAnd && 2 >= minPower:
lhs = AndFilter {lhs, p.parseFilter(3)}
case token.typ == TokenOr && 0 >= minPower:
lhs = OrFilter {lhs, p.parseFilter(1)}
default:
p.rewind(token)
break loop
}
}
return lhs
}
func (p *parser) parseLiterals() (items []WalkItem) {
var path Path
var value WalkValue
loop: for {
token := p.next()
switch token.typ {
case TokenSemicolon, TokenEOF:
p.rewind(token)
break loop
case TokenComma:
case TokenNullLiteral:
value = ValueNull{}
case TokenTrueLiteral:
value = ValueBool(true)
case TokenFalseLiteral:
value = ValueBool(false)
case TokenNumberLiteral:
numberLiteral, err := strconv.ParseFloat(token.val, 64)
if err != nil {
panic("Error parsing number literal to float64")
}
value = ValueNumber(numberLiteral)
case TokenDoubleQuote:
stringToken := p.next()
if stringToken.typ != TokenStringLiteral {
panic("Expected string literal after \"")
}
// TODO: resolve escape characters
stringLiteral := stringToken.val
if p.next().typ != TokenDoubleQuote {
panic("Expected \" after string literal")
}
colon := p.next()
if colon.typ == TokenColon {
if path != nil {
panic("Expected value after path:")
}
path = Path{stringLiteral}
} else {
p.rewind(colon)
value = ValueString(stringLiteral)
}
case TokenTerminalLiteral:
switch token.val {
case "{":
value = MapBegin
case "}":
value = MapEnd
case "[":
value = ArrayBegin
case "]":
value = ArrayEnd
default:
panic("Invalid terminal token")
}
}
if value != nil {
items = append(items, WalkItem {
path: path,
value: value,
})
path = nil
value = nil
}
}
if path != nil {
panic("Expected value after path:")
}
return items
}
func (p *parser) parseBasicCommand(commandChar rune) Command {
switch commandChar {
case 'p':
return PrintValueCommand{}
case 'd':
return DeleteAllCommand{}
case 'n':
return NextCommand{}
case 'N':
return AppendNextCommand{}
case 'i':
items := p.parseLiterals()
return PrintLiteralsCommand {items: items}
default:
panic("Invalid command")
}
}
func (p *parser) parseCommand() Command {
token := p.next()
switch token.typ {
case TokenHash, TokenAt, TokenDot, TokenLParen, TokenHat, TokenDollar, TokenHatDollar, TokenTilde:
p.rewind(token)
filter := p.parseFilter(0)
notToken := p.next()
if notToken.typ == TokenExclamation {
filter = NotFilter {filter}
} else {
p.rewind(notToken)
}
command := p.parseCommand()
command = FilteredCommand {
filter: filter,
command: command,
}
return command
case TokenLBrace:
commands := p.parseCommands()
if p.next().typ != TokenRBrace {
panic("Missing matching }")
}
return SequenceCommand {commands}
case TokenCommand:
commandChar, _, err := strings.NewReader(token.val).ReadRune()
if err != nil {
panic("Error reading a command character!?")
}
return p.parseBasicCommand(commandChar)
default:
panic("Invalid token, expected command")
}
}
func (p *parser) parseCommands() []Command {
var commands []Command
for {
nextToken := p.peek()
if nextToken.typ == TokenEOF || nextToken.typ == TokenRBrace {
return commands
}
commands = append(commands, p.parseCommand())
semicolon := p.next()
if semicolon.typ == TokenEOF || semicolon.typ == TokenRBrace {
p.rewind(semicolon)
return commands
}
if semicolon.typ != TokenSemicolon {
panic("Expected ; after command")
}
}
}
func Parse(tokens chan Token) []Command {
p := parser {
tokenStream: tokens,
}
return p.parseCommands()
}
|