Code

parser/ast: Introduce a data-structure representing a SysQL AST.
[sysdb.git] / src / include / parser / ast.h
1 /*
2  * SysDB - src/include/parser/ast.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 /*
29  * The SysDB query language AST describes the parse-tree of an SysQL query.
30  */
32 #ifndef SDB_PARSER_AST_H
33 #define SDB_PARSER_AST_H 1
35 #include "core/data.h"
36 #include "core/time.h"
37 #include "core/object.h"
39 #include <assert.h>
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
45 /*
46  * sdb_ast_node_type_t describes the type of an AST node.
47  */
48 typedef enum {
49         /* command nodes */
50         SDB_AST_TYPE_FETCH      = 1,
51         SDB_AST_TYPE_LIST       = 2,
52         SDB_AST_TYPE_LOOKUP     = 3,
53         SDB_AST_TYPE_STORE      = 4,
54         SDB_AST_TYPE_TIMESERIES = 5,
56         /* generic expressions */
57         SDB_AST_TYPE_OPERATOR   = 100,
58         SDB_AST_TYPE_ITERATOR   = 101,
60         /* values */
61         SDB_AST_TYPE_CONST      = 200,
62         SDB_AST_TYPE_VALUE      = 201,
64         SDB_AST_TYPE_TYPED      = 210,
65 } sdb_ast_node_type_t;
67 /*
68  * sdb_ast_operator_t describes the type of an operator.
69  */
70 typedef enum {
71         /* logical and comparison operators */
72 #define SDB_AST_IS_LOGICAL(n) \
73         ((((n)->type == SDB_AST_TYPE_OPERATOR) \
74                         && ((SDB_AST_AND <= SDB_AST_OP(n)->kind) \
75                                 && (SDB_AST_OP(n)->kind <= SDB_AST_IN))) \
76                 || (((n)->type == SDB_AST_TYPE_ITERATOR) \
77                         && ((SDB_AST_ALL <= SDB_AST_ITER(n)->kind) \
78                                 && (SDB_AST_ITER(n)->kind <= SDB_AST_ANY))))
79         SDB_AST_AND    = 1000,
80         SDB_AST_OR     = 1001,
81         SDB_AST_NOT    = 1002,
83         SDB_AST_LT     = 1010,
84         SDB_AST_LE     = 1011,
85         SDB_AST_EQ     = 1012,
86         SDB_AST_NE     = 1013,
87         SDB_AST_GE     = 1014,
88         SDB_AST_GT     = 1015,
89         SDB_AST_REGEX  = 1016,
90         SDB_AST_NREGEX = 1017,
91         SDB_AST_ISNULL = 1018,
92         SDB_AST_IN     = 1019,
94         /* arithmetic expressions */
95 #define SDB_AST_IS_ARITHMETIC(n) \
96         (((n)->type == SDB_AST_TYPE_CONST) \
97                 || ((n)->type == SDB_AST_TYPE_VALUE) \
98                 || ((n)->type == SDB_AST_TYPE_TYPED) \
99                 || (((n)->type == SDB_AST_TYPE_OPERATOR) \
100                         && ((SDB_AST_ADD <= SDB_AST_OP(n)->kind) \
101                                 && (SDB_AST_OP(n)->kind <= SDB_AST_CONCAT))))
102         SDB_AST_ADD    = 2000,
103         SDB_AST_SUB    = 2001,
104         SDB_AST_MUL    = 2002,
105         SDB_AST_DIV    = 2003,
106         SDB_AST_MOD    = 2004,
107         SDB_AST_CONCAT = 2005,
109         /* iterators */
110         SDB_AST_ALL    = 3000,
111         SDB_AST_ANY    = 3001,
112 } sdb_ast_operator_t;
114 #define SDB_AST_OP_TO_STRING(op) \
115         (((op) == SDB_AST_AND) ? "AND" \
116                 : ((op) == SDB_AST_OR) ? "OR" \
117                 : ((op) == SDB_AST_NOT) ? "NOT" \
118                 : ((op) == SDB_AST_LT) ? "LT" \
119                 : ((op) == SDB_AST_LE) ? "LE" \
120                 : ((op) == SDB_AST_EQ) ? "EQ" \
121                 : ((op) == SDB_AST_NE) ? "NE" \
122                 : ((op) == SDB_AST_GE) ? "GE" \
123                 : ((op) == SDB_AST_GT) ? "GT" \
124                 : ((op) == SDB_AST_REGEX) ? "REGEX" \
125                 : ((op) == SDB_AST_NREGEX) ? "NREGEX" \
126                 : ((op) == SDB_AST_ISNULL) ? "ISNULL" \
127                 : ((op) == SDB_AST_IN) ? "IN" \
128                 : ((op) == SDB_AST_ADD) ? "ADD" \
129                 : ((op) == SDB_AST_SUB) ? "SUB" \
130                 : ((op) == SDB_AST_MUL) ? "MUL" \
131                 : ((op) == SDB_AST_DIV) ? "DIV" \
132                 : ((op) == SDB_AST_MOD) ? "MOD" \
133                 : ((op) == SDB_AST_CONCAT) ? "CONCAT" \
134                 : ((op) == SDB_AST_ALL) ? "ALL" \
135                 : ((op) == SDB_AST_ANY) ? "ANY" \
136                 : "UNKNOWN")
138 /*
139  * sdb_ast_node_t is the interface for AST nodes. The first field of any
140  * actual implementation of the interface is of type sdb_ast_node_t to
141  * fascilitate casting between the interface and implementation types.
142  *
143  * It inherits from sdb_object_t and instances may safely be cast to a generic
144  * object as well.
145  */
146 typedef struct {
147         sdb_object_t super;
149         /* type describes the type of the actual node */
150         int type;
151 } sdb_ast_node_t;
152 #define SDB_AST_NODE(obj) ((sdb_ast_node_t *)(obj))
154 /*
155  * sdb_ast_op_t represents a simple operation.
156  */
157 typedef struct {
158         sdb_ast_node_t super;
159         int kind;
160         /* left operand is NULL for unary expressions */
161         sdb_ast_node_t *left;
162         sdb_ast_node_t *right;
163 } sdb_ast_op_t;
164 #define SDB_AST_OP(obj) ((sdb_ast_op_t *)(obj))
166 /*
167  * sdb_ast_iter_t represents an iterator.
168  */
169 typedef struct {
170         sdb_ast_node_t super;
171         int kind;
172         int op;
173         sdb_ast_node_t *iter;
174         /* exactly one operand of the expression has to be unset and will be
175          * filled in by the iterator value */
176         sdb_ast_node_t *expr;
177 } sdb_ast_iter_t;
178 #define SDB_AST_ITER(obj) ((sdb_ast_iter_t *)(obj))
180 /*
181  * sdb_ast_typed_t represents a typed value.
182  */
183 typedef struct {
184         sdb_ast_node_t super;
185         int type;
186         sdb_ast_node_t *expr;
187 } sdb_ast_typed_t;
188 #define SDB_AST_TYPED(obj) ((sdb_ast_typed_t *)(obj))
190 /*
191  * sdb_ast_const_t represents a constant value.
192  */
193 typedef struct {
194         sdb_ast_node_t super;
195         sdb_data_t value;
196 } sdb_ast_const_t;
197 #define SDB_AST_CONST(obj) ((sdb_ast_const_t *)(obj))
199 /*
200  * sdb_ast_value_t represents an object-specific value: sibling nodes,
201  * attributes, or field values.
202  */
203 typedef struct {
204         sdb_ast_node_t super;
205         int type; /* attribute or field */
206         char *name; /* object name; optional */
207 } sdb_ast_value_t;
208 #define SDB_AST_VALUE(obj) ((sdb_ast_value_t *)(obj))
210 /*
211  * sdb_ast_fetch_t represents a FETCH command.
212  */
213 typedef struct {
214         sdb_ast_node_t super;
215         int obj_type;
216         char *hostname; /* optional */
217         char *name;
218         sdb_ast_node_t *filter; /* optional */
219 } sdb_ast_fetch_t;
220 #define SDB_AST_FETCH(obj) ((sdb_ast_fetch_t *)(obj))
222 /*
223  * sdb_ast_list_t represents a LIST command.
224  */
225 typedef struct {
226         sdb_ast_node_t super;
227         int obj_type;
228         sdb_ast_node_t *filter; /* optional */
229 } sdb_ast_list_t;
230 #define SDB_AST_LIST(obj) ((sdb_ast_list_t *)(obj))
232 /*
233  * sdb_ast_lookup_t represents a LOOKUP command.
234  */
235 typedef struct {
236         sdb_ast_node_t super;
237         int obj_type;
238         sdb_ast_node_t *matcher; /* optional */
239         sdb_ast_node_t *filter; /* optional */
240 } sdb_ast_lookup_t;
241 #define SDB_AST_LOOKUP(obj) ((sdb_ast_lookup_t *)(obj))
243 /*
244  * sdb_ast_store_t represents a STORE command.
245  */
246 typedef struct {
247         sdb_ast_node_t super;
248         int obj_type;
249         char *hostname;  /* optional */
250         int parent_type; /* optional */
251         char *parent;    /* optional */
252         char *name;
253         sdb_time_t last_update;
255         /* metric specific */
256         char *store_type;
257         char *store_id;
259         /* attribute specific */
260         sdb_data_t value;
261 } sdb_ast_store_t;
262 #define SDB_AST_STORE(obj) ((sdb_ast_store_t *)(obj))
264 /*
265  * sdb_ast_timeseries_t represents a TIMESERIES command.
266  */
267 typedef struct {
268         sdb_ast_node_t super;
269         char *hostname;
270         char *metric;
271         sdb_time_t start;
272         sdb_time_t end;
273 } sdb_ast_timeseries_t;
274 #define SDB_AST_TIMESERIES(obj) ((sdb_ast_timeseries_t *)(obj))
276 /*
277  * AST constructors:
278  * Newly created nodes take ownership of any dynamically allocated objects
279  * (node objects, dynamically allocated constant values, strings). The memory
280  * will be freed when destroying the node using sdb_object_deref.
281  *
282  * The constructors do not verify any arguments. The analyzer has to be used
283  * for that purpose.
284  */
286 /*
287  * sdb_ast_op_create:
288  * Creates an AST node representing a simple (ary or unary) operation. The
289  * newly created node takes ownership of the left and right nodes.
290  */
291 sdb_ast_node_t *
292 sdb_ast_op_create(int kind, sdb_ast_node_t *left, sdb_ast_node_t *right);
294 /*
295  * sdb_ast_iter_create:
296  * Creates an AST node representing an iterator. The newly created node takes
297  * ownership of the iter and expr nodes.
298  */
299 sdb_ast_node_t *
300 sdb_ast_iter_create(int kind, int op,
301                 sdb_ast_node_t *iter, sdb_ast_node_t *expr);
303 /*
304  * sdb_ast_typed_create:
305  * Creates an AST node representing a typed expression. Thew newly created
306  * node takes ownership of the expr node.
307  */
308 sdb_ast_node_t *
309 sdb_ast_typed_create(int type, sdb_ast_node_t *expr);
311 /*
312  * sdb_ast_const_create:
313  * Creates an AST node representing a constant value. The newly created node
314  * takes ownership of the value object.
315  */
316 sdb_ast_node_t *
317 sdb_ast_const_create(sdb_data_t value);
319 /*
320  * sdb_ast_value_create:
321  * Creates an AST node representing an object-specific value (sibling nodes,
322  * attributes, or field values). The newly created node takes ownership of the
323  * string value.
324  */
325 sdb_ast_node_t *
326 sdb_ast_value_create(int type, char *name);
328 /*
329  * sdb_ast_fetch_create:
330  * Creates an AST node representing a FETCH command. The newly created node
331  * takes ownership of the strings and the filter node.
332  */
333 sdb_ast_node_t *
334 sdb_ast_fetch_create(int obj_type, char *hostname, char *name,
335                 sdb_ast_node_t *filter);
337 /*
338  * sdb_ast_list_create:
339  * Creates an AST node representing a LIST command. The newly created node
340  * takes ownership of the filter node.
341  */
342 sdb_ast_node_t *
343 sdb_ast_list_create(int obj_type, sdb_ast_node_t *filter);
345 /*
346  * sdb_ast_lookup_create:
347  * Creates an AST node representing a LOOKUP command. The newly created node
348  * takes ownership of the matcher and filter nodes.
349  */
350 sdb_ast_node_t *
351 sdb_ast_lookup_create(int obj_type, sdb_ast_node_t *matcher,
352                 sdb_ast_node_t *filter);
354 /*
355  * sdb_ast_store_create:
356  * Creates an AST node representing a STORE command. Thew newly created node
357  * takes ownership of all strings and the value object.
358  */
359 sdb_ast_node_t *
360 sdb_ast_store_create(int obj_type, char *hostname,
361                 int parent_type, char *parent, char *name, sdb_time_t last_update,
362                 char *store_type, char *store_id, sdb_data_t value);
364 /*
365  * sdb_ast_timeseries_create:
366  * Creates an AST node representing a TIMESERIES command. The newly created
367  * node takes ownership of the strings.
368  */
369 sdb_ast_node_t *
370 sdb_ast_timeseries_create(char *hostname, char *metric,
371                 sdb_time_t start, sdb_time_t end);
373 #ifdef __cplusplus
374 } /* extern "C" */
375 #endif
377 #endif /* ! SDB_PARSER_AST_H */
379 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */