Code

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