Code

frontend/grammar: Added support for the IN operator.
[sysdb.git] / src / frontend / grammar.y
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 #define MODE_TO_STRING(m) \
60         (((m) == SDB_PARSE_DEFAULT) ? "statement" \
61                 : ((m) == SDB_PARSE_COND) ? "condition" \
62                 : ((m) == SDB_PARSE_EXPR) ? "expression" \
63                 : "UNKNOWN")
65 %}
67 %pure-parser
68 %lex-param {sdb_fe_yyscan_t scanner}
69 %parse-param {sdb_fe_yyscan_t scanner}
70 %locations
71 %error-verbose
72 %expect 0
73 %name-prefix "sdb_fe_yy"
75 %union {
76         const char *sstr; /* static string */
77         char *str;
79         sdb_data_t data;
80         sdb_time_t datetime;
82         sdb_llist_t     *list;
83         sdb_conn_node_t *node;
85         sdb_store_matcher_t *m;
86         sdb_store_expr_t *expr;
87 }
89 %start statements
91 %token SCANNER_ERROR
93 %token AND OR IS NOT MATCHING FILTER
94 %token CMP_EQUAL CMP_NEQUAL CMP_REGEX CMP_NREGEX
95 %token CMP_LT CMP_LE CMP_GE CMP_GT IN
96 %token CONCAT
98 %token START END
100 /* NULL token */
101 %token NULL_T
103 %token FETCH LIST LOOKUP TIMESERIES
105 %token <str> IDENTIFIER STRING
107 %token <data> INTEGER FLOAT
109 %token <datetime> DATE TIME
111 /* Precedence (lowest first): */
112 %left OR
113 %left AND
114 %right NOT
115 %left CMP_EQUAL CMP_NEQUAL
116 %left CMP_LT CMP_LE CMP_GE CMP_GT
117 %nonassoc CMP_REGEX CMP_NREGEX
118 %nonassoc IN
119 %left CONCAT
120 %nonassoc IS
121 %left '+' '-'
122 %left '*' '/' '%'
123 %left '[' ']'
124 %left '(' ')'
125 %left '.'
127 %type <list> statements
128 %type <node> statement
129         fetch_statement
130         list_statement
131         lookup_statement
132         timeseries_statement
133         matching_clause
134         filter_clause
135         condition
137 %type <m> matcher
138         compare_matcher
140 %type <expr> expression
142 %type <sstr> cmp
144 %type <data> data
145         interval interval_elem
147 %type <datetime> datetime
148         start_clause end_clause
150 %destructor { free($$); } <str>
151 %destructor { sdb_object_deref(SDB_OBJ($$)); } <node> <m> <expr>
152 %destructor { sdb_data_free_datum(&$$); } <data>
154 %%
156 statements:
157         statements ';' statement
158                 {
159                         /* only accepted in default parse mode */
160                         if (parser_mode != SDB_PARSE_DEFAULT) {
161                                 char errmsg[1024];
162                                 snprintf(errmsg, sizeof(errmsg),
163                                                 YY_("syntax error, unexpected statement, "
164                                                         "expecting %s"), MODE_TO_STRING(parser_mode));
165                                 sdb_fe_yyerror(&yylloc, scanner, errmsg);
166                                 sdb_object_deref(SDB_OBJ($3));
167                                 YYABORT;
168                         }
170                         if ($3) {
171                                 sdb_llist_append(pt, SDB_OBJ($3));
172                                 sdb_object_deref(SDB_OBJ($3));
173                         }
174                 }
175         |
176         statement
177                 {
178                         /* only accepted in default parse mode */
179                         if (parser_mode != SDB_PARSE_DEFAULT) {
180                                 char errmsg[1024];
181                                 snprintf(errmsg, sizeof(errmsg),
182                                                 YY_("syntax error, unexpected statement, "
183                                                         "expecting %s"), MODE_TO_STRING(parser_mode));
184                                 sdb_fe_yyerror(&yylloc, scanner, errmsg);
185                                 sdb_object_deref(SDB_OBJ($1));
186                                 YYABORT;
187                         }
189                         if ($1) {
190                                 sdb_llist_append(pt, SDB_OBJ($1));
191                                 sdb_object_deref(SDB_OBJ($1));
192                         }
193                 }
194         |
195         condition
196                 {
197                         /* only accepted in condition parse mode */
198                         if (! (parser_mode & SDB_PARSE_COND)) {
199                                 char errmsg[1024];
200                                 snprintf(errmsg, sizeof(errmsg),
201                                                 YY_("syntax error, unexpected condition, "
202                                                         "expecting %s"), MODE_TO_STRING(parser_mode));
203                                 sdb_fe_yyerror(&yylloc, scanner, errmsg);
204                                 sdb_object_deref(SDB_OBJ($1));
205                                 YYABORT;
206                         }
208                         if ($1) {
209                                 sdb_llist_append(pt, SDB_OBJ($1));
210                                 sdb_object_deref(SDB_OBJ($1));
211                         }
212                 }
213         |
214         expression
215                 {
216                         /* only accepted in expression parse mode */
217                         if (! (parser_mode & SDB_PARSE_EXPR)) {
218                                 char errmsg[1024];
219                                 snprintf(errmsg, sizeof(errmsg),
220                                                 YY_("syntax error, unexpected expression, "
221                                                         "expecting %s"), MODE_TO_STRING(parser_mode));
222                                 sdb_fe_yyerror(&yylloc, scanner, errmsg);
223                                 sdb_object_deref(SDB_OBJ($1));
224                                 YYABORT;
225                         }
227                         if ($1) {
228                                 sdb_conn_node_t *n;
229                                 n = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
230                                                         conn_expr_t, conn_expr_destroy));
231                                 n->cmd = CONNECTION_EXPR;
232                                 CONN_EXPR(n)->expr = $1;
234                                 sdb_llist_append(pt, SDB_OBJ(n));
235                                 sdb_object_deref(SDB_OBJ(n));
236                         }
237                 }
238         ;
240 statement:
241         fetch_statement
242         |
243         list_statement
244         |
245         lookup_statement
246         |
247         timeseries_statement
248         |
249         /* empty */
250                 {
251                         $$ = NULL;
252                 }
253         ;
255 /*
256  * FETCH <type> <hostname> [FILTER <condition>];
257  *
258  * Retrieve detailed information about a single host.
259  */
260 fetch_statement:
261         FETCH IDENTIFIER STRING filter_clause
262                 {
263                         /* TODO: support other types as well */
264                         if (strcasecmp($2, "host")) {
265                                 char errmsg[strlen($2) + 32];
266                                 snprintf(errmsg, sizeof(errmsg),
267                                                 YY_("unknown data-source %s"), $2);
268                                 sdb_fe_yyerror(&yylloc, scanner, errmsg);
269                                 free($2); $2 = NULL;
270                                 free($3); $3 = NULL;
271                                 sdb_object_deref(SDB_OBJ($4));
272                                 YYABORT;
273                         }
275                         $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
276                                                 conn_fetch_t, conn_fetch_destroy));
277                         CONN_FETCH($$)->type = SDB_HOST;
278                         CONN_FETCH($$)->name = $3;
279                         CONN_FETCH($$)->filter = CONN_MATCHER($4);
280                         $$->cmd = CONNECTION_FETCH;
281                         free($2); $2 = NULL;
282                 }
283         ;
285 /*
286  * LIST <type> [FILTER <condition>];
287  *
288  * Returns a list of all hosts in the store.
289  */
290 list_statement:
291         LIST IDENTIFIER filter_clause
292                 {
293                         int type = sdb_store_parse_object_type_plural($2);
294                         if (type < 0) {
295                                 char errmsg[strlen($2) + 32];
296                                 snprintf(errmsg, sizeof(errmsg),
297                                                 YY_("unknown data-source %s"), $2);
298                                 sdb_fe_yyerror(&yylloc, scanner, errmsg);
299                                 free($2); $2 = NULL;
300                                 sdb_object_deref(SDB_OBJ($3));
301                                 YYABORT;
302                         }
304                         $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
305                                                 conn_list_t, conn_list_destroy));
306                         CONN_LIST($$)->type = type;
307                         CONN_LIST($$)->filter = CONN_MATCHER($3);
308                         $$->cmd = CONNECTION_LIST;
309                         free($2); $2 = NULL;
310                 }
311         ;
313 /*
314  * LOOKUP <type> MATCHING <condition> [FILTER <condition>];
315  *
316  * Returns detailed information about <type> matching condition.
317  */
318 lookup_statement:
319         LOOKUP IDENTIFIER matching_clause filter_clause
320                 {
321                         /* TODO: support other types as well */
322                         if (strcasecmp($2, "hosts")) {
323                                 char errmsg[strlen($2) + 32];
324                                 snprintf(errmsg, sizeof(errmsg),
325                                                 YY_("unknown data-source %s"), $2);
326                                 sdb_fe_yyerror(&yylloc, scanner, errmsg);
327                                 free($2); $2 = NULL;
328                                 sdb_object_deref(SDB_OBJ($3));
329                                 sdb_object_deref(SDB_OBJ($4));
330                                 YYABORT;
331                         }
333                         $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
334                                                 conn_lookup_t, conn_lookup_destroy));
335                         CONN_LOOKUP($$)->type = SDB_HOST;
336                         CONN_LOOKUP($$)->matcher = CONN_MATCHER($3);
337                         CONN_LOOKUP($$)->filter = CONN_MATCHER($4);
338                         $$->cmd = CONNECTION_LOOKUP;
339                         free($2); $2 = NULL;
340                 }
341         ;
343 matching_clause:
344         MATCHING condition { $$ = $2; }
345         |
346         /* empty */ { $$ = NULL; }
348 filter_clause:
349         FILTER condition { $$ = $2; }
350         |
351         /* empty */ { $$ = NULL; }
353 /*
354  * TIMESERIES <host>.<metric> [START <datetime>] [END <datetime>];
355  *
356  * Returns a time-series for the specified host's metric.
357  */
358 timeseries_statement:
359         TIMESERIES STRING '.' STRING start_clause end_clause
360                 {
361                         $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
362                                                 conn_ts_t, conn_ts_destroy));
363                         CONN_TS($$)->hostname = $2;
364                         CONN_TS($$)->metric = $4;
365                         CONN_TS($$)->opts.start = $5;
366                         CONN_TS($$)->opts.end = $6;
367                         $$->cmd = CONNECTION_TIMESERIES;
368                 }
369         ;
371 start_clause:
372         START datetime { $$ = $2; }
373         |
374         /* empty */ { $$ = sdb_gettime() - SDB_INTERVAL_HOUR; }
376 end_clause:
377         END datetime { $$ = $2; }
378         |
379         /* empty */ { $$ = sdb_gettime(); }
381 /*
382  * Basic expressions.
383  */
385 condition:
386         matcher
387                 {
388                         if (! $1) {
389                                 /* TODO: improve error reporting */
390                                 sdb_fe_yyerror(&yylloc, scanner,
391                                                 YY_("syntax error, invalid condition"));
392                                 YYABORT;
393                         }
395                         $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
396                                                 conn_matcher_t, conn_matcher_destroy));
397                         $$->cmd = CONNECTION_MATCHER;
398                         CONN_MATCHER($$)->matcher = $1;
399                 }
400         ;
402 matcher:
403         '(' matcher ')'
404                 {
405                         $$ = $2;
406                 }
407         |
408         matcher AND matcher
409                 {
410                         $$ = sdb_store_con_matcher($1, $3);
411                         sdb_object_deref(SDB_OBJ($1));
412                         sdb_object_deref(SDB_OBJ($3));
413                 }
414         |
415         matcher OR matcher
416                 {
417                         $$ = sdb_store_dis_matcher($1, $3);
418                         sdb_object_deref(SDB_OBJ($1));
419                         sdb_object_deref(SDB_OBJ($3));
420                 }
421         |
422         NOT matcher
423                 {
424                         $$ = sdb_store_inv_matcher($2);
425                         sdb_object_deref(SDB_OBJ($2));
426                 }
427         |
428         compare_matcher
429                 {
430                         $$ = $1;
431                 }
432         ;
434 /*
435  * <object_type>.<object_attr> <cmp> <value>
436  *
437  * Parse matchers comparing object attributes with a value.
438  */
439 compare_matcher:
440         '.' IDENTIFIER cmp expression
441                 {
442                         $$ = sdb_store_matcher_parse_field_cmp($2, $3, $4);
443                         free($2); $2 = NULL;
444                         sdb_object_deref(SDB_OBJ($4));
445                 }
446         |
447         IDENTIFIER cmp expression
448                 {
449                         $$ = sdb_store_matcher_parse_cmp($1, NULL, $2, $3);
450                         free($1); $1 = NULL;
451                         sdb_object_deref(SDB_OBJ($3));
452                 }
453         |
454         IDENTIFIER '[' STRING ']' cmp expression
455                 {
456                         $$ = sdb_store_matcher_parse_cmp($1, $3, $5, $6);
457                         free($1); $1 = NULL;
458                         free($3); $3 = NULL;
459                         sdb_object_deref(SDB_OBJ($6));
460                 }
461         |
462         expression IS NULL_T
463                 {
464                         $$ = sdb_store_isnull_matcher($1);
465                         sdb_object_deref(SDB_OBJ($1));
466                 }
467         |
468         expression IS NOT NULL_T
469                 {
470                         $$ = sdb_store_isnnull_matcher($1);
471                         sdb_object_deref(SDB_OBJ($1));
472                 }
473         |
474         expression IN expression
475                 {
476                         $$ = sdb_store_in_matcher($1, $3);
477                         sdb_object_deref(SDB_OBJ($1));
478                         sdb_object_deref(SDB_OBJ($3));
479                 }
480         ;
482 expression:
483         '(' expression ')'
484                 {
485                         $$ = $2;
486                 }
487         |
488         expression '+' expression
489                 {
490                         $$ = sdb_store_expr_create(SDB_DATA_ADD, $1, $3);
491                         sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
492                         sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
493                 }
494         |
495         expression '-' expression
496                 {
497                         $$ = sdb_store_expr_create(SDB_DATA_SUB, $1, $3);
498                         sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
499                         sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
500                 }
501         |
502         expression '*' expression
503                 {
504                         $$ = sdb_store_expr_create(SDB_DATA_MUL, $1, $3);
505                         sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
506                         sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
507                 }
508         |
509         expression '/' expression
510                 {
511                         $$ = sdb_store_expr_create(SDB_DATA_DIV, $1, $3);
512                         sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
513                         sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
514                 }
515         |
516         expression '%' expression
517                 {
518                         $$ = sdb_store_expr_create(SDB_DATA_MOD, $1, $3);
519                         sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
520                         sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
521                 }
522         |
523         expression CONCAT expression
524                 {
525                         $$ = sdb_store_expr_create(SDB_DATA_CONCAT, $1, $3);
526                         sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
527                         sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
528                 }
529         |
530         '.' IDENTIFIER
531                 {
532                         int field = sdb_store_parse_field_name($2);
533                         free($2); $2 = NULL;
534                         $$ = sdb_store_expr_fieldvalue(field);
535                 }
536         |
537         IDENTIFIER '[' STRING ']'
538                 {
539                         if (strcasecmp($1, "attribute")) {
540                                 char errmsg[strlen($1) + strlen($3) + 32];
541                                 snprintf(errmsg, sizeof(errmsg),
542                                                 YY_("unknown value %s[%s]"), $1, $3);
543                                 sdb_fe_yyerror(&yylloc, scanner, errmsg);
544                                 free($1); $1 = NULL;
545                                 free($3); $3 = NULL;
546                                 YYABORT;
547                         }
548                         $$ = sdb_store_expr_attrvalue($3);
549                         free($1); $1 = NULL;
550                         free($3); $3 = NULL;
551                 }
552         |
553         data
554                 {
555                         $$ = sdb_store_expr_constvalue(&$1);
556                         sdb_data_free_datum(&$1);
557                 }
558         ;
560 cmp:
561         CMP_EQUAL { $$ = "="; }
562         |
563         CMP_NEQUAL { $$ = "!="; }
564         |
565         CMP_REGEX { $$ = "=~"; }
566         |
567         CMP_NREGEX { $$ = "!~"; }
568         |
569         CMP_LT { $$ = "<"; }
570         |
571         CMP_LE { $$ = "<="; }
572         |
573         CMP_GE { $$ = ">="; }
574         |
575         CMP_GT { $$ = ">"; }
576         ;
578 data:
579         STRING { $$.type = SDB_TYPE_STRING; $$.data.string = $1; }
580         |
581         INTEGER { $$ = $1; }
582         |
583         FLOAT { $$ = $1; }
584         |
585         datetime { $$.type = SDB_TYPE_DATETIME; $$.data.datetime = $1; }
586         |
587         interval { $$ = $1; }
588         ;
590 datetime:
591         DATE TIME { $$ = $1 + $2; }
592         |
593         DATE { $$ = $1; }
594         |
595         TIME { $$ = $1; }
596         ;
598 interval:
599         interval interval_elem
600                 {
601                         $$.data.datetime = $1.data.datetime + $2.data.datetime;
602                 }
603         |
604         interval_elem { $$ = $1; }
605         ;
607 interval_elem:
608         INTEGER IDENTIFIER
609                 {
610                         sdb_time_t unit = 1;
612                         unit = sdb_strpunit($2);
613                         if (! unit) {
614                                 char errmsg[strlen($2) + 32];
615                                 snprintf(errmsg, sizeof(errmsg),
616                                                 YY_("invalid time unit %s"), $2);
617                                 sdb_fe_yyerror(&yylloc, scanner, errmsg);
618                                 free($2); $2 = NULL;
619                                 YYABORT;
620                         }
621                         free($2); $2 = NULL;
623                         $$.type = SDB_TYPE_DATETIME;
624                         $$.data.datetime = (sdb_time_t)$1.data.integer * unit;
626                         if ($1.data.integer < 0) {
627                                 sdb_fe_yyerror(&yylloc, scanner,
628                                                 YY_("syntax error, negative intervals not supported"));
629                                 YYABORT;
630                         }
631                 }
632         ;
634 %%
636 void
637 sdb_fe_yyerror(YYLTYPE *lval, sdb_fe_yyscan_t scanner, const char *msg)
639         sdb_log(SDB_LOG_ERR, "frontend: parse error: %s", msg);
640 } /* sdb_fe_yyerror */
642 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */