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(const char *cond, int len, sdb_strbuf_t *errbuf)
111 {
112 sdb_parser_yyscan_t scanner;
113 sdb_parser_yyextra_t yyextra;
115 sdb_ast_node_t *node;
117 int yyres;
119 if (scanner_init(cond, len, &scanner, &yyextra, errbuf))
120 return NULL;
122 yyextra.mode = SDB_PARSE_COND;
124 yyres = sdb_parser_yyparse(scanner);
125 sdb_parser_scanner_destroy(scanner);
127 if (yyres) {
128 sdb_llist_destroy(yyextra.parsetree);
129 return NULL;
130 }
132 node = SDB_AST_NODE(sdb_llist_get(yyextra.parsetree, 0));
133 if (! node) {
134 sdb_strbuf_sprintf(errbuf, "Empty conditional expression '%s'", cond);
135 sdb_llist_destroy(yyextra.parsetree);
136 return NULL;
137 }
139 assert(SDB_AST_IS_LOGICAL(node));
140 sdb_llist_destroy(yyextra.parsetree);
142 if (sdb_parser_analyze_conditional(node, errbuf)) {
143 sdb_object_deref(SDB_OBJ(node));
144 return NULL;
145 }
146 return node;
147 } /* sdb_parser_parse_conditional */
149 sdb_ast_node_t *
150 sdb_parser_parse_arith(const char *expr, int len, sdb_strbuf_t *errbuf)
151 {
152 sdb_parser_yyscan_t scanner;
153 sdb_parser_yyextra_t yyextra;
155 sdb_ast_node_t *node;
157 int yyres;
159 if (scanner_init(expr, len, &scanner, &yyextra, errbuf))
160 return NULL;
162 yyextra.mode = SDB_PARSE_ARITH;
164 yyres = sdb_parser_yyparse(scanner);
165 sdb_parser_scanner_destroy(scanner);
167 if (yyres) {
168 sdb_llist_destroy(yyextra.parsetree);
169 return NULL;
170 }
172 node = SDB_AST_NODE(sdb_llist_get(yyextra.parsetree, 0));
173 if (! node) {
174 sdb_strbuf_sprintf(errbuf, "Empty expression '%s'", expr);
175 sdb_llist_destroy(yyextra.parsetree);
176 return NULL;
177 }
179 assert(SDB_AST_IS_ARITHMETIC(node));
180 sdb_llist_destroy(yyextra.parsetree);
182 if (sdb_parser_analyze_arith(node, errbuf)) {
183 sdb_object_deref(SDB_OBJ(node));
184 return NULL;
185 }
186 return node;
187 } /* sdb_parser_parse_arith */
189 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */