Skip to content

Commit

Permalink
isNext() a isPrev() called with SIGNIFICANT parameter to ignore leadi…
Browse files Browse the repository at this point in the history
…ng and trailing spaces
  • Loading branch information
dg committed Nov 26, 2021
1 parent 227c86e commit ad5f99e
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 14 deletions.
6 changes: 5 additions & 1 deletion src/Latte/Compiler/MacroTokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}


Expand Down
18 changes: 11 additions & 7 deletions src/Latte/Compiler/PhpWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(':')) {
Expand Down Expand Up @@ -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('(', '[', '{', ':', '!', '@', '\\')
Expand Down Expand Up @@ -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() . "'"
Expand All @@ -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() . "' =>");
Expand Down Expand Up @@ -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)'));
Expand Down
4 changes: 2 additions & 2 deletions src/Latte/Macros/BlockMacros.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand All @@ -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(',');
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/Latte/Macros/CoreMacros.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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');
}
Expand All @@ -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(',');
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Latte/Macros/MacroSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down

0 comments on commit ad5f99e

Please sign in to comment.