finished ast implementation
[swftools.git] / lib / as3 / tokenizer.lex
index e103b25..127379a 100644 (file)
 #include <stdio.h>
 #include <stdarg.h>
 #include "../utf8.h"
+#include "common.h"
 #include "tokenizer.h"
 #include "files.h"
 
-int as3_pass = 0;
-int as3_verbosity = 1;
 unsigned int as3_tokencount = 0;
 
-void as3_error(const char*format, ...)
-{
-    char buf[1024];
-    int l;
-    va_list arglist;
-    if(as3_verbosity<0)
-        exit(1);
-    va_start(arglist, format);
-    vsprintf(buf, format, arglist);
-    va_end(arglist);
-    fprintf(stderr, "%s:%d:%d: error: %s\n", current_filename_short, current_line, current_column, buf);
-    fflush(stderr);
-    exit(1);
-}
-void as3_warning(const char*format, ...)
-{
-    char buf[1024];
-    int l;
-    va_list arglist;
-    if(as3_verbosity<1)
-        return;
-    va_start(arglist, format);
-    vsprintf(buf, format, arglist);
-    va_end(arglist);
-    fprintf(stderr, "%s:%d:%d: warning: %s\n", current_filename_short, current_line, current_column, buf);
-    fflush(stderr);
-}
-void as3_softwarning(const char*format, ...)
-{
-    char buf[1024];
-    int l;
-    va_list arglist;
-    if(as3_verbosity<2)
-       return;
-    va_start(arglist, format);
-    vsprintf(buf, format, arglist);
-    va_end(arglist);
-    fprintf(stderr, "%s:%d:%d: warning: %s\n", current_filename_short, current_line, current_column, buf);
-    fflush(stderr);
-}
 static void dbg(const char*format, ...)
 {
     char buf[1024];
@@ -295,12 +254,6 @@ static void handleCData(char*s, int len)
 
 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;
@@ -310,7 +263,6 @@ static void handleString(char*s, int len)
         s++;len-=2;
     }
     else syntaxerror("String incorrectly terminated");
-
     
     a3_lval.str = string_unescape(s, len);
 }
@@ -339,22 +291,7 @@ static char*nrbuf()
 static inline int setint(int v)
 {
     a3_lval.number_int = v;
-    if(v>-128)
-        return T_BYTE;
-    else if(v>=-32768)
-        return T_SHORT;
-    else
-        return T_INT;
-}
-static inline int setuint(unsigned int v)
-{
-    a3_lval.number_uint = v;
-    if(v<128)
-        return T_BYTE;
-    else if(v<32768)
-        return T_SHORT;
-    else
-        return T_UINT;
+    return T_INT;
 }
 static inline int setfloat(double v)
 {
@@ -374,7 +311,9 @@ static inline int handleint()
     char*s = nrbuf();
     char l = (yytext[0]=='-');
 
-    char*max = l?"1073741824":"2147483647";
+    //char*max = l?"1073741824":"2147483647";
+    char*max = l?"2147483648":"2147483647";
+
     if(yyleng-l>10) {
         as3_softwarning("integer overflow: %s (converted to Number)", s);
         return handlefloat();
@@ -400,7 +339,7 @@ static inline int handleint()
             v*=10;
             v+=yytext[t]-'0';
         }
-        return setuint(v);
+        return setint(v);
     }
 }
 
@@ -449,21 +388,21 @@ static inline int handlehex()
         else if((c>='a' && c<='f') || (c>='A' && c<='F'))
             v|=(c&0x0f)+9;
     }
-    if(l && v>1073741824) {
+    if(l && v>=0x80000000) {
         char*s = nrbuf();
-        as3_softwarning("signed integer overflow: %s (converted to Number)", s);
+        as3_softwarning("integer overflow: %s (converted to Number)", s);
         return setfloat(v);
     }
-    if(!l && v>2147483647) {
+    if(!l && v>0x7fffffff) {
         char*s = nrbuf();
-        as3_softwarning("unsigned integer overflow: %s (converted to Number)", s);
+        as3_softwarning("integer overflow: %s (converted to Number)", s);
         return setfloat(v);
     }
 
     if(l==3) {
         return setint(-(int)v);
     } else {
-        return setuint(v);
+        return setint(v);
     }
 }
 
@@ -523,14 +462,18 @@ static inline void c() {
     current_column+=yyleng;
 }
 
-static trie_t*namespaces = 0;
-void tokenizer_register_namespace(const char*id)
+trie_t*active_namespaces = 0;
+/*void tokenizer_register_namespace(const char*id)
 {
-    trie_put(&namespaces, id);
+    trie_put(namespaces, id, 0);
 }
+void tokenizer_unregister_namespace(const char*id)
+{
+    trie_remove(namespaces, id);
+}*/
 static inline tokenizer_is_namespace(const char*id)
 {
-    return trie_lookup(namespaces, id);
+    return trie_contains(active_namespaces, id);
 }
 
 static inline int handleIdentifier()
@@ -605,6 +548,7 @@ REGEXP   [/]([^/\n]|\\[/])*[/][a-zA-Z]*
 {HEXFLOAT}/{_}               {c(); BEGIN(INITIAL);return handlehexfloat();}
 {INT}/{_}                    {c(); BEGIN(INITIAL);return handleint();}
 {FLOAT}/{_}                  {c(); BEGIN(INITIAL);return handlefloat();}
+NaN                          {c(); BEGIN(INITIAL);return m(KW_NAN);}
 
 3rr0r                        {/* for debugging: generates a tokenizer-level error */
                               syntaxerror("3rr0r");}
@@ -635,6 +579,7 @@ switch                       {c();BEGIN(INITIAL);a3_lval.id="";return T_SWITCH;}
 [%][=]                       {c();BEGIN(REGEXPOK);return m(T_MODBY);}
 [*][=]                       {c();BEGIN(REGEXPOK);return m(T_MULBY);}
 [|][=]                       {c();BEGIN(REGEXPOK);return m(T_ORBY);}
+[&][=]                       {c();BEGIN(REGEXPOK);return m(T_ANDBY);}
 [>][>][=]                    {c();BEGIN(REGEXPOK);return m(T_SHRBY);}
 [<][<][=]                    {c();BEGIN(REGEXPOK);return m(T_SHLBY);}
 [>][>][>][=]                 {c();BEGIN(REGEXPOK);return m(T_USHRBY);}
@@ -761,7 +706,6 @@ char*token2string(enum yytokentype nr, YYSTYPE v)
     }
     else if(nr==T_INT)     return "<int>";
     else if(nr==T_UINT)     return "<uint>";
-    else if(nr==T_BYTE)     return "<byte>";
     else if(nr==T_FLOAT)     return "<float>";
     else if(nr==T_EOF)        return "***END***";
     else if(nr==T_GE)         return ">=";