small optimizations, improved regexp handling
[swftools.git] / lib / as3 / tokenizer.lex
index f26396a..79cdf8a 100644 (file)
 #include "tokenizer.h"
 #include "files.h"
 
-static void countlines(char*text, int len) {
-    int t;
-    for(t=0;t<len;t++) {
-       if(text[t]=='\n') {
-           current_line++;
-           current_column=0;
-       } else {
-           current_column++;
-       }
-    }
-}
-
 static int verbose = 1;
 static void dbg(const char*format, ...)
 {
@@ -80,6 +68,7 @@ void syntaxerror(const char*format, ...)
 }
 void warning(const char*format, ...)
 {
+    return;
     char buf[1024];
     int l;
     va_list arglist;
@@ -306,6 +295,11 @@ static inline int setuint(unsigned int v)
     else
         return T_UINT;
 }
+static inline int setfloat(double v)
+{
+    avm2_lval.number_float = v;
+    return T_FLOAT;
+}
 
 static inline int handlefloat()
 {
@@ -320,13 +314,17 @@ static inline int handleint()
     char l = (yytext[0]=='-');
 
     char*max = l?"1073741824":"2147483647";
-    if(yyleng-l>10)
-        syntaxerror("integer overflow");
+    if(yyleng-l>10) {
+        warning("integer overflow: %s", s);
+        return handlefloat();
+    }
     if(yyleng-l==10) {
         int t;
         for(t=0;t<yyleng-l;t++) {
-            if(yytext[l+t]>max[t])
-                syntaxerror("integer overflow %s > %s", s+l,max);
+            if(yytext[l+t]>max[t]) {
+                warning("integer overflow: %s", s);
+                return handlefloat();
+            }
             else if(yytext[l+t]<max[t])
                 break;
         }
@@ -349,8 +347,11 @@ static inline int handlehex()
 {
     char l = (yytext[0]=='-')+2;
 
-    if(yyleng-l>8)
-        syntaxerror("integer overflow");
+    if(yyleng-l>8) {
+        char*s = nrbuf();
+        syntaxerror("integer overflow %s", s);
+    }
+
     int t;
     unsigned int v = 0;
     for(t=l;t<yyleng;t++) {
@@ -362,10 +363,16 @@ static inline int handlehex()
                 c>='A' && c<='F')
             v|=(c&0x0f)+9;
     }
-    if(l && v>1073741824)
-        syntaxerror("signed integer overflow");
-    if(!l && v>2147483647)
-        syntaxerror("unsigned integer overflow");
+    if(l && v>1073741824) {
+        char*s = nrbuf();
+        warning("signed integer overflow: %s", s);
+        return setfloat(v);
+    }
+    if(!l && v>2147483647) {
+        char*s = nrbuf();
+        warning("unsigned integer overflow: %s", s);
+        return setfloat(v);
+    }
 
     if(l==3) {
         return setint(-(int)v);
@@ -379,7 +386,7 @@ 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);
@@ -388,10 +395,47 @@ void handleLabel(char*text, int len)
     avm2_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;
+        }
+    }
+    avm2_lval.regexp.pattern = s;
+    if(t==len) {
+        avm2_lval.regexp.options = 0;
+    } else {
+        avm2_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<yyleng;t++) {
+       if(yytext[t]=='\n') {
+           current_line++;
+           current_column=0;
+       } else {
+           current_column++;
+       }
+    }
+}
+/* count the number of columns consumed by this token */
+static inline void c() {
+    current_column+=yyleng;
+}
 
 //Boolean                      {c();return m(KW_BOOLEAN);}
 //int                          {c();return m(KW_INT);}
@@ -423,23 +467,23 @@ 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;}
 
 <BEGINNING,REGEXPOK>{
-{REGEXP}                     {c(); BEGIN(INITIAL);return m(T_REGEXP);} 
+{REGEXP}                     {c(); BEGIN(INITIAL);return handleregexp();} 
 {HEXWITHSIGN}                {c(); BEGIN(INITIAL);return handlehex();}
 {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();}
 {INT}                        {c(); BEGIN(INITIAL);return handleint();}
@@ -448,10 +492,10 @@ REGEXP   [/]([^/\n]|\\[/])*[/][a-zA-Z]*
 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;}
+{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();avm2_lval.id="";return T_FOR;}
 do                           {c();avm2_lval.id="";return T_DO;}
 while                        {c();avm2_lval.id="";return T_WHILE;}
@@ -472,6 +516,7 @@ switch                       {c();avm2_lval.id="";return T_SWITCH;}
 [/][=]                       {c();return m(T_DIVBY);}
 [%][=]                       {c();return m(T_MODBY);}
 [*][=]                       {c();return m(T_MULBY);}
+[|][=]                       {c();return m(T_ORBY);}
 [>][>][=]                    {c();return m(T_SHRBY);}
 [<][<][=]                    {c();return m(T_SHLBY);}
 [>][>][>][=]                 {c();return m(T_USHRBY);}
@@ -505,6 +550,7 @@ native                       {c();return m(KW_NATIVE);}
 static                       {c();return m(KW_STATIC);}
 import                       {c();return m(KW_IMPORT);}
 typeof                       {c();return m(KW_TYPEOF);}
+throw                        {c();return m(KW_THROW);}
 class                        {c();return m(KW_CLASS);}
 const                        {c();return m(KW_CONST);}
 catch                        {c();return m(KW_CATCH);}
@@ -518,6 +564,7 @@ true                         {c();return m(KW_TRUE);}
 null                         {c();return m(KW_NULL);}
 else                         {c();return m(KW_ELSE);}
 case                         {c();return m(KW_CASE);}
+with                         {c();return m(KW_WITH);}
 use                          {c();return m(KW_USE);}
 new                          {c();return m(KW_NEW);}
 get                          {c();return m(KW_GET);}
@@ -531,7 +578,7 @@ as                           {c();return m(KW_AS);}
 {NAME}                       {c();BEGIN(INITIAL);return mkid(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];
                               char buf[128];
@@ -552,7 +599,7 @@ as                           {c();return m(KW_AS);}
                              exit(1);
                              yyterminate();
                             }
-<<EOF>>                             {c();
+<<EOF>>                             {l();
                               void*b = leave_file();
                              if (!b) {
                                 yyterminate();