home *** CD-ROM | disk | FTP | other *** search
/ 404 Jogos / CLJG.iso / Puzzle / Clusterz / Clusterz.swf / scripts / it / gotoandplay / smartfoxserver / json / JSONTokenizer.as < prev    next >
Encoding:
Text File  |  2008-09-12  |  8.8 KB  |  321 lines

  1. package it.gotoandplay.smartfoxserver.json
  2. {
  3.    public class JSONTokenizer
  4.    {
  5.        
  6.       
  7.       private var loc:int;
  8.       
  9.       private var ch:String;
  10.       
  11.       private var obj:Object;
  12.       
  13.       private var jsonString:String;
  14.       
  15.       public function JSONTokenizer(param1:String)
  16.       {
  17.          super();
  18.          jsonString = param1;
  19.          loc = 0;
  20.          nextChar();
  21.       }
  22.       
  23.       private function skipComments() : void
  24.       {
  25.          if(ch == "/")
  26.          {
  27.             nextChar();
  28.             switch(ch)
  29.             {
  30.                case "/":
  31.                   do
  32.                   {
  33.                      nextChar();
  34.                   }
  35.                   while(ch != "\n" && ch != "");
  36.                   
  37.                   nextChar();
  38.                   break;
  39.                case "*":
  40.                   nextChar();
  41.                   while(true)
  42.                   {
  43.                      if(ch == "*")
  44.                      {
  45.                         nextChar();
  46.                         if(ch == "/")
  47.                         {
  48.                            break;
  49.                         }
  50.                      }
  51.                      else
  52.                      {
  53.                         nextChar();
  54.                      }
  55.                      if(ch == "")
  56.                      {
  57.                         parseError("Multi-line comment not closed");
  58.                      }
  59.                   }
  60.                   nextChar();
  61.                   break;
  62.                default:
  63.                   parseError("Unexpected " + ch + " encountered (expecting \'/\' or \'*\' )");
  64.             }
  65.          }
  66.       }
  67.       
  68.       private function isDigit(param1:String) : Boolean
  69.       {
  70.          return param1 >= "0" && param1 <= "9";
  71.       }
  72.       
  73.       private function readNumber() : JSONToken
  74.       {
  75.          var _loc1_:JSONToken = null;
  76.          var _loc2_:* = null;
  77.          var _loc3_:Number = NaN;
  78.          _loc1_ = new JSONToken();
  79.          _loc1_.type = JSONTokenType.NUMBER;
  80.          _loc2_ = "";
  81.          if(ch == "-")
  82.          {
  83.             _loc2_ += "-";
  84.             nextChar();
  85.          }
  86.          while(isDigit(ch))
  87.          {
  88.             _loc2_ += ch;
  89.             nextChar();
  90.          }
  91.          if(ch == ".")
  92.          {
  93.             _loc2_ += ".";
  94.             nextChar();
  95.             while(isDigit(ch))
  96.             {
  97.                _loc2_ += ch;
  98.                nextChar();
  99.             }
  100.          }
  101.          _loc3_ = Number(_loc2_);
  102.          if(isFinite(_loc3_))
  103.          {
  104.             _loc1_.value = _loc3_;
  105.             return _loc1_;
  106.          }
  107.          parseError("Number " + _loc3_ + " is not valid!");
  108.          return null;
  109.       }
  110.       
  111.       private function nextChar() : String
  112.       {
  113.          return ch = jsonString.charAt(loc++);
  114.       }
  115.       
  116.       private function isHexDigit(param1:String) : Boolean
  117.       {
  118.          var _loc2_:String = null;
  119.          _loc2_ = param1.toUpperCase();
  120.          return isDigit(param1) || _loc2_ >= "A" && _loc2_ <= "F";
  121.       }
  122.       
  123.       public function getNextToken() : JSONToken
  124.       {
  125.          var _loc1_:JSONToken = null;
  126.          var _loc2_:String = null;
  127.          var _loc3_:String = null;
  128.          var _loc4_:String = null;
  129.          _loc1_ = new JSONToken();
  130.          skipIgnored();
  131.          switch(ch)
  132.          {
  133.             case "{":
  134.                _loc1_.type = JSONTokenType.LEFT_BRACE;
  135.                _loc1_.value = "{";
  136.                nextChar();
  137.                break;
  138.             case "}":
  139.                _loc1_.type = JSONTokenType.RIGHT_BRACE;
  140.                _loc1_.value = "}";
  141.                nextChar();
  142.                break;
  143.             case "[":
  144.                _loc1_.type = JSONTokenType.LEFT_BRACKET;
  145.                _loc1_.value = "[";
  146.                nextChar();
  147.                break;
  148.             case "]":
  149.                _loc1_.type = JSONTokenType.RIGHT_BRACKET;
  150.                _loc1_.value = "]";
  151.                nextChar();
  152.                break;
  153.             case ",":
  154.                _loc1_.type = JSONTokenType.COMMA;
  155.                _loc1_.value = ",";
  156.                nextChar();
  157.                break;
  158.             case ":":
  159.                _loc1_.type = JSONTokenType.COLON;
  160.                _loc1_.value = ":";
  161.                nextChar();
  162.                break;
  163.             case "t":
  164.                _loc2_ = "t" + nextChar() + nextChar() + nextChar();
  165.                if(_loc2_ == "true")
  166.                {
  167.                   _loc1_.type = JSONTokenType.TRUE;
  168.                   _loc1_.value = true;
  169.                   nextChar();
  170.                }
  171.                else
  172.                {
  173.                   parseError("Expecting \'true\' but found " + _loc2_);
  174.                }
  175.                break;
  176.             case "f":
  177.                _loc3_ = "f" + nextChar() + nextChar() + nextChar() + nextChar();
  178.                if(_loc3_ == "false")
  179.                {
  180.                   _loc1_.type = JSONTokenType.FALSE;
  181.                   _loc1_.value = false;
  182.                   nextChar();
  183.                }
  184.                else
  185.                {
  186.                   parseError("Expecting \'false\' but found " + _loc3_);
  187.                }
  188.                break;
  189.             case "n":
  190.                if((_loc4_ = "n" + nextChar() + nextChar() + nextChar()) == "null")
  191.                {
  192.                   _loc1_.type = JSONTokenType.NULL;
  193.                   _loc1_.value = null;
  194.                   nextChar();
  195.                }
  196.                else
  197.                {
  198.                   parseError("Expecting \'null\' but found " + _loc4_);
  199.                }
  200.                break;
  201.             case "\"":
  202.                _loc1_ = readString();
  203.                break;
  204.             default:
  205.                if(isDigit(ch) || ch == "-")
  206.                {
  207.                   _loc1_ = readNumber();
  208.                }
  209.                else
  210.                {
  211.                   if(ch == "")
  212.                   {
  213.                      return null;
  214.                   }
  215.                   parseError("Unexpected " + ch + " encountered");
  216.                }
  217.          }
  218.          return _loc1_;
  219.       }
  220.       
  221.       private function skipWhite() : void
  222.       {
  223.          while(isSpace(ch))
  224.          {
  225.             nextChar();
  226.          }
  227.       }
  228.       
  229.       public function parseError(param1:String) : void
  230.       {
  231.          throw new JSONParseError(param1,loc,jsonString);
  232.       }
  233.       
  234.       private function skipIgnored() : void
  235.       {
  236.          skipWhite();
  237.          skipComments();
  238.          skipWhite();
  239.       }
  240.       
  241.       private function isSpace(param1:String) : Boolean
  242.       {
  243.          return param1 == " " || param1 == "\t";
  244.       }
  245.       
  246.       private function readString() : JSONToken
  247.       {
  248.          var _loc1_:JSONToken = null;
  249.          var _loc2_:* = null;
  250.          var _loc3_:String = null;
  251.          var _loc4_:int = 0;
  252.          _loc1_ = new JSONToken();
  253.          _loc1_.type = JSONTokenType.STRING;
  254.          _loc2_ = "";
  255.          nextChar();
  256.          while(ch != "\"" && ch != "")
  257.          {
  258.             if(ch == "\\")
  259.             {
  260.                nextChar();
  261.                switch(ch)
  262.                {
  263.                   case "\"":
  264.                      _loc2_ += "\"";
  265.                      break;
  266.                   case "/":
  267.                      _loc2_ += "/";
  268.                      break;
  269.                   case "\\":
  270.                      _loc2_ += "\\";
  271.                      break;
  272.                   case "b":
  273.                      _loc2_ += "\b";
  274.                      break;
  275.                   case "f":
  276.                      _loc2_ += "\f";
  277.                      break;
  278.                   case "n":
  279.                      _loc2_ += "\n";
  280.                      break;
  281.                   case "r":
  282.                      _loc2_ += "\r";
  283.                      break;
  284.                   case "t":
  285.                      _loc2_ += "\t";
  286.                      break;
  287.                   case "u":
  288.                      _loc3_ = "";
  289.                      _loc4_ = 0;
  290.                      while(_loc4_ < 4)
  291.                      {
  292.                         if(!isHexDigit(nextChar()))
  293.                         {
  294.                            parseError(" Excepted a hex digit, but found: " + ch);
  295.                         }
  296.                         _loc3_ += ch;
  297.                         _loc4_++;
  298.                      }
  299.                      _loc2_ += String.fromCharCode(parseInt(_loc3_,16));
  300.                      break;
  301.                   default:
  302.                      _loc2_ += "\\" + ch;
  303.                }
  304.             }
  305.             else
  306.             {
  307.                _loc2_ += ch;
  308.             }
  309.             nextChar();
  310.          }
  311.          if(ch == "")
  312.          {
  313.             parseError("Unterminated string literal");
  314.          }
  315.          nextChar();
  316.          _loc1_.value = _loc2_;
  317.          return _loc1_;
  318.       }
  319.    }
  320. }
  321.