Code

store, frontend: Make IS (NOT) NULL an unary operator on expressions.
[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 '[' IDENTIFIER ']' 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         IDENTIFIER '[' IDENTIFIER ']' IS NULL_T
462                 {
463                         sdb_store_expr_t *expr;
465                         if (strcasecmp($1, "attribute")) {
466                                 char errmsg[strlen($1) + strlen($3) + 32];
467                                 snprintf(errmsg, sizeof(errmsg),
468                                                 YY_("unknown value %s[%s]"), $1);
469                                 sdb_fe_yyerror(&yylloc, scanner, errmsg);
470                                 free($1); $1 = NULL;
471                                 free($3); $3 = NULL;
472                                 YYABORT;
473                         }
475                         expr = sdb_store_expr_attrvalue($3);
476                         $$ = sdb_store_isnull_matcher(expr);
477                         sdb_object_deref(SDB_OBJ(expr));
478                         free($1); $1 = NULL;
479                         free($3); $3 = NULL;
480                 }
481         |
482         IDENTIFIER '[' IDENTIFIER ']' IS NOT NULL_T
483                 {
484                         sdb_store_expr_t *expr;
486                         if (strcasecmp($1, "attribute")) {
487                                 char errmsg[strlen($1) + strlen($3) + 32];
488                                 snprintf(errmsg, sizeof(errmsg),
489                                                 YY_("unknown value %s[%s]"), $1);
490                                 sdb_fe_yyerror(&yylloc, scanner, errmsg);
491                                 free($1); $1 = NULL;
492                                 free($3); $3 = NULL;
493                                 YYABORT;
494                         }
496                         expr = sdb_store_expr_attrvalue($3);
497                         $$ = sdb_store_isnnull_matcher(expr);
498                         sdb_object_deref(SDB_OBJ(expr));
499                         free($1); $1 = NULL;
500                         free($3); $3 = NULL;
501                 }
502         ;
504 expression:
505         '(' expression ')'
506                 {
507                         $$ = $2;
508                 }
509         |
510         expression '+' expression
511                 {
512                         $$ = sdb_store_expr_create(SDB_DATA_ADD, $1, $3);
513                         sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
514                         sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
515                 }
516         |
517         expression '-' expression
518                 {
519                         $$ = sdb_store_expr_create(SDB_DATA_SUB, $1, $3);
520                         sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
521                         sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
522                 }
523         |
524         expression '*' expression
525                 {
526                         $$ = sdb_store_expr_create(SDB_DATA_MUL, $1, $3);
527                         sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
528                         sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
529                 }
530         |
531         expression '/' expression
532                 {
533                         $$ = sdb_store_expr_create(SDB_DATA_DIV, $1, $3);
534                         sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
535                         sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
536                 }
537         |
538         expression '%' expression
539                 {
540                         $$ = sdb_store_expr_create(SDB_DATA_MOD, $1, $3);
541                         sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
542                         sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
543                 }
544         |
545         expression CONCAT expression
546                 {
547                         $$ = sdb_store_expr_create(SDB_DATA_CONCAT, $1, $3);
548                         sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
549                         sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
550                 }
551         |
552         '.' IDENTIFIER
553                 {
554                         int field = sdb_store_parse_field_name($2);
555                         free($2); $2 = NULL;
556                         $$ = sdb_store_expr_fieldvalue(field);
557                 }
558         |
559         IDENTIFIER '[' IDENTIFIER ']'
560                 {
561                         $$ = sdb_store_expr_attrvalue($3);
562                         free($1); $1 = NULL;
563                         free($3); $3 = NULL;
564                 }
565         |
566         data
567                 {
568                         $$ = sdb_store_expr_constvalue(&$1);
569                         sdb_data_free_datum(&$1);
570                 }
571         ;
573 cmp:
574         CMP_EQUAL { $$ = "="; }
575         |
576         CMP_NEQUAL { $$ = "!="; }
577         |
578         CMP_REGEX { $$ = "=~"; }
579         |
580         CMP_NREGEX { $$ = "!~"; }
581         |
582         CMP_LT { $$ = "<"; }
583         |
584         CMP_LE { $$ = "<="; }
585         |
586         CMP_GE { $$ = ">="; }
587         |
588         CMP_GT { $$ = ">"; }
589         ;
591 data:
592         STRING { $$.type = SDB_TYPE_STRING; $$.data.string = $1; }
593         |
594         INTEGER { $$ = $1; }
595         |
596         FLOAT { $$ = $1; }
597         |
598         datetime { $$.type = SDB_TYPE_DATETIME; $$.data.datetime = $1; }
599         |
600         interval { $$ = $1; }
601         ;
603 datetime:
604         DATE TIME { $$ = $1 + $2; }
605         |
606         DATE { $$ = $1; }
607         |
608         TIME { $$ = $1; }
609         ;
611 interval:
612         interval interval_elem
613                 {
614                         $$.data.datetime = $1.data.datetime + $2.data.datetime;
615                 }
616         |
617         interval_elem { $$ = $1; }
618         ;
620 interval_elem:
621         INTEGER IDENTIFIER
622                 {
623                         sdb_time_t unit = 1;
625                         unit = sdb_strpunit($2);
626                         if (! unit) {
627                                 char errmsg[strlen($2) + 32];
628                                 snprintf(errmsg, sizeof(errmsg),
629                                                 YY_("invalid time unit %s"), $2);
630                                 sdb_fe_yyerror(&yylloc, scanner, errmsg);
631                                 free($2); $2 = NULL;
632                                 YYABORT;
633                         }
634                         free($2); $2 = NULL;
636                         $$.type = SDB_TYPE_DATETIME;
637                         $$.data.datetime = (sdb_time_t)$1.data.integer * unit;
639                         if ($1.data.integer < 0) {
640                                 sdb_fe_yyerror(&yylloc, scanner,
641                                                 YY_("syntax error, negative intervals not supported"));
642                                 YYABORT;
643                         }
644                 }
645         ;
647 %%
649 void
650 sdb_fe_yyerror(YYLTYPE *lval, sdb_fe_yyscan_t scanner, const char *msg)
652         sdb_log(SDB_LOG_ERR, "frontend: parse error: %s", msg);
653 } /* sdb_fe_yyerror */
655 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */