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) ");
98 #ifndef YY_CURRENT_BUFFER
99 #define YY_CURRENT_BUFFER yy_current_buffer
102 static void*as3_buffer=0;
103 static int as3_buffer_pos=0;
104 static int as3_buffer_len=0;
105 void as3_file_input(FILE*fi)
110 void as3_buffer_input(void*buffer, int len)
113 syntaxerror("trying to parse zero bytearray");
115 as3_buffer_len = len;
120 #define YY_INPUT(buf,result,max_size) { \
123 while((result = fread(buf, 1, max_size, as3_in))==0 && ferror(as3_in)) \
124 { if(errno != EINTR) {YY_FATAL_ERROR("input in flex scanner failed"); break;} \
125 errno=0; clearerr(as3_in); \
128 int to_read = max_size; \
129 if(to_read + as3_buffer_pos > as3_buffer_len) \
130 to_read = as3_buffer_len - as3_buffer_pos; \
131 memcpy(buf, as3_buffer+as3_buffer_pos, to_read); \
132 as3_buffer_pos += to_read; \
137 void handleInclude(char*text, int len, char quotes)
141 char*p1 = strchr(text, '"');
142 char*p2 = strrchr(text, '"');
143 if(!p1 || !p2 || p1==p2) {
144 syntaxerror("Invalid include in line %d\n", current_line);
147 filename = strdup(p1+1);
151 while(!strchr(" \n\r\t", text[i1])) i1++;
153 while(strchr(" \n\r\t", text[i1])) i1++;
154 while(strchr(" \n\r\t", text[i2-1])) i2--;
155 if(i2!=len) text[i2]=0;
156 filename = strdup(&text[i1]);
159 char*fullfilename = find_file(filename);
160 enter_file2(filename, fullfilename, YY_CURRENT_BUFFER);
161 yyin = fopen(fullfilename, "rb");
163 syntaxerror("Couldn't open include file \"%s\"\n", fullfilename);
166 yy_switch_to_buffer(yy_create_buffer( yyin, YY_BUF_SIZE ) );
167 //BEGIN(INITIAL); keep context
170 static int do_unescape(const char*s, const char*end, char*n)
176 if(o) o[len] = *s;len++;
181 if(s==end) syntaxerror("invalid \\ at end of string");
183 /* handle the various line endings (mac, dos, unix) */
196 case '\\': if(o) o[len] = '\\';s++;len++; break;
197 case '"': if(o) o[len] = '"';s++;len++; break;
198 case '\'': if(o) o[len] = '\'';s++;len++; break;
199 case 'b': if(o) o[len] = '\b';s++;len++; break;
200 case 'f': if(o) o[len] = '\f';s++;len++; break;
201 case 'n': if(o) o[len] = '\n';s++;len++; break;
202 case 'r': if(o) o[len] = '\r';s++;len++; break;
203 case 't': if(o) o[len] = '\t';s++;len++; break;
204 case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': {
207 while(strchr("01234567", *s) && nr<3 && s<end) {
214 syntaxerror("octal number out of range (0-255): %d", num);
215 if(o) o[len] = num;len++;
218 case 'x': case 'u': {
227 if(s==end) syntaxerror("invalid \\u or \\x at end of string");
230 if(s==end) syntaxerror("invalid \\u{ at end of string");
235 while(strchr("0123456789abcdefABCDEF", *s) && (bracket || nr < max) && s<end) {
237 if(*s>='0' && *s<='9') num |= *s - '0';
238 if(*s>='a' && *s<='f') num |= *s - 'a' + 10;
239 if(*s>='A' && *s<='F') num |= *s - 'A' + 10;
244 if(*s=='}' && s<end) {
247 syntaxerror("missing terminating '}'");
251 char*utf8 = getUTF8(num);
253 if(o) o[len] = *utf8;utf8++;len++;
257 syntaxerror("byte out of range (0-255): %d", num);
258 if(o) o[len] = num;len++;
263 syntaxerror("unknown escape sequence: \"\\%c\"", *s);
270 static string_t string_unescape(const char*in, int l)
273 const char*end = &in[l];
275 int len = do_unescape(s, end, 0);
276 char*n = (char*)malloc(len+1);
277 do_unescape(s, end, n);
278 string_t out = string_new(n, len);
282 static void handleCData(char*s, int len)
284 a3_lval.str.str = s+9; // <![CDATA[
285 a3_lval.str.len = len-9-3;// ]]>
286 a3_lval.str.str = strdup_n(a3_lval.str.str, a3_lval.str.len);
289 static void handleString(char*s, int len)
292 // don't bother decoding strings in pass 1
293 memset(&a3_lval, 0, sizeof(a3_lval));
298 if(s[len-1]!='"') syntaxerror("String doesn't end with '\"'");
301 else if(s[0]=='\'') {
302 if(s[len-1]!='\'') syntaxerror("String doesn't end with '\"'");
305 else syntaxerror("String incorrectly terminated");
308 a3_lval.str = string_unescape(s, len);
312 char start_of_expression;
314 static inline int m(int type)
316 a3_lval.token = type;
321 static char numberbuf[64];
324 if(yyleng>sizeof(numberbuf)-1)
325 syntaxerror("decimal number overflow");
327 memcpy(s, yytext, yyleng);
332 static inline int setint(int v)
334 a3_lval.number_int = v;
342 static inline int setuint(unsigned int v)
344 a3_lval.number_uint = v;
352 static inline int setfloat(double v)
354 a3_lval.number_float = v;
358 static inline int handlefloat()
361 a3_lval.number_float = atof(s);
365 static inline int handleint()
368 char l = (yytext[0]=='-');
370 char*max = l?"1073741824":"2147483647";
372 as3_warning("integer overflow: %s (converted to Number)", s);
373 return handlefloat();
377 for(t=0;t<yyleng-l;t++) {
378 if(yytext[l+t]>max[t]) {
379 as3_warning("integer overflow: %s (converted to Number)", s);
380 return handlefloat();
382 else if(yytext[l+t]<max[t])
392 for(t=0;t<yyleng;t++) {
400 static inline int handlehexfloat()
402 char l = (yytext[0]=='-')+2;
407 for(t=l;t<yyleng;t++) {
420 else if((c>='a' && c<='f') || (c>='A' && c<='F'))
421 d+=((c&0x0f)+9)*base;
425 static inline int handlehex()
427 char l = (yytext[0]=='-')+2;
432 syntaxerror("integer overflow %s", s);
442 else if((c>='a' && c<='f') || (c>='A' && c<='F'))
445 if(l && v>1073741824) {
447 as3_warning("signed integer overflow: %s (converted to Number)", s);
450 if(!l && v>2147483647) {
452 as3_warning("unsigned integer overflow: %s (converted to Number)", s);
457 return setint(-(int)v);
463 void handleLabel(char*text, int len)
466 for(t=len-1;t>=0;--t) {
471 char*s = malloc(t+1);
472 memcpy(s, yytext, t);
477 static int handleregexp()
479 char*s = malloc(yyleng);
481 memcpy(s, yytext+1, len);
484 for(t=len;t>=0;--t) {
490 a3_lval.regexp.pattern = s;
492 a3_lval.regexp.options = 0;
494 a3_lval.regexp.options = s+t+1;
499 void initialize_scanner();
500 #define YY_USER_INIT initialize_scanner();
502 /* count the number of lines+columns consumed by this token */
503 static inline void l() {
505 for(t=0;t<yyleng;t++) {
506 if(yytext[t]=='\n') {
514 /* count the number of columns consumed by this token */
515 static inline void c() {
516 current_column+=yyleng;
519 static trie_t*namespaces = 0;
520 void tokenizer_register_namespace(const char*id)
522 trie_put(&namespaces, id);
524 static inline tokenizer_is_namespace(const char*id)
526 return trie_lookup(namespaces, id);
529 static inline int handleIdentifier()
531 char*s = malloc(yyleng+1);
532 memcpy(s, yytext, yyleng);
535 if(tokenizer_is_namespace(s))
542 //Boolean {c();return m(KW_BOOLEAN);}
543 //int {c();return m(KW_INT);}
544 //uint {c();return m(KW_UINT);}
545 //Number {c();return m(KW_NUMBER);}
552 NAME [a-zA-Z_][a-zA-Z0-9_\\]*
555 HEXINT 0x[a-zA-Z0-9]+
556 HEXFLOAT 0x[a-zA-Z0-9]*\.[a-zA-Z0-9]*
558 FLOAT ([0-9]+(\.[0-9]*)?|\.[0-9]+)(e[0-9]+)?
560 HEXWITHSIGN [+-]?({HEXINT})
561 HEXFLOATWITHSIGN [+-]?({HEXFLOAT})
562 INTWITHSIGN [+-]?({INT})
563 FLOATWITHSIGN [+-]?({FLOAT})
565 CDATA <!\[CDATA\[([^]]|\][^]]|\]\][^>])*\]*\]\]\>
566 STRING ["](\\[\x00-\xff]|[^\\"\n])*["]|['](\\[\x00-\xff]|[^\\'\n])*[']
568 MULTILINE_COMMENT [/][*]+([*][^/]|[^/*]|[^*][/]|[\x00-\x1f])*[*]+[/]
569 SINGLELINE_COMMENT \/\/[^\n\r]*[\n\r]
570 REGEXP [/]([^/\n]|\\[/])*[/][a-zA-Z]*
574 {SINGLELINE_COMMENT} {l(); /* single line comment */}
575 {MULTILINE_COMMENT} {l(); /* multi line comment */}
576 [/][*] {syntaxerror("syntax error: unterminated comment", yytext);}
578 ^include{S}+{STRING}{S}*/\n {l();handleInclude(yytext, yyleng, 1);}
579 ^include{S}+[^" \t\r\n][\x20-\xff]*{S}*/\n {l();handleInclude(yytext, yyleng, 0);}
580 {STRING} {l(); BEGIN(INITIAL);handleString(yytext, yyleng);return T_STRING;}
581 {CDATA} {l(); BEGIN(INITIAL);handleCData(yytext, yyleng);return T_STRING;}
583 <BEGINNING,REGEXPOK>{
584 {REGEXP} {c(); BEGIN(INITIAL);return handleregexp();}
585 {HEXWITHSIGN}/{_} {c(); BEGIN(INITIAL);return handlehex();}
586 {HEXFLOATWITHSIGN}/{_} {c(); BEGIN(INITIAL);return handlehexfloat();}
587 {INTWITHSIGN}/{_} {c(); BEGIN(INITIAL);return handleint();}
588 {FLOATWITHSIGN}/{_} {c(); BEGIN(INITIAL);return handlefloat();}
591 \xef\xbb\xbf {/* utf 8 bom */}
594 {HEXINT}/{_} {c(); BEGIN(INITIAL);return handlehex();}
595 {HEXFLOAT}/{_} {c(); BEGIN(INITIAL);return handlehexfloat();}
596 {INT}/{_} {c(); BEGIN(INITIAL);return handleint();}
597 {FLOAT}/{_} {c(); BEGIN(INITIAL);return handlefloat();}
599 3rr0r {/* for debugging: generates a tokenizer-level error */
600 syntaxerror("3rr0r");}
602 {NAME}{S}*:{S}*for/{_} {l();handleLabel(yytext, yyleng-3);return T_FOR;}
603 {NAME}{S}*:{S}*do/{_} {l();handleLabel(yytext, yyleng-2);return T_DO;}
604 {NAME}{S}*:{S}*while/{_} {l();handleLabel(yytext, yyleng-5);return T_WHILE;}
605 {NAME}{S}*:{S}*switch/{_} {l();handleLabel(yytext, yyleng-6);return T_SWITCH;}
606 for {c();a3_lval.id="";return T_FOR;}
607 do {c();a3_lval.id="";return T_DO;}
608 while {c();a3_lval.id="";return T_WHILE;}
609 switch {c();a3_lval.id="";return T_SWITCH;}
611 [&][&] {c();BEGIN(REGEXPOK);return m(T_ANDAND);}
612 [|][|] {c();BEGIN(REGEXPOK);return m(T_OROR);}
613 [!][=] {c();BEGIN(REGEXPOK);return m(T_NE);}
614 [!][=][=] {c();BEGIN(REGEXPOK);return m(T_NEE);}
615 [=][=][=] {c();BEGIN(REGEXPOK);return m(T_EQEQEQ);}
616 [=][=] {c();BEGIN(REGEXPOK);return m(T_EQEQ);}
617 [>][=] {c();return m(T_GE);}
618 [<][=] {c();return m(T_LE);}
619 [-][-] {c();BEGIN(INITIAL);return m(T_MINUSMINUS);}
620 [+][+] {c();BEGIN(INITIAL);return m(T_PLUSPLUS);}
621 [+][=] {c();return m(T_PLUSBY);}
622 [-][=] {c();return m(T_MINUSBY);}
623 [/][=] {c();return m(T_DIVBY);}
624 [%][=] {c();return m(T_MODBY);}
625 [*][=] {c();return m(T_MULBY);}
626 [|][=] {c();return m(T_ORBY);}
627 [>][>][=] {c();return m(T_SHRBY);}
628 [<][<][=] {c();return m(T_SHLBY);}
629 [>][>][>][=] {c();return m(T_USHRBY);}
630 [<][<] {c();return m(T_SHL);}
631 [>][>][>] {c();return m(T_USHR);}
632 [>][>] {c();return m(T_SHR);}
633 \.\.\. {c();return m(T_DOTDOTDOT);}
634 \.\. {c();return m(T_DOTDOT);}
635 \. {c();return m('.');}
636 :: {c();return m(T_COLONCOLON);}
637 : {c();return m(':');}
638 instanceof {c();return m(KW_INSTANCEOF);}
639 implements {c();return m(KW_IMPLEMENTS);}
640 interface {c();return m(KW_INTERFACE);}
641 namespace {c();return m(KW_NAMESPACE);}
642 protected {c();return m(KW_PROTECTED);}
643 undefined {c();return m(KW_UNDEFINED);}
644 continue {c();return m(KW_CONTINUE);}
645 override {c();return m(KW_OVERRIDE);}
646 internal {c();return m(KW_INTERNAL);}
647 function {c();return m(KW_FUNCTION);}
648 finally {c();return m(KW_FINALLY);}
649 default {c();return m(KW_DEFAULT);}
650 package {c();return m(KW_PACKAGE);}
651 private {c();return m(KW_PRIVATE);}
652 dynamic {c();return m(KW_DYNAMIC);}
653 extends {c();return m(KW_EXTENDS);}
654 delete {c();return m(KW_DELETE);}
655 return {c();return m(KW_RETURN);}
656 public {c();return m(KW_PUBLIC);}
657 native {c();return m(KW_NATIVE);}
658 static {c();return m(KW_STATIC);}
659 import {c();return m(KW_IMPORT);}
660 typeof {c();return m(KW_TYPEOF);}
661 throw {c();return m(KW_THROW);}
662 class {c();return m(KW_CLASS);}
663 const {c();return m(KW_CONST);}
664 catch {c();return m(KW_CATCH);}
665 final {c();return m(KW_FINAL);}
666 false {c();return m(KW_FALSE);}
667 break {c();return m(KW_BREAK);}
668 super {c();return m(KW_SUPER);}
669 each {c();return m(KW_EACH);}
670 void {c();return m(KW_VOID);}
671 true {c();return m(KW_TRUE);}
672 null {c();return m(KW_NULL);}
673 else {c();return m(KW_ELSE);}
674 case {c();return m(KW_CASE);}
675 with {c();return m(KW_WITH);}
676 use {c();return m(KW_USE);}
677 new {c();return m(KW_NEW);}
678 get {c();return m(KW_GET);}
679 set {c();return m(KW_SET);}
680 var {c();return m(KW_VAR);}
681 try {c();return m(KW_TRY);}
682 is {c();return m(KW_IS) ;}
683 in {c();return m(KW_IN) ;}
684 if {c();return m(KW_IF) ;}
685 as {c();return m(KW_AS);}
686 {NAME} {c();BEGIN(INITIAL);return handleIdentifier();}
688 [\]\}] {c();BEGIN(INITIAL);return m(yytext[0]);}
689 [+-\/*^~@$!%&\(=\[\{|?:;,<>] {c();BEGIN(REGEXPOK);return m(yytext[0]);}
690 [\)\]] {c();BEGIN(INITIAL);return m(yytext[0]);}
698 char c = buf[t]=input();
699 if(c=='\n' || c==EOF) {
704 if(c1>='0' && c1<='9')
705 syntaxerror("syntax error: %s (identifiers must not start with a digit)");
707 syntaxerror("syntax error: %s", buf);
713 void*b = leave_file();
716 yy_delete_buffer(YY_CURRENT_BUFFER);
719 yy_delete_buffer(YY_CURRENT_BUFFER);
720 yy_switch_to_buffer(b);
731 static char mbuf[256];
732 char*token2string(enum yytokentype nr, YYSTYPE v)
735 char*s = malloc(v.str.len+10);
736 strcpy(s, "<string>");
737 memcpy(s+8, v.str.str, v.str.len);
738 sprintf(s+8+v.str.len, " (%d bytes)", v.str.len);
741 else if(nr==T_REGEXP) {
742 char*s = malloc(strlen(v.regexp.pattern)+10);
743 sprintf(s, "<regexp>%s", v.regexp.pattern);
746 else if(nr==T_IDENTIFIER) {
747 char*s = malloc(strlen(v.id)+10);
748 sprintf(s, "<ID>%s", v.id);
751 else if(nr==T_INT) return "<int>";
752 else if(nr==T_UINT) return "<uint>";
753 else if(nr==T_BYTE) return "<byte>";
754 else if(nr==T_FLOAT) return "<float>";
755 else if(nr==T_EOF) return "***END***";
756 else if(nr==T_GE) return ">=";
757 else if(nr==T_LE) return "<=";
758 else if(nr==T_MINUSMINUS) return "--";
759 else if(nr==T_PLUSPLUS) return "++";
760 else if(nr==KW_IMPLEMENTS) return "implements";
761 else if(nr==KW_INTERFACE) return "interface";
762 else if(nr==KW_NAMESPACE) return "namespace";
763 else if(nr==KW_PROTECTED) return "protected";
764 else if(nr==KW_OVERRIDE) return "override";
765 else if(nr==KW_INTERNAL) return "internal";
766 else if(nr==KW_FUNCTION) return "function";
767 else if(nr==KW_PACKAGE) return "package";
768 else if(nr==KW_PRIVATE) return "private";
769 else if(nr==KW_BOOLEAN) return "Boolean";
770 else if(nr==KW_DYNAMIC) return "dynamic";
771 else if(nr==KW_EXTENDS) return "extends";
772 else if(nr==KW_PUBLIC) return "public";
773 else if(nr==KW_NATIVE) return "native";
774 else if(nr==KW_STATIC) return "static";
775 else if(nr==KW_IMPORT) return "import";
776 else if(nr==KW_NUMBER) return "number";
777 else if(nr==KW_CLASS) return "class";
778 else if(nr==KW_CONST) return "const";
779 else if(nr==KW_FINAL) return "final";
780 else if(nr==KW_FALSE) return "False";
781 else if(nr==KW_TRUE) return "True";
782 else if(nr==KW_UINT) return "uint";
783 else if(nr==KW_NULL) return "null";
784 else if(nr==KW_ELSE) return "else";
785 else if(nr==KW_USE) return "use";
786 else if(nr==KW_INT) return "int";
787 else if(nr==KW_NEW) return "new";
788 else if(nr==KW_GET) return "get";
789 else if(nr==KW_SET) return "set";
790 else if(nr==KW_VAR) return "var";
791 else if(nr==KW_IS) return "is";
792 else if(nr==KW_AS) return "as";
794 sprintf(mbuf, "%d", nr);
799 void initialize_scanner()