Code

parser/analyzer: Fixed iterator type 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         /* TODO: support other setups as well */
221         assert((iter->expr->type == SDB_AST_TYPE_OPERATOR)
222                         && (! SDB_AST_OP(iter->expr)->left));
223         /* determine the data-type for better error messages */
224         analyze_node(iter_context, SDB_AST_OP(iter->expr)->right, NULL);
226         if (iter->iter->type == SDB_AST_TYPE_TYPED) {
227                 int iter_type = SDB_AST_TYPED(iter->iter)->type;
229                 c.value.type = iter->iter->data_type;
231                 if (iter_type == SDB_ATTRIBUTE) {
232                         /* attributes are always iterable */
233                 }
234                 else if ((context != SDB_HOST) && (context != SDB_SERVICE)
235                                 && (context != SDB_METRIC) && (context != UNSPEC_CONTEXT)) {
236                         iter_error(errbuf, iter, "%s not iterable in %s context",
237                                         SDB_STORE_TYPE_TO_NAME(iter_type),
238                                         SDB_STORE_TYPE_TO_NAME(context));
239                         return -1;
240                 }
242                 if ((context == iter_type)
243                                 || ((iter_type != SDB_SERVICE)
244                                         && (iter_type != SDB_METRIC)
245                                         && (iter_type != SDB_ATTRIBUTE))
246                                 || ((context == SDB_SERVICE)
247                                         && (iter_type == SDB_METRIC))
248                                 || ((context == SDB_METRIC)
249                                         && (iter_type == SDB_SERVICE))) {
250                         iter_error(errbuf, iter, "%s not iterable in %s context",
251                                         SDB_STORE_TYPE_TO_NAME(iter_type),
252                                         SDB_STORE_TYPE_TO_NAME(context));
253                         return -1;
254                 }
255         }
256         else if (iter->iter->type == SDB_AST_TYPE_VALUE) {
257                 int iter_type = SDB_AST_VALUE(iter->iter)->type;
259                 c.value.type = iter->iter->data_type & 0xff;
261                 if (iter_type != SDB_FIELD_BACKEND) {
262                         iter_error(errbuf, iter, "%s not iterable in %s context",
263                                         (iter_type == SDB_ATTRIBUTE)
264                                                 ? "attribute"
265                                                 : SDB_FIELD_TO_NAME(iter_type),
266                                         SDB_STORE_TYPE_TO_NAME(context));
267                         return -1;
268                 }
269         }
270         else if (iter->iter->type == SDB_AST_TYPE_CONST) {
271                 c.value.type = iter->iter->data_type & 0xff;
273                 if (! (SDB_AST_CONST(iter->iter)->value.type & SDB_TYPE_ARRAY)) {
274                         iter_error(errbuf, iter, "%s not iterable",
275                                         SDB_TYPE_TO_STRING(SDB_AST_CONST(iter->iter)->value.type));
276                         return -1;
277                 }
278         }
279         else {
280                 /* TODO: if we know the data-type of iter->iter and it's an array,
281                  * we should support an iterator for it as well */
282                 iter_error(errbuf, iter, "%s expression not iterable",
283                                 SDB_AST_TYPE_TO_STRING(iter->iter));
284                 return -1;
285         }
287         SDB_AST_OP(iter->expr)->left = SDB_AST_NODE(&c);
288         status = analyze_node(context, iter->expr, errbuf);
289         SDB_AST_OP(iter->expr)->left = NULL;
290         if (status)
291                 return -1;
292         return 0;
293 } /* analyze_iter */
295 static int
296 analyze_const(int __attribute__((unused)) context, sdb_ast_const_t *c,
297                 sdb_strbuf_t __attribute__((unused)) *errbuf)
299         SDB_AST_NODE(c)->data_type = c->value.type;
300         return 0;
301 } /* analyze_const */
303 static int
304 analyze_value(int context, sdb_ast_value_t *v, sdb_strbuf_t *errbuf)
306         if (v->type != SDB_ATTRIBUTE)
307                 SDB_AST_NODE(v)->data_type = SDB_FIELD_TYPE(v->type);
309         if ((v->type != SDB_ATTRIBUTE) && v->name) {
310                 sdb_strbuf_sprintf(errbuf, "Invalid expression %s[%s]",
311                                 SDB_FIELD_TO_NAME(v->type), v->name);
312                 return -1;
313         }
314         else if ((v->type == SDB_ATTRIBUTE) && (! v->name)) {
315                 sdb_strbuf_sprintf(errbuf, "Invalid expression attribute[] "
316                                 "(missing name)");
317                 return -1;
318         }
320         if ((context != SDB_ATTRIBUTE) && (v->type == SDB_FIELD_VALUE)) {
321                 sdb_strbuf_sprintf(errbuf, "Invalid expression %s.value",
322                                 SDB_FIELD_TO_NAME(v->type));
323                 return -1;
324         }
325         return 0;
326 } /* analyze_value */
328 static int
329 analyze_typed(int context, sdb_ast_typed_t *t, sdb_strbuf_t *errbuf)
331         if ((t->expr->type != SDB_AST_TYPE_VALUE)
332                         && (t->expr->type != SDB_AST_TYPE_TYPED)) {
333                 sdb_strbuf_sprintf(errbuf, "Invalid expression %s.%s",
334                                 SDB_STORE_TYPE_TO_NAME(t->type),
335                                 SDB_AST_TYPE_TO_STRING(t->expr));
336                 return -1;
337         }
338         if (analyze_node(t->type, t->expr, errbuf))
339                 return -1;
340         SDB_AST_NODE(t)->data_type = t->expr->data_type;
342         if ((t->type != SDB_ATTRIBUTE) && (! VALID_OBJ_TYPE(t->type))) {
343                 sdb_strbuf_sprintf(errbuf, "Invalid expression %#x.%s",
344                                 t->type, SDB_AST_TYPE_TO_STRING(t->expr));
345                 return -1;
346         }
348         /* self-references are allowed and services and metrics may reference
349          * their parent host; everything may reference attributes */
350         if ((context != t->type) && (context != UNSPEC_CONTEXT)
351                         && (((context != SDB_SERVICE) && (context != SDB_METRIC))
352                                 || (t->type != SDB_HOST))
353                         && (t->type != SDB_ATTRIBUTE)) {
354                 sdb_strbuf_sprintf(errbuf, "Invalid expression %s.%s in %s context",
355                                 SDB_STORE_TYPE_TO_NAME(t->type),
356                                 SDB_AST_TYPE_TO_STRING(t->expr),
357                                 context == -1 ? "generic" : SDB_STORE_TYPE_TO_NAME(context));
358                 return -1;
359         }
360         return 0;
361 } /* analyze_typed */
363 static int
364 analyze_node(int context, sdb_ast_node_t *node, sdb_strbuf_t *errbuf)
366         if (! node) {
367                 sdb_strbuf_sprintf(errbuf, "Empty AST node");
368                 return -1;
369         }
371         /* unknown by default */
372         node->data_type = -1;
374         if ((node->type == SDB_AST_TYPE_OPERATOR)
375                         && (SDB_AST_IS_LOGICAL(node)))
376                 return analyze_logical(context, SDB_AST_OP(node), errbuf);
377         else if ((node->type == SDB_AST_TYPE_OPERATOR)
378                         && (SDB_AST_IS_ARITHMETIC(node)))
379                 return analyze_arith(context, SDB_AST_OP(node), errbuf);
380         else if (node->type == SDB_AST_TYPE_ITERATOR)
381                 return analyze_iter(context, SDB_AST_ITER(node), errbuf);
382         else if (node->type == SDB_AST_TYPE_CONST)
383                 return analyze_const(context, SDB_AST_CONST(node), errbuf);
384         else if (node->type == SDB_AST_TYPE_VALUE)
385                 return analyze_value(context, SDB_AST_VALUE(node), errbuf);
386         else if (node->type == SDB_AST_TYPE_TYPED)
387                 return analyze_typed(context, SDB_AST_TYPED(node), errbuf);
389         sdb_strbuf_sprintf(errbuf, "Invalid expression node "
390                         "of type %#x", node->type);
391         return -1;
392 } /* analyze_node */
394 /*
395  * top level / command nodes
396  */
398 static int
399 analyze_fetch(sdb_ast_fetch_t *fetch, sdb_strbuf_t *errbuf)
401         if (! VALID_OBJ_TYPE(fetch->obj_type)) {
402                 sdb_strbuf_sprintf(errbuf, "Invalid object type %#x "
403                                 "in FETCH command", fetch->obj_type);
404                 return -1;
405         }
406         if (! fetch->name) {
407                 sdb_strbuf_sprintf(errbuf, "Missing object name in "
408                                 "FETCH %s command", SDB_STORE_TYPE_TO_NAME(fetch->obj_type));
409                 return -1;
410         }
412         if ((fetch->obj_type == SDB_HOST) && fetch->hostname) {
413                 sdb_strbuf_sprintf(errbuf, "Unexpected parent hostname '%s' "
414                                 "in FETCH HOST command", fetch->hostname);
415                 return -1;
416         }
417         else if ((fetch->obj_type != SDB_HOST) && (! fetch->hostname)) {
418                 sdb_strbuf_sprintf(errbuf, "Missing parent hostname for '%s' "
419                                 "in FETCH %s command", fetch->name,
420                                 SDB_STORE_TYPE_TO_NAME(fetch->obj_type));
421                 return -1;
422         }
424         if (fetch->filter)
425                 return analyze_node(FILTER_CONTEXT, fetch->filter, errbuf);
426         return 0;
427 } /* analyze_fetch */
429 static int
430 analyze_list(sdb_ast_list_t *list, sdb_strbuf_t *errbuf)
432         if (! VALID_OBJ_TYPE(list->obj_type)) {
433                 sdb_strbuf_sprintf(errbuf, "Invalid object type %#x "
434                                 "in LIST command", list->obj_type);
435                 return -1;
436         }
437         if (list->filter)
438                 return analyze_node(FILTER_CONTEXT, list->filter, errbuf);
439         return 0;
440 } /* analyze_list */
442 static int
443 analyze_lookup(sdb_ast_lookup_t *lookup, sdb_strbuf_t *errbuf)
445         if (! VALID_OBJ_TYPE(lookup->obj_type)) {
446                 sdb_strbuf_sprintf(errbuf, "Invalid object type %#x "
447                                 "in LOOKUP command", lookup->obj_type);
448                 return -1;
449         }
450         if (lookup->matcher)
451                 if (analyze_node(lookup->obj_type, lookup->matcher, errbuf))
452                         return -1;
453         if (lookup->filter)
454                 return analyze_node(FILTER_CONTEXT, lookup->filter, errbuf);
455         return 0;
456 } /* analyze_lookup */
458 static int
459 analyze_store(sdb_ast_store_t *st, sdb_strbuf_t *errbuf)
461         if ((st->obj_type != SDB_ATTRIBUTE)
462                         && (! VALID_OBJ_TYPE(st->obj_type))) {
463                 sdb_strbuf_sprintf(errbuf, "Invalid object type %#x "
464                                 "in STORE command", st->obj_type);
465                 return -1;
466         }
467         if (! st->name) {
468                 sdb_strbuf_sprintf(errbuf, "Missing object name in "
469                                 "STORE %s command", SDB_STORE_TYPE_TO_NAME(st->obj_type));
470                 return -1;
471         }
473         if ((st->obj_type == SDB_HOST) && st->hostname) {
474                 sdb_strbuf_sprintf(errbuf, "Unexpected parent hostname '%s' "
475                                 "in STORE HOST command", st->hostname);
476                 return -1;
477         }
478         else if ((st->obj_type != SDB_HOST) && (! st->hostname)) {
479                 sdb_strbuf_sprintf(errbuf, "Missing parent hostname for '%s' "
480                                 "in STORE %s command", st->name,
481                                 SDB_STORE_TYPE_TO_NAME(st->obj_type));
482                 return -1;
483         }
485         if (st->obj_type == SDB_ATTRIBUTE) {
486                 if ((st->parent_type <= 0) && st->parent) {
487                         sdb_strbuf_sprintf(errbuf, "Unexpected parent hostname '%s' "
488                                         "in STORE %s command", st->parent,
489                                         SDB_STORE_TYPE_TO_NAME(st->obj_type));
490                         return -1;
491                 }
492                 else if (st->parent_type > 0) {
493                         if (! VALID_OBJ_TYPE(st->parent_type)) {
494                                 sdb_strbuf_sprintf(errbuf, "Invalid parent type %#x "
495                                                 "in STORE %s command", st->parent_type,
496                                                 SDB_STORE_TYPE_TO_NAME(st->obj_type));
497                                 return -1;
498                         }
499                         if (! st->parent) {
500                                 sdb_strbuf_sprintf(errbuf, "Missing %s parent name "
501                                                 "in STORE %s command",
502                                                 SDB_STORE_TYPE_TO_NAME(st->parent_type),
503                                                 SDB_STORE_TYPE_TO_NAME(st->obj_type));
504                                 return -1;
505                         }
506                 }
507         }
508         else if ((st->parent_type > 0) || st->parent) {
509                 sdb_strbuf_sprintf(errbuf, "Unexpected %s parent name '%s' "
510                                 "in STORE %s command",
511                                 SDB_STORE_TYPE_TO_NAME(st->parent_type),
512                                 st->parent ? st->parent : "<unknown>",
513                                 SDB_STORE_TYPE_TO_NAME(st->obj_type));
514                 return -1;
515         }
517         if (st->obj_type == SDB_METRIC) {
518                 if ((! st->store_type) != (! st->store_id)) {
519                         sdb_strbuf_sprintf(errbuf, "Incomplete metric store %s %s "
520                                         "in STORE METRIC command",
521                                         st->store_type ? st->store_type : "<unknown>",
522                                         st->store_id ? st->store_id : "<unknown>");
523                         return -1;
524                 }
525         }
526         else if (st->store_type || st->store_id) {
527                 sdb_strbuf_sprintf(errbuf, "Unexpected metric store %s %s "
528                                 "in STORE %s command",
529                                 st->store_type ? st->store_type : "<unknown>",
530                                 st->store_id ? st->store_id : "<unknown>",
531                                 SDB_STORE_TYPE_TO_NAME(st->obj_type));
532                 return -1;
533         }
535         if ((! (st->obj_type == SDB_ATTRIBUTE))
536                         && (st->value.type != SDB_TYPE_NULL)) {
537                 char v_str[sdb_data_format(&st->value, NULL, 0, SDB_DOUBLE_QUOTED) + 1];
538                 sdb_data_format(&st->value, v_str, sizeof(v_str), SDB_DOUBLE_QUOTED);
539                 sdb_strbuf_sprintf(errbuf, "Unexpected value %s in STORE %s command",
540                                 v_str, SDB_STORE_TYPE_TO_NAME(st->obj_type));
541                 return -1;
542         }
543         return 0;
544 } /* analyze_store */
546 static int
547 analyze_timeseries(sdb_ast_timeseries_t *ts, sdb_strbuf_t *errbuf)
549         if (! ts->hostname) {
550                 sdb_strbuf_sprintf(errbuf, "Missing hostname in STORE command");
551                 return -1;
552         }
553         if (! ts->metric) {
554                 sdb_strbuf_sprintf(errbuf, "Missing metric name in STORE command");
555                 return -1;
556         }
557         if (ts->end <= ts->start) {
558                 char start_str[64], end_str[64];
559                 sdb_strftime(start_str, sizeof(start_str), "%F %T Tz", ts->start);
560                 sdb_strftime(end_str, sizeof(end_str), "%F %T Tz", ts->end);
561                 sdb_strbuf_sprintf(errbuf, "Start time (%s) greater than "
562                                 "end time (%s) in STORE command", start_str, end_str);
563                 return -1;
564         }
565         return 0;
566 } /* analyze_timeseries */
568 /*
569  * public API
570  */
572 int
573 sdb_parser_analyze(sdb_ast_node_t *node, sdb_strbuf_t *errbuf)
575         if (! node) {
576                 sdb_strbuf_sprintf(errbuf, "Empty AST node");
577                 return -1;
578         }
580         /* top-level nodes don't have a type */
581         node->data_type = -1;
583         if (node->type == SDB_AST_TYPE_FETCH)
584                 return analyze_fetch(SDB_AST_FETCH(node), errbuf);
585         else if (node->type == SDB_AST_TYPE_LIST)
586                 return analyze_list(SDB_AST_LIST(node), errbuf);
587         else if (node->type == SDB_AST_TYPE_LOOKUP)
588                 return analyze_lookup(SDB_AST_LOOKUP(node), errbuf);
589         else if (node->type == SDB_AST_TYPE_STORE)
590                 return analyze_store(SDB_AST_STORE(node), errbuf);
591         else if (node->type == SDB_AST_TYPE_TIMESERIES)
592                 return analyze_timeseries(SDB_AST_TIMESERIES(node), errbuf);
594         sdb_strbuf_sprintf(errbuf, "Invalid top-level AST node "
595                         "of type %#x", node->type);
596         return -1;
597 } /* sdb_parser_analyze */
599 int
600 sdb_parser_analyze_conditional(sdb_ast_node_t *node, sdb_strbuf_t *errbuf)
602         if (! node) {
603                 sdb_strbuf_sprintf(errbuf, "Empty conditional node");
604                 return -1;
605         }
606         if (! SDB_AST_IS_LOGICAL(node)) {
607                 sdb_strbuf_sprintf(errbuf, "Not a conditional node (got %s)",
608                                 SDB_AST_TYPE_TO_STRING(node));
609                 return -1;
610         }
611         return analyze_node(UNSPEC_CONTEXT, node, errbuf);
612 } /* sdb_parser_analyze_conditional */
614 int
615 sdb_parser_analyze_arith(sdb_ast_node_t *node, sdb_strbuf_t *errbuf)
617         if (! node) {
618                 sdb_strbuf_sprintf(errbuf, "Empty arithmetic node");
619                 return -1;
620         }
621         if (! SDB_AST_IS_ARITHMETIC(node)) {
622                 sdb_strbuf_sprintf(errbuf, "Not an arithmetic node (got %s)",
623                                 SDB_AST_TYPE_TO_STRING(node));
624                 return -1;
625         }
626         return analyze_node(UNSPEC_CONTEXT, node, errbuf);
627 } /* sdb_parser_analyze_arith */
629 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */