Code

parser: Add support for <expr> IS [NOT] TRUE / FALSE queries.
[sysdb.git] / src / parser / analyzer.c
1 /*
2  * SysDB - src/parser/analyzer.c
3  * Copyright (C) 2014-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 #include "sysdb.h"
30 #include "parser/ast.h"
31 #include "parser/parser.h"
32 #include "utils/error.h"
33 #include "utils/strbuf.h"
35 #include <assert.h>
36 #include <stdarg.h>
38 #define VALID_OBJ_TYPE(t) ((SDB_HOST <= (t)) && ((t) <= SDB_METRIC))
40 #define FILTER_CONTEXT -1
41 #define UNSPEC_CONTEXT -2
43 static int
44 analyze_node(int context, sdb_ast_node_t *node, sdb_strbuf_t *errbuf);
46 /*
47  * error reporting
48  */
50 static void
51 op_error(sdb_strbuf_t *errbuf, sdb_ast_op_t *op, const char *reason)
52 {
53         sdb_strbuf_sprintf(errbuf, "Invalid operation %s %s %s (%s)",
54                         SDB_TYPE_TO_STRING(op->left->data_type),
55                         SDB_AST_OP_TO_STRING(op->kind),
56                         SDB_TYPE_TO_STRING(op->right->data_type),
57                         reason);
58 } /* op_error */
60 static void
61 __attribute__((format(printf, 3, 4)))
62 iter_error(sdb_strbuf_t *errbuf, sdb_ast_iter_t *iter, const char *reason, ...)
63 {
64         char r[1024];
65         va_list ap;
67         va_start(ap, reason);
68         vsnprintf(r, sizeof(r), reason, ap);
69         va_end(ap);
71         assert((iter->expr->type == SDB_AST_TYPE_OPERATOR)
72                         && (! SDB_AST_OP(iter->expr)->left));
73         sdb_strbuf_sprintf(errbuf, "Invalid iterator %s %s %s %s (%s)",
74                         SDB_AST_OP_TO_STRING(iter->kind),
75                         SDB_TYPE_TO_STRING(iter->iter->data_type),
76                         SDB_AST_OP_TO_STRING(SDB_AST_OP(iter->expr)->kind),
77                         SDB_TYPE_TO_STRING(SDB_AST_OP(iter->expr)->right->data_type),
78                         r);
79 } /* iter_error */
81 /*
82  * expression nodes
83  */
85 static int
86 analyze_logical(int context, sdb_ast_op_t *op, sdb_strbuf_t *errbuf)
87 {
88         switch (op->kind) {
89         case SDB_AST_OR:
90         case SDB_AST_AND:
91                 if (! SDB_AST_IS_LOGICAL(op->left)) {
92                         sdb_strbuf_sprintf(errbuf, "Invalid left operand (%s) "
93                                         "in %s expression", SDB_AST_TYPE_TO_STRING(op->left),
94                                         SDB_AST_OP_TO_STRING(op->kind));
95                         return -1;
96                 }
97                 if (analyze_node(context, op->left, errbuf))
98                         return -1;
99                 /* fallthrough */
100         case SDB_AST_NOT:
101                 if (! SDB_AST_IS_LOGICAL(op->right)) {
102                         sdb_strbuf_sprintf(errbuf, "Invalid right operand (%s) "
103                                         "in %s expression", SDB_AST_TYPE_TO_STRING(op->right),
104                                         SDB_AST_OP_TO_STRING(op->kind));
105                         return -1;
106                 }
107                 if (analyze_node(context, op->right, errbuf))
108                         return -1;
109                 break;
111         case SDB_AST_LT:
112         case SDB_AST_LE:
113         case SDB_AST_EQ:
114         case SDB_AST_NE:
115         case SDB_AST_GE:
116         case SDB_AST_GT:
117         {
118                 if (analyze_node(context, op->left, errbuf))
119                         return -1;
120                 if (analyze_node(context, op->right, errbuf))
121                         return -1;
123                 if ((op->left->data_type > 0) && (op->right->data_type > 0)) {
124                         if (op->left->data_type == op->right->data_type)
125                                 return 0;
126                         op_error(errbuf, op, "type mismatch");
127                         return -1;
128                 }
129                 if ((op->left->data_type > 0) && (op->left->data_type & SDB_TYPE_ARRAY)) {
130                         op_error(errbuf, op, "array not allowed");
131                         return -1;
132                 }
133                 if ((op->right->data_type > 0) && (op->right->data_type & SDB_TYPE_ARRAY)) {
134                         op_error(errbuf, op, "array not allowed");
135                         return -1;
136                 }
137                 break;
138         }
140         case SDB_AST_REGEX:
141         case SDB_AST_NREGEX:
142                 if (analyze_node(context, op->left, errbuf))
143                         return -1;
144                 if (analyze_node(context, op->right, errbuf))
145                         return -1;
147                 /* all types are supported for the left operand
148                  * TODO: introduce a cast operator if it's not a string */
149                 if ((op->right->data_type > 0)
150                                 && (op->right->data_type != SDB_TYPE_REGEX)
151                                 && (op->right->data_type != SDB_TYPE_STRING)) {
152                         op_error(errbuf, op, "invalid regex");
153                         return -1;
154                 }
155                 break;
157         case SDB_AST_ISNULL:
158         case SDB_AST_ISTRUE:
159         case SDB_AST_ISFALSE:
160                 if (analyze_node(context, op->right, errbuf))
161                         return -1;
162                 break;
164         case SDB_AST_IN:
165                 if (analyze_node(context, op->left, errbuf))
166                         return -1;
167                 if (analyze_node(context, op->right, errbuf))
168                         return -1;
170                 if ((op->right->data_type > 0) && (! (op->right->data_type & SDB_TYPE_ARRAY))) {
171                         op_error(errbuf, op, "array expected");
172                         return -1;
173                 }
174                 /* the left operand may be a scalar or an array but the element
175                  * type has to match */
176                 if ((op->left->data_type > 0) && (op->right->data_type > 0)
177                                 && ((op->left->data_type & 0xff) != (op->right->data_type & 0xff))) {
178                         op_error(errbuf, op, "type mismatch");
179                         return -1;
180                 }
181                 break;
183         default:
184                 sdb_strbuf_sprintf(errbuf, "Unknown operand type %d", op->kind);
185                 return -1;
186         }
187         return 0;
188 } /* analyze_logical */
190 static int
191 analyze_arith(int context, sdb_ast_op_t *op, sdb_strbuf_t *errbuf)
193         if (analyze_node(context, op->left, errbuf))
194                 return -1;
195         if (analyze_node(context, op->right, errbuf))
196                 return -1;
197         SDB_AST_NODE(op)->data_type = sdb_data_expr_type(SDB_AST_OP_TO_DATA_OP(op->kind),
198                         op->left->data_type, op->right->data_type);
200         if ((op->left->data_type > 0) && (op->right->data_type > 0)
201                         && (SDB_AST_NODE(op)->data_type <= 0)) {
202                 op_error(errbuf, op, "type mismatch");
203                 return -1;
204         }
206         /* TODO: replace constant arithmetic operations with a constant value */
207         return 0;
208 } /* analyze_arith */
210 static int
211 analyze_iter(int context, sdb_ast_iter_t *iter, sdb_strbuf_t *errbuf)
213         sdb_ast_const_t c = SDB_AST_CONST_INIT;
214         int iter_context = context;
215         int status;
217         if (iter->iter->type == SDB_AST_TYPE_TYPED)
218                 iter_context = SDB_AST_TYPED(iter->iter)->type;
220         if (analyze_node(iter_context, iter->iter, errbuf))
221                 return -1;
222         /* TODO: support other setups as well */
223         assert((iter->expr->type == SDB_AST_TYPE_OPERATOR)
224                         && (! SDB_AST_OP(iter->expr)->left));
225         /* determine the data-type for better error messages */
226         analyze_node(iter_context, SDB_AST_OP(iter->expr)->right, NULL);
228         if (iter->iter->type == SDB_AST_TYPE_TYPED) {
229                 int iter_type = SDB_AST_TYPED(iter->iter)->type;
231                 c.value.type = iter->iter->data_type;
233                 if (iter_type == SDB_ATTRIBUTE) {
234                         /* attributes are always iterable */
235                 }
236                 else if ((context != SDB_HOST) && (context != SDB_SERVICE)
237                                 && (context != SDB_METRIC) && (context != UNSPEC_CONTEXT)) {
238                         iter_error(errbuf, iter, "%s not iterable in %s context",
239                                         SDB_STORE_TYPE_TO_NAME(iter_type),
240                                         SDB_STORE_TYPE_TO_NAME(context));
241                         return -1;
242                 }
244                 if ((context == iter_type)
245                                 || ((iter_type != SDB_SERVICE)
246                                         && (iter_type != SDB_METRIC)
247                                         && (iter_type != SDB_ATTRIBUTE))
248                                 || ((context == SDB_SERVICE)
249                                         && (iter_type == SDB_METRIC))
250                                 || ((context == SDB_METRIC)
251                                         && (iter_type == SDB_SERVICE))) {
252                         iter_error(errbuf, iter, "%s not iterable in %s context",
253                                         SDB_STORE_TYPE_TO_NAME(iter_type),
254                                         SDB_STORE_TYPE_TO_NAME(context));
255                         return -1;
256                 }
257         }
258         else if (iter->iter->type == SDB_AST_TYPE_VALUE) {
259                 int iter_type = SDB_AST_VALUE(iter->iter)->type;
261                 c.value.type = iter->iter->data_type & 0xff;
263                 if (iter_type != SDB_FIELD_BACKEND) {
264                         iter_error(errbuf, iter, "%s not iterable in %s context",
265                                         (iter_type == SDB_ATTRIBUTE)
266                                                 ? "attribute"
267                                                 : SDB_FIELD_TO_NAME(iter_type),
268                                         SDB_STORE_TYPE_TO_NAME(context));
269                         return -1;
270                 }
271         }
272         else if (iter->iter->type == SDB_AST_TYPE_CONST) {
273                 c.value.type = iter->iter->data_type & 0xff;
275                 if (! (SDB_AST_CONST(iter->iter)->value.type & SDB_TYPE_ARRAY)) {
276                         iter_error(errbuf, iter, "%s not iterable",
277                                         SDB_TYPE_TO_STRING(SDB_AST_CONST(iter->iter)->value.type));
278                         return -1;
279                 }
280         }
281         else {
282                 /* TODO: if we know the data-type of iter->iter and it's an array,
283                  * we should support an iterator for it as well */
284                 iter_error(errbuf, iter, "%s expression not iterable",
285                                 SDB_AST_TYPE_TO_STRING(iter->iter));
286                 return -1;
287         }
289         SDB_AST_OP(iter->expr)->left = SDB_AST_NODE(&c);
290         status = analyze_node(context, iter->expr, errbuf);
291         SDB_AST_OP(iter->expr)->left = NULL;
292         if (status)
293                 return -1;
294         return 0;
295 } /* analyze_iter */
297 static int
298 analyze_const(int __attribute__((unused)) context, sdb_ast_const_t *c,
299                 sdb_strbuf_t __attribute__((unused)) *errbuf)
301         SDB_AST_NODE(c)->data_type = c->value.type;
302         return 0;
303 } /* analyze_const */
305 static int
306 analyze_value(int context, sdb_ast_value_t *v, sdb_strbuf_t *errbuf)
308         if (v->type != SDB_ATTRIBUTE)
309                 SDB_AST_NODE(v)->data_type = SDB_FIELD_TYPE(v->type);
311         if ((v->type != SDB_ATTRIBUTE) && v->name) {
312                 sdb_strbuf_sprintf(errbuf, "Invalid expression %s[%s]",
313                                 SDB_FIELD_TO_NAME(v->type), v->name);
314                 return -1;
315         }
316         else if ((v->type == SDB_ATTRIBUTE) && (! v->name)) {
317                 sdb_strbuf_sprintf(errbuf, "Invalid expression attribute[] "
318                                 "(missing name)");
319                 return -1;
320         }
322         if ((context != SDB_ATTRIBUTE) && (v->type == SDB_FIELD_VALUE)) {
323                 sdb_strbuf_sprintf(errbuf, "Invalid expression %s.value",
324                                 SDB_FIELD_TO_NAME(v->type));
325                 return -1;
326         }
327         return 0;
328 } /* analyze_value */
330 static int
331 analyze_typed(int context, sdb_ast_typed_t *t, sdb_strbuf_t *errbuf)
333         if ((t->expr->type != SDB_AST_TYPE_VALUE)
334                         && (t->expr->type != SDB_AST_TYPE_TYPED)) {
335                 sdb_strbuf_sprintf(errbuf, "Invalid expression %s.%s",
336                                 SDB_STORE_TYPE_TO_NAME(t->type),
337                                 SDB_AST_TYPE_TO_STRING(t->expr));
338                 return -1;
339         }
340         if (analyze_node(t->type, t->expr, errbuf))
341                 return -1;
342         SDB_AST_NODE(t)->data_type = t->expr->data_type;
344         if ((t->type != SDB_ATTRIBUTE) && (! VALID_OBJ_TYPE(t->type))) {
345                 sdb_strbuf_sprintf(errbuf, "Invalid expression %#x.%s",
346                                 t->type, SDB_AST_TYPE_TO_STRING(t->expr));
347                 return -1;
348         }
350         /* self-references are allowed and services and metrics may reference
351          * their parent host; everything may reference attributes */
352         if ((context != t->type) && (context > 0)
353                         && (((context != SDB_SERVICE) && (context != SDB_METRIC))
354                                 || (t->type != SDB_HOST))
355                         && (t->type != SDB_ATTRIBUTE)) {
356                 sdb_strbuf_sprintf(errbuf, "Invalid expression %s.%s in %s context",
357                                 SDB_STORE_TYPE_TO_NAME(t->type),
358                                 SDB_AST_TYPE_TO_STRING(t->expr),
359                                 context == -1 ? "generic" : SDB_STORE_TYPE_TO_NAME(context));
360                 return -1;
361         }
362         return 0;
363 } /* analyze_typed */
365 static int
366 analyze_node(int context, sdb_ast_node_t *node, sdb_strbuf_t *errbuf)
368         if (! node) {
369                 sdb_strbuf_sprintf(errbuf, "Empty AST node");
370                 return -1;
371         }
373         /* unknown by default */
374         node->data_type = -1;
376         if ((node->type == SDB_AST_TYPE_OPERATOR)
377                         && (SDB_AST_IS_LOGICAL(node)))
378                 return analyze_logical(context, SDB_AST_OP(node), errbuf);
379         else if ((node->type == SDB_AST_TYPE_OPERATOR)
380                         && (SDB_AST_IS_ARITHMETIC(node)))
381                 return analyze_arith(context, SDB_AST_OP(node), errbuf);
382         else if (node->type == SDB_AST_TYPE_ITERATOR)
383                 return analyze_iter(context, SDB_AST_ITER(node), errbuf);
384         else if (node->type == SDB_AST_TYPE_CONST)
385                 return analyze_const(context, SDB_AST_CONST(node), errbuf);
386         else if (node->type == SDB_AST_TYPE_VALUE)
387                 return analyze_value(context, SDB_AST_VALUE(node), errbuf);
388         else if (node->type == SDB_AST_TYPE_TYPED)
389                 return analyze_typed(context, SDB_AST_TYPED(node), errbuf);
391         sdb_strbuf_sprintf(errbuf, "Invalid expression node "
392                         "of type %#x", node->type);
393         return -1;
394 } /* analyze_node */
396 /*
397  * top level / command nodes
398  */
400 static int
401 analyze_fetch(sdb_ast_fetch_t *fetch, sdb_strbuf_t *errbuf)
403         if (! VALID_OBJ_TYPE(fetch->obj_type)) {
404                 sdb_strbuf_sprintf(errbuf, "Invalid object type %#x "
405                                 "in FETCH command", fetch->obj_type);
406                 return -1;
407         }
408         if (! fetch->name) {
409                 sdb_strbuf_sprintf(errbuf, "Missing object name in "
410                                 "FETCH %s command", SDB_STORE_TYPE_TO_NAME(fetch->obj_type));
411                 return -1;
412         }
414         if ((fetch->obj_type == SDB_HOST) && fetch->hostname) {
415                 sdb_strbuf_sprintf(errbuf, "Unexpected parent hostname '%s' "
416                                 "in FETCH HOST command", fetch->hostname);
417                 return -1;
418         }
419         else if ((fetch->obj_type != SDB_HOST) && (! fetch->hostname)) {
420                 sdb_strbuf_sprintf(errbuf, "Missing parent hostname for '%s' "
421                                 "in FETCH %s command", fetch->name,
422                                 SDB_STORE_TYPE_TO_NAME(fetch->obj_type));
423                 return -1;
424         }
426         if (fetch->filter)
427                 return analyze_node(FILTER_CONTEXT, fetch->filter, errbuf);
428         return 0;
429 } /* analyze_fetch */
431 static int
432 analyze_list(sdb_ast_list_t *list, sdb_strbuf_t *errbuf)
434         if (! VALID_OBJ_TYPE(list->obj_type)) {
435                 sdb_strbuf_sprintf(errbuf, "Invalid object type %#x "
436                                 "in LIST command", list->obj_type);
437                 return -1;
438         }
439         if (list->filter)
440                 return analyze_node(FILTER_CONTEXT, list->filter, errbuf);
441         return 0;
442 } /* analyze_list */
444 static int
445 analyze_lookup(sdb_ast_lookup_t *lookup, sdb_strbuf_t *errbuf)
447         if (! VALID_OBJ_TYPE(lookup->obj_type)) {
448                 sdb_strbuf_sprintf(errbuf, "Invalid object type %#x "
449                                 "in LOOKUP command", lookup->obj_type);
450                 return -1;
451         }
452         if (lookup->matcher)
453                 if (analyze_node(lookup->obj_type, lookup->matcher, errbuf))
454                         return -1;
455         if (lookup->filter)
456                 return analyze_node(FILTER_CONTEXT, lookup->filter, errbuf);
457         return 0;
458 } /* analyze_lookup */
460 static int
461 analyze_store(sdb_ast_store_t *st, sdb_strbuf_t *errbuf)
463         if ((st->obj_type != SDB_ATTRIBUTE)
464                         && (! VALID_OBJ_TYPE(st->obj_type))) {
465                 sdb_strbuf_sprintf(errbuf, "Invalid object type %#x "
466                                 "in STORE command", st->obj_type);
467                 return -1;
468         }
469         if (! st->name) {
470                 sdb_strbuf_sprintf(errbuf, "Missing object name in "
471                                 "STORE %s command", SDB_STORE_TYPE_TO_NAME(st->obj_type));
472                 return -1;
473         }
475         if ((st->obj_type == SDB_HOST) && st->hostname) {
476                 sdb_strbuf_sprintf(errbuf, "Unexpected parent hostname '%s' "
477                                 "in STORE HOST command", st->hostname);
478                 return -1;
479         }
480         else if ((st->obj_type != SDB_HOST) && (! st->hostname)) {
481                 sdb_strbuf_sprintf(errbuf, "Missing parent hostname for '%s' "
482                                 "in STORE %s command", st->name,
483                                 SDB_STORE_TYPE_TO_NAME(st->obj_type));
484                 return -1;
485         }
487         if (st->obj_type == SDB_ATTRIBUTE) {
488                 if ((st->parent_type <= 0) && st->parent) {
489                         sdb_strbuf_sprintf(errbuf, "Unexpected parent hostname '%s' "
490                                         "in STORE %s command", st->parent,
491                                         SDB_STORE_TYPE_TO_NAME(st->obj_type));
492                         return -1;
493                 }
494                 else if (st->parent_type > 0) {
495                         if (! VALID_OBJ_TYPE(st->parent_type)) {
496                                 sdb_strbuf_sprintf(errbuf, "Invalid parent type %#x "
497                                                 "in STORE %s command", st->parent_type,
498                                                 SDB_STORE_TYPE_TO_NAME(st->obj_type));
499                                 return -1;
500                         }
501                         if (! st->parent) {
502                                 sdb_strbuf_sprintf(errbuf, "Missing %s parent name "
503                                                 "in STORE %s command",
504                                                 SDB_STORE_TYPE_TO_NAME(st->parent_type),
505                                                 SDB_STORE_TYPE_TO_NAME(st->obj_type));
506                                 return -1;
507                         }
508                 }
509         }
510         else if ((st->parent_type > 0) || st->parent) {
511                 sdb_strbuf_sprintf(errbuf, "Unexpected %s parent name '%s' "
512                                 "in STORE %s command",
513                                 SDB_STORE_TYPE_TO_NAME(st->parent_type),
514                                 st->parent ? st->parent : "<unknown>",
515                                 SDB_STORE_TYPE_TO_NAME(st->obj_type));
516                 return -1;
517         }
519         if (st->obj_type == SDB_METRIC) {
520                 if ((! st->store_type) != (! st->store_id)) {
521                         sdb_strbuf_sprintf(errbuf, "Incomplete metric store %s %s "
522                                         "in STORE METRIC command",
523                                         st->store_type ? st->store_type : "<unknown>",
524                                         st->store_id ? st->store_id : "<unknown>");
525                         return -1;
526                 }
527         }
528         else if (st->store_type || st->store_id) {
529                 sdb_strbuf_sprintf(errbuf, "Unexpected metric store %s %s "
530                                 "in STORE %s command",
531                                 st->store_type ? st->store_type : "<unknown>",
532                                 st->store_id ? st->store_id : "<unknown>",
533                                 SDB_STORE_TYPE_TO_NAME(st->obj_type));
534                 return -1;
535         }
537         if ((! (st->obj_type == SDB_ATTRIBUTE))
538                         && (st->value.type != SDB_TYPE_NULL)) {
539                 char v_str[sdb_data_format(&st->value, NULL, 0, SDB_DOUBLE_QUOTED) + 1];
540                 sdb_data_format(&st->value, v_str, sizeof(v_str), SDB_DOUBLE_QUOTED);
541                 sdb_strbuf_sprintf(errbuf, "Unexpected value %s in STORE %s command",
542                                 v_str, SDB_STORE_TYPE_TO_NAME(st->obj_type));
543                 return -1;
544         }
545         return 0;
546 } /* analyze_store */
548 static int
549 analyze_timeseries(sdb_ast_timeseries_t *ts, sdb_strbuf_t *errbuf)
551         if (! ts->hostname) {
552                 sdb_strbuf_sprintf(errbuf, "Missing hostname in STORE command");
553                 return -1;
554         }
555         if (! ts->metric) {
556                 sdb_strbuf_sprintf(errbuf, "Missing metric name in STORE command");
557                 return -1;
558         }
559         if (ts->end <= ts->start) {
560                 char start_str[64], end_str[64];
561                 sdb_strftime(start_str, sizeof(start_str), "%F %T Tz", ts->start);
562                 sdb_strftime(end_str, sizeof(end_str), "%F %T Tz", ts->end);
563                 sdb_strbuf_sprintf(errbuf, "Start time (%s) greater than "
564                                 "end time (%s) in STORE command", start_str, end_str);
565                 return -1;
566         }
567         return 0;
568 } /* analyze_timeseries */
570 /*
571  * public API
572  */
574 int
575 sdb_parser_analyze(sdb_ast_node_t *node, sdb_strbuf_t *errbuf)
577         if (! node) {
578                 sdb_strbuf_sprintf(errbuf, "Empty AST node");
579                 return -1;
580         }
582         /* top-level nodes don't have a type */
583         node->data_type = -1;
585         if (node->type == SDB_AST_TYPE_FETCH)
586                 return analyze_fetch(SDB_AST_FETCH(node), errbuf);
587         else if (node->type == SDB_AST_TYPE_LIST)
588                 return analyze_list(SDB_AST_LIST(node), errbuf);
589         else if (node->type == SDB_AST_TYPE_LOOKUP)
590                 return analyze_lookup(SDB_AST_LOOKUP(node), errbuf);
591         else if (node->type == SDB_AST_TYPE_STORE)
592                 return analyze_store(SDB_AST_STORE(node), errbuf);
593         else if (node->type == SDB_AST_TYPE_TIMESERIES)
594                 return analyze_timeseries(SDB_AST_TIMESERIES(node), errbuf);
596         sdb_strbuf_sprintf(errbuf, "Invalid top-level AST node "
597                         "of type %#x", node->type);
598         return -1;
599 } /* sdb_parser_analyze */
601 int
602 sdb_parser_analyze_conditional(sdb_ast_node_t *node, sdb_strbuf_t *errbuf)
604         if (! node) {
605                 sdb_strbuf_sprintf(errbuf, "Empty conditional node");
606                 return -1;
607         }
608         if (! SDB_AST_IS_LOGICAL(node)) {
609                 sdb_strbuf_sprintf(errbuf, "Not a conditional node (got %s)",
610                                 SDB_AST_TYPE_TO_STRING(node));
611                 return -1;
612         }
613         return analyze_node(UNSPEC_CONTEXT, node, errbuf);
614 } /* sdb_parser_analyze_conditional */
616 int
617 sdb_parser_analyze_arith(sdb_ast_node_t *node, sdb_strbuf_t *errbuf)
619         if (! node) {
620                 sdb_strbuf_sprintf(errbuf, "Empty arithmetic node");
621                 return -1;
622         }
623         if (! SDB_AST_IS_ARITHMETIC(node)) {
624                 sdb_strbuf_sprintf(errbuf, "Not an arithmetic node (got %s)",
625                                 SDB_AST_TYPE_TO_STRING(node));
626                 return -1;
627         }
628         return analyze_node(UNSPEC_CONTEXT, node, errbuf);
629 } /* sdb_parser_analyze_arith */
631 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */