function PoHeader::parseArithmetic
Same name in other branches
- 9 core/lib/Drupal/Component/Gettext/PoHeader.php \Drupal\Component\Gettext\PoHeader::parseArithmetic()
- 8.9.x core/lib/Drupal/Component/Gettext/PoHeader.php \Drupal\Component\Gettext\PoHeader::parseArithmetic()
- 11.x core/lib/Drupal/Component/Gettext/PoHeader.php \Drupal\Component\Gettext\PoHeader::parseArithmetic()
Parses and sanitizes an arithmetic formula into a plural element stack.
While parsing, we ensure, that the operators have the right precedence and associativity.
Parameters
string $string: A string containing the arithmetic formula.
Return value
array|bool A stack of values and operations to be evaluated. False if the formula could not be parsed.
1 call to PoHeader::parseArithmetic()
- PoHeader::parsePluralForms in core/
lib/ Drupal/ Component/ Gettext/ PoHeader.php - Parses a Plural-Forms entry from a Gettext Portable Object file header.
File
-
core/
lib/ Drupal/ Component/ Gettext/ PoHeader.php, line 278
Class
- PoHeader
- Gettext PO header handler.
Namespace
Drupal\Component\GettextCode
private function parseArithmetic($string) {
// Operator precedence table.
$precedence = [
"(" => -1,
")" => -1,
"?" => 1,
":" => 1,
"||" => 3,
"&&" => 4,
"==" => 5,
"!=" => 5,
"<" => 6,
">" => 6,
"<=" => 6,
">=" => 6,
"+" => 7,
"-" => 7,
"*" => 8,
"/" => 8,
"%" => 8,
];
// Right associativity.
$right_associativity = [
"?" => 1,
":" => 1,
];
$tokens = $this->tokenizeFormula($string);
// Parse by converting into infix notation then back into postfix
// Operator stack - holds math operators and symbols.
$operator_stack = [];
// Element Stack - holds data to be operated on.
$element_stack = [];
foreach ($tokens as $token) {
$current_token = $token;
// Numbers and the $n variable are simply pushed into $element_stack.
if (is_numeric($token)) {
$element_stack[] = $current_token;
}
elseif ($current_token == "n") {
$element_stack[] = '$n';
}
elseif ($current_token == "(") {
$operator_stack[] = $current_token;
}
elseif ($current_token == ")") {
$top_op = array_pop($operator_stack);
while (isset($top_op) && $top_op != "(") {
$element_stack[] = $top_op;
$top_op = array_pop($operator_stack);
}
}
elseif (!empty($precedence[$current_token])) {
// If it's an operator, then pop from $operator_stack into
// $element_stack until the precedence in $operator_stack is less
// than current, then push into $operator_stack.
$top_op = array_pop($operator_stack);
while (isset($top_op) && $precedence[$top_op] >= $precedence[$current_token] && !($precedence[$top_op] == $precedence[$current_token] && !empty($right_associativity[$top_op]) && !empty($right_associativity[$current_token]))) {
$element_stack[] = $top_op;
$top_op = array_pop($operator_stack);
}
if ($top_op) {
// Return element to top.
$operator_stack[] = $top_op;
}
// Parentheses are not needed.
$operator_stack[] = $current_token;
}
else {
return FALSE;
}
}
// Flush operator stack.
$top_op = array_pop($operator_stack);
while ($top_op != NULL) {
$element_stack[] = $top_op;
$top_op = array_pop($operator_stack);
}
$return = $element_stack;
// Now validate stack.
$previous_size = count($element_stack) + 1;
while (count($element_stack) < $previous_size) {
$previous_size = count($element_stack);
for ($i = 2; $i < count($element_stack); $i++) {
$op = $element_stack[$i];
if (!empty($precedence[$op])) {
if ($op == ":") {
$f = $element_stack[$i - 2] . "):" . $element_stack[$i - 1] . ")";
}
elseif ($op == "?") {
$f = "(" . $element_stack[$i - 2] . "?(" . $element_stack[$i - 1];
}
else {
$f = "(" . $element_stack[$i - 2] . $op . $element_stack[$i - 1] . ")";
}
array_splice($element_stack, $i - 2, 3, $f);
break;
}
}
}
// If only one element is left, the number of operators is appropriate.
return count($element_stack) == 1 ? $return : FALSE;
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.