home *** CD-ROM | disk | FTP | other *** search
/ Computer Active 2010 August / CA08.iso / Multimedija / shufflr.air / ShufflrClient.swf / scripts / com / adobe / serialization / json / JSONTokenizer.as < prev    next >
Encoding:
Text File  |  2010-06-23  |  10.4 KB  |  359 lines

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