summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVincent Sanders <vince@kyllikki.org>2013-01-02 21:47:13 +0000
committerVincent Sanders <vince@kyllikki.org>2013-01-02 21:47:13 +0000
commit2b722f1ccfecc75ed93651543c9ba22a849396a1 (patch)
tree23c681240e72b15aa65f418ce78b385c2d13f33a
parent3b19d1d1ad88ba63ae821b2aedd4005466ed52fa (diff)
downloadnsgenbind-2b722f1ccfecc75ed93651543c9ba22a849396a1.tar.gz
nsgenbind-2b722f1ccfecc75ed93651543c9ba22a849396a1.tar.bz2
extend binding DSL with prologue and epilogue stanzas
-rw-r--r--doc/example.bnd22
-rw-r--r--src/jsapi-libdom.c78
-rw-r--r--src/nsgenbind-ast.c2
-rw-r--r--src/nsgenbind-lexer.l4
-rw-r--r--src/nsgenbind-parser.y28
-rw-r--r--test/data/bindings/window.bnd11
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 <node> Statements
%type <node> IdlFile
%type <node> Preamble
+%type <node> Prologue
+%type <node> Epilogue
%type <node> HdrComment
%type <node> Strings
%type <node> 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 */