50bb00e7f632fa35abb283ca7025b0c02a80a908
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"
36 #include "utils/error.h"
37 #include "utils/llist.h"
39 #include <stdio.h>
40 #include <string.h>
42 int
43 sdb_fe_yylex(YYSTYPE *yylval, YYLTYPE *yylloc, sdb_fe_yyscan_t yyscanner);
45 sdb_fe_yyextra_t *
46 sdb_fe_yyget_extra(sdb_fe_yyscan_t scanner);
48 void
49 sdb_fe_yyerror(YYLTYPE *lval, sdb_fe_yyscan_t scanner, const char *msg);
51 /* quick access to the current parse tree */
52 #define pt sdb_fe_yyget_extra(scanner)->parsetree
54 /* quick access to the parser mode */
55 #define parser_mode sdb_fe_yyget_extra(scanner)->mode
57 %}
59 %pure-parser
60 %lex-param {sdb_fe_yyscan_t scanner}
61 %parse-param {sdb_fe_yyscan_t scanner}
62 %locations
63 %error-verbose
64 %expect 0
65 %name-prefix="sdb_fe_yy"
67 %union {
68 char *str;
70 sdb_llist_t *list;
71 sdb_conn_node_t *node;
72 }
74 %start statements
76 %token SCANNER_ERROR
78 %token WHERE
80 %token <str> IDENTIFIER STRING
81 %token <node> FETCH LIST LOOKUP
83 %type <list> statements
84 %type <node> statement
85 fetch_statement
86 list_statement
87 lookup_statement
88 expression
90 %%
92 statements:
93 statements ';' statement
94 {
95 /* only accept this in default parse mode */
96 if (parser_mode != SDB_PARSE_DEFAULT) {
97 sdb_fe_yyerror(&yylloc, scanner,
98 YY_("syntax error, unexpected statement, "
99 "expecting expression"));
100 YYABORT;
101 }
103 if ($3) {
104 sdb_llist_append(pt, SDB_OBJ($3));
105 sdb_object_deref(SDB_OBJ($3));
106 }
107 }
108 |
109 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 YYABORT;
117 }
119 if ($1) {
120 sdb_llist_append(pt, SDB_OBJ($1));
121 sdb_object_deref(SDB_OBJ($1));
122 }
123 }
124 |
125 expression
126 {
127 /* only accept this in expression parse mode */
128 if (! (parser_mode & SDB_PARSE_EXPR)) {
129 sdb_fe_yyerror(&yylloc, scanner,
130 YY_("syntax error, unexpected expression, "
131 "expecting statement"));
132 YYABORT;
133 }
135 if ($1) {
136 sdb_llist_append(pt, SDB_OBJ($1));
137 sdb_object_deref(SDB_OBJ($1));
138 }
139 }
140 ;
142 statement:
143 fetch_statement
144 |
145 list_statement
146 |
147 lookup_statement
148 |
149 /* empty */
150 {
151 $$ = NULL;
152 }
153 ;
155 /*
156 * FETCH <hostname>;
157 *
158 * Retrieve detailed information about a single host.
159 */
160 fetch_statement:
161 FETCH STRING
162 {
163 $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
164 conn_fetch_t, conn_fetch_destroy));
165 CONN_FETCH($$)->name = strdup($2);
166 $$->cmd = CONNECTION_FETCH;
167 free($2);
168 $2 = NULL;
169 }
170 ;
172 /*
173 * LIST;
174 *
175 * Returns a list of all hosts in the store.
176 */
177 list_statement:
178 LIST
179 {
180 $$ = SDB_CONN_NODE(sdb_object_create_T(/* name = */ NULL,
181 sdb_conn_node_t));
182 $$->cmd = CONNECTION_LIST;
183 }
184 ;
186 /*
187 * LOOKUP <type> WHERE <expression>;
188 *
189 * Returns detailed information about <type> matching expression.
190 */
191 lookup_statement:
192 LOOKUP IDENTIFIER WHERE expression
193 {
194 /* TODO: support other types as well */
195 if (strcasecmp($2, "hosts")) {
196 char errmsg[strlen($2) + 32];
197 snprintf(errmsg, sizeof(errmsg),
198 YY_("unknown table %s"), $2);
199 sdb_fe_yyerror(&yylloc, scanner, errmsg);
200 YYABORT;
201 }
203 $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
204 conn_lookup_t, conn_lookup_destroy));
205 CONN_LOOKUP($$)->matcher = CONN_MATCHER($4);
206 $$->cmd = CONNECTION_LOOKUP;
207 free($2);
208 $2 = NULL;
209 }
210 ;
212 expression:
213 STRING
214 {
215 $$ = SDB_CONN_NODE(sdb_object_create_T(/* name = */ NULL,
216 conn_node_matcher_t));
217 $$->cmd = CONNECTION_EXPR;
218 /* XXX: this is just a placeholder for now */
219 CONN_MATCHER($$)->matcher = sdb_store_host_matcher($1,
220 /* name_re = */ NULL, /* service = */ NULL,
221 /* attr = */ NULL);
222 free($1);
223 $1 = NULL;
224 }
225 ;
227 %%
229 void
230 sdb_fe_yyerror(YYLTYPE *lval, sdb_fe_yyscan_t scanner, const char *msg)
231 {
232 sdb_log(SDB_LOG_ERR, "frontend: parse error: %s", msg);
233 } /* sdb_fe_yyerror */
235 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */