Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[ExpressionLanguage] Add Parser->parseWithoutContext #50144

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/ExpressionLanguage/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ CHANGELOG
* Add `enum` expression function
* Deprecate loose comparisons when using the "in" operator; normalize the array parameter
so it only has the expected types or implement loose matching in your own expression function
* Add `parseWithoutContext` to `Parser`, use this function to get the expression's AST without validating names

6.2
---
Expand Down
21 changes: 19 additions & 2 deletions src/Symfony/Component/ExpressionLanguage/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class Parser
private array $functions;
private ?array $names;
private bool $lint = false;
private bool $validateIdentifiers = true;

public function __construct(array $functions)
{
Expand Down Expand Up @@ -94,10 +95,25 @@ public function __construct(array $functions)
public function parse(TokenStream $stream, array $names = []): Node\Node
{
$this->lint = false;
$this->validateIdentifiers = true;

return $this->doParse($stream, $names);
}

/**
* Converts a token stream to a node tree.
*
* This function does not validate identifiers.
* Prefer using `parse` if you can.
*/
public function parseWithoutContext(TokenStream $stream): Node\Node
{
$this->lint = false;
$this->validateIdentifiers = false;

return $this->doParse($stream, null);
}

/**
* Validates the syntax of an expression.
*
Expand All @@ -109,6 +125,7 @@ public function parse(TokenStream $stream, array $names = []): Node\Node
public function lint(TokenStream $stream, ?array $names = []): void
{
$this->lint = true;
$this->validateIdentifiers = true;
$this->doParse($stream, $names);
}

Expand Down Expand Up @@ -238,13 +255,13 @@ public function parsePrimaryExpression()

default:
if ('(' === $this->stream->current->value) {
if (false === isset($this->functions[$token->value])) {
if ($this->validateIdentifiers && 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 ($this->validateIdentifiers && (!$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);
}
Expand Down
14 changes: 14 additions & 0 deletions src/Symfony/Component/ExpressionLanguage/Tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\ExpressionLanguage\Lexer;
use Symfony\Component\ExpressionLanguage\Node;
use Symfony\Component\ExpressionLanguage\Node\ConstantNode;
use Symfony\Component\ExpressionLanguage\Node\FunctionNode;
use Symfony\Component\ExpressionLanguage\Node\NameNode;
use Symfony\Component\ExpressionLanguage\Node\Node as NodeNode;
use Symfony\Component\ExpressionLanguage\Parser;
use Symfony\Component\ExpressionLanguage\SyntaxError;

Expand Down Expand Up @@ -378,4 +382,14 @@ public static function getLintData(): array
],
];
}

public function testParseWithoutContext()
{
$expression = 'is_granted(\'ROLE_EDIT\', subject)';
$expected = new FunctionNode('is_granted', new NodeNode([new ConstantNode('ROLE_EDIT'), new NameNode('subject')]));

$lexer = new Lexer();
$parser = new Parser([]);
$this->assertEquals($expected, $parser->parseWithoutContext($lexer->tokenize($expression)));
}
}