Skip to content

Commit

Permalink
Add a script to update operator precedence documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Jan 26, 2025
1 parent 0e9a5ea commit bf31cfc
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 29 deletions.
53 changes: 53 additions & 0 deletions bin/generate_operators_precedence.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

use Twig\Environment;
use Twig\Loader\ArrayLoader;
use Twig\Operator\OperatorArity;

require_once dirname(__DIR__).'/vendor/autoload.php';

function printOperators($output, array $operators)
{
fwrite($output, "\n========== ========\n");
fwrite($output, "Precedence Operator\n");
fwrite($output, "========== ========\n");

usort($operators, function($a, $b) {
$aPrecedence = $a->getPrecedenceChange() ? $a->getPrecedenceChange()->getNewPrecedence() : $a->getPrecedence();
$bPrecedence = $b->getPrecedenceChange() ? $b->getPrecedenceChange()->getNewPrecedence() : $b->getPrecedence();
return $bPrecedence - $aPrecedence;
});

$current = \PHP_INT_MAX;
foreach ($operators as $operator) {
$precedence = $operator->getPrecedenceChange() ? $operator->getPrecedenceChange()->getNewPrecedence() : $operator->getPrecedence();
if ($precedence !== $current) {
$current = $precedence;
fwrite($output, \sprintf("\n%-11d %s", $precedence, $operator->getOperator()));
} else {
fwrite($output, "\n".str_repeat(' ', 12).$operator->getOperator());
}
}
fwrite($output, "\n");
}

$output = fopen(dirname(__DIR__).'/doc/operators_precedence.rst', 'w');

$twig = new Environment(new ArrayLoader([]));
$unaryOperators = [];
$notUnaryOperators = [];
foreach ($twig->getOperators() as $operator) {
if ($operator->getArity()->value == OperatorArity::Unary->value) {
$unaryOperators[] = $operator;
} else {
$notUnaryOperators[] = $operator;
}
}

fwrite($output, "Unary operators precedence:\n");
printOperators($output, $unaryOperators);

fwrite($output, "\nBinary and Ternary operators precedence:\n");
printOperators($output, $notUnaryOperators);

fclose($output);
56 changes: 56 additions & 0 deletions doc/operators_precedence.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
Unary operators precedence:

========== ========
Precedence Operator
========== ========

500 -
+
70 not
0 (

Binary and Ternary operators precedence:

========== ========
Precedence Operator
========== ========

300 |
.
[
(
250 =>
200 **
100 is
is not
60 *
/
//
%
30 +
-
27 ~
25 ..
20 ==
!=
<=>
<
>
>=
<=
not in
in
matches
starts with
ends with
has some
has every
18 b-and
17 b-xor
16 b-or
15 and
12 xor
10 or
5 ?:
??
0 ?
32 changes: 3 additions & 29 deletions doc/templates.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1033,35 +1033,9 @@ Understanding the precedence of these operators is crucial for writing correct
and efficient Twig templates.

The operator precedence rules are as follows, with the lowest-precedence
operators listed first:

============================= =================================== =====================================================
Operator Score of precedence Description
============================= =================================== =====================================================
``?:`` 0 Ternary operator, conditional statement
``or`` 10 Logical OR operation between two boolean expressions
``xor`` 12 Logical XOR operation between two boolean expressions
``and`` 15 Logical AND operation between two boolean expressions
``b-or`` 16 Bitwise OR operation on integers
``b-xor`` 17 Bitwise XOR operation on integers
``b-and`` 18 Bitwise AND operation on integers
``==``, ``!=``, ``<=>``, 20 Comparison operators
``<``, ``>``, ``>=``,
``<=``, ``not in``, ``in``,
``matches``, ``starts with``,
``ends with``, ``has some``,
``has every``
``..`` 25 Range of values
``+``, ``-`` 30 Addition and subtraction on numbers
``~`` 40 String concatenation
``not`` 50 Negates a statement
``*``, ``/``, ``//``, ``%`` 60 Arithmetic operations on numbers
``is``, ``is not`` 100 Tests
``**`` 200 Raises a number to the power of another
``??`` 300 Default value when a variable is null
``+``, ``-`` 500 Unary operations on numbers
``|``,``[]``,``.`` - Filters, sequence, mapping, and attribute access
============================= =================================== =====================================================
operators listed first.

.. include:: operators_precedence.rst

Without using any parentheses, the operator precedence rules are used to
determine how to convert the code to PHP:
Expand Down

0 comments on commit bf31cfc

Please sign in to comment.