From cab5c89e5c608cd239a382b57c063f998da209ec Mon Sep 17 00:00:00 2001 From: Wouter J Date: Sun, 6 Oct 2013 20:04:50 +0200 Subject: [PATCH 1/5] Creates Expression Language chapter --- components/expression_language/index.rst | 7 +++++++ components/index.rst | 1 + components/map.rst.inc | 2 ++ 3 files changed, 10 insertions(+) create mode 100644 components/expression_language/index.rst diff --git a/components/expression_language/index.rst b/components/expression_language/index.rst new file mode 100644 index 00000000000..adddcd4fc6d --- /dev/null +++ b/components/expression_language/index.rst @@ -0,0 +1,7 @@ +Expression Language +=================== + +.. toctree:: + :maxdepth: 2 + + introduction diff --git a/components/index.rst b/components/index.rst index e5dffc58cd6..3cf769b6282 100644 --- a/components/index.rst +++ b/components/index.rst @@ -13,6 +13,7 @@ The Components dom_crawler dependency_injection/index event_dispatcher/index + expression_language/index filesystem finder form/index diff --git a/components/map.rst.inc b/components/map.rst.inc index a915310cde1..e1512b04ec2 100644 --- a/components/map.rst.inc +++ b/components/map.rst.inc @@ -57,6 +57,8 @@ * :doc:`/components/event_dispatcher/generic_event` * :doc:`/components/event_dispatcher/immutable_dispatcher` +* :doc:`/components/expression_language/index` + * **Filesystem** * :doc:`/components/filesystem` From 27822ffabb5eb94b9b8673a4064365b32003a1d8 Mon Sep 17 00:00:00 2001 From: Wouter J Date: Sun, 6 Oct 2013 20:08:43 +0200 Subject: [PATCH 2/5] Bootstraps intro --- .../expression_language/introduction.rst | 23 +++++++++++++++++++ components/map.rst.inc | 2 ++ 2 files changed, 25 insertions(+) create mode 100644 components/expression_language/introduction.rst diff --git a/components/expression_language/introduction.rst b/components/expression_language/introduction.rst new file mode 100644 index 00000000000..230a82ba1c0 --- /dev/null +++ b/components/expression_language/introduction.rst @@ -0,0 +1,23 @@ +.. index:: + single: Expressions + Single: Components; Expression Language + +The ExpressionLanguage Component +================================= + + The ExpressionLanguage component provides an engine that can compile and + evaluate expressions. An expression is a one-liner that returns a value + (mostly, but not limited to, Booleans). + +.. versionadded:: 2.4 + The ExpressionLanguage Component was new in Symfony 2.4. + +Installation +------------ + +You can install the component in 2 different ways: + +* Use the official Git repository (https://github.com/symfony/ExpressionLanguage); +* :doc:`Install it via Composer ` (``symfony/expression-language`` on `Packagist`_). + +.. _Packagist: https://packagist.org/packages/symfony/expression-language diff --git a/components/map.rst.inc b/components/map.rst.inc index e1512b04ec2..d55e3c4e721 100644 --- a/components/map.rst.inc +++ b/components/map.rst.inc @@ -59,6 +59,8 @@ * :doc:`/components/expression_language/index` + * :doc:`/components/expression_language/introduction` + * **Filesystem** * :doc:`/components/filesystem` From 6631f70836fbffdad407eef9e3e5289761756ed7 Mon Sep 17 00:00:00 2001 From: Wouter J Date: Sun, 6 Oct 2013 21:58:04 +0200 Subject: [PATCH 3/5] Added operators --- .../expression_language/introduction.rst | 111 ++++++++++++++++++ 1 file changed, 111 insertions(+) diff --git a/components/expression_language/introduction.rst b/components/expression_language/introduction.rst index 230a82ba1c0..3c3039cccaa 100644 --- a/components/expression_language/introduction.rst +++ b/components/expression_language/introduction.rst @@ -20,4 +20,115 @@ You can install the component in 2 different ways: * Use the official Git repository (https://github.com/symfony/ExpressionLanguage); * :doc:`Install it via Composer ` (``symfony/expression-language`` on `Packagist`_). +Usage +----- + +The ExpressionLanguage component can compile and evaluate expressions. +Expressions are one-liners which most of the time return a boolean, you can +compare them to the expression in an ``if`` statement. A simple example of an +expression is ``1 + 2``. You can also use more complicated expressions, such +as ``someArray[3].someMethod('bar')``. + +The component provide 2 ways to work with expressions: + +* **compile**: the expression is compiled to PHP, so it can be cached and + evaluated; +* **evaluation**: the expression is evaluated without being compiled to PHP. + +The main class of the component is +:class:`Symfony\\Component\\ExpressionLanguage\\ExpressionLanguage`:: + + use Symfony\Component\ExpressionLanguage\ExpressionLanguage; + + $language = new ExpressionLanguage(); + + echo $language->evaluate('1 + 2'); // displays 3 + + echo $language->compile('1 + 2'); // displays (1 + 2) + +Supported Literals +~~~~~~~~~~~~~~~~~~ + +The component supports: + +* **strings** - single and double quotes (e.g. ``'hello'``) +* **numbers** - e.g. ``103`` +* **arrays** - using twig notation (e.g. ``[1, 2]``) +* **hashes** - using twig notation (e.g. ``{ foo: 'bar' }``) +* **booleans** - ``true`` and ``false`` +* **null** - ``null`` + +Supported Operators +~~~~~~~~~~~~~~~~~~~ + +The component comes with a lot of operators: + +Arithmetic Operators +.................... + +* ``+`` (addition) +* ``-`` (subtraction) +* ``*`` (multiplication) +* ``/`` (division) +* ``%`` (modulus) +* ``**`` (pow) + +Assignment Operators +.................... + +* ``=`` + +Bitwise Operators +................. + +* ``&`` (and) +* ``|`` (or) +* ``^`` (xor) + +Comparison Operators +.................... + +* ``==`` (equal) +* ``===`` (identical) +* ``!=`` (not equal) +* ``!==`` (not identical) +* ``<`` (less than) +* ``>`` (greater than) +* ``<=`` (less than or equal to) +* ``>=`` (greater than or equal to) +* ``=~`` (regex match) +* ``!~`` (regex does not match) + +.. sidebar:: Regex Operator + + The Regex Operators (``=~`` and ``!~``) are coming from Perl. This + operator matches if the regular expression on the right side of the + operator matches the string on the left. For instance, + ``'foobar' =~ '/foo/'`` evaluates to true. + ``!~`` is the opposite and matches if the regular expression does *not* + match the string. + +Logical Operators +................. + +* ``not`` or ``!`` +* ``and`` or ``&&`` +* ``or`` or ``||`` + +String Operators +................ + +* ``~`` (concatenation) + +Array Operators +............... + +* ``in`` (contain) +* ``not in`` (does not contain) + +Numeric Operators +................. + +* ``..`` (range) + .. _Packagist: https://packagist.org/packages/symfony/expression-language From 4ff9faeab25fa64ae540181b8b24df450a903f7d Mon Sep 17 00:00:00 2001 From: Wouter J Date: Sun, 6 Oct 2013 22:01:38 +0200 Subject: [PATCH 4/5] Added another operator --- components/expression_language/introduction.rst | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/components/expression_language/introduction.rst b/components/expression_language/introduction.rst index 3c3039cccaa..475b15cb42f 100644 --- a/components/expression_language/introduction.rst +++ b/components/expression_language/introduction.rst @@ -131,4 +131,11 @@ Numeric Operators * ``..`` (range) +Ternary Operators +................. + +* ``foo ? 'yes' : 'no'`` +* ``foo ?: 'no'`` (equal to ``foo ? foo : 'no'``) +* ``foo ? 'yes'`` (equal to ``foo ? 'yes' : ''``) + .. _Packagist: https://packagist.org/packages/symfony/expression-language From 8fe9fd606fdf4b0b20bf55606b9b7472b3965c83 Mon Sep 17 00:00:00 2001 From: Wouter J Date: Sat, 26 Oct 2013 12:32:53 +0200 Subject: [PATCH 5/5] Fixed comments and splitted in intro and syntax --- components/expression_language/index.rst | 1 + .../expression_language/introduction.rst | 100 ++---------------- components/expression_language/syntax.rst | 98 +++++++++++++++++ components/map.rst.inc | 1 + 4 files changed, 107 insertions(+), 93 deletions(-) create mode 100644 components/expression_language/syntax.rst diff --git a/components/expression_language/index.rst b/components/expression_language/index.rst index adddcd4fc6d..e6eb657b4be 100644 --- a/components/expression_language/index.rst +++ b/components/expression_language/index.rst @@ -5,3 +5,4 @@ Expression Language :maxdepth: 2 introduction + syntax diff --git a/components/expression_language/introduction.rst b/components/expression_language/introduction.rst index 475b15cb42f..a7f474c732a 100644 --- a/components/expression_language/introduction.rst +++ b/components/expression_language/introduction.rst @@ -10,14 +10,14 @@ The ExpressionLanguage Component (mostly, but not limited to, Booleans). .. versionadded:: 2.4 - The ExpressionLanguage Component was new in Symfony 2.4. + The ExpressionLanguage component was new in Symfony 2.4. Installation ------------ You can install the component in 2 different ways: -* Use the official Git repository (https://github.com/symfony/ExpressionLanguage); +* Use the official Git repository (https://github.com/symfony/expression-language); * :doc:`Install it via Composer ` (``symfony/expression-language`` on `Packagist`_). Usage @@ -29,7 +29,7 @@ compare them to the expression in an ``if`` statement. A simple example of an expression is ``1 + 2``. You can also use more complicated expressions, such as ``someArray[3].someMethod('bar')``. -The component provide 2 ways to work with expressions: +The component provides 2 ways to work with expressions: * **compile**: the expression is compiled to PHP, so it can be cached and evaluated; @@ -46,96 +46,10 @@ The main class of the component is echo $language->compile('1 + 2'); // displays (1 + 2) -Supported Literals -~~~~~~~~~~~~~~~~~~ +Expression Syntax +----------------- -The component supports: - -* **strings** - single and double quotes (e.g. ``'hello'``) -* **numbers** - e.g. ``103`` -* **arrays** - using twig notation (e.g. ``[1, 2]``) -* **hashes** - using twig notation (e.g. ``{ foo: 'bar' }``) -* **booleans** - ``true`` and ``false`` -* **null** - ``null`` - -Supported Operators -~~~~~~~~~~~~~~~~~~~ - -The component comes with a lot of operators: - -Arithmetic Operators -.................... - -* ``+`` (addition) -* ``-`` (subtraction) -* ``*`` (multiplication) -* ``/`` (division) -* ``%`` (modulus) -* ``**`` (pow) - -Assignment Operators -.................... - -* ``=`` - -Bitwise Operators -................. - -* ``&`` (and) -* ``|`` (or) -* ``^`` (xor) - -Comparison Operators -.................... - -* ``==`` (equal) -* ``===`` (identical) -* ``!=`` (not equal) -* ``!==`` (not identical) -* ``<`` (less than) -* ``>`` (greater than) -* ``<=`` (less than or equal to) -* ``>=`` (greater than or equal to) -* ``=~`` (regex match) -* ``!~`` (regex does not match) - -.. sidebar:: Regex Operator - - The Regex Operators (``=~`` and ``!~``) are coming from Perl. This - operator matches if the regular expression on the right side of the - operator matches the string on the left. For instance, - ``'foobar' =~ '/foo/'`` evaluates to true. - ``!~`` is the opposite and matches if the regular expression does *not* - match the string. - -Logical Operators -................. - -* ``not`` or ``!`` -* ``and`` or ``&&`` -* ``or`` or ``||`` - -String Operators -................ - -* ``~`` (concatenation) - -Array Operators -............... - -* ``in`` (contain) -* ``not in`` (does not contain) - -Numeric Operators -................. - -* ``..`` (range) - -Ternary Operators -................. - -* ``foo ? 'yes' : 'no'`` -* ``foo ?: 'no'`` (equal to ``foo ? foo : 'no'``) -* ``foo ? 'yes'`` (equal to ``foo ? 'yes' : ''``) +See ":doc:`/components/expression_language/syntax`" to learn the syntax of the +ExpressionLanguage component. .. _Packagist: https://packagist.org/packages/symfony/expression-language diff --git a/components/expression_language/syntax.rst b/components/expression_language/syntax.rst new file mode 100644 index 00000000000..7c3a59fccb9 --- /dev/null +++ b/components/expression_language/syntax.rst @@ -0,0 +1,98 @@ +.. index:: + single: Syntax; ExpressionLanguage + +The Expression Syntax +===================== + +The ExpressionLanguage component uses a specific syntax which is based on the +expression syntax of Twig. In this document, you can find all supported +syntaxes. + +Supported Literals +~~~~~~~~~~~~~~~~~~ + +The component supports: + +* **strings** - single and double quotes (e.g. ``'hello'``) +* **numbers** - e.g. ``103`` +* **arrays** - using twig notation (e.g. ``[1, 2]``) +* **hashes** - using twig notation (e.g. ``{ foo: 'bar' }``) +* **booleans** - ``true`` and ``false`` +* **null** - ``null`` + +Supported Operators +~~~~~~~~~~~~~~~~~~~ + +The component comes with a lot of operators: + +Arithmetic Operators +.................... + +* ``+`` (addition) +* ``-`` (subtraction) +* ``*`` (multiplication) +* ``/`` (division) +* ``%`` (modulus) +* ``**`` (pow) + +Assignment Operators +.................... + +* ``=`` + +Bitwise Operators +................. + +* ``&`` (and) +* ``|`` (or) +* ``^`` (xor) + +Comparison Operators +.................... + +* ``==`` (equal) +* ``===`` (identical) +* ``!=`` (not equal) +* ``!==`` (not identical) +* ``<`` (less than) +* ``>`` (greater than) +* ``<=`` (less than or equal to) +* ``>=`` (greater than or equal to) +* ``matches`` (regex match) + +.. tip:: + + To test if a string does *not* match a regex, use the logical ``not`` + operator in combination with the ``matches`` operator:: + + $language->evaluate('not "foo" matches "/bar/"'); // returns true + +Logical Operators +................. + +* ``not`` or ``!`` +* ``and`` or ``&&`` +* ``or`` or ``||`` + +String Operators +................ + +* ``~`` (concatenation) + +Array Operators +............... + +* ``in`` (contain) +* ``not in`` (does not contain) + +Numeric Operators +................. + +* ``..`` (range) + +Ternary Operators +................. + +* ``foo ? 'yes' : 'no'`` +* ``foo ?: 'no'`` (equal to ``foo ? foo : 'no'``) +* ``foo ? 'yes'`` (equal to ``foo ? 'yes' : ''``) diff --git a/components/map.rst.inc b/components/map.rst.inc index d55e3c4e721..fb1055a6555 100644 --- a/components/map.rst.inc +++ b/components/map.rst.inc @@ -60,6 +60,7 @@ * :doc:`/components/expression_language/index` * :doc:`/components/expression_language/introduction` + * :doc:`/components/expression_language/syntax` * **Filesystem**