Code

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