|
31 | 31 | EvalIntrinsicExpression, |
32 | 32 | ExitIntrinsicExpression, |
33 | 33 | IssetIntrinsicExpression, |
| 34 | + MatchExpression, |
34 | 35 | MemberAccessExpression, |
35 | 36 | ParenthesizedExpression, |
36 | 37 | PrefixUpdateExpression, |
|
61 | 62 | use Microsoft\PhpParser\Node\ForeachValue; |
62 | 63 | use Microsoft\PhpParser\Node\InterfaceBaseClause; |
63 | 64 | use Microsoft\PhpParser\Node\InterfaceMembers; |
| 65 | +use Microsoft\PhpParser\Node\MatchArm; |
64 | 66 | use Microsoft\PhpParser\Node\MissingMemberDeclaration; |
65 | 67 | use Microsoft\PhpParser\Node\NamespaceAliasingClause; |
66 | 68 | use Microsoft\PhpParser\Node\NamespaceUseGroupClause; |
@@ -969,6 +971,7 @@ private function isExpressionStartFn() { |
969 | 971 | case TokenKind::ObjectCastToken: |
970 | 972 | case TokenKind::StringCastToken: |
971 | 973 | case TokenKind::UnsetCastToken: |
| 974 | + case TokenKind::MatchKeyword: |
972 | 975 |
|
973 | 976 | // anonymous-function-creation-expression |
974 | 977 | case TokenKind::StaticKeyword: |
@@ -1087,6 +1090,8 @@ private function parsePrimaryExpression($parentNode) { |
1087 | 1090 | return $this->parseQualifiedName($parentNode); |
1088 | 1091 | } |
1089 | 1092 | return $this->parseReservedWordExpression($parentNode); |
| 1093 | + case TokenKind::MatchKeyword: |
| 1094 | + return $this->parseMatchExpression($parentNode); |
1090 | 1095 | } |
1091 | 1096 | if (\in_array($token->kind, TokenStringMaps::RESERVED_WORDS)) { |
1092 | 1097 | return $this->parseQualifiedName($parentNode); |
@@ -3572,6 +3577,59 @@ function ($parentNode) { |
3572 | 3577 | return $anonymousFunctionUseClause; |
3573 | 3578 | } |
3574 | 3579 |
|
| 3580 | + private function parseMatchExpression($parentNode) { |
| 3581 | + $matchExpression = new MatchExpression(); |
| 3582 | + $matchExpression->parent = $parentNode; |
| 3583 | + $matchExpression->matchToken = $this->eat1(TokenKind::MatchKeyword); |
| 3584 | + $matchExpression->openParen = $this->eat1(TokenKind::OpenParenToken); |
| 3585 | + $matchExpression->expression = $this->parseExpression($matchExpression); |
| 3586 | + $matchExpression->closeParen = $this->eat1(TokenKind::CloseParenToken); |
| 3587 | + $matchExpression->openBrace = $this->eat1(TokenKind::OpenBraceToken); |
| 3588 | + $matchExpression->arms = $this->parseDelimitedList( |
| 3589 | + DelimitedList\MatchExpressionArmList::class, |
| 3590 | + TokenKind::CommaToken, |
| 3591 | + $this->isMatchConditionStartFn(), |
| 3592 | + $this->parseMatchArmFn(), |
| 3593 | + $matchExpression); |
| 3594 | + $matchExpression->closeBrace = $this->eat1(TokenKind::CloseBraceToken); |
| 3595 | + return $matchExpression; |
| 3596 | + } |
| 3597 | + |
| 3598 | + private function isMatchConditionStartFn() { |
| 3599 | + return function ($token) { |
| 3600 | + return $token->kind === TokenKind::DefaultKeyword || |
| 3601 | + $this->isExpressionStart($token); |
| 3602 | + }; |
| 3603 | + } |
| 3604 | + |
| 3605 | + private function parseMatchArmFn() { |
| 3606 | + return function ($parentNode) { |
| 3607 | + $matchArm = new MatchArm(); |
| 3608 | + $matchArm->parent = $parentNode; |
| 3609 | + $matchArmConditionList = $this->parseDelimitedList( |
| 3610 | + DelimitedList\MatchArmConditionList::class, |
| 3611 | + TokenKind::CommaToken, |
| 3612 | + $this->isMatchConditionStartFn(), |
| 3613 | + $this->parseMatchConditionFn(), |
| 3614 | + $matchArm |
| 3615 | + ); |
| 3616 | + $matchArmConditionList->parent = $matchArm; |
| 3617 | + $matchArm->conditionList = $matchArmConditionList; |
| 3618 | + $matchArm->arrowToken = $this->eat1(TokenKind::DoubleArrowToken); |
| 3619 | + $matchArm->body = $this->parseExpression($matchArm); |
| 3620 | + return $matchArm; |
| 3621 | + }; |
| 3622 | + } |
| 3623 | + |
| 3624 | + private function parseMatchConditionFn() { |
| 3625 | + return function ($parentNode) { |
| 3626 | + if ($this->token->kind === TokenKind::DefaultKeyword) { |
| 3627 | + return $this->eat1(TokenKind::DefaultKeyword); |
| 3628 | + } |
| 3629 | + return $this->parseExpression($parentNode); |
| 3630 | + }; |
| 3631 | + } |
| 3632 | + |
3575 | 3633 | private function parseCloneExpression($parentNode) { |
3576 | 3634 | $cloneExpression = new CloneExpression(); |
3577 | 3635 | $cloneExpression->parent = $parentNode; |
|
0 commit comments