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 */
25 #include "tokenizer.h"
28 #include "parser.tab.h"
31 #ifdef HAVE_SYS_STAT_H
38 /* flex/bison definitions */
39 extern int a3_parse();
41 extern int as3_lex_destroy();
43 static char config_recurse = 0;
45 void as3_setverbosity(int level)
49 void as3_add_include_dir(char*dir)
53 void as3_set_option(const char*key, const char*value)
55 if(!strcmp(key, "recurse")) {
56 config_recurse=atoi(value);
60 static char registry_initialized = 0;
61 static char parser_initialized = 0;
63 //#define STORE_TOKENS
74 typedef struct _compile_list {
77 struct _compile_list*next;
79 static compile_list_t*compile_list=0;
81 static void as3_parse_file_or_array(const char*name, const char*filename, const void*mem, int length)
83 if(!registry_initialized) {
84 registry_initialized = 1;
87 if(!parser_initialized) {
88 parser_initialized = 1;
94 if(as3_pass==1 && !mem) {
95 // record the fact that we compiled this file
96 compile_list_t*c = rfx_calloc(sizeof(compile_list_t));
97 c->next = compile_list;
98 c->name = strdup(name);
99 c->filename = strdup(filename);
102 DEBUG printf("[pass %d] parse file %s %s\n", as3_pass, name, filename);
103 fi = enter_file2(name, filename, 0);
106 DEBUG printf("[pass %d] parse bytearray %s (%d bytes)\n", as3_pass, name, length);
107 enter_file(name, name, 0);
108 as3_buffer_input((void*)mem, length);
112 initialize_file(name, filename);
118 typedef struct _scheduled_file {
121 struct _scheduled_file*next;
124 static scheduled_file_t*scheduled=0;
125 dict_t*scheduled_dict=0;
127 void as3_parse_scheduled()
129 DEBUG printf("[pass %d] parse scheduled\n", as3_pass);
132 scheduled_file_t*s = scheduled;
135 scheduled_file_t*old = s;
136 as3_parse_file_or_array(s->name, s->filename, 0,0);
141 old->filename = old->name = 0;
146 dict_destroy(scheduled_dict);
151 void as3_schedule_file(const char*name, const char*filename)
153 if(!scheduled_dict) {
154 scheduled_dict = dict_new();
157 filename = normalize_path(filename);
159 if(dict_contains(scheduled_dict, filename)) {
160 return; //already processed
162 dict_put(scheduled_dict, filename, 0);
164 DEBUG printf("[pass %d] schedule %s %s\n", as3_pass, name, filename);
166 NEW(scheduled_file_t, f);
167 f->name = strdup(name);
168 f->filename = strdup(filename);
169 f->next = scheduled; // dfs
173 void as3_parse_list()
175 while(compile_list) {
176 as3_parse_file_or_array(compile_list->name, compile_list->filename, 0,0);
177 compile_list = compile_list->next;
181 void as3_parse_bytearray(const char*name, const void*mem, int length)
184 as3_parse_file_or_array(name, 0, mem, length);
185 as3_parse_scheduled();
187 registry_resolve_all();
190 as3_parse_file_or_array(name, 0, mem, length);
194 void as3_parse_file(const char*filename)
196 char*fullfilename = find_file(filename, 1);
202 as3_parse_file_or_array(filename, fullfilename, 0,0);
203 as3_parse_scheduled();
205 registry_resolve_all();
213 void as3_parse_directory(const char*dir)
218 as3_schedule_directory(dir);
220 as3_warning("Directory %s doesn't contain any ActionScript files", dir);
221 as3_parse_scheduled();
223 registry_resolve_all();
229 char as3_schedule_directory(const char*dirname)
231 DEBUG printf("[pass %d] schedule directory %s\n", as3_pass, dirname);
234 include_dir_t*i = current_include_dirs;
236 char*fulldirname = concat_paths(i->path, dirname);
237 DEBUG printf("[pass %d] ... %s\n", as3_pass, fulldirname);
238 DIR*dir = opendir(fulldirname);
246 char*name = ent->d_name;
252 if(strncasecmp(&name[l-3], ".as", 3))
254 char*fullfilename = concatPaths(fulldirname, name);
255 as3_schedule_file(name, fullfilename);
266 void as3_schedule_package(const char*package)
268 DEBUG printf("[pass %d] schedule package %s\n", as3_pass, package);
269 char*dirname = strdup(package);
272 if(dirname[s]=='.') dirname[s]='/';
275 if(!as3_schedule_directory(dirname))
276 as3_softwarning("Could not find package %s in file system", package);
279 static void schedule_class(const char*package, const char*cls, char error)
282 DEBUG printf("[pass %d] schedule class %s.%s\n", as3_pass, package, cls);
285 as3_schedule_package(package);
288 int l1 = package?strlen(package):0;
289 int l2 = cls?strlen(cls):0;
290 char*filename = malloc(l1+l2+5);
296 filename[t++] = package[s];
302 strcpy(filename+t, cls);
303 strcpy(filename+t+l2, ".as");
305 if(!(f=find_file(filename, error))) {
307 /* try lower case filename (not packagename!), too */
308 for(i=t;i<t+l2;i++) {
309 if(filename[i]>='A' && filename[i]<='Z')
310 filename[i] += 'a'-'A';
312 if(!(f=find_file(filename, error))) {
314 strcpy(filename+t, cls);
315 strcpy(filename+t+l2, ".as");
316 as3_warning("Could not open file %s", filename);
321 as3_schedule_file(filename, f);
324 void as3_schedule_class(const char*package, const char*cls)
326 schedule_class(package, cls, 1);
329 void as3_schedule_class_noerror(const char*package, const char*cls)
332 schedule_class(package, cls, 0);
337 static void*as3code = 0;
340 if(parser_initialized) {
341 parser_initialized = 0;
342 as3code = finish_parser();
346 char* as3_getglobalclass()
348 return as3_globalclass;
353 if(parser_initialized) {
354 parser_initialized = 0;
355 swf_FreeABC(finish_parser());
360 if(as3_globalclass) {
361 free(as3_globalclass);as3_globalclass=0;