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 * Low-level interface.
89 */
91 /* scanner/parser's YY_EXTRA data */
92 typedef struct {
93 /* list of sdb_ast_node_t objects */
94 sdb_llist_t *parsetree;
96 /* parser mode */
97 int mode;
99 /* buffer for parser error messages */
100 sdb_strbuf_t *errbuf;
101 } sdb_parser_yyextra_t;
103 /* see yyscan_t */
104 typedef void *sdb_parser_yyscan_t;
106 /*
107 * sdb_parser_scanner_init:
108 * Allocate and initialize a scanner object. It will operate on the specified
109 * string of the specified length. If len is less than zero, use the entire
110 * string. The scanner/parser extra data stores shared state information
111 * between the scanner and the parser.
112 */
113 sdb_parser_yyscan_t
114 sdb_parser_scanner_init(const char *str, int len, sdb_parser_yyextra_t *yyext);
116 /*
117 * sdb_parser_scanner_destroy:
118 * Destroy a scanner object freeing all of its memory.
119 */
120 void
121 sdb_parser_scanner_destroy(sdb_parser_yyscan_t scanner);
123 /*
124 * sdb_parser_yyparse:
125 * Invoke the low-level parser using the specified scanner. The result will be
126 * returned through the scanner/parser's extra data.
127 *
128 * Returns:
129 * - 0 on success
130 * - a non-zero value else; the error buffer stored in the scanner/parser's
131 * extra data provides an error message in this case
132 */
133 int
134 sdb_parser_yyparse(sdb_parser_yyscan_t scanner);
136 #ifdef __cplusplus
137 } /* extern "C" */
138 #endif
140 #endif /* ! SDB_PARSER_PARSER_H */
142 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */