1 /*
2 * SysDB - src/include/parser/parser.h
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 #ifndef SDB_PARSER_PARSER_H
29 #define SDB_PARSER_PARSER_H 1
31 #include "core/store.h"
32 #include "parser/ast.h"
33 #include "utils/llist.h"
34 #include "utils/strbuf.h"
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
40 /* parser modes */
41 enum {
42 /* parser accepts any command statement */
43 SDB_PARSE_DEFAULT = 0,
45 /* parser accepts any conditional statement */
46 SDB_PARSE_COND = 1 << 1,
48 /* parser accepts any arithmetic expression */
49 SDB_PARSE_ARITH = 1 << 2,
50 };
52 /*
53 * sdb_parser_parse:
54 * Parse the specified query of the specified length. If len is a negative
55 * value, use the entire string.
56 *
57 * Returns:
58 * - a list of AST nodes (sdb_ast_node_t) on success; each node describes one
59 * statement of the query
60 * - NULL else; an error message will be written to the specified error
61 * buffer
62 */
63 sdb_llist_t *
64 sdb_parser_parse(const char *query, int len, sdb_strbuf_t *errbuf);
66 /*
67 * sdb_parser_parse_conditional:
68 * Parse a single conditional expression. This function is similar to
69 * sdb_parse_parse but will only accept a single conditional expression. The
70 * return value is guaranteed to satisfy SDB_AST_IS_LOGICAL().
71 */
72 sdb_ast_node_t *
73 sdb_parser_parse_conditional(const char *cond, int len, sdb_strbuf_t *errbuf);
75 /*
76 * sdb_parser_parse_arith:
77 * Parse a single arithmetic expression. This function is similar to
78 * sdb_parse_parse but will only accept a single arithmetic expression. The
79 * return value is guaranteed to satisfy SDB_AST_IS_ARITHMETIC().
80 */
81 sdb_ast_node_t *
82 sdb_parser_parse_arith(const char *expr, int len, sdb_strbuf_t *errbuf);
84 /*
85 * sdb_parser_analyze:
86 * Semantical analysis of a parse-tree.
87 *
88 * Returns:
89 * - 0 on success
90 * - a negative value else; an error message will be written to the provided
91 * error buffer
92 */
93 int
94 sdb_parser_analyze(sdb_ast_node_t *node, sdb_strbuf_t *errbuf);
96 /*
97 * sdb_parser_analyze_conditional:
98 * Semantical analysis of a conditional node.
99 *
100 * Returns:
101 * - 0 on success
102 * - a negative value else; an error message will be written to the provided
103 * error buffer
104 */
105 int
106 sdb_parser_analyze_conditional(sdb_ast_node_t *node, sdb_strbuf_t *errbuf);
108 /*
109 * sdb_parser_analyze_arith:
110 * Semantical analysis of an arithmetic node.
111 *
112 * Returns:
113 * - 0 on success
114 * - a negative value else; an error message will be written to the provided
115 * error buffer
116 */
117 int
118 sdb_parser_analyze_arith(sdb_ast_node_t *node, sdb_strbuf_t *errbuf);
120 /*
121 * Low-level interface.
122 */
124 /* scanner/parser's YY_EXTRA data */
125 typedef struct {
126 /* list of sdb_ast_node_t objects */
127 sdb_llist_t *parsetree;
129 /* parser mode */
130 int mode;
132 /* buffer for parser error messages */
133 sdb_strbuf_t *errbuf;
134 } sdb_parser_yyextra_t;
136 /* see yyscan_t */
137 typedef void *sdb_parser_yyscan_t;
139 /*
140 * sdb_parser_scanner_init:
141 * Allocate and initialize a scanner object. It will operate on the specified
142 * string of the specified length. If len is less than zero, use the entire
143 * string. The scanner/parser extra data stores shared state information
144 * between the scanner and the parser.
145 */
146 sdb_parser_yyscan_t
147 sdb_parser_scanner_init(const char *str, int len, sdb_parser_yyextra_t *yyext);
149 /*
150 * sdb_parser_scanner_destroy:
151 * Destroy a scanner object freeing all of its memory.
152 */
153 void
154 sdb_parser_scanner_destroy(sdb_parser_yyscan_t scanner);
156 /*
157 * sdb_parser_yyparse:
158 * Invoke the low-level parser using the specified scanner. The result will be
159 * returned through the scanner/parser's extra data.
160 *
161 * Returns:
162 * - 0 on success
163 * - a non-zero value else; the error buffer stored in the scanner/parser's
164 * extra data provides an error message in this case
165 */
166 int
167 sdb_parser_yyparse(sdb_parser_yyscan_t scanner);
169 #ifdef __cplusplus
170 } /* extern "C" */
171 #endif
173 #endif /* ! SDB_PARSER_PARSER_H */
175 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */