Code

frontend/grammar: Changed ‘LIST …’ to ‘LIST hosts …’.
[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         matching_clause
119         filter_clause
120         condition
122 %type <m> matcher
123         compare_matcher
125 %type <expr> expression
127 %type <sstr> op
129 %type <data> data
130         interval interval_elem
132 %destructor { free($$); } <str>
133 %destructor { sdb_object_deref(SDB_OBJ($$)); } <node> <m> <expr>
135 %%
137 statements:
138         statements ';' statement
139                 {
140                         /* only accept this in default parse mode */
141                         if (parser_mode != SDB_PARSE_DEFAULT) {
142                                 sdb_fe_yyerror(&yylloc, scanner,
143                                                 YY_("syntax error, unexpected statement, "
144                                                         "expecting condition"));
145                                 sdb_object_deref(SDB_OBJ($3));
146                                 YYABORT;
147                         }
149                         if ($3) {
150                                 sdb_llist_append(pt, SDB_OBJ($3));
151                                 sdb_object_deref(SDB_OBJ($3));
152                         }
153                 }
154         |
155         statement
156                 {
157                         /* only accept this in default parse mode */
158                         if (parser_mode != SDB_PARSE_DEFAULT) {
159                                 sdb_fe_yyerror(&yylloc, scanner,
160                                                 YY_("syntax error, unexpected statement, "
161                                                         "expecting condition"));
162                                 sdb_object_deref(SDB_OBJ($1));
163                                 YYABORT;
164                         }
166                         if ($1) {
167                                 sdb_llist_append(pt, SDB_OBJ($1));
168                                 sdb_object_deref(SDB_OBJ($1));
169                         }
170                 }
171         |
172         condition
173                 {
174                         /* only accept this in condition parse mode */
175                         if (! (parser_mode & SDB_PARSE_COND)) {
176                                 sdb_fe_yyerror(&yylloc, scanner,
177                                                 YY_("syntax error, unexpected condition, "
178                                                         "expecting statement"));
179                                 sdb_object_deref(SDB_OBJ($1));
180                                 YYABORT;
181                         }
183                         if ($1) {
184                                 sdb_llist_append(pt, SDB_OBJ($1));
185                                 sdb_object_deref(SDB_OBJ($1));
186                         }
187                 }
188         ;
190 statement:
191         fetch_statement
192         |
193         list_statement
194         |
195         lookup_statement
196         |
197         /* empty */
198                 {
199                         $$ = NULL;
200                 }
201         ;
203 /*
204  * FETCH <hostname> [FILTER <condition>];
205  *
206  * Retrieve detailed information about a single host.
207  */
208 fetch_statement:
209         FETCH STRING filter_clause
210                 {
211                         $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
212                                                 conn_fetch_t, conn_fetch_destroy));
213                         CONN_FETCH($$)->name = strdup($2);
214                         CONN_FETCH($$)->filter = CONN_MATCHER($3);
215                         $$->cmd = CONNECTION_FETCH;
216                         free($2); $2 = NULL;
217                 }
218         ;
220 /*
221  * LIST <type> [FILTER <condition>];
222  *
223  * Returns a list of all hosts in the store.
224  */
225 list_statement:
226         LIST IDENTIFIER filter_clause
227                 {
228                         /* TODO: support other types as well */
229                         if (strcasecmp($2, "hosts")) {
230                                 char errmsg[strlen($2) + 32];
231                                 snprintf(errmsg, sizeof(errmsg),
232                                                 YY_("unknown data-source %s"), $2);
233                                 sdb_fe_yyerror(&yylloc, scanner, errmsg);
234                                 free($2); $2 = NULL;
235                                 sdb_object_deref(SDB_OBJ($3));
236                                 YYABORT;
237                         }
239                         $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
240                                                 conn_list_t, conn_list_destroy));
241                         CONN_LIST($$)->filter = CONN_MATCHER($3);
242                         $$->cmd = CONNECTION_LIST;
243                         free($2); $2 = NULL;
244                 }
245         ;
247 /*
248  * LOOKUP <type> MATCHING <condition> [FILTER <condition>];
249  *
250  * Returns detailed information about <type> matching condition.
251  */
252 lookup_statement:
253         LOOKUP IDENTIFIER matching_clause filter_clause
254                 {
255                         /* TODO: support other types as well */
256                         if (strcasecmp($2, "hosts")) {
257                                 char errmsg[strlen($2) + 32];
258                                 snprintf(errmsg, sizeof(errmsg),
259                                                 YY_("unknown data-source %s"), $2);
260                                 sdb_fe_yyerror(&yylloc, scanner, errmsg);
261                                 free($2); $2 = NULL;
262                                 sdb_object_deref(SDB_OBJ($3));
263                                 sdb_object_deref(SDB_OBJ($4));
264                                 YYABORT;
265                         }
267                         $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
268                                                 conn_lookup_t, conn_lookup_destroy));
269                         CONN_LOOKUP($$)->matcher = CONN_MATCHER($3);
270                         CONN_LOOKUP($$)->filter = CONN_MATCHER($4);
271                         $$->cmd = CONNECTION_LOOKUP;
272                         free($2); $2 = NULL;
273                 }
274         ;
276 matching_clause:
277         MATCHING condition { $$ = $2; }
278         |
279         /* empty */ { $$ = NULL; }
281 filter_clause:
282         FILTER condition { $$ = $2; }
283         |
284         /* empty */ { $$ = NULL; }
286 /*
287  * Basic expressions.
288  */
290 condition:
291         matcher
292                 {
293                         if (! $1) {
294                                 /* TODO: improve error reporting */
295                                 sdb_fe_yyerror(&yylloc, scanner,
296                                                 YY_("syntax error, invalid condition"));
297                                 YYABORT;
298                         }
300                         $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
301                                                 conn_matcher_t, conn_matcher_destroy));
302                         $$->cmd = CONNECTION_EXPR;
303                         CONN_MATCHER($$)->matcher = $1;
304                 }
305         ;
307 matcher:
308         '(' matcher ')'
309                 {
310                         $$ = $2;
311                 }
312         |
313         matcher AND matcher
314                 {
315                         $$ = sdb_store_con_matcher($1, $3);
316                         sdb_object_deref(SDB_OBJ($1));
317                         sdb_object_deref(SDB_OBJ($3));
318                 }
319         |
320         matcher OR matcher
321                 {
322                         $$ = sdb_store_dis_matcher($1, $3);
323                         sdb_object_deref(SDB_OBJ($1));
324                         sdb_object_deref(SDB_OBJ($3));
325                 }
326         |
327         NOT matcher
328                 {
329                         $$ = sdb_store_inv_matcher($2);
330                         sdb_object_deref(SDB_OBJ($2));
331                 }
332         |
333         compare_matcher
334                 {
335                         $$ = $1;
336                 }
337         ;
339 /*
340  * <object_type>.<object_attr> <op> <value>
341  *
342  * Parse matchers comparing object attributes with a value.
343  */
344 compare_matcher:
345         ':' IDENTIFIER op expression
346                 {
347                         $$ = sdb_store_matcher_parse_field_cmp($2, $3, $4);
348                         free($2); $2 = NULL;
349                         sdb_object_deref(SDB_OBJ($4));
350                 }
351         |
352         IDENTIFIER op expression
353                 {
354                         $$ = sdb_store_matcher_parse_cmp($1, NULL, $2, $3);
355                         free($1); $1 = NULL;
356                         sdb_object_deref(SDB_OBJ($3));
357                 }
358         |
359         IDENTIFIER '.' IDENTIFIER op expression
360                 {
361                         $$ = sdb_store_matcher_parse_cmp($1, $3, $4, $5);
362                         free($1); $1 = NULL;
363                         free($3); $3 = NULL;
364                         sdb_object_deref(SDB_OBJ($5));
365                 }
366         |
367         IDENTIFIER '.' IDENTIFIER IS NULL_T
368                 {
369                         $$ = sdb_store_matcher_parse_cmp($1, $3, "IS", NULL);
370                         free($1); $1 = NULL;
371                         free($3); $3 = NULL;
372                 }
373         |
374         IDENTIFIER '.' IDENTIFIER IS NOT NULL_T
375                 {
376                         sdb_store_matcher_t *m;
377                         m = sdb_store_matcher_parse_cmp($1, $3, "IS", NULL);
378                         free($1); $1 = NULL;
379                         free($3); $3 = NULL;
381                         /* sdb_store_inv_matcher return NULL if m==NULL */
382                         $$ = sdb_store_inv_matcher(m);
383                         sdb_object_deref(SDB_OBJ(m));
384                 }
385         ;
387 expression:
388         '(' expression ')'
389                 {
390                         $$ = $2;
391                 }
392         |
393         expression '+' expression
394                 {
395                         $$ = sdb_store_expr_create(SDB_DATA_ADD, $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_SUB, $1, $3);
403                         sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
404                         sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
405                 }
406         |
407         expression '*' expression
408                 {
409                         $$ = sdb_store_expr_create(SDB_DATA_MUL, $1, $3);
410                         sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
411                         sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
412                 }
413         |
414         expression '/' expression
415                 {
416                         $$ = sdb_store_expr_create(SDB_DATA_DIV, $1, $3);
417                         sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
418                         sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
419                 }
420         |
421         expression '%' expression
422                 {
423                         $$ = sdb_store_expr_create(SDB_DATA_MOD, $1, $3);
424                         sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
425                         sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
426                 }
427         |
428         ':' IDENTIFIER
429                 {
430                         int field = sdb_store_parse_field_name($2);
431                         free($2); $2 = NULL;
432                         $$ = sdb_store_expr_fieldvalue(field);
433                 }
434         |
435         data
436                 {
437                         $$ = sdb_store_expr_constvalue(&$1);
438                         sdb_data_free_datum(&$1);
439                 }
440         ;
442 op:
443         CMP_EQUAL { $$ = "="; }
444         |
445         CMP_NEQUAL { $$ = "!="; }
446         |
447         CMP_REGEX { $$ = "=~"; }
448         |
449         CMP_NREGEX { $$ = "!~"; }
450         |
451         CMP_LT { $$ = "<"; }
452         |
453         CMP_LE { $$ = "<="; }
454         |
455         CMP_GE { $$ = ">="; }
456         |
457         CMP_GT { $$ = ">"; }
458         ;
460 data:
461         STRING { $$.type = SDB_TYPE_STRING; $$.data.string = $1; }
462         |
463         INTEGER { $$ = $1; }
464         |
465         FLOAT { $$ = $1; }
466         |
467         interval { $$ = $1; }
468         ;
470 interval:
471         interval interval_elem
472                 {
473                         $$.data.datetime = $1.data.datetime + $2.data.datetime;
474                 }
475         |
476         interval_elem { $$ = $1; }
477         ;
479 interval_elem:
480         INTEGER IDENTIFIER
481                 {
482                         sdb_time_t unit = 1;
484                         unit = sdb_strpunit($2);
485                         if (! unit) {
486                                 char errmsg[strlen($2) + 32];
487                                 snprintf(errmsg, sizeof(errmsg),
488                                                 YY_("invalid time unit %s"), $2);
489                                 sdb_fe_yyerror(&yylloc, scanner, errmsg);
490                                 free($2); $2 = NULL;
491                                 YYABORT;
492                         }
493                         free($2); $2 = NULL;
495                         $$.type = SDB_TYPE_DATETIME;
496                         $$.data.datetime = (sdb_time_t)$1.data.integer * unit;
498                         if ($1.data.integer < 0) {
499                                 sdb_fe_yyerror(&yylloc, scanner,
500                                                 YY_("syntax error, negative intervals not supported"));
501                                 YYABORT;
502                         }
503                 }
504         ;
506 %%
508 void
509 sdb_fe_yyerror(YYLTYPE *lval, sdb_fe_yyscan_t scanner, const char *msg)
511         sdb_log(SDB_LOG_ERR, "frontend: parse error: %s", msg);
512 } /* sdb_fe_yyerror */
514 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */