Code

frontend/parser: Added support for the 'TIMESERIES' command.
[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 %}
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($$)->name = $3;
238                         CONN_FETCH($$)->filter = CONN_MATCHER($4);
239                         $$->cmd = CONNECTION_FETCH;
240                         free($2); $2 = NULL;
241                 }
242         ;
244 /*
245  * LIST <type> [FILTER <condition>];
246  *
247  * Returns a list of all hosts in the store.
248  */
249 list_statement:
250         LIST IDENTIFIER filter_clause
251                 {
252                         /* TODO: support other types as well */
253                         if (strcasecmp($2, "hosts")) {
254                                 char errmsg[strlen($2) + 32];
255                                 snprintf(errmsg, sizeof(errmsg),
256                                                 YY_("unknown data-source %s"), $2);
257                                 sdb_fe_yyerror(&yylloc, scanner, errmsg);
258                                 free($2); $2 = NULL;
259                                 sdb_object_deref(SDB_OBJ($3));
260                                 YYABORT;
261                         }
263                         $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
264                                                 conn_list_t, conn_list_destroy));
265                         CONN_LIST($$)->filter = CONN_MATCHER($3);
266                         $$->cmd = CONNECTION_LIST;
267                         free($2); $2 = NULL;
268                 }
269         ;
271 /*
272  * LOOKUP <type> MATCHING <condition> [FILTER <condition>];
273  *
274  * Returns detailed information about <type> matching condition.
275  */
276 lookup_statement:
277         LOOKUP IDENTIFIER matching_clause filter_clause
278                 {
279                         /* TODO: support other types as well */
280                         if (strcasecmp($2, "hosts")) {
281                                 char errmsg[strlen($2) + 32];
282                                 snprintf(errmsg, sizeof(errmsg),
283                                                 YY_("unknown data-source %s"), $2);
284                                 sdb_fe_yyerror(&yylloc, scanner, errmsg);
285                                 free($2); $2 = NULL;
286                                 sdb_object_deref(SDB_OBJ($3));
287                                 sdb_object_deref(SDB_OBJ($4));
288                                 YYABORT;
289                         }
291                         $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
292                                                 conn_lookup_t, conn_lookup_destroy));
293                         CONN_LOOKUP($$)->matcher = CONN_MATCHER($3);
294                         CONN_LOOKUP($$)->filter = CONN_MATCHER($4);
295                         $$->cmd = CONNECTION_LOOKUP;
296                         free($2); $2 = NULL;
297                 }
298         ;
300 matching_clause:
301         MATCHING condition { $$ = $2; }
302         |
303         /* empty */ { $$ = NULL; }
305 filter_clause:
306         FILTER condition { $$ = $2; }
307         |
308         /* empty */ { $$ = NULL; }
310 /*
311  * TIMESERIES <host>.<metric> [START <datetime>] [END <datetime>];
312  *
313  * Returns a time-series for the specified host's metric.
314  */
315 timeseries_statement:
316         TIMESERIES STRING '.' STRING start_clause end_clause
317                 {
318                         $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
319                                                 conn_ts_t, conn_ts_destroy));
320                         CONN_TS($$)->hostname = $2;
321                         CONN_TS($$)->metric = $4;
322                         CONN_TS($$)->opts.start = $5;
323                         CONN_TS($$)->opts.end = $6;
324                         $$->cmd = CONNECTION_TIMESERIES;
325                 }
326         ;
328 start_clause:
329         START datetime { $$ = $2; }
330         |
331         /* empty */ { $$ = sdb_gettime() - SDB_INTERVAL_HOUR; }
333 end_clause:
334         END datetime { $$ = $2; }
335         |
336         /* empty */ { $$ = sdb_gettime(); }
338 /*
339  * Basic expressions.
340  */
342 condition:
343         matcher
344                 {
345                         if (! $1) {
346                                 /* TODO: improve error reporting */
347                                 sdb_fe_yyerror(&yylloc, scanner,
348                                                 YY_("syntax error, invalid condition"));
349                                 YYABORT;
350                         }
352                         $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
353                                                 conn_matcher_t, conn_matcher_destroy));
354                         $$->cmd = CONNECTION_EXPR;
355                         CONN_MATCHER($$)->matcher = $1;
356                 }
357         ;
359 matcher:
360         '(' matcher ')'
361                 {
362                         $$ = $2;
363                 }
364         |
365         matcher AND matcher
366                 {
367                         $$ = sdb_store_con_matcher($1, $3);
368                         sdb_object_deref(SDB_OBJ($1));
369                         sdb_object_deref(SDB_OBJ($3));
370                 }
371         |
372         matcher OR matcher
373                 {
374                         $$ = sdb_store_dis_matcher($1, $3);
375                         sdb_object_deref(SDB_OBJ($1));
376                         sdb_object_deref(SDB_OBJ($3));
377                 }
378         |
379         NOT matcher
380                 {
381                         $$ = sdb_store_inv_matcher($2);
382                         sdb_object_deref(SDB_OBJ($2));
383                 }
384         |
385         compare_matcher
386                 {
387                         $$ = $1;
388                 }
389         ;
391 /*
392  * <object_type>.<object_attr> <op> <value>
393  *
394  * Parse matchers comparing object attributes with a value.
395  */
396 compare_matcher:
397         ':' IDENTIFIER op expression
398                 {
399                         $$ = sdb_store_matcher_parse_field_cmp($2, $3, $4);
400                         free($2); $2 = NULL;
401                         sdb_object_deref(SDB_OBJ($4));
402                 }
403         |
404         IDENTIFIER op expression
405                 {
406                         $$ = sdb_store_matcher_parse_cmp($1, NULL, $2, $3);
407                         free($1); $1 = NULL;
408                         sdb_object_deref(SDB_OBJ($3));
409                 }
410         |
411         IDENTIFIER '.' IDENTIFIER op expression
412                 {
413                         $$ = sdb_store_matcher_parse_cmp($1, $3, $4, $5);
414                         free($1); $1 = NULL;
415                         free($3); $3 = NULL;
416                         sdb_object_deref(SDB_OBJ($5));
417                 }
418         |
419         IDENTIFIER '.' IDENTIFIER IS NULL_T
420                 {
421                         $$ = sdb_store_matcher_parse_cmp($1, $3, "IS", NULL);
422                         free($1); $1 = NULL;
423                         free($3); $3 = NULL;
424                 }
425         |
426         IDENTIFIER '.' IDENTIFIER IS NOT NULL_T
427                 {
428                         sdb_store_matcher_t *m;
429                         m = sdb_store_matcher_parse_cmp($1, $3, "IS", NULL);
430                         free($1); $1 = NULL;
431                         free($3); $3 = NULL;
433                         /* sdb_store_inv_matcher return NULL if m==NULL */
434                         $$ = sdb_store_inv_matcher(m);
435                         sdb_object_deref(SDB_OBJ(m));
436                 }
437         ;
439 expression:
440         '(' expression ')'
441                 {
442                         $$ = $2;
443                 }
444         |
445         expression '+' expression
446                 {
447                         $$ = sdb_store_expr_create(SDB_DATA_ADD, $1, $3);
448                         sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
449                         sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
450                 }
451         |
452         expression '-' expression
453                 {
454                         $$ = sdb_store_expr_create(SDB_DATA_SUB, $1, $3);
455                         sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
456                         sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
457                 }
458         |
459         expression '*' expression
460                 {
461                         $$ = sdb_store_expr_create(SDB_DATA_MUL, $1, $3);
462                         sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
463                         sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
464                 }
465         |
466         expression '/' expression
467                 {
468                         $$ = sdb_store_expr_create(SDB_DATA_DIV, $1, $3);
469                         sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
470                         sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
471                 }
472         |
473         expression '%' expression
474                 {
475                         $$ = sdb_store_expr_create(SDB_DATA_MOD, $1, $3);
476                         sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
477                         sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
478                 }
479         |
480         ':' IDENTIFIER
481                 {
482                         int field = sdb_store_parse_field_name($2);
483                         free($2); $2 = NULL;
484                         $$ = sdb_store_expr_fieldvalue(field);
485                 }
486         |
487         data
488                 {
489                         $$ = sdb_store_expr_constvalue(&$1);
490                         sdb_data_free_datum(&$1);
491                 }
492         ;
494 op:
495         CMP_EQUAL { $$ = "="; }
496         |
497         CMP_NEQUAL { $$ = "!="; }
498         |
499         CMP_REGEX { $$ = "=~"; }
500         |
501         CMP_NREGEX { $$ = "!~"; }
502         |
503         CMP_LT { $$ = "<"; }
504         |
505         CMP_LE { $$ = "<="; }
506         |
507         CMP_GE { $$ = ">="; }
508         |
509         CMP_GT { $$ = ">"; }
510         ;
512 data:
513         STRING { $$.type = SDB_TYPE_STRING; $$.data.string = $1; }
514         |
515         INTEGER { $$ = $1; }
516         |
517         FLOAT { $$ = $1; }
518         |
519         datetime { $$.type = SDB_TYPE_DATETIME; $$.data.datetime = $1; }
520         |
521         interval { $$ = $1; }
522         ;
524 datetime:
525         DATE TIME { $$ = $1 + $2; }
526         |
527         DATE { $$ = $1; }
528         |
529         TIME { $$ = $1; }
530         ;
532 interval:
533         interval interval_elem
534                 {
535                         $$.data.datetime = $1.data.datetime + $2.data.datetime;
536                 }
537         |
538         interval_elem { $$ = $1; }
539         ;
541 interval_elem:
542         INTEGER IDENTIFIER
543                 {
544                         sdb_time_t unit = 1;
546                         unit = sdb_strpunit($2);
547                         if (! unit) {
548                                 char errmsg[strlen($2) + 32];
549                                 snprintf(errmsg, sizeof(errmsg),
550                                                 YY_("invalid time unit %s"), $2);
551                                 sdb_fe_yyerror(&yylloc, scanner, errmsg);
552                                 free($2); $2 = NULL;
553                                 YYABORT;
554                         }
555                         free($2); $2 = NULL;
557                         $$.type = SDB_TYPE_DATETIME;
558                         $$.data.datetime = (sdb_time_t)$1.data.integer * unit;
560                         if ($1.data.integer < 0) {
561                                 sdb_fe_yyerror(&yylloc, scanner,
562                                                 YY_("syntax error, negative intervals not supported"));
563                                 YYABORT;
564                         }
565                 }
566         ;
568 %%
570 void
571 sdb_fe_yyerror(YYLTYPE *lval, sdb_fe_yyscan_t scanner, const char *msg)
573         sdb_log(SDB_LOG_ERR, "frontend: parse error: %s", msg);
574 } /* sdb_fe_yyerror */
576 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */