From b00346cd08b55d25e427cfabafbaa66e09a8444b Mon Sep 17 00:00:00 2001 From: Vincent Sanders Date: Tue, 23 Oct 2012 17:43:09 +0100 Subject: The name of teh tool is nsgenbind correct this everywhere --- Makefile | 4 +- src/Makefile | 4 +- src/genjsbind-ast.c | 363 -------------------------------------------- src/genjsbind-ast.h | 78 ---------- src/genjsbind-lexer.l | 154 ------------------- src/genjsbind-parser.y | 228 ---------------------------- src/genjsbind.c | 108 ------------- src/jsapi-libdom-operator.c | 4 +- src/jsapi-libdom.c | 6 +- src/jsapi-libdom.h | 6 +- src/nsgenbind-ast.c | 363 ++++++++++++++++++++++++++++++++++++++++++++ src/nsgenbind-ast.h | 78 ++++++++++ src/nsgenbind-lexer.l | 154 +++++++++++++++++++ src/nsgenbind-parser.y | 228 ++++++++++++++++++++++++++++ src/nsgenbind.c | 108 +++++++++++++ src/options.h | 6 +- src/webidl-ast.c | 8 +- src/webidl-ast.h | 6 +- src/webidl-lexer.l | 2 +- src/webidl-parser.y | 2 +- 20 files changed, 955 insertions(+), 955 deletions(-) delete mode 100644 src/genjsbind-ast.c delete mode 100644 src/genjsbind-ast.h delete mode 100644 src/genjsbind-lexer.l delete mode 100644 src/genjsbind-parser.y delete mode 100644 src/genjsbind.c create mode 100644 src/nsgenbind-ast.c create mode 100644 src/nsgenbind-ast.h create mode 100644 src/nsgenbind-lexer.l create mode 100644 src/nsgenbind-parser.y create mode 100644 src/nsgenbind.c diff --git a/Makefile b/Makefile index ecb22d2..f149ad0 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ # Define the component name -COMPONENT := genjsbind +COMPONENT := nsgenbind # And the component type COMPONENT_TYPE := binary # Component version @@ -34,5 +34,5 @@ endif # Grab the core makefile include $(NSBUILD)/Makefile.top -# Add extra install rules for our pkg-config control file and the library itself +# Add extra install rules for binary INSTALL_ITEMS := $(INSTALL_ITEMS) /bin:$(BUILDDIR)/$(COMPONENT)$(EXEEXT) diff --git a/src/Makefile b/src/Makefile index c4821bf..2716fa8 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,9 +1,9 @@ CFLAGS := $(CFLAGS) -I$(BUILDDIR) -Isrc/ -g # Sources in this directory -DIR_SOURCES := genjsbind.c webidl-ast.c genjsbind-ast.c jsapi-libdom.c jsapi-libdom-operator.c +DIR_SOURCES := nsgenbind.c webidl-ast.c nsgenbind-ast.c jsapi-libdom.c jsapi-libdom-operator.c -SOURCES := $(SOURCES) $(BUILDDIR)/genjsbind-parser.c $(BUILDDIR)/genjsbind-lexer.c $(BUILDDIR)/webidl-parser.c $(BUILDDIR)/webidl-lexer.c +SOURCES := $(SOURCES) $(BUILDDIR)/nsgenbind-parser.c $(BUILDDIR)/nsgenbind-lexer.c $(BUILDDIR)/webidl-parser.c $(BUILDDIR)/webidl-lexer.c $(BUILDDIR)/%-lexer.c $(BUILDDIR)/%-lexer.h: src/%-lexer.l $(VQ)$(ECHO) " FLEX: $<" diff --git a/src/genjsbind-ast.c b/src/genjsbind-ast.c deleted file mode 100644 index c3329cf..0000000 --- a/src/genjsbind-ast.c +++ /dev/null @@ -1,363 +0,0 @@ -/* binding generator AST implementation for parser - * - * This file is part of nsgenjsbind. - * Licensed under the MIT License, - * http://www.opensource.org/licenses/mit-license.php - * Copyright 2012 Vincent Sanders - */ - -/** @todo this currently stuffs everything in one global tree, not very nice - */ - -#include -#include -#include -#include -#include - -#include "genjsbind-ast.h" -#include "options.h" - -/* parser and lexer interface */ -extern int genjsbind_debug; -extern int genjsbind__flex_debug; -extern void genjsbind_restart(FILE*); -extern int genjsbind_parse(struct genbind_node **genbind_ast); - -/* terminal nodes have a value only */ -struct genbind_node { - enum genbind_node_type type; - struct genbind_node *l; - union { - void *value; - struct genbind_node *node; - char *text; - } r; -}; - - -char *genbind_strapp(char *a, char *b) -{ - char *fullstr; - int fulllen; - fulllen = strlen(a) + strlen(b) + 1; - fullstr = malloc(fulllen); - snprintf(fullstr, fulllen, "%s%s", a, b); - free(a); - free(b); - return fullstr; -} - -struct genbind_node *genbind_node_link(struct genbind_node *tgt, struct genbind_node *src) -{ - tgt->l = src; - return tgt; -} - - -struct genbind_node * -genbind_new_node(enum genbind_node_type type, struct genbind_node *l, void *r) -{ - struct genbind_node *nn; - nn = calloc(1, sizeof(struct genbind_node)); - nn->type = type; - nn->l = l; - nn->r.value = r; - return nn; -} - -int -genbind_node_for_each_type(struct genbind_node *node, - enum genbind_node_type type, - genbind_callback_t *cb, - void *ctx) -{ - int ret; - - if (node == NULL) { - return -1; - } - if (node->l != NULL) { - ret = genbind_node_for_each_type(node->l, type, cb, ctx); - if (ret != 0) { - return ret; - } - } - if (node->type == type) { - return cb(node, ctx); - } - - return 0; -} - - -/* exported interface defined in genjsbind-ast.h */ -struct genbind_node * -genbind_node_find(struct genbind_node *node, - struct genbind_node *prev, - genbind_callback_t *cb, - void *ctx) -{ - struct genbind_node *ret; - - if (node == NULL) { - return NULL; - } - - if (node->l != prev) { - ret = genbind_node_find(node->l, prev, cb, ctx); - if (ret != NULL) { - return ret; - } - } - - if (cb(node, ctx) != 0) { - return node; - } - - return NULL; -} - -/* exported interface defined in genjsbind-ast.h */ -struct genbind_node * -genbind_node_find_type(struct genbind_node *node, - struct genbind_node *prev, - enum genbind_node_type type) -{ - return genbind_node_find(node, - prev, - genbind_cmp_node_type, - (void *)type); -} - -struct genbind_node * -genbind_node_find_type_ident(struct genbind_node *node, - struct genbind_node *prev, - enum genbind_node_type type, - const char *ident) -{ - struct genbind_node *found_node; - struct genbind_node *ident_node; - - found_node = genbind_node_find(node, - prev, - genbind_cmp_node_type, - (void *)type); - - while (found_node != NULL) { - - ident_node = genbind_node_find(genbind_node_getnode(found_node), - NULL, - genbind_cmp_node_type, - (void *)GENBIND_NODE_TYPE_IDENT); - if (ident_node != NULL) { - if (strcmp(ident_node->r.text, ident) == 0) - break; - } - - /* look for next matching node */ - found_node = genbind_node_find(node, - found_node, - genbind_cmp_node_type, - (void *)type); - - } - return found_node; -} - -int genbind_cmp_node_type(struct genbind_node *node, void *ctx) -{ - if (node->type == (enum genbind_node_type)ctx) - return 1; - return 0; -} - -char *genbind_node_gettext(struct genbind_node *node) -{ - switch(node->type) { - case GENBIND_NODE_TYPE_WEBIDLFILE: - case GENBIND_NODE_TYPE_STRING: - case GENBIND_NODE_TYPE_PREAMBLE: - case GENBIND_NODE_TYPE_IDENT: - case GENBIND_NODE_TYPE_BINDING_TYPE: - case GENBIND_NODE_TYPE_BINDING_INTERFACE: - case GENBIND_NODE_TYPE_CBLOCK: - return node->r.text; - - default: - return NULL; - } -} - -struct genbind_node *genbind_node_getnode(struct genbind_node *node) -{ - switch(node->type) { - case GENBIND_NODE_TYPE_HDRCOMMENT: - case GENBIND_NODE_TYPE_BINDING: - case GENBIND_NODE_TYPE_BINDING_PRIVATE: - case GENBIND_NODE_TYPE_OPERATION: - return node->r.node; - - default: - return NULL; - } -} - -static const char *genbind_node_type_to_str(enum genbind_node_type type) -{ - switch(type) { - case GENBIND_NODE_TYPE_IDENT: - return "Ident"; - - case GENBIND_NODE_TYPE_ROOT: - return "Root"; - - case GENBIND_NODE_TYPE_WEBIDLFILE: - return "webidlfile"; - - case GENBIND_NODE_TYPE_HDRCOMMENT: - return "HdrComment"; - - case GENBIND_NODE_TYPE_STRING: - return "String"; - - case GENBIND_NODE_TYPE_PREAMBLE: - return "Preamble"; - - case GENBIND_NODE_TYPE_BINDING: - return "Binding"; - - case GENBIND_NODE_TYPE_BINDING_TYPE: - return "Type"; - - case GENBIND_NODE_TYPE_BINDING_PRIVATE: - return "Private"; - - case GENBIND_NODE_TYPE_BINDING_INTERFACE: - return "Interface"; - - case GENBIND_NODE_TYPE_OPERATION: - return "Operation"; - - case GENBIND_NODE_TYPE_CBLOCK: - return "CBlock"; - - default: - return "Unknown"; - } -} - -int genbind_ast_dump(struct genbind_node *node, int indent) -{ - const char *SPACES=" "; - char *txt; - - while (node != NULL) { - printf("%.*s%s", indent, SPACES, genbind_node_type_to_str(node->type)); - - txt = genbind_node_gettext(node); - if (txt == NULL) { - printf("\n"); - genbind_ast_dump(genbind_node_getnode(node), indent + 2); - } else { - printf(": \"%.*s\"\n", 75 - indent, txt); - } - node = node->l; - } - return 0; -} - -FILE *genbindopen(const char *filename) -{ - FILE *genfile; - char *fullname; - int fulllen; - static char *prevfilepath = NULL; - - /* try filename raw */ - genfile = fopen(filename, "r"); - if (genfile != NULL) { - if (options->verbose) { - printf("Opened Genbind file %s\n", filename); - } - if (prevfilepath == NULL) { - fullname = strrchr(filename, '/'); - if (fullname == NULL) { - fulllen = strlen(filename); - } else { - fulllen = fullname - filename; - } - prevfilepath = strndup(filename,fulllen); - } - return genfile; - } - - /* try based on previous filename */ - if (prevfilepath != NULL) { - fulllen = strlen(prevfilepath) + strlen(filename) + 2; - fullname = malloc(fulllen); - snprintf(fullname, fulllen, "%s/%s", prevfilepath, filename); - if (options->debug) { - printf("Attempting to open Genbind file %s\n", fullname); - } - genfile = fopen(fullname, "r"); - if (genfile != NULL) { - if (options->verbose) { - printf("Opened Genbind file %s\n", fullname); - } - free(fullname); - return genfile; - } - free(fullname); - } - - /* try on idl path */ - if (options->idlpath != NULL) { - fulllen = strlen(options->idlpath) + strlen(filename) + 2; - fullname = malloc(fulllen); - snprintf(fullname, fulllen, "%s/%s", options->idlpath, filename); - genfile = fopen(fullname, "r"); - if ((genfile != NULL) && options->verbose) { - printf("Opend Genbind file %s\n", fullname); - } - - free(fullname); - } - - return genfile; -} - -int genbind_parsefile(char *infilename, struct genbind_node **ast) -{ - FILE *infile; - - /* open input file */ - if ((infilename[0] == '-') && - (infilename[1] == 0)) { - if (options->verbose) { - printf("Using stdin for input\n"); - } - infile = stdin; - } else { - infile = genbindopen(infilename); - } - - if (!infile) { - fprintf(stderr, "Error opening %s: %s\n", - infilename, - strerror(errno)); - return 3; - } - - if (options->debug) { - genjsbind_debug = 1; - genjsbind__flex_debug = 1; - } - - /* set flex to read from file */ - genjsbind_restart(infile); - - /* process binding */ - return genjsbind_parse(ast); - -} diff --git a/src/genjsbind-ast.h b/src/genjsbind-ast.h deleted file mode 100644 index 416fa7b..0000000 --- a/src/genjsbind-ast.h +++ /dev/null @@ -1,78 +0,0 @@ -/* binding file AST interface - * - * This file is part of nsgenjsbind. - * Licensed under the MIT License, - * http://www.opensource.org/licenses/mit-license.php - * Copyright 2012 Vincent Sanders - */ - -#ifndef genjsbind_genjsbind_ast_h -#define genjsbind_genjsbind_ast_h - -enum genbind_node_type { - GENBIND_NODE_TYPE_ROOT = 0, - GENBIND_NODE_TYPE_IDENT, - GENBIND_NODE_TYPE_CBLOCK, - GENBIND_NODE_TYPE_WEBIDLFILE, - GENBIND_NODE_TYPE_HDRCOMMENT, - GENBIND_NODE_TYPE_STRING, - GENBIND_NODE_TYPE_PREAMBLE, - GENBIND_NODE_TYPE_BINDING, - GENBIND_NODE_TYPE_BINDING_TYPE, - GENBIND_NODE_TYPE_BINDING_PRIVATE, - GENBIND_NODE_TYPE_BINDING_INTERFACE, - GENBIND_NODE_TYPE_OPERATION, -}; - - -struct genbind_node; - -/** callback for search and iteration routines */ -typedef int (genbind_callback_t)(struct genbind_node *node, void *ctx); - -int genbind_cmp_node_type(struct genbind_node *node, void *ctx); - -FILE *genbindopen(const char *filename); - -int genbind_parsefile(char *infilename, struct genbind_node **ast); - -char *genbind_strapp(char *a, char *b); - -struct genbind_node *genbind_new_node(enum genbind_node_type type, struct genbind_node *l, void *r); -struct genbind_node *genbind_node_link(struct genbind_node *tgt, struct genbind_node *src); - -int genbind_ast_dump(struct genbind_node *ast, int indent); - -/** Depth first left hand search using user provided comparison - * - * @param node The node to start the search from - - * @param prev The node at which to stop the search, either NULL to - * search the full tree depth (initial search) or the result - * of a previous search to continue. - * @param cb Comparison callback - * @param ctx Context for callback - */ -struct genbind_node * -genbind_node_find(struct genbind_node *node, - struct genbind_node *prev, - genbind_callback_t *cb, - void *ctx); - -struct genbind_node * -genbind_node_find_type(struct genbind_node *node, - struct genbind_node *prev, - enum genbind_node_type type); - -struct genbind_node * -genbind_node_find_type_ident(struct genbind_node *node, - struct genbind_node *prev, - enum genbind_node_type type, - const char *ident); - -int genbind_node_for_each_type(struct genbind_node *node, enum genbind_node_type type, genbind_callback_t *cb, void *ctx); - -char *genbind_node_gettext(struct genbind_node *node); -struct genbind_node *genbind_node_getnode(struct genbind_node *node); - -#endif diff --git a/src/genjsbind-lexer.l b/src/genjsbind-lexer.l deleted file mode 100644 index 3701252..0000000 --- a/src/genjsbind-lexer.l +++ /dev/null @@ -1,154 +0,0 @@ -%{ - -/* lexer for the binding generation config file - * - * This file is part of nsgenjsbind. - * Licensed under the MIT License, - * http://www.opensource.org/licenses/mit-license.php - * Copyright 2012 Vincent Sanders - */ - -#include -#include -#include - -#include "genjsbind-parser.h" -#include "genjsbind-ast.h" - -#define YY_USER_ACTION yylloc->first_line = yylloc->last_line; \ - yylloc->first_column = yylloc->last_column + 1; \ - yylloc->last_column += yyleng; - -%} - -/* lexer options */ -%option never-interactive -%option yylineno -%option bison-bridge -%option bison-locations -%option nodefault -%option warn -%option prefix="genjsbind_" -%option nounput -%option noinput -%option noyywrap - -/* other Unicode “space separator” */ -USP (\xe1\x9a\x80)|(\xe1\xa0\x8e)|(\xe2\x80[\x80-\x8a])|(\xe2\x80\xaf)|(\xe2\x81\x9f)|(\xe3\x80\x80) - -/* non breaking space \u00A0 */ -NBSP (\xc2\xa0) - -/* Line separator \u2028 */ -LS (\xe2\x80\xa8) - -/* paragraph separator \u2029 */ -PS (\xe2\x80\xa9) - -whitespace ([ \t\v\f]|{NBSP}|{USP}) - -lineend ([\n\r]|{LS}|{PS}) - -multicomment \/\*(([^*])|(\*[^/]))*\*\/ - -quotedstring [^\"\\\n\r] - -identifier [A-Z_a-z][0-9A-Z_a-z]* - -other [^\t\n\r 0-9A-Z_a-z] - -cblockopen \%\{ - -cblockclose \%\} - -/* used for #include directive */ -poundsign ^{whitespace}*# - -%x cblock - -%x incl - -%% - -{whitespace} ++yylloc->last_column;/* nothing */ - -{lineend} if (yytext[0] != '\r') { - /* update position counts */ - ++yylloc->last_line; - yylloc->last_column = 0; - } - - /* terminals */ - -webidlfile return TOK_IDLFILE; - -hdrcomment return TOK_HDR_COMMENT; - -preamble return TOK_PREAMBLE; - -binding return TOK_BINDING; - -interface return TOK_INTERFACE; - -type return TOK_TYPE; - -private return TOK_PRIVATE; - -operation return TOK_OPERATION; - -{cblockopen} BEGIN(cblock); - -{identifier} { - /* A leading "_" is used to escape an identifier from - * looking like a reserved word terminal. - */ - yylval->text = (yytext[0] == '_') ? strdup(yytext + 1) : strdup(yytext); - return TOK_IDENTIFIER; - } - -\"{quotedstring}*\" yylval->text = strndup(yytext + 1, yyleng - 2 ); return TOK_STRING_LITERAL; - -{multicomment} /* nothing */ - -{poundsign}include BEGIN(incl); - -{other} return (int) yytext[0]; - -. /* nothing */ - -[^\%]* yylval->text = strdup(yytext); return TOK_CCODE_LITERAL; -{cblockclose} BEGIN(INITIAL); -\% yylval->text = strdup(yytext); return TOK_CCODE_LITERAL; - - -[ \t]*\" /* eat the whitespace and open quotes */ - -[^\t\n\"]+ { - /* got the include file name */ - yyin = genbindopen(yytext); - - if (! yyin) { - fprintf(stderr, "Unable to open include %s\n", yytext); - exit(3); - } - yypush_buffer_state(yy_create_buffer( yyin, YY_BUF_SIZE )); - - BEGIN(INITIAL); - } - -\n BEGIN(INITIAL); - -. /* nothing */ - -<> { - yypop_buffer_state(); - - if ( !YY_CURRENT_BUFFER ) { - yyterminate(); - } else { - BEGIN(incl); - } - - } - -%% diff --git a/src/genjsbind-parser.y b/src/genjsbind-parser.y deleted file mode 100644 index cf9ab61..0000000 --- a/src/genjsbind-parser.y +++ /dev/null @@ -1,228 +0,0 @@ -%{ -/* parser for the binding generation config file - * - * This file is part of nsgenjsbind. - * Licensed under the MIT License, - * http://www.opensource.org/licenses/mit-license.php - * Copyright 2012 Vincent Sanders - */ - -#include -#include - -#include "genjsbind-parser.h" -#include "genjsbind-lexer.h" -#include "webidl-ast.h" -#include "genjsbind-ast.h" - -char *errtxt; - - static void genjsbind_error(YYLTYPE *locp, struct genbind_node **genbind_ast, const char *str) -{ - locp = locp; - genbind_ast = genbind_ast; - errtxt = strdup(str); -} - - -%} - -%locations -%define api.pure -%error-verbose -%parse-param { struct genbind_node **genbind_ast } - -%union -{ - char* text; - struct genbind_node *node; -} - -%token TOK_IDLFILE -%token TOK_HDR_COMMENT -%token TOK_PREAMBLE - -%token TOK_BINDING -%token TOK_OPERATION -%token TOK_INTERFACE -%token TOK_TYPE -%token TOK_PRIVATE - -%token TOK_IDENTIFIER -%token TOK_STRING_LITERAL -%token TOK_CCODE_LITERAL - -%type CBlock - -%type Statement -%type Statements -%type IdlFile -%type Preamble -%type HdrComment -%type Strings -%type Binding -%type BindingArgs -%type BindingArg -%type Type -%type Private -%type Interface -%type Operation - -%% - -Input - : - Statements - { - *genbind_ast = $1; - } - ; - - -Statements - : - Statement - | - Statements Statement - { - $$ = genbind_node_link($2, $1); - } - | - error ';' - { - fprintf(stderr, "%d: %s\n", yylloc.first_line, errtxt); - free(errtxt); - YYABORT ; - } - ; - -Statement - : - IdlFile - | - HdrComment - | - Preamble - | - Binding - | - Operation - ; - - /* [3] load a web IDL file */ -IdlFile - : - TOK_IDLFILE TOK_STRING_LITERAL ';' - { - $$ = genbind_new_node(GENBIND_NODE_TYPE_WEBIDLFILE, NULL, $2); - } - ; - -HdrComment - : - TOK_HDR_COMMENT Strings ';' - { - $$ = genbind_new_node(GENBIND_NODE_TYPE_HDRCOMMENT, NULL, $2); - } - ; - -Strings - : - TOK_STRING_LITERAL - { - $$ = genbind_new_node(GENBIND_NODE_TYPE_STRING, NULL, $1); - } - | - Strings TOK_STRING_LITERAL - { - $$ = genbind_new_node(GENBIND_NODE_TYPE_STRING, $1, $2); - } - ; - -Preamble - : - TOK_PREAMBLE CBlock - { - $$ = genbind_new_node(GENBIND_NODE_TYPE_PREAMBLE, NULL, $2); - } - ; - -CBlock - : - TOK_CCODE_LITERAL - | - CBlock TOK_CCODE_LITERAL - { - $$ = genbind_strapp($1, $2); - } - ; - -Operation - : - TOK_OPERATION TOK_IDENTIFIER CBlock - { - $$ = genbind_new_node(GENBIND_NODE_TYPE_OPERATION, - NULL, - genbind_new_node(GENBIND_NODE_TYPE_IDENT, - genbind_new_node(GENBIND_NODE_TYPE_CBLOCK, - NULL, - $3), - $2)); - } - -Binding - : - TOK_BINDING TOK_IDENTIFIER '{' BindingArgs '}' - { - $$ = genbind_new_node(GENBIND_NODE_TYPE_BINDING, - NULL, - genbind_new_node(GENBIND_NODE_TYPE_IDENT, $4, $2)); - } - ; - -BindingArgs - : - BindingArg - | - BindingArgs BindingArg - { - $$ = genbind_node_link($2, $1); - } - ; - -BindingArg - : - Type - | - Private - | - Interface - ; - -Type - : - TOK_TYPE TOK_IDENTIFIER ';' - { - $$ = genbind_new_node(GENBIND_NODE_TYPE_BINDING_TYPE, NULL, $2); - } - ; - -Private - : - TOK_PRIVATE TOK_STRING_LITERAL TOK_IDENTIFIER ';' - { - $$ = genbind_new_node(GENBIND_NODE_TYPE_BINDING_PRIVATE, NULL, - genbind_new_node(GENBIND_NODE_TYPE_IDENT, - genbind_new_node(GENBIND_NODE_TYPE_STRING, NULL, $2), $3)); - } - ; - -Interface - : - TOK_INTERFACE TOK_IDENTIFIER ';' - { - $$ = genbind_new_node(GENBIND_NODE_TYPE_BINDING_INTERFACE, NULL, $2); - } - ; - -%% diff --git a/src/genjsbind.c b/src/genjsbind.c deleted file mode 100644 index bcaf62f..0000000 --- a/src/genjsbind.c +++ /dev/null @@ -1,108 +0,0 @@ -/* binding generator main and command line parsing - * - * This file is part of nsgenjsbind. - * Licensed under the MIT License, - * http://www.opensource.org/licenses/mit-license.php - * Copyright 2012 Vincent Sanders - */ - -#include -#include -#include -#include -#include -#include -#include - -#include "genjsbind-ast.h" -#include "jsapi-libdom.h" -#include "options.h" - -struct options *options; - -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; - } - - 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) -{ - int res; - struct genbind_node *genbind_root; - - options = process_cmdline(argc, argv); - if (options == NULL) { - return 1; /* bad commandline */ - } - - if (options->verbose && - (options->outfilename == NULL)) { - fprintf(stderr, - "Error: output to stdout with verbose logging would fail\n"); - return 2; - } - - res = genbind_parsefile(options->infilename, &genbind_root); - if (res != 0) { - fprintf(stderr, "Error: parse failed with code %d\n", res); - return res; - } - - if (options->verbose) { - genbind_ast_dump(genbind_root, 0); - } - - res = jsapi_libdom_output(options->outfilename, genbind_root); - if (res != 0) { - fprintf(stderr, "Error: output failed with code %d\n", res); - unlink(options->outfilename); - return res; - } - - return 0; -} diff --git a/src/jsapi-libdom-operator.c b/src/jsapi-libdom-operator.c index 97369a3..0ff79c0 100644 --- a/src/jsapi-libdom-operator.c +++ b/src/jsapi-libdom-operator.c @@ -1,6 +1,6 @@ /* operator body generation * - * This file is part of nsgenjsbind. + * This file is part of nsgenbind. * Licensed under the MIT License, * http://www.opensource.org/licenses/mit-license.php * Copyright 2012 Vincent Sanders @@ -13,7 +13,7 @@ #include #include "options.h" -#include "genjsbind-ast.h" +#include "nsgenbind-ast.h" #include "webidl-ast.h" #include "jsapi-libdom.h" diff --git a/src/jsapi-libdom.c b/src/jsapi-libdom.c index fdcebe7..1dad62a 100644 --- a/src/jsapi-libdom.c +++ b/src/jsapi-libdom.c @@ -1,6 +1,6 @@ /* binding output generator for jsapi(spidermonkey) to libdom * - * This file is part of nsgenjsbind. + * This file is part of nsgenbind. * Licensed under the MIT License, * http://www.opensource.org/licenses/mit-license.php * Copyright 2012 Vincent Sanders @@ -13,12 +13,12 @@ #include #include "options.h" -#include "genjsbind-ast.h" +#include "nsgenbind-ast.h" #include "webidl-ast.h" #include "jsapi-libdom.h" #define HDR_COMMENT_SEP "\n * " -#define HDR_COMMENT_PREABLE "Generated by nsgenjsapi" +#define HDR_COMMENT_PREABLE "Generated by nsgenbind " static int webidl_file_cb(struct genbind_node *node, void *ctx) diff --git a/src/jsapi-libdom.h b/src/jsapi-libdom.h index e199ec7..67d4238 100644 --- a/src/jsapi-libdom.h +++ b/src/jsapi-libdom.h @@ -1,13 +1,13 @@ /* binding generator output * - * This file is part of nsgenjsbind. + * This file is part of nsgenbind. * Licensed under the MIT License, * http://www.opensource.org/licenses/mit-license.php * Copyright 2012 Vincent Sanders */ -#ifndef genjsbind_jsapi_libdom_h -#define genjsbind_jsapi_libdom_h +#ifndef nsgenbind_jsapi_libdom_h +#define nsgenbind_jsapi_libdom_h struct binding { struct genbind_node *gb_ast; diff --git a/src/nsgenbind-ast.c b/src/nsgenbind-ast.c new file mode 100644 index 0000000..646548b --- /dev/null +++ b/src/nsgenbind-ast.c @@ -0,0 +1,363 @@ +/* binding generator AST implementation for parser + * + * This file is part of nsgenbind. + * Licensed under the MIT License, + * http://www.opensource.org/licenses/mit-license.php + * Copyright 2012 Vincent Sanders + */ + +/** @todo this currently stuffs everything in one global tree, not very nice + */ + +#include +#include +#include +#include +#include + +#include "nsgenbind-ast.h" +#include "options.h" + +/* parser and lexer interface */ +extern int nsgenbind_debug; +extern int nsgenbind__flex_debug; +extern void nsgenbind_restart(FILE*); +extern int nsgenbind_parse(struct genbind_node **genbind_ast); + +/* terminal nodes have a value only */ +struct genbind_node { + enum genbind_node_type type; + struct genbind_node *l; + union { + void *value; + struct genbind_node *node; + char *text; + } r; +}; + + +char *genbind_strapp(char *a, char *b) +{ + char *fullstr; + int fulllen; + fulllen = strlen(a) + strlen(b) + 1; + fullstr = malloc(fulllen); + snprintf(fullstr, fulllen, "%s%s", a, b); + free(a); + free(b); + return fullstr; +} + +struct genbind_node *genbind_node_link(struct genbind_node *tgt, struct genbind_node *src) +{ + tgt->l = src; + return tgt; +} + + +struct genbind_node * +genbind_new_node(enum genbind_node_type type, struct genbind_node *l, void *r) +{ + struct genbind_node *nn; + nn = calloc(1, sizeof(struct genbind_node)); + nn->type = type; + nn->l = l; + nn->r.value = r; + return nn; +} + +int +genbind_node_for_each_type(struct genbind_node *node, + enum genbind_node_type type, + genbind_callback_t *cb, + void *ctx) +{ + int ret; + + if (node == NULL) { + return -1; + } + if (node->l != NULL) { + ret = genbind_node_for_each_type(node->l, type, cb, ctx); + if (ret != 0) { + return ret; + } + } + if (node->type == type) { + return cb(node, ctx); + } + + return 0; +} + + +/* exported interface defined in nsgenbind-ast.h */ +struct genbind_node * +genbind_node_find(struct genbind_node *node, + struct genbind_node *prev, + genbind_callback_t *cb, + void *ctx) +{ + struct genbind_node *ret; + + if (node == NULL) { + return NULL; + } + + if (node->l != prev) { + ret = genbind_node_find(node->l, prev, cb, ctx); + if (ret != NULL) { + return ret; + } + } + + if (cb(node, ctx) != 0) { + return node; + } + + return NULL; +} + +/* exported interface defined in nsgenbind-ast.h */ +struct genbind_node * +genbind_node_find_type(struct genbind_node *node, + struct genbind_node *prev, + enum genbind_node_type type) +{ + return genbind_node_find(node, + prev, + genbind_cmp_node_type, + (void *)type); +} + +struct genbind_node * +genbind_node_find_type_ident(struct genbind_node *node, + struct genbind_node *prev, + enum genbind_node_type type, + const char *ident) +{ + struct genbind_node *found_node; + struct genbind_node *ident_node; + + found_node = genbind_node_find(node, + prev, + genbind_cmp_node_type, + (void *)type); + + while (found_node != NULL) { + + ident_node = genbind_node_find(genbind_node_getnode(found_node), + NULL, + genbind_cmp_node_type, + (void *)GENBIND_NODE_TYPE_IDENT); + if (ident_node != NULL) { + if (strcmp(ident_node->r.text, ident) == 0) + break; + } + + /* look for next matching node */ + found_node = genbind_node_find(node, + found_node, + genbind_cmp_node_type, + (void *)type); + + } + return found_node; +} + +int genbind_cmp_node_type(struct genbind_node *node, void *ctx) +{ + if (node->type == (enum genbind_node_type)ctx) + return 1; + return 0; +} + +char *genbind_node_gettext(struct genbind_node *node) +{ + switch(node->type) { + case GENBIND_NODE_TYPE_WEBIDLFILE: + case GENBIND_NODE_TYPE_STRING: + case GENBIND_NODE_TYPE_PREAMBLE: + case GENBIND_NODE_TYPE_IDENT: + case GENBIND_NODE_TYPE_BINDING_TYPE: + case GENBIND_NODE_TYPE_BINDING_INTERFACE: + case GENBIND_NODE_TYPE_CBLOCK: + return node->r.text; + + default: + return NULL; + } +} + +struct genbind_node *genbind_node_getnode(struct genbind_node *node) +{ + switch(node->type) { + case GENBIND_NODE_TYPE_HDRCOMMENT: + case GENBIND_NODE_TYPE_BINDING: + case GENBIND_NODE_TYPE_BINDING_PRIVATE: + case GENBIND_NODE_TYPE_OPERATION: + return node->r.node; + + default: + return NULL; + } +} + +static const char *genbind_node_type_to_str(enum genbind_node_type type) +{ + switch(type) { + case GENBIND_NODE_TYPE_IDENT: + return "Ident"; + + case GENBIND_NODE_TYPE_ROOT: + return "Root"; + + case GENBIND_NODE_TYPE_WEBIDLFILE: + return "webidlfile"; + + case GENBIND_NODE_TYPE_HDRCOMMENT: + return "HdrComment"; + + case GENBIND_NODE_TYPE_STRING: + return "String"; + + case GENBIND_NODE_TYPE_PREAMBLE: + return "Preamble"; + + case GENBIND_NODE_TYPE_BINDING: + return "Binding"; + + case GENBIND_NODE_TYPE_BINDING_TYPE: + return "Type"; + + case GENBIND_NODE_TYPE_BINDING_PRIVATE: + return "Private"; + + case GENBIND_NODE_TYPE_BINDING_INTERFACE: + return "Interface"; + + case GENBIND_NODE_TYPE_OPERATION: + return "Operation"; + + case GENBIND_NODE_TYPE_CBLOCK: + return "CBlock"; + + default: + return "Unknown"; + } +} + +int genbind_ast_dump(struct genbind_node *node, int indent) +{ + const char *SPACES=" "; + char *txt; + + while (node != NULL) { + printf("%.*s%s", indent, SPACES, genbind_node_type_to_str(node->type)); + + txt = genbind_node_gettext(node); + if (txt == NULL) { + printf("\n"); + genbind_ast_dump(genbind_node_getnode(node), indent + 2); + } else { + printf(": \"%.*s\"\n", 75 - indent, txt); + } + node = node->l; + } + return 0; +} + +FILE *genbindopen(const char *filename) +{ + FILE *genfile; + char *fullname; + int fulllen; + static char *prevfilepath = NULL; + + /* try filename raw */ + genfile = fopen(filename, "r"); + if (genfile != NULL) { + if (options->verbose) { + printf("Opened Genbind file %s\n", filename); + } + if (prevfilepath == NULL) { + fullname = strrchr(filename, '/'); + if (fullname == NULL) { + fulllen = strlen(filename); + } else { + fulllen = fullname - filename; + } + prevfilepath = strndup(filename,fulllen); + } + return genfile; + } + + /* try based on previous filename */ + if (prevfilepath != NULL) { + fulllen = strlen(prevfilepath) + strlen(filename) + 2; + fullname = malloc(fulllen); + snprintf(fullname, fulllen, "%s/%s", prevfilepath, filename); + if (options->debug) { + printf("Attempting to open Genbind file %s\n", fullname); + } + genfile = fopen(fullname, "r"); + if (genfile != NULL) { + if (options->verbose) { + printf("Opened Genbind file %s\n", fullname); + } + free(fullname); + return genfile; + } + free(fullname); + } + + /* try on idl path */ + if (options->idlpath != NULL) { + fulllen = strlen(options->idlpath) + strlen(filename) + 2; + fullname = malloc(fulllen); + snprintf(fullname, fulllen, "%s/%s", options->idlpath, filename); + genfile = fopen(fullname, "r"); + if ((genfile != NULL) && options->verbose) { + printf("Opend Genbind file %s\n", fullname); + } + + free(fullname); + } + + return genfile; +} + +int genbind_parsefile(char *infilename, struct genbind_node **ast) +{ + FILE *infile; + + /* open input file */ + if ((infilename[0] == '-') && + (infilename[1] == 0)) { + if (options->verbose) { + printf("Using stdin for input\n"); + } + infile = stdin; + } else { + infile = genbindopen(infilename); + } + + if (!infile) { + fprintf(stderr, "Error opening %s: %s\n", + infilename, + strerror(errno)); + return 3; + } + + if (options->debug) { + nsgenbind_debug = 1; + nsgenbind__flex_debug = 1; + } + + /* set flex to read from file */ + nsgenbind_restart(infile); + + /* process binding */ + return nsgenbind_parse(ast); + +} diff --git a/src/nsgenbind-ast.h b/src/nsgenbind-ast.h new file mode 100644 index 0000000..b51e6b8 --- /dev/null +++ b/src/nsgenbind-ast.h @@ -0,0 +1,78 @@ +/* binding file AST interface + * + * This file is part of nsnsgenbind. + * Licensed under the MIT License, + * http://www.opensource.org/licenses/mit-license.php + * Copyright 2012 Vincent Sanders + */ + +#ifndef nsgenbind_nsgenbind_ast_h +#define nsgenbind_nsgenbind_ast_h + +enum genbind_node_type { + GENBIND_NODE_TYPE_ROOT = 0, + GENBIND_NODE_TYPE_IDENT, + GENBIND_NODE_TYPE_CBLOCK, + GENBIND_NODE_TYPE_WEBIDLFILE, + GENBIND_NODE_TYPE_HDRCOMMENT, + GENBIND_NODE_TYPE_STRING, + GENBIND_NODE_TYPE_PREAMBLE, + GENBIND_NODE_TYPE_BINDING, + GENBIND_NODE_TYPE_BINDING_TYPE, + GENBIND_NODE_TYPE_BINDING_PRIVATE, + GENBIND_NODE_TYPE_BINDING_INTERFACE, + GENBIND_NODE_TYPE_OPERATION, +}; + + +struct genbind_node; + +/** callback for search and iteration routines */ +typedef int (genbind_callback_t)(struct genbind_node *node, void *ctx); + +int genbind_cmp_node_type(struct genbind_node *node, void *ctx); + +FILE *genbindopen(const char *filename); + +int genbind_parsefile(char *infilename, struct genbind_node **ast); + +char *genbind_strapp(char *a, char *b); + +struct genbind_node *genbind_new_node(enum genbind_node_type type, struct genbind_node *l, void *r); +struct genbind_node *genbind_node_link(struct genbind_node *tgt, struct genbind_node *src); + +int genbind_ast_dump(struct genbind_node *ast, int indent); + +/** Depth first left hand search using user provided comparison + * + * @param node The node to start the search from + + * @param prev The node at which to stop the search, either NULL to + * search the full tree depth (initial search) or the result + * of a previous search to continue. + * @param cb Comparison callback + * @param ctx Context for callback + */ +struct genbind_node * +genbind_node_find(struct genbind_node *node, + struct genbind_node *prev, + genbind_callback_t *cb, + void *ctx); + +struct genbind_node * +genbind_node_find_type(struct genbind_node *node, + struct genbind_node *prev, + enum genbind_node_type type); + +struct genbind_node * +genbind_node_find_type_ident(struct genbind_node *node, + struct genbind_node *prev, + enum genbind_node_type type, + const char *ident); + +int genbind_node_for_each_type(struct genbind_node *node, enum genbind_node_type type, genbind_callback_t *cb, void *ctx); + +char *genbind_node_gettext(struct genbind_node *node); +struct genbind_node *genbind_node_getnode(struct genbind_node *node); + +#endif diff --git a/src/nsgenbind-lexer.l b/src/nsgenbind-lexer.l new file mode 100644 index 0000000..4bdc803 --- /dev/null +++ b/src/nsgenbind-lexer.l @@ -0,0 +1,154 @@ +%{ + +/* lexer for the binding generation config file + * + * This file is part of nsgenbind. + * Licensed under the MIT License, + * http://www.opensource.org/licenses/mit-license.php + * Copyright 2012 Vincent Sanders + */ + +#include +#include +#include + +#include "nsgenbind-parser.h" +#include "nsgenbind-ast.h" + +#define YY_USER_ACTION yylloc->first_line = yylloc->last_line; \ + yylloc->first_column = yylloc->last_column + 1; \ + yylloc->last_column += yyleng; + +%} + +/* lexer options */ +%option never-interactive +%option yylineno +%option bison-bridge +%option bison-locations +%option nodefault +%option warn +%option prefix="nsgenbind_" +%option nounput +%option noinput +%option noyywrap + +/* other Unicode “space separator” */ +USP (\xe1\x9a\x80)|(\xe1\xa0\x8e)|(\xe2\x80[\x80-\x8a])|(\xe2\x80\xaf)|(\xe2\x81\x9f)|(\xe3\x80\x80) + +/* non breaking space \u00A0 */ +NBSP (\xc2\xa0) + +/* Line separator \u2028 */ +LS (\xe2\x80\xa8) + +/* paragraph separator \u2029 */ +PS (\xe2\x80\xa9) + +whitespace ([ \t\v\f]|{NBSP}|{USP}) + +lineend ([\n\r]|{LS}|{PS}) + +multicomment \/\*(([^*])|(\*[^/]))*\*\/ + +quotedstring [^\"\\\n\r] + +identifier [A-Z_a-z][0-9A-Z_a-z]* + +other [^\t\n\r 0-9A-Z_a-z] + +cblockopen \%\{ + +cblockclose \%\} + +/* used for #include directive */ +poundsign ^{whitespace}*# + +%x cblock + +%x incl + +%% + +{whitespace} ++yylloc->last_column;/* nothing */ + +{lineend} if (yytext[0] != '\r') { + /* update position counts */ + ++yylloc->last_line; + yylloc->last_column = 0; + } + + /* terminals */ + +webidlfile return TOK_IDLFILE; + +hdrcomment return TOK_HDR_COMMENT; + +preamble return TOK_PREAMBLE; + +binding return TOK_BINDING; + +interface return TOK_INTERFACE; + +type return TOK_TYPE; + +private return TOK_PRIVATE; + +operation return TOK_OPERATION; + +{cblockopen} BEGIN(cblock); + +{identifier} { + /* A leading "_" is used to escape an identifier from + * looking like a reserved word terminal. + */ + yylval->text = (yytext[0] == '_') ? strdup(yytext + 1) : strdup(yytext); + return TOK_IDENTIFIER; + } + +\"{quotedstring}*\" yylval->text = strndup(yytext + 1, yyleng - 2 ); return TOK_STRING_LITERAL; + +{multicomment} /* nothing */ + +{poundsign}include BEGIN(incl); + +{other} return (int) yytext[0]; + +. /* nothing */ + +[^\%]* yylval->text = strdup(yytext); return TOK_CCODE_LITERAL; +{cblockclose} BEGIN(INITIAL); +\% yylval->text = strdup(yytext); return TOK_CCODE_LITERAL; + + +[ \t]*\" /* eat the whitespace and open quotes */ + +[^\t\n\"]+ { + /* got the include file name */ + yyin = genbindopen(yytext); + + if (! yyin) { + fprintf(stderr, "Unable to open include %s\n", yytext); + exit(3); + } + yypush_buffer_state(yy_create_buffer( yyin, YY_BUF_SIZE )); + + BEGIN(INITIAL); + } + +\n BEGIN(INITIAL); + +. /* nothing */ + +<> { + yypop_buffer_state(); + + if ( !YY_CURRENT_BUFFER ) { + yyterminate(); + } else { + BEGIN(incl); + } + + } + +%% diff --git a/src/nsgenbind-parser.y b/src/nsgenbind-parser.y new file mode 100644 index 0000000..500f4b8 --- /dev/null +++ b/src/nsgenbind-parser.y @@ -0,0 +1,228 @@ +%{ +/* parser for the binding generation config file + * + * This file is part of nsgenbind. + * Licensed under the MIT License, + * http://www.opensource.org/licenses/mit-license.php + * Copyright 2012 Vincent Sanders + */ + +#include +#include + +#include "nsgenbind-parser.h" +#include "nsgenbind-lexer.h" +#include "webidl-ast.h" +#include "nsgenbind-ast.h" + +char *errtxt; + + static void nsgenbind_error(YYLTYPE *locp, struct genbind_node **genbind_ast, const char *str) +{ + locp = locp; + genbind_ast = genbind_ast; + errtxt = strdup(str); +} + + +%} + +%locations +%define api.pure +%error-verbose +%parse-param { struct genbind_node **genbind_ast } + +%union +{ + char* text; + struct genbind_node *node; +} + +%token TOK_IDLFILE +%token TOK_HDR_COMMENT +%token TOK_PREAMBLE + +%token TOK_BINDING +%token TOK_OPERATION +%token TOK_INTERFACE +%token TOK_TYPE +%token TOK_PRIVATE + +%token TOK_IDENTIFIER +%token TOK_STRING_LITERAL +%token TOK_CCODE_LITERAL + +%type CBlock + +%type Statement +%type Statements +%type IdlFile +%type Preamble +%type HdrComment +%type Strings +%type Binding +%type BindingArgs +%type BindingArg +%type Type +%type Private +%type Interface +%type Operation + +%% + +Input + : + Statements + { + *genbind_ast = $1; + } + ; + + +Statements + : + Statement + | + Statements Statement + { + $$ = genbind_node_link($2, $1); + } + | + error ';' + { + fprintf(stderr, "%d: %s\n", yylloc.first_line, errtxt); + free(errtxt); + YYABORT ; + } + ; + +Statement + : + IdlFile + | + HdrComment + | + Preamble + | + Binding + | + Operation + ; + + /* [3] load a web IDL file */ +IdlFile + : + TOK_IDLFILE TOK_STRING_LITERAL ';' + { + $$ = genbind_new_node(GENBIND_NODE_TYPE_WEBIDLFILE, NULL, $2); + } + ; + +HdrComment + : + TOK_HDR_COMMENT Strings ';' + { + $$ = genbind_new_node(GENBIND_NODE_TYPE_HDRCOMMENT, NULL, $2); + } + ; + +Strings + : + TOK_STRING_LITERAL + { + $$ = genbind_new_node(GENBIND_NODE_TYPE_STRING, NULL, $1); + } + | + Strings TOK_STRING_LITERAL + { + $$ = genbind_new_node(GENBIND_NODE_TYPE_STRING, $1, $2); + } + ; + +Preamble + : + TOK_PREAMBLE CBlock + { + $$ = genbind_new_node(GENBIND_NODE_TYPE_PREAMBLE, NULL, $2); + } + ; + +CBlock + : + TOK_CCODE_LITERAL + | + CBlock TOK_CCODE_LITERAL + { + $$ = genbind_strapp($1, $2); + } + ; + +Operation + : + TOK_OPERATION TOK_IDENTIFIER CBlock + { + $$ = genbind_new_node(GENBIND_NODE_TYPE_OPERATION, + NULL, + genbind_new_node(GENBIND_NODE_TYPE_IDENT, + genbind_new_node(GENBIND_NODE_TYPE_CBLOCK, + NULL, + $3), + $2)); + } + +Binding + : + TOK_BINDING TOK_IDENTIFIER '{' BindingArgs '}' + { + $$ = genbind_new_node(GENBIND_NODE_TYPE_BINDING, + NULL, + genbind_new_node(GENBIND_NODE_TYPE_IDENT, $4, $2)); + } + ; + +BindingArgs + : + BindingArg + | + BindingArgs BindingArg + { + $$ = genbind_node_link($2, $1); + } + ; + +BindingArg + : + Type + | + Private + | + Interface + ; + +Type + : + TOK_TYPE TOK_IDENTIFIER ';' + { + $$ = genbind_new_node(GENBIND_NODE_TYPE_BINDING_TYPE, NULL, $2); + } + ; + +Private + : + TOK_PRIVATE TOK_STRING_LITERAL TOK_IDENTIFIER ';' + { + $$ = genbind_new_node(GENBIND_NODE_TYPE_BINDING_PRIVATE, NULL, + genbind_new_node(GENBIND_NODE_TYPE_IDENT, + genbind_new_node(GENBIND_NODE_TYPE_STRING, NULL, $2), $3)); + } + ; + +Interface + : + TOK_INTERFACE TOK_IDENTIFIER ';' + { + $$ = genbind_new_node(GENBIND_NODE_TYPE_BINDING_INTERFACE, NULL, $2); + } + ; + +%% diff --git a/src/nsgenbind.c b/src/nsgenbind.c new file mode 100644 index 0000000..14854ad --- /dev/null +++ b/src/nsgenbind.c @@ -0,0 +1,108 @@ +/* binding generator main and command line parsing + * + * This file is part of nsgenbind. + * Licensed under the MIT License, + * http://www.opensource.org/licenses/mit-license.php + * Copyright 2012 Vincent Sanders + */ + +#include +#include +#include +#include +#include +#include +#include + +#include "nsgenbind-ast.h" +#include "jsapi-libdom.h" +#include "options.h" + +struct options *options; + +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; + } + + 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) +{ + int res; + struct genbind_node *genbind_root; + + options = process_cmdline(argc, argv); + if (options == NULL) { + return 1; /* bad commandline */ + } + + if (options->verbose && + (options->outfilename == NULL)) { + fprintf(stderr, + "Error: output to stdout with verbose logging would fail\n"); + return 2; + } + + res = genbind_parsefile(options->infilename, &genbind_root); + if (res != 0) { + fprintf(stderr, "Error: parse failed with code %d\n", res); + return res; + } + + if (options->verbose) { + genbind_ast_dump(genbind_root, 0); + } + + res = jsapi_libdom_output(options->outfilename, genbind_root); + if (res != 0) { + fprintf(stderr, "Error: output failed with code %d\n", res); + unlink(options->outfilename); + return res; + } + + return 0; +} diff --git a/src/options.h b/src/options.h index 041f4cd..696a907 100644 --- a/src/options.h +++ b/src/options.h @@ -1,13 +1,13 @@ /* binding generator options * - * This file is part of nsgenjsbind. + * This file is part of nsgenbind. * Licensed under the MIT License, * http://www.opensource.org/licenses/mit-license.php * Copyright 2012 Vincent Sanders */ -#ifndef genjsbind_options_h -#define genjsbind_options_h +#ifndef nsgenbind_options_h +#define nsgenbind_options_h struct options { char *outfilename; diff --git a/src/webidl-ast.c b/src/webidl-ast.c index 7e6fc2c..fef5f35 100644 --- a/src/webidl-ast.c +++ b/src/webidl-ast.c @@ -1,6 +1,6 @@ /* AST generator for the WEB IDL parser * - * This file is part of nsgenjsbind. + * This file is part of nsgenbind. * Licensed under the MIT License, * http://www.opensource.org/licenses/mit-license.php * Copyright 2012 Vincent Sanders @@ -136,7 +136,7 @@ webidl_node_for_each_type(struct webidl_node *node, return 0; } -/* exported interface defined in genjsbind-ast.h */ +/* exported interface defined in webidl-ast.h */ int webidl_cmp_node_type(struct webidl_node *node, void *ctx) { if (node->type == (enum webidl_node_type)ctx) @@ -144,7 +144,7 @@ int webidl_cmp_node_type(struct webidl_node *node, void *ctx) return 0; } -/* exported interface defined in genjsbind-ast.h */ +/* exported interface defined in webidl-ast.h */ struct webidl_node * webidl_node_find(struct webidl_node *node, struct webidl_node *prev, @@ -172,7 +172,7 @@ webidl_node_find(struct webidl_node *node, } -/* exported interface defined in genjsbind-ast.h */ +/* exported interface defined in webidl-ast.h */ struct webidl_node * webidl_node_find_type(struct webidl_node *node, struct webidl_node *prev, diff --git a/src/webidl-ast.h b/src/webidl-ast.h index 8e14fa4..2117cfa 100644 --- a/src/webidl-ast.h +++ b/src/webidl-ast.h @@ -1,13 +1,13 @@ /* Web IDL AST interface * - * This file is part of nsgenjsbind. + * This file is part of nsgenbind. * Licensed under the MIT License, * http://www.opensource.org/licenses/mit-license.php * Copyright 2012 Vincent Sanders */ -#ifndef genjsbind_webidl_ast_h -#define genjsbind_webidl_ast_h +#ifndef nsgenbind_webidl_ast_h +#define nsgenbind_webidl_ast_h enum webidl_node_type { /* generic node types which define structure or attributes */ diff --git a/src/webidl-lexer.l b/src/webidl-lexer.l index bfa4fb5..5369d99 100644 --- a/src/webidl-lexer.l +++ b/src/webidl-lexer.l @@ -2,7 +2,7 @@ /* This is a unicode tolerant lexer for web IDL * - * This file is part of nsgenjsbind. + * This file is part of nsgenbind. * Licensed under the MIT License, * http://www.opensource.org/licenses/mit-license.php * Copyright 2012 Vincent Sanders diff --git a/src/webidl-parser.y b/src/webidl-parser.y index 74af1c1..2fe549b 100644 --- a/src/webidl-parser.y +++ b/src/webidl-parser.y @@ -2,7 +2,7 @@ /* This is a bison parser for Web IDL * - * This file is part of nsgenjsbind. + * This file is part of nsgenbind. * Licensed under the MIT License, * http://www.opensource.org/licenses/mit-license.php * Copyright 2012 Vincent Sanders -- cgit v1.2.3