a297714c074f20a22690245c5c51a8ab1f6d89bb
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 <type> <hostname> [FILTER <condition>];
205 *
206 * Retrieve detailed information about a single host.
207 */
208 fetch_statement:
209 FETCH IDENTIFIER STRING filter_clause
210 {
211 /* TODO: support other types as well */
212 if (strcasecmp($2, "host")) {
213 char errmsg[strlen($2) + 32];
214 snprintf(errmsg, sizeof(errmsg),
215 YY_("unknown data-source %s"), $2);
216 sdb_fe_yyerror(&yylloc, scanner, errmsg);
217 free($2); $2 = NULL;
218 free($3); $3 = NULL;
219 sdb_object_deref(SDB_OBJ($4));
220 YYABORT;
221 }
223 $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
224 conn_fetch_t, conn_fetch_destroy));
225 CONN_FETCH($$)->name = $3;
226 CONN_FETCH($$)->filter = CONN_MATCHER($4);
227 $$->cmd = CONNECTION_FETCH;
228 free($2); $2 = NULL;
229 }
230 ;
232 /*
233 * LIST <type> [FILTER <condition>];
234 *
235 * Returns a list of all hosts in the store.
236 */
237 list_statement:
238 LIST IDENTIFIER filter_clause
239 {
240 /* TODO: support other types as well */
241 if (strcasecmp($2, "hosts")) {
242 char errmsg[strlen($2) + 32];
243 snprintf(errmsg, sizeof(errmsg),
244 YY_("unknown data-source %s"), $2);
245 sdb_fe_yyerror(&yylloc, scanner, errmsg);
246 free($2); $2 = NULL;
247 sdb_object_deref(SDB_OBJ($3));
248 YYABORT;
249 }
251 $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
252 conn_list_t, conn_list_destroy));
253 CONN_LIST($$)->filter = CONN_MATCHER($3);
254 $$->cmd = CONNECTION_LIST;
255 free($2); $2 = NULL;
256 }
257 ;
259 /*
260 * LOOKUP <type> MATCHING <condition> [FILTER <condition>];
261 *
262 * Returns detailed information about <type> matching condition.
263 */
264 lookup_statement:
265 LOOKUP IDENTIFIER matching_clause filter_clause
266 {
267 /* TODO: support other types as well */
268 if (strcasecmp($2, "hosts")) {
269 char errmsg[strlen($2) + 32];
270 snprintf(errmsg, sizeof(errmsg),
271 YY_("unknown data-source %s"), $2);
272 sdb_fe_yyerror(&yylloc, scanner, errmsg);
273 free($2); $2 = NULL;
274 sdb_object_deref(SDB_OBJ($3));
275 sdb_object_deref(SDB_OBJ($4));
276 YYABORT;
277 }
279 $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
280 conn_lookup_t, conn_lookup_destroy));
281 CONN_LOOKUP($$)->matcher = CONN_MATCHER($3);
282 CONN_LOOKUP($$)->filter = CONN_MATCHER($4);
283 $$->cmd = CONNECTION_LOOKUP;
284 free($2); $2 = NULL;
285 }
286 ;
288 matching_clause:
289 MATCHING condition { $$ = $2; }
290 |
291 /* empty */ { $$ = NULL; }
293 filter_clause:
294 FILTER condition { $$ = $2; }
295 |
296 /* empty */ { $$ = NULL; }
298 /*
299 * Basic expressions.
300 */
302 condition:
303 matcher
304 {
305 if (! $1) {
306 /* TODO: improve error reporting */
307 sdb_fe_yyerror(&yylloc, scanner,
308 YY_("syntax error, invalid condition"));
309 YYABORT;
310 }
312 $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
313 conn_matcher_t, conn_matcher_destroy));
314 $$->cmd = CONNECTION_EXPR;
315 CONN_MATCHER($$)->matcher = $1;
316 }
317 ;
319 matcher:
320 '(' matcher ')'
321 {
322 $$ = $2;
323 }
324 |
325 matcher AND matcher
326 {
327 $$ = sdb_store_con_matcher($1, $3);
328 sdb_object_deref(SDB_OBJ($1));
329 sdb_object_deref(SDB_OBJ($3));
330 }
331 |
332 matcher OR matcher
333 {
334 $$ = sdb_store_dis_matcher($1, $3);
335 sdb_object_deref(SDB_OBJ($1));
336 sdb_object_deref(SDB_OBJ($3));
337 }
338 |
339 NOT matcher
340 {
341 $$ = sdb_store_inv_matcher($2);
342 sdb_object_deref(SDB_OBJ($2));
343 }
344 |
345 compare_matcher
346 {
347 $$ = $1;
348 }
349 ;
351 /*
352 * <object_type>.<object_attr> <op> <value>
353 *
354 * Parse matchers comparing object attributes with a value.
355 */
356 compare_matcher:
357 ':' IDENTIFIER op expression
358 {
359 $$ = sdb_store_matcher_parse_field_cmp($2, $3, $4);
360 free($2); $2 = NULL;
361 sdb_object_deref(SDB_OBJ($4));
362 }
363 |
364 IDENTIFIER op expression
365 {
366 $$ = sdb_store_matcher_parse_cmp($1, NULL, $2, $3);
367 free($1); $1 = NULL;
368 sdb_object_deref(SDB_OBJ($3));
369 }
370 |
371 IDENTIFIER '.' IDENTIFIER op expression
372 {
373 $$ = sdb_store_matcher_parse_cmp($1, $3, $4, $5);
374 free($1); $1 = NULL;
375 free($3); $3 = NULL;
376 sdb_object_deref(SDB_OBJ($5));
377 }
378 |
379 IDENTIFIER '.' IDENTIFIER IS NULL_T
380 {
381 $$ = sdb_store_matcher_parse_cmp($1, $3, "IS", NULL);
382 free($1); $1 = NULL;
383 free($3); $3 = NULL;
384 }
385 |
386 IDENTIFIER '.' IDENTIFIER IS NOT NULL_T
387 {
388 sdb_store_matcher_t *m;
389 m = sdb_store_matcher_parse_cmp($1, $3, "IS", NULL);
390 free($1); $1 = NULL;
391 free($3); $3 = NULL;
393 /* sdb_store_inv_matcher return NULL if m==NULL */
394 $$ = sdb_store_inv_matcher(m);
395 sdb_object_deref(SDB_OBJ(m));
396 }
397 ;
399 expression:
400 '(' expression ')'
401 {
402 $$ = $2;
403 }
404 |
405 expression '+' expression
406 {
407 $$ = sdb_store_expr_create(SDB_DATA_ADD, $1, $3);
408 sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
409 sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
410 }
411 |
412 expression '-' expression
413 {
414 $$ = sdb_store_expr_create(SDB_DATA_SUB, $1, $3);
415 sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
416 sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
417 }
418 |
419 expression '*' expression
420 {
421 $$ = sdb_store_expr_create(SDB_DATA_MUL, $1, $3);
422 sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
423 sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
424 }
425 |
426 expression '/' expression
427 {
428 $$ = sdb_store_expr_create(SDB_DATA_DIV, $1, $3);
429 sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
430 sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
431 }
432 |
433 expression '%' expression
434 {
435 $$ = sdb_store_expr_create(SDB_DATA_MOD, $1, $3);
436 sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
437 sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
438 }
439 |
440 ':' IDENTIFIER
441 {
442 int field = sdb_store_parse_field_name($2);
443 free($2); $2 = NULL;
444 $$ = sdb_store_expr_fieldvalue(field);
445 }
446 |
447 data
448 {
449 $$ = sdb_store_expr_constvalue(&$1);
450 sdb_data_free_datum(&$1);
451 }
452 ;
454 op:
455 CMP_EQUAL { $$ = "="; }
456 |
457 CMP_NEQUAL { $$ = "!="; }
458 |
459 CMP_REGEX { $$ = "=~"; }
460 |
461 CMP_NREGEX { $$ = "!~"; }
462 |
463 CMP_LT { $$ = "<"; }
464 |
465 CMP_LE { $$ = "<="; }
466 |
467 CMP_GE { $$ = ">="; }
468 |
469 CMP_GT { $$ = ">"; }
470 ;
472 data:
473 STRING { $$.type = SDB_TYPE_STRING; $$.data.string = $1; }
474 |
475 INTEGER { $$ = $1; }
476 |
477 FLOAT { $$ = $1; }
478 |
479 interval { $$ = $1; }
480 ;
482 interval:
483 interval interval_elem
484 {
485 $$.data.datetime = $1.data.datetime + $2.data.datetime;
486 }
487 |
488 interval_elem { $$ = $1; }
489 ;
491 interval_elem:
492 INTEGER IDENTIFIER
493 {
494 sdb_time_t unit = 1;
496 unit = sdb_strpunit($2);
497 if (! unit) {
498 char errmsg[strlen($2) + 32];
499 snprintf(errmsg, sizeof(errmsg),
500 YY_("invalid time unit %s"), $2);
501 sdb_fe_yyerror(&yylloc, scanner, errmsg);
502 free($2); $2 = NULL;
503 YYABORT;
504 }
505 free($2); $2 = NULL;
507 $$.type = SDB_TYPE_DATETIME;
508 $$.data.datetime = (sdb_time_t)$1.data.integer * unit;
510 if ($1.data.integer < 0) {
511 sdb_fe_yyerror(&yylloc, scanner,
512 YY_("syntax error, negative intervals not supported"));
513 YYABORT;
514 }
515 }
516 ;
518 %%
520 void
521 sdb_fe_yyerror(YYLTYPE *lval, sdb_fe_yyscan_t scanner, const char *msg)
522 {
523 sdb_log(SDB_LOG_ERR, "frontend: parse error: %s", msg);
524 } /* sdb_fe_yyerror */
526 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */