3802c9c97de1aa70c8d387acd99df728c70ed0bb
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 <str> IDENTIFIER STRING
79 %token <node> FETCH LIST
81 %type <list> statements
82 %type <node> statement
83 fetch_statement
84 list_statement
85 expression
87 %%
89 statements:
90 statements ';' statement
91 {
92 /* only accept this in default parse mode */
93 if (parser_mode != SDB_PARSE_DEFAULT) {
94 sdb_fe_yyerror(&yylloc, scanner,
95 YY_("syntax error, unexpected statement, "
96 "expecting expression"));
97 YYABORT;
98 }
100 if ($3) {
101 sdb_llist_append(pt, SDB_OBJ($3));
102 sdb_object_deref(SDB_OBJ($3));
103 }
104 }
105 |
106 statement
107 {
108 /* only accept this in default parse mode */
109 if (parser_mode != SDB_PARSE_DEFAULT) {
110 sdb_fe_yyerror(&yylloc, scanner,
111 YY_("syntax error, unexpected statement, "
112 "expecting expression"));
113 YYABORT;
114 }
116 if ($1) {
117 sdb_llist_append(pt, SDB_OBJ($1));
118 sdb_object_deref(SDB_OBJ($1));
119 }
120 }
121 |
122 expression
123 {
124 /* only accept this in expression parse mode */
125 if (! (parser_mode & SDB_PARSE_EXPR)) {
126 sdb_fe_yyerror(&yylloc, scanner,
127 YY_("syntax error, unexpected expression, "
128 "expecting statement"));
129 YYABORT;
130 }
132 if ($1) {
133 sdb_llist_append(pt, SDB_OBJ($1));
134 sdb_object_deref(SDB_OBJ($1));
135 }
136 }
137 ;
139 statement:
140 fetch_statement
141 |
142 list_statement
143 |
144 /* empty */
145 {
146 $$ = NULL;
147 }
148 ;
150 /*
151 * FETCH <hostname>;
152 *
153 * Retrieve detailed information about a single host.
154 */
155 fetch_statement:
156 FETCH STRING
157 {
158 $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
159 conn_fetch_t, conn_fetch_destroy));
160 CONN_FETCH($$)->name = strdup($2);
161 $$->cmd = CONNECTION_FETCH;
162 free($2);
163 $2 = NULL;
164 }
165 ;
167 /*
168 * LIST;
169 *
170 * Returns a list of all hosts in the store.
171 */
172 list_statement:
173 LIST
174 {
175 $$ = SDB_CONN_NODE(sdb_object_create_T(/* name = */ NULL,
176 sdb_conn_node_t));
177 $$->cmd = CONNECTION_LIST;
178 }
179 ;
181 expression:
182 STRING
183 {
184 $$ = SDB_CONN_NODE(sdb_object_create_T(/* name = */ NULL,
185 conn_node_matcher_t));
186 $$->cmd = CONNECTION_EXPR;
187 /* XXX: this is just a placeholder for now */
188 CONN_MATCHER($$)->matcher = sdb_store_host_matcher($1,
189 /* name_re = */ NULL, /* service = */ NULL,
190 /* attr = */ NULL);
191 free($1);
192 $1 = NULL;
193 }
194 ;
196 %%
198 void
199 sdb_fe_yyerror(YYLTYPE *lval, sdb_fe_yyscan_t scanner, const char *msg)
200 {
201 sdb_log(SDB_LOG_ERR, "frontend: parse error: %s", msg);
202 } /* sdb_fe_yyerror */
204 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */