Twig_Lexer::nextToken PHP Method

nextToken() protected method

Parses the next token and returns it.
protected nextToken ( )
    protected function nextToken()
    {
        // do we have tokens pushed back? get one
        if (!empty($this->pushedBack)) {
            return array_shift($this->pushedBack);
        }

        // have we reached the end of the code?
        if ($this->cursor >= $this->end) {
            return new Twig_Token(Twig_Token::EOF_TYPE, '', $this->lineno);
        }

        // otherwise dispatch to the lexing functions depending
        // on our current position in the code.
        switch ($this->position) {
            case self::POSITION_DATA:
                $tokens = $this->lexData();
                break;

            case self::POSITION_BLOCK:
                $tokens = $this->lexBlock();
                break;

            case self::POSITION_VAR:
                $tokens = $this->lexVar();
                break;
        }

        // if the return value is not an array it's a token
        if (!is_array($tokens)) {
            return $tokens;
        }
        // empty array, call again
        else if (empty($tokens)) {
            return $this->nextToken();
        }
        // if we have multiple items we push them to the buffer
        else if (count($tokens) > 1) {
            $first = array_shift($tokens);
            $this->pushedBack = $tokens;

            return $first;
        }
        // otherwise return the first item of the array.
        else {
            return $tokens[0];
        }
    }