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