-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a script to update operator precedence documentation
- Loading branch information
Showing
3 changed files
with
112 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters