Code

parser: Add support for analyzing conditional and arithmetic expressions.
[sysdb.git] / src / parser / parser.c
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 "core/store.h"
32 #include "parser/ast.h"
33 #include "parser/parser.h"
34 #include "parser/grammar.h"
36 #include "utils/llist.h"
37 #include "utils/strbuf.h"
39 #include <assert.h>
40 #include <string.h>
42 /*
43  * private helper functions
44  */
46 static int
47 scanner_init(const char *input, int len,
48                 sdb_parser_yyscan_t *scanner, sdb_parser_yyextra_t *extra,
49                 sdb_strbuf_t *errbuf)
50 {
51         if (! input) {
52                 sdb_strbuf_sprintf(errbuf, "Missing scanner input");
53                 return -1;
54         }
56         memset(extra, 0, sizeof(*extra));
57         extra->parsetree = sdb_llist_create();
58         extra->mode = SDB_PARSE_DEFAULT;
59         extra->errbuf = errbuf;
61         if (! extra->parsetree) {
62                 sdb_strbuf_sprintf(errbuf, "Failed to allocate parse-tree");
63                 return -1;
64         }
66         *scanner = sdb_parser_scanner_init(input, len, extra);
67         if (! *scanner) {
68                 sdb_llist_destroy(extra->parsetree);
69                 return -1;
70         }
71         return 0;
72 } /* scanner_init */
74 /*
75  * public API
76  */
78 sdb_llist_t *
79 sdb_parser_parse(const char *query, int len, sdb_strbuf_t *errbuf)
80 {
81         sdb_parser_yyscan_t scanner;
82         sdb_parser_yyextra_t yyextra;
83         sdb_llist_iter_t *iter;
84         int yyres;
86         if (scanner_init(query, len, &scanner, &yyextra, errbuf))
87                 return NULL;
89         yyres = sdb_parser_yyparse(scanner);
90         sdb_parser_scanner_destroy(scanner);
92         if (yyres) {
93                 sdb_llist_destroy(yyextra.parsetree);
94                 return NULL;
95         }
97         iter = sdb_llist_get_iter(yyextra.parsetree);
98         while (sdb_llist_iter_has_next(iter)) {
99                 sdb_ast_node_t *node;
100                 node = SDB_AST_NODE(sdb_llist_iter_get_next(iter));
101                 if (sdb_parser_analyze(node, errbuf) < 0) {
102                         sdb_llist_iter_destroy(iter);
103                         sdb_llist_destroy(yyextra.parsetree);
104                         return NULL;
105                 }
106         }
107         sdb_llist_iter_destroy(iter);
108         return yyextra.parsetree;
109 } /* sdb_parser_parse */
111 sdb_ast_node_t *
112 sdb_parser_parse_conditional(const char *cond, int len, sdb_strbuf_t *errbuf)
114         sdb_parser_yyscan_t scanner;
115         sdb_parser_yyextra_t yyextra;
117         sdb_ast_node_t *node;
119         int yyres;
121         if (scanner_init(cond, len, &scanner, &yyextra, errbuf))
122                 return NULL;
124         yyextra.mode = SDB_PARSE_COND;
126         yyres = sdb_parser_yyparse(scanner);
127         sdb_parser_scanner_destroy(scanner);
129         if (yyres) {
130                 sdb_llist_destroy(yyextra.parsetree);
131                 return NULL;
132         }
134         node = SDB_AST_NODE(sdb_llist_get(yyextra.parsetree, 0));
135         if (! node) {
136                 sdb_strbuf_sprintf(errbuf, "Empty conditional expression '%s'", cond);
137                 sdb_llist_destroy(yyextra.parsetree);
138                 return NULL;
139         }
141         assert(SDB_AST_IS_LOGICAL(node));
142         sdb_llist_destroy(yyextra.parsetree);
144         if (sdb_parser_analyze_conditional(node, errbuf)) {
145                 sdb_object_deref(SDB_OBJ(node));
146                 return NULL;
147         }
148         return node;
149 } /* sdb_parser_parse_conditional */
151 sdb_ast_node_t *
152 sdb_parser_parse_arith(const char *expr, int len, sdb_strbuf_t *errbuf)
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(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 : */