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
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
Prev Previous commit
Fixes and rewords
  • Loading branch information
javiereguiluz committed May 3, 2017
commit bfb54e24703eb10359ccec8d46cd380b89da875c
16 changes: 9 additions & 7 deletions components/expression_language/ast.rst
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
.. 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
===============================================

In computer science, `AST`_ (*Abstract Syntax Trees*) is *"a tree representation
of the structure of source code written in a programming language"*.

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, which is a set of nodes that contain PHP
classes.
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 ``getNodes()`` method after parsing any expression to get its AST::
Call the :method:`Symfony\\Component\\ExpressionLanguage\\ExpressionLanguage::getNodes`
method after parsing any expression to get its AST::

use Symfony\Component\ExpressionLanguage\ExpressionLanguage;

Expand All @@ -34,7 +35,8 @@ Manipulating the AST
--------------------

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

// ...

Expand Down