Skip to content

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions components/expression_language.rst
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ Caching
The component provides some different caching strategies, read more about them
in :doc:`/components/expression_language/caching`.

AST Dumping and Editing
-----------------------

The AST (*Abstract Syntax Tree*) of expressions can be dumped and manipulated
as explained in :doc:`/components/expression_language/ast`.

Learn More
----------

Expand Down
33 changes: 33 additions & 0 deletions components/expression_language/ast.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
.. index::
single: AST; ExpressionLanguage
Copy link
Member

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.


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');
Copy link
Member

Choose a reason for hiding this comment

The 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) dump($ast->dump());

Finally, Instead of getting a string, it's possible to get an array in order to manipulate it: dump($ast->toArray());

Copy link
Member Author

Choose a reason for hiding this comment

The 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