3 Routines for compiling Flash2 AVM2 ABC Actionscript
5 Extension module for the rfxswf library.
6 Part of the swftools package.
8 Copyright (c) 2008 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 */
31 #include "tokenizer.h"
34 static void countlines(char*text, int len) {
46 static int verbose = 1;
47 static void dbg(const char*format, ...)
54 va_start(arglist, format);
55 vsprintf(buf, format, arglist);
58 while(l && buf[l-1]=='\n') {
62 printf("(tokenizer) ");
67 void syntaxerror(const char*format, ...)
74 va_start(arglist, format);
75 vsprintf(buf, format, arglist);
77 fprintf(stderr, "%s:%d:%d: error: %s\n", current_filename, current_line, current_column, buf);
83 #ifndef YY_CURRENT_BUFFER
84 #define YY_CURRENT_BUFFER yy_current_buffer
87 void handleInclude(char*text, int len, char quotes)
91 char*p1 = strchr(text, '"');
92 char*p2 = strrchr(text, '"');
93 if(!p1 || !p2 || p1==p2) {
94 syntaxerror("Invalid include in line %d\n", current_line);
97 filename = strdup(p1+1);
101 while(!strchr(" \n\r\t", text[i1])) i1++;
103 while(strchr(" \n\r\t", text[i1])) i1++;
104 while(strchr(" \n\r\t", text[i2-1])) i2--;
105 if(i2!=len) text[i2]=0;
106 filename = strdup(&text[i1]);
109 char*fullfilename = enter_file(filename, YY_CURRENT_BUFFER);
110 yyin = fopen(fullfilename, "rb");
112 syntaxerror("Couldn't open include file \"%s\"\n", fullfilename);
115 yy_switch_to_buffer(yy_create_buffer( yyin, YY_BUF_SIZE ) );
116 //BEGIN(INITIAL); keep context
119 static void handleString(char*s, int len)
122 if(s[len-1]!='"') syntaxerror("String doesn't end with '\"'");
125 else if(s[0]=='\'') {
126 if(s[len-1]!='\'') syntaxerror("String doesn't end with '\"'");
129 else syntaxerror("String incorrectly terminated");
131 avm2_lval.string = s;
135 char start_of_expression;
137 static inline int m(int type)
139 char*s = malloc(yyleng+1);
140 memcpy(s, yytext, yyleng);
150 static char numberbuf[64];
151 static inline int handlenumber()
153 if(yyleng>sizeof(numberbuf)-1)
154 syntaxerror("decimal number overflow");
157 memcpy(s, yytext, yyleng);
162 for(t=0;t<yyleng;t++) {
165 syntaxerror("Invalid number");
167 } else if(!strchr("-0123456789", yytext[t])) {
168 syntaxerror("Invalid number");
172 avm2_lval.number_float = atof(s);
175 char l = (yytext[0]=='-');
177 char*max = l?"1073741824":"2147483647";
179 syntaxerror("integer overflow");
182 for(t=0;t<yyleng-l;t++) {
183 if(yytext[l+t]>max[t])
184 syntaxerror("integer overflow %s > %s", s+l,max);
185 else if(yytext[l+t]<max[t])
191 avm2_lval.number_int = v;
200 for(t=0;t<yyleng;t++) {
204 avm2_lval.number_uint = v;
214 void initialize_scanner();
215 #define YY_USER_INIT initialize_scanner();
217 #define c() {countlines(yytext, yyleng);}
224 NAME [a-zA-Z_][a-zA-Z0-9_\\]*
226 NUMBER -?[0-9]+(\.[0-9]*)?
228 STRING ["](\\[\x00-\xff]|[^\\"\n])*["]|['](\\[\x00-\xff]|[^\\'\n])*[']
230 MULTILINE_COMMENT [/][*]([*][^/]|[^*]|[\x00-\x31])*[*]+[/]
231 SINGLELINE_COMMENT \/\/[^\n]*\n
232 REGEXP [/]([^/\n]|\\[/])*[/][a-zA-Z]*
236 {SINGLELINE_COMMENT} {c(); /* single line comment */}
237 {MULTILINE_COMMENT} {c(); /* multi line comment */}
238 [/][*] {syntaxerror("syntax error: unterminated comment", yytext);}
240 ^include{S}+{STRING}{S}*/\n {c();handleInclude(yytext, yyleng, 1);}
241 ^include{S}+[^" \t\r\n][\x20-\xff]*{S}*/\n {c();handleInclude(yytext, yyleng, 0);}
242 {STRING} {c(); BEGIN(INITIAL);handleString(yytext, yyleng);return T_STRING;}
244 <BEGINNING,REGEXPOK>{
245 {REGEXP} {c(); BEGIN(INITIAL);return m(T_REGEXP);}
248 \xef\xbb\xbf {/* utf 8 bom */}
251 {NUMBER} {c(); BEGIN(INITIAL);return handlenumber();}
253 [!][=] {BEGIN(REGEXPOK);return m(T_NE);}
254 [=][=] {BEGIN(REGEXPOK);return m(T_EQEQ);}
255 [>][=] {return m(T_GE);}
256 [<][=] {return m(T_LE);}
257 [-][-] {BEGIN(INITIAL);return m(T_MINUSMINUS);}
258 [+][+] {BEGIN(INITIAL);return m(T_PLUSPLUS);}
259 \.\. {return m(T_DOTDOT);}
261 :: {return m(T_COLONCOLON);}
263 implements {return m(KW_IMPLEMENTS);}
264 interface {return m(KW_INTERFACE);}
265 namespace {return m(KW_NAMESPACE);}
266 protected {return m(KW_PROTECTED);}
267 override {return m(KW_OVERRIDE);}
268 internal {return m(KW_INTERNAL);}
269 function {return m(KW_FUNCTION);}
270 package {return m(KW_PACKAGE);}
271 private {return m(KW_PRIVATE);}
272 Boolean {return m(KW_BOOLEAN);}
273 dynamic {return m(KW_DYNAMIC);}
274 extends {return m(KW_EXTENDS);}
275 public {return m(KW_PUBLIC);}
276 native {return m(KW_NATIVE);}
277 static {return m(KW_STATIC);}
278 import {return m(KW_IMPORT);}
279 Number {return m(KW_NUMBER);}
280 class {return m(KW_CLASS);}
281 const {return m(KW_CONST);}
282 final {return m(KW_FINAL);}
283 false {return m(KW_FALSE);}
284 true {return m(KW_TRUE);}
285 uint {return m(KW_UINT);}
286 null {return m(KW_NULL);}
287 else {return m(KW_ELSE);}
288 use {return m(KW_USE);}
289 int {return m(KW_INT);}
290 new {return m(KW_NEW);}
291 get {return m(KW_GET);}
292 for {return m(KW_FOR);}
293 set {return m(KW_SET);}
294 var {return m(KW_VAR);}
295 is {return m(KW_IS) ;}
296 if {return m(KW_IF) ;}
297 as {return m(KW_AS);}
298 {NAME} {c();BEGIN(INITIAL);return m(T_IDENTIFIER);}
300 [+-\/*^~@$!%&\(=\[\]\{\}|?:;,.<>] {c();BEGIN(REGEXPOK);return m(yytext[0]);}
301 [\)\]] {c();BEGIN(INITIAL);return m(yytext[0]);}
303 . {char c1=yytext[0];
308 char c = buf[t]=input();
309 if(c=='\n' || c==EOF) {
314 if(c1>='0' && c1<='9')
315 syntaxerror("syntax error: %s (identifiers must not start with a digit)");
317 syntaxerror("syntax error: %s", buf);
323 void*b = leave_file();
326 yy_delete_buffer(YY_CURRENT_BUFFER);
329 yy_delete_buffer(YY_CURRENT_BUFFER);
330 yy_switch_to_buffer(b);
341 static char mbuf[256];
342 char*token2string(token_t*t)
345 if(nr==T_STRING) return "<string>";
346 else if(nr==T_INT) return "<int>";
347 else if(nr==T_UINT) return "<uint>";
348 else if(nr==T_FLOAT) return "<float>";
349 else if(nr==T_REGEXP) return "REGEXP";
350 else if(nr==T_EOF) return "***END***";
351 else if(nr==T_GE) return ">=";
352 else if(nr==T_LE) return "<=";
353 else if(nr==T_MINUSMINUS) return "--";
354 else if(nr==T_PLUSPLUS) return "++";
355 else if(nr==KW_IMPLEMENTS) return "implements";
356 else if(nr==KW_INTERFACE) return "interface";
357 else if(nr==KW_NAMESPACE) return "namespace";
358 else if(nr==KW_PROTECTED) return "protected";
359 else if(nr==KW_OVERRIDE) return "override";
360 else if(nr==KW_INTERNAL) return "internal";
361 else if(nr==KW_FUNCTION) return "function";
362 else if(nr==KW_PACKAGE) return "package";
363 else if(nr==KW_PRIVATE) return "private";
364 else if(nr==KW_BOOLEAN) return "Boolean";
365 else if(nr==KW_DYNAMIC) return "dynamic";
366 else if(nr==KW_EXTENDS) return "extends";
367 else if(nr==KW_PUBLIC) return "public";
368 else if(nr==KW_NATIVE) return "native";
369 else if(nr==KW_STATIC) return "static";
370 else if(nr==KW_IMPORT) return "import";
371 else if(nr==KW_NUMBER) return "number";
372 else if(nr==KW_CLASS) return "class";
373 else if(nr==KW_CONST) return "const";
374 else if(nr==KW_FINAL) return "final";
375 else if(nr==KW_FALSE) return "False";
376 else if(nr==KW_TRUE) return "True";
377 else if(nr==KW_UINT) return "uint";
378 else if(nr==KW_NULL) return "null";
379 else if(nr==KW_ELSE) return "else";
380 else if(nr==KW_USE) return "use";
381 else if(nr==KW_INT) return "int";
382 else if(nr==KW_NEW) return "new";
383 else if(nr==KW_GET) return "get";
384 else if(nr==KW_FOR) return "for";
385 else if(nr==KW_SET) return "set";
386 else if(nr==KW_VAR) return "var";
387 else if(nr==KW_IS) return "is";
388 else if(nr==KW_AS) return "as";
389 else if(nr==T_IDENTIFIER) {
390 if(strlen(t->text)>sizeof(mbuf)-1)
392 sprintf(mbuf, "ID(%s)", t->text);
395 sprintf(mbuf, "%d", nr);
400 void initialize_scanner()