diff options
author | Vincent Sanders <vincent.sanders@collabora.co.uk> | 2012-09-05 11:38:56 +0100 |
---|---|---|
committer | Vincent Sanders <vincent.sanders@collabora.co.uk> | 2012-09-05 20:06:05 +0100 |
commit | 26dc8906aeb0783cf36bde31e9051b29a193eb23 (patch) | |
tree | 0921de4f6a92c3dc6571c1ba693e4ff19d76df8d /src | |
parent | 1c7bc7e17ace1e457c4c0336353f142aef36d254 (diff) | |
download | nsgenbind-26dc8906aeb0783cf36bde31e9051b29a193eb23.tar.gz nsgenbind-26dc8906aeb0783cf36bde31e9051b29a193eb23.tar.bz2 |
make tests work
add basic commandlien handling
Diffstat (limited to 'src')
-rw-r--r-- | src/Makefile | 4 | ||||
-rw-r--r-- | src/genbind-parser.y | 58 | ||||
-rw-r--r-- | src/genjsbind-lexer.l (renamed from src/genbind-lexer.l) | 7 | ||||
-rw-r--r-- | src/genjsbind-parser.y | 60 | ||||
-rw-r--r-- | src/genjsbind.c | 173 | ||||
-rw-r--r-- | src/genjsbind.h | 1 | ||||
-rw-r--r-- | src/webidl-ast.h | 6 | ||||
-rw-r--r-- | src/webidl-lexer.l | 1 | ||||
-rw-r--r-- | src/webidl-parser.y | 10 |
9 files changed, 227 insertions, 93 deletions
diff --git a/src/Makefile b/src/Makefile index 232a7dd..6740140 100644 --- a/src/Makefile +++ b/src/Makefile @@ -3,7 +3,7 @@ CFLAGS := $(CFLAGS) -I$(BUILDDIR) -Isrc/ # Sources in this directory DIR_SOURCES := genjsbind.c -SOURCES := $(SOURCES) $(BUILDDIR)/genbind-parser.c $(BUILDDIR)/genbind-lexer.c $(BUILDDIR)/webidl-parser.c $(BUILDDIR)/webidl-lexer.c +SOURCES := $(SOURCES) $(BUILDDIR)/genjsbind-parser.c $(BUILDDIR)/genjsbind-lexer.c $(BUILDDIR)/webidl-parser.c $(BUILDDIR)/webidl-lexer.c $(BUILDDIR)/%-lexer.c $(BUILDDIR)/%-lexer.h: src/%-lexer.l $(VQ)$(ECHO) " FLEX: $<" @@ -31,7 +31,7 @@ endif $(BUILDDIR)/%-parser.c $(BUILDDIR)/%-parser.h: src/%-parser.y $(VQ)$(ECHO) " BISON: $<" - $(Q)bison -d -t $(BISON_DEFINES) --output=$(BUILDDIR)/$(*F)-parser.c --defines=$(BUILDDIR)/$(*F)-parser.h $< + $(Q)bison -d -t $(BISON_DEFINES) --report=all --output=$(BUILDDIR)/$(*F)-parser.c --defines=$(BUILDDIR)/$(*F)-parser.h $< # Grab the core makefile include $(NSBUILD)/Makefile.subdir diff --git a/src/genbind-parser.y b/src/genbind-parser.y deleted file mode 100644 index cad20b4..0000000 --- a/src/genbind-parser.y +++ /dev/null @@ -1,58 +0,0 @@ -/* - * This is a bison parser for genbind - * - */ - -%{ - -#include <stdio.h> -#include <string.h> - -#include "genbind-parser.h" -#include "genbind-lexer.h" - - extern int loadwebidl(char *filename); - -void genbind_error(const char *str) -{ - fprintf(stderr,"error: %s\n",str); -} - - -int genbind_wrap() -{ - return 1; -} - - - -%} - -%define api.pure - -%union -{ - char* text; -} - -%token TOK_IDLFILE - -%token <text> TOK_STRING_LITERAL - -%% - - /* [1] start with instructions */ -Instructions: - /* empty */ - | - IdlFile - ; - -IdlFile: - TOK_IDLFILE TOK_STRING_LITERAL - { - loadwebidl($2); - } - ; - -%% diff --git a/src/genbind-lexer.l b/src/genjsbind-lexer.l index 6150f09..e976a8f 100644 --- a/src/genbind-lexer.l +++ b/src/genjsbind-lexer.l @@ -7,8 +7,7 @@ %option bison-bridge %option nodefault %option warn -%option debug -%option prefix="genbind_" +%option prefix="genjsbind_" %option nounput /* header block */ @@ -18,7 +17,7 @@ #include <stdio.h> #include <string.h> -#include "genbind-parser.h" +#include "genjsbind-parser.h" %} @@ -50,4 +49,4 @@ webidlfile return TOK_IDLFILE; . /* nothing */ -%%
\ No newline at end of file +%% diff --git a/src/genjsbind-parser.y b/src/genjsbind-parser.y new file mode 100644 index 0000000..08b8926 --- /dev/null +++ b/src/genjsbind-parser.y @@ -0,0 +1,60 @@ +/* + * This is a bison parser for genbind + * + */ + +%{ + +#include <stdio.h> +#include <string.h> + +#include "genjsbind-parser.h" +#include "genjsbind-lexer.h" +#include "genjsbind.h" + +static void genjsbind_error(const char *str) +{ + fprintf(stderr,"error: %s\n",str); +} + + +int genjsbind_wrap() +{ + return 1; +} + + + +%} + +%define api.pure + +%union +{ + char* text; +} + +%token TOK_IDLFILE + +%token <text> TOK_STRING_LITERAL + +%% + + /* [1] start with Statements */ +Statements: + /* empty */ + | + IdlFile + ; + + /* [2] load a web IDL file */ +IdlFile: + TOK_IDLFILE TOK_STRING_LITERAL ';' + { + if (loadwebidl($2) != 0) { + YYABORT; + } + } + ; + +%% diff --git a/src/genjsbind.c b/src/genjsbind.c index 150c780..145c16e 100644 --- a/src/genjsbind.c +++ b/src/genjsbind.c @@ -1,52 +1,177 @@ #include <stdio.h> +#include <stdlib.h> +#include <stdbool.h> +#include <string.h> +#include <unistd.h> +#include <getopt.h> +#include <errno.h> #include "webidl-ast.h" - #include "webidl-parser.h" -#include "genbind-parser.h" +#include "genjsbind-parser.h" +#include "genjsbind.h" extern int webidl_debug; +extern int webidl__flex_debug; extern FILE* webidl_in; -extern int webidl_parse(); +extern int webidl_parse(void); + +extern int genjsbind_debug; +extern int genjsbind__flex_debug; +extern FILE* genjsbind_in; +extern int genjsbind_parse(void); + +struct options { + char *outfilename; + char *infilename; + char *idlpath; + bool verbose; + bool debug; +}; + +struct options *options; + +static FILE *idlopen(const char *filename) +{ + FILE *idlfile; -extern int genbind_debug; -extern FILE* genbind_in; -extern int genbind_parse(); + if (options->idlpath == NULL) { + if (options->verbose) { + printf("Opening IDL file %s\n", filename); + } + idlfile = fopen(filename, "r"); + } else { + char *fullname; + int fulllen = strlen(options->idlpath) + strlen(filename) + 2; + fullname = malloc(fulllen); + snprintf(fullname, fulllen, "%s/%s", options->idlpath, filename); + if (options->verbose) { + printf("Opening IDL file %s\n", fullname); + } + idlfile = fopen(fullname, "r"); + free(fullname); + } + return idlfile; +} int loadwebidl(char *filename) { - FILE *myfile = fopen(filename, "r"); - if (!myfile) { - perror(filename); + /* set flex to read from file */ + webidl_in = idlopen(filename); + if (!webidl_in) { + fprintf(stderr, "Error opening %s: %s\n", + filename, + strerror(errno)); return 2; } - /* set flex to read from file */ - webidl_in = myfile; - webidl_debug = 1; + if (options->debug) { + webidl_debug = 1; + webidl__flex_debug = 1; + } - /* parse through the input until there is no more: */ - while (!feof(webidl_in)) { - webidl_parse(); + /* parse the file */ + return webidl_parse(); +} + + +static struct options* process_cmdline(int argc, char **argv) +{ + int opt; + + options = calloc(1,sizeof(struct options)); + if (options == NULL) { + fprintf(stderr, "Allocation error\n"); + return NULL; } - return 0; + + while ((opt = getopt(argc, argv, "vdI:o:")) != -1) { + switch (opt) { + case 'I': + options->idlpath = strdup(optarg); + break; + + case 'o': + options->outfilename = strdup(optarg); + break; + + case 'v': + options->verbose = true; + break; + + case 'd': + options->debug = true; + break; + + default: /* '?' */ + fprintf(stderr, + "Usage: %s [-I idlpath] [-o filename] inputfile\n", + argv[0]); + free(options); + return NULL; + + } + } + + if (optind >= argc) { + fprintf(stderr, "Error: expected input filename\n"); + free(options); + return NULL; + } + + options->infilename = strdup(argv[optind]); + + return options; + } int main(int argc, char **argv) { - FILE *myfile = fopen("htmldocument.bnd", "r"); - if (!myfile) { - perror(NULL); + FILE *infile; + int parse_res; + + options = process_cmdline(argc, argv); + if (options == NULL) { + return 1; /* bad commandline */ + } + + if (options->verbose && (options->outfilename == NULL)) { + fprintf(stderr, "Error: outputting to stdout with verbose logging would fail\n"); return 2; } + + if ((options->infilename[0] == '-') && + (options->infilename[1] == 0)) { + if (options->verbose) { + printf("Using stdin for input\n"); + } + infile = stdin; + } else { + if (options->verbose) { + printf("Opening binding file %s\n", options->infilename); + } + infile = fopen(options->infilename, "r"); + } + + if (!infile) { + fprintf(stderr, "Error opening %s: %s\n", + options->infilename, + strerror(errno)); + return 3; + } + /* set flex to read from file */ - genbind_in = myfile; + genjsbind_in = infile; - genbind_debug = 1; + if (options->debug) { + genjsbind_debug = 1; + genjsbind__flex_debug = 1; + } - /* parse through the input until there is no more: */ - while (!feof(genbind_in)) { - genbind_parse(); + parse_res = genjsbind_parse(); + if (parse_res) { + fprintf(stderr, "parse result was %d\n", parse_res); + return parse_res; } return 0; } diff --git a/src/genjsbind.h b/src/genjsbind.h new file mode 100644 index 0000000..69d2f59 --- /dev/null +++ b/src/genjsbind.h @@ -0,0 +1 @@ +extern int loadwebidl(char *filename); diff --git a/src/webidl-ast.h b/src/webidl-ast.h index 96d7c2a..f56c5d9 100644 --- a/src/webidl-ast.h +++ b/src/webidl-ast.h @@ -1,2 +1,4 @@ - struct ifmembers_s { - }; + +struct ifmembers_s { + char *name; +}; diff --git a/src/webidl-lexer.l b/src/webidl-lexer.l index 1d2db67..20ee4c5 100644 --- a/src/webidl-lexer.l +++ b/src/webidl-lexer.l @@ -16,7 +16,6 @@ %option bison-locations %option nodefault %option warn -%option debug %option prefix="webidl_" %option nounput diff --git a/src/webidl-parser.y b/src/webidl-parser.y index 73ed2c6..c4e25f1 100644 --- a/src/webidl-parser.y +++ b/src/webidl-parser.y @@ -16,9 +16,9 @@ -void webidl_error(const char *str) +static void webidl_error(const char *str) { - fprintf(stderr,"error: %s\n",str); + fprintf(stderr,"error: %s\n",str); } int webidl_wrap() @@ -33,6 +33,12 @@ int webidl_wrap() %locations %define api.pure + /* the w3c grammar results in 10 shift/reduce, 2 reduce/reduce conflicts + * The reduce/reduce error are both the result of empty sequences + */ + /* %expect 10 */ + /* %expect-rr 2 */ + %union { int attr; |