X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=lib%2Fas3%2Ftokenizer.lex;h=63f586dda0e60cbc55ad3cc53ff9690bb32293b1;hb=063a400cad92488e0cc96f8362a3d54d894e0422;hp=d2e277c91029c34c3d9287c439aafb04af5bcb11;hpb=6a35c7253ba161c50a18ecc86e46d0b3ead9a2a0;p=swftools.git diff --git a/lib/as3/tokenizer.lex b/lib/as3/tokenizer.lex index d2e277c..63f586d 100644 --- a/lib/as3/tokenizer.lex +++ b/lib/as3/tokenizer.lex @@ -31,59 +31,43 @@ #include "tokenizer.h" #include "files.h" -static void countlines(char*text, int len) { - int t; - for(t=0;t as3_buffer_len) \ + to_read = as3_buffer_len - as3_buffer_pos; \ + memcpy(buf, as3_buffer+as3_buffer_pos, to_read); \ + as3_buffer_pos += to_read; \ + result=to_read; \ + } \ +} + void handleInclude(char*text, int len, char quotes) { char*filename = 0; @@ -119,7 +154,8 @@ void handleInclude(char*text, int len, char quotes) filename = strdup(&text[i1]); } - char*fullfilename = enter_file(filename, YY_CURRENT_BUFFER); + char*fullfilename = find_file(filename); + enter_file2(filename, fullfilename, YY_CURRENT_BUFFER); yyin = fopen(fullfilename, "rb"); if (!yyin) { syntaxerror("Couldn't open include file \"%s\"\n", fullfilename); @@ -157,6 +193,7 @@ static int do_unescape(const char*s, const char*end, char*n) switch(*s) { case '\\': if(o) o[len] = '\\';s++;len++; break; case '"': if(o) o[len] = '"';s++;len++; break; + case '\'': if(o) o[len] = '\'';s++;len++; break; case 'b': if(o) o[len] = '\b';s++;len++; break; case 'f': if(o) o[len] = '\f';s++;len++; break; case 'n': if(o) o[len] = '\n';s++;len++; break; @@ -240,8 +277,20 @@ static string_t string_unescape(const char*in, int l) return out; } +static void handleCData(char*s, int len) +{ + a3_lval.str.str = s+9; // +} + static void handleString(char*s, int len) { + if(as3_pass < 2) { + // don't bother decoding strings in pass 1 + memset(&a3_lval, 0, sizeof(a3_lval)); + return; + } + if(s[0]=='"') { if(s[len-1]!='"') syntaxerror("String doesn't end with '\"'"); s++;len-=2; @@ -253,24 +302,24 @@ static void handleString(char*s, int len) else syntaxerror("String incorrectly terminated"); - avm2_lval.str = string_unescape(s, len); + a3_lval.str = string_unescape(s, len); } char start_of_expression; -static inline int mkid(int type) +static inline int handleIdentifier(int type) { char*s = malloc(yyleng+1); memcpy(s, yytext, yyleng); s[yyleng]=0; - avm2_lval.id = s; + a3_lval.id = s; return type; } static inline int m(int type) { - avm2_lval.token = type; + a3_lval.token = type; return type; } @@ -288,7 +337,7 @@ static char*nrbuf() static inline int setint(int v) { - avm2_lval.number_int = v; + a3_lval.number_int = v; if(v>-128) return T_BYTE; else if(v>=-32768) @@ -298,7 +347,7 @@ static inline int setint(int v) } static inline int setuint(unsigned int v) { - avm2_lval.number_uint = v; + a3_lval.number_uint = v; if(v<128) return T_BYTE; else if(v<32768) @@ -308,14 +357,14 @@ static inline int setuint(unsigned int v) } static inline int setfloat(double v) { - avm2_lval.number_float = v; + a3_lval.number_float = v; return T_FLOAT; } static inline int handlefloat() { char*s = nrbuf(); - avm2_lval.number_float = atof(s); + a3_lval.number_float = atof(s); return T_FLOAT; } @@ -326,14 +375,16 @@ static inline int handleint() char*max = l?"1073741824":"2147483647"; if(yyleng-l>10) { - warning("integer overflow: %s", s); + as3_warning("integer overflow: %s (converted to Number)", s); return handlefloat(); } if(yyleng-l==10) { int t; for(t=0;tmax[t]) - syntaxerror("integer overflow %s > %s", s+l,max); + if(yytext[l+t]>max[t]) { + as3_warning("integer overflow: %s (converted to Number)", s); + return handlefloat(); + } else if(yytext[l+t]='0' && c<='9') + d+=(c&15)*base; + else if((c>='a' && c<='f') || (c>='A' && c<='F')) + d+=((c&0x0f)+9)*base; + } + return setfloat(d); +} static inline int handlehex() { char l = (yytext[0]=='-')+2; + int len = yyleng; - if(yyleng-l>8) { + if(len-l>8) { char*s = nrbuf(); syntaxerror("integer overflow %s", s); } int t; unsigned int v = 0; - for(t=l;t='0' && c<='9') v|=(c&15); - else if(c>='a' && c<='f' || - c>='A' && c<='F') + else if((c>='a' && c<='f') || (c>='A' && c<='F')) v|=(c&0x0f)+9; } if(l && v>1073741824) { char*s = nrbuf(); - warning("signed integer overflow: %s", s); + as3_warning("signed integer overflow: %s (converted to Number)", s); return setfloat(v); } if(!l && v>2147483647) { char*s = nrbuf(); - warning("unsigned integer overflow: %s", s); + as3_warning("unsigned integer overflow: %s (converted to Number)", s); return setfloat(v); } @@ -395,26 +471,62 @@ void handleLabel(char*text, int len) int t; for(t=len-1;t>=0;--t) { if(text[t]!=' ' && - text[t]!='.') + text[t]!=':') break; } char*s = malloc(t+1); memcpy(s, yytext, t); s[t]=0; - avm2_lval.id = s; + a3_lval.id = s; +} + +static int handleregexp() +{ + char*s = malloc(yyleng); + int len=yyleng-1; + memcpy(s, yytext+1, len); + s[len] = 0; + int t; + for(t=len;t>=0;--t) { + if(s[t]=='/') { + s[t] = 0; + break; + } + } + a3_lval.regexp.pattern = s; + if(t==len) { + a3_lval.regexp.options = 0; + } else { + a3_lval.regexp.options = s+t+1; + } + return T_REGEXP; } void initialize_scanner(); #define YY_USER_INIT initialize_scanner(); -#define c() {countlines(yytext, yyleng);} +/* count the number of lines+columns consumed by this token */ +static inline void l() { + int t; + for(t=0;t])*\]*\]\]\> STRING ["](\\[\x00-\xff]|[^\\"\n])*["]|['](\\[\x00-\xff]|[^\\'\n])*['] S [ \n\r\t] MULTILINE_COMMENT [/][*]+([*][^/]|[^/*]|[^*][/]|[\x00-\x1f])*[*]+[/] @@ -439,39 +554,42 @@ REGEXP [/]([^/\n]|\\[/])*[/][a-zA-Z]* %% -{SINGLELINE_COMMENT} {c(); /* single line comment */} -{MULTILINE_COMMENT} {c(); /* multi line comment */} +{SINGLELINE_COMMENT} {l(); /* single line comment */} +{MULTILINE_COMMENT} {l(); /* multi line comment */} [/][*] {syntaxerror("syntax error: unterminated comment", yytext);} -^include{S}+{STRING}{S}*/\n {c();handleInclude(yytext, yyleng, 1);} -^include{S}+[^" \t\r\n][\x20-\xff]*{S}*/\n {c();handleInclude(yytext, yyleng, 0);} -{STRING} {c(); BEGIN(INITIAL);handleString(yytext, yyleng);return T_STRING;} +^include{S}+{STRING}{S}*/\n {l();handleInclude(yytext, yyleng, 1);} +^include{S}+[^" \t\r\n][\x20-\xff]*{S}*/\n {l();handleInclude(yytext, yyleng, 0);} +{STRING} {l(); BEGIN(INITIAL);handleString(yytext, yyleng);return T_STRING;} +{CDATA} {l(); BEGIN(INITIAL);handleCData(yytext, yyleng);return T_STRING;} { -{REGEXP} {c(); BEGIN(INITIAL);return m(T_REGEXP);} +{REGEXP} {c(); BEGIN(INITIAL);return handleregexp();} {HEXWITHSIGN} {c(); BEGIN(INITIAL);return handlehex();} +{HEXFLOATWITHSIGN} {c(); BEGIN(INITIAL);return handlehexfloat();} {INTWITHSIGN} {c(); BEGIN(INITIAL);return handleint();} {FLOATWITHSIGN} {c(); BEGIN(INITIAL);return handlefloat();} } \xef\xbb\xbf {/* utf 8 bom */} -{S} {c();} +{S} {l();} {HEXINT} {c(); BEGIN(INITIAL);return handlehex();} +{HEXFLOAT} {c(); BEGIN(INITIAL);return handlehexfloat();} {INT} {c(); BEGIN(INITIAL);return handleint();} {FLOAT} {c(); BEGIN(INITIAL);return handlefloat();} 3rr0r {/* for debugging: generates a tokenizer-level error */ syntaxerror("3rr0r");} -{NAME}{S}*:{S}*for/{_} {c();handleLabel(yytext, yyleng-3);return T_FOR;} -{NAME}{S}*:{S}*do/{_} {c();handleLabel(yytext, yyleng-2);return T_DO;} -{NAME}{S}*:{S}*while/{_} {c();handleLabel(yytext, yyleng-5);return T_WHILE;} -{NAME}{S}*:{S}*switch/{_} {c();handleLabel(yytext, yyleng-6);return T_SWITCH;} -for {c();avm2_lval.id="";return T_FOR;} -do {c();avm2_lval.id="";return T_DO;} -while {c();avm2_lval.id="";return T_WHILE;} -switch {c();avm2_lval.id="";return T_SWITCH;} +{NAME}{S}*:{S}*for/{_} {l();handleLabel(yytext, yyleng-3);return T_FOR;} +{NAME}{S}*:{S}*do/{_} {l();handleLabel(yytext, yyleng-2);return T_DO;} +{NAME}{S}*:{S}*while/{_} {l();handleLabel(yytext, yyleng-5);return T_WHILE;} +{NAME}{S}*:{S}*switch/{_} {l();handleLabel(yytext, yyleng-6);return T_SWITCH;} +for {c();a3_lval.id="";return T_FOR;} +do {c();a3_lval.id="";return T_DO;} +while {c();a3_lval.id="";return T_WHILE;} +switch {c();a3_lval.id="";return T_SWITCH;} [&][&] {c();BEGIN(REGEXPOK);return m(T_ANDAND);} [|][|] {c();BEGIN(REGEXPOK);return m(T_OROR);} @@ -510,6 +628,7 @@ continue {c();return m(KW_CONTINUE);} override {c();return m(KW_OVERRIDE);} internal {c();return m(KW_INTERNAL);} function {c();return m(KW_FUNCTION);} +finally {c();return m(KW_FINALLY);} default {c();return m(KW_DEFAULT);} package {c();return m(KW_PACKAGE);} private {c();return m(KW_PRIVATE);} @@ -547,12 +666,13 @@ is {c();return m(KW_IS) ;} in {c();return m(KW_IN) ;} if {c();return m(KW_IF) ;} as {c();return m(KW_AS);} -{NAME} {c();BEGIN(INITIAL);return mkid(T_IDENTIFIER);} +{NAME} {c();BEGIN(INITIAL);return handleIdentifier(T_IDENTIFIER);} [+-\/*^~@$!%&\(=\[\]\{\}|?:;,<>] {c();BEGIN(REGEXPOK);return m(yytext[0]);} -[\)\]] {c();BEGIN(INITIAL);return m(yytext[0]);} +[\)\]] {c();BEGIN(INITIAL);return m(yytext[0]);} -. {char c1=yytext[0]; +. {/* ERROR */ + char c1=yytext[0]; char buf[128]; buf[0] = yytext[0]; int t; @@ -571,7 +691,7 @@ as {c();return m(KW_AS);} exit(1); yyterminate(); } -<> {c(); +<> {l(); void*b = leave_file(); if (!b) { yyterminate(); @@ -649,3 +769,4 @@ void initialize_scanner() BEGIN(BEGINNING); } +