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 void as3_setverbosity(int level)
46 void as3_add_include_dir(char*dir)
51 static char registry_initialized = 0;
52 static char parser_initialized = 0;
54 //#define STORE_TOKENS
65 typedef struct _compile_list {
68 struct _compile_list*next;
70 static compile_list_t*compile_list=0;
72 static void as3_parse_file_or_array(const char*name, const char*filename, const void*mem, int length)
74 if(!registry_initialized) {
75 registry_initialized = 1;
78 if(!parser_initialized) {
79 parser_initialized = 1;
85 if(as3_pass==1 && !mem) {
86 // record the fact that we compiled this file
87 compile_list_t*c = rfx_calloc(sizeof(compile_list_t));
88 c->next = compile_list;
89 c->name = strdup(name);
90 c->filename = strdup(filename);
93 DEBUG printf("[pass %d] parse file %s %s\n", as3_pass, name, filename);
94 fi = enter_file2(name, filename, 0);
97 DEBUG printf("[pass %d] parse bytearray %s (%d bytes)\n", as3_pass, name, length);
98 enter_file(name, name, 0);
99 as3_buffer_input((void*)mem, length);
103 initialize_file(name, filename);
109 typedef struct _scheduled_file {
112 struct _scheduled_file*next;
115 static scheduled_file_t*scheduled=0;
116 dict_t*scheduled_dict=0;
118 void as3_parse_scheduled()
120 DEBUG printf("[pass %d] parse scheduled\n", as3_pass);
123 scheduled_file_t*s = scheduled;
126 scheduled_file_t*old = s;
127 as3_parse_file_or_array(s->name, s->filename, 0,0);
132 old->filename = old->name = 0;
137 dict_destroy(scheduled_dict);
142 void as3_schedule_file(const char*name, const char*filename)
144 if(!scheduled_dict) {
145 scheduled_dict = dict_new();
148 filename = normalize_path(filename);
150 if(dict_contains(scheduled_dict, filename)) {
151 return; //already processed
153 dict_put(scheduled_dict, filename, 0);
155 DEBUG printf("[pass %d] schedule %s %s\n", as3_pass, name, filename);
157 NEW(scheduled_file_t, f);
158 f->name = strdup(name);
159 f->filename = strdup(filename);
160 f->next = scheduled; // dfs
164 void as3_parse_list()
166 while(compile_list) {
167 as3_parse_file_or_array(compile_list->name, compile_list->filename, 0,0);
168 compile_list = compile_list->next;
172 void as3_parse_bytearray(const char*name, const void*mem, int length)
175 as3_parse_file_or_array(name, 0, mem, length);
176 as3_parse_scheduled();
178 registry_resolve_all();
181 as3_parse_file_or_array(name, 0, mem, length);
185 void as3_parse_file(const char*filename)
187 char*fullfilename = find_file(filename, 1);
193 as3_parse_file_or_array(filename, fullfilename, 0,0);
194 as3_parse_scheduled();
196 registry_resolve_all();
204 void as3_parse_directory(const char*dir)
209 as3_schedule_directory(dir);
211 as3_warning("Directory %s doesn't contain any ActionScript files", dir);
212 as3_parse_scheduled();
214 registry_resolve_all();
220 char as3_schedule_directory(const char*dirname)
222 DEBUG printf("[pass %d] schedule directory %s\n", as3_pass, dirname);
225 include_dir_t*i = current_include_dirs;
227 char*fulldirname = concat_paths(i->path, dirname);
228 DEBUG printf("[pass %d] ... %s\n", as3_pass, fulldirname);
229 DIR*dir = opendir(fulldirname);
237 char*name = ent->d_name;
243 if(strncasecmp(&name[l-3], ".as", 3))
245 char*fullfilename = concatPaths(fulldirname, name);
246 as3_schedule_file(name, fullfilename);
257 void as3_schedule_package(const char*package)
259 DEBUG printf("[pass %d] schedule package %s\n", as3_pass, package);
260 char*dirname = strdup(package);
263 if(dirname[s]=='.') dirname[s]='/';
266 if(!as3_schedule_directory(dirname))
267 as3_softwarning("Could not find package %s in file system", package);
270 static void schedule_class(const char*package, const char*cls, char error)
273 DEBUG printf("[pass %d] schedule class %s.%s\n", as3_pass, package, cls);
276 as3_schedule_package(package);
279 int l1 = package?strlen(package):0;
280 int l2 = cls?strlen(cls):0;
281 char*filename = malloc(l1+l2+5);
287 filename[t++] = package[s];
293 strcpy(filename+t, cls);
294 strcpy(filename+t+l2, ".as");
296 if(!(f=find_file(filename, error))) {
298 /* try lower case filename (not packagename!), too */
299 for(i=t;i<t+l2;i++) {
300 if(filename[i]>='A' && filename[i]<='Z')
301 filename[i] += 'a'-'A';
303 if(!(f=find_file(filename, error))) {
305 strcpy(filename+t, cls);
306 strcpy(filename+t+l2, ".as");
307 as3_warning("Could not open file %s", filename);
312 as3_schedule_file(filename, f);
315 void as3_schedule_class(const char*package, const char*cls)
317 schedule_class(package, cls, 1);
320 void as3_schedule_class_noerror(const char*package, const char*cls)
322 schedule_class(package, cls, 0);
326 static void*as3code = 0;
329 if(parser_initialized) {
330 parser_initialized = 0;
331 as3code = finish_parser();
335 char* as3_getglobalclass()
337 return as3_globalclass;
342 if(parser_initialized) {
343 parser_initialized = 0;
344 swf_FreeABC(finish_parser());
349 if(as3_globalclass) {
350 free(as3_globalclass);as3_globalclass=0;