summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 136847e)
raw | patch | inline | side by side (parent: 136847e)
author | Sebastian Harl <sh@tokkee.org> | |
Tue, 31 Dec 2013 00:05:22 +0000 (01:05 +0100) | ||
committer | Sebastian Harl <sh@tokkee.org> | |
Tue, 31 Dec 2013 00:05:22 +0000 (01:05 +0100) |
For now, this implements a mostly empty parser and helper functions to parse
strings (rather than open files). The parser accepts the "LIST" command and
ignores SQL and C style comments.
strings (rather than open files). The parser accepts the "LIST" command and
ignores SQL and C style comments.
13 files changed:
.gitignore | patch | blob | history | |
src/Makefile.am | patch | blob | history | |
src/frontend/grammar.y | [new file with mode: 0644] | patch | blob |
src/frontend/parser.c | [new file with mode: 0644] | patch | blob |
src/frontend/scanner.l | [new file with mode: 0644] | patch | blob |
src/include/frontend/connection.h | patch | blob | history | |
src/include/frontend/parser.h | [new file with mode: 0644] | patch | blob |
src/include/frontend/proto.h | patch | blob | history | |
src/tools/sysdb/scanner.l | patch | blob | history | |
t/Makefile.am | patch | blob | history | |
t/frontend/parser_test.c | [new file with mode: 0644] | patch | blob |
t/libsysdb_test.c | patch | blob | history | |
t/libsysdb_test.h | patch | blob | history |
diff --git a/.gitignore b/.gitignore
index 9cbc7c55ec993b156d627818119e11c57c30579c..f097f95edab772a39db674201301578f8e708b9a 100644 (file)
--- a/.gitignore
+++ b/.gitignore
ltmain.sh
# build output
+src/frontend/grammar.c
+src/frontend/grammar.h
+src/frontend/scanner.c
src/liboconfig/parser.c
src/liboconfig/parser.h
src/liboconfig/scanner.c
diff --git a/src/Makefile.am b/src/Makefile.am
index 53f4efb4e26222063c082e3325fc3ab0a7864090..c0b7576165150b5c5f4ce1f088538942e34ffe04 100644 (file)
--- a/src/Makefile.am
+++ b/src/Makefile.am
AM_CPPFLAGS += -DLOCALSTATEDIR='"${localstatedir}"'
AM_CPPFLAGS += -DPKGLIBDIR='"${pkglibdir}"'
+AM_YFLAGS = -d
+
BUILT_SOURCES = include/client/sysdb.h include/sysdb.h
EXTRA_DIST = include/client/sysdb.h.in include/sysdb.h.in
libsysdbclient_la_LDFLAGS = $(AM_LDFLAGS) -version-info 0:0:0 -pthread
libsysdbclient_la_LIBADD = $(LIBLTDL)
+# don't use strict CFLAGS for flex code
+noinst_LTLIBRARIES += libsysdb_fe_parser.la
+libsysdb_fe_parser_la_SOURCES = \
+ frontend/grammar.y frontend/scanner.l
+libsysdb_fe_parser_la_CFLAGS = -DBUILD_DATE="\"$$( date --utc '+%F %T' ) (UTC)\""
libsysdb_la_SOURCES = \
sysdb.c include/sysdb.h \
core/object.c include/core/object.h \
include/core/data.h \
frontend/connection.c include/frontend/connection.h \
frontend/connection-private.h \
+ frontend/parser.c \
frontend/sock.c include/frontend/sock.h \
frontend/session.c \
frontend/query.c \
libsysdb_la_CFLAGS = $(AM_CFLAGS)
libsysdb_la_CPPFLAGS = $(AM_CPPFLAGS) $(LTDLINCL)
libsysdb_la_LDFLAGS = $(AM_LDFLAGS) -version-info 0:0:0 -pthread
-libsysdb_la_LIBADD = $(LIBLTDL) -lrt liboconfig/liboconfig.la
-libsysdb_la_DEPENDENCIES = liboconfig/liboconfig.la
+libsysdb_la_LIBADD = libsysdb_fe_parser.la \
+ $(LIBLTDL) -lrt liboconfig/liboconfig.la
+libsysdb_la_DEPENDENCIES = libsysdb_fe_parser.la liboconfig/liboconfig.la
if BUILD_WITH_LIBDBI
libsysdb_la_SOURCES += \
diff --git a/src/frontend/grammar.y b/src/frontend/grammar.y
--- /dev/null
+++ b/src/frontend/grammar.y
@@ -0,0 +1,89 @@
+/*
+ * SysDB - src/frontend/grammar.y
+ * Copyright (C) 2013 Sebastian 'tokkee' Harl <sh@tokkee.org>
+ * 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.
+ */
+
+%{
+
+#include "frontend/parser.h"
+#include "frontend/grammar.h"
+#include "utils/error.h"
+
+#include <stdio.h>
+
+void
+sdb_fe_yyerror(YYLTYPE *lval, sdb_fe_yyscan_t scanner, const char *msg);
+
+%}
+
+%pure-parser
+%lex-param {sdb_fe_yyscan_t scanner}
+%parse-param {sdb_fe_yyscan_t scanner}
+%locations
+%error-verbose
+%expect 0
+%name-prefix="sdb_fe_yy"
+
+%start statements
+
+%token SCANNER_ERROR
+
+%token IDENTIFIER
+%token LIST
+
+%%
+
+statements:
+ statements ';' statement
+ {
+ }
+ |
+ statement
+ {
+ }
+ ;
+
+statement:
+ list_statement
+ {
+ }
+ |
+ /* empty */
+ ;
+
+list_statement:
+ LIST
+ ;
+
+%%
+
+void
+sdb_fe_yyerror(YYLTYPE *lval, sdb_fe_yyscan_t scanner, const char *msg)
+{
+ sdb_log(SDB_LOG_ERR, "frontend: parse error: %s", msg);
+} /* sdb_fe_yyerror */
+
+/* vim: set tw=78 sw=4 ts=4 noexpandtab : */
+
diff --git a/src/frontend/parser.c b/src/frontend/parser.c
--- /dev/null
+++ b/src/frontend/parser.c
@@ -0,0 +1,62 @@
+/*
+ * SysDB - src/frontend/parser.c
+ * Copyright (C) 2013 Sebastian 'tokkee' Harl <sh@tokkee.org>
+ * 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.
+ */
+
+#include "sysdb.h"
+
+#include "frontend/parser.h"
+#include "frontend/grammar.h"
+#include "frontend/connection-private.h"
+
+#include <string.h>
+
+/*
+ * public API
+ */
+
+int
+sdb_fe_parse(const char *query)
+{
+ sdb_fe_yyscan_t scanner;
+ int yyres;
+
+ if (! query)
+ return -1;
+
+ scanner = sdb_fe_scanner_init(query);
+ if (! scanner)
+ return -1;
+
+ yyres = sdb_fe_yyparse(scanner);
+ sdb_fe_scanner_destroy(scanner);
+
+ if (yyres)
+ return -1;
+ return 0;
+} /* sdb_fe_parse */
+
+/* vim: set tw=78 sw=4 ts=4 noexpandtab : */
+
diff --git a/src/frontend/scanner.l b/src/frontend/scanner.l
--- /dev/null
+++ b/src/frontend/scanner.l
@@ -0,0 +1,121 @@
+/*
+ * SysDB - src/frontend/scanner.l
+ * Copyright (C) 2013 Sebastian 'tokkee' Harl <sh@tokkee.org>
+ * 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.
+ */
+
+%{
+
+#include "frontend/parser.h"
+#include "frontend/grammar.h"
+#include "utils/error.h"
+
+#include <errno.h>
+
+#include <string.h>
+
+void
+sdb_fe_yyerror(YYLTYPE *lval, sdb_fe_yyscan_t scanner, const char *msg);
+
+%}
+
+%option never-interactive
+%option reentrant
+%option bison-bridge
+%option bison-locations
+%option 8bit
+%option yylineno
+%option nodefault
+%option noinput
+%option nounput
+%option noyywrap
+%option verbose
+%option warn
+%option prefix="sdb_fe_yy" outfile="lex.yy.c"
+
+%x CSC
+
+whitespace ([ \t\n\r\f]+)
+simple_comment ("--"[^\n\r]*)
+
+/*
+ * C style comments
+ */
+csc_start \/\*
+csc_inside ([^*/]+|[^*]\/|\*[^/])
+csc_end \*\/
+
+identifier ([A-Za-z_][A-Za-z_0-9$]*)
+
+%%
+
+{whitespace} |
+{simple_comment} { /* ignore */ }
+
+{csc_start} { BEGIN(CSC); }
+<CSC>{csc_inside} { /* ignore */ }
+<CSC>{csc_end} { BEGIN(INITIAL); }
+<CSC><<EOF>> {
+ sdb_fe_yyerror(yylval, yyscanner, "unterminated C-style comment");
+ return SCANNER_ERROR;
+ }
+
+{identifier} {
+ /* XXX */
+ if (! strcasecmp(yytext, "LIST"))
+ return LIST;
+
+ return IDENTIFIER;
+ }
+
+. { /* do nothing for now */ }
+
+%%
+
+sdb_fe_yyscan_t
+sdb_fe_scanner_init(const char *str)
+{
+ yyscan_t scanner;
+
+ if (sdb_fe_yylex_init(&scanner)) {
+ char errbuf[1024];
+ sdb_log(SDB_LOG_ERR, "frontend: yylex_init failed: %s",
+ sdb_strerror(errno, errbuf, sizeof(errbuf)));
+ return NULL;
+ }
+
+ /* 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);
+ return scanner;
+} /* sdb_fe_scanner_init */
+
+void
+sdb_fe_scanner_destroy(sdb_fe_yyscan_t scanner)
+{
+ sdb_fe_yylex_destroy(scanner);
+} /* sdb_fe_scanner_destroy */
+
+/* vim: set tw=78 sw=4 ts=4 noexpandtab : */
+
index a2163bce9841d479557ad5f8c4f93bdf69ba4af5..bdebe7d1f9f0d3ba70222893ee73b810cec55b7a 100644 (file)
int
sdb_connection_ping(sdb_conn_t *conn);
+/*
+ * sdb_fe_parse:
+ * Parse the query text specified in 'query'.
+ */
+int
+sdb_fe_parse(const char *query);
+
/*
* session handling
*/
diff --git a/src/include/frontend/parser.h b/src/include/frontend/parser.h
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ * SysDB - src/include/frontend/parser.h
+ * Copyright (C) 2013 Sebastian 'tokkee' Harl <sh@tokkee.org>
+ * 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.
+ */
+
+#ifndef SDB_FRONTEND_PARSER_H
+#define SDB_FRONTEND_PARSER_H 1
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* see yyscan_t */
+typedef void *sdb_fe_yyscan_t;
+
+sdb_fe_yyscan_t
+sdb_fe_scanner_init(const char *str);
+
+void
+sdb_fe_scanner_destroy(sdb_fe_yyscan_t scanner);
+
+#ifdef __cplusplus
+} /* extern "C" */
+#endif
+
+#endif /* ! SDB_FRONTEND_PARSER_H */
+
+/* vim: set tw=78 sw=4 ts=4 noexpandtab : */
+
index 1d021519cd02cf2de2f2e9c376067cd9cd4448fd..fd78c26cf61833bd5ac07cc40a17217982c71fdb 100644 (file)
CONNECTION_STARTUP,
/* querying */
+ CONNECTION_QUERY,
CONNECTION_LIST,
} sdb_conn_state_t;
index 3ec4b04f32f2ce302064866f63b0cd7e15910594..07a941370d9eb5f419674806544d9337f3f21f36 100644 (file)
*/
%{
+
+/*
+ * This is a simplified version of frontend/scanner.l. The only purpose is to
+ * find queries (terminated by semicolon).
+ */
+
#include "tools/sysdb/input.h"
#ifdef YY_INPUT
} while (0)
static sdb_input_t *sdb_input;
+
%}
%option interactive
diff --git a/t/Makefile.am b/t/Makefile.am
index 7aa6d77af1bda155c2fc8a28b9ce3fb5b2cd4bce..6adf484bf9ab8acca0761c7a93c8a7ea5f35a095 100644 (file)
--- a/t/Makefile.am
+++ b/t/Makefile.am
libsysdb_test.c libsysdb_test.h \
core/object_test.c \
core/store_test.c \
+ frontend/parser_test.c \
frontend/sock_test.c \
utils/channel_test.c \
utils/dbi_test.c \
diff --git a/t/frontend/parser_test.c b/t/frontend/parser_test.c
--- /dev/null
+++ b/t/frontend/parser_test.c
@@ -0,0 +1,94 @@
+/*
+ * SysDB - t/frontend/parser_test.c
+ * Copyright (C) 2013 Sebastian 'tokkee' Harl <sh@tokkee.org>
+ * 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.
+ */
+
+#include "frontend/connection.h"
+#include "libsysdb_test.h"
+
+#include <check.h>
+
+/*
+ * tests
+ */
+
+START_TEST(test_parse)
+{
+ struct {
+ const char *query;
+ int expected;
+ } golden_data[] = {
+ /* empty commands */
+ { NULL, -1 },
+ { "", 0 },
+ { ";", 0 },
+ { ";;", 0 },
+
+ /* valid commands */
+ { "LIST", 0 },
+ { "LIST;", 0 },
+
+ /* comments */
+ { "/* some comment */", 0 },
+ { "-- another comment", 0 },
+
+ /* syntax errors */
+ { "INVALID", -1 },
+ { "/* some incomplete", -1 },
+ };
+
+ size_t i;
+ int 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;
+ 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);
+ }
+}
+END_TEST
+
+Suite *
+fe_parser_suite(void)
+{
+ Suite *s = suite_create("frontend::parser");
+ TCase *tc;
+
+ tc = tcase_create("core");
+ tcase_add_test(tc, test_parse);
+ suite_add_tcase(s, tc);
+
+ return s;
+} /* util_parser_suite */
+
+/* vim: set tw=78 sw=4 ts=4 noexpandtab : */
+
diff --git a/t/libsysdb_test.c b/t/libsysdb_test.c
index 110df8b08c37e3f3abf3a5e01dfadee4c83cddb0..2fa83dc6af7f4dcbd758e19459cc8b37e37a7cde 100644 (file)
--- a/t/libsysdb_test.c
+++ b/t/libsysdb_test.c
suite_creator_t creators[] = {
{ core_object_suite, NULL },
{ core_store_suite, NULL },
+ { fe_parser_suite, NULL },
{ fe_sock_suite, NULL },
{ util_channel_suite, NULL },
{ util_dbi_suite, NULL },
diff --git a/t/libsysdb_test.h b/t/libsysdb_test.h
index 616a84e0a437921c9b491b9d09d952258ac9e7dc..64558cdadebfb2861f3c6f0fb459d7307c28eb65 100644 (file)
--- a/t/libsysdb_test.h
+++ b/t/libsysdb_test.h
Suite *
core_store_suite(void);
+/* t/frontend/parser_test */
+Suite *
+fe_parser_suite(void);
+
/* t/frontend/sock_test */
Suite *
fe_sock_suite(void);