summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: ea29adb)
raw | patch | inline | side by side (parent: ea29adb)
author | Sebastian Harl <sh@tokkee.org> | |
Thu, 2 Jan 2014 00:00:07 +0000 (01:00 +0100) | ||
committer | Sebastian Harl <sh@tokkee.org> | |
Thu, 2 Jan 2014 00:00:07 +0000 (01:00 +0100) |
Each parse-tree node object represents a single command.
diff --git a/src/frontend/grammar.y b/src/frontend/grammar.y
index ef88272d6b237f9885ca30761891ca3e00ab9437..876f48a04a2d718864020473304e30d34ca0848a 100644 (file)
--- a/src/frontend/grammar.y
+++ b/src/frontend/grammar.y
%{
+#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
%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;
+ }
;
%%
diff --git a/src/frontend/parser.c b/src/frontend/parser.c
index b970e73671eceefd8fd697d3029046b8fb8ac7f1..037550fbddd731bf4bd84c51c8d126d50466ff16 100644 (file)
--- a/src/frontend/parser.c
+++ b/src/frontend/parser.c
#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 : */
diff --git a/src/frontend/scanner.l b/src/frontend/scanner.l
index cd7ea8513109fd266437b10dfaf5a2428b55aec3..f300e46d73160d906f841dd3c421f125d3a93fcc 100644 (file)
--- a/src/frontend/scanner.l
+++ b/src/frontend/scanner.l
%{
+#include "frontend/connection.h"
#include "frontend/parser.h"
#include "frontend/grammar.h"
#include "utils/error.h"
#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);
%%
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;
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)
#define SDB_FRONTEND_CONNECTION_H 1
#include "frontend/proto.h"
+#include "utils/llist.h"
#include "utils/strbuf.h"
#include <inttypes.h>
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
/*
* 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)
--- a/t/frontend/parser_test.c
+++ b/t/frontend/parser_test.c
{ ";;", 0 },
/* valid commands */
- { "LIST", 0 },
- { "LIST;", 0 },
+ { "LIST", 1 },
+ { "LIST;", 1 },
/* comments */
{ "/* some comment */", 0 },
};
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