Code

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