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 fi = enter_file2(name, filename, 0);
102 enter_file(name, name, 0);
103 as3_buffer_input(mem, length);
107 initialize_file(name);
113 typedef struct _scheduled_file {
115 struct _scheduled_file*next;
118 static scheduled_file_t*scheduled=0;
119 dict_t*scheduled_dict=0;
121 void as3_parse_scheduled()
124 scheduled_file_t*old = scheduled;
125 as3_parse_file(scheduled->filename);
126 scheduled = scheduled->next;
131 dict_destroy(scheduled_dict);
135 void as3_schedule_file(const char*filename)
138 scheduled_dict = dict_new();
140 if(dict_lookup(scheduled_dict, filename)) {
141 return; //already processed
143 dict_put(scheduled_dict, filename, 0);
146 NEW(scheduled_file_t, f);
147 f->filename = strdup(filename);
148 f->next = scheduled; // dfs
150 printf("schedule %s\n", filename);
153 void as3_parse_bytearray(const char*name, void*mem, int length)
155 as3_parse_file_or_array(1, name, 0, mem, length);
156 as3_parse_scheduled();
158 as3_parse_file_or_array(2, name, 0, mem, length);
159 as3_parse_scheduled();
162 void as3_parse_file(const char*filename)
164 char*fullfilename = find_file(filename);
168 as3_parse_file_or_array(1, filename, fullfilename, 0,0);
169 as3_parse_scheduled();
171 as3_parse_file_or_array(2, filename, fullfilename, 0,0);
172 as3_parse_scheduled();
177 void as3_schedule_package(const char*package)
179 char*dirname = strdup(package);
182 if(package[s]=='.') dirname[s]='/';
187 include_dir_t*i = current_include_dirs;
189 char*fulldirname = concatPaths(i->path, dirname);
190 DIR*dir = opendir(dirname);
198 char*name = ent->d_name;
204 if(strncasecmp(&name[l-3], ".as", 3))
206 char*fullfilename = concatPaths(dirname, name);
207 as3_schedule_file(fullfilename);
216 as3_warning("Could not find package %s", package);
219 void as3_schedule_class(const char*package, const char*cls)
222 as3_schedule_package(package);
225 int l1 = package?strlen(package):0;
226 int l2 = cls?strlen(cls):0;
227 char*filename = malloc(l1+l2+5);
233 filename[t++] = package[s];
239 strcpy(filename+t, cls);
240 strcpy(filename+t+l2, ".as");
242 if(!(f=find_file(filename))) {
244 /* try lower case filename (not packagename!), too */
245 for(i=t;i<t+l2;i++) {
246 if(filename[i]>='A' && filename[i]<='Z')
247 filename[i] += 'a'-'A';
249 if(!(f=find_file(filename))) {
250 strcpy(filename+t, cls);
251 strcpy(filename+t+l2, ".as");
252 as3_warning("Could not open file %s", filename);
256 as3_schedule_file(f);
259 static void*as3code = 0;
262 if(parser_initialized) {
263 parser_initialized = 0;
264 as3code = finish_parser();
268 char* as3_getglobalclass()
270 return as3_globalclass;
275 if(parser_initialized) {
276 parser_initialized = 0;
277 swf_FreeABC(finish_parser());
282 if(as3_globalclass) {
283 free(as3_globalclass);as3_globalclass=0;