Skip to content

Commit

Permalink
ParametersNode & DefineNode parses parameters as ParameterNode[] with…
Browse files Browse the repository at this point in the history
… full type
  • Loading branch information
dg committed May 18, 2022
1 parent 987bcd8 commit 8742292
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/Latte/Compiler/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
namespace Latte\Compiler;

use Latte;
use Latte\Compiler\Nodes\Php\Expression;
use Latte\Compiler\Nodes\Php\ExpressionNode;
use Latte\Compiler\Nodes\Php\ParameterNode;
use Latte\Compiler\Nodes\Php\Scalar;


Expand All @@ -24,7 +24,7 @@ final class Block
public string $content;
public string $escaping;

/** @var Expression\AssignNode[] */
/** @var ParameterNode[] */
public array $parameters = [];


Expand Down
29 changes: 29 additions & 0 deletions src/Latte/Compiler/Nodes/Php/SuperiorTypeNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/**
* This file is part of the Latte (https://latte.nette.org)
* Copyright (c) 2008 David Grudl (https://davidgrudl.com)
*/

declare(strict_types=1);

namespace Latte\Compiler\Nodes\Php;

use Latte\Compiler\Position;
use Latte\Compiler\PrintContext;


class SuperiorTypeNode extends ComplexTypeNode
{
public function __construct(
public string $type,
public ?Position $position = null,
) {
}


public function print(PrintContext $context): string
{
throw new \LogicException('Cannot directly print SuperiorTypeNode');
}
}
4 changes: 2 additions & 2 deletions src/Latte/Compiler/TagParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public function tryConsumeModifier(string ...$modifiers): ?Token
}


public function parseType(): ?string
public function parseType(): ?Node\SuperiorTypeNode
{
$kind = [
Token::Php_Identifier, Token::Php_Constant, Token::Php_Ellipsis, Token::Php_Array, Token::Php_Integer,
Expand All @@ -113,7 +113,7 @@ public function parseType(): ?string
$res .= $token->text;
}

return $res;
return $res ? new Node\SuperiorTypeNode($res) : null;
}


Expand Down
9 changes: 5 additions & 4 deletions src/Latte/Compiler/TemplateGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,20 +127,21 @@ private function buildParams(string $body, array $params, string $cont, PrintCon
return $body;
}

foreach ($params as $i => &$param) {
$param = $context->format(
$res = [];
foreach ($params as $i => $param) {
$res[] = $context->format(
'%node = %raw[%dump] ?? %raw[%dump] ?? %node;',
$param->var,
$cont,
$i,
$cont,
$param->var->name,
$param->expr,
$param->default,
);
}

$extract = $params
? implode('', $params) . 'unset($ʟ_args);'
? implode('', $res) . 'unset($ʟ_args);'
: "extract($cont);" . (str_contains($cont, '$this') ? '' : "unset($cont);");
return $extract . "\n\n" . $body;
}
Expand Down
7 changes: 4 additions & 3 deletions src/Latte/Essential/Nodes/DefineNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Latte\Compiler\Nodes\AreaNode;
use Latte\Compiler\Nodes\Php\Expression\AssignNode;
use Latte\Compiler\Nodes\Php\Expression\VariableNode;
use Latte\Compiler\Nodes\Php\ParameterNode;
use Latte\Compiler\Nodes\Php\Scalar;
use Latte\Compiler\Nodes\StatementNode;
use Latte\Compiler\PrintContext;
Expand Down Expand Up @@ -64,18 +65,18 @@ private static function parseParameters(Tag $tag): array
$stream = $tag->parser->stream;
$params = [];
while (!$stream->is(Token::End)) {
$tag->parser->parseType();
$type = $tag->parser->parseType();

$save = $stream->getIndex();
$expr = $stream->is(Token::Php_Variable) ? $tag->parser->parseExpression() : null;
if ($expr instanceof VariableNode && is_string($expr->name)) {
$params[] = new AssignNode($expr, new Scalar\NullNode);
$params[] = new ParameterNode($expr, new Scalar\NullNode, $type);
} elseif (
$expr instanceof AssignNode
&& $expr->var instanceof VariableNode
&& is_string($expr->var->name)
) {
$params[] = $expr;
$params[] = new ParameterNode($expr->var, $expr->expr, $type);
} else {
$stream->seek($save);
$stream->throwUnexpectedException(addendum: ' in ' . $tag->getNotation());
Expand Down
9 changes: 5 additions & 4 deletions src/Latte/Essential/Nodes/ParametersNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Latte\CompileException;
use Latte\Compiler\Nodes\Php\Expression\AssignNode;
use Latte\Compiler\Nodes\Php\Expression\VariableNode;
use Latte\Compiler\Nodes\Php\ParameterNode;
use Latte\Compiler\Nodes\Php\Scalar\NullNode;
use Latte\Compiler\Nodes\StatementNode;
use Latte\Compiler\PrintContext;
Expand All @@ -24,7 +25,7 @@
*/
class ParametersNode extends StatementNode
{
/** @var AssignNode[] */
/** @var ParameterNode[] */
public array $parameters = [];


Expand All @@ -45,18 +46,18 @@ private static function parseParameters(Tag $tag): array
$stream = $tag->parser->stream;
$params = [];
do {
$tag->parser->parseType();
$type = $tag->parser->parseType();

$save = $stream->getIndex();
$expr = $stream->is(Token::Php_Variable) ? $tag->parser->parseExpression() : null;
if ($expr instanceof VariableNode && is_string($expr->name)) {
$params[] = new AssignNode($expr, new NullNode);
$params[] = new ParameterNode($expr, new NullNode, $type);
} elseif (
$expr instanceof AssignNode
&& $expr->var instanceof VariableNode
&& is_string($expr->var->name)
) {
$params[] = $expr;
$params[] = new ParameterNode($expr->var, $expr->expr, $type);
} else {
$stream->seek($save);
$stream->throwUnexpectedException(addendum: ' in ' . $tag->getNotation());
Expand Down
1 change: 1 addition & 0 deletions tests/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ public function export(?Node $node = null)
$node instanceof Nodes\Html\ElementNode,
$node instanceof Nodes\Php\IdentifierNode => 'name: ' . $node->name,
$node instanceof Nodes\Php\NameNode => 'parts: ' . PhpHelpers::dump($node->parts),
$node instanceof Nodes\Php\SuperiorTypeNode => PhpHelpers::dump($node->type),
$node instanceof Nodes\Php\Scalar\FloatNode,
$node instanceof Nodes\Php\Scalar\EncapsedStringPartNode,
$node instanceof Nodes\Php\Scalar\IntegerNode,
Expand Down
8 changes: 5 additions & 3 deletions tests/tags/define.nodes.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ Assert::match(<<<'XX'
Define:
String:
value: test
Assign:
Parameter:
SuperiorType:
'int'
Variable:
name: a
Null:
Assign:
Parameter:
Variable:
name: b
New:
Expand All @@ -31,4 +33,4 @@ Assert::match(<<<'XX'
Fragment:
Text:
content: '...'
XX, exportTraversing('{define test, $a, $b = new Foo}...{/define}'));
XX, exportTraversing('{define test, int $a, $b = new Foo}...{/define}'));
6 changes: 4 additions & 2 deletions tests/tags/parameters.nodes.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ Assert::match(<<<'XX'
Template:
Fragment:
Parameters:
Assign:
Parameter:
Variable:
name: b
Null:
Assign:
Parameter:
SuperiorType:
'int'
Variable:
name: a
Integer:
Expand Down

0 comments on commit 8742292

Please sign in to comment.