Twig_Lexer::lexExpression PHP Method

lexExpression() protected method

protected lexExpression ( )
    protected function lexExpression()
    {
        $match = null;

        // whitespace
        while (preg_match('/\s+/As', $this->code, $match, null, $this->cursor)) {
            $this->moveCursor($match[0]);
            $this->moveLineNo($match[0]);
        }

        // sanity check
        if ($this->cursor >= $this->end) {
            throw new Twig_Error_Syntax('Unexpected end of stream', $this->lineno, $this->filename);
        }

        // first parse operators
        if (preg_match($this->getOperatorRegex(), $this->code, $match, null, $this->cursor)) {
            $this->moveCursor(trim($match[0], ' ()'));

            return new Twig_Token(Twig_Token::OPERATOR_TYPE, trim($match[0], ' ()'), $this->lineno);
        }
        // now names
        else if (preg_match(self::REGEX_NAME, $this->code, $match, null, $this->cursor)) {
            $this->moveCursor($match[0]);

            return new Twig_Token(Twig_Token::NAME_TYPE, $match[0], $this->lineno);
        }
        // then numbers
        else if (preg_match(self::REGEX_NUMBER, $this->code, $match, null, $this->cursor)) {
            $this->moveCursor($match[0]);
            $value = (float)$match[0];
            if ((int)$value === $value) {
                $value = (int)$value;
            }

            return new Twig_Token(Twig_Token::NUMBER_TYPE, $value, $this->lineno);
        }
        // punctuation
        else if (preg_match(self::REGEX_PUNCTUATION, $this->code, $match, null, $this->cursor)) {
            $this->moveCursor($match[0]);
            $this->moveLineNo($match[0]);

            return new Twig_Token(Twig_Token::PUNCTUATION_TYPE, $match[0], $this->lineno);
        }
        // and finally strings
        else if (preg_match(self::REGEX_STRING, $this->code, $match, null, $this->cursor)) {
            $this->moveCursor($match[0]);
            $this->moveLineNo($match[0]);
            $value = stripcslashes(substr($match[0], 1, strlen($match[0]) - 2));

            return new Twig_Token(Twig_Token::STRING_TYPE, $value, $this->lineno);
        }

        // unlexable
        throw new Twig_Error_Syntax(sprintf("Unexpected character '%s'", $this->code[$this->cursor]), $this->lineno, $this->filename);
    }

Usage Example

示例#1
0
文件: Lexer.php 项目: allocine/twigcs
 protected function lexExpression()
 {
     // collect whitespaces and new lines
     if (preg_match('/\\s+/A', $this->code, $match, null, $this->cursor)) {
         $emptyLines = explode("\n", $match[0]);
         foreach ($emptyLines as $line) {
             if (strlen($line) == 0) {
                 $this->pushToken(Token::NEWLINE_TYPE);
             } else {
                 $this->pushToken(Token::WHITESPACE_TYPE, $line);
             }
         }
     }
     parent::lexExpression();
 }