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 int verbose = 1;
35 static void dbg(const char*format, ...)
42 va_start(arglist, format);
43 vsprintf(buf, format, arglist);
46 while(l && buf[l-1]=='\n') {
50 printf("(tokenizer) ");
55 void syntaxerror(const char*format, ...)
62 va_start(arglist, format);
63 vsprintf(buf, format, arglist);
65 fprintf(stderr, "%s:%d:%d: error: %s\n", current_filename_short, current_line, current_column, buf);
69 void warning(const char*format, ...)
77 va_start(arglist, format);
78 vsprintf(buf, format, arglist);
80 fprintf(stderr, "%s:%d:%d: warning: %s\n", current_filename_short, current_line, current_column, buf);
85 #ifndef YY_CURRENT_BUFFER
86 #define YY_CURRENT_BUFFER yy_current_buffer
89 void handleInclude(char*text, int len, char quotes)
93 char*p1 = strchr(text, '"');
94 char*p2 = strrchr(text, '"');
95 if(!p1 || !p2 || p1==p2) {
96 syntaxerror("Invalid include in line %d\n", current_line);
99 filename = strdup(p1+1);
103 while(!strchr(" \n\r\t", text[i1])) i1++;
105 while(strchr(" \n\r\t", text[i1])) i1++;
106 while(strchr(" \n\r\t", text[i2-1])) i2--;
107 if(i2!=len) text[i2]=0;
108 filename = strdup(&text[i1]);
111 char*fullfilename = enter_file(filename, YY_CURRENT_BUFFER);
112 yyin = fopen(fullfilename, "rb");
114 syntaxerror("Couldn't open include file \"%s\"\n", fullfilename);
117 yy_switch_to_buffer(yy_create_buffer( yyin, YY_BUF_SIZE ) );
118 //BEGIN(INITIAL); keep context
121 static int do_unescape(const char*s, const char*end, char*n)
127 if(o) o[len] = *s;len++;
132 if(s==end) syntaxerror("invalid \\ at end of string");
134 /* handle the various line endings (mac, dos, unix) */
147 case '\\': if(o) o[len] = '\\';s++;len++; break;
148 case '"': if(o) o[len] = '"';s++;len++; break;
149 case 'b': if(o) o[len] = '\b';s++;len++; break;
150 case 'f': if(o) o[len] = '\f';s++;len++; break;
151 case 'n': if(o) o[len] = '\n';s++;len++; break;
152 case 'r': if(o) o[len] = '\r';s++;len++; break;
153 case 't': if(o) o[len] = '\t';s++;len++; break;
154 case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': {
157 while(strchr("01234567", *s) && nr<3 && s<end) {
164 syntaxerror("octal number out of range (0-255): %d", num);
165 if(o) o[len] = num;len++;
168 case 'x': case 'u': {
177 if(s==end) syntaxerror("invalid \\u or \\x at end of string");
180 if(s==end) syntaxerror("invalid \\u{ at end of string");
185 while(strchr("0123456789abcdefABCDEF", *s) && (bracket || nr < max) && s<end) {
187 if(*s>='0' && *s<='9') num |= *s - '0';
188 if(*s>='a' && *s<='f') num |= *s - 'a' + 10;
189 if(*s>='A' && *s<='F') num |= *s - 'A' + 10;
194 if(*s=='}' && s<end) {
197 syntaxerror("missing terminating '}'");
201 char*utf8 = getUTF8(num);
203 if(o) o[len] = *utf8;utf8++;len++;
207 syntaxerror("byte out of range (0-255): %d", num);
208 if(o) o[len] = num;len++;
213 syntaxerror("unknown escape sequence: \"\\%c\"", *s);
220 static string_t string_unescape(const char*in, int l)
223 const char*end = &in[l];
225 int len = do_unescape(s, end, 0);
226 char*n = (char*)malloc(len+1);
227 do_unescape(s, end, n);
228 string_t out = string_new(n, len);
232 static void handleString(char*s, int len)
235 if(s[len-1]!='"') syntaxerror("String doesn't end with '\"'");
238 else if(s[0]=='\'') {
239 if(s[len-1]!='\'') syntaxerror("String doesn't end with '\"'");
242 else syntaxerror("String incorrectly terminated");
245 avm2_lval.str = string_unescape(s, len);
249 char start_of_expression;
251 static inline int mkid(int type)
253 char*s = malloc(yyleng+1);
254 memcpy(s, yytext, yyleng);
260 static inline int m(int type)
262 avm2_lval.token = type;
267 static char numberbuf[64];
270 if(yyleng>sizeof(numberbuf)-1)
271 syntaxerror("decimal number overflow");
273 memcpy(s, yytext, yyleng);
278 static inline int setint(int v)
280 avm2_lval.number_int = v;
288 static inline int setuint(unsigned int v)
290 avm2_lval.number_uint = v;
298 static inline int setfloat(double v)
300 avm2_lval.number_float = v;
304 static inline int handlefloat()
307 avm2_lval.number_float = atof(s);
311 static inline int handleint()
314 char l = (yytext[0]=='-');
316 char*max = l?"1073741824":"2147483647";
318 warning("integer overflow: %s", s);
319 return handlefloat();
323 for(t=0;t<yyleng-l;t++) {
324 if(yytext[l+t]>max[t]) {
325 warning("integer overflow: %s", s);
326 return handlefloat();
328 else if(yytext[l+t]<max[t])
338 for(t=0;t<yyleng;t++) {
346 static inline int handlehex()
348 char l = (yytext[0]=='-')+2;
352 syntaxerror("integer overflow %s", s);
357 for(t=l;t<yyleng;t++) {
362 else if(c>='a' && c<='f' ||
366 if(l && v>1073741824) {
368 warning("signed integer overflow: %s", s);
371 if(!l && v>2147483647) {
373 warning("unsigned integer overflow: %s", s);
378 return setint(-(int)v);
384 void handleLabel(char*text, int len)
387 for(t=len-1;t>=0;--t) {
392 char*s = malloc(t+1);
393 memcpy(s, yytext, t);
398 static int handleregexp()
400 char*s = malloc(yyleng);
402 memcpy(s, yytext+1, len);
405 for(t=len;t>=0;--t) {
411 avm2_lval.regexp.pattern = s;
413 avm2_lval.regexp.options = 0;
415 avm2_lval.regexp.options = s+t+1;
420 void initialize_scanner();
421 #define YY_USER_INIT initialize_scanner();
423 /* count the number of lines+columns consumed by this token */
424 static inline void l() {
426 for(t=0;t<yyleng;t++) {
427 if(yytext[t]=='\n') {
435 /* count the number of columns consumed by this token */
436 static inline void c() {
437 current_column+=yyleng;
440 //Boolean {c();return m(KW_BOOLEAN);}
441 //int {c();return m(KW_INT);}
442 //uint {c();return m(KW_UINT);}
443 //Number {c();return m(KW_NUMBER);}
451 NAME [a-zA-Z_][a-zA-Z0-9_\\]*
454 HEXINT 0x[a-zA-Z0-9]+
456 FLOAT [0-9]+(\.[0-9]*)?|\.[0-9]+
458 HEXWITHSIGN [+-]?({HEXINT})
459 INTWITHSIGN [+-]?({INT})
460 FLOATWITHSIGN [+-]?({FLOAT})
462 STRING ["](\\[\x00-\xff]|[^\\"\n])*["]|['](\\[\x00-\xff]|[^\\'\n])*[']
464 MULTILINE_COMMENT [/][*]+([*][^/]|[^/*]|[^*][/]|[\x00-\x1f])*[*]+[/]
465 SINGLELINE_COMMENT \/\/[^\n]*\n
466 REGEXP [/]([^/\n]|\\[/])*[/][a-zA-Z]*
470 {SINGLELINE_COMMENT} {l(); /* single line comment */}
471 {MULTILINE_COMMENT} {l(); /* multi line comment */}
472 [/][*] {syntaxerror("syntax error: unterminated comment", yytext);}
474 ^include{S}+{STRING}{S}*/\n {l();handleInclude(yytext, yyleng, 1);}
475 ^include{S}+[^" \t\r\n][\x20-\xff]*{S}*/\n {l();handleInclude(yytext, yyleng, 0);}
476 {STRING} {l(); BEGIN(INITIAL);handleString(yytext, yyleng);return T_STRING;}
478 <BEGINNING,REGEXPOK>{
479 {REGEXP} {c(); BEGIN(INITIAL);return handleregexp();}
480 {HEXWITHSIGN} {c(); BEGIN(INITIAL);return handlehex();}
481 {INTWITHSIGN} {c(); BEGIN(INITIAL);return handleint();}
482 {FLOATWITHSIGN} {c(); BEGIN(INITIAL);return handlefloat();}
485 \xef\xbb\xbf {/* utf 8 bom */}
488 {HEXINT} {c(); BEGIN(INITIAL);return handlehex();}
489 {INT} {c(); BEGIN(INITIAL);return handleint();}
490 {FLOAT} {c(); BEGIN(INITIAL);return handlefloat();}
492 3rr0r {/* for debugging: generates a tokenizer-level error */
493 syntaxerror("3rr0r");}
495 {NAME}{S}*:{S}*for/{_} {l();handleLabel(yytext, yyleng-3);return T_FOR;}
496 {NAME}{S}*:{S}*do/{_} {l();handleLabel(yytext, yyleng-2);return T_DO;}
497 {NAME}{S}*:{S}*while/{_} {l();handleLabel(yytext, yyleng-5);return T_WHILE;}
498 {NAME}{S}*:{S}*switch/{_} {l();handleLabel(yytext, yyleng-6);return T_SWITCH;}
499 for {c();avm2_lval.id="";return T_FOR;}
500 do {c();avm2_lval.id="";return T_DO;}
501 while {c();avm2_lval.id="";return T_WHILE;}
502 switch {c();avm2_lval.id="";return T_SWITCH;}
504 [&][&] {c();BEGIN(REGEXPOK);return m(T_ANDAND);}
505 [|][|] {c();BEGIN(REGEXPOK);return m(T_OROR);}
506 [!][=] {c();BEGIN(REGEXPOK);return m(T_NE);}
507 [!][=][=] {c();BEGIN(REGEXPOK);return m(T_NEE);}
508 [=][=][=] {c();BEGIN(REGEXPOK);return m(T_EQEQEQ);}
509 [=][=] {c();BEGIN(REGEXPOK);return m(T_EQEQ);}
510 [>][=] {c();return m(T_GE);}
511 [<][=] {c();return m(T_LE);}
512 [-][-] {c();BEGIN(INITIAL);return m(T_MINUSMINUS);}
513 [+][+] {c();BEGIN(INITIAL);return m(T_PLUSPLUS);}
514 [+][=] {c();return m(T_PLUSBY);}
515 [-][=] {c();return m(T_MINUSBY);}
516 [/][=] {c();return m(T_DIVBY);}
517 [%][=] {c();return m(T_MODBY);}
518 [*][=] {c();return m(T_MULBY);}
519 [|][=] {c();return m(T_ORBY);}
520 [>][>][=] {c();return m(T_SHRBY);}
521 [<][<][=] {c();return m(T_SHLBY);}
522 [>][>][>][=] {c();return m(T_USHRBY);}
523 [<][<] {c();return m(T_SHL);}
524 [>][>][>] {c();return m(T_USHR);}
525 [>][>] {c();return m(T_SHR);}
526 \.\.\. {c();return m(T_DOTDOTDOT);}
527 \.\. {c();return m(T_DOTDOT);}
528 \. {c();return m('.');}
529 :: {c();return m(T_COLONCOLON);}
530 : {c();return m(':');}
531 instanceof {c();return m(KW_INSTANCEOF);}
532 implements {c();return m(KW_IMPLEMENTS);}
533 interface {c();return m(KW_INTERFACE);}
534 namespace {c();return m(KW_NAMESPACE);}
535 protected {c();return m(KW_PROTECTED);}
536 undefined {c();return m(KW_UNDEFINED);}
537 continue {c();return m(KW_CONTINUE);}
538 override {c();return m(KW_OVERRIDE);}
539 internal {c();return m(KW_INTERNAL);}
540 function {c();return m(KW_FUNCTION);}
541 default {c();return m(KW_DEFAULT);}
542 package {c();return m(KW_PACKAGE);}
543 private {c();return m(KW_PRIVATE);}
544 dynamic {c();return m(KW_DYNAMIC);}
545 extends {c();return m(KW_EXTENDS);}
546 delete {c();return m(KW_DELETE);}
547 return {c();return m(KW_RETURN);}
548 public {c();return m(KW_PUBLIC);}
549 native {c();return m(KW_NATIVE);}
550 static {c();return m(KW_STATIC);}
551 import {c();return m(KW_IMPORT);}
552 typeof {c();return m(KW_TYPEOF);}
553 throw {c();return m(KW_THROW);}
554 class {c();return m(KW_CLASS);}
555 const {c();return m(KW_CONST);}
556 catch {c();return m(KW_CATCH);}
557 final {c();return m(KW_FINAL);}
558 false {c();return m(KW_FALSE);}
559 break {c();return m(KW_BREAK);}
560 super {c();return m(KW_SUPER);}
561 each {c();return m(KW_EACH);}
562 void {c();return m(KW_VOID);}
563 true {c();return m(KW_TRUE);}
564 null {c();return m(KW_NULL);}
565 else {c();return m(KW_ELSE);}
566 case {c();return m(KW_CASE);}
567 with {c();return m(KW_WITH);}
568 use {c();return m(KW_USE);}
569 new {c();return m(KW_NEW);}
570 get {c();return m(KW_GET);}
571 set {c();return m(KW_SET);}
572 var {c();return m(KW_VAR);}
573 try {c();return m(KW_TRY);}
574 is {c();return m(KW_IS) ;}
575 in {c();return m(KW_IN) ;}
576 if {c();return m(KW_IF) ;}
577 as {c();return m(KW_AS);}
578 {NAME} {c();BEGIN(INITIAL);return mkid(T_IDENTIFIER);}
580 [+-\/*^~@$!%&\(=\[\]\{\}|?:;,<>] {c();BEGIN(REGEXPOK);return m(yytext[0]);}
581 [\)\]] {c();BEGIN(INITIAL);return m(yytext[0]);}
583 . {char c1=yytext[0];
588 char c = buf[t]=input();
589 if(c=='\n' || c==EOF) {
594 if(c1>='0' && c1<='9')
595 syntaxerror("syntax error: %s (identifiers must not start with a digit)");
597 syntaxerror("syntax error: %s", buf);
603 void*b = leave_file();
606 yy_delete_buffer(YY_CURRENT_BUFFER);
609 yy_delete_buffer(YY_CURRENT_BUFFER);
610 yy_switch_to_buffer(b);
621 static char mbuf[256];
622 char*token2string(enum yytokentype nr, YYSTYPE v)
624 if(nr==T_STRING) return "<string>";
625 else if(nr==T_INT) return "<int>";
626 else if(nr==T_UINT) return "<uint>";
627 else if(nr==T_BYTE) return "<byte>";
628 else if(nr==T_FLOAT) return "<float>";
629 else if(nr==T_REGEXP) return "REGEXP";
630 else if(nr==T_EOF) return "***END***";
631 else if(nr==T_GE) return ">=";
632 else if(nr==T_LE) return "<=";
633 else if(nr==T_MINUSMINUS) return "--";
634 else if(nr==T_PLUSPLUS) return "++";
635 else if(nr==KW_IMPLEMENTS) return "implements";
636 else if(nr==KW_INTERFACE) return "interface";
637 else if(nr==KW_NAMESPACE) return "namespace";
638 else if(nr==KW_PROTECTED) return "protected";
639 else if(nr==KW_OVERRIDE) return "override";
640 else if(nr==KW_INTERNAL) return "internal";
641 else if(nr==KW_FUNCTION) return "function";
642 else if(nr==KW_PACKAGE) return "package";
643 else if(nr==KW_PRIVATE) return "private";
644 else if(nr==KW_BOOLEAN) return "Boolean";
645 else if(nr==KW_DYNAMIC) return "dynamic";
646 else if(nr==KW_EXTENDS) return "extends";
647 else if(nr==KW_PUBLIC) return "public";
648 else if(nr==KW_NATIVE) return "native";
649 else if(nr==KW_STATIC) return "static";
650 else if(nr==KW_IMPORT) return "import";
651 else if(nr==KW_NUMBER) return "number";
652 else if(nr==KW_CLASS) return "class";
653 else if(nr==KW_CONST) return "const";
654 else if(nr==KW_FINAL) return "final";
655 else if(nr==KW_FALSE) return "False";
656 else if(nr==KW_TRUE) return "True";
657 else if(nr==KW_UINT) return "uint";
658 else if(nr==KW_NULL) return "null";
659 else if(nr==KW_ELSE) return "else";
660 else if(nr==KW_USE) return "use";
661 else if(nr==KW_INT) return "int";
662 else if(nr==KW_NEW) return "new";
663 else if(nr==KW_GET) return "get";
664 else if(nr==KW_SET) return "set";
665 else if(nr==KW_VAR) return "var";
666 else if(nr==KW_IS) return "is";
667 else if(nr==KW_AS) return "as";
668 else if(nr==T_IDENTIFIER) return "ID";
670 sprintf(mbuf, "%d", nr);
675 void initialize_scanner()