You are here

function JSON::getNextToken in Drupal Most Popular 7

Tokenizer

Return to the Parser the next valid token and the type of the token. If the tokenizer fails it returns false.

@access private

Parameters

string $e Text to extract token: @param integer $i Start position to search next token @param integer $state Variable to get the token type @return string|bool Token in string or false on error.

2 calls to JSON::getNextToken()
JSON::_unserialize in modules/mostpopular_disqus/disqusapi/json.php
UnSerialize
JSON::_unserializeArray in modules/mostpopular_disqus/disqusapi/json.php
JSON Array Parser

File

modules/mostpopular_disqus/disqusapi/json.php, line 213

Class

JSON
JSON

Code

function getNextToken($e, &$i, &$state) {
  $state = IN_NOWHERE;
  $end = -1;
  $start = -1;
  while ($i < strlen($e) && $end == -1) {
    switch ($e[$i]) {

      /* objects */
      case "{":
      case "[":
        $_tag = $e[$i];
        $_endtag = $_tag == "{" ? "}" : "]";
        if ($state == IN_NOWHERE) {
          $start = $i + 1;
          switch ($state) {
            case IN_NOWHERE:
              $aux = 1;

              /* for loop objects */
              $state = $_tag == "{" ? IN_OBJECT : IN_ARRAY;
              break;
            default:
              break 2;
          }
          while (++$i && $i < strlen($e) && $aux != 0) {
            switch ($e[$i]) {
              case $_tag:
                $aux++;
                break;
              case $_endtag:
                $aux--;
                break;
            }
          }
          $end = $i - 1;
        }
        break;
      case '"':
      case "'":
        $state = IN_STRING;
        $buf = "";
        while (++$i && $i < strlen($e) && $e[$i] != '"') {
          if ($e[$i] == "\\") {
            $i++;
          }
          $buf .= $e[$i];
        }
        $i++;
        return eval('return "' . str_replace('"', '\\"', $buf) . '";');
        break;
      case ":":
        $state = IN_ASSIGN;
        $end = 1;
        break;
      case "n":
        if (substr($e, $i, 4) == "null") {
          $i = $i + 4;
          $state = IN_ATOMIC;
          return NULL;
        }
        else {
          break 2;
        }

      /* exit from switch and while */
      case "t":
        if (substr($e, $i, 4) == "true") {
          $state = IN_ATOMIC;
          $i = $i + 4;
          return true;
        }
        else {
          break 2;
        }

        /* exit from switch and while */
        break;
      case "f":
        if (substr($e, $i, 5) == "false") {
          $state = IN_ATOMIC;
          $i = $i + 5;
          return false;
        }
        else {
          break 2;
        }

        /* exit from switch and while */
        break;
      case ",":
        $state = IN_ENDSTMT;
        $end = 1;
        break;
      case " ":
      case "\t":
      case "\r":
      case "\n":
        break;
      case "+":
      case "-":
      case 0:
      case 1:
      case 2:
      case 3:
      case 4:
      case 5:
      case 6:
      case 7:
      case 8:
      case 9:
      case '.':
        $state = IN_ATOMIC;
        $start = (int) $i;
        if ($e[$i] == "-" || $e[$i] == "+") {
          $i++;
        }
        for (; $i < strlen($e) && (is_numeric($e[$i]) || $e[$i] == "." || strtolower($e[$i]) == "e"); $i++) {
          $n = $i + 1 < strlen($e) ? $e[$i + 1] : "";
          $a = strtolower($e[$i]);
          if ($a == "e" && ($n == "+" || $n == "-")) {
            $i++;
          }
          else {
            if ($a == "e") {
              $this->error = true;
            }
          }
        }
        $end = $i;
        break 2;

      /* break while too */
      default:
        $this->error = true;
    }
    $i++;
  }
  return $start == -1 || $end == -1 ? false : substr($e, $start, $end - $start);
}