Code

frontend parser: Added support for AND and OR matchers.
[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 WHERE
83 %token CMP_EQUAL CMP_REGEX
85 %token <str> IDENTIFIER STRING
86 %token <node> FETCH LIST LOOKUP
88 /* Precedence (lowest first): */
89 %left OR
90 %left AND
91 %left CMP_EQUAL
92 %left CMP_REGEX
94 %type <list> statements
95 %type <node> statement
96         fetch_statement
97         list_statement
98         lookup_statement
99         expression
101 %type <m> matcher
102         compare_matcher
104 %%
106 statements:
107         statements ';' statement
108                 {
109                         /* only accept this in default parse mode */
110                         if (parser_mode != SDB_PARSE_DEFAULT) {
111                                 sdb_fe_yyerror(&yylloc, scanner,
112                                                 YY_("syntax error, unexpected statement, "
113                                                         "expecting expression"));
114                                 YYABORT;
115                         }
117                         if ($3) {
118                                 sdb_llist_append(pt, SDB_OBJ($3));
119                                 sdb_object_deref(SDB_OBJ($3));
120                         }
121                 }
122         |
123         statement
124                 {
125                         /* only accept this in default parse mode */
126                         if (parser_mode != SDB_PARSE_DEFAULT) {
127                                 sdb_fe_yyerror(&yylloc, scanner,
128                                                 YY_("syntax error, unexpected statement, "
129                                                         "expecting expression"));
130                                 YYABORT;
131                         }
133                         if ($1) {
134                                 sdb_llist_append(pt, SDB_OBJ($1));
135                                 sdb_object_deref(SDB_OBJ($1));
136                         }
137                 }
138         |
139         expression
140                 {
141                         /* only accept this in expression parse mode */
142                         if (! (parser_mode & SDB_PARSE_EXPR)) {
143                                 sdb_fe_yyerror(&yylloc, scanner,
144                                                 YY_("syntax error, unexpected expression, "
145                                                         "expecting statement"));
146                                 YYABORT;
147                         }
149                         if ($1) {
150                                 sdb_llist_append(pt, SDB_OBJ($1));
151                                 sdb_object_deref(SDB_OBJ($1));
152                         }
153                 }
154         ;
156 statement:
157         fetch_statement
158         |
159         list_statement
160         |
161         lookup_statement
162         |
163         /* empty */
164                 {
165                         $$ = NULL;
166                 }
167         ;
169 /*
170  * FETCH <hostname>;
171  *
172  * Retrieve detailed information about a single host.
173  */
174 fetch_statement:
175         FETCH STRING
176                 {
177                         $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
178                                                 conn_fetch_t, conn_fetch_destroy));
179                         CONN_FETCH($$)->name = strdup($2);
180                         $$->cmd = CONNECTION_FETCH;
181                         free($2); $2 = NULL;
182                 }
183         ;
185 /*
186  * LIST;
187  *
188  * Returns a list of all hosts in the store.
189  */
190 list_statement:
191         LIST
192                 {
193                         $$ = SDB_CONN_NODE(sdb_object_create_T(/* name = */ NULL,
194                                                 sdb_conn_node_t));
195                         $$->cmd = CONNECTION_LIST;
196                 }
197         ;
199 /*
200  * LOOKUP <type> WHERE <expression>;
201  *
202  * Returns detailed information about <type> matching expression.
203  */
204 lookup_statement:
205         LOOKUP IDENTIFIER WHERE expression
206                 {
207                         /* TODO: support other types as well */
208                         if (strcasecmp($2, "hosts")) {
209                                 char errmsg[strlen($2) + 32];
210                                 snprintf(errmsg, sizeof(errmsg),
211                                                 YY_("unknown table %s"), $2);
212                                 sdb_fe_yyerror(&yylloc, scanner, errmsg);
213                                 YYABORT;
214                         }
216                         $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
217                                                 conn_lookup_t, conn_lookup_destroy));
218                         CONN_LOOKUP($$)->matcher = CONN_MATCHER($4);
219                         $$->cmd = CONNECTION_LOOKUP;
220                         free($2); $2 = NULL;
221                 }
222         ;
224 expression:
225         matcher
226                 {
227                         sdb_store_matcher_t *m = $1;
228                         if (! m) {
229                                 /* TODO: improve error reporting */
230                                 sdb_fe_yyerror(&yylloc, scanner,
231                                                 YY_("syntax error, invalid expression"));
232                                 YYABORT;
233                         }
235                         $$ = SDB_CONN_NODE(sdb_object_create_T(/* name = */ NULL,
236                                                 conn_node_matcher_t));
237                         $$->cmd = CONNECTION_EXPR;
239                         if ((M(m)->type == MATCHER_HOST)
240                                         || (M(m)->type == MATCHER_AND)
241                                         || (M(m)->type == MATCHER_OR))
242                                 CONN_MATCHER($$)->matcher = m;
243                         else if (M(m)->type == MATCHER_SERVICE)
244                                 CONN_MATCHER($$)->matcher = sdb_store_host_matcher(NULL,
245                                                 /* name_re = */ NULL, /* service = */ m,
246                                                 /* attr = */ NULL);
247                         else if (M(m)->type == MATCHER_ATTR)
248                                 CONN_MATCHER($$)->matcher = sdb_store_host_matcher(NULL,
249                                                 /* name_re = */ NULL, /* service = */ NULL,
250                                                 /* attr = */ m);
251                         else {
252                                 char errbuf[1024];
253                                 snprintf(errbuf, sizeof(errbuf),
254                                                 YY_("syntax error, unexpected matcher type %d"),
255                                                 M(m)->type);
256                                 sdb_fe_yyerror(&yylloc, scanner, errbuf);
257                                 YYABORT;
258                         }
259                 }
260         ;
262 matcher:
263         matcher AND matcher
264                 {
265                         $$ = sdb_store_con_matcher($1, $3);
266                 }
267         |
268         matcher OR matcher
269                 {
270                         $$ = sdb_store_dis_matcher($1, $3);
271                 }
272         |
273         compare_matcher
274                 {
275                         $$ = $1;
276                 }
277         ;
279 /*
280  * <object_type>.<object_attr> <op> <value>
281  *
282  * Parse matchers comparing object attributes with a value.
283  */
284 compare_matcher:
285         IDENTIFIER '.' IDENTIFIER CMP_EQUAL STRING
286                 {
287                         $$ = sdb_store_matcher_parse_cmp($1, $3, "=", $5);
288                         /* TODO: simplify memory management in the parser */
289                         free($1); $1 = NULL;
290                         free($3); $3 = NULL;
291                         free($5); $5 = NULL;
292                 }
293         |
294         IDENTIFIER '.' IDENTIFIER CMP_REGEX STRING
295                 {
296                         $$ = sdb_store_matcher_parse_cmp($1, $3, "=~", $5);
297                         free($1); $1 = NULL;
298                         free($3); $3 = NULL;
299                         free($5); $5 = NULL;
300                 }
301         ;
303 %%
305 void
306 sdb_fe_yyerror(YYLTYPE *lval, sdb_fe_yyscan_t scanner, const char *msg)
308         sdb_log(SDB_LOG_ERR, "frontend: parse error: %s", msg);
309 } /* sdb_fe_yyerror */
311 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */