forked from google/mtail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.y
489 lines (447 loc) · 8.23 KB
/
parser.y
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
// Copyright 2011 Google Inc. All Rights Reserved.
// This file is available under the Apache license.
%{
package vm
import (
"fmt"
"github.com/google/mtail/metrics"
)
%}
%union
{
intVal int64
floatVal float64
op int
text string
texts []string
flag bool
n node
kind metrics.Kind
}
%type <n> stmt_list stmt cond arg_expr_list compound_statement conditional_statement expression_statement
%type <n> expr primary_expr multiplicative_expr additive_expr postfix_expr unary_expr assign_expr rel_expr shift_expr bitwise_expr
%type <n> declaration declarator definition decoration_statement
%type <kind> type_spec
%type <text> as_spec
%type <texts> by_spec by_expr_list
%type <flag> hide_spec
%type <op> relop shift_op bitwise_op
%type <text> pattern_expr
// Tokens and types are defined here.
// Invalid input
%token <text> INVALID
// Types
%token COUNTER GAUGE TIMER
// Reserved words
%token AS BY CONST HIDDEN DEF DEL NEXT OTHERWISE ELSE
// Builtins
%token <text> BUILTIN
// Literals: re2 syntax regular expression, quoted strings, regex capture group
// references, identifiers, decorators, and numerical constants.
%token <text> REGEX
%token <text> STRING
%token <text> CAPREF
%token <text> ID
%token <text> DECO
%token <intVal> INTLITERAL
%token <floatVal> FLOATLITERAL
// Operators, in order of precedence
%token <op> INC
%token <op> DIV MOD MUL MINUS PLUS POW
%token <op> SHL SHR
%token <op> LT GT LE GE EQ NE
%token <op> AND OR XOR NOT
%token <op> ADD_ASSIGN ASSIGN
// Punctuation
%token LCURLY RCURLY LPAREN RPAREN LSQUARE RSQUARE
%token COMMA
%token NL
%start start
%%
start
: stmt_list
{
mtaillex.(*parser).root = $1
}
;
stmt_list
: /* empty */
{
$$ = &stmtlistNode{}
}
| stmt_list stmt
{
$$ = $1
if ($2 != nil) {
$$.(*stmtlistNode).children = append($$.(*stmtlistNode).children, $2)
}
}
;
stmt
: conditional_statement
{ $$ = $1 }
| expression_statement
{ $$ = $1 }
| declaration
{ $$ = $1 }
| definition
{ $$ = $1 }
| decoration_statement
{ $$ = $1 }
| NEXT
{
$$ = &nextNode{mtaillex.(*parser).t.pos}
}
| CONST ID pattern_expr
{
// Store the regex for concatenation
mtaillex.(*parser).res[$2] = $3
}
| DEL postfix_expr
{
$$ = &delNode{mtaillex.(*parser).t.pos, $2}
}
;
conditional_statement
: cond compound_statement ELSE compound_statement
{
$$ = &condNode{$1, $2, $4}
}
| cond compound_statement
{
if $1 != nil {
$$ = &condNode{$1, $2, nil}
} else {
$$ = $2
}
}
;
expression_statement
: NL
{ $$ = nil }
| expr NL
{ $$ = $1 }
;
compound_statement
: LCURLY stmt_list RCURLY
{
$$ = $2
}
;
expr
: assign_expr
{ $$ = $1 }
;
assign_expr
: bitwise_expr
{
$$ = $1
}
| unary_expr ASSIGN bitwise_expr
{
$$ = &binaryExprNode{lhs: $1, rhs: $3, op: $2}
}
| unary_expr ADD_ASSIGN bitwise_expr
{
$$ = &binaryExprNode{lhs: $1, rhs: &binaryExprNode{lhs: $1, rhs: $3, op: PLUS}, op: ASSIGN}
}
;
bitwise_expr
: rel_expr
{ $$ = $1 }
| bitwise_expr bitwise_op rel_expr
{
$$ = &binaryExprNode{lhs: $1, rhs: $3, op: $2}
}
;
bitwise_op
: AND
{ $$ = $1 }
| OR
{ $$ = $1 }
| XOR
{ $$ = $1 }
;
rel_expr
: shift_expr
{ $$ = $1 }
| rel_expr relop shift_expr
{
$$ = &binaryExprNode{lhs: $1, rhs: $3, op: $2}
}
;
relop
: LT
{ $$ = $1 }
| GT
{ $$ = $1 }
| LE
{ $$ = $1 }
| GE
{ $$ = $1 }
| EQ
{ $$ = $1 }
| NE
{ $$ = $1 }
;
shift_expr
: additive_expr
{ $$ = $1 }
| shift_expr shift_op additive_expr
{
$$ = &binaryExprNode{lhs: $1, rhs: $3, op: $2}
}
;
shift_op
: SHL
{ $$ = $1 }
| SHR
{ $$ = $1 }
;
additive_expr
: multiplicative_expr
{ $$ = $1 }
| additive_expr PLUS multiplicative_expr
{
$$ = &binaryExprNode{lhs: $1, rhs: $3, op: PLUS}
}
| additive_expr MINUS multiplicative_expr
{
$$ = &binaryExprNode{lhs: $1, rhs: $3, op: MINUS}
}
;
multiplicative_expr
: unary_expr
{
$$ = $1
}
| multiplicative_expr MUL unary_expr
{
$$ = &binaryExprNode{lhs: $1, rhs: $3, op: MUL}
}
| multiplicative_expr DIV unary_expr
{
$$ = &binaryExprNode{lhs: $1, rhs: $3, op: DIV}
}
| multiplicative_expr MOD unary_expr
{
$$ = &binaryExprNode{lhs: $1, rhs: $3, op: MOD}
}
| multiplicative_expr POW unary_expr
{
$$ = &binaryExprNode{lhs: $1, rhs: $3, op: $2}
}
;
unary_expr
: postfix_expr
{
$$ = $1
}
| NOT postfix_expr
{
$$ = &unaryExprNode{pos: mtaillex.(*parser).t.pos, expr: $2, op: $1}
}
| BUILTIN LPAREN RPAREN
{
$$ = &builtinNode{pos: mtaillex.(*parser).t.pos, name: $1, args: nil}
}
| BUILTIN LPAREN arg_expr_list RPAREN
{
$$ = &builtinNode{pos: mtaillex.(*parser).t.pos, name: $1, args: $3}
}
;
arg_expr_list
: assign_expr
{
$$ = &exprlistNode{}
$$.(*exprlistNode).children = append($$.(*exprlistNode).children, $1)
}
| arg_expr_list COMMA assign_expr
{
$$ = $1
$$.(*exprlistNode).children = append($$.(*exprlistNode).children, $3)
}
;
postfix_expr
: primary_expr
{
$$ = $1
}
| postfix_expr INC
{
$$ = &unaryExprNode{pos: mtaillex.(*parser).t.pos, expr: $1, op: $2}
}
| postfix_expr LSQUARE expr RSQUARE
{
$$ = &indexedExprNode{lhs: $1, index: $3}
}
;
primary_expr
: ID
{
$$ = &idNode{mtaillex.(*parser).t.pos, $1, nil}
}
| CAPREF
{
$$ = &caprefNode{mtaillex.(*parser).t.pos, $1, nil}
}
| STRING
{
$$ = &stringConstNode{mtaillex.(*parser).t.pos, $1}
}
| LPAREN expr RPAREN
{
$$ = $2
}
| INTLITERAL
{
$$ = &intConstNode{mtaillex.(*parser).t.pos, $1}
}
| FLOATLITERAL
{
$$ = &floatConstNode{mtaillex.(*parser).t.pos, $1}
}
;
cond
: pattern_expr
{
// pos, endPos were stashed during the concatenation of the regex.
pos := MergePosition(&mtaillex.(*parser).pos, &mtaillex.(*parser).endPos)
$$ = ®exNode{pos: *pos, pattern: $1}
}
| rel_expr
{
$$ = $1
}
| OTHERWISE
{
$$ = &otherwiseNode{mtaillex.(*parser).t.pos}
}
;
pattern_expr
: { mtaillex.(*parser).pos = mtaillex.(*parser).t.pos } DIV { mtaillex.(*parser).inRegex() } REGEX DIV
{
// Before the first DIV, stash the start of the pattern_expr in a state
// variable. We know it's the start because pattern_expr is left
// associative. Then, store the end position after the second DIV.
mtaillex.(*parser).endPos = mtaillex.(*parser).t.pos
$$ = $4
}
| pattern_expr PLUS opt_nl DIV { mtaillex.(*parser).inRegex() } REGEX DIV
{
mtaillex.(*parser).endPos = mtaillex.(*parser).t.pos
$$ = $1 + $6
}
| pattern_expr PLUS ID
{
if s, ok := mtaillex.(*parser).res[$3]; ok {
$$ = $1 + s
} else {
mtaillex.Error(fmt.Sprintf("Constant '%s' not defined.\n\tTry adding `const %s /.../' earlier in the program.", $3, $3))
}
}
;
declaration
: hide_spec type_spec declarator
{
$$ = $3
d := $$.(*declNode)
d.kind = $2
d.hidden = $1
}
;
hide_spec
: /* empty */
{
$$ = false
}
| HIDDEN
{
$$ = true
}
;
declarator
: declarator by_spec
{
$$ = $1
$$.(*declNode).keys = $2
}
| declarator as_spec
{
$$ = $1
$$.(*declNode).exportedName = $2
}
| ID
{
$$ = &declNode{pos: mtaillex.(*parser).t.pos, name: $1}
}
| STRING
{
$$ = &declNode{pos: mtaillex.(*parser).t.pos, name: $1}
}
;
type_spec
: COUNTER
{
$$ = metrics.Counter
}
| GAUGE
{
$$ = metrics.Gauge
}
| TIMER
{
$$ = metrics.Timer
}
;
by_spec
: BY by_expr_list
{
$$ = $2
}
;
by_expr_list
: ID
{
$$ = make([]string, 0)
$$ = append($$, $1)
}
| STRING
{
$$ = make([]string, 0)
$$ = append($$, $1)
}
| by_expr_list COMMA ID
{
$$ = $1
$$ = append($$, $3)
}
| by_expr_list COMMA STRING
{
$$ = $1
$$ = append($$, $3)
}
;
as_spec
: AS STRING
{
$$ = $2
}
;
definition
: { mtaillex.(*parser).pos = mtaillex.(*parser).t.pos } DEF ID compound_statement
{
$$ = &defNode{pos: mtaillex.(*parser).pos, name: $3, block: $4}
}
;
decoration_statement
: { mtaillex.(*parser).pos = mtaillex.(*parser).t.pos } DECO compound_statement
{
$$ = &decoNode{mtaillex.(*parser).pos, $2, $3, nil}
}
;
opt_nl
: /* empty */
| NL
;
%%