Code

frontend: Let sdb_fe_parse() return a list of parsed node objects.
authorSebastian Harl <sh@tokkee.org>
Thu, 2 Jan 2014 00:00:07 +0000 (01:00 +0100)
committerSebastian Harl <sh@tokkee.org>
Thu, 2 Jan 2014 00:00:07 +0000 (01:00 +0100)
Each parse-tree node object represents a single command.

src/frontend/grammar.y
src/frontend/parser.c
src/frontend/scanner.l
src/include/frontend/connection.h
src/include/frontend/parser.h
t/frontend/parser_test.c

index ef88272d6b237f9885ca30761891ca3e00ab9437..876f48a04a2d718864020473304e30d34ca0848a 100644 (file)
 
 %{
 
+#include "frontend/connection.h"
 #include "frontend/parser.h"
 #include "frontend/grammar.h"
+
 #include "utils/error.h"
+#include "utils/llist.h"
 
 #include <stdio.h>
 
+sdb_fe_yyextra_t *
+sdb_fe_yyget_extra(sdb_fe_yyscan_t scanner);
+
 void
 sdb_fe_yyerror(YYLTYPE *lval, sdb_fe_yyscan_t scanner, const char *msg);
 
+/* quick access to the current parse tree */
+#define pt sdb_fe_yyget_extra(scanner)->parsetree
+
 %}
 
 %pure-parser
@@ -46,35 +55,57 @@ sdb_fe_yyerror(YYLTYPE *lval, sdb_fe_yyscan_t scanner, const char *msg);
 %expect 0
 %name-prefix="sdb_fe_yy"
 
+%union {
+       sdb_llist_t     *list;
+       sdb_conn_node_t *node;
+}
+
 %start statements
 
 %token SCANNER_ERROR
 
 %token IDENTIFIER
-%token LIST
+%token <node> LIST
+
+%type <list> statements
+%type <node> statement
+       list_statement
 
 %%
 
 statements:
        statements ';' statement
                {
+                       if ($3) {
+                               sdb_llist_append(pt, SDB_OBJ($3));
+                               sdb_object_deref(SDB_OBJ($3));
+                       }
                }
        |
        statement
                {
+                       if ($1) {
+                               sdb_llist_append(pt, SDB_OBJ($1));
+                               sdb_object_deref(SDB_OBJ($1));
+                       }
                }
        ;
 
 statement:
        list_statement
-               {
-               }
        |
        /* empty */
+               {
+                       $$ = NULL;
+               }
        ;
 
 list_statement:
        LIST
+               {
+                       $$ = sdb_object_create_T(/* name = */ NULL, sdb_conn_node_t);
+                       ((sdb_conn_node_t *)$$)->cmd = CONNECTION_LIST;
+               }
        ;
 
 %%
index b970e73671eceefd8fd697d3029046b8fb8ac7f1..037550fbddd731bf4bd84c51c8d126d50466ff16 100644 (file)
 
 #include "sysdb.h"
 
+#include "frontend/connection-private.h"
 #include "frontend/parser.h"
 #include "frontend/grammar.h"
-#include "frontend/connection-private.h"
+
+#include "utils/llist.h"
 
 #include <string.h>
 
  * public API
  */
 
-int
+sdb_llist_t *
 sdb_fe_parse(const char *query)
 {
        sdb_fe_yyscan_t scanner;
+       sdb_fe_yyextra_t yyextra;
        int yyres;
 
        if (! query)
-               return -1;
+               return NULL;
+
+       memset(&yyextra, 0, sizeof(yyextra));
+       yyextra.parsetree = sdb_llist_create();
 
-       scanner = sdb_fe_scanner_init(query);
-       if (! scanner)
-               return -1;
+       scanner = sdb_fe_scanner_init(query, &yyextra);
+       if (! scanner) {
+               sdb_llist_destroy(yyextra.parsetree);
+               return NULL;
+       }
 
        yyres = sdb_fe_yyparse(scanner);
        sdb_fe_scanner_destroy(scanner);
 
-       if (yyres)
-               return -1;
-       return 0;
+       if (yyres) {
+               sdb_llist_destroy(yyextra.parsetree);
+               return NULL;
+       }
+       return yyextra.parsetree;
 } /* sdb_fe_parse */
 
 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */
