-
Notifications
You must be signed in to change notification settings - Fork 163
/
expr_parser.go
362 lines (322 loc) · 7.71 KB
/
expr_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
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
package actionlint
import (
"fmt"
"strconv"
"strings"
)
func errorAtToken(t *Token, msg string) *ExprError {
return &ExprError{
Message: msg,
Offset: t.Offset,
Line: t.Line,
Column: t.Column,
}
}
// ExprParser is a parser for expression syntax. To know the details, see
// https://docs.github.com/en/actions/learn-github-actions/expressions
type ExprParser struct {
cur *Token
lexer *ExprLexer
err *ExprError
}
// NewExprParser creates new ExprParser instance.
func NewExprParser() *ExprParser {
return &ExprParser{}
}
func (p *ExprParser) error(msg string) {
if p.err == nil {
p.err = errorAtToken(p.cur, msg)
}
}
func (p *ExprParser) errorf(format string, args ...interface{}) {
p.error(fmt.Sprintf(format, args...))
}
func (p *ExprParser) unexpected(where string, expected []TokenKind) {
if p.err != nil {
return
}
qb := quotesBuilder{}
for _, e := range expected {
qb.append(e.String())
}
var what string
if p.cur.Kind == TokenKindEnd {
what = "end of input"
} else {
what = fmt.Sprintf("token %q", p.cur.Kind.String())
}
msg := fmt.Sprintf("unexpected %s while parsing %s. expecting %s", what, where, qb.build())
p.error(msg)
}
func (p *ExprParser) next() *Token {
ret := p.cur
p.cur = p.lexer.Next()
return ret
}
func (p *ExprParser) peek() *Token {
return p.cur
}
func (p *ExprParser) parseIdent() ExprNode {
ident := p.next() // eat ident
switch p.peek().Kind {
case TokenKindLeftParen:
// Parse function call as primary expression though generally function call is parsed as
// postfix expression. The reason is that only built-in function call is allowed in workflow
// expression syntax, meant that callee is always built-in function name, not a general
// expression.
p.next() // eat '('
args := []ExprNode{}
if p.peek().Kind == TokenKindRightParen {
// no arguments
p.next() // eat ')'
} else {
LoopArgs:
for {
arg := p.parseLogicalOr()
if arg == nil {
return nil
}
args = append(args, arg)
switch p.peek().Kind {
case TokenKindComma:
p.next() // eat ','
// continue to next argument
case TokenKindRightParen:
p.next() // eat ')'
break LoopArgs
default:
p.unexpected("arguments of function call", []TokenKind{TokenKindComma, TokenKindRightParen})
return nil
}
}
}
return &FuncCallNode{ident.Value, args, ident}
default:
// Handle keywords. Note that keywords are case sensitive. TRUE, FALSE, NULL are invalid named value.
switch ident.Value {
case "null":
return &NullNode{ident}
case "true":
return &BoolNode{true, ident}
case "false":
return &BoolNode{false, ident}
default:
// Variable name access is case insensitive. github.event and GITHUB.event are the same.
return &VariableNode{strings.ToLower(ident.Value), ident}
}
}
}
func (p *ExprParser) parseNestedExpr() ExprNode {
p.next() // eat '('
nested := p.parseLogicalOr()
if nested == nil {
return nil
}
if p.peek().Kind == TokenKindRightParen {
p.next() // eat ')'
} else {
p.unexpected("closing ')' of nested expression (...)", []TokenKind{TokenKindRightParen})
return nil
}
return nested
}
func (p *ExprParser) parseInt() ExprNode {
t := p.peek()
i, err := strconv.ParseInt(t.Value, 0, 32)
if err != nil {
p.errorf("parsing invalid integer literal %q: %s", t.Value, err)
return nil
}
p.next() // eat int
return &IntNode{int(i), t}
}
func (p *ExprParser) parseFloat() ExprNode {
t := p.peek()
f, err := strconv.ParseFloat(t.Value, 64)
if err != nil {
p.errorf("parsing invalid float literal %q: %s", t.Value, err)
return nil
}
p.next() // eat float
return &FloatNode{f, t}
}
func (p *ExprParser) parseString() ExprNode {
t := p.next() // eat string
s := t.Value
s = s[1 : len(s)-1] // strip first and last single quotes
s = strings.ReplaceAll(s, "''", "'") // unescape ''
return &StringNode{s, t}
}
func (p *ExprParser) parsePrimaryExpr() ExprNode {
switch p.peek().Kind {
case TokenKindIdent:
return p.parseIdent()
case TokenKindLeftParen:
return p.parseNestedExpr()
case TokenKindInt:
return p.parseInt()
case TokenKindFloat:
return p.parseFloat()
case TokenKindString:
return p.parseString()
default:
p.unexpected(
"variable access, function call, null, bool, int, float or string",
[]TokenKind{
TokenKindIdent,
TokenKindLeftParen,
TokenKindInt,
TokenKindFloat,
TokenKindString,
},
)
return nil
}
}
func (p *ExprParser) parsePostfixOp() ExprNode {
ret := p.parsePrimaryExpr()
if ret == nil {
return nil
}
for {
switch p.peek().Kind {
case TokenKindDot:
p.next() // eat '.'
switch p.peek().Kind {
case TokenKindStar:
p.next() // eat '*'
ret = &ArrayDerefNode{ret}
case TokenKindIdent:
t := p.next() // eat 'b' of 'a.b'
// Property name is case insensitive. github.event and github.EVENT are the same
ret = &ObjectDerefNode{ret, strings.ToLower(t.Value)}
default:
p.unexpected(
"object property dereference like 'a.b' or array element dereference like 'a.*'",
[]TokenKind{TokenKindIdent, TokenKindStar},
)
return nil
}
case TokenKindLeftBracket:
p.next() // eat '['
idx := p.parseLogicalOr()
if idx == nil {
return nil
}
ret = &IndexAccessNode{ret, idx}
if p.peek().Kind != TokenKindRightBracket {
p.unexpected("closing bracket ']' for index access", []TokenKind{TokenKindRightBracket})
return nil
}
p.next() // eat ']'
default:
return ret
}
}
}
func (p *ExprParser) parsePrefixOp() ExprNode {
t := p.peek()
if t.Kind != TokenKindNot {
return p.parsePostfixOp()
}
p.next() // eat '!' token
o := p.parsePrefixOp()
if o == nil {
return nil
}
return &NotOpNode{o, t}
}
func (p *ExprParser) parseCompareBinOp() ExprNode {
l := p.parsePrefixOp()
if l == nil {
return nil
}
k := CompareOpNodeKindInvalid
switch p.peek().Kind {
case TokenKindLess:
k = CompareOpNodeKindLess
case TokenKindLessEq:
k = CompareOpNodeKindLessEq
case TokenKindGreater:
k = CompareOpNodeKindGreater
case TokenKindGreaterEq:
k = CompareOpNodeKindGreaterEq
case TokenKindEq:
k = CompareOpNodeKindEq
case TokenKindNotEq:
k = CompareOpNodeKindNotEq
default:
return l
}
p.next() // eat the operator token
r := p.parseCompareBinOp()
if r == nil {
return nil
}
return &CompareOpNode{k, l, r}
}
func (p *ExprParser) parseLogicalAnd() ExprNode {
l := p.parseCompareBinOp()
if l == nil {
return nil
}
if p.peek().Kind != TokenKindAnd {
return l
}
p.next() // eat &&
r := p.parseLogicalAnd()
if r == nil {
return nil
}
return &LogicalOpNode{LogicalOpNodeKindAnd, l, r}
}
func (p *ExprParser) parseLogicalOr() ExprNode {
l := p.parseLogicalAnd()
if l == nil {
return nil
}
if p.peek().Kind != TokenKindOr {
return l
}
p.next() // eat ||
r := p.parseLogicalOr()
if r == nil {
return nil
}
return &LogicalOpNode{LogicalOpNodeKindOr, l, r}
}
// Err returns an error which was caused while previous parsing.
func (p *ExprParser) Err() *ExprError {
if err := p.lexer.Err(); err != nil {
return err
}
return p.err
}
// Parse parses token sequence lexed by a given lexer into syntax tree.
func (p *ExprParser) Parse(l *ExprLexer) (ExprNode, *ExprError) {
// Init
p.err = nil
p.lexer = l
p.cur = l.Next()
root := p.parseLogicalOr()
if err := p.Err(); err != nil {
return nil, err
}
if t := p.peek(); t.Kind != TokenKindEnd {
// It did not reach the end of sequence
qb := quotesBuilder{}
qb.append(t.Kind.String())
c := 1
for {
t := l.Next()
if t.Kind == TokenKindEnd {
break
}
qb.append(t.Kind.String())
c++
}
p.errorf("parser did not reach end of input after parsing the expression. %d remaining token(s) in the input: %s", c, qb.build())
return nil, p.err
}
return root, nil
}