3 Compiler for parsing Flash2 AVM2 ABC Actionscript
5 Extension module for the rfxswf library.
6 Part of the swftools package.
8 Copyright (c) 2008/2009 Matthias Kramm <kramm@quiss.org>
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.
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.
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 */
24 #include "tokenizer.h"
27 #include "parser.tab.h"
34 /* flex/bison definitions */
35 extern int a3_parse();
37 extern int as3_lex_destroy();
39 void as3_setverbosity(int level)
43 void as3_add_include_dir(char*dir)
48 static char registry_initialized = 0;
49 static char parser_initialized = 0;
51 //#define STORE_TOKENS
62 int token = as3_lex();
63 /* FIXME: current_file needs to be stored, too */
64 mem_put(&tokens, &token, sizeof(token));
65 mem_put(&tokens, &a3_lval, sizeof(a3_lval));
66 mem_put(&tokens, ¤t_line, sizeof(current_line));
67 mem_put(&tokens, ¤t_column, sizeof(current_column));
71 mem_get(&tokens, &token, sizeof(token));
72 mem_get(&tokens, &a3_lval, sizeof(a3_lval));
73 mem_get(&tokens, ¤t_line, sizeof(current_line));
74 mem_get(&tokens, ¤t_column, sizeof(current_column));
82 static void as3_parse_file_or_array(int pass, const char*name, const char*filename, void*mem, int length)
84 if(!registry_initialized) {
85 registry_initialized = 1;
88 if(!parser_initialized) {
89 parser_initialized = 1;
91 #if defined(STORE_TOKENS)
99 //printf("[pass %d] parse file %s %s\n", pass, name, filename);
100 fi = enter_file2(name, filename, 0);
103 //printf("[pass %d] parse bytearray %s (%d bytes)\n", pass, name, length);
104 enter_file(name, name, 0);
105 as3_buffer_input(mem, length);
109 initialize_file(name, filename);
115 typedef struct _scheduled_file {
118 struct _scheduled_file*next;
121 static scheduled_file_t*scheduled=0;
122 dict_t*scheduled_dict=0;
124 void as3_parse_scheduled(int pass)
126 scheduled_file_t*s = scheduled;
129 scheduled_file_t*old = s;
130 as3_parse_file_or_array(pass, s->name, s->filename, 0,0);
135 old->filename = old->name = 0;
139 dict_destroy(scheduled_dict);
143 void as3_schedule_file(const char*name, const char*filename)
146 scheduled_dict = dict_new();
148 //printf("schedule %s %s\n", name, filename);
150 if(dict_contains(scheduled_dict, filename)) {
151 return; //already processed
153 dict_put(scheduled_dict, filename, 0);
156 NEW(scheduled_file_t, f);
157 f->name = strdup(name);
158 f->filename = strdup(filename);
159 f->next = scheduled; // dfs
163 void as3_parse_bytearray(const char*name, void*mem, int length)
165 as3_parse_file_or_array(1, name, 0, mem, length);
166 as3_parse_scheduled(1);
168 as3_parse_file_or_array(2, name, 0, mem, length);
169 as3_parse_scheduled(2);
172 void as3_parse_file(const char*filename)
174 char*fullfilename = find_file(filename);
178 as3_parse_file_or_array(1, filename, fullfilename, 0,0);
179 as3_parse_scheduled(1);
181 as3_parse_file_or_array(2, filename, fullfilename, 0,0);
182 as3_parse_scheduled(2);
187 void as3_schedule_package(const char*package)
189 char*dirname = strdup(package);
192 if(package[s]=='.') dirname[s]='/';
197 include_dir_t*i = current_include_dirs;
199 char*fulldirname = concatPaths(i->path, dirname);
200 DIR*dir = opendir(dirname);
208 char*name = ent->d_name;
214 if(strncasecmp(&name[l-3], ".as", 3))
216 char*fullfilename = concatPaths(dirname, name);
217 as3_schedule_file(name, fullfilename);
226 as3_softwarning("Could not find package %s in file system", package);
229 void as3_schedule_class(const char*package, const char*cls)
232 as3_schedule_package(package);
235 int l1 = package?strlen(package):0;
236 int l2 = cls?strlen(cls):0;
237 char*filename = malloc(l1+l2+5);
243 filename[t++] = package[s];
249 strcpy(filename+t, cls);
250 strcpy(filename+t+l2, ".as");
252 if(!(f=find_file(filename))) {
254 /* try lower case filename (not packagename!), too */
255 for(i=t;i<t+l2;i++) {
256 if(filename[i]>='A' && filename[i]<='Z')
257 filename[i] += 'a'-'A';
259 if(!(f=find_file(filename))) {
260 strcpy(filename+t, cls);
261 strcpy(filename+t+l2, ".as");
262 as3_warning("Could not open file %s", filename);
266 as3_schedule_file(filename, f);
269 static void*as3code = 0;
272 if(parser_initialized) {
273 parser_initialized = 0;
274 as3code = finish_parser();
278 char* as3_getglobalclass()
280 return as3_globalclass;
285 if(parser_initialized) {
286 parser_initialized = 0;
287 swf_FreeABC(finish_parser());
292 if(as3_globalclass) {
293 free(as3_globalclass);as3_globalclass=0;