Code

frontend: Added a parser for arithmetic 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"
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
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
129 %destructor { free($$); } <str>
130 %destructor { sdb_object_deref(SDB_OBJ($$)); } <node> <m> <expr>
132 %%
134 statements:
135         statements ';' statement
136                 {
137                         /* only accept this in default parse mode */
138                         if (parser_mode != SDB_PARSE_DEFAULT) {
139                                 sdb_fe_yyerror(&yylloc, scanner,
140                                                 YY_("syntax error, unexpected statement, "
141                                                         "expecting condition"));
142                                 sdb_object_deref(SDB_OBJ($3));
143                                 YYABORT;
144                         }
146                         if ($3) {
147                                 sdb_llist_append(pt, SDB_OBJ($3));
148                                 sdb_object_deref(SDB_OBJ($3));
149                         }
150                 }
151         |
152         statement
153                 {
154                         /* only accept this in default parse mode */
155                         if (parser_mode != SDB_PARSE_DEFAULT) {
156                                 sdb_fe_yyerror(&yylloc, scanner,
157                                                 YY_("syntax error, unexpected statement, "
158                                                         "expecting condition"));
159                                 sdb_object_deref(SDB_OBJ($1));
160                                 YYABORT;
161                         }
163                         if ($1) {
164                                 sdb_llist_append(pt, SDB_OBJ($1));
165                                 sdb_object_deref(SDB_OBJ($1));
166                         }
167                 }
168         |
169         condition
170                 {
171                         /* only accept this in condition parse mode */
172                         if (! (parser_mode & SDB_PARSE_COND)) {
173                                 sdb_fe_yyerror(&yylloc, scanner,
174                                                 YY_("syntax error, unexpected condition, "
175                                                         "expecting statement"));
176                                 sdb_object_deref(SDB_OBJ($1));
177                                 YYABORT;
178                         }
180                         if ($1) {
181                                 sdb_llist_append(pt, SDB_OBJ($1));
182                                 sdb_object_deref(SDB_OBJ($1));
183                         }
184                 }
185         ;
187 statement:
188         fetch_statement
189         |
190         list_statement
191         |
192         lookup_statement
193         |
194         /* empty */
195                 {
196                         $$ = NULL;
197                 }
198         ;
200 /*
201  * FETCH <hostname>;
202  *
203  * Retrieve detailed information about a single host.
204  */
205 fetch_statement:
206         FETCH STRING
207                 {
208                         $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
209                                                 conn_fetch_t, conn_fetch_destroy));
210                         CONN_FETCH($$)->name = strdup($2);
211                         $$->cmd = CONNECTION_FETCH;
212                         free($2); $2 = NULL;
213                 }
214         ;
216 /*
217  * LIST;
218  *
219  * Returns a list of all hosts in the store.
220  */
221 list_statement:
222         LIST
223                 {
224                         $$ = SDB_CONN_NODE(sdb_object_create_T(/* name = */ NULL,
225                                                 sdb_conn_node_t));
226                         $$->cmd = CONNECTION_LIST;
227                 }
228         ;
230 /*
231  * LOOKUP <type> MATCHING <condition>;
232  *
233  * Returns detailed information about <type> matching condition.
234  */
235 lookup_statement:
236         LOOKUP IDENTIFIER MATCHING condition
237                 {
238                         /* TODO: support other types as well */
239                         if (strcasecmp($2, "hosts")) {
240                                 char errmsg[strlen($2) + 32];
241                                 snprintf(errmsg, sizeof(errmsg),
242                                                 YY_("unknown table %s"), $2);
243                                 sdb_fe_yyerror(&yylloc, scanner, errmsg);
244                                 free($2); $2 = NULL;
245                                 sdb_object_deref(SDB_OBJ($4));
246                                 YYABORT;
247                         }
249                         $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
250                                                 conn_lookup_t, conn_lookup_destroy));
251                         CONN_LOOKUP($$)->matcher = CONN_MATCHER($4);
252                         $$->cmd = CONNECTION_LOOKUP;
253                         free($2); $2 = NULL;
254                 }
255         ;
257 condition:
258         matcher
259                 {
260                         if (! $1) {
261                                 /* TODO: improve error reporting */
262                                 sdb_fe_yyerror(&yylloc, scanner,
263                                                 YY_("syntax error, invalid condition"));
264                                 YYABORT;
265                         }
267                         $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
268                                                 conn_node_matcher_t, conn_matcher_destroy));
269                         $$->cmd = CONNECTION_EXPR;
270                         CONN_MATCHER($$)->matcher = $1;
271                 }
272         ;
274 matcher:
275         '(' matcher ')'
276                 {
277                         $$ = $2;
278                 }
279         |
280         matcher AND matcher
281                 {
282                         $$ = sdb_store_con_matcher($1, $3);
283                         sdb_object_deref(SDB_OBJ($1));
284                         sdb_object_deref(SDB_OBJ($3));
285                 }
286         |
287         matcher OR matcher
288                 {
289                         $$ = sdb_store_dis_matcher($1, $3);
290                         sdb_object_deref(SDB_OBJ($1));
291                         sdb_object_deref(SDB_OBJ($3));
292                 }
293         |
294         NOT matcher
295                 {
296                         $$ = sdb_store_inv_matcher($2);
297                         sdb_object_deref(SDB_OBJ($2));
298                 }
299         |
300         compare_matcher
301                 {
302                         $$ = $1;
303                 }
304         ;
306 /*
307  * <object_type>.<object_attr> <op> <value>
308  *
309  * Parse matchers comparing object attributes with a value.
310  */
311 compare_matcher:
312         IDENTIFIER op expression
313                 {
314                         sdb_data_t value = SDB_DATA_INIT;
315                         if (sdb_store_expr_eval($3, &value)) {
316                                 sdb_object_deref(SDB_OBJ($3));
317                                 free($1); $1 = NULL;
318                                 sdb_fe_yyerror(&yylloc, scanner,
319                                                 YY_("syntax error, failed to evaluate expression"));
320                                 YYABORT;
321                         }
322                         sdb_object_deref(SDB_OBJ($3));
323                         $$ = sdb_store_matcher_parse_cmp($1, NULL, $2, &value);
324                         free($1); $1 = NULL;
325                         sdb_data_free_datum(&value);
326                 }
327         |
328         IDENTIFIER '.' IDENTIFIER op expression
329                 {
330                         sdb_data_t value = SDB_DATA_INIT;
331                         if (sdb_store_expr_eval($5, &value)) {
332                                 sdb_object_deref(SDB_OBJ($5));
333                                 free($1); $1 = NULL;
334                                 free($3); $3 = NULL;
335                                 sdb_fe_yyerror(&yylloc, scanner,
336                                                 YY_("syntax error, failed to evaluate expression"));
337                                 YYABORT;
338                         }
339                         sdb_object_deref(SDB_OBJ($5));
340                         $$ = sdb_store_matcher_parse_cmp($1, $3, $4, &value);
341                         free($1); $1 = NULL;
342                         free($3); $3 = NULL;
343                         sdb_data_free_datum(&value);
344                 }
345         |
346         IDENTIFIER '.' IDENTIFIER IS NULL_T
347                 {
348                         $$ = sdb_store_matcher_parse_cmp($1, $3, "IS", NULL);
349                         free($1); $1 = NULL;
350                         free($3); $3 = NULL;
351                 }
352         |
353         IDENTIFIER '.' IDENTIFIER IS NOT NULL_T
354                 {
355                         sdb_store_matcher_t *m;
356                         m = sdb_store_matcher_parse_cmp($1, $3, "IS", NULL);
357                         free($1); $1 = NULL;
358                         free($3); $3 = NULL;
360                         /* sdb_store_inv_matcher return NULL if m==NULL */
361                         $$ = sdb_store_inv_matcher(m);
362                         sdb_object_deref(SDB_OBJ(m));
363                 }
364         ;
366 expression:
367         '(' expression ')'
368                 {
369                         $$ = $2;
370                 }
371         |
372         expression '+' expression
373                 {
374                         $$ = sdb_store_expr_create(SDB_DATA_ADD, $1, $3);
375                         sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
376                         sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
377                 }
378         |
379         expression '-' expression
380                 {
381                         $$ = sdb_store_expr_create(SDB_DATA_SUB, $1, $3);
382                         sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
383                         sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
384                 }
385         |
386         expression '*' expression
387                 {
388                         $$ = sdb_store_expr_create(SDB_DATA_MUL, $1, $3);
389                         sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
390                         sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
391                 }
392         |
393         expression '/' expression
394                 {
395                         $$ = sdb_store_expr_create(SDB_DATA_DIV, $1, $3);
396                         sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
397                         sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
398                 }
399         |
400         expression '%' expression
401                 {
402                         $$ = sdb_store_expr_create(SDB_DATA_MOD, $1, $3);
403                         sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
404                         sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
405                 }
406         |
407         data
408                 {
409                         $$ = sdb_store_expr_constvalue(&$1);
410                         sdb_data_free_datum(&$1);
411                 }
412         ;
414 op:
415         CMP_EQUAL { $$ = "="; }
416         |
417         CMP_NEQUAL { $$ = "!="; }
418         |
419         CMP_REGEX { $$ = "=~"; }
420         |
421         CMP_NREGEX { $$ = "!~"; }
422         |
423         CMP_LT { $$ = "<"; }
424         |
425         CMP_LE { $$ = "<="; }
426         |
427         CMP_GE { $$ = ">="; }
428         |
429         CMP_GT { $$ = ">"; }
430         ;
432 data:
433         STRING { $$.type = SDB_TYPE_STRING; $$.data.string = $1; }
434         |
435         INTEGER { $$ = $1; }
436         |
437         FLOAT { $$ = $1; }
438         ;
440 %%
442 void
443 sdb_fe_yyerror(YYLTYPE *lval, sdb_fe_yyscan_t scanner, const char *msg)
445         sdb_log(SDB_LOG_ERR, "frontend: parse error: %s", msg);
446 } /* sdb_fe_yyerror */
448 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */