'Unclosed "%s".', self::STATE_BLOCK === $this->state ? 'block' : 'variable'), $this->currentVarBlockLine, $this->source); } } // arrow function if ('=' === $this->code[$this->cursor] && '>' === $this->code[$this->cursor + 1]) { $this->pushToken(Token::ARROW_TYPE, '=>'); $this->moveCursor('=>'); } elseif (\preg_match($this->regexes['operator'], $this->code, $match, 0, $this->cursor)) { $this->pushToken( 8, \preg_replace('/\\s+/', ' ', $match[0]) ); $this->moveCursor($match[0]); } elseif (\preg_match(self::REGEX_NAME, $this->code, $match, 0, $this->cursor)) { $this->pushToken( 5, $match[0] ); $this->moveCursor($match[0]); } elseif (\preg_match(self::REGEX_NUMBER, $this->code, $match, 0, $this->cursor)) { $number = (float) $match[0]; // floats if (\ctype_digit($match[0]) && $number <= \PHP_INT_MAX) { $number = (int) $match[0]; // integers lower than the maximum } $this->pushToken( 6, $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[] = [$this->code[$this->cursor], $this->lineno]; } elseif (\false !== \strpos(')]}', $this->code[$this->cursor])) { if (empty($this->brackets)) { throw new SyntaxError(\sprintf('Unexpected "%s".', $this->code[$this->cursor]), $this->lineno, $this->source); } list($expect, $lineno) = \array_pop($this->brackets); if ($this->code[$this->cursor] != \strtr($expect, '([{', ')]}')) { throw new SyntaxError(\sprintf('Unclosed "%s".', $expect), $lineno, $this->source); } } $this->pushToken( 9, $this->code[$this->cursor] ); ++$this->cursor; } elseif (\preg_match(self::REGEX_STRING, $this->code, $match, 0, $this->cursor)) { $this->pushToken( 7, \stripcslashes(\substr($match[0], 1, -1)) ); $this->moveCursor($match[0]); } elseif (\preg_match(self::REGEX_DQ_STRING_DELIM, $this->code, $match, 0, $this->cursor)) { $this->brackets[] = ['"', $this->lineno]; $this->pushState(self::STATE_STRING); $this->moveCursor($match[0]); } else { throw new SyntaxError(\sprintf('Unexpected character "%s".', $this->code[$this->cursor]), $this->lineno, $this->source); } } private function lexRawData() : void { if (!\preg_match($this->regexes['lex_raw_data'], $this->code, $match, \PREG_OFFSET_CAPTURE, $this->cursor)) { throw new SyntaxError('Unexpected end of file: Unclosed "verbatim" block.', $this->lineno, $this->source); } $text = \substr($this->code, $this->cursor, $match[0][1] - $this->cursor); $this->moveCursor($text . $match[0][0]); // trim? if (isset($match[1][0])) { if ($this->options['whitespace_trim'] === $match[1][0]) { // whitespace_trim detected ({%-, {{- or {#-) $text = \rtrim($text); } else { // whitespace_line_trim detected ({%~, {{~ or {#~) // don't trim \r and \n $text = \rtrim($text, " \t\x00\v"); } } $this->pushToken( 0, $text ); } private function lexComment() : void { if (!\preg_match($this->regexes['lex_comment'], $this->code, $match, \PREG_OFFSET_CAPTURE, $this->cursor)) { throw new SyntaxError('Unclosed comment.', $this->lineno, $this->source); } $this->moveCursor(\substr($this->code, $this->cursor, $match[0][1] - $this->cursor) . $match[0][0]); } private function lexString() : void { if (\preg_match($this->regexes['interpolation_start'], $this->code, $match, 0, $this->cursor)) { $this->brackets[] = [$this->options['interpolation'][0], $this->lineno]; $this->pushToken( 10 ); $this->moveCursor($match[0]); $this->pushState(self::STATE_INTERPOLATION); } elseif (\preg_match(self::REGEX_DQ_STRING_PART, $this->code, $match, 0, $this->cursor) && \strlen($match[0]) > 0) { $this->pushToken( 7, \stripcslashes($match[0]) ); $this->moveCursor($match[0]); } elseif (\preg_match(self::REGEX_DQ_STRING_DELIM, $this->code, $match, 0, $this->cursor)) { list($expect, $lineno) = \array_pop($this->brackets); if ('"' != $this->code[$this->cursor]) { throw new SyntaxError(\sprintf('Unclosed "%s".', $expect), $lineno, $this->source); } $this->popState(); ++$this->cursor; } else { // unlexable throw new SyntaxError(\sprintf('Unexpected character "%s".', $this->code[$this->cursor]), $this->lineno, $this->source); } } private function lexInterpolation() : void { $bracket = \end($this->brackets); if ($this->options['interpolation'][0] === $bracket[0] && \preg_match($this->regexes['interpolation_end'], $this->code, $match, 0, $this->cursor)) { \array_pop($this->brackets); $this->pushToken( 11 ); $this->moveCursor($match[0]); $this->popState(); } else { $this->lexExpression(); } } private function pushToken($type, $value = '') : void { // do not push empty text tokens if (0 === $type && '' === $value) { return; } $this->tokens[] = new Token($type, $value, $this->lineno); } private function moveCursor($text) : void { $this->cursor += \strlen($text); $this->lineno += \substr_count($text, "\n"); } private function getOperatorRegex() : string { $operators = \array_merge(['='], \array_keys($this->env->getUnaryOperators()), \array_keys($this->env->getBinaryOperators())); $operators = \array_combine($operators, \array_map('strlen', $operators)); \arsort($operators); $regex = []; foreach ($operators as $operator => $length) { // an operator that ends with a character must be followed by // a whitespace, a parenthesis, an opening map [ or sequence { $r = \preg_quote($operator, '/'); if (\ctype_alpha($operator[$length - 1])) { $r .= '(?=[\\s()\\[{])'; } // an operator that begins with a character must not have a dot or pipe before if (\ctype_alpha($operator[0])) { $r = '(?states[] = $this->state; $this->state = $state; } private function popState() : void { if (0 === \count($this->states)) { throw new \LogicException('Cannot pop state without a previous state.'); } $this->state = \array_pop($this->states); } }