Code

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