vendor/twig/twig/src/Lexer.php line 182

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Twig.
  4.  *
  5.  * (c) Fabien Potencier
  6.  * (c) Armin Ronacher
  7.  *
  8.  * For the full copyright and license information, please view the LICENSE
  9.  * file that was distributed with this source code.
  10.  */
  11. namespace Twig;
  12. use Twig\Error\SyntaxError;
  13. /**
  14.  * @author Fabien Potencier <fabien@symfony.com>
  15.  */
  16. class Lexer
  17. {
  18.     private $isInitialized false;
  19.     private $tokens;
  20.     private $code;
  21.     private $cursor;
  22.     private $lineno;
  23.     private $end;
  24.     private $state;
  25.     private $states;
  26.     private $brackets;
  27.     private $env;
  28.     private $source;
  29.     private $options;
  30.     private $regexes;
  31.     private $position;
  32.     private $positions;
  33.     private $currentVarBlockLine;
  34.     public const STATE_DATA 0;
  35.     public const STATE_BLOCK 1;
  36.     public const STATE_VAR 2;
  37.     public const STATE_STRING 3;
  38.     public const STATE_INTERPOLATION 4;
  39.     public const REGEX_NAME '/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/A';
  40.     public const REGEX_STRING '/"([^#"\\\\]*(?:\\\\.[^#"\\\\]*)*)"|\'([^\'\\\\]*(?:\\\\.[^\'\\\\]*)*)\'/As';
  41.     public const REGEX_NUMBER '/(?(DEFINE)
  42.         (?<LNUM>[0-9]+(_[0-9]+)*)               # Integers (with underscores)   123_456
  43.         (?<FRAC>\.(?&LNUM))                     # Fractional part               .456
  44.         (?<EXPONENT>[eE][+-]?(?&LNUM))          # Exponent part                 E+10
  45.         (?<DNUM>(?&LNUM)(?:(?&FRAC))?)          # Decimal number                123_456.456
  46.     )(?:(?&DNUM)(?:(?&EXPONENT))?)              #                               123_456.456E+10
  47.     /Ax';
  48.     
  49.     public const REGEX_DQ_STRING_DELIM '/"/A';
  50.     public const REGEX_DQ_STRING_PART '/[^#"\\\\]*(?:(?:\\\\.|#(?!\{))[^#"\\\\]*)*/As';
  51.     public const REGEX_INLINE_COMMENT '/#[^\n]*/A';
  52.     public const PUNCTUATION '()[]{}?:.,|';
  53.     private const SPECIAL_CHARS = [
  54.         'f' => "\f",
  55.         'n' => "\n",
  56.         'r' => "\r",
  57.         't' => "\t",
  58.         'v' => "\v",
  59.     ];
  60.     public function __construct(Environment $env, array $options = [])
  61.     {
  62.         $this->env $env;
  63.         $this->options array_merge([
  64.             'tag_comment' => ['{#''#}'],
  65.             'tag_block' => ['{%''%}'],
  66.             'tag_variable' => ['{{''}}'],
  67.             'whitespace_trim' => '-',
  68.             'whitespace_line_trim' => '~',
  69.             'whitespace_line_chars' => ' \t\0\x0B',
  70.             'interpolation' => ['#{''}'],
  71.         ], $options);
  72.     }
  73.     private function initialize()
  74.     {
  75.         if ($this->isInitialized) {
  76.             return;
  77.         }
  78.         // when PHP 7.3 is the min version, we will be able to remove the '#' part in preg_quote as it's part of the default
  79.         $this->regexes = [
  80.             // }}
  81.             'lex_var' => '{
  82.                 \s*
  83.                 (?:'.
  84.                     preg_quote($this->options['whitespace_trim'].$this->options['tag_variable'][1], '#').'\s*'// -}}\s*
  85.                     '|'.
  86.                     preg_quote($this->options['whitespace_line_trim'].$this->options['tag_variable'][1], '#').'['.$this->options['whitespace_line_chars'].']*'// ~}}[ \t\0\x0B]*
  87.                     '|'.
  88.                     preg_quote($this->options['tag_variable'][1], '#'). // }}
  89.                 ')
  90.             }Ax',
  91.             // %}
  92.             'lex_block' => '{
  93.                 \s*
  94.                 (?:'.
  95.                     preg_quote($this->options['whitespace_trim'].$this->options['tag_block'][1], '#').'\s*\n?'// -%}\s*\n?
  96.                     '|'.
  97.                     preg_quote($this->options['whitespace_line_trim'].$this->options['tag_block'][1], '#').'['.$this->options['whitespace_line_chars'].']*'// ~%}[ \t\0\x0B]*
  98.                     '|'.
  99.                     preg_quote($this->options['tag_block'][1], '#').'\n?'// %}\n?
  100.                 ')
  101.             }Ax',
  102.             // {% endverbatim %}
  103.             'lex_raw_data' => '{'.
  104.                 preg_quote($this->options['tag_block'][0], '#'). // {%
  105.                 '('.
  106.                     $this->options['whitespace_trim']. // -
  107.                     '|'.
  108.                     $this->options['whitespace_line_trim']. // ~
  109.                 ')?\s*endverbatim\s*'.
  110.                 '(?:'.
  111.                     preg_quote($this->options['whitespace_trim'].$this->options['tag_block'][1], '#').'\s*'// -%}
  112.                     '|'.
  113.                     preg_quote($this->options['whitespace_line_trim'].$this->options['tag_block'][1], '#').'['.$this->options['whitespace_line_chars'].']*'// ~%}[ \t\0\x0B]*
  114.                     '|'.
  115.                     preg_quote($this->options['tag_block'][1], '#'). // %}
  116.                 ')
  117.             }sx',
  118.             'operator' => $this->getOperatorRegex(),
  119.             // #}
  120.             'lex_comment' => '{
  121.                 (?:'.
  122.                     preg_quote($this->options['whitespace_trim'].$this->options['tag_comment'][1], '#').'\s*\n?'// -#}\s*\n?
  123.                     '|'.
  124.                     preg_quote($this->options['whitespace_line_trim'].$this->options['tag_comment'][1], '#').'['.$this->options['whitespace_line_chars'].']*'// ~#}[ \t\0\x0B]*
  125.                     '|'.
  126.                     preg_quote($this->options['tag_comment'][1], '#').'\n?'// #}\n?
  127.                 ')
  128.             }sx',
  129.             // verbatim %}
  130.             'lex_block_raw' => '{
  131.                 \s*verbatim\s*
  132.                 (?:'.
  133.                     preg_quote($this->options['whitespace_trim'].$this->options['tag_block'][1], '#').'\s*'// -%}\s*
  134.                     '|'.
  135.                     preg_quote($this->options['whitespace_line_trim'].$this->options['tag_block'][1], '#').'['.$this->options['whitespace_line_chars'].']*'// ~%}[ \t\0\x0B]*
  136.                     '|'.
  137.                     preg_quote($this->options['tag_block'][1], '#'). // %}
  138.                 ')
  139.             }Asx',
  140.             'lex_block_line' => '{\s*line\s+(\d+)\s*'.preg_quote($this->options['tag_block'][1], '#').'}As',
  141.             // {{ or {% or {#
  142.             'lex_tokens_start' => '{
  143.                 ('.
  144.                     preg_quote($this->options['tag_variable'][0], '#'). // {{
  145.                     '|'.
  146.                     preg_quote($this->options['tag_block'][0], '#'). // {%
  147.                     '|'.
  148.                     preg_quote($this->options['tag_comment'][0], '#'). // {#
  149.                 ')('.
  150.                     preg_quote($this->options['whitespace_trim'], '#'). // -
  151.                     '|'.
  152.                     preg_quote($this->options['whitespace_line_trim'], '#'). // ~
  153.                 ')?
  154.             }sx',
  155.             'interpolation_start' => '{'.preg_quote($this->options['interpolation'][0], '#').'\s*}A',
  156.             'interpolation_end' => '{\s*'.preg_quote($this->options['interpolation'][1], '#').'}A',
  157.         ];
  158.         $this->isInitialized true;
  159.     }
  160.     public function tokenize(Source $source): TokenStream
  161.     {
  162.         $this->initialize();
  163.         $this->source $source;
  164.         $this->code str_replace(["\r\n""\r"], "\n"$source->getCode());
  165.         $this->cursor 0;
  166.         $this->lineno 1;
  167.         $this->end \strlen($this->code);
  168.         $this->tokens = [];
  169.         $this->state self::STATE_DATA;
  170.         $this->states = [];
  171.         $this->brackets = [];
  172.         $this->position = -1;
  173.         // find all token starts in one go
  174.         preg_match_all($this->regexes['lex_tokens_start'], $this->code$matches\PREG_OFFSET_CAPTURE);
  175.         $this->positions $matches;
  176.         while ($this->cursor $this->end) {
  177.             // dispatch to the lexing functions depending
  178.             // on the current state
  179.             switch ($this->state) {
  180.                 case self::STATE_DATA:
  181.                     $this->lexData();
  182.                     break;
  183.                 case self::STATE_BLOCK:
  184.                     $this->lexBlock();
  185.                     break;
  186.                 case self::STATE_VAR:
  187.                     $this->lexVar();
  188.                     break;
  189.                 case self::STATE_STRING:
  190.                     $this->lexString();
  191.                     break;
  192.                 case self::STATE_INTERPOLATION:
  193.                     $this->lexInterpolation();
  194.                     break;
  195.             }
  196.         }
  197.         $this->pushToken(Token::EOF_TYPE);
  198.         if ($this->brackets) {
  199.             [$expect$lineno] = array_pop($this->brackets);
  200.             throw new SyntaxError(\sprintf('Unclosed "%s".'$expect), $lineno$this->source);
  201.         }
  202.         return new TokenStream($this->tokens$this->source);
  203.     }
  204.     private function lexData(): void
  205.     {
  206.         // if no matches are left we return the rest of the template as simple text token
  207.         if ($this->position == \count($this->positions[0]) - 1) {
  208.             $this->pushToken(Token::TEXT_TYPEsubstr($this->code$this->cursor));
  209.             $this->cursor $this->end;
  210.             return;
  211.         }
  212.         // Find the first token after the current cursor
  213.         $position $this->positions[0][++$this->position];
  214.         while ($position[1] < $this->cursor) {
  215.             if ($this->position == \count($this->positions[0]) - 1) {
  216.                 return;
  217.             }
  218.             $position $this->positions[0][++$this->position];
  219.         }
  220.         // push the template text first
  221.         $text $textContent substr($this->code$this->cursor$position[1] - $this->cursor);
  222.         // trim?
  223.         if (isset($this->positions[2][$this->position][0])) {
  224.             if ($this->options['whitespace_trim'] === $this->positions[2][$this->position][0]) {
  225.                 // whitespace_trim detected ({%-, {{- or {#-)
  226.                 $text rtrim($text);
  227.             } elseif ($this->options['whitespace_line_trim'] === $this->positions[2][$this->position][0]) {
  228.                 // whitespace_line_trim detected ({%~, {{~ or {#~)
  229.                 // don't trim \r and \n
  230.                 $text rtrim($text" \t\0\x0B");
  231.             }
  232.         }
  233.         $this->pushToken(Token::TEXT_TYPE$text);
  234.         $this->moveCursor($textContent.$position[0]);
  235.         switch ($this->positions[1][$this->position][0]) {
  236.             case $this->options['tag_comment'][0]:
  237.                 $this->lexComment();
  238.                 break;
  239.             case $this->options['tag_block'][0]:
  240.                 // raw data?
  241.                 if (preg_match($this->regexes['lex_block_raw'], $this->code$match0$this->cursor)) {
  242.                     $this->moveCursor($match[0]);
  243.                     $this->lexRawData();
  244.                 // {% line \d+ %}
  245.                 } elseif (preg_match($this->regexes['lex_block_line'], $this->code$match0$this->cursor)) {
  246.                     $this->moveCursor($match[0]);
  247.                     $this->lineno = (int) $match[1];
  248.                 } else {
  249.                     $this->pushToken(Token::BLOCK_START_TYPE);
  250.                     $this->pushState(self::STATE_BLOCK);
  251.                     $this->currentVarBlockLine $this->lineno;
  252.                 }
  253.                 break;
  254.             case $this->options['tag_variable'][0]:
  255.                 $this->pushToken(Token::VAR_START_TYPE);
  256.                 $this->pushState(self::STATE_VAR);
  257.                 $this->currentVarBlockLine $this->lineno;
  258.                 break;
  259.         }
  260.     }
  261.     private function lexBlock(): void
  262.     {
  263.         if (!$this->brackets && preg_match($this->regexes['lex_block'], $this->code$match0$this->cursor)) {
  264.             $this->pushToken(Token::BLOCK_END_TYPE);
  265.             $this->moveCursor($match[0]);
  266.             $this->popState();
  267.         } else {
  268.             $this->lexExpression();
  269.         }
  270.     }
  271.     private function lexVar(): void
  272.     {
  273.         if (!$this->brackets && preg_match($this->regexes['lex_var'], $this->code$match0$this->cursor)) {
  274.             $this->pushToken(Token::VAR_END_TYPE);
  275.             $this->moveCursor($match[0]);
  276.             $this->popState();
  277.         } else {
  278.             $this->lexExpression();
  279.         }
  280.     }
  281.     private function lexExpression(): void
  282.     {
  283.         // whitespace
  284.         if (preg_match('/\s+/A'$this->code$match0$this->cursor)) {
  285.             $this->moveCursor($match[0]);
  286.             if ($this->cursor >= $this->end) {
  287.                 throw new SyntaxError(\sprintf('Unclosed "%s".'self::STATE_BLOCK === $this->state 'block' 'variable'), $this->currentVarBlockLine$this->source);
  288.             }
  289.         }
  290.         // spread operator
  291.         if ('.' === $this->code[$this->cursor] && ($this->cursor $this->end) && '.' === $this->code[$this->cursor 1] && '.' === $this->code[$this->cursor 2]) {
  292.             $this->pushToken(Token::SPREAD_TYPE'...');
  293.             $this->moveCursor('...');
  294.         }
  295.         // arrow function
  296.         elseif ('=' === $this->code[$this->cursor] && ($this->cursor $this->end) && '>' === $this->code[$this->cursor 1]) {
  297.             $this->pushToken(Token::ARROW_TYPE'=>');
  298.             $this->moveCursor('=>');
  299.         }
  300.         // operators
  301.         elseif (preg_match($this->regexes['operator'], $this->code$match0$this->cursor)) {
  302.             $this->pushToken(Token::OPERATOR_TYPEpreg_replace('/\s+/'' '$match[0]));
  303.             $this->moveCursor($match[0]);
  304.         }
  305.         // names
  306.         elseif (preg_match(self::REGEX_NAME$this->code$match0$this->cursor)) {
  307.             $this->pushToken(Token::NAME_TYPE$match[0]);
  308.             $this->moveCursor($match[0]);
  309.         }
  310.         // numbers
  311.         elseif (preg_match(self::REGEX_NUMBER$this->code$match0$this->cursor)) {
  312.             $this->pushToken(Token::NUMBER_TYPEstr_replace('_'''$match[0]));
  313.             $this->moveCursor($match[0]);
  314.         }
  315.         // punctuation
  316.         elseif (str_contains(self::PUNCTUATION$this->code[$this->cursor])) {
  317.             // opening bracket
  318.             if (str_contains('([{'$this->code[$this->cursor])) {
  319.                 $this->brackets[] = [$this->code[$this->cursor], $this->lineno];
  320.             }
  321.             // closing bracket
  322.             elseif (str_contains(')]}'$this->code[$this->cursor])) {
  323.                 if (!$this->brackets) {
  324.                     throw new SyntaxError(\sprintf('Unexpected "%s".'$this->code[$this->cursor]), $this->lineno$this->source);
  325.                 }
  326.                 [$expect$lineno] = array_pop($this->brackets);
  327.                 if ($this->code[$this->cursor] != strtr($expect'([{'')]}')) {
  328.                     throw new SyntaxError(\sprintf('Unclosed "%s".'$expect), $lineno$this->source);
  329.                 }
  330.             }
  331.             $this->pushToken(Token::PUNCTUATION_TYPE$this->code[$this->cursor]);
  332.             ++$this->cursor;
  333.         }
  334.         // strings
  335.         elseif (preg_match(self::REGEX_STRING$this->code$match0$this->cursor)) {
  336.             $this->pushToken(Token::STRING_TYPE$this->stripcslashes(substr($match[0], 1, -1), substr($match[0], 01)));
  337.             $this->moveCursor($match[0]);
  338.         }
  339.         // opening double quoted string
  340.         elseif (preg_match(self::REGEX_DQ_STRING_DELIM$this->code$match0$this->cursor)) {
  341.             $this->brackets[] = ['"'$this->lineno];
  342.             $this->pushState(self::STATE_STRING);
  343.             $this->moveCursor($match[0]);
  344.         }
  345.         // inline comment
  346.         elseif (preg_match(self::REGEX_INLINE_COMMENT$this->code$match0$this->cursor)) {
  347.             $this->moveCursor($match[0]);
  348.         }
  349.         // unlexable
  350.         else {
  351.             throw new SyntaxError(\sprintf('Unexpected character "%s".'$this->code[$this->cursor]), $this->lineno$this->source);
  352.         }
  353.     }
  354.     private function stripcslashes(string $strstring $quoteType): string
  355.     {
  356.         $result '';
  357.         $length \strlen($str);
  358.         $i 0;
  359.         while ($i $length) {
  360.             if (false === $pos strpos($str'\\'$i)) {
  361.                 $result .= substr($str$i);
  362.                 break;
  363.             }
  364.             $result .= substr($str$i$pos $i);
  365.             $i $pos 1;
  366.             if ($i >= $length) {
  367.                 $result .= '\\';
  368.                 break;
  369.             }
  370.             $nextChar $str[$i];
  371.             if (isset(self::SPECIAL_CHARS[$nextChar])) {
  372.                 $result .= self::SPECIAL_CHARS[$nextChar];
  373.             } elseif ('\\' === $nextChar) {
  374.                 $result .= $nextChar;
  375.             } elseif ("'" === $nextChar || '"' === $nextChar) {
  376.                 if ($nextChar !== $quoteType) {
  377.                     trigger_deprecation('twig/twig''3.12''Character "%s" should not be escaped; the "\" character is ignored in Twig 3 but will not be in Twig 4. Please remove the extra "\" character at position %d in "%s" at line %d.'$nextChar$i 1$this->source->getName(), $this->lineno);
  378.                 }
  379.                 $result .= $nextChar;
  380.             } elseif ('#' === $nextChar && $i $length && '{' === $str[$i 1]) {
  381.                 $result .= '#{';
  382.                 ++$i;
  383.             } elseif ('x' === $nextChar && $i $length && ctype_xdigit($str[$i 1])) {
  384.                 $hex $str[++$i];
  385.                 if ($i $length && ctype_xdigit($str[$i 1])) {
  386.                     $hex .= $str[++$i];
  387.                 }
  388.                 $result .= \chr(hexdec($hex));
  389.             } elseif (ctype_digit($nextChar) && $nextChar '8') {
  390.                 $octal $nextChar;
  391.                 while ($i $length && ctype_digit($str[$i 1]) && $str[$i 1] < '8' && \strlen($octal) < 3) {
  392.                     $octal .= $str[++$i];
  393.                 }
  394.                 $result .= \chr(octdec($octal));
  395.             } else {
  396.                 trigger_deprecation('twig/twig''3.12''Character "%s" should not be escaped; the "\" character is ignored in Twig 3 but will not be in Twig 4. Please remove the extra "\" character at position %d in "%s" at line %d.'$nextChar$i 1$this->source->getName(), $this->lineno);
  397.                 $result .= $nextChar;
  398.             }
  399.             ++$i;
  400.         }
  401.         return $result;
  402.     }
  403.     private function lexRawData(): void
  404.     {
  405.         if (!preg_match($this->regexes['lex_raw_data'], $this->code$match\PREG_OFFSET_CAPTURE$this->cursor)) {
  406.             throw new SyntaxError('Unexpected end of file: Unclosed "verbatim" block.'$this->lineno$this->source);
  407.         }
  408.         $text substr($this->code$this->cursor$match[0][1] - $this->cursor);
  409.         $this->moveCursor($text.$match[0][0]);
  410.         // trim?
  411.         if (isset($match[1][0])) {
  412.             if ($this->options['whitespace_trim'] === $match[1][0]) {
  413.                 // whitespace_trim detected ({%-, {{- or {#-)
  414.                 $text rtrim($text);
  415.             } else {
  416.                 // whitespace_line_trim detected ({%~, {{~ or {#~)
  417.                 // don't trim \r and \n
  418.                 $text rtrim($text" \t\0\x0B");
  419.             }
  420.         }
  421.         $this->pushToken(Token::TEXT_TYPE$text);
  422.     }
  423.     private function lexComment(): void
  424.     {
  425.         if (!preg_match($this->regexes['lex_comment'], $this->code$match\PREG_OFFSET_CAPTURE$this->cursor)) {
  426.             throw new SyntaxError('Unclosed comment.'$this->lineno$this->source);
  427.         }
  428.         $this->moveCursor(substr($this->code$this->cursor$match[0][1] - $this->cursor).$match[0][0]);
  429.     }
  430.     private function lexString(): void
  431.     {
  432.         if (preg_match($this->regexes['interpolation_start'], $this->code$match0$this->cursor)) {
  433.             $this->brackets[] = [$this->options['interpolation'][0], $this->lineno];
  434.             $this->pushToken(Token::INTERPOLATION_START_TYPE);
  435.             $this->moveCursor($match[0]);
  436.             $this->pushState(self::STATE_INTERPOLATION);
  437.         } elseif (preg_match(self::REGEX_DQ_STRING_PART$this->code$match0$this->cursor) && '' !== $match[0]) {
  438.             $this->pushToken(Token::STRING_TYPE$this->stripcslashes($match[0], '"'));
  439.             $this->moveCursor($match[0]);
  440.         } elseif (preg_match(self::REGEX_DQ_STRING_DELIM$this->code$match0$this->cursor)) {
  441.             [$expect$lineno] = array_pop($this->brackets);
  442.             if ('"' != $this->code[$this->cursor]) {
  443.                 throw new SyntaxError(\sprintf('Unclosed "%s".'$expect), $lineno$this->source);
  444.             }
  445.             $this->popState();
  446.             ++$this->cursor;
  447.         } else {
  448.             // unlexable
  449.             throw new SyntaxError(\sprintf('Unexpected character "%s".'$this->code[$this->cursor]), $this->lineno$this->source);
  450.         }
  451.     }
  452.     private function lexInterpolation(): void
  453.     {
  454.         $bracket end($this->brackets);
  455.         if ($this->options['interpolation'][0] === $bracket[0] && preg_match($this->regexes['interpolation_end'], $this->code$match0$this->cursor)) {
  456.             array_pop($this->brackets);
  457.             $this->pushToken(Token::INTERPOLATION_END_TYPE);
  458.             $this->moveCursor($match[0]);
  459.             $this->popState();
  460.         } else {
  461.             $this->lexExpression();
  462.         }
  463.     }
  464.     private function pushToken($type$value ''): void
  465.     {
  466.         // do not push empty text tokens
  467.         if (Token::TEXT_TYPE === $type && '' === $value) {
  468.             return;
  469.         }
  470.         $this->tokens[] = new Token($type$value$this->lineno);
  471.     }
  472.     private function moveCursor($text): void
  473.     {
  474.         $this->cursor += \strlen($text);
  475.         $this->lineno += substr_count($text"\n");
  476.     }
  477.     private function getOperatorRegex(): string
  478.     {
  479.         $operators array_merge(
  480.             ['='],
  481.             array_keys($this->env->getUnaryOperators()),
  482.             array_keys($this->env->getBinaryOperators())
  483.         );
  484.         $operators array_combine($operatorsarray_map('strlen'$operators));
  485.         arsort($operators);
  486.         $regex = [];
  487.         foreach ($operators as $operator => $length) {
  488.             // an operator that ends with a character must be followed by
  489.             // a whitespace, a parenthesis, an opening map [ or sequence {
  490.             $r preg_quote($operator'/');
  491.             if (ctype_alpha($operator[$length 1])) {
  492.                 $r .= '(?=[\s()\[{])';
  493.             }
  494.             // an operator that begins with a character must not have a dot or pipe before
  495.             if (ctype_alpha($operator[0])) {
  496.                 $r '(?<![\.\|])'.$r;
  497.             }
  498.             // an operator with a space can be any amount of whitespaces
  499.             $r preg_replace('/\s+/''\s+'$r);
  500.             $regex[] = $r;
  501.         }
  502.         return '/'.implode('|'$regex).'/A';
  503.     }
  504.     private function pushState($state): void
  505.     {
  506.         $this->states[] = $this->state;
  507.         $this->state $state;
  508.     }
  509.     private function popState(): void
  510.     {
  511.         if (=== \count($this->states)) {
  512.             throw new \LogicException('Cannot pop state without a previous state.');
  513.         }
  514.         $this->state array_pop($this->states);
  515.     }
  516. }