Code

parser: Require a context for each parser operation.
[sysdb.git] / src / include / parser / parser.h
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 which can be evaluated in the
69  * specified context (any valid store object type). This function is similar
70  * to sdb_parse_parse but will only accept a single conditional expression.
71  * The return value is guaranteed to satisfy SDB_AST_IS_LOGICAL().
72  */
73 sdb_ast_node_t *
74 sdb_parser_parse_conditional(int context,
75                 const char *cond, int len, sdb_strbuf_t *errbuf);
77 /*
78  * sdb_parser_parse_arith:
79  * Parse a single arithmetic expression which can be evaluated in the
80  * specified context (any valid store object type). This function is similar
81  * to sdb_parse_parse but will only accept a single arithmetic expression. The
82  * return value is guaranteed to satisfy SDB_AST_IS_ARITHMETIC().
83  */
84 sdb_ast_node_t *
85 sdb_parser_parse_arith(int context,
86                 const char *expr, int len, sdb_strbuf_t *errbuf);
88 /*
89  * sdb_parser_analyze:
90  * Semantical analysis of a parse-tree.
91  *
92  * Returns:
93  *  - 0 on success
94  *  - a negative value else; an error message will be written to the provided
95  *    error buffer
96  */
97 int
98 sdb_parser_analyze(sdb_ast_node_t *node, sdb_strbuf_t *errbuf);
100 /*
101  * sdb_parser_analyze_conditional:
102  * Semantical analysis of a conditional node in the specified context (any
103  * valid store object type).
104  *
105  * Returns:
106  *  - 0 on success
107  *  - a negative value else; an error message will be written to the provided
108  *    error buffer
109  */
110 int
111 sdb_parser_analyze_conditional(int context,
112                 sdb_ast_node_t *node, sdb_strbuf_t *errbuf);
114 /*
115  * sdb_parser_analyze_arith:
116  * Semantical analysis of an arithmetic node in the specified context (any
117  * valid store object type).
118  *
119  * Returns:
120  *  - 0 on success
121  *  - a negative value else; an error message will be written to the provided
122  *    error buffer
123  */
124 int
125 sdb_parser_analyze_arith(int context,
126                 sdb_ast_node_t *node, sdb_strbuf_t *errbuf);
128 /*
129  * Low-level interface.
130  */
132 /* scanner/parser's YY_EXTRA data */
133 typedef struct {
134         /* list of sdb_ast_node_t objects */
135         sdb_llist_t *parsetree;
137         /* parser mode */
138         int mode;
140         /* buffer for parser error messages */
141         sdb_strbuf_t *errbuf;
142 } sdb_parser_yyextra_t;
144 /* see yyscan_t */
145 typedef void *sdb_parser_yyscan_t;
147 /*
148  * sdb_parser_scanner_init:
149  * Allocate and initialize a scanner object. It will operate on the specified
150  * string of the specified length. If len is less than zero, use the entire
151  * string. The scanner/parser extra data stores shared state information
152  * between the scanner and the parser.
153  */
154 sdb_parser_yyscan_t
155 sdb_parser_scanner_init(const char *str, int len, sdb_parser_yyextra_t *yyext);
157 /*
158  * sdb_parser_scanner_destroy:
159  * Destroy a scanner object freeing all of its memory.
160  */
161 void
162 sdb_parser_scanner_destroy(sdb_parser_yyscan_t scanner);
164 /*
165  * sdb_parser_yyparse:
166  * Invoke the low-level parser using the specified scanner. The result will be
167  * returned through the scanner/parser's extra data.
168  *
169  * Returns:
170  *  - 0 on success
171  *  - a non-zero value else; the error buffer stored in the scanner/parser's
172  *    extra data provides an error message in this case
173  */
174 int
175 sdb_parser_yyparse(sdb_parser_yyscan_t scanner);
177 #ifdef __cplusplus
178 } /* extern "C" */
179 #endif
181 #endif /* ! SDB_PARSER_PARSER_H */
183 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */