2e5274469057b12ac195997982e4018df727268e
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"
36 #include "core/time.h"
38 #include "utils/error.h"
39 #include "utils/llist.h"
41 #include <assert.h>
43 #include <stdio.h>
44 #include <string.h>
46 /*
47 * private helper functions
48 */
50 static sdb_store_matcher_t *
51 name_iter_matcher(int m_type, int type, const char *cmp,
52 sdb_store_expr_t *expr);
54 /*
55 * public API
56 */
58 int
59 sdb_fe_yylex(YYSTYPE *yylval, YYLTYPE *yylloc, sdb_fe_yyscan_t yyscanner);
61 sdb_fe_yyextra_t *
62 sdb_fe_yyget_extra(sdb_fe_yyscan_t scanner);
64 void
65 sdb_fe_yyerror(YYLTYPE *lval, sdb_fe_yyscan_t scanner, const char *msg);
66 void
67 sdb_fe_yyerrorf(YYLTYPE *lval, sdb_fe_yyscan_t scanner, const char *fmt, ...);
69 /* quick access to the current parse tree */
70 #define pt sdb_fe_yyget_extra(scanner)->parsetree
72 /* quick access to the parser mode */
73 #define parser_mode sdb_fe_yyget_extra(scanner)->mode
75 /* quick access to the parser's error buffer */
76 #define errbuf sdb_fe_yyget_extra(scanner)->errbuf
78 #define MODE_TO_STRING(m) \
79 (((m) == SDB_PARSE_DEFAULT) ? "statement" \
80 : ((m) == SDB_PARSE_COND) ? "condition" \
81 : ((m) == SDB_PARSE_EXPR) ? "expression" \
82 : "UNKNOWN")
84 %}
86 %pure-parser
87 %lex-param {sdb_fe_yyscan_t scanner}
88 %parse-param {sdb_fe_yyscan_t scanner}
89 %locations
90 %error-verbose
91 %expect 0
92 %name-prefix "sdb_fe_yy"
94 %union {
95 const char *sstr; /* static string */
96 char *str;
97 int integer;
99 sdb_data_t data;
100 sdb_time_t datetime;
102 sdb_llist_t *list;
103 sdb_conn_node_t *node;
105 sdb_store_matcher_t *m;
106 sdb_store_expr_t *expr;
107 }
109 %start statements
111 %token SCANNER_ERROR
113 %token AND OR IS NOT MATCHING FILTER
114 %token CMP_EQUAL CMP_NEQUAL CMP_REGEX CMP_NREGEX
115 %token CMP_LT CMP_LE CMP_GE CMP_GT ALL ANY IN
116 %token CONCAT
118 %token HOST_T HOSTS_T SERVICE_T SERVICES_T METRIC_T METRICS_T
119 %token ATTRIBUTE_T ATTRIBUTES_T
120 %token NAME_T LAST_UPDATE_T AGE_T INTERVAL_T BACKEND_T
122 %token START END
124 /* NULL token */
125 %token NULL_T
127 %token FETCH LIST LOOKUP TIMESERIES
129 %token <str> IDENTIFIER STRING
131 %token <data> INTEGER FLOAT
133 %token <datetime> DATE TIME
135 /* Precedence (lowest first): */
136 %left OR
137 %left AND
138 %right NOT
139 %left CMP_EQUAL CMP_NEQUAL
140 %left CMP_LT CMP_LE CMP_GE CMP_GT
141 %nonassoc CMP_REGEX CMP_NREGEX
142 %nonassoc IN
143 %left CONCAT
144 %nonassoc IS
145 %left '+' '-'
146 %left '*' '/' '%'
147 %left '[' ']'
148 %left '(' ')'
149 %left '.'
151 %type <list> statements
152 %type <node> statement
153 fetch_statement
154 list_statement
155 lookup_statement
156 timeseries_statement
157 matching_clause
158 filter_clause
159 condition
161 %type <m> matcher
162 compare_matcher
164 %type <expr> expression
166 %type <integer> object_type object_type_plural
167 %type <integer> iterable
168 %type <integer> field
170 %type <sstr> cmp
172 %type <data> data
173 interval interval_elem
175 %type <datetime> datetime
176 start_clause end_clause
178 %destructor { free($$); } <str>
179 %destructor { sdb_object_deref(SDB_OBJ($$)); } <node> <m> <expr>
180 %destructor { sdb_data_free_datum(&$$); } <data>
182 %%
184 statements:
185 statements ';' statement
186 {
187 /* only accepted in default parse mode */
188 if (parser_mode != SDB_PARSE_DEFAULT) {
189 sdb_fe_yyerrorf(&yylloc, scanner,
190 YY_("syntax error, unexpected statement, "
191 "expecting %s"), MODE_TO_STRING(parser_mode));
192 sdb_object_deref(SDB_OBJ($3));
193 YYABORT;
194 }
196 if ($3) {
197 sdb_llist_append(pt, SDB_OBJ($3));
198 sdb_object_deref(SDB_OBJ($3));
199 }
200 }
201 |
202 statement
203 {
204 /* only accepted in default parse mode */
205 if (parser_mode != SDB_PARSE_DEFAULT) {
206 sdb_fe_yyerrorf(&yylloc, scanner,
207 YY_("syntax error, unexpected statement, "
208 "expecting %s"), MODE_TO_STRING(parser_mode));
209 sdb_object_deref(SDB_OBJ($1));
210 YYABORT;
211 }
213 if ($1) {
214 sdb_llist_append(pt, SDB_OBJ($1));
215 sdb_object_deref(SDB_OBJ($1));
216 }
217 }
218 |
219 condition
220 {
221 /* only accepted in condition parse mode */
222 if (! (parser_mode & SDB_PARSE_COND)) {
223 sdb_fe_yyerrorf(&yylloc, scanner,
224 YY_("syntax error, unexpected condition, "
225 "expecting %s"), MODE_TO_STRING(parser_mode));
226 sdb_object_deref(SDB_OBJ($1));
227 YYABORT;
228 }
230 if ($1) {
231 sdb_llist_append(pt, SDB_OBJ($1));
232 sdb_object_deref(SDB_OBJ($1));
233 }
234 }
235 |
236 expression
237 {
238 /* only accepted in expression parse mode */
239 if (! (parser_mode & SDB_PARSE_EXPR)) {
240 sdb_fe_yyerrorf(&yylloc, scanner,
241 YY_("syntax error, unexpected expression, "
242 "expecting %s"), MODE_TO_STRING(parser_mode));
243 sdb_object_deref(SDB_OBJ($1));
244 YYABORT;
245 }
247 if ($1) {
248 sdb_conn_node_t *n;
249 n = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
250 conn_expr_t, conn_expr_destroy));
251 n->cmd = CONNECTION_EXPR;
252 CONN_EXPR(n)->expr = $1;
254 sdb_llist_append(pt, SDB_OBJ(n));
255 sdb_object_deref(SDB_OBJ(n));
256 }
257 }
258 ;
260 statement:
261 fetch_statement
262 |
263 list_statement
264 |
265 lookup_statement
266 |
267 timeseries_statement
268 |
269 /* empty */
270 {
271 $$ = NULL;
272 }
273 ;
275 /*
276 * FETCH <type> <hostname> [FILTER <condition>];
277 *
278 * Retrieve detailed information about a single host.
279 */
280 fetch_statement:
281 FETCH object_type STRING filter_clause
282 {
283 $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
284 conn_fetch_t, conn_fetch_destroy));
285 CONN_FETCH($$)->type = $2;
286 CONN_FETCH($$)->host = $3;
287 CONN_FETCH($$)->name = NULL;
288 CONN_FETCH($$)->filter = CONN_MATCHER($4);
289 $$->cmd = CONNECTION_FETCH;
290 }
291 |
292 FETCH object_type STRING '.' STRING filter_clause
293 {
294 $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
295 conn_fetch_t, conn_fetch_destroy));
296 CONN_FETCH($$)->type = $2;
297 CONN_FETCH($$)->host = $3;
298 CONN_FETCH($$)->name = $5;
299 CONN_FETCH($$)->filter = CONN_MATCHER($6);
300 $$->cmd = CONNECTION_FETCH;
301 }
302 ;
304 /*
305 * LIST <type> [FILTER <condition>];
306 *
307 * Returns a list of all hosts in the store.
308 */
309 list_statement:
310 LIST object_type_plural filter_clause
311 {
312 $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
313 conn_list_t, conn_list_destroy));
314 CONN_LIST($$)->type = $2;
315 CONN_LIST($$)->filter = CONN_MATCHER($3);
316 $$->cmd = CONNECTION_LIST;
317 }
318 ;
320 /*
321 * LOOKUP <type> MATCHING <condition> [FILTER <condition>];
322 *
323 * Returns detailed information about <type> matching condition.
324 */
325 lookup_statement:
326 LOOKUP object_type_plural matching_clause filter_clause
327 {
328 $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
329 conn_lookup_t, conn_lookup_destroy));
330 CONN_LOOKUP($$)->type = $2;
331 CONN_LOOKUP($$)->matcher = CONN_MATCHER($3);
332 CONN_LOOKUP($$)->filter = CONN_MATCHER($4);
333 $$->cmd = CONNECTION_LOOKUP;
334 }
335 ;
337 matching_clause:
338 MATCHING condition { $$ = $2; }
339 |
340 /* empty */ { $$ = NULL; }
342 filter_clause:
343 FILTER condition { $$ = $2; }
344 |
345 /* empty */ { $$ = NULL; }
347 /*
348 * TIMESERIES <host>.<metric> [START <datetime>] [END <datetime>];
349 *
350 * Returns a time-series for the specified host's metric.
351 */
352 timeseries_statement:
353 TIMESERIES STRING '.' STRING start_clause end_clause
354 {
355 $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
356 conn_ts_t, conn_ts_destroy));
357 CONN_TS($$)->hostname = $2;
358 CONN_TS($$)->metric = $4;
359 CONN_TS($$)->opts.start = $5;
360 CONN_TS($$)->opts.end = $6;
361 $$->cmd = CONNECTION_TIMESERIES;
362 }
363 ;
365 start_clause:
366 START datetime { $$ = $2; }
367 |
368 /* empty */ { $$ = sdb_gettime() - SDB_INTERVAL_HOUR; }
370 end_clause:
371 END datetime { $$ = $2; }
372 |
373 /* empty */ { $$ = sdb_gettime(); }
375 /*
376 * Basic expressions.
377 */
379 condition:
380 matcher
381 {
382 if (! $1) {
383 /* TODO: improve error reporting */
384 sdb_fe_yyerror(&yylloc, scanner,
385 YY_("syntax error, invalid condition"));
386 YYABORT;
387 }
389 $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
390 conn_matcher_t, conn_matcher_destroy));
391 $$->cmd = CONNECTION_MATCHER;
392 CONN_MATCHER($$)->matcher = $1;
393 }
394 ;
396 matcher:
397 '(' matcher ')'
398 {
399 $$ = $2;
400 }
401 |
402 matcher AND matcher
403 {
404 $$ = sdb_store_con_matcher($1, $3);
405 sdb_object_deref(SDB_OBJ($1));
406 sdb_object_deref(SDB_OBJ($3));
407 }
408 |
409 matcher OR matcher
410 {
411 $$ = sdb_store_dis_matcher($1, $3);
412 sdb_object_deref(SDB_OBJ($1));
413 sdb_object_deref(SDB_OBJ($3));
414 }
415 |
416 NOT matcher
417 {
418 $$ = sdb_store_inv_matcher($2);
419 sdb_object_deref(SDB_OBJ($2));
420 }
421 |
422 compare_matcher
423 {
424 $$ = $1;
425 }
426 ;
428 compare_matcher:
429 expression cmp expression
430 {
431 sdb_store_matcher_op_cb cb = sdb_store_parse_matcher_op($2);
432 assert(cb); /* else, the grammar accepts invalid 'cmp' */
433 $$ = cb($1, $3);
434 sdb_object_deref(SDB_OBJ($1));
435 sdb_object_deref(SDB_OBJ($3));
436 }
437 |
438 ANY iterable cmp expression
439 {
440 $$ = name_iter_matcher(MATCHER_ANY, $2, $3, $4);
441 sdb_object_deref(SDB_OBJ($4));
442 }
443 |
444 ALL iterable cmp expression
445 {
446 $$ = name_iter_matcher(MATCHER_ALL, $2, $3, $4);
447 sdb_object_deref(SDB_OBJ($4));
448 }
449 |
450 expression IS NULL_T
451 {
452 $$ = sdb_store_isnull_matcher($1);
453 sdb_object_deref(SDB_OBJ($1));
454 }
455 |
456 expression IS NOT NULL_T
457 {
458 $$ = sdb_store_isnnull_matcher($1);
459 sdb_object_deref(SDB_OBJ($1));
460 }
461 |
462 expression IN expression
463 {
464 $$ = sdb_store_in_matcher($1, $3);
465 sdb_object_deref(SDB_OBJ($1));
466 sdb_object_deref(SDB_OBJ($3));
467 }
468 ;
470 expression:
471 '(' expression ')'
472 {
473 $$ = $2;
474 }
475 |
476 expression '+' expression
477 {
478 $$ = sdb_store_expr_create(SDB_DATA_ADD, $1, $3);
479 sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
480 sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
481 }
482 |
483 expression '-' expression
484 {
485 $$ = sdb_store_expr_create(SDB_DATA_SUB, $1, $3);
486 sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
487 sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
488 }
489 |
490 expression '*' expression
491 {
492 $$ = sdb_store_expr_create(SDB_DATA_MUL, $1, $3);
493 sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
494 sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
495 }
496 |
497 expression '/' expression
498 {
499 $$ = sdb_store_expr_create(SDB_DATA_DIV, $1, $3);
500 sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
501 sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
502 }
503 |
504 expression '%' expression
505 {
506 $$ = sdb_store_expr_create(SDB_DATA_MOD, $1, $3);
507 sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
508 sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
509 }
510 |
511 expression CONCAT expression
512 {
513 $$ = sdb_store_expr_create(SDB_DATA_CONCAT, $1, $3);
514 sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
515 sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
516 }
517 |
518 HOST_T
519 {
520 /* XXX: this doesn't work correctly when not
521 * querying hosts => use <type>.<field> instead
522 * and let the analyzer verify <type> */
523 $$ = sdb_store_expr_fieldvalue(SDB_FIELD_NAME);
524 }
525 |
526 field
527 {
528 $$ = sdb_store_expr_fieldvalue($1);
529 }
530 |
531 ATTRIBUTE_T '[' STRING ']'
532 {
533 $$ = sdb_store_expr_attrvalue($3);
534 free($3); $3 = NULL;
535 }
536 |
537 data
538 {
539 $$ = sdb_store_expr_constvalue(&$1);
540 sdb_data_free_datum(&$1);
541 }
542 ;
544 object_type:
545 HOST_T { $$ = SDB_HOST; }
546 |
547 SERVICE_T { $$ = SDB_SERVICE; }
548 |
549 METRIC_T { $$ = SDB_METRIC; }
550 ;
552 object_type_plural:
553 HOSTS_T { $$ = SDB_HOST; }
554 |
555 SERVICES_T { $$ = SDB_SERVICE; }
556 |
557 METRICS_T { $$ = SDB_METRIC; }
558 ;
560 iterable:
561 SERVICE_T { $$ = SDB_SERVICE; }
562 |
563 METRIC_T { $$ = SDB_METRIC; }
564 |
565 ATTRIBUTE_T { $$ = SDB_ATTRIBUTE; }
566 ;
568 field:
569 NAME_T { $$ = SDB_FIELD_NAME; }
570 |
571 LAST_UPDATE_T { $$ = SDB_FIELD_LAST_UPDATE; }
572 |
573 AGE_T { $$ = SDB_FIELD_AGE; }
574 |
575 INTERVAL_T { $$ = SDB_FIELD_INTERVAL; }
576 |
577 BACKEND_T { $$ = SDB_FIELD_BACKEND; }
578 ;
580 cmp:
581 CMP_EQUAL { $$ = "="; }
582 |
583 CMP_NEQUAL { $$ = "!="; }
584 |
585 CMP_REGEX { $$ = "=~"; }
586 |
587 CMP_NREGEX { $$ = "!~"; }
588 |
589 CMP_LT { $$ = "<"; }
590 |
591 CMP_LE { $$ = "<="; }
592 |
593 CMP_GE { $$ = ">="; }
594 |
595 CMP_GT { $$ = ">"; }
596 ;
598 data:
599 STRING { $$.type = SDB_TYPE_STRING; $$.data.string = $1; }
600 |
601 INTEGER { $$ = $1; }
602 |
603 FLOAT { $$ = $1; }
604 |
605 datetime { $$.type = SDB_TYPE_DATETIME; $$.data.datetime = $1; }
606 |
607 interval { $$ = $1; }
608 ;
610 datetime:
611 DATE TIME { $$ = $1 + $2; }
612 |
613 DATE { $$ = $1; }
614 |
615 TIME { $$ = $1; }
616 ;
618 interval:
619 interval interval_elem
620 {
621 $$.data.datetime = $1.data.datetime + $2.data.datetime;
622 }
623 |
624 interval_elem { $$ = $1; }
625 ;
627 interval_elem:
628 INTEGER IDENTIFIER
629 {
630 sdb_time_t unit = 1;
632 unit = sdb_strpunit($2);
633 if (! unit) {
634 sdb_fe_yyerrorf(&yylloc, scanner,
635 YY_("invalid time unit %s"), $2);
636 free($2); $2 = NULL;
637 YYABORT;
638 }
639 free($2); $2 = NULL;
641 $$.type = SDB_TYPE_DATETIME;
642 $$.data.datetime = (sdb_time_t)$1.data.integer * unit;
644 if ($1.data.integer < 0) {
645 sdb_fe_yyerror(&yylloc, scanner,
646 YY_("syntax error, negative intervals not supported"));
647 YYABORT;
648 }
649 }
650 ;
652 %%
654 void
655 sdb_fe_yyerror(YYLTYPE *lval, sdb_fe_yyscan_t scanner, const char *msg)
656 {
657 sdb_log(SDB_LOG_ERR, "frontend: parse error: %s", msg);
658 sdb_strbuf_sprintf(errbuf, "%s", msg);
659 } /* sdb_fe_yyerror */
661 void
662 sdb_fe_yyerrorf(YYLTYPE *lval, sdb_fe_yyscan_t scanner, const char *fmt, ...)
663 {
664 va_list ap, aq;
665 va_start(ap, fmt);
666 va_copy(aq, ap);
667 sdb_vlog(SDB_LOG_ERR, fmt, ap);
668 sdb_strbuf_vsprintf(errbuf, "%s", aq);
669 va_end(ap);
670 } /* sdb_fe_yyerrorf */
672 static sdb_store_matcher_t *
673 name_iter_matcher(int m_type, int type, const char *cmp,
674 sdb_store_expr_t *expr)
675 {
676 sdb_store_matcher_op_cb cb = sdb_store_parse_matcher_op(cmp);
677 sdb_store_expr_t *e;
678 sdb_store_matcher_t *m, *tmp = NULL;
679 assert(cb);
681 /* TODO: this only works as long as queries
682 * are limited to hosts */
683 if (type == SDB_HOST) {
684 return NULL;
685 }
687 e = sdb_store_expr_fieldvalue(SDB_FIELD_NAME);
688 m = cb(e, expr);
689 if (m_type == MATCHER_ANY)
690 tmp = sdb_store_any_matcher(type, m);
691 else if (m_type == MATCHER_ALL)
692 tmp = sdb_store_all_matcher(type, m);
693 sdb_object_deref(SDB_OBJ(m));
694 sdb_object_deref(SDB_OBJ(e));
695 return tmp;
696 } /* name_iter_matcher */
698 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */