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
68 int token = as3_lex();
69 /* FIXME: current_file needs to be stored, too */
70 mem_put(&tokens, &token, sizeof(token));
71 mem_put(&tokens, &a3_lval, sizeof(a3_lval));
72 mem_put(&tokens, ¤t_line, sizeof(current_line));
73 mem_put(&tokens, ¤t_column, sizeof(current_column));
77 mem_get(&tokens, &token, sizeof(token));
78 mem_get(&tokens, &a3_lval, sizeof(a3_lval));
79 mem_get(&tokens, ¤t_line, sizeof(current_line));
80 mem_get(&tokens, ¤t_column, sizeof(current_column));
88 static void as3_parse_file_or_array(int pass, const char*name, const char*filename, void*mem, int length)
90 if(!registry_initialized) {
91 registry_initialized = 1;
94 if(!parser_initialized) {
95 parser_initialized = 1;
97 #if defined(STORE_TOKENS)
105 DEBUG printf("[pass %d] parse file %s %s\n", pass, name, filename);
106 fi = enter_file2(name, filename, 0);
109 DEBUG printf("[pass %d] parse bytearray %s (%d bytes)\n", pass, name, length);
110 enter_file(name, name, 0);
111 as3_buffer_input(mem, length);
115 initialize_file(name, filename);
121 typedef struct _scheduled_file {
124 struct _scheduled_file*next;
127 static scheduled_file_t*scheduled=0;
128 dict_t*scheduled_dict=0;
130 void as3_parse_scheduled(int pass)
132 DEBUG printf("[pass %d] parse scheduled\n", pass);
135 scheduled_file_t*s = scheduled;
138 scheduled_file_t*old = s;
139 as3_parse_file_or_array(pass, s->name, s->filename, 0,0);
144 old->filename = old->name = 0;
149 dict_destroy(scheduled_dict);
154 void as3_schedule_file(const char*name, const char*filename)
156 if(!scheduled_dict) {
157 scheduled_dict = dict_new();
160 filename = normalize_path(filename);
162 if(dict_contains(scheduled_dict, filename)) {
163 return; //already processed
165 dict_put(scheduled_dict, filename, 0);
167 DEBUG printf("[pass %d] schedule %s %s\n", as3_pass, name, filename);
169 NEW(scheduled_file_t, f);
170 f->name = strdup(name);
171 f->filename = strdup(filename);
172 f->next = scheduled; // dfs
176 void as3_parse_bytearray(const char*name, void*mem, int length)
178 as3_parse_file_or_array(1, name, 0, mem, length);
179 as3_parse_scheduled(1);
181 as3_parse_file_or_array(2, name, 0, mem, length);
182 as3_parse_scheduled(2);
185 void as3_parse_file(const char*filename)
187 char*fullfilename = find_file(filename);
191 as3_parse_file_or_array(1, filename, fullfilename, 0,0);
192 as3_parse_scheduled(1);
194 as3_parse_file_or_array(2, filename, fullfilename, 0,0);
195 as3_parse_scheduled(2);
200 void as3_parse_directory(const char*dir)
203 as3_schedule_directory(dir);
205 as3_warning("Directory %s doesn't contain any ActionScript files", dir);
206 as3_parse_scheduled(1);
208 as3_schedule_directory(dir);
209 as3_parse_scheduled(2);
212 char as3_schedule_directory(const char*dirname)
214 DEBUG printf("[pass %d] schedule directory %s\n", as3_pass, dirname);
217 include_dir_t*i = current_include_dirs;
219 char*fulldirname = concat_paths(i->path, dirname);
220 DEBUG printf("[pass %d] ... %s\n", as3_pass, fulldirname);
221 DIR*dir = opendir(fulldirname);
229 char*name = ent->d_name;
235 if(strncasecmp(&name[l-3], ".as", 3))
237 char*fullfilename = concatPaths(fulldirname, name);
238 as3_schedule_file(name, fullfilename);
249 void as3_schedule_package(const char*package)
251 DEBUG printf("[pass %d] schedule package %s\n", as3_pass, package);
252 char*dirname = strdup(package);
255 if(dirname[s]=='.') dirname[s]='/';
258 if(!as3_schedule_directory(package))
259 as3_softwarning("Could not find package %s in file system", package);
262 void as3_schedule_class(const char*package, const char*cls)
264 DEBUG printf("[pass %d] schedule class %s.%s\n", as3_pass, package, cls);
266 as3_schedule_package(package);
269 int l1 = package?strlen(package):0;
270 int l2 = cls?strlen(cls):0;
271 char*filename = malloc(l1+l2+5);
277 filename[t++] = package[s];
283 strcpy(filename+t, cls);
284 strcpy(filename+t+l2, ".as");
286 if(!(f=find_file(filename))) {
288 /* try lower case filename (not packagename!), too */
289 for(i=t;i<t+l2;i++) {
290 if(filename[i]>='A' && filename[i]<='Z')
291 filename[i] += 'a'-'A';
293 if(!(f=find_file(filename))) {
294 strcpy(filename+t, cls);
295 strcpy(filename+t+l2, ".as");
296 as3_warning("Could not open file %s", filename);
300 as3_schedule_file(filename, f);
303 static void*as3code = 0;
306 if(parser_initialized) {
307 parser_initialized = 0;
308 as3code = finish_parser();
312 char* as3_getglobalclass()
314 return as3_globalclass;
319 if(parser_initialized) {
320 parser_initialized = 0;
321 swf_FreeABC(finish_parser());
326 if(as3_globalclass) {
327 free(as3_globalclass);as3_globalclass=0;