From ad5f99efad91bc3653972609baa6170d45438533 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Fri, 26 Nov 2021 23:24:18 +0100 Subject: [PATCH] isNext() a isPrev() called with SIGNIFICANT parameter to ignore leading and trailing spaces --- src/Latte/Compiler/MacroTokens.php | 6 +++++- src/Latte/Compiler/PhpWriter.php | 18 +++++++++++------- src/Latte/Macros/BlockMacros.php | 4 ++-- src/Latte/Macros/CoreMacros.php | 6 +++--- src/Latte/Macros/MacroSet.php | 2 +- 5 files changed, 22 insertions(+), 14 deletions(-) diff --git a/src/Latte/Compiler/MacroTokens.php b/src/Latte/Compiler/MacroTokens.php index 42b5c93df..efa19de6f 100644 --- a/src/Latte/Compiler/MacroTokens.php +++ b/src/Latte/Compiler/MacroTokens.php @@ -26,6 +26,10 @@ class MacroTokens extends TokenIterator T_KEYWORD = 8, T_CHAR = 9; + public const + SIGNIFICANT = [self::T_SYMBOL, self::T_NUMBER, self::T_VARIABLE, self::T_STRING, self::T_CAST, self::T_KEYWORD, self::T_CHAR], + NON_SIGNIFICANT = [self::T_COMMENT, self::T_WHITESPACE]; + /** @var int */ public $depth = 0; @@ -39,7 +43,7 @@ class MacroTokens extends TokenIterator public function __construct($input = []) { parent::__construct(is_array($input) ? $input : $this->parse($input)); - $this->ignored = [self::T_COMMENT, self::T_WHITESPACE]; + $this->ignored = self::NON_SIGNIFICANT; } diff --git a/src/Latte/Compiler/PhpWriter.php b/src/Latte/Compiler/PhpWriter.php index f3c428468..28af2fe68 100644 --- a/src/Latte/Compiler/PhpWriter.php +++ b/src/Latte/Compiler/PhpWriter.php @@ -316,7 +316,11 @@ public function shortTernaryPass(MacroTokens $tokens): MacroTokens $res = new MacroTokens; $inTernary = []; while ($tokens->nextToken()) { - if ($tokens->isCurrent('?') && $tokens->isNext() && !$tokens->isNext(',', ')', ']', '|', '[')) { + if ( + $tokens->isCurrent('?') + && $tokens->isNext(...$tokens::SIGNIFICANT) + && !$tokens->isNext(',', ')', ']', '|', '[') + ) { $inTernary[] = $tokens->depth; } elseif ($tokens->isCurrent(':')) { @@ -361,7 +365,7 @@ public function optionalChainingPass(MacroTokens $tokens): MacroTokens do { if ($tokens->nextToken('?')) { if ( // is it ternary operator? - $tokens->isNext() + $tokens->isNext(...$tokens::SIGNIFICANT) && ( !$tokens->isNext($tokens::T_CHAR) || $tokens->isNext('(', '[', '{', ':', '!', '@', '\\') @@ -476,9 +480,9 @@ public function quotingPass(MacroTokens $tokens): MacroTokens while ($tokens->nextToken()) { $res->append( $tokens->isCurrent($tokens::T_SYMBOL) - && (!$tokens->isPrev() || $tokens->isPrev(',', '(', '[', '=>', ':', '?', '.', '<', '>', '<=', '>=', '===', '!==', '==', '!=', '<>', '&&', '||', '=', 'and', 'or', 'xor', '??')) - && (!$tokens->isNext() || $tokens->isNext(',', ';', ')', ']', '=>', ':', '?', '.', '<', '>', '<=', '>=', '===', '!==', '==', '!=', '<>', '&&', '||', 'and', 'or', 'xor', '??')) - && !((!$tokens->isPrev() || $tokens->isPrev('(', ',')) && $tokens->isNext(':')) + && (!$tokens->isPrev(...$tokens::SIGNIFICANT) || $tokens->isPrev(',', '(', '[', '=>', ':', '?', '.', '<', '>', '<=', '>=', '===', '!==', '==', '!=', '<>', '&&', '||', '=', 'and', 'or', 'xor', '??')) + && (!$tokens->isNext(...$tokens::SIGNIFICANT) || $tokens->isNext(',', ';', ')', ']', '=>', ':', '?', '.', '<', '>', '<=', '>=', '===', '!==', '==', '!=', '<>', '&&', '||', 'and', 'or', 'xor', '??')) + && !((!$tokens->isPrev(...$tokens::SIGNIFICANT) || $tokens->isPrev('(', ',')) && $tokens->isNext(':')) && !preg_match('#^[A-Z_][A-Z0-9_]{2,}$#', $tokens->currentValue()) && !($tokens->isCurrent('default') && $tokens->isNext('=>')) ? "'" . $tokens->currentValue() . "'" @@ -499,7 +503,7 @@ public function namedArgumentsPass(MacroTokens $tokens): MacroTokens if ( $tokens->depth === 0 && $tokens->isCurrent($tokens::T_SYMBOL) - && (!$tokens->isPrev() || $tokens->isPrev(',')) + && (!$tokens->isPrev(...$tokens::SIGNIFICANT) || $tokens->isPrev(',')) && $tokens->isNext(':') ) { $res->append("'" . $tokens->currentValue() . "' =>"); @@ -559,7 +563,7 @@ public function inOperatorPass(MacroTokens $tokens): MacroTokens } if ($depth === $tokens->depth && $tokens->nextValue('in') && ($arr[] = $tokens->nextToken('['))) { - while ($tokens->isNext()) { + while ($tokens->isNext(...$tokens::SIGNIFICANT)) { $arr[] = $tokens->nextToken(); if ($tokens->isCurrent(']') && $tokens->depth === $depth) { $new = array_merge($tokens->parse('in_array('), $expr, $tokens->parse(', '), $arr, $tokens->parse(', true)')); diff --git a/src/Latte/Macros/BlockMacros.php b/src/Latte/Macros/BlockMacros.php index bf8c27579..764a5e872 100644 --- a/src/Latte/Macros/BlockMacros.php +++ b/src/Latte/Macros/BlockMacros.php @@ -333,7 +333,7 @@ public function macroDefine(MacroNode $node, PhpWriter $writer): string $tokens = $node->tokenizer; $params = []; - while ($tokens->isNext()) { + while ($tokens->isNext(...$tokens::SIGNIFICANT)) { if ($tokens->nextToken($tokens::T_SYMBOL, '?', 'null', '\\')) { // type $tokens->nextAll($tokens::T_SYMBOL, '\\', '|', '[', ']', 'null'); } @@ -348,7 +348,7 @@ public function macroDefine(MacroNode $node, PhpWriter $writer): string substr($param, 1), $default ); - if ($tokens->isNext()) { + if ($tokens->isNext(...$tokens::SIGNIFICANT)) { $tokens->consumeValue(','); } } diff --git a/src/Latte/Macros/CoreMacros.php b/src/Latte/Macros/CoreMacros.php index b0e588319..8050658b9 100644 --- a/src/Latte/Macros/CoreMacros.php +++ b/src/Latte/Macros/CoreMacros.php @@ -687,7 +687,7 @@ public function macroVar(MacroNode $node, PhpWriter $writer): string && $tokens->isCurrent($tokens::T_SYMBOL) && ( $tokens->isNext(',', '=>', '=') - || !$tokens->isNext() + || !$tokens->isNext(...$tokens::SIGNIFICANT) ) ) { trigger_error("Inside tag {{$node->name} {$node->args}} should be '{$tokens->currentValue()}' replaced with '\${$tokens->currentValue()}'", E_USER_DEPRECATED); @@ -812,7 +812,7 @@ public function macroParameters(MacroNode $node, PhpWriter $writer): void $tokens = $node->tokenizer; $params = []; - while ($tokens->isNext()) { + while ($tokens->isNext(...$tokens::SIGNIFICANT)) { if ($tokens->nextToken($tokens::T_SYMBOL, '?', 'null', '\\')) { // type $tokens->nextAll($tokens::T_SYMBOL, '\\', '|', '[', ']', 'null'); } @@ -827,7 +827,7 @@ public function macroParameters(MacroNode $node, PhpWriter $writer): void substr($param, 1), $default ); - if ($tokens->isNext()) { + if ($tokens->isNext(...$tokens::SIGNIFICANT)) { $tokens->consumeValue(','); } } diff --git a/src/Latte/Macros/MacroSet.php b/src/Latte/Macros/MacroSet.php index 7966cf677..601253071 100644 --- a/src/Latte/Macros/MacroSet.php +++ b/src/Latte/Macros/MacroSet.php @@ -169,7 +169,7 @@ public function getCompiler(): Latte\Compiler /** @internal */ protected function checkExtraArgs(MacroNode $node): void { - if ($node->tokenizer->isNext()) { + if ($node->tokenizer->isNext(...$node->tokenizer::SIGNIFICANT)) { $args = Latte\Runtime\Filters::truncate($node->tokenizer->joinAll(), 20); throw new CompileException("Unexpected arguments '$args' in " . $node->getNotation()); }