parseArguments example


final class EmbedTokenParser extends IncludeTokenParser
{
    public function parse(Token $token): Node
    {
        $stream = $this->parser->getStream();

        $parent = $this->parser->getExpressionParser()->parseExpression();

        list($variables$only$ignoreMissing) = $this->parseArguments();

        $parentToken = $fakeParentToken = new Token(/* Token::STRING_TYPE */ 7, '__parent__', $token->getLine());
        if ($parent instanceof ConstantExpression) {
            $parentToken = new Token(/* Token::STRING_TYPE */ 7, $parent->getAttribute('value')$token->getLine());
        } elseif ($parent instanceof NameExpression) {
            $parentToken = new Token(/* Token::NAME_TYPE */ 5, $parent->getAttribute('name')$token->getLine());
        }

        // inject a fake parent to make the parent() function work         $stream->injectTokens([
            new Token(/* Token::BLOCK_START_TYPE */ 1, '', $token->getLine()),
            
break;
            }
        }

        return $node;
    }

    public function getFunctionNode($name$line)
    {
        switch ($name) {
            case 'parent':
                $this->parseArguments();
                if (!\count($this->parser->getBlockStack())) {
                    throw new SyntaxError('Calling "parent" outside a block is forbidden.', $line$this->parser->getStream()->getSourceContext());
                }

                if (!$this->parser->getParent() && !$this->parser->hasTraits()) {
                    throw new SyntaxError('Calling "parent" on a template that does not extend nor "use" another template is forbidden.', $line$this->parser->getStream()->getSourceContext());
                }

                return new ParentExpression($this->parser->peekBlockStack()$line);
            case 'block':
                $args = $this->parseArguments();
                

class IncludeTokenParser extends AbstractTokenParser
{
    public function parse(Token $token): Node
    {
        $expr = $this->parser->getExpressionParser()->parseExpression();

        list($variables$only$ignoreMissing) = $this->parseArguments();

        return new IncludeNode($expr$variables$only$ignoreMissing$token->getLine()$this->getTag());
    }

    protected function parseArguments()
    {
        $stream = $this->parser->getStream();

        $ignoreMissing = false;
        if ($stream->nextIf(/* Token::NAME_TYPE */ 5, 'ignore')) {
            $stream->expect(/* Token::NAME_TYPE */ 5, 'missing');

            
public function __construct(private readonly TemplateFinder $finder)
    {
    }

    /** * @return Node */
    public function parse(Token $token)
    {
        $expr = $this->parser->getExpressionParser()->parseExpression();

        [$variables$only$ignoreMissing] = $this->parseArguments();

        // resolves parent template         if ($expr->hasAttribute('value')) {
            // set pointer to next value (contains the template file name)             $parent = $this->finder->find($expr->getAttribute('value')$ignoreMissing);

            $expr->setAttribute('value', $parent);

            return new IncludeNode($expr$variables$only$ignoreMissing$token->getLine()$this->getTag());
        }

        
if ($optionalChain) {
                    $this->error(
                        "Optional chain can't appear in tagged template expressions"
                    );
                }
                $valid = true;
                $properties[] = array(
                    "type"=> "template",
                    "info" => $property,
                    "optional" => $optional
                );
            } elseif (($args = $this->parseArguments()) !== null) {
                $valid = true;
                $properties[] = array(
                    "type"=> "args",
                    "info" => array($args$this->scanner->getPosition()),
                    "optional" => $optional
                );
            } else {
                break;
            }
        }
        
        
case 'null':
                    case 'NULL':
                        return new Node\ConstantNode(null);

                    default:
                        if ('(' === $this->stream->current->value) {
                            if (false === isset($this->functions[$token->value])) {
                                throw new SyntaxError(sprintf('The function "%s" does not exist.', $token->value)$token->cursor, $this->stream->getExpression()$token->value, array_keys($this->functions));
                            }

                            $node = new Node\FunctionNode($token->value, $this->parseArguments());
                        } else {
                            if (!$this->lint || \is_array($this->names)) {
                                if (!\in_array($token->value, $this->names, true)) {
                                    throw new SyntaxError(sprintf('Variable "%s" is not valid.', $token->value)$token->cursor, $this->stream->getExpression()$token->value, $this->names);
                                }

                                // is the name used in the compiled code different                                 // from the name used in the expression?                                 if (\is_int($name = array_search($token->value, $this->names))) {
                                    $name = $token->value;
                                }
                            }
                continue;
            }

            /** @var Deprecated|null $deprecated */
            $deprecated = $docBlock->getTagsByName('deprecated')[0] ?? null;

            $methods[] = [
                'title' => $method->getName() . '()',
                'summary' => $docBlock->getSummary(),
                'description' => $docBlock->getDescription()->render(),
                'deprecated' => $deprecated ? (string) $deprecated : null,
                'arguments' => $this->parseArguments($method$docBlock$scriptServices),
                'return' => $this->parseReturn($method$docBlock$scriptServices),
                'examples' => $this->parseExamples($method$docBlock),
            ];
        }

        return $methods;
    }

    /** * @param list<class-string<object>> $scriptServices * * @return list<array<string, mixed>> */

final class MacroTokenParser extends AbstractTokenParser
{
    public function parse(Token $token): Node
    {
        $lineno = $token->getLine();
        $stream = $this->parser->getStream();
        $name = $stream->expect(/* Token::NAME_TYPE */ 5)->getValue();

        $arguments = $this->parser->getExpressionParser()->parseArguments(true, true);

        $stream->expect(/* Token::BLOCK_END_TYPE */ 3);
        $this->parser->pushLocalScope();
        $body = $this->parser->subparse([$this, 'decideBlockEnd'], true);
        if ($token = $stream->nextIf(/* Token::NAME_TYPE */ 5)) {
            $value = $token->getValue();

            if ($value != $name) {
                throw new SyntaxError(sprintf('Expected endmacro for macro "%s" (but "%s" given).', $name$value)$stream->getCurrent()->getLine()$stream->getSourceContext());
            }
        }
        
Home | Imprint | This part of the site doesn't use cookies.