-
Notifications
You must be signed in to change notification settings - Fork 0
/
expr.y
65 lines (55 loc) · 1007 Bytes
/
expr.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
%{
package query
%}
%union {
str string
expr Expr
list []Expr
}
%token <str> '(' ')' ','
/* The 'p' is for privacy */
%token <str> pNUMBER
%token <str> pWORD
%token <str> pSTRING
/* it was easier to recognize metrics
in the lexer than here in the parser */
%token <str> pMETRIC
%token <str> pERROR /* not used */
%type <expr> function query expr
%type <list> arglist
%%
top: query { yylex.(*lexer).result = &Query{Expr: $1} }
/* A query consists of a single metric pattern or a single
function call. Numbers and quoted strings are not allowed
at the top level. */
query:
pMETRIC
{
m := new(Metric)
*m = Metric($1)
$$ = m
}
| function
function:
pWORD '(' arglist ')'
{
$$ = &Func{Name: $1, Args: $3}
}
arglist:
/* empty */ { $$ = nil }
| expr { $$ = append($$, $1) }
| arglist ',' expr { $$ = append($1, $3) }
expr:
query { $$ = $1 }
| pSTRING
{
v := new(Value)
*v = Value($1)
$$ = v
}
| pNUMBER
{
v := new(Value)
*v = Value($1)
$$ = v
}