forked from ampproject/amphtml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
access-expr-impl.jison
176 lines (156 loc) · 3.64 KB
/
access-expr-impl.jison
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
/* Lexical grammar */
%lex
%%
\s+ /* skip whitespace */
"AND" return 'AND'
"OR" return 'OR'
"NOT" return 'NOT'
"NULL" return 'NULL'
"TRUE" return 'TRUE'
"true" return 'TRUE'
"FALSE" return 'FALSE'
"false" return 'FALSE'
"(" return '('
")" return ')'
"|" return '|'
"<=" return 'LTE'
"<" return 'LT'
">=" return 'GTE'
">" return 'GT'
"!=" return 'NEQ'
"==" return 'DEQ'
"=" return 'EQ'
[0-9]+("."[0-9]+)?\b return 'NUMERIC'
[a-zA-Z_][a-zA-Z0-9_]* return 'NAME'
\'[^\']*\' return 'STRING'
\"[^\"]*\" return 'STRING'
\. return 'DOT'
. return 'INVALID'
<<EOF>> return 'EOF'
/lex
/* Symbolic tokens */
%token NAME
%token STRING
%token NUMERIC
%token TRUE FALSE
%token NULL
%token DOT
%token EOF
/* Operators */
%left OR
%left AND
%left NOT
%left EQ
%left NEQ
%%
/* Rules */
result:
search_condition EOF
{return $1;}
;
/**
* Top level conditions: logical boolean operators or parenthesis.
* Ex: `X = 1 OR X = 2`, `NOT (X = 1)`, `(X = 1)`
*/
search_condition:
search_condition OR search_condition
{$$ = $1 || $3;}
| search_condition AND search_condition
{$$ = $1 && $3;}
| NOT search_condition
{$$ = !$2;}
| '(' search_condition ')'
{$$ = $2;}
| predicate
{$$ = $1;}
;
/**
* Comparison or "truthy" predicates.
* Ex: `X = 1` or just `X`
*/
predicate:
comparison_predicate
{$$ = $1;}
| truthy_predicate
{$$ = $1;}
;
/**
* Comparison predicates.
* Ex: `X = 1`, `X != 1`, `X <= 10`.
*/
comparison_predicate:
scalar_exp EQ scalar_exp
{$$ = $1 === $3;}
| scalar_exp DEQ scalar_exp
{throw new Error('"==" is not allowed, use "="');}
| scalar_exp NEQ scalar_exp
{$$ = $1 !== $3;}
| scalar_exp LT scalar_exp
{$$ = typeof $1 == typeof $3 && $1 < $3;}
| scalar_exp LTE scalar_exp
{$$ = typeof $1 == typeof $3 && $1 <= $3;}
| scalar_exp GT scalar_exp
{$$ = typeof $1 == typeof $3 && $1 > $3;}
| scalar_exp GTE scalar_exp
{$$ = typeof $1 == typeof $3 && $1 >= $3;}
;
/**
* "Truthy" predicate where only one term is used without operators. It's
* evaluated to boolean using the following rule:
* `X != null && X != '' && X != 0 && X != false`
*
* Ex: `X`.
*/
truthy_predicate:
scalar_exp
{$$ = ($1 !== undefined && $1 !== null
&& $1 !== '' && $1 !== 0 && $1 !== false);}
;
/**
* A scalar - either a literal or a field reference.
* Ex: `field1`, `1234`, `FALSE`, `"A"`.
*/
scalar_exp:
atom
{$$ = $1;}
| field_ref
{$$ = $1;}
;
atom:
literal
{$$ = $1;}
;
/**
* A field reference.
* Ex: `field1`.
*/
field_ref:
field_ref DOT field_name
{$$ = Object.prototype.toString.call($1) == '[object Object]' && $1.hasOwnProperty($3) ? $1[$3] : null;}
| field_name
{$$ = yy[$1] !== undefined ? yy[$1] : null;}
;
/**
* A field name.
* Ex: `"field1"`.
*/
field_name:
NAME
{$$ = yytext;}
;
/**
* A literal: string, number, boolean (true/false) or null.
* Ex: `"A"`, `1234`, `TRUE`, `NULL`.
*/
literal:
STRING
{$$ = yytext.substring(1, yytext.length - 1);}
| NUMERIC
{$$ = Number(yytext);}
| TRUE
{$$ = true}
| FALSE
{$$ = false}
| NULL
{$$ = null}
;