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