-
Notifications
You must be signed in to change notification settings - Fork 79
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support parsing of PHP 8.4 property hooks #411
Open
TysonAndre
wants to merge
1
commit into
microsoft:main
Choose a base branch
from
TysonAndre:property-hook-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
namespace Microsoft\PhpParser\Node; | ||
|
||
use Microsoft\PhpParser\Node; | ||
use Microsoft\PhpParser\Node\Statement\CompoundStatementNode; | ||
use Microsoft\PhpParser\Token; | ||
|
||
class PropertyHook extends Node { | ||
/** @var AttributeGroup[]|null */ | ||
public $attributes; | ||
/** @var Token */ | ||
public $byRefToken; | ||
/** @var Token */ | ||
public $name; | ||
/** @var Token|null */ | ||
public $openParen; | ||
/** @var DelimitedList\ParameterDeclarationList|null */ | ||
public $parameters; | ||
/** @var Token|null */ | ||
public $closeParen; | ||
/** @var Token|null */ | ||
public $arrowToken; | ||
/** @var CompoundStatementNode|Expression|Token|null */ | ||
public $body; | ||
/** @var Token|null */ | ||
public $semicolon; | ||
|
||
const CHILD_NAMES = [ | ||
'attributes', | ||
'byRefToken', | ||
'name', | ||
'openParen', | ||
'parameters', | ||
'closeParen', | ||
'arrowToken', | ||
'body', | ||
'semicolon', | ||
]; | ||
} |
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,28 @@ | ||
<?php | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
namespace Microsoft\PhpParser\Node; | ||
|
||
use Microsoft\PhpParser\Node; | ||
use Microsoft\PhpParser\Node\PropertyHooks; | ||
use Microsoft\PhpParser\Token; | ||
|
||
class PropertyHooks extends Node { | ||
/** @var Token */ | ||
public $openBrace; | ||
|
||
/** @var PropertyHook[] */ | ||
public $hookDeclarations; | ||
|
||
/** @var Token */ | ||
public $closeBrace; | ||
|
||
const CHILD_NAMES = [ | ||
'openBrace', | ||
'hookDeclarations', | ||
'closeBrace' | ||
]; | ||
} |
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 |
---|---|---|
|
@@ -74,6 +74,8 @@ | |
use Microsoft\PhpParser\Node\NumericLiteral; | ||
use Microsoft\PhpParser\Node\ParenthesizedIntersectionType; | ||
use Microsoft\PhpParser\Node\PropertyDeclaration; | ||
use Microsoft\PhpParser\Node\PropertyHooks; | ||
use Microsoft\PhpParser\Node\PropertyHook; | ||
use Microsoft\PhpParser\Node\ReservedWord; | ||
use Microsoft\PhpParser\Node\StringLiteral; | ||
use Microsoft\PhpParser\Node\MethodDeclaration; | ||
|
@@ -175,7 +177,7 @@ protected function makeLexer(string $fileContents): TokenStreamProviderInterface | |
* @param string $fileContents | ||
* @return SourceFileNode | ||
*/ | ||
public function parseSourceFile(string $fileContents, string $uri = null) : SourceFileNode { | ||
public function parseSourceFile(string $fileContents, ?string $uri = null) : SourceFileNode { | ||
$this->lexer = $this->makeLexer($fileContents); | ||
|
||
$this->reset(); | ||
|
@@ -877,6 +879,10 @@ private function parseParameterFn() { | |
// TODO add post-parse rule that checks for invalid assignments | ||
$parameter->default = $this->parseExpression($parameter); | ||
} | ||
|
||
if ($this->token->kind === TokenKind::OpenBraceToken) { | ||
$parameter->propertyHooks = $this->parsePropertyHooks($parameter); | ||
} | ||
return $parameter; | ||
}; | ||
} | ||
|
@@ -1417,7 +1423,7 @@ private function parseStringLiteralExpression2($parentNode): StringLiteral { | |
case TokenKind::DollarOpenBraceToken: | ||
case TokenKind::OpenBraceDollarToken: | ||
$expression->children[] = $this->eat(TokenKind::DollarOpenBraceToken, TokenKind::OpenBraceDollarToken); | ||
/** | ||
/** | ||
* @phpstan-ignore-next-line "Strict comparison using | ||
* === between 403|404 and 408 will always evaluate to | ||
* false" is wrong because those tokens were eaten above | ||
|
@@ -3423,12 +3429,115 @@ private function parsePropertyDeclaration($parentNode, $modifiers, $questionToke | |
} elseif ($questionToken) { | ||
$propertyDeclaration->typeDeclarationList = new MissingToken(TokenKind::PropertyType, $this->token->fullStart); | ||
} | ||
$propertyDeclaration->propertyElements = $this->parseExpressionList($propertyDeclaration); | ||
$propertyDeclaration->semicolon = $this->eat1(TokenKind::SemicolonToken); | ||
$propertyDeclaration->propertyElements = $this->parsePropertyNameList($propertyDeclaration); | ||
if ($this->token->kind === TokenKind::OpenBraceToken) { | ||
$propertyDeclaration->propertyHooks = $this->parsePropertyHooks($propertyDeclaration); | ||
} else { | ||
$propertyDeclaration->semicolon = $this->eat1(TokenKind::SemicolonToken); | ||
} | ||
|
||
return $propertyDeclaration; | ||
} | ||
|
||
/** | ||
* @param PropertyDeclaration $parentNode | ||
* @return DelimitedList\VariableNameList | ||
*/ | ||
private function parsePropertyNameList($parentNode) { | ||
// XXX this used to be implemented with parseExpressionList so keep the same classes. | ||
return $this->parseDelimitedList( | ||
DelimitedList\ExpressionList::class, | ||
TokenKind::CommaToken, | ||
function ($token) { | ||
return $token->kind === TokenKind::VariableName; | ||
}, | ||
$this->parsePropertyVariableNameAndDefault(), | ||
$parentNode | ||
); | ||
} | ||
|
||
private function parsePropertyVariableNameAndDefault() { | ||
return function ($parentNode) { | ||
// Imitate the format that parseExpression would have returned from when | ||
// parseExpression was originally used. | ||
// This is more precise and rules out parse errors such as `public $propName + 2;` | ||
// This approach also avoids conflict with the deprecated $x{expr} array access syntax. | ||
$variable = new Variable(); | ||
$variable->name = $this->eat1(TokenKind::VariableName); | ||
$equalsToken = $this->eatOptional1(TokenKind::EqualsToken); | ||
if ($equalsToken === null) { | ||
$variable->parent = $parentNode; | ||
return $variable; | ||
} | ||
|
||
$byRefToken = $this->eatOptional1(TokenKind::AmpersandToken); // byRef default is nonsense, but this is a compile-time error instead of a parse error. | ||
$rightOperand = $this->parseExpression($parentNode); // gets overridden in makeBinaryExpression | ||
return $this->makeBinaryExpression($variable, $equalsToken, $byRefToken, $rightOperand, $parentNode); | ||
}; | ||
} | ||
|
||
/** | ||
* @param PropertyDeclaration|Parameter $parent | ||
* @return PropertyHooks | ||
*/ | ||
private function parsePropertyHooks(Node $parent) { | ||
$propertyHooks = new PropertyHooks(); | ||
$propertyHooks->parent = $parent; | ||
$propertyHooks->openBrace = $this->eat1(TokenKind::OpenBraceToken); | ||
$hooks = []; | ||
while (in_array($this->getCurrentToken()->kind, self::PROPERTY_HOOK_START_TOKENS, true)) { | ||
$hooks[] = $this->parsePropertyHook($propertyHooks); | ||
} | ||
$propertyHooks->hookDeclarations = $hooks; | ||
$propertyHooks->closeBrace = $this->eat1(TokenKind::CloseBraceToken); | ||
return $propertyHooks; | ||
} | ||
|
||
const PROPERTY_HOOK_START_TOKENS = [ | ||
TokenKind::Name, | ||
TokenKind::AmpersandToken, // by reference | ||
TokenKind::AttributeToken, | ||
]; | ||
|
||
private function isPropertyHookStart() { | ||
return function ($token) { | ||
return \in_array($token->kind, self::PROPERTY_HOOK_START_TOKENS, true); | ||
}; | ||
} | ||
|
||
/** | ||
* @param PropertyHooks $parentNode | ||
* @return PropertyHook | ||
*/ | ||
private function parsePropertyHook($parentNode) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we can safely add a real type hint here I think and a real return type? |
||
$node = new PropertyHook(); | ||
$node->parent = $parentNode; | ||
if ($this->getCurrentToken()->kind === TokenKind::AttributeToken) { | ||
$node->attributes = $this->parseAttributeGroups($node); | ||
} | ||
$node->byRefToken = $this->eatOptional1(TokenKind::AmpersandToken); | ||
$node->name = $this->eat1(TokenKind::Name); // "get" or "set" - other values are compile errors, not parse errors. | ||
$node->openParen = $this->eatOptional1(TokenKind::OpenParenToken); | ||
if ($node->openParen) { | ||
$node->parameters = $this->parseDelimitedList( | ||
DelimitedList\ParameterDeclarationList::class, | ||
TokenKind::CommaToken, | ||
$this->isParameterStartFn(), | ||
$this->parseParameterFn(), | ||
$node); | ||
$node->closeParen = $this->eat1(TokenKind::CloseParenToken); | ||
} | ||
// e.g. `get => expr;` or `get { return $expr; }` | ||
$node->arrowToken = $this->eatOptional1(TokenKind::DoubleArrowToken); | ||
if ($node->arrowToken) { | ||
$node->body = $this->parseExpression($node); | ||
$node->semicolon = $this->eat1(TokenKind::SemicolonToken); | ||
} else { | ||
$node->body = $this->parseCompoundStatement($node); | ||
} | ||
return $node; | ||
} | ||
|
||
/** | ||
* Parse a comma separated qualified name list (e.g. interfaces implemented by a class) | ||
* | ||
|
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
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 |
---|---|---|
|
@@ -135,6 +135,7 @@ | |
] | ||
} | ||
}, | ||
"propertyHooks": null, | ||
"semicolon": { | ||
"kind": "SemicolonToken", | ||
"textLength": 1 | ||
|
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not related to this PR but I guess we could consider bumping the minimum version to 8.0 or even 7.4? (nikic parser requires at least 7.4)