From: Sebastian Harl Date: Tue, 5 May 2015 21:33:04 +0000 (+0200) Subject: parser/ast: Add some helper defines. X-Git-Tag: sysdb-0.8.0~109 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=8c60138c3491a844ed2302709763331d1c8375fd;p=sysdb.git parser/ast: Add some helper defines. --- diff --git a/src/include/parser/ast.h b/src/include/parser/ast.h index 3bdc5b6..665caf5 100644 --- a/src/include/parser/ast.h +++ b/src/include/parser/ast.h @@ -107,6 +107,10 @@ typedef enum { SDB_AST_CONCAT = 2005, /* iterators */ +#define SDB_AST_IS_ITERATOR(n) \ + (((n)->type == SDB_AST_TYPE_ITERATOR) \ + && ((SDB_AST_ALL <= SDB_AST_ITER(n)->kind) \ + && (SDB_AST_ITER(n)->kind <= SDB_AST_ANY))) SDB_AST_ALL = 3000, SDB_AST_ANY = 3001, } sdb_ast_operator_t; @@ -135,6 +139,20 @@ typedef enum { : ((op) == SDB_AST_ANY) ? "ANY" \ : "UNKNOWN") +#define SDB_AST_TYPE_TO_STRING(n) \ + (((n)->type == SDB_AST_TYPE_FETCH) ? "FETCH" \ + : ((n)->type == SDB_AST_TYPE_LIST) ? "LIST" \ + : ((n)->type == SDB_AST_TYPE_LOOKUP) ? "LOOKUP" \ + : ((n)->type == SDB_AST_TYPE_STORE) ? "STORE" \ + : ((n)->type == SDB_AST_TYPE_TIMESERIES) ? "TIMESERIES" \ + : ((n)->type == SDB_AST_TYPE_OPERATOR) \ + ? SDB_AST_OP_TO_STRING(SDB_AST_OP(n)->kind) \ + : ((n)->type == SDB_AST_TYPE_ITERATOR) ? "ITERATOR" \ + : ((n)->type == SDB_AST_TYPE_CONST) ? "CONSTANT" \ + : ((n)->type == SDB_AST_TYPE_VALUE) ? "VALUE" \ + : ((n)->type == SDB_AST_TYPE_TYPED) ? "TYPED VALUE" \ + : "UNKNOWN") + /* * sdb_ast_node_t is the interface for AST nodes. The first field of any * actual implementation of the interface is of type sdb_ast_node_t to @@ -162,6 +180,8 @@ typedef struct { sdb_ast_node_t *right; } sdb_ast_op_t; #define SDB_AST_OP(obj) ((sdb_ast_op_t *)(obj)) +#define SDB_AST_OP_INIT \ + { { SDB_OBJECT_INIT, SDB_AST_TYPE_OPERATOR }, -1, NULL, NULL } /* * sdb_ast_iter_t represents an iterator. @@ -176,6 +196,8 @@ typedef struct { sdb_ast_node_t *expr; } sdb_ast_iter_t; #define SDB_AST_ITER(obj) ((sdb_ast_iter_t *)(obj)) +#define SDB_AST_ITER_INIT \ + { { SDB_OBJECT_INIT, SDB_AST_TYPE_ITERATOR }, -1, -1, NULL, NULL } /* * sdb_ast_typed_t represents a typed value. @@ -186,6 +208,8 @@ typedef struct { sdb_ast_node_t *expr; } sdb_ast_typed_t; #define SDB_AST_TYPED(obj) ((sdb_ast_typed_t *)(obj)) +#define SDB_AST_TYPED_INIT \ + { { SDB_OBJECT_INIT, SDB_AST_TYPE_TYPED }, -1, NULL } /* * sdb_ast_const_t represents a constant value. @@ -195,6 +219,8 @@ typedef struct { sdb_data_t value; } sdb_ast_const_t; #define SDB_AST_CONST(obj) ((sdb_ast_const_t *)(obj)) +#define SDB_AST_CONST_INIT \ + { { SDB_OBJECT_INIT, SDB_AST_TYPE_CONST }, SDB_DATA_INIT } /* * sdb_ast_value_t represents an object-specific value: sibling nodes, @@ -206,6 +232,8 @@ typedef struct { char *name; /* object name; optional */ } sdb_ast_value_t; #define SDB_AST_VALUE(obj) ((sdb_ast_value_t *)(obj)) +#define SDB_AST_VALUE_INIT \ + { { SDB_OBJECT_INIT, SDB_AST_TYPE_VALUE }, -1, NULL } /* * sdb_ast_fetch_t represents a FETCH command. @@ -218,6 +246,8 @@ typedef struct { sdb_ast_node_t *filter; /* optional */ } sdb_ast_fetch_t; #define SDB_AST_FETCH(obj) ((sdb_ast_fetch_t *)(obj)) +#define SDB_AST_FETCH_INIT \ + { { SDB_OBJECT_INIT, SDB_AST_TYPE_FETCH }, -1, NULL, NULL, NULL } /* * sdb_ast_list_t represents a LIST command. @@ -228,6 +258,8 @@ typedef struct { sdb_ast_node_t *filter; /* optional */ } sdb_ast_list_t; #define SDB_AST_LIST(obj) ((sdb_ast_list_t *)(obj)) +#define SDB_AST_LIST_INIT \ + { { SDB_OBJECT_INIT, SDB_AST_TYPE_LIST }, -1, NULL } /* * sdb_ast_lookup_t represents a LOOKUP command. @@ -239,6 +271,8 @@ typedef struct { sdb_ast_node_t *filter; /* optional */ } sdb_ast_lookup_t; #define SDB_AST_LOOKUP(obj) ((sdb_ast_lookup_t *)(obj)) +#define SDB_AST_LOOKUP_INIT \ + { { SDB_OBJECT_INIT, SDB_AST_TYPE_LOOKUP }, -1, NULL, NULL } /* * sdb_ast_store_t represents a STORE command. @@ -260,6 +294,9 @@ typedef struct { sdb_data_t value; } sdb_ast_store_t; #define SDB_AST_STORE(obj) ((sdb_ast_store_t *)(obj)) +#define SDB_AST_STORE_INIT \ + { { SDB_OBJECT_INIT, SDB_AST_TYPE_STORE }, \ + -1, NULL, -1, NULL, NULL, 0, NULL, NULL, SDB_DATA_INIT } /* * sdb_ast_timeseries_t represents a TIMESERIES command. @@ -272,6 +309,8 @@ typedef struct { sdb_time_t end; } sdb_ast_timeseries_t; #define SDB_AST_TIMESERIES(obj) ((sdb_ast_timeseries_t *)(obj)) +#define SDB_AST_TIMESERIES_INIT \ + { { SDB_OBJECT_INIT, SDB_AST_TYPE_TIMESERIES }, NULL, NULL, 0, 0 } /* * AST constructors: diff --git a/t/Makefile.am b/t/Makefile.am index 3f1fd4b..1b2960d 100644 --- a/t/Makefile.am +++ b/t/Makefile.am @@ -38,6 +38,8 @@ UNIT_TESTS = \ unit/frontend/parser_test \ unit/frontend/query_test \ unit/frontend/sock_test \ + unit/parser/ast_test \ + unit/parser/ast_test \ unit/parser/parser_test \ unit/utils/avltree_test \ unit/utils/channel_test \ @@ -102,6 +104,10 @@ unit_frontend_sock_test_SOURCES = $(UNIT_TEST_SOURCES) unit/frontend/sock_test.c unit_frontend_sock_test_CFLAGS = $(UNIT_TEST_CFLAGS) unit_frontend_sock_test_LDADD = $(UNIT_TEST_LDADD) +unit_parser_ast_test_SOURCES = $(UNIT_TEST_SOURCES) unit/parser/ast_test.c +unit_parser_ast_test_CFLAGS = $(UNIT_TEST_CFLAGS) +unit_parser_ast_test_LDADD = $(UNIT_TEST_LDADD) + unit_parser_parser_test_SOURCES = $(UNIT_TEST_SOURCES) unit/parser/parser_test.c unit_parser_parser_test_CFLAGS = $(UNIT_TEST_CFLAGS) unit_parser_parser_test_LDADD = $(UNIT_TEST_LDADD) diff --git a/t/unit/parser/ast_test.c b/t/unit/parser/ast_test.c new file mode 100644 index 0000000..ebb74cb --- /dev/null +++ b/t/unit/parser/ast_test.c @@ -0,0 +1,80 @@ +/* + * SysDB - t/unit/parser/ast_test.c + * Copyright (C) 2015 Sebastian 'tokkee' Harl + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#if HAVE_CONFIG_H +# include "config.h" +#endif + +#include "parser/ast.h" +#include "parser/parser.h" +#include "testutils.h" + +#include + +/* + * tests + */ + +START_TEST(test_init) +{ + /* simple test to check that the initializers match; + * not all of them are used at all times */ + sdb_ast_op_t op = SDB_AST_OP_INIT; + sdb_ast_iter_t iter = SDB_AST_ITER_INIT; + sdb_ast_typed_t typed = SDB_AST_TYPED_INIT; + sdb_ast_const_t constant = SDB_AST_CONST_INIT; + sdb_ast_value_t value = SDB_AST_VALUE_INIT; + sdb_ast_fetch_t fetch = SDB_AST_FETCH_INIT; + sdb_ast_list_t list = SDB_AST_LIST_INIT; + sdb_ast_lookup_t lookup = SDB_AST_LOOKUP_INIT; + sdb_ast_store_t store = SDB_AST_STORE_INIT; + sdb_ast_timeseries_t ts = SDB_AST_TIMESERIES_INIT; + + /* do some (mostly) dummy operation */ + sdb_parser_analyze(SDB_AST_NODE(&op), NULL); + sdb_parser_analyze(SDB_AST_NODE(&iter), NULL); + sdb_parser_analyze(SDB_AST_NODE(&typed), NULL); + sdb_parser_analyze(SDB_AST_NODE(&constant), NULL); + sdb_parser_analyze(SDB_AST_NODE(&value), NULL); + sdb_parser_analyze(SDB_AST_NODE(&fetch), NULL); + sdb_parser_analyze(SDB_AST_NODE(&list), NULL); + sdb_parser_analyze(SDB_AST_NODE(&lookup), NULL); + sdb_parser_analyze(SDB_AST_NODE(&store), NULL); + sdb_parser_analyze(SDB_AST_NODE(&ts), NULL); +} +END_TEST + +TEST_MAIN("parser::ast") +{ + TCase *tc = tcase_create("core"); + tcase_add_test(tc, test_init); + ADD_TCASE(tc); +} +TEST_MAIN_END + +/* vim: set tw=78 sw=4 ts=4 noexpandtab : */ +