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);
67 /* quick access to the current parse tree */
68 #define pt sdb_fe_yyget_extra(scanner)->parsetree
70 /* quick access to the parser mode */
71 #define parser_mode sdb_fe_yyget_extra(scanner)->mode
73 #define MODE_TO_STRING(m) \
74 (((m) == SDB_PARSE_DEFAULT) ? "statement" \
75 : ((m) == SDB_PARSE_COND) ? "condition" \
76 : ((m) == SDB_PARSE_EXPR) ? "expression" \
77 : "UNKNOWN")
79 %}
81 %pure-parser
82 %lex-param {sdb_fe_yyscan_t scanner}
83 %parse-param {sdb_fe_yyscan_t scanner}
84 %locations
85 %error-verbose
86 %expect 0
87 %name-prefix "sdb_fe_yy"
89 %union {
90 const char *sstr; /* static string */
91 char *str;
92 int integer;
94 sdb_data_t data;
95 sdb_time_t datetime;
97 sdb_llist_t *list;
98 sdb_conn_node_t *node;
100 sdb_store_matcher_t *m;
101 sdb_store_expr_t *expr;
102 }
104 %start statements
106 %token SCANNER_ERROR
108 %token AND OR IS NOT MATCHING FILTER
109 %token CMP_EQUAL CMP_NEQUAL CMP_REGEX CMP_NREGEX
110 %token CMP_LT CMP_LE CMP_GE CMP_GT ALL ANY IN
111 %token CONCAT
113 %token HOST_T HOSTS_T SERVICE_T SERVICES_T METRIC_T METRICS_T
114 %token ATTRIBUTE_T ATTRIBUTES_T
115 %token NAME_T LAST_UPDATE_T AGE_T INTERVAL_T BACKEND_T
117 %token START END
119 /* NULL token */
120 %token NULL_T
122 %token FETCH LIST LOOKUP TIMESERIES
124 %token <str> IDENTIFIER STRING
126 %token <data> INTEGER FLOAT
128 %token <datetime> DATE TIME
130 /* Precedence (lowest first): */
131 %left OR
132 %left AND
133 %right NOT
134 %left CMP_EQUAL CMP_NEQUAL
135 %left CMP_LT CMP_LE CMP_GE CMP_GT
136 %nonassoc CMP_REGEX CMP_NREGEX
137 %nonassoc IN
138 %left CONCAT
139 %nonassoc IS
140 %left '+' '-'
141 %left '*' '/' '%'
142 %left '[' ']'
143 %left '(' ')'
144 %left '.'
146 %type <list> statements
147 %type <node> statement
148 fetch_statement
149 list_statement
150 lookup_statement
151 timeseries_statement
152 matching_clause
153 filter_clause
154 condition
156 %type <m> matcher
157 compare_matcher
159 %type <expr> expression
161 %type <integer> object_type_plural
162 %type <integer> iterable
163 %type <integer> field
165 %type <sstr> cmp
167 %type <data> data
168 interval interval_elem
170 %type <datetime> datetime
171 start_clause end_clause
173 %destructor { free($$); } <str>
174 %destructor { sdb_object_deref(SDB_OBJ($$)); } <node> <m> <expr>
175 %destructor { sdb_data_free_datum(&$$); } <data>
177 %%
179 statements:
180 statements ';' statement
181 {
182 /* only accepted in default parse mode */
183 if (parser_mode != SDB_PARSE_DEFAULT) {
184 char errmsg[1024];
185 snprintf(errmsg, sizeof(errmsg),
186 YY_("syntax error, unexpected statement, "
187 "expecting %s"), MODE_TO_STRING(parser_mode));
188 sdb_fe_yyerror(&yylloc, scanner, errmsg);
189 sdb_object_deref(SDB_OBJ($3));
190 YYABORT;
191 }
193 if ($3) {
194 sdb_llist_append(pt, SDB_OBJ($3));
195 sdb_object_deref(SDB_OBJ($3));
196 }
197 }
198 |
199 statement
200 {
201 /* only accepted in default parse mode */
202 if (parser_mode != SDB_PARSE_DEFAULT) {
203 char errmsg[1024];
204 snprintf(errmsg, sizeof(errmsg),
205 YY_("syntax error, unexpected statement, "
206 "expecting %s"), MODE_TO_STRING(parser_mode));
207 sdb_fe_yyerror(&yylloc, scanner, errmsg);
208 sdb_object_deref(SDB_OBJ($1));
209 YYABORT;
210 }
212 if ($1) {
213 sdb_llist_append(pt, SDB_OBJ($1));
214 sdb_object_deref(SDB_OBJ($1));
215 }
216 }
217 |
218 condition
219 {
220 /* only accepted in condition parse mode */
221 if (! (parser_mode & SDB_PARSE_COND)) {
222 char errmsg[1024];
223 snprintf(errmsg, sizeof(errmsg),
224 YY_("syntax error, unexpected condition, "
225 "expecting %s"), MODE_TO_STRING(parser_mode));
226 sdb_fe_yyerror(&yylloc, scanner, errmsg);
227 sdb_object_deref(SDB_OBJ($1));
228 YYABORT;
229 }
231 if ($1) {
232 sdb_llist_append(pt, SDB_OBJ($1));
233 sdb_object_deref(SDB_OBJ($1));
234 }
235 }
236 |
237 expression
238 {
239 /* only accepted in expression parse mode */
240 if (! (parser_mode & SDB_PARSE_EXPR)) {
241 char errmsg[1024];
242 snprintf(errmsg, sizeof(errmsg),
243 YY_("syntax error, unexpected expression, "
244 "expecting %s"), MODE_TO_STRING(parser_mode));
245 sdb_fe_yyerror(&yylloc, scanner, errmsg);
246 sdb_object_deref(SDB_OBJ($1));
247 YYABORT;
248 }
250 if ($1) {
251 sdb_conn_node_t *n;
252 n = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
253 conn_expr_t, conn_expr_destroy));
254 n->cmd = CONNECTION_EXPR;
255 CONN_EXPR(n)->expr = $1;
257 sdb_llist_append(pt, SDB_OBJ(n));
258 sdb_object_deref(SDB_OBJ(n));
259 }
260 }
261 ;
263 statement:
264 fetch_statement
265 |
266 list_statement
267 |
268 lookup_statement
269 |
270 timeseries_statement
271 |
272 /* empty */
273 {
274 $$ = NULL;
275 }
276 ;
278 /*
279 * FETCH <type> <hostname> [FILTER <condition>];
280 *
281 * Retrieve detailed information about a single host.
282 */
283 fetch_statement:
284 FETCH HOST_T STRING filter_clause
285 {
286 /* TODO: support other types as well */
288 $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
289 conn_fetch_t, conn_fetch_destroy));
290 CONN_FETCH($$)->type = SDB_HOST;
291 CONN_FETCH($$)->host = $3;
292 CONN_FETCH($$)->name = NULL;
293 CONN_FETCH($$)->filter = CONN_MATCHER($4);
294 $$->cmd = CONNECTION_FETCH;
295 }
296 ;
298 /*
299 * LIST <type> [FILTER <condition>];
300 *
301 * Returns a list of all hosts in the store.
302 */
303 list_statement:
304 LIST object_type_plural filter_clause
305 {
306 $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
307 conn_list_t, conn_list_destroy));
308 CONN_LIST($$)->type = $2;
309 CONN_LIST($$)->filter = CONN_MATCHER($3);
310 $$->cmd = CONNECTION_LIST;
311 }
312 ;
314 /*
315 * LOOKUP <type> MATCHING <condition> [FILTER <condition>];
316 *
317 * Returns detailed information about <type> matching condition.
318 */
319 lookup_statement:
320 LOOKUP HOSTS_T matching_clause filter_clause
321 {
322 /* TODO: support other types as well */
324 $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
325 conn_lookup_t, conn_lookup_destroy));
326 CONN_LOOKUP($$)->type = SDB_HOST;
327 CONN_LOOKUP($$)->matcher = CONN_MATCHER($3);
328 CONN_LOOKUP($$)->filter = CONN_MATCHER($4);
329 $$->cmd = CONNECTION_LOOKUP;
330 }
331 ;
333 matching_clause:
334 MATCHING condition { $$ = $2; }
335 |
336 /* empty */ { $$ = NULL; }
338 filter_clause:
339 FILTER condition { $$ = $2; }
340 |
341 /* empty */ { $$ = NULL; }
343 /*
344 * TIMESERIES <host>.<metric> [START <datetime>] [END <datetime>];
345 *
346 * Returns a time-series for the specified host's metric.
347 */
348 timeseries_statement:
349 TIMESERIES STRING '.' STRING start_clause end_clause
350 {
351 $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
352 conn_ts_t, conn_ts_destroy));
353 CONN_TS($$)->hostname = $2;
354 CONN_TS($$)->metric = $4;
355 CONN_TS($$)->opts.start = $5;
356 CONN_TS($$)->opts.end = $6;
357 $$->cmd = CONNECTION_TIMESERIES;
358 }
359 ;
361 start_clause:
362 START datetime { $$ = $2; }
363 |
364 /* empty */ { $$ = sdb_gettime() - SDB_INTERVAL_HOUR; }
366 end_clause:
367 END datetime { $$ = $2; }
368 |
369 /* empty */ { $$ = sdb_gettime(); }
371 /*
372 * Basic expressions.
373 */
375 condition:
376 matcher
377 {
378 if (! $1) {
379 /* TODO: improve error reporting */
380 sdb_fe_yyerror(&yylloc, scanner,
381 YY_("syntax error, invalid condition"));
382 YYABORT;
383 }
385 $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
386 conn_matcher_t, conn_matcher_destroy));
387 $$->cmd = CONNECTION_MATCHER;
388 CONN_MATCHER($$)->matcher = $1;
389 }
390 ;
392 matcher:
393 '(' matcher ')'
394 {
395 $$ = $2;
396 }
397 |
398 matcher AND matcher
399 {
400 $$ = sdb_store_con_matcher($1, $3);
401 sdb_object_deref(SDB_OBJ($1));
402 sdb_object_deref(SDB_OBJ($3));
403 }
404 |
405 matcher OR matcher
406 {
407 $$ = sdb_store_dis_matcher($1, $3);
408 sdb_object_deref(SDB_OBJ($1));
409 sdb_object_deref(SDB_OBJ($3));
410 }
411 |
412 NOT matcher
413 {
414 $$ = sdb_store_inv_matcher($2);
415 sdb_object_deref(SDB_OBJ($2));
416 }
417 |
418 compare_matcher
419 {
420 $$ = $1;
421 }
422 ;
424 compare_matcher:
425 expression cmp expression
426 {
427 sdb_store_matcher_op_cb cb = sdb_store_parse_matcher_op($2);
428 assert(cb); /* else, the grammar accepts invalid 'cmp' */
429 $$ = cb($1, $3);
430 sdb_object_deref(SDB_OBJ($1));
431 sdb_object_deref(SDB_OBJ($3));
432 }
433 |
434 ANY iterable cmp expression
435 {
436 $$ = name_iter_matcher(MATCHER_ANY, $2, $3, $4);
437 sdb_object_deref(SDB_OBJ($4));
438 }
439 |
440 ALL iterable cmp expression
441 {
442 $$ = name_iter_matcher(MATCHER_ALL, $2, $3, $4);
443 sdb_object_deref(SDB_OBJ($4));
444 }
445 |
446 expression IS NULL_T
447 {
448 $$ = sdb_store_isnull_matcher($1);
449 sdb_object_deref(SDB_OBJ($1));
450 }
451 |
452 expression IS NOT NULL_T
453 {
454 $$ = sdb_store_isnnull_matcher($1);
455 sdb_object_deref(SDB_OBJ($1));
456 }
457 |
458 expression IN expression
459 {
460 $$ = sdb_store_in_matcher($1, $3);
461 sdb_object_deref(SDB_OBJ($1));
462 sdb_object_deref(SDB_OBJ($3));
463 }
464 ;
466 expression:
467 '(' expression ')'
468 {
469 $$ = $2;
470 }
471 |
472 expression '+' expression
473 {
474 $$ = sdb_store_expr_create(SDB_DATA_ADD, $1, $3);
475 sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
476 sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
477 }
478 |
479 expression '-' expression
480 {
481 $$ = sdb_store_expr_create(SDB_DATA_SUB, $1, $3);
482 sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
483 sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
484 }
485 |
486 expression '*' expression
487 {
488 $$ = sdb_store_expr_create(SDB_DATA_MUL, $1, $3);
489 sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
490 sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
491 }
492 |
493 expression '/' expression
494 {
495 $$ = sdb_store_expr_create(SDB_DATA_DIV, $1, $3);
496 sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
497 sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
498 }
499 |
500 expression '%' expression
501 {
502 $$ = sdb_store_expr_create(SDB_DATA_MOD, $1, $3);
503 sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
504 sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
505 }
506 |
507 expression CONCAT expression
508 {
509 $$ = sdb_store_expr_create(SDB_DATA_CONCAT, $1, $3);
510 sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
511 sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
512 }
513 |
514 HOST_T
515 {
516 /* TODO: this only works as long as queries
517 * are limited to hosts */
518 $$ = sdb_store_expr_fieldvalue(SDB_FIELD_NAME);
519 }
520 |
521 field
522 {
523 $$ = sdb_store_expr_fieldvalue($1);
524 }
525 |
526 ATTRIBUTE_T '[' STRING ']'
527 {
528 $$ = sdb_store_expr_attrvalue($3);
529 free($3); $3 = NULL;
530 }
531 |
532 data
533 {
534 $$ = sdb_store_expr_constvalue(&$1);
535 sdb_data_free_datum(&$1);
536 }
537 ;
539 object_type_plural:
540 HOSTS_T { $$ = SDB_HOST; }
541 |
542 SERVICES_T { $$ = SDB_SERVICE; }
543 |
544 METRICS_T { $$ = SDB_METRIC; }
545 ;
547 iterable:
548 SERVICE_T { $$ = SDB_SERVICE; }
549 |
550 METRIC_T { $$ = SDB_METRIC; }
551 |
552 ATTRIBUTE_T { $$ = SDB_ATTRIBUTE; }
553 ;
555 field:
556 NAME_T { $$ = SDB_FIELD_NAME; }
557 |
558 LAST_UPDATE_T { $$ = SDB_FIELD_LAST_UPDATE; }
559 |
560 AGE_T { $$ = SDB_FIELD_AGE; }
561 |
562 INTERVAL_T { $$ = SDB_FIELD_INTERVAL; }
563 |
564 BACKEND_T { $$ = SDB_FIELD_BACKEND; }
565 ;
567 cmp:
568 CMP_EQUAL { $$ = "="; }
569 |
570 CMP_NEQUAL { $$ = "!="; }
571 |
572 CMP_REGEX { $$ = "=~"; }
573 |
574 CMP_NREGEX { $$ = "!~"; }
575 |
576 CMP_LT { $$ = "<"; }
577 |
578 CMP_LE { $$ = "<="; }
579 |
580 CMP_GE { $$ = ">="; }
581 |
582 CMP_GT { $$ = ">"; }
583 ;
585 data:
586 STRING { $$.type = SDB_TYPE_STRING; $$.data.string = $1; }
587 |
588 INTEGER { $$ = $1; }
589 |
590 FLOAT { $$ = $1; }
591 |
592 datetime { $$.type = SDB_TYPE_DATETIME; $$.data.datetime = $1; }
593 |
594 interval { $$ = $1; }
595 ;
597 datetime:
598 DATE TIME { $$ = $1 + $2; }
599 |
600 DATE { $$ = $1; }
601 |
602 TIME { $$ = $1; }
603 ;
605 interval:
606 interval interval_elem
607 {
608 $$.data.datetime = $1.data.datetime + $2.data.datetime;
609 }
610 |
611 interval_elem { $$ = $1; }
612 ;
614 interval_elem:
615 INTEGER IDENTIFIER
616 {
617 sdb_time_t unit = 1;
619 unit = sdb_strpunit($2);
620 if (! unit) {
621 char errmsg[strlen($2) + 32];
622 snprintf(errmsg, sizeof(errmsg),
623 YY_("invalid time unit %s"), $2);
624 sdb_fe_yyerror(&yylloc, scanner, errmsg);
625 free($2); $2 = NULL;
626 YYABORT;
627 }
628 free($2); $2 = NULL;
630 $$.type = SDB_TYPE_DATETIME;
631 $$.data.datetime = (sdb_time_t)$1.data.integer * unit;
633 if ($1.data.integer < 0) {
634 sdb_fe_yyerror(&yylloc, scanner,
635 YY_("syntax error, negative intervals not supported"));
636 YYABORT;
637 }
638 }
639 ;
641 %%
643 void
644 sdb_fe_yyerror(YYLTYPE *lval, sdb_fe_yyscan_t scanner, const char *msg)
645 {
646 sdb_log(SDB_LOG_ERR, "frontend: parse error: %s", msg);
647 } /* sdb_fe_yyerror */
649 static sdb_store_matcher_t *
650 name_iter_matcher(int m_type, int type, const char *cmp,
651 sdb_store_expr_t *expr)
652 {
653 sdb_store_matcher_op_cb cb = sdb_store_parse_matcher_op(cmp);
654 sdb_store_expr_t *e;
655 sdb_store_matcher_t *m, *tmp = NULL;
656 assert(cb);
658 /* TODO: this only works as long as queries
659 * are limited to hosts */
660 if (type == SDB_HOST) {
661 return NULL;
662 }
664 e = sdb_store_expr_fieldvalue(SDB_FIELD_NAME);
665 m = cb(e, expr);
666 if (m_type == MATCHER_ANY)
667 tmp = sdb_store_any_matcher(type, m);
668 else if (m_type == MATCHER_ALL)
669 tmp = sdb_store_all_matcher(type, m);
670 sdb_object_deref(SDB_OBJ(m));
671 sdb_object_deref(SDB_OBJ(e));
672 return tmp;
673 } /* name_iter_matcher */
675 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */