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 HOSTS_T matching_clause filter_clause
327 {
328 /* TODO: support other types as well */
330 $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
331 conn_lookup_t, conn_lookup_destroy));
332 CONN_LOOKUP($$)->type = SDB_HOST;
333 CONN_LOOKUP($$)->matcher = CONN_MATCHER($3);
334 CONN_LOOKUP($$)->filter = CONN_MATCHER($4);
335 $$->cmd = CONNECTION_LOOKUP;
336 }
337 ;
339 matching_clause:
340 MATCHING condition { $$ = $2; }
341 |
342 /* empty */ { $$ = NULL; }
344 filter_clause:
345 FILTER condition { $$ = $2; }
346 |
347 /* empty */ { $$ = NULL; }
349 /*
350 * TIMESERIES <host>.<metric> [START <datetime>] [END <datetime>];
351 *
352 * Returns a time-series for the specified host's metric.
353 */
354 timeseries_statement:
355 TIMESERIES STRING '.' STRING start_clause end_clause
356 {
357 $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
358 conn_ts_t, conn_ts_destroy));
359 CONN_TS($$)->hostname = $2;
360 CONN_TS($$)->metric = $4;
361 CONN_TS($$)->opts.start = $5;
362 CONN_TS($$)->opts.end = $6;
363 $$->cmd = CONNECTION_TIMESERIES;
364 }
365 ;
367 start_clause:
368 START datetime { $$ = $2; }
369 |
370 /* empty */ { $$ = sdb_gettime() - SDB_INTERVAL_HOUR; }
372 end_clause:
373 END datetime { $$ = $2; }
374 |
375 /* empty */ { $$ = sdb_gettime(); }
377 /*
378 * Basic expressions.
379 */
381 condition:
382 matcher
383 {
384 if (! $1) {
385 /* TODO: improve error reporting */
386 sdb_fe_yyerror(&yylloc, scanner,
387 YY_("syntax error, invalid condition"));
388 YYABORT;
389 }
391 $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
392 conn_matcher_t, conn_matcher_destroy));
393 $$->cmd = CONNECTION_MATCHER;
394 CONN_MATCHER($$)->matcher = $1;
395 }
396 ;
398 matcher:
399 '(' matcher ')'
400 {
401 $$ = $2;
402 }
403 |
404 matcher AND matcher
405 {
406 $$ = sdb_store_con_matcher($1, $3);
407 sdb_object_deref(SDB_OBJ($1));
408 sdb_object_deref(SDB_OBJ($3));
409 }
410 |
411 matcher OR matcher
412 {
413 $$ = sdb_store_dis_matcher($1, $3);
414 sdb_object_deref(SDB_OBJ($1));
415 sdb_object_deref(SDB_OBJ($3));
416 }
417 |
418 NOT matcher
419 {
420 $$ = sdb_store_inv_matcher($2);
421 sdb_object_deref(SDB_OBJ($2));
422 }
423 |
424 compare_matcher
425 {
426 $$ = $1;
427 }
428 ;
430 compare_matcher:
431 expression cmp expression
432 {
433 sdb_store_matcher_op_cb cb = sdb_store_parse_matcher_op($2);
434 assert(cb); /* else, the grammar accepts invalid 'cmp' */
435 $$ = cb($1, $3);
436 sdb_object_deref(SDB_OBJ($1));
437 sdb_object_deref(SDB_OBJ($3));
438 }
439 |
440 ANY iterable cmp expression
441 {
442 $$ = name_iter_matcher(MATCHER_ANY, $2, $3, $4);
443 sdb_object_deref(SDB_OBJ($4));
444 }
445 |
446 ALL iterable cmp expression
447 {
448 $$ = name_iter_matcher(MATCHER_ALL, $2, $3, $4);
449 sdb_object_deref(SDB_OBJ($4));
450 }
451 |
452 expression IS NULL_T
453 {
454 $$ = sdb_store_isnull_matcher($1);
455 sdb_object_deref(SDB_OBJ($1));
456 }
457 |
458 expression IS NOT NULL_T
459 {
460 $$ = sdb_store_isnnull_matcher($1);
461 sdb_object_deref(SDB_OBJ($1));
462 }
463 |
464 expression IN expression
465 {
466 $$ = sdb_store_in_matcher($1, $3);
467 sdb_object_deref(SDB_OBJ($1));
468 sdb_object_deref(SDB_OBJ($3));
469 }
470 ;
472 expression:
473 '(' expression ')'
474 {
475 $$ = $2;
476 }
477 |
478 expression '+' expression
479 {
480 $$ = sdb_store_expr_create(SDB_DATA_ADD, $1, $3);
481 sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
482 sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
483 }
484 |
485 expression '-' expression
486 {
487 $$ = sdb_store_expr_create(SDB_DATA_SUB, $1, $3);
488 sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
489 sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
490 }
491 |
492 expression '*' expression
493 {
494 $$ = sdb_store_expr_create(SDB_DATA_MUL, $1, $3);
495 sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
496 sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
497 }
498 |
499 expression '/' expression
500 {
501 $$ = sdb_store_expr_create(SDB_DATA_DIV, $1, $3);
502 sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
503 sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
504 }
505 |
506 expression '%' expression
507 {
508 $$ = sdb_store_expr_create(SDB_DATA_MOD, $1, $3);
509 sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
510 sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
511 }
512 |
513 expression CONCAT expression
514 {
515 $$ = sdb_store_expr_create(SDB_DATA_CONCAT, $1, $3);
516 sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
517 sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
518 }
519 |
520 HOST_T
521 {
522 /* TODO: this only works as long as queries
523 * are limited to hosts */
524 $$ = sdb_store_expr_fieldvalue(SDB_FIELD_NAME);
525 }
526 |
527 field
528 {
529 $$ = sdb_store_expr_fieldvalue($1);
530 }
531 |
532 ATTRIBUTE_T '[' STRING ']'
533 {
534 $$ = sdb_store_expr_attrvalue($3);
535 free($3); $3 = NULL;
536 }
537 |
538 data
539 {
540 $$ = sdb_store_expr_constvalue(&$1);
541 sdb_data_free_datum(&$1);
542 }
543 ;
545 object_type:
546 HOST_T { $$ = SDB_HOST; }
547 |
548 SERVICE_T { $$ = SDB_SERVICE; }
549 |
550 METRIC_T { $$ = SDB_METRIC; }
551 ;
553 object_type_plural:
554 HOSTS_T { $$ = SDB_HOST; }
555 |
556 SERVICES_T { $$ = SDB_SERVICE; }
557 |
558 METRICS_T { $$ = SDB_METRIC; }
559 ;
561 iterable:
562 SERVICE_T { $$ = SDB_SERVICE; }
563 |
564 METRIC_T { $$ = SDB_METRIC; }
565 |
566 ATTRIBUTE_T { $$ = SDB_ATTRIBUTE; }
567 ;
569 field:
570 NAME_T { $$ = SDB_FIELD_NAME; }
571 |
572 LAST_UPDATE_T { $$ = SDB_FIELD_LAST_UPDATE; }
573 |
574 AGE_T { $$ = SDB_FIELD_AGE; }
575 |
576 INTERVAL_T { $$ = SDB_FIELD_INTERVAL; }
577 |
578 BACKEND_T { $$ = SDB_FIELD_BACKEND; }
579 ;
581 cmp:
582 CMP_EQUAL { $$ = "="; }
583 |
584 CMP_NEQUAL { $$ = "!="; }
585 |
586 CMP_REGEX { $$ = "=~"; }
587 |
588 CMP_NREGEX { $$ = "!~"; }
589 |
590 CMP_LT { $$ = "<"; }
591 |
592 CMP_LE { $$ = "<="; }
593 |
594 CMP_GE { $$ = ">="; }
595 |
596 CMP_GT { $$ = ">"; }
597 ;
599 data:
600 STRING { $$.type = SDB_TYPE_STRING; $$.data.string = $1; }
601 |
602 INTEGER { $$ = $1; }
603 |
604 FLOAT { $$ = $1; }
605 |
606 datetime { $$.type = SDB_TYPE_DATETIME; $$.data.datetime = $1; }
607 |
608 interval { $$ = $1; }
609 ;
611 datetime:
612 DATE TIME { $$ = $1 + $2; }
613 |
614 DATE { $$ = $1; }
615 |
616 TIME { $$ = $1; }
617 ;
619 interval:
620 interval interval_elem
621 {
622 $$.data.datetime = $1.data.datetime + $2.data.datetime;
623 }
624 |
625 interval_elem { $$ = $1; }
626 ;
628 interval_elem:
629 INTEGER IDENTIFIER
630 {
631 sdb_time_t unit = 1;
633 unit = sdb_strpunit($2);
634 if (! unit) {
635 sdb_fe_yyerrorf(&yylloc, scanner,
636 YY_("invalid time unit %s"), $2);
637 free($2); $2 = NULL;
638 YYABORT;
639 }
640 free($2); $2 = NULL;
642 $$.type = SDB_TYPE_DATETIME;
643 $$.data.datetime = (sdb_time_t)$1.data.integer * unit;
645 if ($1.data.integer < 0) {
646 sdb_fe_yyerror(&yylloc, scanner,
647 YY_("syntax error, negative intervals not supported"));
648 YYABORT;
649 }
650 }
651 ;
653 %%
655 void
656 sdb_fe_yyerror(YYLTYPE *lval, sdb_fe_yyscan_t scanner, const char *msg)
657 {
658 sdb_log(SDB_LOG_ERR, "frontend: parse error: %s", msg);
659 sdb_strbuf_sprintf(errbuf, "%s", msg);
660 } /* sdb_fe_yyerror */
662 void
663 sdb_fe_yyerrorf(YYLTYPE *lval, sdb_fe_yyscan_t scanner, const char *fmt, ...)
664 {
665 va_list ap, aq;
666 va_start(ap, fmt);
667 va_copy(aq, ap);
668 sdb_vlog(SDB_LOG_ERR, fmt, ap);
669 sdb_strbuf_vsprintf(errbuf, "%s", aq);
670 va_end(ap);
671 } /* sdb_fe_yyerrorf */
673 static sdb_store_matcher_t *
674 name_iter_matcher(int m_type, int type, const char *cmp,
675 sdb_store_expr_t *expr)
676 {
677 sdb_store_matcher_op_cb cb = sdb_store_parse_matcher_op(cmp);
678 sdb_store_expr_t *e;
679 sdb_store_matcher_t *m, *tmp = NULL;
680 assert(cb);
682 /* TODO: this only works as long as queries
683 * are limited to hosts */
684 if (type == SDB_HOST) {
685 return NULL;
686 }
688 e = sdb_store_expr_fieldvalue(SDB_FIELD_NAME);
689 m = cb(e, expr);
690 if (m_type == MATCHER_ANY)
691 tmp = sdb_store_any_matcher(type, m);
692 else if (m_type == MATCHER_ALL)
693 tmp = sdb_store_all_matcher(type, m);
694 sdb_object_deref(SDB_OBJ(m));
695 sdb_object_deref(SDB_OBJ(e));
696 return tmp;
697 } /* name_iter_matcher */
699 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */