function Parser::parseLogicalBinaryExpression
Parses a logical or a binary expression
Return value
Node\Node|null
1 call to Parser::parseLogicalBinaryExpression()
- Parser::parseConditionalExpression in vendor/
mck89/ peast/ lib/ Peast/ Syntax/ Parser.php - Parses a conditional expression
File
-
vendor/
mck89/ peast/ lib/ Peast/ Syntax/ Parser.php, line 3134
Class
- Parser
- Parser class
Namespace
Peast\SyntaxCode
protected function parseLogicalBinaryExpression() {
$operators = $this->logicalBinaryOperators;
if (!$this->context->allowIn) {
unset($operators["in"]);
}
if (!($exp = $this->parseUnaryExpression())) {
if (!$this->features->classFieldsPrivateIn || !$this->context->allowIn) {
return null;
}
//Support "#private in x" syntax
$state = $this->scanner
->getState();
if (!($exp = $this->parsePrivateIdentifier()) || !$this->scanner
->isBefore(array(
"in",
))) {
if ($exp) {
$this->scanner
->setState($state);
}
return null;
}
}
$list = array(
$exp,
);
$coalescingFound = $andOrFound = false;
while ($token = $this->scanner
->consumeOneOf(array_keys($operators))) {
$op = $token->value;
// Coalescing and logical expressions can't be used together
if ($op === "??") {
$coalescingFound = true;
}
elseif ($op === "&&" || $op === "||") {
$andOrFound = true;
}
if ($coalescingFound && $andOrFound) {
$this->error("Logical expressions must be wrapped in parentheses when " . "inside coalesce expressions");
}
if (!($exp = $this->parseUnaryExpression())) {
$this->error();
}
$list[] = $op;
$list[] = $exp;
}
$len = count($list);
if ($len > 1) {
$maxGrade = max($operators);
for ($grade = $maxGrade; $grade >= 0; $grade--) {
$class = $grade < 2 ? "LogicalExpression" : "BinaryExpression";
$r2l = $grade === 10;
//Exponentiation operator must be parsed right to left
if ($r2l) {
$i = $len - 2;
$step = -2;
}
else {
$i = 1;
$step = 2;
}
for (; $r2l && $i > 0 || !$r2l && $i < $len; $i += $step) {
if ($operators[$list[$i]] === $grade) {
$node = $this->createNode($class, $list[$i - 1]);
$node->setLeft($list[$i - 1]);
$node->setOperator($list[$i]);
$node->setRight($list[$i + 1]);
$node = $this->completeNode($node, $list[$i + 1]->location->end);
array_splice($list, $i - 1, 3, array(
$node,
));
if (!$r2l) {
$i -= $step;
}
$len = count($list);
}
}
}
}
return $list[0];
}