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 if ($3) {
91 sdb_llist_append(pt, SDB_OBJ($3));
92 sdb_object_deref(SDB_OBJ($3));
93 }
94 }
95 |
96 statement
97 {
98 if ($1) {
99 sdb_llist_append(pt, SDB_OBJ($1));
100 sdb_object_deref(SDB_OBJ($1));
101 }
102 }
103 |
104 expression
105 {
106 /* only accept this in expression parse mode */
107 if (! (parser_mode & SDB_PARSE_EXPR)) {
108 sdb_fe_yyerror(&yylloc, scanner,
109 YY_("syntax error, unexpected expression, "
110 "expecting statement"));
111 YYABORT;
112 }
114 if ($1) {
115 sdb_llist_append(pt, SDB_OBJ($1));
116 sdb_object_deref(SDB_OBJ($1));
117 }
118 }
119 ;
121 statement:
122 list_statement
123 |
124 /* empty */
125 {
126 $$ = NULL;
127 }
128 ;
130 list_statement:
131 LIST
132 {
133 $$ = SDB_CONN_NODE(sdb_object_create_T(/* name = */ NULL,
134 sdb_conn_node_t));
135 $$->cmd = CONNECTION_LIST;
136 }
137 ;
139 expression:
140 IDENTIFIER
141 {
142 $$ = SDB_CONN_NODE(sdb_object_create_T(/* name = */ NULL,
143 conn_node_matcher_t));
144 $$->cmd = CONNECTION_EXPR;
145 /* XXX: this is just a placeholder for now */
146 CONN_MATCHER($$)->matcher = sdb_store_host_matcher($1,
147 /* name_re = */ NULL, /* service = */ NULL,
148 /* attr = */ NULL);
149 free($1);
150 $1 = NULL;
151 }
152 ;
154 %%
156 void
157 sdb_fe_yyerror(YYLTYPE *lval, sdb_fe_yyscan_t scanner, const char *msg)
158 {
159 sdb_log(SDB_LOG_ERR, "frontend: parse error: %s", msg);
160 } /* sdb_fe_yyerror */
162 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */