Add support for spaceship operator (<=>) [PHP 7]

Added as Expr\BinaryOp\Spaceship.
This commit is contained in:
Nikita Popov 2015-03-12 23:13:31 +01:00
parent 01f4be6b4d
commit 251e689283
9 changed files with 1026 additions and 970 deletions

View File

@ -16,7 +16,7 @@
%left '|'
%left '^'
%left '&'
%nonassoc T_IS_EQUAL T_IS_NOT_EQUAL T_IS_IDENTICAL T_IS_NOT_IDENTICAL
%nonassoc T_IS_EQUAL T_IS_NOT_EQUAL T_IS_IDENTICAL T_IS_NOT_IDENTICAL T_SPACESHIP
%nonassoc '<' T_IS_SMALLER_OR_EQUAL '>' T_IS_GREATER_OR_EQUAL
%left T_SL T_SR
%left '+' '-' '.'
@ -581,6 +581,7 @@ expr:
| expr T_IS_NOT_IDENTICAL expr { $$ = Expr\BinaryOp\NotIdentical [$1, $3]; }
| expr T_IS_EQUAL expr { $$ = Expr\BinaryOp\Equal [$1, $3]; }
| expr T_IS_NOT_EQUAL expr { $$ = Expr\BinaryOp\NotEqual [$1, $3]; }
| expr T_SPACESHIP expr { $$ = Expr\BinaryOp\Spaceship [$1, $3]; }
| expr '<' expr { $$ = Expr\BinaryOp\Smaller [$1, $3]; }
| expr T_IS_SMALLER_OR_EQUAL expr { $$ = Expr\BinaryOp\SmallerOrEqual[$1, $3]; }
| expr '>' expr { $$ = Expr\BinaryOp\Greater [$1, $3]; }

View File

@ -16,6 +16,7 @@ class Emulative extends \PhpParser\Lexer
const T_POW = 1002;
const T_POW_EQUAL = 1003;
const T_COALESCE = 1004;
const T_SPACESHIP = 1005;
const PHP_7_0 = '7.0.0dev';
const PHP_5_6 = '5.6.0rc1';
@ -51,6 +52,7 @@ class Emulative extends \PhpParser\Lexer
return;
}
$this->tokenMap[self::T_COALESCE] = Parser::T_COALESCE;
$this->tokenMap[self::T_SPACESHIP] = Parser::T_SPACESHIP;
if (version_compare(PHP_VERSION, self::PHP_5_6, '>=')) {
return;
@ -88,6 +90,7 @@ class Emulative extends \PhpParser\Lexer
}
$code = str_replace('??', '~__EMU__COALESCE__~', $code);
$code = str_replace('<=>', '~__EMU__SPACESHIP__~', $code);
if (version_compare(PHP_VERSION, self::PHP_5_6, '>=')) {
return $code;
@ -143,6 +146,10 @@ class Emulative extends \PhpParser\Lexer
$replace = array(
array(self::T_COALESCE, '??', $this->tokens[$i + 1][2])
);
} else if ('SPACESHIP' === $matches[1]) {
$replace = array(
array(self::T_SPACESHIP, '<=>', $this->tokens[$i + 1][2]),
);
} else {
// just ignore all other __EMU__ sequences
continue;
@ -179,6 +186,8 @@ class Emulative extends \PhpParser\Lexer
return '**=';
} else if ('COALESCE' === $matches[1]) {
return '??';
} else if ('SPACESHIP' === $matches[1]) {
return '<=>';
} else {
return $matches[0];
}

View File

@ -0,0 +1,9 @@
<?php
namespace PhpParser\Node\Expr\BinaryOp;
use PhpParser\Node\Expr\BinaryOp;
class Spaceship extends BinaryOp
{
}

File diff suppressed because it is too large Load Diff

View File

@ -246,6 +246,10 @@ class Standard extends PrettyPrinterAbstract
return $this->pInfixOp('Expr_BinaryOp_NotIdentical', $node->left, ' !== ', $node->right);
}
public function pExpr_BinaryOp_Spaceship(BinaryOp\Spaceship $node) {
return $this->pInfixOp('Expr_BinaryOp_Spaceship', $node->left, ' <=> ', $node->right);
}
public function pExpr_BinaryOp_Greater(BinaryOp\Greater $node) {
return $this->pInfixOp('Expr_BinaryOp_Greater', $node->left, ' > ', $node->right);
}

View File

@ -43,6 +43,7 @@ abstract class PrettyPrinterAbstract
'Expr_BinaryOp_NotEqual' => array( 80, 0),
'Expr_BinaryOp_Identical' => array( 80, 0),
'Expr_BinaryOp_NotIdentical' => array( 80, 0),
'Expr_BinaryOp_Spaceship' => array( 80, 0),
'Expr_BinaryOp_BitwiseAnd' => array( 90, -1),
'Expr_BinaryOp_BitwiseXor' => array(100, -1),
'Expr_BinaryOp_BitwiseOr' => array(110, -1),

View File

@ -99,6 +99,9 @@ class EmulativeTest extends LexerTest
array('??', array(
array(Parser::T_COALESCE, '??'),
)),
array('<=>', array(
array(Parser::T_SPACESHIP, '<=>'),
)),
array('0b1010110', array(
array(Parser::T_LNUMBER, '0b1010110'),
)),

View File

@ -9,6 +9,7 @@ $a == $b;
$a === $b;
$a != $b;
$a !== $b;
$a <=> $b;
$a instanceof B;
$a instanceof $b;
-----
@ -77,7 +78,15 @@ array(
name: b
)
)
8: Expr_Instanceof(
8: Expr_BinaryOp_Spaceship(
left: Expr_Variable(
name: a
)
right: Expr_Variable(
name: b
)
)
9: Expr_Instanceof(
expr: Expr_Variable(
name: a
)
@ -87,7 +96,7 @@ array(
)
)
)
9: Expr_Instanceof(
10: Expr_Instanceof(
expr: Expr_Variable(
name: a
)
@ -95,4 +104,4 @@ array(
name: b
)
)
)
)

View File

@ -44,6 +44,7 @@ $a != $b;
$a <> $b;
$a === $b;
$a !== $b;
$a <=> $b;
$a & $b;
$a ^ $b;
$a | $b;
@ -113,6 +114,7 @@ $a != $b;
$a != $b;
$a === $b;
$a !== $b;
$a <=> $b;
$a & $b;
$a ^ $b;
$a | $b;