Code

parser/analyzer: Migrate value expression checks.
[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                 if (analyze_node(context, op->right, errbuf))
159                         return -1;
160                 break;
162         case SDB_AST_IN:
163                 if (analyze_node(context, op->left, errbuf))
164                         return -1;
165                 if (analyze_node(context, op->right, errbuf))
166                         return -1;
168                 if ((op->right->data_type > 0) && (! (op->right->data_type & SDB_TYPE_ARRAY))) {
169                         op_error(errbuf, op, "array expected");
170                         return -1;
171                 }
172                 /* the left operand may be a scalar or an array but the element
173                  * type has to match */
174                 if ((op->left->data_type > 0) && (op->right->data_type > 0)
175                                 && ((op->left->data_type & 0xff) != (op->right->data_type & 0xff))) {
176                         op_error(errbuf, op, "type mismatch");
177                         return -1;
178                 }
179                 break;
181         default:
182                 sdb_strbuf_sprintf(errbuf, "Unknown operand type %d", op->kind);
183                 return -1;
184         }
185         return 0;
186 } /* analyze_logical */
188 static int
189 analyze_arith(int context, sdb_ast_op_t *op, sdb_strbuf_t *errbuf)
191         if (analyze_node(context, op->left, errbuf))
192                 return -1;
193         if (analyze_node(context, op->right, errbuf))
194                 return -1;
195         SDB_AST_NODE(op)->data_type = sdb_data_expr_type(SDB_AST_OP_TO_DATA_OP(op->kind),
196                         op->left->data_type, op->right->data_type);
198         if ((op->left->data_type > 0) && (op->right->data_type > 0)
199                         && (SDB_AST_NODE(op)->data_type <= 0)) {
200                 op_error(errbuf, op, "type mismatch");
201                 return -1;
202         }
204         /* TODO: replace constant arithmetic operations with a constant value */
205         return 0;
206 } /* analyze_arith */
208 static int
209 analyze_iter(int context, sdb_ast_iter_t *iter, sdb_strbuf_t *errbuf)
211         sdb_ast_const_t c = SDB_AST_CONST_INIT;
212         int iter_context = context;
213         int status;
215         if (iter->iter->type == SDB_AST_TYPE_TYPED)
216                 iter_context = SDB_AST_TYPED(iter->iter)->type;
218         if (analyze_node(iter_context, iter->iter, errbuf))
219                 return -1;
220         if (iter->iter->data_type > 0)
221                 c.value.type = iter->iter->data_type & 0xff;
222         else
223                 c.value.type = -1;
225         /* TODO: support other setups as well */
226         assert((iter->expr->type == SDB_AST_TYPE_OPERATOR)
227                         && (! SDB_AST_OP(iter->expr)->left));
228         SDB_AST_OP(iter->expr)->left = SDB_AST_NODE(&c);
229         status = analyze_node(context, iter->expr, errbuf);
230         SDB_AST_OP(iter->expr)->left = NULL;
231         if (status)
232                 return -1;
234         if (iter->iter->type == SDB_AST_TYPE_TYPED) {
235                 int iter_type = SDB_AST_TYPED(iter->iter)->type;
237                 if (iter_type == SDB_ATTRIBUTE) {
238                         /* attributes are always iterable */
239                 }
240                 else if ((context != SDB_HOST) && (context != SDB_SERVICE)
241                                 && (context != SDB_METRIC) && (context != UNSPEC_CONTEXT)) {
242                         iter_error(errbuf, iter, "%s not iterable in %s context",
243                                         SDB_STORE_TYPE_TO_NAME(iter_type),
244                                         SDB_STORE_TYPE_TO_NAME(context));
245                         return -1;
246                 }
248                 if ((context == iter_type)
249                                 || ((iter_type != SDB_SERVICE)
250                                         && (iter_type != SDB_METRIC)
251                                         && (iter_type != SDB_ATTRIBUTE))
252                                 || ((context == SDB_SERVICE)
253                                         && (iter_type == SDB_METRIC))
254                                 || ((context == SDB_METRIC)
255                                         && (iter_type == SDB_SERVICE))) {
256                         iter_error(errbuf, iter, "%s not iterable in %s context",
257                                         SDB_STORE_TYPE_TO_NAME(iter_type),
258                                         SDB_STORE_TYPE_TO_NAME(context));
259                         return -1;
260                 }
261         }
262         else if (iter->iter->type == SDB_AST_TYPE_VALUE) {
263                 int iter_type = SDB_AST_VALUE(iter->iter)->type;
265                 if (iter_type == SDB_FIELD_BACKEND) {
266                         /* backends are always iterable */
267                 }
268                 else if ((context != SDB_HOST) && (context != SDB_SERVICE)
269                                 && (context != SDB_METRIC) && (context != SDB_ATTRIBUTE)
270                                 && (context != UNSPEC_CONTEXT)) {
271                         iter_error(errbuf, iter, "%s not iterable in %s context",
272                                         (iter_type == SDB_ATTRIBUTE)
273                                                 ? "attribute"
274                                                 : SDB_FIELD_TO_NAME(iter_type),
275                                         SDB_STORE_TYPE_TO_NAME(context));
276                         return -1;
277                 }
278         }
279         else if (iter->iter->type == SDB_AST_TYPE_CONST) {
280                 if (! (SDB_AST_CONST(iter->iter)->value.type & SDB_TYPE_ARRAY)) {
281                         iter_error(errbuf, iter, "%s not iterable",
282                                         SDB_TYPE_TO_STRING(SDB_AST_CONST(iter->iter)->value.type));
283                         return -1;
284                 }
285         }
286         return 0;
287 } /* analyze_iter */
289 static int
290 analyze_const(int __attribute__((unused)) context, sdb_ast_const_t *c,
291                 sdb_strbuf_t __attribute__((unused)) *errbuf)
293         SDB_AST_NODE(c)->data_type = c->value.type;
294         return 0;
295 } /* analyze_const */
297 static int
298 analyze_value(int context, sdb_ast_value_t *v, sdb_strbuf_t *errbuf)
300         if (v->type != SDB_ATTRIBUTE)
301                 SDB_AST_NODE(v)->data_type = SDB_FIELD_TYPE(v->type);
303         if ((v->type != SDB_ATTRIBUTE) && v->name) {
304                 sdb_strbuf_sprintf(errbuf, "Invalid expression %s[%s]",
305                                 SDB_FIELD_TO_NAME(v->type), v->name);
306                 return -1;
307         }
308         else if ((v->type == SDB_ATTRIBUTE) && (! v->name)) {
309                 sdb_strbuf_sprintf(errbuf, "Invalid expression attribute[] "
310                                 "(missing name)");
311                 return -1;
312         }
314         if ((context != SDB_ATTRIBUTE) && (v->type == SDB_FIELD_VALUE)) {
315                 sdb_strbuf_sprintf(errbuf, "Invalid expression %s.value",
316                                 SDB_FIELD_TO_NAME(v->type));
317                 return -1;
318         }
319         return 0;
320 } /* analyze_value */
322 static int
323 analyze_typed(int context, sdb_ast_typed_t *t, sdb_strbuf_t *errbuf)
325         if ((t->expr->type != SDB_AST_TYPE_VALUE)
326                         && (t->expr->type != SDB_AST_TYPE_TYPED)) {
327                 sdb_strbuf_sprintf(errbuf, "Invalid expression %s.%s",
328                                 SDB_STORE_TYPE_TO_NAME(t->type),
329                                 SDB_AST_TYPE_TO_STRING(t->expr));
330                 return -1;
331         }
332         if (analyze_node(t->type, t->expr, errbuf))
333                 return -1;
334         SDB_AST_NODE(t)->data_type = t->expr->data_type;
336         if ((t->type != SDB_ATTRIBUTE) && (! VALID_OBJ_TYPE(t->type))) {
337                 sdb_strbuf_sprintf(errbuf, "Invalid expression %#x.%s",
338                                 t->type, SDB_AST_TYPE_TO_STRING(t->expr));
339                 return -1;
340         }
342         /* self-references are allowed and services and metrics may reference
343          * their parent host; everything may reference attributes */
344         if ((context != t->type) && (context != UNSPEC_CONTEXT)
345                         && (((context != SDB_SERVICE) && (context != SDB_METRIC))
346                                 || (t->type != SDB_HOST))
347                         && (t->type != SDB_ATTRIBUTE)) {
348                 sdb_strbuf_sprintf(errbuf, "Invalid expression %s.%s in %s context",
349                                 SDB_STORE_TYPE_TO_NAME(t->type),
350                                 SDB_AST_TYPE_TO_STRING(t->expr),
351                                 context == -1 ? "generic" : SDB_STORE_TYPE_TO_NAME(context));
352                 return -1;
353         }
354         return 0;
355 } /* analyze_typed */
357 static int
358 analyze_node(int context, sdb_ast_node_t *node, sdb_strbuf_t *errbuf)
360         if (! node) {
361                 sdb_strbuf_sprintf(errbuf, "Empty AST node");
362                 return -1;
363         }
365         /* unknown by default */
366         node->data_type = -1;
368         if ((node->type == SDB_AST_TYPE_OPERATOR)
369                         && (SDB_AST_IS_LOGICAL(node)))
370                 return analyze_logical(context, SDB_AST_OP(node), errbuf);
371         else if ((node->type == SDB_AST_TYPE_OPERATOR)
372                         && (SDB_AST_IS_ARITHMETIC(node)))
373                 return analyze_arith(context, SDB_AST_OP(node), errbuf);
374         else if (node->type == SDB_AST_TYPE_ITERATOR)
375                 return analyze_iter(context, SDB_AST_ITER(node), errbuf);
376         else if (node->type == SDB_AST_TYPE_CONST)
377                 return analyze_const(context, SDB_AST_CONST(node), errbuf);
378         else if (node->type == SDB_AST_TYPE_VALUE)
379                 return analyze_value(context, SDB_AST_VALUE(node), errbuf);
380         else if (node->type == SDB_AST_TYPE_TYPED)
381                 return analyze_typed(context, SDB_AST_TYPED(node), errbuf);
383         sdb_strbuf_sprintf(errbuf, "Invalid expression node "
384                         "of type %#x", node->type);
385         return -1;
386 } /* analyze_node */
388 /*
389  * top level / command nodes
390  */
392 static int
393 analyze_fetch(sdb_ast_fetch_t *fetch, sdb_strbuf_t *errbuf)
395         if (! VALID_OBJ_TYPE(fetch->obj_type)) {
396                 sdb_strbuf_sprintf(errbuf, "Invalid object type %#x "
397                                 "in FETCH command", fetch->obj_type);
398                 return -1;
399         }
400         if (! fetch->name) {
401                 sdb_strbuf_sprintf(errbuf, "Missing object name in "
402                                 "FETCH %s command", SDB_STORE_TYPE_TO_NAME(fetch->obj_type));
403                 return -1;
404         }
406         if ((fetch->obj_type == SDB_HOST) && fetch->hostname) {
407                 sdb_strbuf_sprintf(errbuf, "Unexpected parent hostname '%s' "
408                                 "in FETCH HOST command", fetch->hostname);
409                 return -1;
410         }
411         else if ((fetch->obj_type != SDB_HOST) && (! fetch->hostname)) {
412                 sdb_strbuf_sprintf(errbuf, "Missing parent hostname for '%s' "
413                                 "in FETCH %s command", fetch->name,
414                                 SDB_STORE_TYPE_TO_NAME(fetch->obj_type));
415                 return -1;
416         }
418         if (fetch->filter)
419                 return analyze_node(FILTER_CONTEXT, fetch->filter, errbuf);
420         return 0;
421 } /* analyze_fetch */
423 static int
424 analyze_list(sdb_ast_list_t *list, sdb_strbuf_t *errbuf)
426         if (! VALID_OBJ_TYPE(list->obj_type)) {
427                 sdb_strbuf_sprintf(errbuf, "Invalid object type %#x "
428                                 "in LIST command", list->obj_type);
429                 return -1;
430         }
431         if (list->filter)
432                 return analyze_node(FILTER_CONTEXT, list->filter, errbuf);
433         return 0;
434 } /* analyze_list */
436 static int
437 analyze_lookup(sdb_ast_lookup_t *lookup, sdb_strbuf_t *errbuf)
439         if (! VALID_OBJ_TYPE(lookup->obj_type)) {
440                 sdb_strbuf_sprintf(errbuf, "Invalid object type %#x "
441                                 "in LOOKUP command", lookup->obj_type);
442                 return -1;
443         }
444         if (lookup->matcher)
445                 if (analyze_node(lookup->obj_type, lookup->matcher, errbuf))
446                         return -1;
447         if (lookup->filter)
448                 return analyze_node(FILTER_CONTEXT, lookup->filter, errbuf);
449         return 0;
450 } /* analyze_lookup */
452 static int
453 analyze_store(sdb_ast_store_t *st, sdb_strbuf_t *errbuf)
455         if ((st->obj_type != SDB_ATTRIBUTE)
456                         && (! VALID_OBJ_TYPE(st->obj_type))) {
457                 sdb_strbuf_sprintf(errbuf, "Invalid object type %#x "
458                                 "in STORE command", st->obj_type);
459                 return -1;
460         }
461         if (! st->name) {
462                 sdb_strbuf_sprintf(errbuf, "Missing object name in "
463                                 "STORE %s command", SDB_STORE_TYPE_TO_NAME(st->obj_type));
464                 return -1;
465         }
467         if ((st->obj_type == SDB_HOST) && st->hostname) {
468                 sdb_strbuf_sprintf(errbuf, "Unexpected parent hostname '%s' "
469                                 "in STORE HOST command", st->hostname);
470                 return -1;
471         }
472         else if ((st->obj_type != SDB_HOST) && (! st->hostname)) {
473                 sdb_strbuf_sprintf(errbuf, "Missing parent hostname for '%s' "
474                                 "in STORE %s command", st->name,
475                                 SDB_STORE_TYPE_TO_NAME(st->obj_type));
476                 return -1;
477         }
479         if (st->obj_type == SDB_ATTRIBUTE) {
480                 if ((st->parent_type <= 0) && st->parent) {
481                         sdb_strbuf_sprintf(errbuf, "Unexpected parent hostname '%s' "
482                                         "in STORE %s command", st->parent,
483                                         SDB_STORE_TYPE_TO_NAME(st->obj_type));
484                         return -1;
485                 }
486                 else if (st->parent_type > 0) {
487                         if (! VALID_OBJ_TYPE(st->parent_type)) {
488                                 sdb_strbuf_sprintf(errbuf, "Invalid parent type %#x "
489                                                 "in STORE %s command", st->parent_type,
490                                                 SDB_STORE_TYPE_TO_NAME(st->obj_type));
491                                 return -1;
492                         }
493                         if (! st->parent) {
494                                 sdb_strbuf_sprintf(errbuf, "Missing %s parent name "
495                                                 "in STORE %s command",
496                                                 SDB_STORE_TYPE_TO_NAME(st->parent_type),
497                                                 SDB_STORE_TYPE_TO_NAME(st->obj_type));
498                                 return -1;
499                         }
500                 }
501         }
502         else if ((st->parent_type > 0) || st->parent) {
503                 sdb_strbuf_sprintf(errbuf, "Unexpected %s parent name '%s' "
504                                 "in STORE %s command",
505                                 SDB_STORE_TYPE_TO_NAME(st->parent_type),
506                                 st->parent ? st->parent : "<unknown>",
507                                 SDB_STORE_TYPE_TO_NAME(st->obj_type));
508                 return -1;
509         }
511         if (st->obj_type == SDB_METRIC) {
512                 if ((! st->store_type) != (! st->store_id)) {
513                         sdb_strbuf_sprintf(errbuf, "Incomplete metric store %s %s "
514                                         "in STORE METRIC command",
515                                         st->store_type ? st->store_type : "<unknown>",
516                                         st->store_id ? st->store_id : "<unknown>");
517                         return -1;
518                 }
519         }
520         else if (st->store_type || st->store_id) {
521                 sdb_strbuf_sprintf(errbuf, "Unexpected metric store %s %s "
522                                 "in STORE %s command",
523                                 st->store_type ? st->store_type : "<unknown>",
524                                 st->store_id ? st->store_id : "<unknown>",
525                                 SDB_STORE_TYPE_TO_NAME(st->obj_type));
526                 return -1;
527         }
529         if ((! (st->obj_type == SDB_ATTRIBUTE))
530                         && (st->value.type != SDB_TYPE_NULL)) {
531                 char v_str[sdb_data_format(&st->value, NULL, 0, SDB_DOUBLE_QUOTED) + 1];
532                 sdb_data_format(&st->value, v_str, sizeof(v_str), SDB_DOUBLE_QUOTED);
533                 sdb_strbuf_sprintf(errbuf, "Unexpected value %s in STORE %s command",
534                                 v_str, SDB_STORE_TYPE_TO_NAME(st->obj_type));
535                 return -1;
536         }
537         return 0;
538 } /* analyze_store */
540 static int
541 analyze_timeseries(sdb_ast_timeseries_t *ts, sdb_strbuf_t *errbuf)
543         if (! ts->hostname) {
544                 sdb_strbuf_sprintf(errbuf, "Missing hostname in STORE command");
545                 return -1;
546         }
547         if (! ts->metric) {
548                 sdb_strbuf_sprintf(errbuf, "Missing metric name in STORE command");
549                 return -1;
550         }
551         if (ts->end <= ts->start) {
552                 char start_str[64], end_str[64];
553                 sdb_strftime(start_str, sizeof(start_str), "%F %T Tz", ts->start);
554                 sdb_strftime(end_str, sizeof(end_str), "%F %T Tz", ts->end);
555                 sdb_strbuf_sprintf(errbuf, "Start time (%s) greater than "
556                                 "end time (%s) in STORE command", start_str, end_str);
557                 return -1;
558         }
559         return 0;
560 } /* analyze_timeseries */
562 /*
563  * public API
564  */
566 int
567 sdb_parser_analyze(sdb_ast_node_t *node, sdb_strbuf_t *errbuf)
569         if (! node) {
570                 sdb_strbuf_sprintf(errbuf, "Empty AST node");
571                 return -1;
572         }
574         /* top-level nodes don't have a type */
575         node->data_type = -1;
577         if (node->type == SDB_AST_TYPE_FETCH)
578                 return analyze_fetch(SDB_AST_FETCH(node), errbuf);
579         else if (node->type == SDB_AST_TYPE_LIST)
580                 return analyze_list(SDB_AST_LIST(node), errbuf);
581         else if (node->type == SDB_AST_TYPE_LOOKUP)
582                 return analyze_lookup(SDB_AST_LOOKUP(node), errbuf);
583         else if (node->type == SDB_AST_TYPE_STORE)
584                 return analyze_store(SDB_AST_STORE(node), errbuf);
585         else if (node->type == SDB_AST_TYPE_TIMESERIES)
586                 return analyze_timeseries(SDB_AST_TIMESERIES(node), errbuf);
588         sdb_strbuf_sprintf(errbuf, "Invalid top-level AST node "
589                         "of type %#x", node->type);
590         return -1;
591 } /* sdb_parser_analyze */
593 int
594 sdb_parser_analyze_conditional(sdb_ast_node_t *node, sdb_strbuf_t *errbuf)
596         if (! node) {
597                 sdb_strbuf_sprintf(errbuf, "Empty conditional node");
598                 return -1;
599         }
600         if (! SDB_AST_IS_LOGICAL(node)) {
601                 sdb_strbuf_sprintf(errbuf, "Not a conditional node (got %s)",
602                                 SDB_AST_TYPE_TO_STRING(node));
603                 return -1;
604         }
605         return analyze_node(UNSPEC_CONTEXT, node, errbuf);
606 } /* sdb_parser_analyze_conditional */
608 int
609 sdb_parser_analyze_arith(sdb_ast_node_t *node, sdb_strbuf_t *errbuf)
611         if (! node) {
612                 sdb_strbuf_sprintf(errbuf, "Empty arithmetic node");
613                 return -1;
614         }
615         if (! SDB_AST_IS_ARITHMETIC(node)) {
616                 sdb_strbuf_sprintf(errbuf, "Not an arithmetic node (got %s)",
617                                 SDB_AST_TYPE_TO_STRING(node));
618                 return -1;
619         }
620         return analyze_node(UNSPEC_CONTEXT, node, errbuf);
621 } /* sdb_parser_analyze_arith */
623 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */