예제 #1
0
파일: lexer.php 프로젝트: betsyzhang/chyrp
 private function lexExpression()
 {
     $match = NULL;
     // skip whitespace
     while (preg_match('/\\s+/A', $this->code, $match, NULL, $this->cursor)) {
         $this->cursor += strlen($match[0]);
         $this->lineno += substr_count($match[0], '\\n');
     }
     // sanity check
     if ($this->cursor >= $this->end) {
         throw new Twig_SyntaxError('unexpected end of stream', $this->lineno, $this->filename);
     }
     // first parse operators
     if (preg_match(self::REGEX_OPERATOR, $this->code, $match, NULL, $this->cursor)) {
         $this->cursor += strlen($match[0]);
         return Twig_Token::Operator($match[0], $this->lineno);
     }
     // now names
     if (preg_match(self::REGEX_NAME, $this->code, $match, NULL, $this->cursor)) {
         $this->cursor += strlen($match[0]);
         return Twig_Token::Name($match[0], $this->lineno);
     } else {
         if (preg_match(self::REGEX_NUMBER, $this->code, $match, NULL, $this->cursor)) {
             $this->cursor += strlen($match[0]);
             $value = (double) $match[0];
             if ((int) $value === $value) {
                 $value = (int) $value;
             }
             return Twig_Token::Number($value, $this->lineno);
         } else {
             if (preg_match(self::REGEX_STRING, $this->code, $match, NULL, $this->cursor)) {
                 $this->cursor += strlen($match[0]);
                 $this->lineno += substr_count($match[0], '\\n');
                 $value = stripcslashes(substr($match[0], 1, strlen($match[0]) - 2));
                 return Twig_Token::String($value, $this->lineno);
             }
         }
     }
     // unlexable
     throw new Twig_SyntaxError("Unexpected character '" . $this->code[$this->cursor] . "'.", $this->lineno, $this->filename);
 }