summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVincent Sanders <vince@kyllikki.org>2012-10-21 12:56:50 +0100
committerVincent Sanders <vince@kyllikki.org>2012-10-21 12:56:50 +0100
commitd264e721abc7848ae4f82fbaae444245f0e1e2b9 (patch)
tree00255013bf0803c4badfa02313af5cdec4ff054d
parent903f328e2ea9405a0d351c828389cf0080670b6c (diff)
downloadnsgenbind-d264e721abc7848ae4f82fbaae444245f0e1e2b9.tar.gz
nsgenbind-d264e721abc7848ae4f82fbaae444245f0e1e2b9.tar.bz2
add includes in binding files to allow binding definitions to be split up
-rw-r--r--src/genjsbind-ast.c65
-rw-r--r--src/genjsbind-ast.h2
-rw-r--r--src/genjsbind-lexer.l41
-rw-r--r--src/webidl-ast.c24
-rw-r--r--test/data/bindings/document.bnd16
-rw-r--r--test/data/bindings/htmldocument.bnd22
6 files changed, 139 insertions, 31 deletions
diff --git a/src/genjsbind-ast.c b/src/genjsbind-ast.c
index 21be715..c3329cf 100644
--- a/src/genjsbind-ast.c
+++ b/src/genjsbind-ast.c
@@ -267,6 +267,66 @@ int genbind_ast_dump(struct genbind_node *node, int indent)
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;
@@ -279,10 +339,7 @@ int genbind_parsefile(char *infilename, struct genbind_node **ast)
}
infile = stdin;
} else {
- if (options->verbose) {
- printf("Opening binding file %s\n", options->infilename);
- }
- infile = fopen(infilename, "r");
+ infile = genbindopen(infilename);
}
if (!infile) {
diff --git a/src/genjsbind-ast.h b/src/genjsbind-ast.h
index 6be13f9..416fa7b 100644
--- a/src/genjsbind-ast.h
+++ b/src/genjsbind-ast.h
@@ -32,6 +32,8 @@ 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);
diff --git a/src/genjsbind-lexer.l b/src/genjsbind-lexer.l
index 2282c5e..3701252 100644
--- a/src/genjsbind-lexer.l
+++ b/src/genjsbind-lexer.l
@@ -13,6 +13,7 @@
#include <string.h>
#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; \
@@ -60,8 +61,13 @@ cblockopen \%\{
cblockclose \%\}
+/* used for #include directive */
+poundsign ^{whitespace}*#
+
%x cblock
+%x incl
+
%%
{whitespace} ++yylloc->last_column;/* nothing */
@@ -104,12 +110,45 @@ operation return TOK_OPERATION;
{multicomment} /* nothing */
+{poundsign}include BEGIN(incl);
+
{other} return (int) yytext[0];
. /* nothing */
<cblock>[^\%]* yylval->text = strdup(yytext); return TOK_CCODE_LITERAL;
<cblock>{cblockclose} BEGIN(INITIAL);
-<cblock>\% yylval->text = strdup(yytext); return TOK_CCODE_LITERAL;
+<cblock>\% yylval->text = strdup(yytext); return TOK_CCODE_LITERAL;
+
+
+<incl>[ \t]*\" /* eat the whitespace and open quotes */
+
+<incl>[^\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);
+ }
+
+<incl>\n BEGIN(INITIAL);
+
+<incl>. /* nothing */
+
+<<EOF>> {
+ yypop_buffer_state();
+
+ if ( !YY_CURRENT_BUFFER ) {
+ yyterminate();
+ } else {
+ BEGIN(incl);
+ }
+
+ }
%%
diff --git a/src/webidl-ast.c b/src/webidl-ast.c
index 75dbdeb..7e6fc2c 100644
--- a/src/webidl-ast.c
+++ b/src/webidl-ast.c
@@ -359,23 +359,25 @@ int webidl_ast_dump(struct webidl_node *node, int indent)
static FILE *idlopen(const char *filename)
{
FILE *idlfile;
+ char *fullname;
+ int fulllen;
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 fopen(filename, "r");
+ }
+
+ 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;
}
diff --git a/test/data/bindings/document.bnd b/test/data/bindings/document.bnd
new file mode 100644
index 0000000..54268e2
--- /dev/null
+++ b/test/data/bindings/document.bnd
@@ -0,0 +1,16 @@
+/* test binding for document - must be included */
+
+webidlfile "eventtarget.idl";
+webidlfile "node.idl";
+webidlfile "document.idl";
+
+operation getElementById %{
+ dom_string *elementId_dom;
+ dom_element *element;
+
+ dom_string_create((unsigned char*)elementId, elementId_len, &elementId_dom);
+
+ dom_document_get_element_by_id(private->node, elementId_dom, &element);
+
+ jsretval = OBJECT_TO_JSVAL(jsapi_new_element(cx, JS_GetGlobalObject(cx), private->htmlc, element));
+%}
diff --git a/test/data/bindings/htmldocument.bnd b/test/data/bindings/htmldocument.bnd
index 9b47433..310fc29 100644
--- a/test/data/bindings/htmldocument.bnd
+++ b/test/data/bindings/htmldocument.bnd
@@ -1,8 +1,7 @@
/* test binding to generate htmldocument */
-webidlfile "eventtarget.idl";
-webidlfile "node.idl";
-webidlfile "document.idl";
+#include "document.bnd"
+
webidlfile "htmldocument.idl";
hdrcomment "Part of NetSurf Project";
@@ -21,22 +20,15 @@ preamble %{
#include "javascript/jsapi.h"
-
%}
operation write %{
+ LOG(("content %p parser %p writing %s",
+ private->htmlc, private->htmlc->parser, text));
- if (!JS_ConvertArguments(cx, argc, JSAPI_ARGV(cx, vp), "S", &u16_txt)) {
- return JS_FALSE;
- }
-
- JSString_to_char(u16_txt, txt, length);
-
- LOG(("content %p parser %p writing %s",
- document->htmlc, document->htmlc->parser, txt));
- if (document->htmlc->parser != NULL) {
- dom_hubbub_parser_insert_chunk(document->htmlc->parser, (uint8_t *)txt, length);
- }
+ if (private->htmlc->parser != NULL) {
+ dom_hubbub_parser_insert_chunk(private->htmlc->parser, (uint8_t *)text, text_len);
+ }
%}
binding document {