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"
30 #ifdef HAVE_SYS_STAT_H
37 /* flex/bison definitions */
38 extern int a3_parse();
40 extern int as3_lex_destroy();
42 static char config_recurse = 0;
44 void as3_setverbosity(int level)
48 void as3_add_include_dir(char*dir)
52 void as3_set_option(char*key, char*value)
54 if(!strcmp(key, "recurse")) {
55 config_recurse=atoi(value);
59 static char registry_initialized = 0;
60 static char parser_initialized = 0;
62 //#define STORE_TOKENS
73 typedef struct _compile_list {
76 struct _compile_list*next;
78 static compile_list_t*compile_list=0;
80 static void as3_parse_file_or_array(const char*name, const char*filename, const void*mem, int length)
82 if(!registry_initialized) {
83 registry_initialized = 1;
86 if(!parser_initialized) {
87 parser_initialized = 1;
93 if(as3_pass==1 && !mem) {
94 // record the fact that we compiled this file
95 compile_list_t*c = rfx_calloc(sizeof(compile_list_t));
96 c->next = compile_list;
97 c->name = strdup(name);
98 c->filename = strdup(filename);
101 DEBUG printf("[pass %d] parse file %s %s\n", as3_pass, name, filename);
102 fi = enter_file2(name, filename, 0);
105 DEBUG printf("[pass %d] parse bytearray %s (%d bytes)\n", as3_pass, name, length);
106 enter_file(name, name, 0);
107 as3_buffer_input((void*)mem, length);
111 initialize_file(name, filename);
117 typedef struct _scheduled_file {
120 struct _scheduled_file*next;
123 static scheduled_file_t*scheduled=0;
124 dict_t*scheduled_dict=0;
126 void as3_parse_scheduled()
128 DEBUG printf("[pass %d] parse scheduled\n", as3_pass);
131 scheduled_file_t*s = scheduled;
134 scheduled_file_t*old = s;
135 as3_parse_file_or_array(s->name, s->filename, 0,0);
140 old->filename = old->name = 0;
145 dict_destroy(scheduled_dict);
150 void as3_schedule_file(const char*name, const char*filename)
152 if(!scheduled_dict) {
153 scheduled_dict = dict_new();
156 filename = normalize_path(filename);
158 if(dict_contains(scheduled_dict, filename)) {
159 return; //already processed
161 dict_put(scheduled_dict, filename, 0);
163 DEBUG printf("[pass %d] schedule %s %s\n", as3_pass, name, filename);
165 NEW(scheduled_file_t, f);
166 f->name = strdup(name);
167 f->filename = strdup(filename);
168 f->next = scheduled; // dfs
172 void as3_parse_list()
174 while(compile_list) {
175 as3_parse_file_or_array(compile_list->name, compile_list->filename, 0,0);
176 compile_list = compile_list->next;
180 void as3_parse_bytearray(const char*name, const void*mem, int length)
183 as3_parse_file_or_array(name, 0, mem, length);
184 as3_parse_scheduled();
186 registry_resolve_all();
189 as3_parse_file_or_array(name, 0, mem, length);
193 void as3_parse_file(const char*filename)
195 char*fullfilename = find_file(filename, 1);
201 as3_parse_file_or_array(filename, fullfilename, 0,0);
202 as3_parse_scheduled();
204 registry_resolve_all();
212 void as3_parse_directory(const char*dir)
217 as3_schedule_directory(dir);
219 as3_warning("Directory %s doesn't contain any ActionScript files", dir);
220 as3_parse_scheduled();
222 registry_resolve_all();
228 char as3_schedule_directory(const char*dirname)
230 DEBUG printf("[pass %d] schedule directory %s\n", as3_pass, dirname);
233 include_dir_t*i = current_include_dirs;
235 char*fulldirname = concat_paths(i->path, dirname);
236 DEBUG printf("[pass %d] ... %s\n", as3_pass, fulldirname);
237 DIR*dir = opendir(fulldirname);
245 char*name = ent->d_name;
251 if(strncasecmp(&name[l-3], ".as", 3))
253 char*fullfilename = concatPaths(fulldirname, name);
254 as3_schedule_file(name, fullfilename);
265 void as3_schedule_package(const char*package)
267 DEBUG printf("[pass %d] schedule package %s\n", as3_pass, package);
268 char*dirname = strdup(package);
271 if(dirname[s]=='.') dirname[s]='/';
274 if(!as3_schedule_directory(dirname))
275 as3_softwarning("Could not find package %s in file system", package);
278 static void schedule_class(const char*package, const char*cls, char error)
281 DEBUG printf("[pass %d] schedule class %s.%s\n", as3_pass, package, cls);
284 as3_schedule_package(package);
287 int l1 = package?strlen(package):0;
288 int l2 = cls?strlen(cls):0;
289 char*filename = malloc(l1+l2+5);
295 filename[t++] = package[s];
301 strcpy(filename+t, cls);
302 strcpy(filename+t+l2, ".as");
304 if(!(f=find_file(filename, error))) {
306 /* try lower case filename (not packagename!), too */
307 for(i=t;i<t+l2;i++) {
308 if(filename[i]>='A' && filename[i]<='Z')
309 filename[i] += 'a'-'A';
311 if(!(f=find_file(filename, error))) {
313 strcpy(filename+t, cls);
314 strcpy(filename+t+l2, ".as");
315 as3_warning("Could not open file %s", filename);
320 as3_schedule_file(filename, f);
323 void as3_schedule_class(const char*package, const char*cls)
325 schedule_class(package, cls, 1);
328 void as3_schedule_class_noerror(const char*package, const char*cls)
331 schedule_class(package, cls, 0);
336 static void*as3code = 0;
339 if(parser_initialized) {
340 parser_initialized = 0;
341 as3code = finish_parser();
345 char* as3_getglobalclass()
347 return as3_globalclass;
352 if(parser_initialized) {
353 parser_initialized = 0;
354 swf_FreeABC(finish_parser());
359 if(as3_globalclass) {
360 free(as3_globalclass);as3_globalclass=0;