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 #define SDB_AST_IS_ITERATOR(n) \
111 (((n)->type == SDB_AST_TYPE_ITERATOR) \
112 && ((SDB_AST_ALL <= SDB_AST_ITER(n)->kind) \
113 && (SDB_AST_ITER(n)->kind <= SDB_AST_ANY)))
114 SDB_AST_ALL = 3000,
115 SDB_AST_ANY = 3001,
116 } sdb_ast_operator_t;
118 #define SDB_AST_OP_TO_STRING(op) \
119 (((op) == SDB_AST_AND) ? "AND" \
120 : ((op) == SDB_AST_OR) ? "OR" \
121 : ((op) == SDB_AST_NOT) ? "NOT" \
122 : ((op) == SDB_AST_LT) ? "LT" \
123 : ((op) == SDB_AST_LE) ? "LE" \
124 : ((op) == SDB_AST_EQ) ? "EQ" \
125 : ((op) == SDB_AST_NE) ? "NE" \
126 : ((op) == SDB_AST_GE) ? "GE" \
127 : ((op) == SDB_AST_GT) ? "GT" \
128 : ((op) == SDB_AST_REGEX) ? "REGEX" \
129 : ((op) == SDB_AST_NREGEX) ? "NREGEX" \
130 : ((op) == SDB_AST_ISNULL) ? "ISNULL" \
131 : ((op) == SDB_AST_IN) ? "IN" \
132 : ((op) == SDB_AST_ADD) ? "ADD" \
133 : ((op) == SDB_AST_SUB) ? "SUB" \
134 : ((op) == SDB_AST_MUL) ? "MUL" \
135 : ((op) == SDB_AST_DIV) ? "DIV" \
136 : ((op) == SDB_AST_MOD) ? "MOD" \
137 : ((op) == SDB_AST_CONCAT) ? "CONCAT" \
138 : ((op) == SDB_AST_ALL) ? "ALL" \
139 : ((op) == SDB_AST_ANY) ? "ANY" \
140 : "UNKNOWN")
142 #define SDB_AST_OP_TO_DATA_OP(op) \
143 (((op) == SDB_AST_ADD) ? SDB_DATA_ADD \
144 : ((op) == SDB_AST_SUB) ? SDB_DATA_SUB \
145 : ((op) == SDB_AST_MUL) ? SDB_DATA_MUL \
146 : ((op) == SDB_AST_DIV) ? SDB_DATA_DIV \
147 : ((op) == SDB_AST_MOD) ? SDB_DATA_MOD \
148 : ((op) == SDB_AST_CONCAT) ? SDB_DATA_CONCAT \
149 : -1)
151 #define SDB_AST_TYPE_TO_STRING(n) \
152 (((n)->type == SDB_AST_TYPE_FETCH) ? "FETCH" \
153 : ((n)->type == SDB_AST_TYPE_LIST) ? "LIST" \
154 : ((n)->type == SDB_AST_TYPE_LOOKUP) ? "LOOKUP" \
155 : ((n)->type == SDB_AST_TYPE_STORE) ? "STORE" \
156 : ((n)->type == SDB_AST_TYPE_TIMESERIES) ? "TIMESERIES" \
157 : ((n)->type == SDB_AST_TYPE_OPERATOR) \
158 ? SDB_AST_OP_TO_STRING(SDB_AST_OP(n)->kind) \
159 : ((n)->type == SDB_AST_TYPE_ITERATOR) ? "ITERATOR" \
160 : ((n)->type == SDB_AST_TYPE_CONST) ? "CONSTANT" \
161 : ((n)->type == SDB_AST_TYPE_VALUE) ? "VALUE" \
162 : ((n)->type == SDB_AST_TYPE_TYPED) ? "TYPED VALUE" \
163 : "UNKNOWN")
165 /*
166 * sdb_ast_node_t is the interface for AST nodes. The first field of any
167 * actual implementation of the interface is of type sdb_ast_node_t to
168 * fascilitate casting between the interface and implementation types.
169 *
170 * It inherits from sdb_object_t and instances may safely be cast to a generic
171 * object as well.
172 */
173 typedef struct {
174 sdb_object_t super;
176 /* type describes the type of the actual node */
177 int type;
179 /* data-type describes the type of the result value */
180 int data_type;
181 } sdb_ast_node_t;
182 #define SDB_AST_NODE(obj) ((sdb_ast_node_t *)(obj))
184 /*
185 * sdb_ast_op_t represents a simple operation.
186 */
187 typedef struct {
188 sdb_ast_node_t super;
189 int kind;
190 /* left operand is NULL for unary expressions */
191 sdb_ast_node_t *left;
192 sdb_ast_node_t *right;
193 } sdb_ast_op_t;
194 #define SDB_AST_OP(obj) ((sdb_ast_op_t *)(obj))
195 #define SDB_AST_OP_INIT \
196 { { SDB_OBJECT_INIT, SDB_AST_TYPE_OPERATOR, -1 }, -1, NULL, NULL }
198 /*
199 * sdb_ast_iter_t represents an iterator.
200 */
201 typedef struct {
202 sdb_ast_node_t super;
203 int kind;
204 sdb_ast_node_t *iter;
205 /* exactly one operand of the expression has to be unset and will be
206 * filled in by the iterator value */
207 sdb_ast_node_t *expr;
208 } sdb_ast_iter_t;
209 #define SDB_AST_ITER(obj) ((sdb_ast_iter_t *)(obj))
210 #define SDB_AST_ITER_INIT \
211 { { SDB_OBJECT_INIT, SDB_AST_TYPE_ITERATOR, -1 }, -1, NULL, NULL }
213 /*
214 * sdb_ast_typed_t represents a typed value.
215 */
216 typedef struct {
217 sdb_ast_node_t super;
218 int type;
219 sdb_ast_node_t *expr;
220 } sdb_ast_typed_t;
221 #define SDB_AST_TYPED(obj) ((sdb_ast_typed_t *)(obj))
222 #define SDB_AST_TYPED_INIT \
223 { { SDB_OBJECT_INIT, SDB_AST_TYPE_TYPED, -1 }, -1, NULL }
225 /*
226 * sdb_ast_const_t represents a constant value.
227 */
228 typedef struct {
229 sdb_ast_node_t super;
230 sdb_data_t value;
231 } sdb_ast_const_t;
232 #define SDB_AST_CONST(obj) ((sdb_ast_const_t *)(obj))
233 #define SDB_AST_CONST_INIT \
234 { { SDB_OBJECT_INIT, SDB_AST_TYPE_CONST, -1 }, SDB_DATA_INIT }
236 /*
237 * sdb_ast_value_t represents an object-specific value: sibling nodes,
238 * attributes, or field values.
239 */
240 typedef struct {
241 sdb_ast_node_t super;
242 int type; /* attribute or field */
243 char *name; /* object name; optional */
244 } sdb_ast_value_t;
245 #define SDB_AST_VALUE(obj) ((sdb_ast_value_t *)(obj))
246 #define SDB_AST_VALUE_INIT \
247 { { SDB_OBJECT_INIT, SDB_AST_TYPE_VALUE, -1 }, -1, NULL }
249 /*
250 * sdb_ast_fetch_t represents a FETCH command.
251 */
252 typedef struct {
253 sdb_ast_node_t super;
254 int obj_type;
255 char *hostname; /* optional */
256 char *name;
257 sdb_ast_node_t *filter; /* optional */
258 } sdb_ast_fetch_t;
259 #define SDB_AST_FETCH(obj) ((sdb_ast_fetch_t *)(obj))
260 #define SDB_AST_FETCH_INIT \
261 { { SDB_OBJECT_INIT, SDB_AST_TYPE_FETCH, -1 }, -1, NULL, NULL, NULL }
263 /*
264 * sdb_ast_list_t represents a LIST command.
265 */
266 typedef struct {
267 sdb_ast_node_t super;
268 int obj_type;
269 sdb_ast_node_t *filter; /* optional */
270 } sdb_ast_list_t;
271 #define SDB_AST_LIST(obj) ((sdb_ast_list_t *)(obj))
272 #define SDB_AST_LIST_INIT \
273 { { SDB_OBJECT_INIT, SDB_AST_TYPE_LIST, -1 }, -1, NULL }
275 /*
276 * sdb_ast_lookup_t represents a LOOKUP command.
277 */
278 typedef struct {
279 sdb_ast_node_t super;
280 int obj_type;
281 sdb_ast_node_t *matcher; /* optional */
282 sdb_ast_node_t *filter; /* optional */
283 } sdb_ast_lookup_t;
284 #define SDB_AST_LOOKUP(obj) ((sdb_ast_lookup_t *)(obj))
285 #define SDB_AST_LOOKUP_INIT \
286 { { SDB_OBJECT_INIT, SDB_AST_TYPE_LOOKUP, -1 }, -1, NULL, NULL }
288 /*
289 * sdb_ast_store_t represents a STORE command.
290 */
291 typedef struct {
292 sdb_ast_node_t super;
293 int obj_type;
294 char *hostname; /* optional */
295 int parent_type; /* optional */
296 char *parent; /* optional */
297 char *name;
298 sdb_time_t last_update;
300 /* metric specific */
301 char *store_type;
302 char *store_id;
304 /* attribute specific */
305 sdb_data_t value;
306 } sdb_ast_store_t;
307 #define SDB_AST_STORE(obj) ((sdb_ast_store_t *)(obj))
308 #define SDB_AST_STORE_INIT \
309 { { SDB_OBJECT_INIT, SDB_AST_TYPE_STORE, -1 }, \
310 -1, NULL, -1, NULL, NULL, 0, NULL, NULL, SDB_DATA_INIT }
312 /*
313 * sdb_ast_timeseries_t represents a TIMESERIES command.
314 */
315 typedef struct {
316 sdb_ast_node_t super;
317 char *hostname;
318 char *metric;
319 sdb_time_t start;
320 sdb_time_t end;
321 } sdb_ast_timeseries_t;
322 #define SDB_AST_TIMESERIES(obj) ((sdb_ast_timeseries_t *)(obj))
323 #define SDB_AST_TIMESERIES_INIT \
324 { { SDB_OBJECT_INIT, SDB_AST_TYPE_TIMESERIES, -1 }, NULL, NULL, 0, 0 }
326 /*
327 * AST constructors:
328 * Newly created nodes take ownership of any dynamically allocated objects
329 * (node objects, dynamically allocated constant values, strings). The memory
330 * will be freed when destroying the node using sdb_object_deref.
331 *
332 * The constructors do not verify any arguments. The analyzer has to be used
333 * for that purpose.
334 */
336 /*
337 * sdb_ast_op_create:
338 * Creates an AST node representing a simple (ary or unary) operation. The
339 * newly created node takes ownership of the left and right nodes.
340 */
341 sdb_ast_node_t *
342 sdb_ast_op_create(int kind, sdb_ast_node_t *left, sdb_ast_node_t *right);
344 /*
345 * sdb_ast_iter_create:
346 * Creates an AST node representing an iterator. The newly created node takes
347 * ownership of the iter and expr nodes.
348 */
349 sdb_ast_node_t *
350 sdb_ast_iter_create(int kind, sdb_ast_node_t *iter, sdb_ast_node_t *expr);
352 /*
353 * sdb_ast_typed_create:
354 * Creates an AST node representing a typed expression. Thew newly created
355 * node takes ownership of the expr node.
356 */
357 sdb_ast_node_t *
358 sdb_ast_typed_create(int type, sdb_ast_node_t *expr);
360 /*
361 * sdb_ast_const_create:
362 * Creates an AST node representing a constant value. The newly created node
363 * takes ownership of the value object.
364 */
365 sdb_ast_node_t *
366 sdb_ast_const_create(sdb_data_t value);
368 /*
369 * sdb_ast_value_create:
370 * Creates an AST node representing an object-specific value (sibling nodes,
371 * attributes, or field values). The newly created node takes ownership of the
372 * string value.
373 */
374 sdb_ast_node_t *
375 sdb_ast_value_create(int type, char *name);
377 /*
378 * sdb_ast_fetch_create:
379 * Creates an AST node representing a FETCH command. The newly created node
380 * takes ownership of the strings and the filter node.
381 */
382 sdb_ast_node_t *
383 sdb_ast_fetch_create(int obj_type, char *hostname, char *name,
384 sdb_ast_node_t *filter);
386 /*
387 * sdb_ast_list_create:
388 * Creates an AST node representing a LIST command. The newly created node
389 * takes ownership of the filter node.
390 */
391 sdb_ast_node_t *
392 sdb_ast_list_create(int obj_type, sdb_ast_node_t *filter);
394 /*
395 * sdb_ast_lookup_create:
396 * Creates an AST node representing a LOOKUP command. The newly created node
397 * takes ownership of the matcher and filter nodes.
398 */
399 sdb_ast_node_t *
400 sdb_ast_lookup_create(int obj_type, sdb_ast_node_t *matcher,
401 sdb_ast_node_t *filter);
403 /*
404 * sdb_ast_store_create:
405 * Creates an AST node representing a STORE command. Thew newly created node
406 * takes ownership of all strings and the value object.
407 */
408 sdb_ast_node_t *
409 sdb_ast_store_create(int obj_type, char *hostname,
410 int parent_type, char *parent, char *name, sdb_time_t last_update,
411 char *store_type, char *store_id, sdb_data_t value);
413 /*
414 * sdb_ast_timeseries_create:
415 * Creates an AST node representing a TIMESERIES command. The newly created
416 * node takes ownership of the strings.
417 */
418 sdb_ast_node_t *
419 sdb_ast_timeseries_create(char *hostname, char *metric,
420 sdb_time_t start, sdb_time_t end);
422 #ifdef __cplusplus
423 } /* extern "C" */
424 #endif
426 #endif /* ! SDB_PARSER_AST_H */
428 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */