diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2011-06-23 13:29:06 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2011-06-23 13:46:11 +0300 |
commit | 582d89672b36583a50a10be741853d3315d95bd0 (patch) | |
tree | 4c1e03152001525517abc784b4d546f40a5d2902 | |
parent | 00466520a845c0abd58ca7e98bc8bdbbe754702a (diff) | |
download | grecs-582d89672b36583a50a10be741853d3315d95bd0.tar.gz grecs-582d89672b36583a50a10be741853d3315d95bd0.tar.bz2 |
Conditionally include tests related to parser formats that can be disabled at configuration time.
* src/Make.am (PARSER_DEFS, AM_CPPFLAGS): New variables.
[GRECS_COND_META1_PARSER]: Update PARSER_DEFS.
[GRECS_COND_BIND_PARSER]: Likewise.
[GRECS_COND_GIT_PARSER]: Likewise.
(GRECS_SRC): Add parsertab.c
* src/parsertab.c: New file.
* src/grecs.h (grecs_parser_t): New typedef.
(grecs_enumerate_parsers)
(grecs_get_parser_by_type): New functions.
* tests/Makefile.am (PARSER_DEFS): New variable.
[GRECS_COND_META1_PARSER]: Update PARSER_DEFS.
[GRECS_COND_BIND_PARSER]: Likewise.
[GRECS_COND_GIT_PARSER]: Likewise.
(package.m4): Define ENABLE_* according to the PARSER_DEFS settings.
* tests/empty.at: Test ENABLE_* defines to avoid testing
unsupported parser formats.
* tests/testsuite.at: Likewise.
* tests/gcffmt.c: New option -list. Improve help output.
-rw-r--r-- | src/Make.am | 7 | ||||
-rw-r--r-- | src/grecs.h | 10 | ||||
-rw-r--r-- | src/parsertab.c | 65 | ||||
-rw-r--r-- | tests/Makefile.am | 16 | ||||
-rw-r--r-- | tests/empty.at | 9 | ||||
-rw-r--r-- | tests/gcffmt.c | 83 | ||||
-rw-r--r-- | tests/testsuite.at | 8 |
7 files changed, 176 insertions, 22 deletions
diff --git a/src/Make.am b/src/Make.am index ae6af64..bc70edc 100644 --- a/src/Make.am +++ b/src/Make.am @@ -14,18 +14,23 @@ # You should have received a copy of the GNU General Public License # along with Grecs. If not, see <http://www.gnu.org/licenses/>. +PARSER_DEFS = + if GRECS_COND_META1_PARSER GRECS_PARSER_META1 = meta1-gram.y meta1-lex.l GRECS_EXTRA_META1 = meta1-gram.h + PARSER_DEFS += -DENABLE_META1_PARSER endif if GRECS_COND_BIND_PARSER GRECS_PARSER_BIND = bind-gram.y bind-lex.l GRECS_EXTRA_BIND = bind-gram.h + PARSER_DEFS += -DENABLE_BIND_PARSER endif if GRECS_COND_GIT_PARSER GRECS_PARSER_GIT = git-parser.c + PARSER_DEFS += -DENABLE_GIT_PARSER endif GRECS_SRC = \ @@ -40,6 +45,7 @@ GRECS_SRC = \ lookup.c\ mem.c\ parser.c\ + parsertab.c\ path-parser.c\ preproc.c\ sort.c\ @@ -63,6 +69,7 @@ EXTRA_DIST=\ Make.am Make-inst.am Make-shared.am Make-static.am INCLUDES = -I$(srcdir) -I$(top_srcdir)/@GRECS_SUBDIR@ @GRECS_INCLUDES@ @GRECS_HOST_PROJECT_INCLUDES@ +AM_CPPFLAGS = $(PARSER_DEFS) AM_YFLAGS = -dtv AM_LFLAGS = -d diff --git a/src/grecs.h b/src/grecs.h index fd957a8..8bd70d1 100644 --- a/src/grecs.h +++ b/src/grecs.h @@ -218,7 +218,9 @@ int grecs_lex_begin(const char*, int); void grecs_lex_end(int err); struct grecs_node *grecs_parse(const char *name); -extern struct grecs_node *(*grecs_parser_fun)(const char *name, int trace); +typedef struct grecs_node *(*grecs_parser_t)(const char *name, int trace); + +extern grecs_parser_t grecs_parser_fun; /* Parsers: */ struct grecs_node *grecs_grecs_parser(const char *name, int traceflags); @@ -228,6 +230,12 @@ struct grecs_node *grecs_git_parser(const char *name, int traceflags); struct grecs_node *grecs_path_parser(const char *name, int traceflags); +/* Parser database */ +int grecs_enumerate_parsers(int (*fun)(const char *, grecs_parser_t, void *), + void *); +grecs_parser_t grecs_get_parser_by_type(const char *type); + + struct grecs_list *_grecs_simple_list_create(int dispose); struct grecs_list *grecs_value_list_create(void); diff --git a/src/parsertab.c b/src/parsertab.c new file mode 100644 index 0000000..ce4c7b4 --- /dev/null +++ b/src/parsertab.c @@ -0,0 +1,65 @@ +/* grecs - Gray's Extensible Configuration System + Copyright (C) 2007-2011 Sergey Poznyakoff + + Grecs is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by the + Free Software Foundation; either version 3 of the License, or (at your + option) any later version. + + Grecs is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with Grecs. If not, see <http://www.gnu.org/licenses/>. */ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif +#include <stdlib.h> +#include <errno.h> +#include "grecs.h" + +static struct parser_tab { + const char *name; + grecs_parser_t parser; +} parser_tab[] = { + { "GRECS", grecs_grecs_parser }, + { "PATH", grecs_path_parser }, +#ifdef ENABLE_META1_PARSER + { "META1", grecs_meta1_parser }, +#endif +#ifdef ENABLE_BIND_PARSER + { "BIND", grecs_bind_parser }, +#endif +#ifdef ENABLE_GIT_PARSER + { "GIT", grecs_git_parser }, +#endif + { NULL } +}; + +int +grecs_enumerate_parsers(int (*fun)(const char *, grecs_parser_t, void *), + void *data) +{ + struct parser_tab *pt; + int rc = 0; + + for (pt = parser_tab; rc == 0 && pt->name; pt++) + rc = fun(pt->name, pt->parser, data); + return rc; +} + +grecs_parser_t +grecs_get_parser_by_type(const char *type) +{ + struct parser_tab *pt; + + for (pt = parser_tab; pt->name; pt++) { + if (strcasecmp(pt->name, type) == 0) + return pt->parser; + } + return NULL; +} + diff --git a/tests/Makefile.am b/tests/Makefile.am index 6813bfc..cf80c97 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -30,6 +30,19 @@ EXTRA_DIST = \ DISTCLEANFILES = atconfig $(check_SCRIPTS) MAINTAINERCLEANFILES = Makefile.in $(TESTSUITE) +PARSER_DEFS = + +if GRECS_COND_META1_PARSER + PARSER_DEFS += ENABLE_META1_PARSER +endif + +if GRECS_COND_BIND_PARSER + PARSER_DEFS += ENABLE_BIND_PARSER +endif + +if GRECS_COND_GIT_PARSER + PARSER_DEFS += ENABLE_GIT_PARSER +endif ## ------------ ## ## package.m4. ## @@ -44,6 +57,9 @@ $(srcdir)/package.m4: $(top_srcdir)/configure.ac $(top_srcdir)/@GRECS_SUBDIR@/am echo 'm4_define([AT_PACKAGE_STRING], [@PACKAGE_STRING@])'; \ echo 'm4_define([AT_PACKAGE_BUGREPORT], [@PACKAGE_BUGREPORT@])'; \ echo 'm4_define([GRECS_DISTCK_AT], [@GRECS_DISTCK_AT@])'; \ + for var in $(PARSER_DEFS); do \ + echo "m4_define([$$var],1)"; \ + done; \ } >$(srcdir)/package.m4 # diff --git a/tests/empty.at b/tests/empty.at index db4bebc..b0762af 100644 --- a/tests/empty.at +++ b/tests/empty.at @@ -23,17 +23,19 @@ GRECS_TEST([], [ ]) +m4_ifdef([ENABLE_BIND_PARSER],[ GRECS_TEST([], [gcffmt -type=bind], [0], [ -]) +])]) +m4_ifdef([ENABLE_META1_PARSER],[ GRECS_TEST([], [gcffmt -type=meta1], [0], [ -]) +])]) GRECS_TEST([], [gcffmt -type=path], @@ -41,11 +43,12 @@ GRECS_TEST([], [ ]) +m4_ifdef([ENABLE_GIT_PARSER],[ GRECS_TEST([], [gcffmt -type=git], [0], [ -]) +])]) AT_CLEANUP diff --git a/tests/gcffmt.c b/tests/gcffmt.c index 6df2490..1352e5c 100644 --- a/tests/gcffmt.c +++ b/tests/gcffmt.c @@ -21,12 +21,38 @@ #include <errno.h> #include "grecs.h" +struct list_helper { + FILE *file; + int delim; +}; + +static int +list_parser_types(const char *name, grecs_parser_t parser, void *data) +{ + struct list_helper *p = data; + fprintf(p->file, "%c%s", p->delim, name); + p->delim = '|'; + return 0; +} + static void usage(const char *arg, FILE *fp, int code) { + struct list_helper lh; + int w; + fprintf(fp, - "usage: %s [-h] [-locus] [-delim=char] [-reduce] [-sort] " - "[-type=grecs|bind|meta1|git|path] [-Idir] [-include=dir] file [file...]\n", + "usage: %s%n [-h] [-list[=type]] [-locus] [-delim=char] [-reduce] [-sort]\n", + arg, &w); + do + fputc(' ', fp); + while (w--); + + fprintf(fp, "[-type"); + lh.file = fp; + lh.delim = '='; + grecs_enumerate_parsers(list_parser_types, &lh); + fprintf(fp, "] [-Idir] [-include=dir] file [file...]\n", arg); exit(code); } @@ -36,7 +62,33 @@ node_ident_cmp(struct grecs_node const *a, struct grecs_node const *b) { return strcmp(a->ident, b->ident); } - + +int +list_parser(const char *name, grecs_parser_t parser, void *data) +{ + printf("%s\n", name); + return 0; +} + +int +find_parser(const char *name, grecs_parser_t parser, void *data) +{ + if (strcasecmp(name, (char*)data) == 0) + exit(0); + return 0; +} + +int +set_parser(const char *arg) +{ + grecs_parser_t p = grecs_get_parser_by_type(arg); + if (p) { + grecs_parser_fun = p; + return 0; + } + return 1; +} + int main(int argc, char **argv) { @@ -59,18 +111,14 @@ main(int argc, char **argv) sort = 1; else if (strcmp(arg, "-h") == 0) usage(progname, stdout, 0); - else if (strncmp(arg, "-type=", 6) == 0) { - if (strcasecmp(arg+6, "GRECS") == 0) - grecs_parser_fun = grecs_grecs_parser; - else if (strcasecmp(arg+6, "META1") == 0) - grecs_parser_fun = grecs_meta1_parser; - else if (strcasecmp(arg+6, "BIND") == 0) - grecs_parser_fun = grecs_bind_parser; - else if (strcasecmp(arg+6, "GIT") == 0) - grecs_parser_fun = grecs_git_parser; - else if (strcasecmp(arg+6, "PATH") == 0) - grecs_parser_fun = grecs_path_parser; - else + else if (strcmp(arg, "-list") == 0) { + grecs_enumerate_parsers(list_parser, NULL); + exit(0); + } else if (strncmp(arg, "-list=", 6) == 0) { + grecs_enumerate_parsers(find_parser, arg + 6); + exit(2); + } else if (strncmp(arg, "-type=", 6) == 0) { + if (set_parser(arg + 6)) usage(progname, stderr, 1); } else if (strncmp(arg, "-I", 2) == 0) grecs_preproc_add_include_dir(arg+2); @@ -88,6 +136,11 @@ main(int argc, char **argv) break; } } + + if (!grecs_parser_fun) { + fprintf(stderr, "%s: requested type not supported", progname); + exit(2); + } if (!file) usage(progname, stderr, 1); diff --git a/tests/testsuite.at b/tests/testsuite.at index d8045bb..3da0bc8 100644 --- a/tests/testsuite.at +++ b/tests/testsuite.at @@ -73,14 +73,16 @@ m4_include([reduce03.at]) m4_include([join.at]) +m4_ifdef([ENABLE_META1_PARSER],[ m4_include([parser-bind.at]) -m4_include([parser-git.at]) -m4_include([parser-meta1.at]) +m4_include([bind00.at])]) + +m4_ifdef([ENABLE_BIND_PARSER],[m4_include([parser-meta1.at])]) +m4_ifdef([ENABLE_GIT_PARSER],[m4_include([parser-git.at])]) m4_include([vercmp.at]) m4_include([grecs00.at]) -m4_include([bind00.at]) m4_include([empty.at]) |