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