Skip to content

Commit

Permalink
fix the rst syntax of the operator precedence table
Browse files Browse the repository at this point in the history
  • Loading branch information
xabbuh committed Feb 16, 2025
1 parent 576b412 commit a544dc8
Show file tree
Hide file tree
Showing 2 changed files with 208 additions and 110 deletions.
28 changes: 18 additions & 10 deletions bin/generate_operators_precedence.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,32 @@
$output = fopen(\dirname(__DIR__).'/doc/operators_precedence.rst', 'w');

$twig = new Environment(new ArrayLoader([]));
$descriptionLength = 11;
$expressionParsers = [];
foreach ($twig->getExpressionParsers() as $expressionParser) {
$expressionParsers[] = $expressionParser;
$descriptionLength = max($descriptionLength, $expressionParser instanceof ExpressionParserDescriptionInterface ? strlen($expressionParser->getDescription()) : '');
}

fwrite($output, "\n=========== ================ ======= ============= ===========\n");
fwrite($output, "Precedence Operator Type Associativity Description\n");
fwrite($output, '=========== ================ ======= ============= ===========');
fwrite($output, "\n+------------+------------------+---------+---------------+".str_repeat('-', $descriptionLength + 2)."+\n");
fwrite($output, "| Precedence | Operator | Type | Associativity | Description".str_repeat(' ', $descriptionLength - 11)." |\n");
fwrite($output, '+============+==================+=========+===============+'.str_repeat('=', $descriptionLength + 2).'+');

usort($expressionParsers, fn ($a, $b) => $b->getPrecedence() <=> $a->getPrecedence());

$previous = null;
foreach ($expressionParsers as $expressionParser) {
if (null !== $previous) {
fwrite($output, "\n+------------+------------------+---------+---------------+".str_repeat('-', $descriptionLength + 2).'+');
}
$precedence = $expressionParser->getPrecedence();
$previousPrecedence = $previous ? $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 %-16s %-7s %-13s %s\n",
fwrite($output, rtrim(\sprintf("\n| %-10s | %-16s | %-7s | %-13s | %-{$descriptionLength}s |\n",
(!$previous || $previousPrecedence !== $precedence ? $precedence : '').($expressionParser->getPrecedenceChange() ? ' => '.$expressionParser->getPrecedenceChange()->getNewPrecedence() : ''),
'``'.$expressionParser->getName().'``',
!$previous || ExpressionParserType::getType($previous) !== ExpressionParserType::getType($expressionParser) ? ExpressionParserType::getType($expressionParser)->value : '',
Expand All @@ -50,14 +55,14 @@
)));
$previous = $expressionParser;
}
fwrite($output, "\n=========== ================ ======= ============= ===========\n");
fwrite($output, "\n+------------+------------------+---------+---------------+".str_repeat('-', $descriptionLength + 2)."+\n");
fwrite($output, "\nWhen a precedence will change in 4.0, the new precedence is indicated by the arrow ``=>``.\n");

fwrite($output, "\nHere is the same table for Twig 4.0 with adjusted precedences:\n");

fwrite($output, "\n=========== ================ ======= ============= ===========\n");
fwrite($output, "Precedence Operator Type Associativity Description\n");
fwrite($output, '=========== ================ ======= ============= ===========');
fwrite($output, "\n+------------+------------------+---------+---------------+".str_repeat('-', $descriptionLength + 2)."+\n");
fwrite($output, "| Precedence | Operator | Type | Associativity | Description".str_repeat(' ', $descriptionLength - 11)." |\n");
fwrite($output, '+============+==================+=========+===============+'.str_repeat('=', $descriptionLength + 2).'+');

usort($expressionParsers, function ($a, $b) {
$aPrecedence = $a->getPrecedenceChange() ? $a->getPrecedenceChange()->getNewPrecedence() : $a->getPrecedence();
Expand All @@ -68,14 +73,17 @@

$previous = null;
foreach ($expressionParsers as $expressionParser) {
if (null !== $previous) {
fwrite($output, "\n+------------+------------------+---------+---------------+".str_repeat('-', $descriptionLength + 2).'+');
}
$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 %-16s %-7s %-13s %s\n",
fwrite($output, rtrim(\sprintf("\n| %-10s | %-16s | %-7s | %-13s | %-{$descriptionLength}s |\n",
!$previous || $previousPrecedence !== $precedence ? $precedence : '',
'``'.$expressionParser->getName().'``',
!$previous || ExpressionParserType::getType($previous) !== ExpressionParserType::getType($expressionParser) ? ExpressionParserType::getType($expressionParser)->value : '',
Expand All @@ -84,6 +92,6 @@
)));
$previous = $expressionParser;
}
fwrite($output, "\n=========== ================ ======= ============= ===========\n");
fwrite($output, "\n+------------+------------------+---------+---------------+".str_repeat('-', $descriptionLength + 2)."+\n");

fclose($output);
Loading

0 comments on commit a544dc8

Please sign in to comment.