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"
35 int as3_verbosity = 1;
36 unsigned int as3_tokencount = 0;
38 void as3_error(const char*format, ...)
45 va_start(arglist, format);
46 vsprintf(buf, format, arglist);
48 fprintf(stderr, "%s:%d:%d: error: %s\n", current_filename_short, current_line, current_column, buf);
52 void as3_warning(const char*format, ...)
59 va_start(arglist, format);
60 vsprintf(buf, format, arglist);
62 fprintf(stderr, "%s:%d:%d: warning: %s\n", current_filename_short, current_line, current_column, buf);
65 void as3_softwarning(const char*format, ...)
72 va_start(arglist, format);
73 vsprintf(buf, format, arglist);
75 fprintf(stderr, "%s:%d:%d: warning: %s\n", current_filename_short, current_line, current_column, buf);
78 static void dbg(const char*format, ...)
85 va_start(arglist, format);
86 vsprintf(buf, format, arglist);
89 while(l && buf[l-1]=='\n') {
93 printf("(tokenizer) ");
100 #ifndef YY_CURRENT_BUFFER
101 #define YY_CURRENT_BUFFER yy_current_buffer
104 void handleInclude(char*text, int len, char quotes)
108 char*p1 = strchr(text, '"');
109 char*p2 = strrchr(text, '"');
110 if(!p1 || !p2 || p1==p2) {
111 syntaxerror("Invalid include in line %d\n", current_line);
114 filename = strdup(p1+1);
118 while(!strchr(" \n\r\t", text[i1])) i1++;
120 while(strchr(" \n\r\t", text[i1])) i1++;
121 while(strchr(" \n\r\t", text[i2-1])) i2--;
122 if(i2!=len) text[i2]=0;
123 filename = strdup(&text[i1]);
126 char*fullfilename = enter_file(filename, YY_CURRENT_BUFFER);
127 yyin = fopen(fullfilename, "rb");
129 syntaxerror("Couldn't open include file \"%s\"\n", fullfilename);
132 yy_switch_to_buffer(yy_create_buffer( yyin, YY_BUF_SIZE ) );
133 //BEGIN(INITIAL); keep context
136 static int do_unescape(const char*s, const char*end, char*n)
142 if(o) o[len] = *s;len++;
147 if(s==end) syntaxerror("invalid \\ at end of string");
149 /* handle the various line endings (mac, dos, unix) */
162 case '\\': if(o) o[len] = '\\';s++;len++; break;
163 case '"': if(o) o[len] = '"';s++;len++; break;
164 case '\'': if(o) o[len] = '\'';s++;len++; break;
165 case 'b': if(o) o[len] = '\b';s++;len++; break;
166 case 'f': if(o) o[len] = '\f';s++;len++; break;
167 case 'n': if(o) o[len] = '\n';s++;len++; break;
168 case 'r': if(o) o[len] = '\r';s++;len++; break;
169 case 't': if(o) o[len] = '\t';s++;len++; break;
170 case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': {
173 while(strchr("01234567", *s) && nr<3 && s<end) {
180 syntaxerror("octal number out of range (0-255): %d", num);
181 if(o) o[len] = num;len++;
184 case 'x': case 'u': {
193 if(s==end) syntaxerror("invalid \\u or \\x at end of string");
196 if(s==end) syntaxerror("invalid \\u{ at end of string");
201 while(strchr("0123456789abcdefABCDEF", *s) && (bracket || nr < max) && s<end) {
203 if(*s>='0' && *s<='9') num |= *s - '0';
204 if(*s>='a' && *s<='f') num |= *s - 'a' + 10;
205 if(*s>='A' && *s<='F') num |= *s - 'A' + 10;
210 if(*s=='}' && s<end) {
213 syntaxerror("missing terminating '}'");
217 char*utf8 = getUTF8(num);
219 if(o) o[len] = *utf8;utf8++;len++;
223 syntaxerror("byte out of range (0-255): %d", num);
224 if(o) o[len] = num;len++;
229 syntaxerror("unknown escape sequence: \"\\%c\"", *s);
236 static string_t string_unescape(const char*in, int l)
239 const char*end = &in[l];
241 int len = do_unescape(s, end, 0);
242 char*n = (char*)malloc(len+1);
243 do_unescape(s, end, n);
244 string_t out = string_new(n, len);
248 static void handleString(char*s, int len)
251 // don't bother decoding strings in pass 1
252 memset(&a3_lval, 0, sizeof(a3_lval));
257 if(s[len-1]!='"') syntaxerror("String doesn't end with '\"'");
260 else if(s[0]=='\'') {
261 if(s[len-1]!='\'') syntaxerror("String doesn't end with '\"'");
264 else syntaxerror("String incorrectly terminated");
267 a3_lval.str = string_unescape(s, len);
271 char start_of_expression;
273 static inline int mkid(int type)
275 char*s = malloc(yyleng+1);
276 memcpy(s, yytext, yyleng);
282 static inline int m(int type)
284 a3_lval.token = type;
289 static char numberbuf[64];
292 if(yyleng>sizeof(numberbuf)-1)
293 syntaxerror("decimal number overflow");
295 memcpy(s, yytext, yyleng);
300 static inline int setint(int v)
302 a3_lval.number_int = v;
310 static inline int setuint(unsigned int v)
312 a3_lval.number_uint = v;
320 static inline int setfloat(double v)
322 a3_lval.number_float = v;
326 static inline int handlefloat()
329 a3_lval.number_float = atof(s);
333 static inline int handleint()
336 char l = (yytext[0]=='-');
338 char*max = l?"1073741824":"2147483647";
340 as3_warning("integer overflow: %s (converted to Number)", s);
341 return handlefloat();
345 for(t=0;t<yyleng-l;t++) {
346 if(yytext[l+t]>max[t]) {
347 as3_warning("integer overflow: %s (converted to Number)", s);
348 return handlefloat();
350 else if(yytext[l+t]<max[t])
360 for(t=0;t<yyleng;t++) {
368 static inline int handlehexfloat()
370 char l = (yytext[0]=='-')+2;
375 for(t=l;t<yyleng;t++) {
388 else if((c>='a' && c<='f') || (c>='A' && c<='F'))
389 d+=((c&0x0f)+9)*base;
393 static inline int handlehex()
395 char l = (yytext[0]=='-')+2;
400 syntaxerror("integer overflow %s", s);
410 else if((c>='a' && c<='f') || (c>='A' && c<='F'))
413 if(l && v>1073741824) {
415 as3_warning("signed integer overflow: %s (converted to Number)", s);
418 if(!l && v>2147483647) {
420 as3_warning("unsigned integer overflow: %s (converted to Number)", s);
425 return setint(-(int)v);
431 void handleLabel(char*text, int len)
434 for(t=len-1;t>=0;--t) {
439 char*s = malloc(t+1);
440 memcpy(s, yytext, t);
445 static int handleregexp()
447 char*s = malloc(yyleng);
449 memcpy(s, yytext+1, len);
452 for(t=len;t>=0;--t) {
458 a3_lval.regexp.pattern = s;
460 a3_lval.regexp.options = 0;
462 a3_lval.regexp.options = s+t+1;
467 void initialize_scanner();
468 #define YY_USER_INIT initialize_scanner();
470 /* count the number of lines+columns consumed by this token */
471 static inline void l() {
473 for(t=0;t<yyleng;t++) {
474 if(yytext[t]=='\n') {
482 /* count the number of columns consumed by this token */
483 static inline void c() {
484 current_column+=yyleng;
487 //Boolean {c();return m(KW_BOOLEAN);}
488 //int {c();return m(KW_INT);}
489 //uint {c();return m(KW_UINT);}
490 //Number {c();return m(KW_NUMBER);}
498 NAME [a-zA-Z_][a-zA-Z0-9_\\]*
501 HEXINT 0x[a-zA-Z0-9]+
502 HEXFLOAT 0x[a-zA-Z0-9]*\.[a-zA-Z0-9]*
504 FLOAT [0-9]+(\.[0-9]*)?|\.[0-9]+
506 HEXWITHSIGN [+-]?({HEXINT})
507 HEXFLOATWITHSIGN [+-]?({HEXFLOAT})
508 INTWITHSIGN [+-]?({INT})
509 FLOATWITHSIGN [+-]?({FLOAT})
511 STRING ["](\\[\x00-\xff]|[^\\"\n])*["]|['](\\[\x00-\xff]|[^\\'\n])*[']
513 MULTILINE_COMMENT [/][*]+([*][^/]|[^/*]|[^*][/]|[\x00-\x1f])*[*]+[/]
514 SINGLELINE_COMMENT \/\/[^\n]*\n
515 REGEXP [/]([^/\n]|\\[/])*[/][a-zA-Z]*
519 {SINGLELINE_COMMENT} {l(); /* single line comment */}
520 {MULTILINE_COMMENT} {l(); /* multi line comment */}
521 [/][*] {syntaxerror("syntax error: unterminated comment", yytext);}
523 ^include{S}+{STRING}{S}*/\n {l();handleInclude(yytext, yyleng, 1);}
524 ^include{S}+[^" \t\r\n][\x20-\xff]*{S}*/\n {l();handleInclude(yytext, yyleng, 0);}
525 {STRING} {l(); BEGIN(INITIAL);handleString(yytext, yyleng);return T_STRING;}
527 <BEGINNING,REGEXPOK>{
528 {REGEXP} {c(); BEGIN(INITIAL);return handleregexp();}
529 {HEXWITHSIGN} {c(); BEGIN(INITIAL);return handlehex();}
530 {HEXFLOATWITHSIGN} {c(); BEGIN(INITIAL);return handlehexfloat();}
531 {INTWITHSIGN} {c(); BEGIN(INITIAL);return handleint();}
532 {FLOATWITHSIGN} {c(); BEGIN(INITIAL);return handlefloat();}
535 \xef\xbb\xbf {/* utf 8 bom */}
538 {HEXINT} {c(); BEGIN(INITIAL);return handlehex();}
539 {HEXFLOAT} {c(); BEGIN(INITIAL);return handlehexfloat();}
540 {INT} {c(); BEGIN(INITIAL);return handleint();}
541 {FLOAT} {c(); BEGIN(INITIAL);return handlefloat();}
543 3rr0r {/* for debugging: generates a tokenizer-level error */
544 syntaxerror("3rr0r");}
546 {NAME}{S}*:{S}*for/{_} {l();handleLabel(yytext, yyleng-3);return T_FOR;}
547 {NAME}{S}*:{S}*do/{_} {l();handleLabel(yytext, yyleng-2);return T_DO;}
548 {NAME}{S}*:{S}*while/{_} {l();handleLabel(yytext, yyleng-5);return T_WHILE;}
549 {NAME}{S}*:{S}*switch/{_} {l();handleLabel(yytext, yyleng-6);return T_SWITCH;}
550 for {c();a3_lval.id="";return T_FOR;}
551 do {c();a3_lval.id="";return T_DO;}
552 while {c();a3_lval.id="";return T_WHILE;}
553 switch {c();a3_lval.id="";return T_SWITCH;}
555 [&][&] {c();BEGIN(REGEXPOK);return m(T_ANDAND);}
556 [|][|] {c();BEGIN(REGEXPOK);return m(T_OROR);}
557 [!][=] {c();BEGIN(REGEXPOK);return m(T_NE);}
558 [!][=][=] {c();BEGIN(REGEXPOK);return m(T_NEE);}
559 [=][=][=] {c();BEGIN(REGEXPOK);return m(T_EQEQEQ);}
560 [=][=] {c();BEGIN(REGEXPOK);return m(T_EQEQ);}
561 [>][=] {c();return m(T_GE);}
562 [<][=] {c();return m(T_LE);}
563 [-][-] {c();BEGIN(INITIAL);return m(T_MINUSMINUS);}
564 [+][+] {c();BEGIN(INITIAL);return m(T_PLUSPLUS);}
565 [+][=] {c();return m(T_PLUSBY);}
566 [-][=] {c();return m(T_MINUSBY);}
567 [/][=] {c();return m(T_DIVBY);}
568 [%][=] {c();return m(T_MODBY);}
569 [*][=] {c();return m(T_MULBY);}
570 [|][=] {c();return m(T_ORBY);}
571 [>][>][=] {c();return m(T_SHRBY);}
572 [<][<][=] {c();return m(T_SHLBY);}
573 [>][>][>][=] {c();return m(T_USHRBY);}
574 [<][<] {c();return m(T_SHL);}
575 [>][>][>] {c();return m(T_USHR);}
576 [>][>] {c();return m(T_SHR);}
577 \.\.\. {c();return m(T_DOTDOTDOT);}
578 \.\. {c();return m(T_DOTDOT);}
579 \. {c();return m('.');}
580 :: {c();return m(T_COLONCOLON);}
581 : {c();return m(':');}
582 instanceof {c();return m(KW_INSTANCEOF);}
583 implements {c();return m(KW_IMPLEMENTS);}
584 interface {c();return m(KW_INTERFACE);}
585 namespace {c();return m(KW_NAMESPACE);}
586 protected {c();return m(KW_PROTECTED);}
587 undefined {c();return m(KW_UNDEFINED);}
588 continue {c();return m(KW_CONTINUE);}
589 override {c();return m(KW_OVERRIDE);}
590 internal {c();return m(KW_INTERNAL);}
591 function {c();return m(KW_FUNCTION);}
592 finally {c();return m(KW_FINALLY);}
593 default {c();return m(KW_DEFAULT);}
594 package {c();return m(KW_PACKAGE);}
595 private {c();return m(KW_PRIVATE);}
596 dynamic {c();return m(KW_DYNAMIC);}
597 extends {c();return m(KW_EXTENDS);}
598 delete {c();return m(KW_DELETE);}
599 return {c();return m(KW_RETURN);}
600 public {c();return m(KW_PUBLIC);}
601 native {c();return m(KW_NATIVE);}
602 static {c();return m(KW_STATIC);}
603 import {c();return m(KW_IMPORT);}
604 typeof {c();return m(KW_TYPEOF);}
605 throw {c();return m(KW_THROW);}
606 class {c();return m(KW_CLASS);}
607 const {c();return m(KW_CONST);}
608 catch {c();return m(KW_CATCH);}
609 final {c();return m(KW_FINAL);}
610 false {c();return m(KW_FALSE);}
611 break {c();return m(KW_BREAK);}
612 super {c();return m(KW_SUPER);}
613 each {c();return m(KW_EACH);}
614 void {c();return m(KW_VOID);}
615 true {c();return m(KW_TRUE);}
616 null {c();return m(KW_NULL);}
617 else {c();return m(KW_ELSE);}
618 case {c();return m(KW_CASE);}
619 with {c();return m(KW_WITH);}
620 use {c();return m(KW_USE);}
621 new {c();return m(KW_NEW);}
622 get {c();return m(KW_GET);}
623 set {c();return m(KW_SET);}
624 var {c();return m(KW_VAR);}
625 try {c();return m(KW_TRY);}
626 is {c();return m(KW_IS) ;}
627 in {c();return m(KW_IN) ;}
628 if {c();return m(KW_IF) ;}
629 as {c();return m(KW_AS);}
630 {NAME} {c();BEGIN(INITIAL);return mkid(T_IDENTIFIER);}
632 [+-\/*^~@$!%&\(=\[\]\{\}|?:;,<>] {c();BEGIN(REGEXPOK);return m(yytext[0]);}
633 [\)\]] {c();BEGIN(INITIAL);return m(yytext[0]);}
641 char c = buf[t]=input();
642 if(c=='\n' || c==EOF) {
647 if(c1>='0' && c1<='9')
648 syntaxerror("syntax error: %s (identifiers must not start with a digit)");
650 syntaxerror("syntax error: %s", buf);
656 void*b = leave_file();
659 yy_delete_buffer(YY_CURRENT_BUFFER);
662 yy_delete_buffer(YY_CURRENT_BUFFER);
663 yy_switch_to_buffer(b);
674 static char mbuf[256];
675 char*token2string(enum yytokentype nr, YYSTYPE v)
677 if(nr==T_STRING) return "<string>";
678 else if(nr==T_INT) return "<int>";
679 else if(nr==T_UINT) return "<uint>";
680 else if(nr==T_BYTE) return "<byte>";
681 else if(nr==T_FLOAT) return "<float>";
682 else if(nr==T_REGEXP) return "REGEXP";
683 else if(nr==T_EOF) return "***END***";
684 else if(nr==T_GE) return ">=";
685 else if(nr==T_LE) return "<=";
686 else if(nr==T_MINUSMINUS) return "--";
687 else if(nr==T_PLUSPLUS) return "++";
688 else if(nr==KW_IMPLEMENTS) return "implements";
689 else if(nr==KW_INTERFACE) return "interface";
690 else if(nr==KW_NAMESPACE) return "namespace";
691 else if(nr==KW_PROTECTED) return "protected";
692 else if(nr==KW_OVERRIDE) return "override";
693 else if(nr==KW_INTERNAL) return "internal";
694 else if(nr==KW_FUNCTION) return "function";
695 else if(nr==KW_PACKAGE) return "package";
696 else if(nr==KW_PRIVATE) return "private";
697 else if(nr==KW_BOOLEAN) return "Boolean";
698 else if(nr==KW_DYNAMIC) return "dynamic";
699 else if(nr==KW_EXTENDS) return "extends";
700 else if(nr==KW_PUBLIC) return "public";
701 else if(nr==KW_NATIVE) return "native";
702 else if(nr==KW_STATIC) return "static";
703 else if(nr==KW_IMPORT) return "import";
704 else if(nr==KW_NUMBER) return "number";
705 else if(nr==KW_CLASS) return "class";
706 else if(nr==KW_CONST) return "const";
707 else if(nr==KW_FINAL) return "final";
708 else if(nr==KW_FALSE) return "False";
709 else if(nr==KW_TRUE) return "True";
710 else if(nr==KW_UINT) return "uint";
711 else if(nr==KW_NULL) return "null";
712 else if(nr==KW_ELSE) return "else";
713 else if(nr==KW_USE) return "use";
714 else if(nr==KW_INT) return "int";
715 else if(nr==KW_NEW) return "new";
716 else if(nr==KW_GET) return "get";
717 else if(nr==KW_SET) return "set";
718 else if(nr==KW_VAR) return "var";
719 else if(nr==KW_IS) return "is";
720 else if(nr==KW_AS) return "as";
721 else if(nr==T_IDENTIFIER) return "ID";
723 sprintf(mbuf, "%d", nr);
728 void initialize_scanner()