diff options
Diffstat (limited to 'src/jsapi-binding.c')
-rw-r--r-- | src/jsapi-binding.c | 142 |
1 files changed, 108 insertions, 34 deletions
diff --git a/src/jsapi-binding.c b/src/jsapi-binding.c index 51a0df7..9f1bc7e 100644 --- a/src/jsapi-binding.c +++ b/src/jsapi-binding.c @@ -5,15 +5,114 @@ #include <string.h> #include "jsapi-binding.h" +#include "webidl-ast.h" +#include "options.h" -#define HDR_COMMENT_PREABLE "/* Generated by nsgenjsapi\n" +/* parser and lexer interface */ +extern int genjsbind_debug; +extern int genjsbind__flex_debug; +extern void genjsbind_restart(FILE*); +extern int genjsbind_parse(void); -static FILE *outfile = NULL; + +#define HDR_COMMENT_SEP "\n * " +#define HDR_COMMENT_SEP_LEN 4 +#define HDR_COMMENT_PREABLE "Generated by nsgenjsapi"HDR_COMMENT_SEP + +/* current state */ static char *hdr_comments = NULL; -static bool hdr_comments_output = false; +static char *preamble = NULL; +static char *ifname; + +int genjsbind_header_comment(char *comment) +{ + char *fullstr; + int fulllen; + fulllen = strlen(hdr_comments) + strlen(comment) + HDR_COMMENT_SEP_LEN + 1; + fullstr = malloc(fulllen); + snprintf(fullstr, fulllen, "%s"HDR_COMMENT_SEP"%s", hdr_comments , comment); + free(hdr_comments); + free(comment); + hdr_comments = fullstr; + + return 0; +} + +int genjsbind_preamble(char *ccode) +{ + char *fullstr; + int fulllen; + fulllen = strlen(preamble) + strlen(ccode) + 1; + fullstr = malloc(fulllen); + snprintf(fullstr, fulllen, "%s%s", preamble , ccode); + free(preamble); + free(ccode); + preamble = fullstr; + + return 0; +} + +int genjsbind_interface(char *interface) +{ + ifname = interface; + return 0; +} + +static void init_state(void) +{ + /* initialise root node */ + webidl_root = webidl_new_node(WEBIDL_NODE_TYPE_ROOT); + + /* set default comment header text */ + hdr_comments = strdup(HDR_COMMENT_PREABLE); + + preamble = strdup(""); +} + +int genjsbind_parsefile(char *infilename) +{ + FILE *infile; -int genjsbind_outputopen(char *outfilename) + /* open input file */ + if ((infilename[0] == '-') && + (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(infilename, "r"); + } + + if (!infile) { + fprintf(stderr, "Error opening %s: %s\n", + infilename, + strerror(errno)); + return 3; + } + + /* initialise internal state */ + init_state(); + + if (options->debug) { + genjsbind_debug = 1; + genjsbind__flex_debug = 1; + } + + /* set flex to read from file */ + genjsbind_restart(infile); + + /* process binding */ + return genjsbind_parse(); + +} + +int genjsbind_output(char *outfilename) { + FILE *outfile = NULL; /* open output file */ if (outfilename == NULL) { outfile = stdout; @@ -28,39 +127,14 @@ int genjsbind_outputopen(char *outfilename) return 4; } - hdr_comments = strdup(HDR_COMMENT_PREABLE); - return 0; -} + fprintf(outfile, "/* %s\n */\n\n", hdr_comments); -int genjsbind_outputclose() -{ - fclose(outfile); - return 0; -} + fprintf(outfile, "%s", preamble); -int genjsbind_header_comment(char *comment) -{ - char *fullstr; - int fulllen; - fulllen = strlen(hdr_comments) + strlen(comment) + 2; - fullstr = malloc(fulllen); - snprintf(fullstr, fulllen, "%s\n%s", hdr_comments , comment); - free(hdr_comments); - free(comment); - hdr_comments = fullstr; + fprintf(outfile, "/* interface %s */\n\n", ifname); - if (hdr_comments_output) { - fprintf(stderr, "Warning: adding header comments after output already started\n"); - } - return 0; -} + fclose(outfile); -int genjsbind_output_interface(const char *interface) -{ - if (!hdr_comments_output) { - fprintf(outfile, "%s\n*/\n\n", hdr_comments); - hdr_comments_output = true; - } - fprintf(outfile, "/* interface %s */\n\n", interface); return 0; } + |