-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PHP 8.0] Add match expressions (#672)
RFC: https://wiki.php.net/rfc/match_expression_v2 Upstream implementation: php/php-src#5371 Closes #671.
- Loading branch information
1 parent
6ec527b
commit 69c5d48
Showing
18 changed files
with
2,581 additions
and
2,048 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
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
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,51 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace PhpParser\Lexer\TokenEmulator; | ||
|
||
use PhpParser\Lexer\Emulative; | ||
|
||
final class MatchTokenEmulator implements TokenEmulatorInterface | ||
{ | ||
public function isEmulationNeeded(string $code) : bool | ||
{ | ||
// skip version where this is supported | ||
if (version_compare(\PHP_VERSION, Emulative::PHP_8_0, '>=')) { | ||
return false; | ||
} | ||
|
||
return strpos($code, 'match') !== false; | ||
} | ||
|
||
public function emulate(string $code, array $tokens): array | ||
{ | ||
foreach ($tokens as $i => $token) { | ||
if ($token[0] === T_STRING && strtolower($token[1]) === 'match') { | ||
$previousNonSpaceToken = $this->getPreviousNonSpaceToken($tokens, $i); | ||
if ($previousNonSpaceToken !== null && $previousNonSpaceToken[0] === T_OBJECT_OPERATOR) { | ||
continue; | ||
} | ||
|
||
$tokens[$i][0] = Emulative::T_MATCH; | ||
} | ||
} | ||
|
||
return $tokens; | ||
} | ||
|
||
/** | ||
* @param mixed[] $tokens | ||
* @return mixed[]|null | ||
*/ | ||
private function getPreviousNonSpaceToken(array $tokens, int $start) | ||
{ | ||
for ($i = $start - 1; $i >= 0; --$i) { | ||
if ($tokens[$i][0] === T_WHITESPACE) { | ||
continue; | ||
} | ||
|
||
return $tokens[$i]; | ||
} | ||
|
||
return null; | ||
} | ||
} |
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,31 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace PhpParser\Node\Expr; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\MatchArm; | ||
|
||
class Match_ extends Node\Expr | ||
{ | ||
/** @var Node\Expr */ | ||
public $cond; | ||
/** @var MatchArm[] */ | ||
public $arms; | ||
|
||
/** | ||
* @param MatchArm[] $arms | ||
*/ | ||
public function __construct(Node\Expr $cond, array $arms = [], array $attributes = []) { | ||
$this->attributes = $attributes; | ||
$this->cond = $cond; | ||
$this->arms = $arms; | ||
} | ||
|
||
public function getSubNodeNames() : array { | ||
return ['cond', 'arms']; | ||
} | ||
|
||
public function getType() : string { | ||
return 'Expr_Match'; | ||
} | ||
} |
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,31 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace PhpParser\Node; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\NodeAbstract; | ||
|
||
class MatchArm extends NodeAbstract | ||
{ | ||
/** @var null|Node\Expr[] */ | ||
public $conds; | ||
/** @var Node\Expr */ | ||
public $body; | ||
|
||
/** | ||
* @param null|Node\Expr[] $conds | ||
*/ | ||
public function __construct($conds, Node\Expr $body, array $attributes = []) { | ||
$this->conds = $conds; | ||
$this->body = $body; | ||
$this->attributes = $attributes; | ||
} | ||
|
||
public function getSubNodeNames() : array { | ||
return ['conds', 'body']; | ||
} | ||
|
||
public function getType() : string { | ||
return 'MatchArm'; | ||
} | ||
} |
Oops, something went wrong.