BadVPN – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /** |
2 | * @file BPredicate.y |
||
3 | * @author Ambroz Bizjak <ambrop7@gmail.com> |
||
4 | * |
||
5 | * @section LICENSE |
||
6 | * |
||
7 | * Redistribution and use in source and binary forms, with or without |
||
8 | * modification, are permitted provided that the following conditions are met: |
||
9 | * 1. Redistributions of source code must retain the above copyright |
||
10 | * notice, this list of conditions and the following disclaimer. |
||
11 | * 2. Redistributions in binary form must reproduce the above copyright |
||
12 | * notice, this list of conditions and the following disclaimer in the |
||
13 | * documentation and/or other materials provided with the distribution. |
||
14 | * 3. Neither the name of the author nor the |
||
15 | * names of its contributors may be used to endorse or promote products |
||
16 | * derived from this software without specific prior written permission. |
||
17 | * |
||
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
||
19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
||
20 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
||
21 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
||
22 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
||
23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
||
24 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
||
25 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
||
27 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
28 | * |
||
29 | * @section DESCRIPTION |
||
30 | * |
||
31 | * {@link BPredicate} grammar file. |
||
32 | */ |
||
33 | |||
34 | %{ |
||
35 | |||
36 | #include <stdlib.h> |
||
37 | |||
38 | #include <predicate/BPredicate_internal.h> |
||
39 | #include <predicate/BPredicate_parser.h> |
||
40 | |||
41 | #define YYLEX_PARAM scanner |
||
42 | |||
43 | static struct predicate_node * make_constant (int val) |
||
44 | { |
||
45 | struct predicate_node *n = (struct predicate_node *)malloc(sizeof(*n)); |
||
46 | if (!n) { |
||
47 | return NULL; |
||
48 | } |
||
49 | |||
50 | n->type = NODE_CONSTANT; |
||
51 | n->constant.val = val; |
||
52 | |||
53 | return n; |
||
54 | } |
||
55 | |||
56 | static struct predicate_node * make_negation (struct predicate_node *op) |
||
57 | { |
||
58 | if (!op) { |
||
59 | goto fail; |
||
60 | } |
||
61 | |||
62 | struct predicate_node *n = (struct predicate_node *)malloc(sizeof(*n)); |
||
63 | if (!n) { |
||
64 | goto fail; |
||
65 | } |
||
66 | |||
67 | n->type = NODE_NEG; |
||
68 | n->neg.op = op; |
||
69 | |||
70 | return n; |
||
71 | |||
72 | fail: |
||
73 | if (op) { |
||
74 | free_predicate_node(op); |
||
75 | } |
||
76 | return NULL; |
||
77 | } |
||
78 | |||
79 | static struct predicate_node * make_conjunction (struct predicate_node *op1, struct predicate_node *op2) |
||
80 | { |
||
81 | if (!op1 || !op2) { |
||
82 | goto fail; |
||
83 | } |
||
84 | |||
85 | struct predicate_node *n = (struct predicate_node *)malloc(sizeof(*n)); |
||
86 | if (!n) { |
||
87 | goto fail; |
||
88 | } |
||
89 | |||
90 | n->type = NODE_CONJUNCT; |
||
91 | n->conjunct.op1 = op1; |
||
92 | n->conjunct.op2 = op2; |
||
93 | |||
94 | return n; |
||
95 | |||
96 | fail: |
||
97 | if (op1) { |
||
98 | free_predicate_node(op1); |
||
99 | } |
||
100 | if (op2) { |
||
101 | free_predicate_node(op2); |
||
102 | } |
||
103 | return NULL; |
||
104 | } |
||
105 | |||
106 | static struct predicate_node * make_disjunction (struct predicate_node *op1, struct predicate_node *op2) |
||
107 | { |
||
108 | if (!op1 || !op2) { |
||
109 | goto fail; |
||
110 | } |
||
111 | |||
112 | struct predicate_node *n = (struct predicate_node *)malloc(sizeof(*n)); |
||
113 | if (!n) { |
||
114 | goto fail; |
||
115 | } |
||
116 | |||
117 | n->type = NODE_DISJUNCT; |
||
118 | n->disjunct.op1 = op1; |
||
119 | n->disjunct.op2 = op2; |
||
120 | |||
121 | return n; |
||
122 | |||
123 | fail: |
||
124 | if (op1) { |
||
125 | free_predicate_node(op1); |
||
126 | } |
||
127 | if (op2) { |
||
128 | free_predicate_node(op2); |
||
129 | } |
||
130 | return NULL; |
||
131 | } |
||
132 | |||
133 | static struct predicate_node * make_function (char *name, struct arguments_node *args, int need_args) |
||
134 | { |
||
135 | if (!name || (!args && need_args)) { |
||
136 | goto fail; |
||
137 | } |
||
138 | |||
139 | struct predicate_node *n = (struct predicate_node *)malloc(sizeof(*n)); |
||
140 | if (!n) { |
||
141 | goto fail; |
||
142 | } |
||
143 | |||
144 | n->type = NODE_FUNCTION; |
||
145 | n->function.name = name; |
||
146 | n->function.args = args; |
||
147 | |||
148 | return n; |
||
149 | |||
150 | fail: |
||
151 | if (name) { |
||
152 | free(name); |
||
153 | } |
||
154 | if (args) { |
||
155 | free_arguments_node(args); |
||
156 | } |
||
157 | return NULL; |
||
158 | } |
||
159 | |||
160 | static struct arguments_node * make_arguments (struct arguments_arg arg, struct arguments_node *next, int need_next) |
||
161 | { |
||
162 | if (arg.type == ARGUMENT_INVALID || (!next && need_next)) { |
||
163 | goto fail; |
||
164 | } |
||
165 | |||
166 | struct arguments_node *n = (struct arguments_node *)malloc(sizeof(*n)); |
||
167 | if (!n) { |
||
168 | goto fail; |
||
169 | } |
||
170 | |||
171 | n->arg = arg; |
||
172 | n->next = next; |
||
173 | |||
174 | return n; |
||
175 | |||
176 | fail: |
||
177 | free_argument(arg); |
||
178 | if (next) { |
||
179 | free_arguments_node(next); |
||
180 | } |
||
181 | return NULL; |
||
182 | } |
||
183 | |||
184 | static struct arguments_arg make_argument_predicate (struct predicate_node *pr) |
||
185 | { |
||
186 | struct arguments_arg ret; |
||
187 | |||
188 | if (!pr) { |
||
189 | goto fail; |
||
190 | } |
||
191 | |||
192 | ret.type = ARGUMENT_PREDICATE; |
||
193 | ret.predicate = pr; |
||
194 | |||
195 | return ret; |
||
196 | |||
197 | fail: |
||
198 | ret.type = ARGUMENT_INVALID; |
||
199 | return ret; |
||
200 | } |
||
201 | |||
202 | static struct arguments_arg make_argument_string (char *string) |
||
203 | { |
||
204 | struct arguments_arg ret; |
||
205 | |||
206 | if (!string) { |
||
207 | goto fail; |
||
208 | } |
||
209 | |||
210 | ret.type = ARGUMENT_STRING; |
||
211 | ret.string = string; |
||
212 | |||
213 | return ret; |
||
214 | |||
215 | fail: |
||
216 | ret.type = ARGUMENT_INVALID; |
||
217 | return ret; |
||
218 | } |
||
219 | |||
220 | %} |
||
221 | |||
222 | %pure-parser |
||
223 | %locations |
||
224 | %parse-param {void *scanner} |
||
225 | %parse-param {struct predicate_node **result} |
||
226 | |||
227 | %union { |
||
228 | char *text; |
||
229 | struct predicate_node *node; |
||
230 | struct arguments_node *arg_node; |
||
231 | struct predicate_node nfaw; |
||
232 | struct arguments_arg arg_arg; |
||
233 | }; |
||
234 | |||
235 | // token types |
||
236 | %token <text> STRING NAME |
||
237 | %token PEER1_NAME PEER2_NAME AND OR NOT SPAR EPAR CONSTANT_TRUE CONSTANT_FALSE COMMA |
||
238 | |||
239 | // string token destructor |
||
240 | %destructor { |
||
241 | free($$); |
||
242 | } STRING NAME |
||
243 | |||
244 | // return values |
||
245 | %type <node> predicate constant parentheses neg conjunct disjunct function |
||
246 | %type <arg_node> arguments |
||
247 | %type <arg_arg> argument |
||
248 | |||
249 | // predicate node destructor |
||
250 | %destructor { |
||
251 | if ($$) { |
||
252 | free_predicate_node($$); |
||
253 | } |
||
254 | } predicate constant parentheses neg conjunct disjunct function |
||
255 | |||
256 | // argument node destructor |
||
257 | %destructor { |
||
258 | if ($$) { |
||
259 | free_arguments_node($$); |
||
260 | } |
||
261 | } arguments |
||
262 | |||
263 | // argument argument destructor |
||
264 | %destructor { |
||
265 | free_argument($$); |
||
266 | } argument |
||
267 | |||
268 | %left OR |
||
269 | %left AND |
||
270 | %nonassoc NOT |
||
271 | %right COMMA |
||
272 | |||
273 | %% |
||
274 | |||
275 | input: |
||
276 | predicate { |
||
277 | *result = $1; |
||
278 | } |
||
279 | ; |
||
280 | |||
281 | predicate: constant | parentheses | neg | conjunct | disjunct | function; |
||
282 | |||
283 | constant: |
||
284 | CONSTANT_TRUE { |
||
285 | $$ = make_constant(1); |
||
286 | } |
||
287 | | |
||
288 | CONSTANT_FALSE { |
||
289 | $$ = make_constant(0); |
||
290 | } |
||
291 | ; |
||
292 | |||
293 | parentheses: |
||
294 | SPAR predicate EPAR { |
||
295 | $$ = $2; |
||
296 | } |
||
297 | ; |
||
298 | |||
299 | neg: |
||
300 | NOT predicate { |
||
301 | $$ = make_negation($2); |
||
302 | } |
||
303 | ; |
||
304 | |||
305 | conjunct: |
||
306 | predicate AND predicate { |
||
307 | $$ = make_conjunction($1, $3); |
||
308 | } |
||
309 | ; |
||
310 | |||
311 | disjunct: |
||
312 | predicate OR predicate { |
||
313 | $$ = make_disjunction($1, $3); |
||
314 | } |
||
315 | ; |
||
316 | |||
317 | function: |
||
318 | NAME SPAR EPAR { |
||
319 | $$ = make_function($1, NULL, 0); |
||
320 | } |
||
321 | | |
||
322 | NAME SPAR arguments EPAR { |
||
323 | $$ = make_function($1, $3, 1); |
||
324 | } |
||
325 | ; |
||
326 | |||
327 | arguments: |
||
328 | argument { |
||
329 | $$ = make_arguments($1, NULL, 0); |
||
330 | } |
||
331 | | |
||
332 | argument COMMA arguments { |
||
333 | $$ = make_arguments($1, $3, 1); |
||
334 | } |
||
335 | ; |
||
336 | |||
337 | argument: |
||
338 | predicate { |
||
339 | $$ = make_argument_predicate($1); |
||
340 | } |
||
341 | | |
||
342 | STRING { |
||
343 | $$ = make_argument_string($1); |
||
344 | } |
||
345 | ; |