Code

frontend: Added 'FILTER' support to the 'LOOKUP' 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"
37 #include "utils/error.h"
38 #include "utils/llist.h"
40 #include <stdio.h>
41 #include <string.h>
43 int
44 sdb_fe_yylex(YYSTYPE *yylval, YYLTYPE *yylloc, sdb_fe_yyscan_t yyscanner);
46 sdb_fe_yyextra_t *
47 sdb_fe_yyget_extra(sdb_fe_yyscan_t scanner);
49 void
50 sdb_fe_yyerror(YYLTYPE *lval, sdb_fe_yyscan_t scanner, const char *msg);
52 /* quick access to the current parse tree */
53 #define pt sdb_fe_yyget_extra(scanner)->parsetree
55 /* quick access to the parser mode */
56 #define parser_mode sdb_fe_yyget_extra(scanner)->mode
58 %}
60 %pure-parser
61 %lex-param {sdb_fe_yyscan_t scanner}
62 %parse-param {sdb_fe_yyscan_t scanner}
63 %locations
64 %error-verbose
65 %expect 0
66 %name-prefix "sdb_fe_yy"
68 %union {
69         const char *sstr; /* static string */
70         char *str;
72         sdb_data_t data;
74         sdb_llist_t     *list;
75         sdb_conn_node_t *node;
77         sdb_store_matcher_t *m;
78         sdb_store_expr_t *expr;
79 }
81 %start statements
83 %token SCANNER_ERROR
85 %token AND OR IS NOT MATCHING FILTER
86 %token CMP_EQUAL CMP_NEQUAL CMP_REGEX CMP_NREGEX
87 %token CMP_LT CMP_LE CMP_GE CMP_GT
88 %token CONCAT
90 /* NULL token */
91 %token NULL_T
93 %token FETCH LIST LOOKUP
95 %token <str> IDENTIFIER STRING
97 %token <data> INTEGER FLOAT
99 /* Precedence (lowest first): */
100 %left OR
101 %left AND
102 %right NOT
103 %left CMP_EQUAL CMP_NEQUAL
104 %left CMP_LT CMP_LE CMP_GE CMP_GT
105 %nonassoc CMP_REGEX CMP_NREGEX
106 %left CONCAT
107 %nonassoc IS
108 %left '+' '-'
109 %left '*' '/' '%'
110 %left '(' ')'
111 %left '.'
113 %type <list> statements
114 %type <node> statement
115         fetch_statement
116         list_statement
117         lookup_statement
118         condition
120 %type <m> matcher
121         compare_matcher
123 %type <expr> expression
125 %type <sstr> op
127 %type <data> data
128         interval interval_elem
130 %destructor { free($$); } <str>
131 %destructor { sdb_object_deref(SDB_OBJ($$)); } <node> <m> <expr>
133 %%
135 statements:
136         statements ';' statement
137                 {
138                         /* only accept this in default parse mode */
139                         if (parser_mode != SDB_PARSE_DEFAULT) {
140                                 sdb_fe_yyerror(&yylloc, scanner,
141                                                 YY_("syntax error, unexpected statement, "
142                                                         "expecting condition"));
143                                 sdb_object_deref(SDB_OBJ($3));
144                                 YYABORT;
145                         }
147                         if ($3) {
148                                 sdb_llist_append(pt, SDB_OBJ($3));
149                                 sdb_object_deref(SDB_OBJ($3));
150                         }
151                 }
152         |
153         statement
154                 {
155                         /* only accept this in default parse mode */
156                         if (parser_mode != SDB_PARSE_DEFAULT) {
157                                 sdb_fe_yyerror(&yylloc, scanner,
158                                                 YY_("syntax error, unexpected statement, "
159                                                         "expecting condition"));
160                                 sdb_object_deref(SDB_OBJ($1));
161                                 YYABORT;
162                         }
164                         if ($1) {
165                                 sdb_llist_append(pt, SDB_OBJ($1));
166                                 sdb_object_deref(SDB_OBJ($1));
167                         }
168                 }
169         |
170         condition
171                 {
172                         /* only accept this in condition parse mode */
173                         if (! (parser_mode & SDB_PARSE_COND)) {
174                                 sdb_fe_yyerror(&yylloc, scanner,
175                                                 YY_("syntax error, unexpected condition, "
176                                                         "expecting statement"));
177                                 sdb_object_deref(SDB_OBJ($1));
178                                 YYABORT;
179                         }
181                         if ($1) {
182                                 sdb_llist_append(pt, SDB_OBJ($1));
183                                 sdb_object_deref(SDB_OBJ($1));
184                         }
185                 }
186         ;
188 statement:
189         fetch_statement
190         |
191         list_statement
192         |
193         lookup_statement
194         |
195         /* empty */
196                 {
197                         $$ = NULL;
198                 }
199         ;
201 /*
202  * FETCH <hostname>;
203  *
204  * Retrieve detailed information about a single host.
205  */
206 fetch_statement:
207         FETCH STRING
208                 {
209                         $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
210                                                 conn_fetch_t, conn_fetch_destroy));
211                         CONN_FETCH($$)->name = strdup($2);
212                         $$->cmd = CONNECTION_FETCH;
213                         free($2); $2 = NULL;
214                 }
215         ;
217 /*
218  * LIST;
219  *
220  * Returns a list of all hosts in the store.
221  */
222 list_statement:
223         LIST
224                 {
225                         $$ = SDB_CONN_NODE(sdb_object_create_T(/* name = */ NULL,
226                                                 sdb_conn_node_t));
227                         $$->cmd = CONNECTION_LIST;
228                 }
229         ;
231 /*
232  * LOOKUP <type> MATCHING <condition> [FILTER <condition>];
233  *
234  * Returns detailed information about <type> matching condition.
235  */
236 lookup_statement:
237         LOOKUP IDENTIFIER MATCHING condition
238                 {
239                         /* TODO: support other types as well */
240                         if (strcasecmp($2, "hosts")) {
241                                 char errmsg[strlen($2) + 32];
242                                 snprintf(errmsg, sizeof(errmsg),
243                                                 YY_("unknown table %s"), $2);
244                                 sdb_fe_yyerror(&yylloc, scanner, errmsg);
245                                 free($2); $2 = NULL;
246                                 sdb_object_deref(SDB_OBJ($4));
247                                 YYABORT;
248                         }
250                         $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
251                                                 conn_lookup_t, conn_lookup_destroy));
252                         CONN_LOOKUP($$)->matcher = CONN_MATCHER($4);
253                         $$->cmd = CONNECTION_LOOKUP;
254                         free($2); $2 = NULL;
255                 }
256         |
257         LOOKUP IDENTIFIER MATCHING condition FILTER condition
258                 {
259                         /* TODO: support other types as well */
260                         if (strcasecmp($2, "hosts")) {
261                                 char errmsg[strlen($2) + 32];
262                                 snprintf(errmsg, sizeof(errmsg),
263                                                 YY_("unknown table %s"), $2);
264                                 sdb_fe_yyerror(&yylloc, scanner, errmsg);
265                                 free($2); $2 = NULL;
266                                 sdb_object_deref(SDB_OBJ($4));
267                                 YYABORT;
268                         }
270                         $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
271                                                 conn_lookup_t, conn_lookup_destroy));
272                         CONN_LOOKUP($$)->matcher = CONN_MATCHER($4);
273                         CONN_LOOKUP($$)->filter = CONN_MATCHER($6);
274                         $$->cmd = CONNECTION_LOOKUP;
275                         free($2); $2 = NULL;
276                 }
277         ;
279 condition:
280         matcher
281                 {
282                         if (! $1) {
283                                 /* TODO: improve error reporting */
284                                 sdb_fe_yyerror(&yylloc, scanner,
285                                                 YY_("syntax error, invalid condition"));
286                                 YYABORT;
287                         }
289                         $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
290                                                 conn_matcher_t, conn_matcher_destroy));
291                         $$->cmd = CONNECTION_EXPR;
292                         CONN_MATCHER($$)->matcher = $1;
293                 }
294         ;
296 matcher:
297         '(' matcher ')'
298                 {
299                         $$ = $2;
300                 }
301         |
302         matcher AND matcher
303                 {
304                         $$ = sdb_store_con_matcher($1, $3);
305                         sdb_object_deref(SDB_OBJ($1));
306                         sdb_object_deref(SDB_OBJ($3));
307                 }
308         |
309         matcher OR matcher
310                 {
311                         $$ = sdb_store_dis_matcher($1, $3);
312                         sdb_object_deref(SDB_OBJ($1));
313                         sdb_object_deref(SDB_OBJ($3));
314                 }
315         |
316         NOT matcher
317                 {
318                         $$ = sdb_store_inv_matcher($2);
319                         sdb_object_deref(SDB_OBJ($2));
320                 }
321         |
322         compare_matcher
323                 {
324                         $$ = $1;
325                 }
326         ;
328 /*
329  * <object_type>.<object_attr> <op> <value>
330  *
331  * Parse matchers comparing object attributes with a value.
332  */
333 compare_matcher:
334         ':' IDENTIFIER op expression
335                 {
336                         $$ = sdb_store_matcher_parse_field_cmp($2, $3, $4);
337                         free($2); $2 = NULL;
338                         sdb_object_deref(SDB_OBJ($4));
339                 }
340         |
341         IDENTIFIER op expression
342                 {
343                         $$ = sdb_store_matcher_parse_cmp($1, NULL, $2, $3);
344                         free($1); $1 = NULL;
345                         sdb_object_deref(SDB_OBJ($3));
346                 }
347         |
348         IDENTIFIER '.' IDENTIFIER op expression
349                 {
350                         $$ = sdb_store_matcher_parse_cmp($1, $3, $4, $5);
351                         free($1); $1 = NULL;
352                         free($3); $3 = NULL;
353                         sdb_object_deref(SDB_OBJ($5));
354                 }
355         |
356         IDENTIFIER '.' IDENTIFIER IS NULL_T
357                 {
358                         $$ = sdb_store_matcher_parse_cmp($1, $3, "IS", NULL);
359                         free($1); $1 = NULL;
360                         free($3); $3 = NULL;
361                 }
362         |
363         IDENTIFIER '.' IDENTIFIER IS NOT NULL_T
364                 {
365                         sdb_store_matcher_t *m;
366                         m = sdb_store_matcher_parse_cmp($1, $3, "IS", NULL);
367                         free($1); $1 = NULL;
368                         free($3); $3 = NULL;
370                         /* sdb_store_inv_matcher return NULL if m==NULL */
371                         $$ = sdb_store_inv_matcher(m);
372                         sdb_object_deref(SDB_OBJ(m));
373                 }
374         ;
376 expression:
377         '(' expression ')'
378                 {
379                         $$ = $2;
380                 }
381         |
382         expression '+' expression
383                 {
384                         $$ = sdb_store_expr_create(SDB_DATA_ADD, $1, $3);
385                         sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
386                         sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
387                 }
388         |
389         expression '-' expression
390                 {
391                         $$ = sdb_store_expr_create(SDB_DATA_SUB, $1, $3);
392                         sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
393                         sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
394                 }
395         |
396         expression '*' expression
397                 {
398                         $$ = sdb_store_expr_create(SDB_DATA_MUL, $1, $3);
399                         sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
400                         sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
401                 }
402         |
403         expression '/' expression
404                 {
405                         $$ = sdb_store_expr_create(SDB_DATA_DIV, $1, $3);
406                         sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
407                         sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
408                 }
409         |
410         expression '%' expression
411                 {
412                         $$ = sdb_store_expr_create(SDB_DATA_MOD, $1, $3);
413                         sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
414                         sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
415                 }
416         |
417         data
418                 {
419                         $$ = sdb_store_expr_constvalue(&$1);
420                         sdb_data_free_datum(&$1);
421                 }
422         ;
424 op:
425         CMP_EQUAL { $$ = "="; }
426         |
427         CMP_NEQUAL { $$ = "!="; }
428         |
429         CMP_REGEX { $$ = "=~"; }
430         |
431         CMP_NREGEX { $$ = "!~"; }
432         |
433         CMP_LT { $$ = "<"; }
434         |
435         CMP_LE { $$ = "<="; }
436         |
437         CMP_GE { $$ = ">="; }
438         |
439         CMP_GT { $$ = ">"; }
440         ;
442 data:
443         STRING { $$.type = SDB_TYPE_STRING; $$.data.string = $1; }
444         |
445         INTEGER { $$ = $1; }
446         |
447         FLOAT { $$ = $1; }
448         |
449         interval { $$ = $1; }
450         ;
452 interval:
453         interval interval_elem
454                 {
455                         $$.data.datetime = $1.data.datetime + $2.data.datetime;
456                 }
457         |
458         interval_elem { $$ = $1; }
459         ;
461 interval_elem:
462         INTEGER IDENTIFIER
463                 {
464                         sdb_time_t unit = 1;
466                         unit = sdb_strpunit($2);
467                         if (! unit) {
468                                 char errmsg[strlen($2) + 32];
469                                 snprintf(errmsg, sizeof(errmsg),
470                                                 YY_("invalid time unit %s"), $2);
471                                 sdb_fe_yyerror(&yylloc, scanner, errmsg);
472                                 free($2); $2 = NULL;
473                                 YYABORT;
474                         }
475                         free($2); $2 = NULL;
477                         $$.type = SDB_TYPE_DATETIME;
478                         $$.data.datetime = (sdb_time_t)$1.data.integer * unit;
480                         if ($1.data.integer < 0) {
481                                 sdb_fe_yyerror(&yylloc, scanner,
482                                                 YY_("syntax error, negative intervals not supported"));
483                                 YYABORT;
484                         }
485                 }
486         ;
488 %%
490 void
491 sdb_fe_yyerror(YYLTYPE *lval, sdb_fe_yyscan_t scanner, const char *msg)
493         sdb_log(SDB_LOG_ERR, "frontend: parse error: %s", msg);
494 } /* sdb_fe_yyerror */
496 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */