You are here

protected function Twig_Lexer::lexExpression in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/twig/twig/lib/Twig/Lexer.php \Twig_Lexer::lexExpression()
3 calls to Twig_Lexer::lexExpression()
Twig_Lexer::lexBlock in vendor/twig/twig/lib/Twig/Lexer.php
Twig_Lexer::lexInterpolation in vendor/twig/twig/lib/Twig/Lexer.php
Twig_Lexer::lexVar in vendor/twig/twig/lib/Twig/Lexer.php

File

vendor/twig/twig/lib/Twig/Lexer.php, line 220

Class

Twig_Lexer
Lexes a template string.

Code

protected function lexExpression() {

  // whitespace
  if (preg_match('/\\s+/A', $this->code, $match, null, $this->cursor)) {
    $this
      ->moveCursor($match[0]);
    if ($this->cursor >= $this->end) {
      throw new Twig_Error_Syntax(sprintf('Unclosed "%s".', $this->state === self::STATE_BLOCK ? 'block' : 'variable'), $this->currentVarBlockLine, $this->filename);
    }
  }

  // operators
  if (preg_match($this->regexes['operator'], $this->code, $match, null, $this->cursor)) {
    $this
      ->pushToken(Twig_Token::OPERATOR_TYPE, preg_replace('/\\s+/', ' ', $match[0]));
    $this
      ->moveCursor($match[0]);
  }
  elseif (preg_match(self::REGEX_NAME, $this->code, $match, null, $this->cursor)) {
    $this
      ->pushToken(Twig_Token::NAME_TYPE, $match[0]);
    $this
      ->moveCursor($match[0]);
  }
  elseif (preg_match(self::REGEX_NUMBER, $this->code, $match, null, $this->cursor)) {
    $number = (double) $match[0];

    // floats
    if (ctype_digit($match[0]) && $number <= PHP_INT_MAX) {
      $number = (int) $match[0];

      // integers lower than the maximum
    }
    $this
      ->pushToken(Twig_Token::NUMBER_TYPE, $number);
    $this
      ->moveCursor($match[0]);
  }
  elseif (false !== strpos(self::PUNCTUATION, $this->code[$this->cursor])) {

    // opening bracket
    if (false !== strpos('([{', $this->code[$this->cursor])) {
      $this->brackets[] = array(
        $this->code[$this->cursor],
        $this->lineno,
      );
    }
    elseif (false !== strpos(')]}', $this->code[$this->cursor])) {
      if (empty($this->brackets)) {
        throw new Twig_Error_Syntax(sprintf('Unexpected "%s".', $this->code[$this->cursor]), $this->lineno, $this->filename);
      }
      list($expect, $lineno) = array_pop($this->brackets);
      if ($this->code[$this->cursor] != strtr($expect, '([{', ')]}')) {
        throw new Twig_Error_Syntax(sprintf('Unclosed "%s".', $expect), $lineno, $this->filename);
      }
    }
    $this
      ->pushToken(Twig_Token::PUNCTUATION_TYPE, $this->code[$this->cursor]);
    ++$this->cursor;
  }
  elseif (preg_match(self::REGEX_STRING, $this->code, $match, null, $this->cursor)) {
    $this
      ->pushToken(Twig_Token::STRING_TYPE, stripcslashes(substr($match[0], 1, -1)));
    $this
      ->moveCursor($match[0]);
  }
  elseif (preg_match(self::REGEX_DQ_STRING_DELIM, $this->code, $match, null, $this->cursor)) {
    $this->brackets[] = array(
      '"',
      $this->lineno,
    );
    $this
      ->pushState(self::STATE_STRING);
    $this
      ->moveCursor($match[0]);
  }
  else {
    throw new Twig_Error_Syntax(sprintf('Unexpected character "%s".', $this->code[$this->cursor]), $this->lineno, $this->filename);
  }
}