-
-
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.
- Loading branch information
Showing
26 changed files
with
346 additions
and
161 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
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 |
---|---|---|
@@ -1,65 +1,50 @@ | ||
<?php | ||
|
||
use Twig\Environment; | ||
use Twig\ExpressionParser\ExpressionParserDescriptionInterface; | ||
use Twig\ExpressionParser\ExpressionParserType; | ||
use Twig\ExpressionParser\InfixAssociativity; | ||
use Twig\ExpressionParser\InfixExpressionParserInterface; | ||
use Twig\ExpressionParser\PrefixExpressionParserInterface; | ||
use Twig\Loader\ArrayLoader; | ||
|
||
require_once dirname(__DIR__).'/vendor/autoload.php'; | ||
|
||
function printExpressionParsers($output, array $expressionParsers, bool $withAssociativity = false) | ||
{ | ||
if ($withAssociativity) { | ||
fwrite($output, "\n=========== =========== =============\n"); | ||
fwrite($output, "Precedence Operator Associativity\n"); | ||
fwrite($output, "=========== =========== =============\n"); | ||
} else { | ||
fwrite($output, "\n=========== ===========\n"); | ||
fwrite($output, "Precedence Operator\n"); | ||
fwrite($output, "=========== ===========\n"); | ||
} | ||
|
||
usort($expressionParsers, 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 ($expressionParsers as $expressionParser) { | ||
$precedence = $expressionParser->getPrecedenceChange() ? $expressionParser->getPrecedenceChange()->getNewPrecedence() : $expressionParser->getPrecedence(); | ||
if ($precedence !== $current) { | ||
$current = $precedence; | ||
if ($withAssociativity) { | ||
fwrite($output, \sprintf("\n%-11d %-11s %s", $precedence, $expressionParser->getName(), InfixAssociativity::Left === $expressionParser->getAssociativity() ? 'Left' : 'Right')); | ||
} else { | ||
fwrite($output, \sprintf("\n%-11d %s", $precedence, $expressionParser->getName())); | ||
} | ||
} else { | ||
fwrite($output, "\n".str_repeat(' ', 12).$expressionParser->getName()); | ||
} | ||
} | ||
fwrite($output, "\n"); | ||
} | ||
|
||
$output = fopen(dirname(__DIR__).'/doc/operators_precedence.rst', 'w'); | ||
|
||
$twig = new Environment(new ArrayLoader([])); | ||
$prefixExpressionParsers = []; | ||
$infixExpressionParsers = []; | ||
$expressionParsers = []; | ||
foreach ($twig->getExpressionParsers() as $expressionParser) { | ||
if ($expressionParser instanceof PrefixExpressionParserInterface) { | ||
$prefixExpressionParsers[] = $expressionParser; | ||
} elseif ($expressionParser instanceof InfixExpressionParserInterface) { | ||
$infixExpressionParsers[] = $expressionParser; | ||
} | ||
$expressionParsers[] = $expressionParser; | ||
} | ||
|
||
fwrite($output, "Unary operators precedence:\n"); | ||
printExpressionParsers($output, $prefixExpressionParsers); | ||
|
||
fwrite($output, "\nBinary and Ternary operators precedence:\n"); | ||
printExpressionParsers($output, $infixExpressionParsers, true); | ||
fwrite($output, "\n=========== ============== ======= ============= ===========\n"); | ||
fwrite($output, "Precedence Operator Type Associativity Description\n"); | ||
fwrite($output, "=========== ============== ======= ============= ==========="); | ||
|
||
usort($expressionParsers, function($a, $b) { | ||
$aPrecedence = $a->getPrecedenceChange() ? $a->getPrecedenceChange()->getNewPrecedence() : $a->getPrecedence(); | ||
$bPrecedence = $b->getPrecedenceChange() ? $b->getPrecedenceChange()->getNewPrecedence() : $b->getPrecedence(); | ||
return $bPrecedence - $aPrecedence; | ||
}); | ||
|
||
$previous = null; | ||
foreach ($expressionParsers as $expressionParser) { | ||
$precedence = $expressionParser->getPrecedenceChange() ? $expressionParser->getPrecedenceChange()->getNewPrecedence() : $expressionParser->getPrecedence(); | ||
$previousPrecedence = $previous ? ($previous->getPrecedenceChange() ? $previous->getPrecedenceChange()->getNewPrecedence() : $previous->getPrecedence()) : \PHP_INT_MAX; | ||
$associativity = $expressionParser instanceof InfixExpressionParserInterface ? (InfixAssociativity::Left === $expressionParser->getAssociativity() ? 'Left' : 'Right') : 'n/a'; | ||
$previousAssociativity = $previous ? ($previous instanceof InfixExpressionParserInterface ? (InfixAssociativity::Left === $previous->getAssociativity() ? 'Left' : 'Right') : 'n/a') : 'n/a'; | ||
if ($previousPrecedence !== $precedence) { | ||
$previous = null; | ||
} | ||
fwrite($output, rtrim(\sprintf("\n%-11s %-14s %-7s %-13s %s\n", | ||
!$previous || $previousPrecedence !== $precedence ? $precedence : '', | ||
'`'.$expressionParser->getName().'`', | ||
!$previous || ExpressionParserType::getType($previous) !== ExpressionParserType::getType($expressionParser) ? ExpressionParserType::getType($expressionParser)->value : '', | ||
!$previous || $previousAssociativity !== $associativity ? $associativity : '', | ||
$expressionParser instanceof ExpressionParserDescriptionInterface ? $expressionParser->getDescription() : '', | ||
))); | ||
$previous = $expressionParser; | ||
} | ||
fwrite($output, "\n=========== ============== ======= ============= ===========\n"); | ||
|
||
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
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 |
---|---|---|
@@ -1,57 +1,49 @@ | ||
Unary operators precedence: | ||
|
||
=========== =========== | ||
Precedence Operator | ||
=========== =========== | ||
|
||
500 - | ||
+ | ||
70 not | ||
0 ( | ||
literal | ||
|
||
Binary and Ternary operators precedence: | ||
|
||
=========== =========== ============= | ||
Precedence Operator Associativity | ||
=========== =========== ============= | ||
|
||
300 . Left | ||
[ | ||
| | ||
( | ||
250 => Left | ||
200 ** Right | ||
100 is Left | ||
is not | ||
60 * Left | ||
/ | ||
// | ||
% | ||
30 + Left | ||
- | ||
27 ~ Left | ||
25 .. Left | ||
20 == Left | ||
!= | ||
<=> | ||
< | ||
> | ||
>= | ||
<= | ||
not in | ||
in | ||
matches | ||
starts with | ||
ends with | ||
has some | ||
has every | ||
18 b-and Left | ||
17 b-xor Left | ||
16 b-or Left | ||
15 and Left | ||
12 xor Left | ||
10 or Left | ||
5 ?: Right | ||
?? | ||
0 ? Left | ||
=========== ============== ======= ============= =========== | ||
Precedence Operator Type Associativity Description | ||
=========== ============== ======= ============= =========== | ||
512 `.` infix Left Get an attribute on a variable | ||
`[` Array access | ||
`(` Function call | ||
500 `-` prefix n/a | ||
`+` | ||
300 `|` infix Left Filter call | ||
250 `=>` infix Left Arrow function (x => expr) | ||
200 `**` infix Right | ||
100 `is` infix Left | ||
`is not` | ||
70 `not` prefix n/a | ||
60 `*` infix Left | ||
`/` | ||
`//` Floor division | ||
`%` | ||
30 `+` infix Left | ||
`-` | ||
27 `~` infix Left | ||
25 `..` infix Left | ||
20 `==` infix Left | ||
`!=` | ||
`<=>` | ||
`<` | ||
`>` | ||
`>=` | ||
`<=` | ||
`not in` | ||
`in` | ||
`matches` | ||
`starts with` | ||
`ends with` | ||
`has some` | ||
`has every` | ||
18 `b-and` infix Left | ||
17 `b-xor` infix Left | ||
16 `b-or` infix Left | ||
15 `and` infix Left | ||
12 `xor` infix Left | ||
10 `or` infix Left | ||
5 `?:` infix Right Elvis operator (a ?: b) | ||
`??` Null coalescing operator (a ?? b) | ||
0 `(` prefix n/a Explicit group expression (a) | ||
`literal` A literal value (boolean, string, number, sequence, mapping, ...) | ||
`?` infix Left Conditional operator (a ? b : c) | ||
=========== ============== ======= ============= =========== |
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
17 changes: 17 additions & 0 deletions
17
src/ExpressionParser/ExpressionParserDescriptionInterface.php
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,17 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Twig. | ||
* | ||
* (c) Fabien Potencier | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Twig\ExpressionParser; | ||
|
||
interface ExpressionParserDescriptionInterface | ||
{ | ||
public function getDescription(): string; | ||
} |
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
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
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
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
Oops, something went wrong.