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 FILTER
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 matching_clause
119 filter_clause
120 condition
122 %type <m> matcher
123 compare_matcher
125 %type <expr> expression
127 %type <sstr> op
129 %type <data> data
130 interval interval_elem
132 %destructor { free($$); } <str>
133 %destructor { sdb_object_deref(SDB_OBJ($$)); } <node> <m> <expr>
135 %%
137 statements:
138 statements ';' statement
139 {
140 /* only accept this in default parse mode */
141 if (parser_mode != SDB_PARSE_DEFAULT) {
142 sdb_fe_yyerror(&yylloc, scanner,
143 YY_("syntax error, unexpected statement, "
144 "expecting condition"));
145 sdb_object_deref(SDB_OBJ($3));
146 YYABORT;
147 }
149 if ($3) {
150 sdb_llist_append(pt, SDB_OBJ($3));
151 sdb_object_deref(SDB_OBJ($3));
152 }
153 }
154 |
155 statement
156 {
157 /* only accept this in default parse mode */
158 if (parser_mode != SDB_PARSE_DEFAULT) {
159 sdb_fe_yyerror(&yylloc, scanner,
160 YY_("syntax error, unexpected statement, "
161 "expecting condition"));
162 sdb_object_deref(SDB_OBJ($1));
163 YYABORT;
164 }
166 if ($1) {
167 sdb_llist_append(pt, SDB_OBJ($1));
168 sdb_object_deref(SDB_OBJ($1));
169 }
170 }
171 |
172 condition
173 {
174 /* only accept this in condition parse mode */
175 if (! (parser_mode & SDB_PARSE_COND)) {
176 sdb_fe_yyerror(&yylloc, scanner,
177 YY_("syntax error, unexpected condition, "
178 "expecting statement"));
179 sdb_object_deref(SDB_OBJ($1));
180 YYABORT;
181 }
183 if ($1) {
184 sdb_llist_append(pt, SDB_OBJ($1));
185 sdb_object_deref(SDB_OBJ($1));
186 }
187 }
188 ;
190 statement:
191 fetch_statement
192 |
193 list_statement
194 |
195 lookup_statement
196 |
197 /* empty */
198 {
199 $$ = NULL;
200 }
201 ;
203 /*
204 * FETCH <hostname>;
205 *
206 * Retrieve detailed information about a single host.
207 */
208 fetch_statement:
209 FETCH STRING filter_clause
210 {
211 $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
212 conn_fetch_t, conn_fetch_destroy));
213 CONN_FETCH($$)->name = strdup($2);
214 CONN_FETCH($$)->filter = CONN_MATCHER($3);
215 $$->cmd = CONNECTION_FETCH;
216 free($2); $2 = NULL;
217 }
218 ;
220 /*
221 * LIST;
222 *
223 * Returns a list of all hosts in the store.
224 */
225 list_statement:
226 LIST filter_clause
227 {
228 $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
229 conn_list_t, conn_list_destroy));
230 CONN_LIST($$)->filter = CONN_MATCHER($2);
231 $$->cmd = CONNECTION_LIST;
232 }
233 ;
235 /*
236 * LOOKUP <type> MATCHING <condition> [FILTER <condition>];
237 *
238 * Returns detailed information about <type> matching condition.
239 */
240 lookup_statement:
241 LOOKUP IDENTIFIER matching_clause filter_clause
242 {
243 /* TODO: support other types as well */
244 if (strcasecmp($2, "hosts")) {
245 char errmsg[strlen($2) + 32];
246 snprintf(errmsg, sizeof(errmsg),
247 YY_("unknown data-source %s"), $2);
248 sdb_fe_yyerror(&yylloc, scanner, errmsg);
249 free($2); $2 = NULL;
250 sdb_object_deref(SDB_OBJ($3));
251 sdb_object_deref(SDB_OBJ($4));
252 YYABORT;
253 }
255 $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
256 conn_lookup_t, conn_lookup_destroy));
257 CONN_LOOKUP($$)->matcher = CONN_MATCHER($3);
258 CONN_LOOKUP($$)->filter = CONN_MATCHER($4);
259 $$->cmd = CONNECTION_LOOKUP;
260 free($2); $2 = NULL;
261 }
262 ;
264 matching_clause:
265 MATCHING condition { $$ = $2; }
266 |
267 /* empty */ { $$ = NULL; }
269 filter_clause:
270 FILTER condition { $$ = $2; }
271 |
272 /* empty */ { $$ = NULL; }
274 /*
275 * Basic expressions.
276 */
278 condition:
279 matcher
280 {
281 if (! $1) {
282 /* TODO: improve error reporting */
283 sdb_fe_yyerror(&yylloc, scanner,
284 YY_("syntax error, invalid condition"));
285 YYABORT;
286 }
288 $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
289 conn_matcher_t, conn_matcher_destroy));
290 $$->cmd = CONNECTION_EXPR;
291 CONN_MATCHER($$)->matcher = $1;
292 }
293 ;
295 matcher:
296 '(' matcher ')'
297 {
298 $$ = $2;
299 }
300 |
301 matcher AND matcher
302 {
303 $$ = sdb_store_con_matcher($1, $3);
304 sdb_object_deref(SDB_OBJ($1));
305 sdb_object_deref(SDB_OBJ($3));
306 }
307 |
308 matcher OR matcher
309 {
310 $$ = sdb_store_dis_matcher($1, $3);
311 sdb_object_deref(SDB_OBJ($1));
312 sdb_object_deref(SDB_OBJ($3));
313 }
314 |
315 NOT matcher
316 {
317 $$ = sdb_store_inv_matcher($2);
318 sdb_object_deref(SDB_OBJ($2));
319 }
320 |
321 compare_matcher
322 {
323 $$ = $1;
324 }
325 ;
327 /*
328 * <object_type>.<object_attr> <op> <value>
329 *
330 * Parse matchers comparing object attributes with a value.
331 */
332 compare_matcher:
333 ':' IDENTIFIER op expression
334 {
335 $$ = sdb_store_matcher_parse_field_cmp($2, $3, $4);
336 free($2); $2 = NULL;
337 sdb_object_deref(SDB_OBJ($4));
338 }
339 |
340 IDENTIFIER op expression
341 {
342 $$ = sdb_store_matcher_parse_cmp($1, NULL, $2, $3);
343 free($1); $1 = NULL;
344 sdb_object_deref(SDB_OBJ($3));
345 }
346 |
347 IDENTIFIER '.' IDENTIFIER op expression
348 {
349 $$ = sdb_store_matcher_parse_cmp($1, $3, $4, $5);
350 free($1); $1 = NULL;
351 free($3); $3 = NULL;
352 sdb_object_deref(SDB_OBJ($5));
353 }
354 |
355 IDENTIFIER '.' IDENTIFIER IS NULL_T
356 {
357 $$ = sdb_store_matcher_parse_cmp($1, $3, "IS", NULL);
358 free($1); $1 = NULL;
359 free($3); $3 = NULL;
360 }
361 |
362 IDENTIFIER '.' IDENTIFIER IS NOT NULL_T
363 {
364 sdb_store_matcher_t *m;
365 m = sdb_store_matcher_parse_cmp($1, $3, "IS", NULL);
366 free($1); $1 = NULL;
367 free($3); $3 = NULL;
369 /* sdb_store_inv_matcher return NULL if m==NULL */
370 $$ = sdb_store_inv_matcher(m);
371 sdb_object_deref(SDB_OBJ(m));
372 }
373 ;
375 expression:
376 '(' expression ')'
377 {
378 $$ = $2;
379 }
380 |
381 expression '+' expression
382 {
383 $$ = sdb_store_expr_create(SDB_DATA_ADD, $1, $3);
384 sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
385 sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
386 }
387 |
388 expression '-' expression
389 {
390 $$ = sdb_store_expr_create(SDB_DATA_SUB, $1, $3);
391 sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
392 sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
393 }
394 |
395 expression '*' expression
396 {
397 $$ = sdb_store_expr_create(SDB_DATA_MUL, $1, $3);
398 sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
399 sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
400 }
401 |
402 expression '/' expression
403 {
404 $$ = sdb_store_expr_create(SDB_DATA_DIV, $1, $3);
405 sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
406 sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
407 }
408 |
409 expression '%' expression
410 {
411 $$ = sdb_store_expr_create(SDB_DATA_MOD, $1, $3);
412 sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
413 sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
414 }
415 |
416 ':' IDENTIFIER
417 {
418 int field = sdb_store_parse_field_name($2);
419 free($2); $2 = NULL;
420 $$ = sdb_store_expr_fieldvalue(field);
421 }
422 |
423 data
424 {
425 $$ = sdb_store_expr_constvalue(&$1);
426 sdb_data_free_datum(&$1);
427 }
428 ;
430 op:
431 CMP_EQUAL { $$ = "="; }
432 |
433 CMP_NEQUAL { $$ = "!="; }
434 |
435 CMP_REGEX { $$ = "=~"; }
436 |
437 CMP_NREGEX { $$ = "!~"; }
438 |
439 CMP_LT { $$ = "<"; }
440 |
441 CMP_LE { $$ = "<="; }
442 |
443 CMP_GE { $$ = ">="; }
444 |
445 CMP_GT { $$ = ">"; }
446 ;
448 data:
449 STRING { $$.type = SDB_TYPE_STRING; $$.data.string = $1; }
450 |
451 INTEGER { $$ = $1; }
452 |
453 FLOAT { $$ = $1; }
454 |
455 interval { $$ = $1; }
456 ;
458 interval:
459 interval interval_elem
460 {
461 $$.data.datetime = $1.data.datetime + $2.data.datetime;
462 }
463 |
464 interval_elem { $$ = $1; }
465 ;
467 interval_elem:
468 INTEGER IDENTIFIER
469 {
470 sdb_time_t unit = 1;
472 unit = sdb_strpunit($2);
473 if (! unit) {
474 char errmsg[strlen($2) + 32];
475 snprintf(errmsg, sizeof(errmsg),
476 YY_("invalid time unit %s"), $2);
477 sdb_fe_yyerror(&yylloc, scanner, errmsg);
478 free($2); $2 = NULL;
479 YYABORT;
480 }
481 free($2); $2 = NULL;
483 $$.type = SDB_TYPE_DATETIME;
484 $$.data.datetime = (sdb_time_t)$1.data.integer * unit;
486 if ($1.data.integer < 0) {
487 sdb_fe_yyerror(&yylloc, scanner,
488 YY_("syntax error, negative intervals not supported"));
489 YYABORT;
490 }
491 }
492 ;
494 %%
496 void
497 sdb_fe_yyerror(YYLTYPE *lval, sdb_fe_yyscan_t scanner, const char *msg)
498 {
499 sdb_log(SDB_LOG_ERR, "frontend: parse error: %s", msg);
500 } /* sdb_fe_yyerror */
502 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */