1 /*
2 * SysDB - src/parser/parser.c
3 * Copyright (C) 2013-2015 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 #include "sysdb.h"
30 #include "parser/ast.h"
31 #include "parser/parser.h"
32 #include "parser/grammar.h"
34 #include "utils/llist.h"
35 #include "utils/strbuf.h"
37 #include <assert.h>
38 #include <string.h>
40 /*
41 * private helper functions
42 */
44 static int
45 scanner_init(const char *input, int len,
46 sdb_parser_yyscan_t *scanner, sdb_parser_yyextra_t *extra,
47 sdb_strbuf_t *errbuf)
48 {
49 if (! input) {
50 sdb_strbuf_sprintf(errbuf, "Missing scanner input");
51 return -1;
52 }
54 memset(extra, 0, sizeof(*extra));
55 extra->parsetree = sdb_llist_create();
56 extra->mode = SDB_PARSE_DEFAULT;
57 extra->errbuf = errbuf;
59 if (! extra->parsetree) {
60 sdb_strbuf_sprintf(errbuf, "Failed to allocate parse-tree");
61 return -1;
62 }
64 *scanner = sdb_parser_scanner_init(input, len, extra);
65 if (! *scanner) {
66 sdb_llist_destroy(extra->parsetree);
67 return -1;
68 }
69 return 0;
70 } /* scanner_init */
72 /*
73 * public API
74 */
76 sdb_llist_t *
77 sdb_parser_parse(const char *query, int len, sdb_strbuf_t *errbuf)
78 {
79 sdb_parser_yyscan_t scanner;
80 sdb_parser_yyextra_t yyextra;
81 sdb_llist_iter_t *iter;
82 int yyres;
84 if (scanner_init(query, len, &scanner, &yyextra, errbuf))
85 return NULL;
87 yyres = sdb_parser_yyparse(scanner);
88 sdb_parser_scanner_destroy(scanner);
90 if (yyres) {
91 sdb_llist_destroy(yyextra.parsetree);
92 return NULL;
93 }
95 iter = sdb_llist_get_iter(yyextra.parsetree);
96 while (sdb_llist_iter_has_next(iter)) {
97 sdb_ast_node_t *node;
98 node = SDB_AST_NODE(sdb_llist_iter_get_next(iter));
99 if (sdb_parser_analyze(node, errbuf) < 0) {
100 sdb_llist_iter_destroy(iter);
101 sdb_llist_destroy(yyextra.parsetree);
102 return NULL;
103 }
104 }
105 sdb_llist_iter_destroy(iter);
106 return yyextra.parsetree;
107 } /* sdb_parser_parse */
109 sdb_ast_node_t *
110 sdb_parser_parse_conditional(int context,
111 const char *cond, int len, sdb_strbuf_t *errbuf)
112 {
113 sdb_parser_yyscan_t scanner;
114 sdb_parser_yyextra_t yyextra;
116 sdb_ast_node_t *node;
118 int yyres;
120 if (scanner_init(cond, len, &scanner, &yyextra, errbuf))
121 return NULL;
123 yyextra.mode = SDB_PARSE_COND;
125 yyres = sdb_parser_yyparse(scanner);
126 sdb_parser_scanner_destroy(scanner);
128 if (yyres) {
129 sdb_llist_destroy(yyextra.parsetree);
130 return NULL;
131 }
133 node = SDB_AST_NODE(sdb_llist_get(yyextra.parsetree, 0));
134 if (! node) {
135 sdb_strbuf_sprintf(errbuf, "Empty conditional expression '%s'", cond);
136 sdb_llist_destroy(yyextra.parsetree);
137 return NULL;
138 }
140 assert(SDB_AST_IS_LOGICAL(node));
141 sdb_llist_destroy(yyextra.parsetree);
143 if (sdb_parser_analyze_conditional(context, node, errbuf)) {
144 sdb_object_deref(SDB_OBJ(node));
145 return NULL;
146 }
147 return node;
148 } /* sdb_parser_parse_conditional */
150 sdb_ast_node_t *
151 sdb_parser_parse_arith(int context,
152 const char *expr, int len, sdb_strbuf_t *errbuf)
153 {
154 sdb_parser_yyscan_t scanner;
155 sdb_parser_yyextra_t yyextra;
157 sdb_ast_node_t *node;
159 int yyres;
161 if (scanner_init(expr, len, &scanner, &yyextra, errbuf))
162 return NULL;
164 yyextra.mode = SDB_PARSE_ARITH;
166 yyres = sdb_parser_yyparse(scanner);
167 sdb_parser_scanner_destroy(scanner);
169 if (yyres) {
170 sdb_llist_destroy(yyextra.parsetree);
171 return NULL;
172 }
174 node = SDB_AST_NODE(sdb_llist_get(yyextra.parsetree, 0));
175 if (! node) {
176 sdb_strbuf_sprintf(errbuf, "Empty expression '%s'", expr);
177 sdb_llist_destroy(yyextra.parsetree);
178 return NULL;
179 }
181 assert(SDB_AST_IS_ARITHMETIC(node));
182 sdb_llist_destroy(yyextra.parsetree);
184 if (sdb_parser_analyze_arith(context, node, errbuf)) {
185 sdb_object_deref(SDB_OBJ(node));
186 return NULL;
187 }
188 return node;
189 } /* sdb_parser_parse_arith */
191 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */