From 2b722f1ccfecc75ed93651543c9ba22a849396a1 Mon Sep 17 00:00:00 2001 From: Vincent Sanders Date: Wed, 2 Jan 2013 21:47:13 +0000 Subject: extend binding DSL with prologue and epilogue stanzas --- doc/example.bnd | 22 ++++++++---- src/jsapi-libdom.c | 78 +++++++++++++++++++++++++++++++------------ src/nsgenbind-ast.c | 2 ++ src/nsgenbind-lexer.l | 4 +++ src/nsgenbind-parser.y | 28 ++++++++++++++-- test/data/bindings/window.bnd | 11 +++++- 6 files changed, 114 insertions(+), 31 deletions(-) diff --git a/doc/example.bnd b/doc/example.bnd index 6bd051c..690eace 100644 --- a/doc/example.bnd +++ b/doc/example.bnd @@ -11,12 +11,6 @@ * http://www.opensource.org/licenses/mit-license */ -/* additional binding fragments may be included - * Note: this is not preprocessed despite the #include name, the - * parser will simply switch input to the included file and carry on - * cosntructing the bindings Abstract Syntax Tree (AST) - */ -#include "dom.bnd" /* directive to read WebIDL file and add its contents to the webidl AST */ webidlfile "html.idl"; @@ -43,6 +37,22 @@ preamble %{ %} +/* code block emitted immediately before the binding function bodies */ +prologue %{ +%} + +/* code block emmitted at the end of the output */ +epilogue %{ +%} + +/* additional binding fragments may be included + * Note: this is not preprocessed despite the #include name, the + * parser will simply switch input to the included file and carry on + * cosntructing the bindings Abstract Syntax Tree (AST) + */ +#include "dom.bnd" + + /* this block describes the binding to be generated * * Depending on the type of binding being generated multiple blocks diff --git a/src/jsapi-libdom.c b/src/jsapi-libdom.c index 39225ea..2c1278d 100644 --- a/src/jsapi-libdom.c +++ b/src/jsapi-libdom.c @@ -69,6 +69,15 @@ static int webidl_preamble_cb(struct genbind_node *node, void *ctx) return 0; } +static int webidl_prologue_cb(struct genbind_node *node, void *ctx) +{ + struct binding *binding = ctx; + + fprintf(binding->outfile, "%s", genbind_node_gettext(node)); + + return 0; +} + static int webidl_epilogue_cb(struct genbind_node *node, void *ctx) { struct binding *binding = ctx; @@ -172,6 +181,46 @@ static int webidl_private_assign_cb(struct genbind_node *node, void *ctx) return 0; } +/* section output generators */ + +/** Output the epilogue right at the end of the generated program */ +static int +output_epilogue(struct binding *binding) +{ + genbind_node_for_each_type(binding->gb_ast, + GENBIND_NODE_TYPE_EPILOGUE, + webidl_epilogue_cb, + binding); + + fprintf(binding->outfile,"\n\n"); + + if (binding->hdrfile) { + binding->outfile = binding->hdrfile; + + fprintf(binding->outfile, + "\n\n#endif /* _%s_ */\n", + binding->hdrguard); + + binding->outfile = binding->srcfile; + } + + return 0; +} + +/** Output the prologue right before the generated function bodies */ +static int +output_prologue(struct binding *binding) +{ + genbind_node_for_each_type(binding->gb_ast, + GENBIND_NODE_TYPE_PROLOGUE, + webidl_prologue_cb, + binding); + + fprintf(binding->outfile,"\n\n"); + + return 0; +} + static int output_api_operations(struct binding *binding) @@ -623,28 +672,6 @@ output_preamble(struct binding *binding) return 0; } -static int -output_epilogue(struct binding *binding) -{ - genbind_node_for_each_type(binding->gb_ast, - GENBIND_NODE_TYPE_EPILOGUE, - webidl_epilogue_cb, - binding); - - fprintf(binding->outfile,"\n\n"); - - if (binding->hdrfile) { - binding->outfile = binding->hdrfile; - - fprintf(binding->outfile, - "\n\n#endif /* _%s_ */\n", - binding->hdrguard); - - binding->outfile = binding->srcfile; - } - - return 0; -} static int output_header_comments(struct binding *binding) @@ -843,6 +870,7 @@ jsapi_libdom_output(char *outfilename, return 40; } + /* start with comment block */ res = output_header_comments(binding); if (res) { return 50; @@ -868,6 +896,12 @@ jsapi_libdom_output(char *outfilename, return 85; } + /* user code outout just before function bodies emitted */ + res = output_prologue(binding); + if (res) { + return 89; + } + /* operator and atrtribute body generation */ res = output_operator_body(binding, binding->interface); diff --git a/src/nsgenbind-ast.c b/src/nsgenbind-ast.c index f5cdc78..6b7bf5d 100644 --- a/src/nsgenbind-ast.c +++ b/src/nsgenbind-ast.c @@ -207,6 +207,8 @@ char *genbind_node_gettext(struct genbind_node *node) case GENBIND_NODE_TYPE_WEBIDLFILE: case GENBIND_NODE_TYPE_STRING: case GENBIND_NODE_TYPE_PREAMBLE: + case GENBIND_NODE_TYPE_PROLOGUE: + case GENBIND_NODE_TYPE_EPILOGUE: case GENBIND_NODE_TYPE_IDENT: case GENBIND_NODE_TYPE_TYPE: case GENBIND_NODE_TYPE_BINDING_INTERFACE: diff --git a/src/nsgenbind-lexer.l b/src/nsgenbind-lexer.l index e1c7740..b257c83 100644 --- a/src/nsgenbind-lexer.l +++ b/src/nsgenbind-lexer.l @@ -86,6 +86,10 @@ hdrcomment return TOK_HDR_COMMENT; preamble return TOK_PREAMBLE; +prologue return TOK_PROLOGUE; + +epilogue return TOK_EPILOGUE; + binding return TOK_BINDING; interface return TOK_INTERFACE; diff --git a/src/nsgenbind-parser.y b/src/nsgenbind-parser.y index 984513e..472e655 100644 --- a/src/nsgenbind-parser.y +++ b/src/nsgenbind-parser.y @@ -42,6 +42,8 @@ char *errtxt; %token TOK_IDLFILE %token TOK_HDR_COMMENT %token TOK_PREAMBLE +%token TOK_PROLOGUE; +%token TOK_EPILOGUE; %token TOK_API %token TOK_BINDING @@ -69,6 +71,8 @@ char *errtxt; %type Statements %type IdlFile %type Preamble +%type Prologue +%type Epilogue %type HdrComment %type Strings %type Binding @@ -120,7 +124,11 @@ Statement HdrComment | Preamble - | + | + Prologue + | + Epilogue + | Binding | Operation @@ -164,12 +172,28 @@ Strings Preamble : - TOK_PREAMBLE CBlock + TOK_PREAMBLE CBlock { $$ = genbind_new_node(GENBIND_NODE_TYPE_PREAMBLE, NULL, $2); } ; +Prologue + : + TOK_PROLOGUE CBlock + { + $$ = genbind_new_node(GENBIND_NODE_TYPE_PROLOGUE, NULL, $2); + } + ; + +Epilogue + : + TOK_EPILOGUE CBlock + { + $$ = genbind_new_node(GENBIND_NODE_TYPE_EPILOGUE, NULL, $2); + } + ; + CBlock : TOK_CCODE_LITERAL diff --git a/test/data/bindings/window.bnd b/test/data/bindings/window.bnd index 50ffe51..e59f65a 100644 --- a/test/data/bindings/window.bnd +++ b/test/data/bindings/window.bnd @@ -1,6 +1,5 @@ /* binding to generate window */ -#include "dom.bnd" webidlfile "html.idl"; @@ -18,6 +17,16 @@ preamble %{ %} +prologue %{ +/* prologue comment */ +%} + +epilogue %{ +/* epilogue comment */ +%} + +#include "dom.bnd" + binding window { type js_libdom; /* the binding type */ -- cgit v1.2.3