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 <stdio.h>
42 #include <string.h>
44 int
45 sdb_fe_yylex(YYSTYPE *yylval, YYLTYPE *yylloc, sdb_fe_yyscan_t yyscanner);
47 sdb_fe_yyextra_t *
48 sdb_fe_yyget_extra(sdb_fe_yyscan_t scanner);
50 void
51 sdb_fe_yyerror(YYLTYPE *lval, sdb_fe_yyscan_t scanner, const char *msg);
53 /* quick access to the current parse tree */
54 #define pt sdb_fe_yyget_extra(scanner)->parsetree
56 /* quick access to the parser mode */
57 #define parser_mode sdb_fe_yyget_extra(scanner)->mode
59 %}
61 %pure-parser
62 %lex-param {sdb_fe_yyscan_t scanner}
63 %parse-param {sdb_fe_yyscan_t scanner}
64 %locations
65 %error-verbose
66 %expect 0
67 %name-prefix "sdb_fe_yy"
69 %union {
70 const char *sstr; /* static string */
71 char *str;
73 sdb_data_t data;
74 sdb_time_t datetime;
76 sdb_llist_t *list;
77 sdb_conn_node_t *node;
79 sdb_store_matcher_t *m;
80 sdb_store_expr_t *expr;
81 }
83 %start statements
85 %token SCANNER_ERROR
87 %token AND OR IS NOT MATCHING FILTER
88 %token CMP_EQUAL CMP_NEQUAL CMP_REGEX CMP_NREGEX
89 %token CMP_LT CMP_LE CMP_GE CMP_GT
90 %token CONCAT
92 %token START END
94 /* NULL token */
95 %token NULL_T
97 %token FETCH LIST LOOKUP TIMESERIES
99 %token <str> IDENTIFIER STRING
101 %token <data> INTEGER FLOAT
103 %token <datetime> DATE TIME
105 /* Precedence (lowest first): */
106 %left OR
107 %left AND
108 %right NOT
109 %left CMP_EQUAL CMP_NEQUAL
110 %left CMP_LT CMP_LE CMP_GE CMP_GT
111 %nonassoc CMP_REGEX CMP_NREGEX
112 %left CONCAT
113 %nonassoc IS
114 %left '+' '-'
115 %left '*' '/' '%'
116 %left '(' ')'
117 %left '.'
119 %type <list> statements
120 %type <node> statement
121 fetch_statement
122 list_statement
123 lookup_statement
124 timeseries_statement
125 matching_clause
126 filter_clause
127 condition
129 %type <m> matcher
130 compare_matcher
132 %type <expr> expression
134 %type <sstr> op
136 %type <data> data
137 interval interval_elem
139 %type <datetime> datetime
140 start_clause end_clause
142 %destructor { free($$); } <str>
143 %destructor { sdb_object_deref(SDB_OBJ($$)); } <node> <m> <expr>
145 %%
147 statements:
148 statements ';' statement
149 {
150 /* only accept this in default parse mode */
151 if (parser_mode != SDB_PARSE_DEFAULT) {
152 sdb_fe_yyerror(&yylloc, scanner,
153 YY_("syntax error, unexpected statement, "
154 "expecting condition"));
155 sdb_object_deref(SDB_OBJ($3));
156 YYABORT;
157 }
159 if ($3) {
160 sdb_llist_append(pt, SDB_OBJ($3));
161 sdb_object_deref(SDB_OBJ($3));
162 }
163 }
164 |
165 statement
166 {
167 /* only accept this in default parse mode */
168 if (parser_mode != SDB_PARSE_DEFAULT) {
169 sdb_fe_yyerror(&yylloc, scanner,
170 YY_("syntax error, unexpected statement, "
171 "expecting condition"));
172 sdb_object_deref(SDB_OBJ($1));
173 YYABORT;
174 }
176 if ($1) {
177 sdb_llist_append(pt, SDB_OBJ($1));
178 sdb_object_deref(SDB_OBJ($1));
179 }
180 }
181 |
182 condition
183 {
184 /* only accept this in condition parse mode */
185 if (! (parser_mode & SDB_PARSE_COND)) {
186 sdb_fe_yyerror(&yylloc, scanner,
187 YY_("syntax error, unexpected condition, "
188 "expecting statement"));
189 sdb_object_deref(SDB_OBJ($1));
190 YYABORT;
191 }
193 if ($1) {
194 sdb_llist_append(pt, SDB_OBJ($1));
195 sdb_object_deref(SDB_OBJ($1));
196 }
197 }
198 ;
200 statement:
201 fetch_statement
202 |
203 list_statement
204 |
205 lookup_statement
206 |
207 timeseries_statement
208 |
209 /* empty */
210 {
211 $$ = NULL;
212 }
213 ;
215 /*
216 * FETCH <type> <hostname> [FILTER <condition>];
217 *
218 * Retrieve detailed information about a single host.
219 */
220 fetch_statement:
221 FETCH IDENTIFIER STRING filter_clause
222 {
223 /* TODO: support other types as well */
224 if (strcasecmp($2, "host")) {
225 char errmsg[strlen($2) + 32];
226 snprintf(errmsg, sizeof(errmsg),
227 YY_("unknown data-source %s"), $2);
228 sdb_fe_yyerror(&yylloc, scanner, errmsg);
229 free($2); $2 = NULL;
230 free($3); $3 = NULL;
231 sdb_object_deref(SDB_OBJ($4));
232 YYABORT;
233 }
235 $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
236 conn_fetch_t, conn_fetch_destroy));
237 CONN_FETCH($$)->type = SDB_HOST;
238 CONN_FETCH($$)->name = $3;
239 CONN_FETCH($$)->filter = CONN_MATCHER($4);
240 $$->cmd = CONNECTION_FETCH;
241 free($2); $2 = NULL;
242 }
243 ;
245 /*
246 * LIST <type> [FILTER <condition>];
247 *
248 * Returns a list of all hosts in the store.
249 */
250 list_statement:
251 LIST IDENTIFIER filter_clause
252 {
253 int type = sdb_store_parse_object_type_plural($2);
254 if (type < 0) {
255 char errmsg[strlen($2) + 32];
256 snprintf(errmsg, sizeof(errmsg),
257 YY_("unknown data-source %s"), $2);
258 sdb_fe_yyerror(&yylloc, scanner, errmsg);
259 free($2); $2 = NULL;
260 sdb_object_deref(SDB_OBJ($3));
261 YYABORT;
262 }
264 $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
265 conn_list_t, conn_list_destroy));
266 CONN_LIST($$)->type = type;
267 CONN_LIST($$)->filter = CONN_MATCHER($3);
268 $$->cmd = CONNECTION_LIST;
269 free($2); $2 = NULL;
270 }
271 ;
273 /*
274 * LOOKUP <type> MATCHING <condition> [FILTER <condition>];
275 *
276 * Returns detailed information about <type> matching condition.
277 */
278 lookup_statement:
279 LOOKUP IDENTIFIER matching_clause filter_clause
280 {
281 /* TODO: support other types as well */
282 if (strcasecmp($2, "hosts")) {
283 char errmsg[strlen($2) + 32];
284 snprintf(errmsg, sizeof(errmsg),
285 YY_("unknown data-source %s"), $2);
286 sdb_fe_yyerror(&yylloc, scanner, errmsg);
287 free($2); $2 = NULL;
288 sdb_object_deref(SDB_OBJ($3));
289 sdb_object_deref(SDB_OBJ($4));
290 YYABORT;
291 }
293 $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
294 conn_lookup_t, conn_lookup_destroy));
295 CONN_LOOKUP($$)->type = SDB_HOST;
296 CONN_LOOKUP($$)->matcher = CONN_MATCHER($3);
297 CONN_LOOKUP($$)->filter = CONN_MATCHER($4);
298 $$->cmd = CONNECTION_LOOKUP;
299 free($2); $2 = NULL;
300 }
301 ;
303 matching_clause:
304 MATCHING condition { $$ = $2; }
305 |
306 /* empty */ { $$ = NULL; }
308 filter_clause:
309 FILTER condition { $$ = $2; }
310 |
311 /* empty */ { $$ = NULL; }
313 /*
314 * TIMESERIES <host>.<metric> [START <datetime>] [END <datetime>];
315 *
316 * Returns a time-series for the specified host's metric.
317 */
318 timeseries_statement:
319 TIMESERIES STRING '.' STRING start_clause end_clause
320 {
321 $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
322 conn_ts_t, conn_ts_destroy));
323 CONN_TS($$)->hostname = $2;
324 CONN_TS($$)->metric = $4;
325 CONN_TS($$)->opts.start = $5;
326 CONN_TS($$)->opts.end = $6;
327 $$->cmd = CONNECTION_TIMESERIES;
328 }
329 ;
331 start_clause:
332 START datetime { $$ = $2; }
333 |
334 /* empty */ { $$ = sdb_gettime() - SDB_INTERVAL_HOUR; }
336 end_clause:
337 END datetime { $$ = $2; }
338 |
339 /* empty */ { $$ = sdb_gettime(); }
341 /*
342 * Basic expressions.
343 */
345 condition:
346 matcher
347 {
348 if (! $1) {
349 /* TODO: improve error reporting */
350 sdb_fe_yyerror(&yylloc, scanner,
351 YY_("syntax error, invalid condition"));
352 YYABORT;
353 }
355 $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
356 conn_matcher_t, conn_matcher_destroy));
357 $$->cmd = CONNECTION_EXPR;
358 CONN_MATCHER($$)->matcher = $1;
359 }
360 ;
362 matcher:
363 '(' matcher ')'
364 {
365 $$ = $2;
366 }
367 |
368 matcher AND matcher
369 {
370 $$ = sdb_store_con_matcher($1, $3);
371 sdb_object_deref(SDB_OBJ($1));
372 sdb_object_deref(SDB_OBJ($3));
373 }
374 |
375 matcher OR matcher
376 {
377 $$ = sdb_store_dis_matcher($1, $3);
378 sdb_object_deref(SDB_OBJ($1));
379 sdb_object_deref(SDB_OBJ($3));
380 }
381 |
382 NOT matcher
383 {
384 $$ = sdb_store_inv_matcher($2);
385 sdb_object_deref(SDB_OBJ($2));
386 }
387 |
388 compare_matcher
389 {
390 $$ = $1;
391 }
392 ;
394 /*
395 * <object_type>.<object_attr> <op> <value>
396 *
397 * Parse matchers comparing object attributes with a value.
398 */
399 compare_matcher:
400 ':' IDENTIFIER op expression
401 {
402 $$ = sdb_store_matcher_parse_field_cmp($2, $3, $4);
403 free($2); $2 = NULL;
404 sdb_object_deref(SDB_OBJ($4));
405 }
406 |
407 IDENTIFIER op expression
408 {
409 $$ = sdb_store_matcher_parse_cmp($1, NULL, $2, $3);
410 free($1); $1 = NULL;
411 sdb_object_deref(SDB_OBJ($3));
412 }
413 |
414 IDENTIFIER '.' IDENTIFIER op expression
415 {
416 $$ = sdb_store_matcher_parse_cmp($1, $3, $4, $5);
417 free($1); $1 = NULL;
418 free($3); $3 = NULL;
419 sdb_object_deref(SDB_OBJ($5));
420 }
421 |
422 IDENTIFIER '.' IDENTIFIER IS NULL_T
423 {
424 $$ = sdb_store_matcher_parse_cmp($1, $3, "IS", NULL);
425 free($1); $1 = NULL;
426 free($3); $3 = NULL;
427 }
428 |
429 IDENTIFIER '.' IDENTIFIER IS NOT NULL_T
430 {
431 sdb_store_matcher_t *m;
432 m = sdb_store_matcher_parse_cmp($1, $3, "IS", NULL);
433 free($1); $1 = NULL;
434 free($3); $3 = NULL;
436 /* sdb_store_inv_matcher return NULL if m==NULL */
437 $$ = sdb_store_inv_matcher(m);
438 sdb_object_deref(SDB_OBJ(m));
439 }
440 ;
442 expression:
443 '(' expression ')'
444 {
445 $$ = $2;
446 }
447 |
448 expression '+' expression
449 {
450 $$ = sdb_store_expr_create(SDB_DATA_ADD, $1, $3);
451 sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
452 sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
453 }
454 |
455 expression '-' expression
456 {
457 $$ = sdb_store_expr_create(SDB_DATA_SUB, $1, $3);
458 sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
459 sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
460 }
461 |
462 expression '*' expression
463 {
464 $$ = sdb_store_expr_create(SDB_DATA_MUL, $1, $3);
465 sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
466 sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
467 }
468 |
469 expression '/' expression
470 {
471 $$ = sdb_store_expr_create(SDB_DATA_DIV, $1, $3);
472 sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
473 sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
474 }
475 |
476 expression '%' expression
477 {
478 $$ = sdb_store_expr_create(SDB_DATA_MOD, $1, $3);
479 sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
480 sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
481 }
482 |
483 ':' IDENTIFIER
484 {
485 int field = sdb_store_parse_field_name($2);
486 free($2); $2 = NULL;
487 $$ = sdb_store_expr_fieldvalue(field);
488 }
489 |
490 data
491 {
492 $$ = sdb_store_expr_constvalue(&$1);
493 sdb_data_free_datum(&$1);
494 }
495 ;
497 op:
498 CMP_EQUAL { $$ = "="; }
499 |
500 CMP_NEQUAL { $$ = "!="; }
501 |
502 CMP_REGEX { $$ = "=~"; }
503 |
504 CMP_NREGEX { $$ = "!~"; }
505 |
506 CMP_LT { $$ = "<"; }
507 |
508 CMP_LE { $$ = "<="; }
509 |
510 CMP_GE { $$ = ">="; }
511 |
512 CMP_GT { $$ = ">"; }
513 ;
515 data:
516 STRING { $$.type = SDB_TYPE_STRING; $$.data.string = $1; }
517 |
518 INTEGER { $$ = $1; }
519 |
520 FLOAT { $$ = $1; }
521 |
522 datetime { $$.type = SDB_TYPE_DATETIME; $$.data.datetime = $1; }
523 |
524 interval { $$ = $1; }
525 ;
527 datetime:
528 DATE TIME { $$ = $1 + $2; }
529 |
530 DATE { $$ = $1; }
531 |
532 TIME { $$ = $1; }
533 ;
535 interval:
536 interval interval_elem
537 {
538 $$.data.datetime = $1.data.datetime + $2.data.datetime;
539 }
540 |
541 interval_elem { $$ = $1; }
542 ;
544 interval_elem:
545 INTEGER IDENTIFIER
546 {
547 sdb_time_t unit = 1;
549 unit = sdb_strpunit($2);
550 if (! unit) {
551 char errmsg[strlen($2) + 32];
552 snprintf(errmsg, sizeof(errmsg),
553 YY_("invalid time unit %s"), $2);
554 sdb_fe_yyerror(&yylloc, scanner, errmsg);
555 free($2); $2 = NULL;
556 YYABORT;
557 }
558 free($2); $2 = NULL;
560 $$.type = SDB_TYPE_DATETIME;
561 $$.data.datetime = (sdb_time_t)$1.data.integer * unit;
563 if ($1.data.integer < 0) {
564 sdb_fe_yyerror(&yylloc, scanner,
565 YY_("syntax error, negative intervals not supported"));
566 YYABORT;
567 }
568 }
569 ;
571 %%
573 void
574 sdb_fe_yyerror(YYLTYPE *lval, sdb_fe_yyscan_t scanner, const char *msg)
575 {
576 sdb_log(SDB_LOG_ERR, "frontend: parse error: %s", msg);
577 } /* sdb_fe_yyerror */
579 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */