-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Added an article about ExpressionLanguage AST #7831
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
.. index:: | ||
single: AST; ExpressionLanguage | ||
|
||
Dumping and Manipulating the AST of Expressions | ||
=============================================== | ||
|
||
In computer science, `AST`_ (*Abstract Syntax Trees*) are a tree representation | ||
of the structure of source code written in a programming language. The | ||
expressions created with the ExpressionLanguage component are strings, which | ||
make them difficult to manipulate or inspect. | ||
|
||
A better approach is to dump the AST of those expressions using the ``dump()`` | ||
method. This turns the original string expression into a set of PHP classes | ||
describing the operations of that expression:: | ||
|
||
use Symfony\Component\ExpressionLanguage\ExpressionLanguage; | ||
|
||
$language = new ExpressionLanguage(); | ||
$ast = $language->dump('1 + 2'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi; Actually, this method does not exist. You can get the AST like this: <?php
require __DIR__.'/vendor/autoload.php';
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
$ast = (new ExpressionLanguage())
->parse('var.a.b + 12', ['var'])
->getNodes()
;
dump($ast); And then you can dump the AST (as a string) Finally, Instead of getting a string, it's possible to get an array in order to manipulate it: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for your help and for the examples! I've updated the article contents. |
||
// $ast = new BinaryNode('+', new ConstantNode(1), new ConstantNode(2)); | ||
|
||
$ast = $language->dump('"a" not in ["a", "b"]'); | ||
// $ast = new BinaryNode('not in', new ConstantNode('a'), new ArrayNode(new ConstantNode('a'), new ConstantNode('b'))); | ||
|
||
$ast = $language->dump('foo[0]'); | ||
// $ast = new GetAttrNode(new NameNode('foo'), new ConstantNode(0)); | ||
|
||
Manipulating the AST | ||
-------------------- | ||
|
||
.. TODO: https://github.com/symfony/symfony/pull/19060 | ||
|
||
.. _`AST`: https://en.wikipedia.org/wiki/Abstract_syntax_tree |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should at least add "Abstract Syntax Tree" to the index.