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 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
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
49 changes: 49 additions & 0 deletions components/expression_language/ast.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
.. 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.

single: AST; Abstract Syntax Tree

Dumping and Manipulating the AST of Expressions
===============================================

Manipulating or inspecting the expressions created with the ExpressionLanguage
component is difficult because they are plain strings. A better approach is to
turn those expressions into an AST. In computer science, `AST`_ (*Abstract
Syntax Tree*) is *"a tree representation of the structure of source code written
in a programming language"*. In Symfony, a ExpressionLanguage AST is a set of
nodes that contain PHP classes representing the given expression.

Dumping the AST
---------------

Call the :method:`Symfony\\Component\\ExpressionLanguage\\ExpressionLanguage::getNodes`
method after parsing any expression to get its AST::

use Symfony\Component\ExpressionLanguage\ExpressionLanguage;

$ast = (new ExpressionLanguage())
->parse('1 + 2')
->getNodes()
;

// dump the AST nodes for inspection
var_dump($ast);

// dump the AST nodes as a string representation
$astAsString = $ast->dump();

Manipulating the AST
--------------------

The nodes of the AST can also be dumped into a PHP array of nodes to allow
manipulating them. Call the :method:`Symfony\\Component\\ExpressionLanguage\\ExpressionLanguage::toArray`
method to turn the AST into an array::

// ...

$astAsArray = (new ExpressionLanguage())
->parse('1 + 2')
->getNodes()
->toArray()
;

.. _`AST`: https://en.wikipedia.org/wiki/Abstract_syntax_tree