index cd7ea8513109fd266437b10dfaf5a2428b55aec3..f300e46d73160d906f841dd3c421f125d3a93fcc 100644 (file)
@@ -27,6 +27,7 @@
 
 %{
 
+#include "frontend/connection.h"
 #include "frontend/parser.h"
 #include "frontend/grammar.h"
 #include "utils/error.h"
@@ -35,6 +36,8 @@
 
 #include <string.h>
 
+#define YY_EXTRA_TYPE sdb_fe_yyextra_t *
+
 void
 sdb_fe_yyerror(YYLTYPE *lval, sdb_fe_yyscan_t scanner, const char *msg);
 
@@ -94,7 +97,7 @@ identifier    ([A-Za-z_][A-Za-z_0-9$]*)
 %%
 
 sdb_fe_yyscan_t
-sdb_fe_scanner_init(const char *str)
+sdb_fe_scanner_init(const char *str, sdb_fe_yyextra_t *yyext)
 {
        yyscan_t scanner;
 
@@ -105,6 +108,8 @@ sdb_fe_scanner_init(const char *str)
                return NULL;
        }
 
+       sdb_fe_yyset_extra(yyext, scanner);
+
        /* the newly allocated buffer state (YY_BUFFER_STATE) is stored inside the
         * scanner and, thus, will be freed by yylex_destroy */
        yy_scan_string(str, scanner);
index bdebe7d1f9f0d3ba70222893ee73b810cec55b7a..c4478fb327116774114f07ac0e21515b811ecef0 100644 (file)
@@ -29,6 +29,7 @@
 #define SDB_FRONTEND_CONNECTION_H 1
 
 #include "frontend/proto.h"
+#include "utils/llist.h"
 #include "utils/strbuf.h"
 
 #include <inttypes.h>
@@ -39,6 +40,18 @@ extern "C" {
 
 typedef struct sdb_conn sdb_conn_t;
 
+/*
+ * sdb_conn_node_t represents an interface for the result of parsing a command
+ * string. The first field of an actual implementation of the interface is a
+ * sdb_conn_state_t in order to fascilitate casting to and from the interface
+ * type to the implementation type.
+ */
+typedef struct {
+       sdb_object_t super;
+       sdb_conn_state_t cmd;
+} sdb_conn_node_t;
+#define SDB_CONN_NODE(obj) ((sdb_conn_node_t *)(obj))
+
 /*
  * sdb_connection_accpet:
  * Accept a new connection on the specified file-descriptor 'fd' and return a
@@ -95,11 +108,28 @@ sdb_connection_ping(sdb_conn_t *conn);
 
 /*
  * sdb_fe_parse:
- * Parse the query text specified in 'query'.
+ * Parse the query text specified in 'query' and return a list of parse trees
+ * (for each command) to be executed by sdb_fe_exec. The list has to be freed
+ * by the caller.
+ *
+ * Returns:
+ *  - an sdb_llist_t object of sdb_conn_node_t on success
+ *  - NULL in case of an error
  */
-int
+sdb_llist_t *
 sdb_fe_parse(const char *query);
 
+/*
+ * sdb_fe_exec:
+ * Execute the command identified by 'node'.
+ *
+ * Returns:
+ *  - 0 on success
+ *  - a negative value else
+ */
+int
+sdb_fe_exec(sdb_conn_node_t *node);
+
 /*
  * session handling
  */
index 5491e9cac7778c24047ebb71896cdedf44b5781a..a80969ccb14fb746ddc0a3cad3e7a9f5c7dbb481 100644 (file)
 #ifndef SDB_FRONTEND_PARSER_H
 #define SDB_FRONTEND_PARSER_H 1
 
+#include "utils/llist.h"
+
 #ifdef __cplusplus
 extern "C" {
 #endif
 
+/* YY_EXTRA data */
+typedef struct {
+       /* list of sdb_conn_node_t objects */
+       sdb_llist_t *parsetree;
+} sdb_fe_yyextra_t;
+
 /* see yyscan_t */
 typedef void *sdb_fe_yyscan_t;
 
 sdb_fe_yyscan_t
-sdb_fe_scanner_init(const char *str);
+sdb_fe_scanner_init(const char *str, sdb_fe_yyextra_t *yyext);
 
 void
 sdb_fe_scanner_destroy(sdb_fe_yyscan_t scanner);
index bebe15efeabaeb1c413db3f2c136f4767f4ff727..444c8a70bfc79d27ed7e1f5dd47610871018c96d 100644 (file)
@@ -47,8 +47,8 @@ START_TEST(test_parse)
                { ";;",                  0 },
 
                /* valid commands */
-               { "LIST",                0 },
-               { "LIST;",               0 },
+               { "LIST",                1 },
+               { "LIST;",               1 },
 
                /* comments */
                { "/* some comment */",  0 },
@@ -60,19 +60,32 @@ START_TEST(test_parse)
        };
 
        size_t i;
-       int check;
+       sdb_llist_t *check;
 
        for (i = 0; i < SDB_STATIC_ARRAY_LEN(golden_data); ++i) {
                _Bool ok;
 
                check = sdb_fe_parse(golden_data[i].query);
                if (golden_data[i].expected < 0)
-                       ok = check < 0;
+                       ok = check == 0;
                else
-                       ok = check == golden_data[i].expected;
-
-               fail_unless(ok, "sdb_fe_parse(%s) = %d; expected: %d",
-                               golden_data[i].query, check, golden_data[i].expected);
+                       ok = sdb_llist_len(check) == (size_t)golden_data[i].expected;
+
+               fail_unless(ok, "sdb_fe_parse(%s) = %p (len: %zu); expected: %d",
+                               golden_data[i].query, check, sdb_llist_len(check),
+                               golden_data[i].expected);
+
+               if (! check)
+                       continue;
+
+               if ((! strcmp(golden_data[i].query, "LIST"))
+                               || (! strcmp(golden_data[i].query, "LIST;"))) {
+                       sdb_object_t *obj = sdb_llist_get(check, 0);
+                       fail_unless(SDB_CONN_NODE(obj)->cmd == CONNECTION_LIST,
+                                       "sdb_fe_parse(LIST)->cmd = %i; expected: %d "
+                                       "(CONNECTION_LIST)", SDB_CONN_NODE(obj)->cmd,
+                                       CONNECTION_LIST);
+               }
        }
 }
 END_TEST