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