forked from rhysd/actionlint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpr_parser.go
359 lines (318 loc) · 8.5 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
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/reference/context-and-expression-syntax-for-github-actions
type ExprParser struct {
input []*Token
sawOperator *Token
}
// NewExprParser creates new ExprParser instance.
func NewExprParser() *ExprParser {
return &ExprParser{}
}
func (p *ExprParser) error(msg string) *ExprError {
return errorAtToken(p.input[0], msg)
}
func (p *ExprParser) errorf(format string, args ...interface{}) *ExprError {
msg := fmt.Sprintf(format, args...)
return errorAtToken(p.input[0], msg)
}
func (p *ExprParser) unexpected(where string, expected []TokenKind) *ExprError {
qb := quotesBuilder{}
for _, e := range expected {
qb.append(e.String())
}
var what string
if p.input[0].Kind == TokenKindEnd {
what = "end of input"
} else {
what = fmt.Sprintf("token %q", p.input[0].Kind.String())
}
msg := fmt.Sprintf("unexpected %s while parsing %s. expecting %s", what, where, qb.build())
return p.error(msg)
}
func (p *ExprParser) next() *Token {
// Do not consume final token to remember position of the end
if p.input[0].Kind == TokenKindEnd {
return p.input[0]
}
t := p.input[0]
p.input = p.input[1:]
return t
}
func (p *ExprParser) peek() *Token {
return p.input[0]
}
// Note: List of operators: https://docs.github.com/en/actions/reference/context-and-expression-syntax-for-github-actions#operators
func (p *ExprParser) seeingOperator(tok *Token) {
if p.sawOperator == nil {
p.sawOperator = tok
}
}
func (p *ExprParser) parseIdent() (ExprNode, *ExprError) {
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, err := p.parseLogicalOr()
if err != nil {
return nil, err
}
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:
return nil, p.unexpected("arguments of function call", []TokenKind{TokenKindComma, TokenKindRightParen})
}
}
}
return &FuncCallNode{ident.Value, args, ident}, nil
default:
// Handle keywords. Note that keywords are case sensitive. TRUE, FALSE, NULL are invalid named value.
switch ident.Value {
case "null":
return &NullNode{ident}, nil
case "true":
return &BoolNode{true, ident}, nil
case "false":
return &BoolNode{false, ident}, nil
default:
// Variable name access is case insensitive. github.event and GITHUB.event are the same.
return &VariableNode{strings.ToLower(ident.Value), ident}, nil
}
}
}
func (p *ExprParser) parseNestedExpr() (ExprNode, *ExprError) {
lparen := p.next() // eat '('
nested, err := p.parseLogicalOr()
if err != nil {
return nil, err
}
if p.peek().Kind == TokenKindRightParen {
p.next() // eat ')'
} else {
return nil, p.unexpected("closing ')' of nexted expression (...)", []TokenKind{TokenKindRightParen})
}
p.seeingOperator(lparen)
return nested, nil
}
func (p *ExprParser) parseInt() (ExprNode, *ExprError) {
t := p.peek()
i, err := strconv.ParseInt(t.Value, 0, 32)
if err != nil {
return nil, p.errorf("parsing invalid integer literal %q: %s", t.Value, err)
}
p.next() // eat int
return &IntNode{int(i), t}, nil
}
func (p *ExprParser) parseFloat() (ExprNode, *ExprError) {
t := p.peek()
f, err := strconv.ParseFloat(t.Value, 64)
if err != nil {
return nil, p.errorf("parsing invalid float literal %q: %s", t.Value, err)
}
p.next() // eat float
return &FloatNode{f, t}, nil
}
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, *ExprError) {
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(), nil
default:
return nil, p.unexpected(
"variable access, function call, null, bool, int, float or string",
[]TokenKind{
TokenKindIdent,
TokenKindLeftParen,
TokenKindInt,
TokenKindFloat,
TokenKindString,
},
)
}
}
func (p *ExprParser) parsePostfixOp() (ExprNode, *ExprError) {
ret, err := p.parsePrimaryExpr()
if err != nil {
return nil, err
}
for {
switch p.peek().Kind {
case TokenKindDot:
dot := 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:
return nil, p.unexpected(
"object property dereference like 'a.b' or array element dereference like 'a.*'",
[]TokenKind{TokenKindIdent, TokenKindStar},
)
}
p.seeingOperator(dot)
case TokenKindLeftBracket:
lbra := p.next() // eat '['
idx, err := p.parseLogicalOr()
if err != nil {
return nil, err
}
ret = &IndexAccessNode{ret, idx}
if p.peek().Kind != TokenKindRightBracket {
return nil, p.unexpected("closing bracket ']' for index access", []TokenKind{TokenKindRightBracket})
}
p.next() // eat ']'
p.seeingOperator(lbra)
default:
return ret, nil
}
}
}
func (p *ExprParser) parsePrefixOp() (ExprNode, *ExprError) {
t := p.peek()
if t.Kind != TokenKindNot {
return p.parsePostfixOp()
}
not := p.next() // eat '!' token
o, err := p.parsePostfixOp()
if err != nil {
return nil, err
}
p.seeingOperator(not)
return &NotOpNode{o, t}, nil
}
func (p *ExprParser) parseCompareBinOp() (ExprNode, *ExprError) {
l, err := p.parsePrefixOp()
if err != nil {
return nil, err
}
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, nil
}
op := p.next() // eat the operator token
r, err := p.parseCompareBinOp()
if err != nil {
return nil, err
}
p.seeingOperator(op)
return &CompareOpNode{k, l, r}, nil
}
func (p *ExprParser) parseLogicalAnd() (ExprNode, *ExprError) {
l, err := p.parseCompareBinOp()
if err != nil {
return nil, err
}
if p.peek().Kind != TokenKindAnd {
return l, nil
}
and := p.next() // eat &&
r, err := p.parseLogicalAnd()
if err != nil {
return nil, err
}
p.seeingOperator(and)
return &LogicalOpNode{LogicalOpNodeKindAnd, l, r}, nil
}
func (p *ExprParser) parseLogicalOr() (ExprNode, *ExprError) {
l, err := p.parseLogicalAnd()
if err != nil {
return nil, err
}
if p.peek().Kind != TokenKindOr {
return l, nil
}
or := p.next() // eat ||
r, err := p.parseLogicalOr()
if err != nil {
return nil, err
}
p.seeingOperator(or)
return &LogicalOpNode{LogicalOpNodeKindOr, l, r}, nil
}
// Parse parses given token sequence into syntax tree.
// Token sequence t must end with TokenKindEnd token and it cannot be empty.
func (p *ExprParser) Parse(t []*Token) (ExprNode, *ExprError) {
// Init
if len(t) == 0 {
panic("tokens must not be empty")
}
p.input = t
p.sawOperator = nil
root, err := p.parseLogicalOr()
if err != nil {
return nil, err
}
if p.peek().Kind != TokenKindEnd {
// It did not reach the end of sequence
qb := quotesBuilder{}
for _, t := range p.input {
if t.Kind == TokenKindEnd {
break
}
qb.append(t.Kind.String())
}
return nil, p.errorf("parser did not reach end of input after parsing expression. remaining tokens are %s", qb.build())
}
return root, nil
}