Code

frontend: Let the parser not accept other grammars in default mode.
[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"
36 #include "utils/error.h"
37 #include "utils/llist.h"
39 #include <stdio.h>
41 int
42 sdb_fe_yylex(YYSTYPE *yylval, YYLTYPE *yylloc, sdb_fe_yyscan_t yyscanner);
44 sdb_fe_yyextra_t *
45 sdb_fe_yyget_extra(sdb_fe_yyscan_t scanner);
47 void
48 sdb_fe_yyerror(YYLTYPE *lval, sdb_fe_yyscan_t scanner, const char *msg);
50 /* quick access to the current parse tree */
51 #define pt sdb_fe_yyget_extra(scanner)->parsetree
53 /* quick access to the parser mode */
54 #define parser_mode sdb_fe_yyget_extra(scanner)->mode
56 %}
58 %pure-parser
59 %lex-param {sdb_fe_yyscan_t scanner}
60 %parse-param {sdb_fe_yyscan_t scanner}
61 %locations
62 %error-verbose
63 %expect 0
64 %name-prefix="sdb_fe_yy"
66 %union {
67         char *str;
69         sdb_llist_t     *list;
70         sdb_conn_node_t *node;
71 }
73 %start statements
75 %token SCANNER_ERROR
77 %token <str> IDENTIFIER
78 %token <node> LIST
80 %type <list> statements
81 %type <node> statement
82         list_statement
83         expression
85 %%
87 statements:
88         statements ';' statement
89                 {
90                         /* only accept this in default parse mode */
91                         if (parser_mode != SDB_PARSE_DEFAULT) {
92                                 sdb_fe_yyerror(&yylloc, scanner,
93                                                 YY_("syntax error, unexpected statement, "
94                                                         "expecting expression"));
95                                 YYABORT;
96                         }
98                         if ($3) {
99                                 sdb_llist_append(pt, SDB_OBJ($3));
100                                 sdb_object_deref(SDB_OBJ($3));
101                         }
102                 }
103         |
104         statement
105                 {
106                         /* only accept this in default parse mode */
107                         if (parser_mode != SDB_PARSE_DEFAULT) {
108                                 sdb_fe_yyerror(&yylloc, scanner,
109                                                 YY_("syntax error, unexpected statement, "
110                                                         "expecting expression"));
111                                 YYABORT;
112                         }
114                         if ($1) {
115                                 sdb_llist_append(pt, SDB_OBJ($1));
116                                 sdb_object_deref(SDB_OBJ($1));
117                         }
118                 }
119         |
120         expression
121                 {
122                         /* only accept this in expression parse mode */
123                         if (! (parser_mode & SDB_PARSE_EXPR)) {
124                                 sdb_fe_yyerror(&yylloc, scanner,
125                                                 YY_("syntax error, unexpected expression, "
126                                                         "expecting statement"));
127                                 YYABORT;
128                         }
130                         if ($1) {
131                                 sdb_llist_append(pt, SDB_OBJ($1));
132                                 sdb_object_deref(SDB_OBJ($1));
133                         }
134                 }
135         ;
137 statement:
138         list_statement
139         |
140         /* empty */
141                 {
142                         $$ = NULL;
143                 }
144         ;
146 list_statement:
147         LIST
148                 {
149                         $$ = SDB_CONN_NODE(sdb_object_create_T(/* name = */ NULL,
150                                                 sdb_conn_node_t));
151                         $$->cmd = CONNECTION_LIST;
152                 }
153         ;
155 expression:
156         IDENTIFIER
157                 {
158                         $$ = SDB_CONN_NODE(sdb_object_create_T(/* name = */ NULL,
159                                                 conn_node_matcher_t));
160                         $$->cmd = CONNECTION_EXPR;
161                         /* XXX: this is just a placeholder for now */
162                         CONN_MATCHER($$)->matcher = sdb_store_host_matcher($1,
163                                         /* name_re = */ NULL, /* service = */ NULL,
164                                         /* attr = */ NULL);
165                         free($1);
166                         $1 = NULL;
167                 }
168         ;
170 %%
172 void
173 sdb_fe_yyerror(YYLTYPE *lval, sdb_fe_yyscan_t scanner, const char *msg)
175         sdb_log(SDB_LOG_ERR, "frontend: parse error: %s", msg);
176 } /* sdb_fe_yyerror */
178 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */