-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.go.y
171 lines (154 loc) · 3.78 KB
/
parser.go.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
%{
package filter
import "fmt"
type Token struct {
tok int
lit interface{}
pos Position
}
%}
%union{
statements []Statement
statement Statement
expr Expression
tok Token
}
%type<statements> statements
%type<statement> statement
%type<expr> attrName
%type<expr> attrValue
%token<tok> IDENT TRUE FALSE NULL VALUE NUMBER PR EQ NE CO SW EW GT GE LT LE AND OR NOT LPAREN RPAREN LBOXP RBOXP SP
%left AND
%left OR
%right NOT
%%
statements
:
{
$$ = nil
if l, isLexer := yylex.(*Lexer); isLexer {
l.statements = $$
}
}
| statement statements
{
$$ = append([]Statement{$1}, $2...)
if l, isLexer := yylex.(*Lexer); isLexer {
l.statements = $$
}
}
statement //TODO: gt,ge,lt,le operators should take string and boolean
: attrName PR
{
$$ = &AttrStatement{Attr: $1, Operator: $2.lit.(string) }
}
| attrName EQ attrValue
{
$$ = &CompStatement{LHE: $1, Operator: $2.lit.(string), RHE: $3}
}
| attrName NE attrValue
{
$$ = &CompStatement{LHE: $1, Operator: $2.lit.(string), RHE: $3}
}
| attrName CO VALUE
{
$$ = &RegexStatement{LHE: $1, Operator: $2.lit.(string), Value: $3.lit}
}
| attrName SW VALUE
{
$$ = &RegexStatement{LHE: $1, Operator: $2.lit.(string), Value: $3.lit}
}
| attrName EW VALUE
{
$$ = &RegexStatement{LHE: $1, Operator: $2.lit.(string), Value: $3.lit}
}
| attrName GT attrValue
{
$$ = &CompStatement{LHE: $1, Operator: $2.lit.(string), RHE: $3}
}
| attrName GE attrValue
{
$$ = &CompStatement{LHE: $1, Operator: $2.lit.(string), RHE: $3}
}
| attrName LT attrValue
{
$$ = &CompStatement{LHE: $1, Operator: $2.lit.(string), RHE: $3}
}
| attrName LE attrValue
{
$$ = &CompStatement{LHE: $1, Operator: $2.lit.(string), RHE: $3}
}
| statement AND statement
{
$$ = &LogStatement{LHS: $1, Operator: $2.lit.(string), RHS: $3}
}
| statement OR statement
{
$$ = &LogStatement{LHS: $1, Operator: $2.lit.(string), RHS: $3}
}
| LPAREN statement RPAREN
{
$$ = &ParenStatement{Operator: "", SubStatement: $2}
}
| NOT LPAREN statement RPAREN
{
$$ = &ParenStatement{Operator: $1.lit.(string), SubStatement: $3}
}
| attrName LBOXP statement RBOXP
{
$$ = &GroupStatement{ParentAttr: $1, SubStatement: $3}
}
attrName
: IDENT
{
$$ = &IdentifierExpression{Lit: $1.lit.(string)}
}
attrValue
: NUMBER
{
$$ = &NumberExpression{Lit: $1.lit.(int)}
}
| TRUE
{
$$ = &BoolExpression{Lit: true}
}
| FALSE
{
$$ = &BoolExpression{Lit: false}
}
| NULL
{
$$ = &IdentifierExpression{Lit: $1.lit.(string)}
}
| VALUE
{
$$ = &AttrValueExpression{Lit: $1.lit.(string)}
}
%%
type Lexer struct {
s *Scanner
recentLit interface{}
recentPos Position
statements []Statement
}
func (l *Lexer) Lex(lval *yySymType) int {
tok, lit, pos := l.s.Scan()
if tok == EOF {
return 0
}
lval.tok = Token{tok: tok, lit: lit, pos: pos}
l.recentLit = lit
l.recentPos = pos
return tok
}
func (l *Lexer) Error(e string) {
panic("parse error, " + fmt.Sprintf("Line %d, Column %d: %q %s",
l.recentPos.Line, l.recentPos.Column, l.recentLit, e))
}
func Parse(s *Scanner) []Statement {
l := Lexer{s: s}
if yyParse(&l) != 0 {
panic("Parse error")
}
return l.statements
}