new function as3_parse_bytearray
[swftools.git] / lib / as3 / compiler.c
index af418cb..19816b1 100644 (file)
 #include "parser.h"
 #include "parser.tab.h"
 #include "compiler.h"
-#include <errno.h>
 
 /* flex/bison definitions */
-extern void avm2_set_in (FILE *  in_str );
-extern int avm2_parse();
-extern int avm2_lex_destroy();
+extern int a3_parse();
+extern int as3_lex();
+extern int as3_lex_destroy();
 
 void as3_setverbosity(int level)
 {
@@ -45,7 +44,38 @@ void as3_add_include_dir(char*dir)
 static char registry_initialized = 0;
 static char parser_initialized = 0;
 
-void as3_parse_file(char*filename) 
+//#define STORE_TOKENS
+
+#ifdef STORE_TOKENS
+static mem_t tokens;
+#endif
+
+int a3_lex()
+{
+    as3_tokencount++;
+#ifdef STORE_TOKENS
+    if(as3_pass==1) {
+        int token = as3_lex();
+        /* FIXME: current_file needs to be stored, too */
+        mem_put(&tokens, &token, sizeof(token));
+        mem_put(&tokens, &a3_lval, sizeof(a3_lval));
+        mem_put(&tokens, &current_line, sizeof(current_line));
+        mem_put(&tokens, &current_column, sizeof(current_column));
+        return token;
+    } else {
+        int token;
+        mem_get(&tokens, &token, sizeof(token));
+        mem_get(&tokens, &a3_lval, sizeof(a3_lval));
+        mem_get(&tokens, &current_line, sizeof(current_line));
+        mem_get(&tokens, &current_column, sizeof(current_column));
+        return token;
+    }
+#else
+    return as3_lex();
+#endif
+}
+
+static void as3_parse(const char*name, const char*filename, void*mem, int length)
 {
     if(!registry_initialized) {
         registry_initialized = 1;
@@ -54,19 +84,70 @@ void as3_parse_file(char*filename)
     if(!parser_initialized) {
         parser_initialized = 1;
         initialize_parser();
+#if defined(STORE_TOKENS)
+        mem_init(&tokens);
+#endif
     }
 
-    char*fullfilename = enter_file(filename, 0);
-    FILE*fi = fopen(fullfilename, "rb");
-    if(!fi) {
-       syntaxerror("Couldn't find file %s: %s", fullfilename, strerror(errno));
+#ifdef STORE_TOKENS
+    tokens.pos = 0;
+    tokens.read_pos = 0;
+#endif
+
+    FILE*fi = 0;
+    if(filename) {
+        fi = enter_file2(name, filename, 0);
+        as3_file_input(fi);
+    } else {
+        enter_file(name, name, 0);
+        as3_buffer_input(mem, length);
+    }
+
+    /* pass 1 */
+    as3_pass = 1;
+    as3_tokencount=0;
+    initialize_file(name);
+    a3_parse();
+    as3_lex_destroy();
+    finish_file();
+    
+#ifdef STORE_TOKENS
+    tokens.read_pos = 0;
+#endif
+
+    if(filename) {
+        fclose(fi);
+        fi = enter_file2(name, filename, 0);
+        as3_file_input(fi);
+    } else {
+        enter_file(name, name, 0);
+        as3_buffer_input(mem, length);
     }
-    avm2_set_in(fi);
-    initialize_file(filename);
-    avm2_parse();
-    avm2_lex_destroy();
-    fclose(fi);
+
+    /* pass 2 */
+    as3_pass = 2;
+    as3_tokencount=0;
+    initialize_file(name);
+    a3_parse();
+    as3_lex_destroy();
     finish_file();
+
+    if(filename) {
+        fclose(fi);
+    }
+}
+
+void as3_parse_bytearray(const char*name, void*mem, int length)
+{
+    as3_parse(name, 0, mem, length);
+}
+
+void as3_parse_file(const char*filename) 
+{
+    char*fullfilename = find_file(filename);
+    if(!fullfilename)
+        return; // not found
+    as3_parse(filename, fullfilename, 0,0);
 }
 
 static void*as3code = 0;
@@ -88,6 +169,9 @@ void as3_destroy()
     if(parser_initialized) {
         parser_initialized = 0;
         swf_FreeABC(finish_parser());
+#ifdef STORE_TOKENS
+        mem_clear(&tokens);
+#endif
     }
     if(as3_globalclass) {
         free(as3_globalclass);as3_globalclass=0;