Code

frontend: Added initial parser for "data" values.
[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 }
80 %start statements
82 %token SCANNER_ERROR
84 %token AND OR NOT WHERE
85 %token CMP_EQUAL CMP_NEQUAL CMP_REGEX CMP_NREGEX
87 %token FETCH LIST LOOKUP
89 %token <str> IDENTIFIER STRING
91 /* Precedence (lowest first): */
92 %left OR
93 %left AND
94 %left NOT
95 %left CMP_EQUAL CMP_NEQUAL
96 %left CMP_REGEX CMP_NREGEX
97 %left '(' ')'
98 %left '.'
100 %type <list> statements
101 %type <node> statement
102         fetch_statement
103         list_statement
104         lookup_statement
105         expression
107 %type <m> matcher
108         compare_matcher
110 %type <sstr> op
112 %type <data> data
114 %destructor { free($$); } <str>
115 %destructor { sdb_object_deref(SDB_OBJ($$)); } <node> <m>
117 %%
119 statements:
120         statements ';' statement
121                 {
122                         /* only accept this in default parse mode */
123                         if (parser_mode != SDB_PARSE_DEFAULT) {
124                                 sdb_fe_yyerror(&yylloc, scanner,
125                                                 YY_("syntax error, unexpected statement, "
126                                                         "expecting expression"));
127                                 sdb_object_deref(SDB_OBJ($3));
128                                 YYABORT;
129                         }
131                         if ($3) {
132                                 sdb_llist_append(pt, SDB_OBJ($3));
133                                 sdb_object_deref(SDB_OBJ($3));
134                         }
135                 }
136         |
137         statement
138                 {
139                         /* only accept this in default parse mode */
140                         if (parser_mode != SDB_PARSE_DEFAULT) {
141                                 sdb_fe_yyerror(&yylloc, scanner,
142                                                 YY_("syntax error, unexpected statement, "
143                                                         "expecting expression"));
144                                 sdb_object_deref(SDB_OBJ($1));
145                                 YYABORT;
146                         }
148                         if ($1) {
149                                 sdb_llist_append(pt, SDB_OBJ($1));
150                                 sdb_object_deref(SDB_OBJ($1));
151                         }
152                 }
153         |
154         expression
155                 {
156                         /* only accept this in expression parse mode */
157                         if (! (parser_mode & SDB_PARSE_EXPR)) {
158                                 sdb_fe_yyerror(&yylloc, scanner,
159                                                 YY_("syntax error, unexpected expression, "
160                                                         "expecting statement"));
161                                 sdb_object_deref(SDB_OBJ($1));
162                                 YYABORT;
163                         }
165                         if ($1) {
166                                 sdb_llist_append(pt, SDB_OBJ($1));
167                                 sdb_object_deref(SDB_OBJ($1));
168                         }
169                 }
170         ;
172 statement:
173         fetch_statement
174         |
175         list_statement
176         |
177         lookup_statement
178         |
179         /* empty */
180                 {
181                         $$ = NULL;
182                 }
183         ;
185 /*
186  * FETCH <hostname>;
187  *
188  * Retrieve detailed information about a single host.
189  */
190 fetch_statement:
191         FETCH STRING
192                 {
193                         $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
194                                                 conn_fetch_t, conn_fetch_destroy));
195                         CONN_FETCH($$)->name = strdup($2);
196                         $$->cmd = CONNECTION_FETCH;
197                         free($2); $2 = NULL;
198                 }
199         ;
201 /*
202  * LIST;
203  *
204  * Returns a list of all hosts in the store.
205  */
206 list_statement:
207         LIST
208                 {
209                         $$ = SDB_CONN_NODE(sdb_object_create_T(/* name = */ NULL,
210                                                 sdb_conn_node_t));
211                         $$->cmd = CONNECTION_LIST;
212                 }
213         ;
215 /*
216  * LOOKUP <type> WHERE <expression>;
217  *
218  * Returns detailed information about <type> matching expression.
219  */
220 lookup_statement:
221         LOOKUP IDENTIFIER WHERE expression
222                 {
223                         /* TODO: support other types as well */
224                         if (strcasecmp($2, "hosts")) {
225                                 char errmsg[strlen($2) + 32];
226                                 snprintf(errmsg, sizeof(errmsg),
227                                                 YY_("unknown table %s"), $2);
228                                 sdb_fe_yyerror(&yylloc, scanner, errmsg);
229                                 free($2); $2 = NULL;
230                                 sdb_object_deref(SDB_OBJ($4));
231                                 YYABORT;
232                         }
234                         $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
235                                                 conn_lookup_t, conn_lookup_destroy));
236                         CONN_LOOKUP($$)->matcher = CONN_MATCHER($4);
237                         $$->cmd = CONNECTION_LOOKUP;
238                         free($2); $2 = NULL;
239                 }
240         ;
242 expression:
243         matcher
244                 {
245                         if (! $1) {
246                                 /* TODO: improve error reporting */
247                                 sdb_fe_yyerror(&yylloc, scanner,
248                                                 YY_("syntax error, invalid expression"));
249                                 YYABORT;
250                         }
252                         $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
253                                                 conn_node_matcher_t, conn_matcher_destroy));
254                         $$->cmd = CONNECTION_EXPR;
255                         CONN_MATCHER($$)->matcher = $1;
256                 }
257         ;
259 matcher:
260         '(' matcher ')'
261                 {
262                         $$ = $2;
263                 }
264         |
265         matcher AND matcher
266                 {
267                         $$ = sdb_store_con_matcher($1, $3);
268                         sdb_object_deref(SDB_OBJ($1));
269                         sdb_object_deref(SDB_OBJ($3));
270                 }
271         |
272         matcher OR matcher
273                 {
274                         $$ = sdb_store_dis_matcher($1, $3);
275                         sdb_object_deref(SDB_OBJ($1));
276                         sdb_object_deref(SDB_OBJ($3));
277                 }
278         |
279         NOT matcher
280                 {
281                         $$ = sdb_store_inv_matcher($2);
282                         sdb_object_deref(SDB_OBJ($2));
283                 }
284         |
285         compare_matcher
286                 {
287                         $$ = $1;
288                 }
289         ;
291 /*
292  * <object_type>.<object_attr> <op> <value>
293  *
294  * Parse matchers comparing object attributes with a value.
295  */
296 compare_matcher:
297         IDENTIFIER '.' IDENTIFIER op data
298                 {
299                         $$ = sdb_store_matcher_parse_cmp($1, $3, $4, &$5);
300                         free($1); $1 = NULL;
301                         free($3); $3 = NULL;
302                         sdb_data_free_datum(&$5);
303                 }
304         ;
306 op:
307         CMP_EQUAL { $$ = "="; }
308         |
309         CMP_NEQUAL { $$ = "!="; }
310         |
311         CMP_REGEX { $$ = "=~"; }
312         |
313         CMP_NREGEX { $$ = "!~"; }
314         ;
316 data:
317         STRING { $$.type = SDB_TYPE_STRING; $$.data.string = $1; }
318         ;
320 %%
322 void
323 sdb_fe_yyerror(YYLTYPE *lval, sdb_fe_yyscan_t scanner, const char *msg)
325         sdb_log(SDB_LOG_ERR, "frontend: parse error: %s", msg);
326 } /* sdb_fe_yyerror */
328 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */