flex and bison now have different prefixes
[swftools.git] / lib / as3 / compiler.c
1 /* compiler.h
2
3    Compiler for parsing Flash2 AVM2 ABC Actionscript
4
5    Extension module for the rfxswf library.
6    Part of the swftools package.
7
8    Copyright (c) 2008/2009 Matthias Kramm <kramm@quiss.org>
9  
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 2 of the License, or
13    (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
23
24 #include "tokenizer.h"
25 #include "files.h"
26 #include "parser.h"
27 #include "parser.tab.h"
28 #include "compiler.h"
29 #include <errno.h>
30
31 /* flex/bison definitions */
32 extern void as3_set_in (FILE *  in_str );
33 extern int a3_parse();
34 extern int as3_lex();
35 extern int as3_lex_destroy();
36
37 void as3_setverbosity(int level)
38 {
39     as3_verbosity=level;
40 }
41 void as3_add_include_dir(char*dir)
42 {
43     add_include_dir(dir);
44 }
45
46 static char registry_initialized = 0;
47 static char parser_initialized = 0;
48
49 void a3_lex()
50 {
51     as3_lex();
52 }
53
54 void as3_parse_file(char*filename) 
55 {
56     if(!registry_initialized) {
57         registry_initialized = 1;
58         registry_init();
59     }
60     if(!parser_initialized) {
61         parser_initialized = 1;
62         initialize_parser();
63     }
64
65     as3_pass = 0;
66     
67     char*fullfilename = enter_file(filename, 0);
68     FILE*fi = fopen(fullfilename, "rb");
69     if(!fi) {
70         syntaxerror("Couldn't find file %s: %s", fullfilename, strerror(errno));
71     }
72     /* pass 1 */
73     as3_pass = 1;
74     as3_set_in(fi);
75     initialize_file(filename);
76     a3_parse();
77     as3_lex_destroy();
78     finish_file();
79
80     /* pass 2 */
81     // TODO: this should re-use tokens!
82     enter_file(filename, 0);
83     as3_pass = 2;
84     fseek(fi, 0, SEEK_SET);
85     as3_set_in(fi);
86     initialize_file(filename);
87     a3_parse();
88     as3_lex_destroy();
89     finish_file();
90
91     fclose(fi);
92
93 }
94
95 static void*as3code = 0;
96 void* as3_getcode()
97 {
98     if(parser_initialized) {
99         parser_initialized = 0;
100         as3code = finish_parser();
101     }
102     return as3code;
103 }
104 char* as3_getglobalclass()
105 {
106     return as3_globalclass;
107 }
108
109 void as3_destroy() 
110 {
111     if(parser_initialized) {
112         parser_initialized = 0;
113         swf_FreeABC(finish_parser());
114     }
115     if(as3_globalclass) {
116         free(as3_globalclass);as3_globalclass=0;
117     }
118 }
119