1 /*
2 * SysDB - src/frontend/grammar.y
3 * Copyright (C) 2013 Sebastian 'tokkee' Harl <sh@tokkee.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * 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 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
28 %{
30 #include "frontend/connection-private.h"
31 #include "frontend/parser.h"
32 #include "frontend/grammar.h"
34 #include "core/store.h"
35 #include "core/store-private.h"
37 #include "utils/error.h"
38 #include "utils/llist.h"
40 #include <stdio.h>
41 #include <string.h>
43 int
44 sdb_fe_yylex(YYSTYPE *yylval, YYLTYPE *yylloc, sdb_fe_yyscan_t yyscanner);
46 sdb_fe_yyextra_t *
47 sdb_fe_yyget_extra(sdb_fe_yyscan_t scanner);
49 void
50 sdb_fe_yyerror(YYLTYPE *lval, sdb_fe_yyscan_t scanner, const char *msg);
52 /* quick access to the current parse tree */
53 #define pt sdb_fe_yyget_extra(scanner)->parsetree
55 /* quick access to the parser mode */
56 #define parser_mode sdb_fe_yyget_extra(scanner)->mode
58 %}
60 %pure-parser
61 %lex-param {sdb_fe_yyscan_t scanner}
62 %parse-param {sdb_fe_yyscan_t scanner}
63 %locations
64 %error-verbose
65 %expect 0
66 %name-prefix "sdb_fe_yy"
68 %union {
69 const char *sstr; /* static string */
70 char *str;
72 sdb_data_t data;
74 sdb_llist_t *list;
75 sdb_conn_node_t *node;
77 sdb_store_matcher_t *m;
78 sdb_store_expr_t *expr;
79 }
81 %start statements
83 %token SCANNER_ERROR
85 %token AND OR IS NOT MATCHING
86 %token CMP_EQUAL CMP_NEQUAL CMP_REGEX CMP_NREGEX
87 %token CMP_LT CMP_LE CMP_GE CMP_GT
88 %token CONCAT
90 /* NULL token */
91 %token NULL_T
93 %token FETCH LIST LOOKUP
95 %token <str> IDENTIFIER STRING
97 %token <data> INTEGER FLOAT
99 /* Precedence (lowest first): */
100 %left OR
101 %left AND
102 %right NOT
103 %left CMP_EQUAL CMP_NEQUAL
104 %left CMP_LT CMP_LE CMP_GE CMP_GT
105 %nonassoc CMP_REGEX CMP_NREGEX
106 %left CONCAT
107 %nonassoc IS
108 %left '+' '-'
109 %left '*' '/' '%'
110 %left '(' ')'
111 %left '.'
113 %type <list> statements
114 %type <node> statement
115 fetch_statement
116 list_statement
117 lookup_statement
118 condition
120 %type <m> matcher
121 compare_matcher
123 %type <expr> expression
125 %type <sstr> op
127 %type <data> data
129 %destructor { free($$); } <str>
130 %destructor { sdb_object_deref(SDB_OBJ($$)); } <node> <m> <expr>
132 %%
134 statements:
135 statements ';' statement
136 {
137 /* only accept this in default parse mode */
138 if (parser_mode != SDB_PARSE_DEFAULT) {
139 sdb_fe_yyerror(&yylloc, scanner,
140 YY_("syntax error, unexpected statement, "
141 "expecting condition"));
142 sdb_object_deref(SDB_OBJ($3));
143 YYABORT;
144 }
146 if ($3) {
147 sdb_llist_append(pt, SDB_OBJ($3));
148 sdb_object_deref(SDB_OBJ($3));
149 }
150 }
151 |
152 statement
153 {
154 /* only accept this in default parse mode */
155 if (parser_mode != SDB_PARSE_DEFAULT) {
156 sdb_fe_yyerror(&yylloc, scanner,
157 YY_("syntax error, unexpected statement, "
158 "expecting condition"));
159 sdb_object_deref(SDB_OBJ($1));
160 YYABORT;
161 }
163 if ($1) {
164 sdb_llist_append(pt, SDB_OBJ($1));
165 sdb_object_deref(SDB_OBJ($1));
166 }
167 }
168 |
169 condition
170 {
171 /* only accept this in condition parse mode */
172 if (! (parser_mode & SDB_PARSE_COND)) {
173 sdb_fe_yyerror(&yylloc, scanner,
174 YY_("syntax error, unexpected condition, "
175 "expecting statement"));
176 sdb_object_deref(SDB_OBJ($1));
177 YYABORT;
178 }
180 if ($1) {
181 sdb_llist_append(pt, SDB_OBJ($1));
182 sdb_object_deref(SDB_OBJ($1));
183 }
184 }
185 ;
187 statement:
188 fetch_statement
189 |
190 list_statement
191 |
192 lookup_statement
193 |
194 /* empty */
195 {
196 $$ = NULL;
197 }
198 ;
200 /*
201 * FETCH <hostname>;
202 *
203 * Retrieve detailed information about a single host.
204 */
205 fetch_statement:
206 FETCH STRING
207 {
208 $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
209 conn_fetch_t, conn_fetch_destroy));
210 CONN_FETCH($$)->name = strdup($2);
211 $$->cmd = CONNECTION_FETCH;
212 free($2); $2 = NULL;
213 }
214 ;
216 /*
217 * LIST;
218 *
219 * Returns a list of all hosts in the store.
220 */
221 list_statement:
222 LIST
223 {
224 $$ = SDB_CONN_NODE(sdb_object_create_T(/* name = */ NULL,
225 sdb_conn_node_t));
226 $$->cmd = CONNECTION_LIST;
227 }
228 ;
230 /*
231 * LOOKUP <type> MATCHING <condition>;
232 *
233 * Returns detailed information about <type> matching condition.
234 */
235 lookup_statement:
236 LOOKUP IDENTIFIER MATCHING condition
237 {
238 /* TODO: support other types as well */
239 if (strcasecmp($2, "hosts")) {
240 char errmsg[strlen($2) + 32];
241 snprintf(errmsg, sizeof(errmsg),
242 YY_("unknown table %s"), $2);
243 sdb_fe_yyerror(&yylloc, scanner, errmsg);
244 free($2); $2 = NULL;
245 sdb_object_deref(SDB_OBJ($4));
246 YYABORT;
247 }
249 $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
250 conn_lookup_t, conn_lookup_destroy));
251 CONN_LOOKUP($$)->matcher = CONN_MATCHER($4);
252 $$->cmd = CONNECTION_LOOKUP;
253 free($2); $2 = NULL;
254 }
255 ;
257 condition:
258 matcher
259 {
260 if (! $1) {
261 /* TODO: improve error reporting */
262 sdb_fe_yyerror(&yylloc, scanner,
263 YY_("syntax error, invalid condition"));
264 YYABORT;
265 }
267 $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
268 conn_node_matcher_t, conn_matcher_destroy));
269 $$->cmd = CONNECTION_EXPR;
270 CONN_MATCHER($$)->matcher = $1;
271 }
272 ;
274 matcher:
275 '(' matcher ')'
276 {
277 $$ = $2;
278 }
279 |
280 matcher AND matcher
281 {
282 $$ = sdb_store_con_matcher($1, $3);
283 sdb_object_deref(SDB_OBJ($1));
284 sdb_object_deref(SDB_OBJ($3));
285 }
286 |
287 matcher OR matcher
288 {
289 $$ = sdb_store_dis_matcher($1, $3);
290 sdb_object_deref(SDB_OBJ($1));
291 sdb_object_deref(SDB_OBJ($3));
292 }
293 |
294 NOT matcher
295 {
296 $$ = sdb_store_inv_matcher($2);
297 sdb_object_deref(SDB_OBJ($2));
298 }
299 |
300 compare_matcher
301 {
302 $$ = $1;
303 }
304 ;
306 /*
307 * <object_type>.<object_attr> <op> <value>
308 *
309 * Parse matchers comparing object attributes with a value.
310 */
311 compare_matcher:
312 IDENTIFIER op expression
313 {
314 $$ = sdb_store_matcher_parse_cmp($1, NULL, $2, $3);
315 free($1); $1 = NULL;
316 sdb_object_deref(SDB_OBJ($3));
317 }
318 |
319 IDENTIFIER '.' IDENTIFIER op expression
320 {
321 $$ = sdb_store_matcher_parse_cmp($1, $3, $4, $5);
322 free($1); $1 = NULL;
323 free($3); $3 = NULL;
324 sdb_object_deref(SDB_OBJ($5));
325 }
326 |
327 IDENTIFIER '.' IDENTIFIER IS NULL_T
328 {
329 $$ = sdb_store_matcher_parse_cmp($1, $3, "IS", NULL);
330 free($1); $1 = NULL;
331 free($3); $3 = NULL;
332 }
333 |
334 IDENTIFIER '.' IDENTIFIER IS NOT NULL_T
335 {
336 sdb_store_matcher_t *m;
337 m = sdb_store_matcher_parse_cmp($1, $3, "IS", NULL);
338 free($1); $1 = NULL;
339 free($3); $3 = NULL;
341 /* sdb_store_inv_matcher return NULL if m==NULL */
342 $$ = sdb_store_inv_matcher(m);
343 sdb_object_deref(SDB_OBJ(m));
344 }
345 ;
347 expression:
348 '(' expression ')'
349 {
350 $$ = $2;
351 }
352 |
353 expression '+' expression
354 {
355 $$ = sdb_store_expr_create(SDB_DATA_ADD, $1, $3);
356 sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
357 sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
358 }
359 |
360 expression '-' expression
361 {
362 $$ = sdb_store_expr_create(SDB_DATA_SUB, $1, $3);
363 sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
364 sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
365 }
366 |
367 expression '*' expression
368 {
369 $$ = sdb_store_expr_create(SDB_DATA_MUL, $1, $3);
370 sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
371 sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
372 }
373 |
374 expression '/' expression
375 {
376 $$ = sdb_store_expr_create(SDB_DATA_DIV, $1, $3);
377 sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
378 sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
379 }
380 |
381 expression '%' expression
382 {
383 $$ = sdb_store_expr_create(SDB_DATA_MOD, $1, $3);
384 sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
385 sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
386 }
387 |
388 data
389 {
390 $$ = sdb_store_expr_constvalue(&$1);
391 sdb_data_free_datum(&$1);
392 }
393 ;
395 op:
396 CMP_EQUAL { $$ = "="; }
397 |
398 CMP_NEQUAL { $$ = "!="; }
399 |
400 CMP_REGEX { $$ = "=~"; }
401 |
402 CMP_NREGEX { $$ = "!~"; }
403 |
404 CMP_LT { $$ = "<"; }
405 |
406 CMP_LE { $$ = "<="; }
407 |
408 CMP_GE { $$ = ">="; }
409 |
410 CMP_GT { $$ = ">"; }
411 ;
413 data:
414 STRING { $$.type = SDB_TYPE_STRING; $$.data.string = $1; }
415 |
416 INTEGER { $$ = $1; }
417 |
418 FLOAT { $$ = $1; }
419 ;
421 %%
423 void
424 sdb_fe_yyerror(YYLTYPE *lval, sdb_fe_yyscan_t scanner, const char *msg)
425 {
426 sdb_log(SDB_LOG_ERR, "frontend: parse error: %s", msg);
427 } /* sdb_fe_yyerror */
429 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */