Skip to content

Support parsing php 8.0 named arguments #335

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

Merged
merged 1 commit into from
Aug 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Node/Expression/ArgumentExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
use Microsoft\PhpParser\Token;

class ArgumentExpression extends Expression {
/** @var Token|null for php named arguments. If this is set, byRefToken and dotDotDotToken will not be set. */
public $name;

/** @var Token|null */
public $colonToken;

/** @var Token|null */
public $byRefToken; // TODO removed in newer versions of PHP. Also only accept variable, not expression if byRef

Expand All @@ -20,6 +26,8 @@ class ArgumentExpression extends Expression {
public $expression;

const CHILD_NAMES = [
'name',
'colonToken',
'byRefToken',
'dotDotDotToken',
'expression'
Expand Down
24 changes: 21 additions & 3 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,17 @@ class Parser {
private $nameOrStaticOrReservedWordTokens;
private $reservedWordTokens;
private $keywordTokens;
private $argumentStartTokensSet;
// TODO consider validating parameter and return types on post-parse instead so we can be more permissive
private $parameterTypeDeclarationTokens;
private $returnTypeDeclarationTokens;

public function __construct() {
$this->reservedWordTokens = \array_values(TokenStringMaps::RESERVED_WORDS);
$this->keywordTokens = \array_values(TokenStringMaps::KEYWORDS);
$this->argumentStartTokensSet = \array_flip(TokenStringMaps::KEYWORDS);
unset($this->argumentStartTokensSet[TokenKind::YieldFromKeyword]);
$this->argumentStartTokensSet[TokenKind::DotDotDotToken] = '...';
$this->nameOrKeywordOrReservedWordTokens = \array_merge([TokenKind::Name], $this->keywordTokens, $this->reservedWordTokens);
$this->nameOrReservedWordTokens = \array_merge([TokenKind::Name], $this->reservedWordTokens);
$this->nameOrStaticOrReservedWordTokens = \array_merge([TokenKind::Name, TokenKind::StaticKeyword], $this->reservedWordTokens);
Expand Down Expand Up @@ -2762,16 +2766,30 @@ private function parseMemberName($parentNode) {
private function isArgumentExpressionStartFn() {
return function ($token) {
return
$token->kind === TokenKind::DotDotDotToken ? true : $this->isExpressionStart($token);
isset($this->argumentStartTokensSet[$token->kind]) || $this->isExpressionStart($token);
};
}

private function parseArgumentExpressionFn() {
return function ($parentNode) {
$argumentExpression = new ArgumentExpression();
$argumentExpression->parent = $parentNode;
$argumentExpression->byRefToken = $this->eatOptional1(TokenKind::AmpersandToken);
$argumentExpression->dotDotDotToken = $this->eatOptional1(TokenKind::DotDotDotToken);

$nextToken = $this->lexer->getTokensArray()[$this->lexer->getCurrentPosition()] ?? null;
if ($nextToken && $nextToken->kind === TokenKind::ColonToken) {
$name = $this->token;
$this->advanceToken();
if ($name->kind === TokenKind::YieldFromKeyword || !\in_array($name->kind, $this->nameOrKeywordOrReservedWordTokens)) {
$name = new SkippedToken($name);
} else {
$name->kind = TokenKind::Name;
}
$argumentExpression->name = $name;
$argumentExpression->colonToken = $this->eat1(TokenKind::ColonToken);
} else {
$argumentExpression->byRefToken = $this->eatOptional1(TokenKind::AmpersandToken);
$argumentExpression->dotDotDotToken = $this->eatOptional1(TokenKind::DotDotDotToken);
}
$argumentExpression->expression = $this->parseExpression($argumentExpression);
return $argumentExpression;
};
Expand Down
2 changes: 2 additions & 0 deletions tests/cases/parser/binaryAssignmentExpressions6.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
"children": [
{
"ArgumentExpression": {
"name": null,
"colonToken": null,
"byRefToken": null,
"dotDotDotToken": null,
"expression": {
Expand Down
6 changes: 6 additions & 0 deletions tests/cases/parser/callExpression10.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
"children": [
{
"ArgumentExpression": {
"name": null,
"colonToken": null,
"byRefToken": null,
"dotDotDotToken": null,
"expression": {
Expand All @@ -54,6 +56,8 @@
},
{
"ArgumentExpression": {
"name": null,
"colonToken": null,
"byRefToken": null,
"dotDotDotToken": null,
"expression": {
Expand All @@ -72,6 +76,8 @@
},
{
"ArgumentExpression": {
"name": null,
"colonToken": null,
"byRefToken": null,
"dotDotDotToken": null,
"expression": {
Expand Down
2 changes: 2 additions & 0 deletions tests/cases/parser/callExpression12.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
"children": [
{
"ArgumentExpression": {
"name": null,
"colonToken": null,
"byRefToken": null,
"dotDotDotToken": null,
"expression": {
Expand Down
2 changes: 2 additions & 0 deletions tests/cases/parser/callExpression15.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@
"children": [
{
"ArgumentExpression": {
"name": null,
"colonToken": null,
"byRefToken": null,
"dotDotDotToken": null,
"expression": {
Expand Down
2 changes: 2 additions & 0 deletions tests/cases/parser/callExpression2.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
"children": [
{
"ArgumentExpression": {
"name": null,
"colonToken": null,
"byRefToken": null,
"dotDotDotToken": null,
"expression": {
Expand Down
2 changes: 2 additions & 0 deletions tests/cases/parser/callExpression3.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
"children": [
{
"ArgumentExpression": {
"name": null,
"colonToken": null,
"byRefToken": null,
"dotDotDotToken": null,
"expression": {
Expand Down
4 changes: 4 additions & 0 deletions tests/cases/parser/callExpression4.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
"children": [
{
"ArgumentExpression": {
"name": null,
"colonToken": null,
"byRefToken": null,
"dotDotDotToken": null,
"expression": {
Expand Down Expand Up @@ -69,6 +71,8 @@
},
{
"ArgumentExpression": {
"name": null,
"colonToken": null,
"byRefToken": null,
"dotDotDotToken": null,
"expression": {
Expand Down
4 changes: 4 additions & 0 deletions tests/cases/parser/callExpression5.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
"children": [
{
"ArgumentExpression": {
"name": null,
"colonToken": null,
"byRefToken": null,
"dotDotDotToken": null,
"expression": {
Expand Down Expand Up @@ -69,6 +71,8 @@
},
{
"ArgumentExpression": {
"name": null,
"colonToken": null,
"byRefToken": null,
"dotDotDotToken": null,
"expression": {
Expand Down
4 changes: 4 additions & 0 deletions tests/cases/parser/callExpression6.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
"children": [
{
"ArgumentExpression": {
"name": null,
"colonToken": null,
"byRefToken": null,
"dotDotDotToken": null,
"expression": {
Expand Down Expand Up @@ -69,6 +71,8 @@
},
{
"ArgumentExpression": {
"name": null,
"colonToken": null,
"byRefToken": null,
"dotDotDotToken": {
"kind": "DotDotDotToken",
Expand Down
2 changes: 2 additions & 0 deletions tests/cases/parser/callExpression7.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
"children": [
{
"ArgumentExpression": {
"name": null,
"colonToken": null,
"byRefToken": null,
"dotDotDotToken": null,
"expression": {
Expand Down
2 changes: 2 additions & 0 deletions tests/cases/parser/callExpression8.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@
"children": [
{
"ArgumentExpression": {
"name": null,
"colonToken": null,
"byRefToken": null,
"dotDotDotToken": null,
"expression": {
Expand Down
2 changes: 2 additions & 0 deletions tests/cases/parser/callExpression9.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
"children": [
{
"ArgumentExpression": {
"name": null,
"colonToken": null,
"byRefToken": null,
"dotDotDotToken": null,
"expression": {
Expand Down
2 changes: 2 additions & 0 deletions tests/cases/parser/conditionalExpressions2.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@
"children": [
{
"ArgumentExpression": {
"name": null,
"colonToken": null,
"byRefToken": null,
"dotDotDotToken": null,
"expression": {
Expand Down
2 changes: 2 additions & 0 deletions tests/cases/parser/conditionalExpressions3.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@
"children": [
{
"ArgumentExpression": {
"name": null,
"colonToken": null,
"byRefToken": null,
"dotDotDotToken": null,
"expression": {
Expand Down
4 changes: 4 additions & 0 deletions tests/cases/parser/conditionalExpressions4.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@
"children": [
{
"ArgumentExpression": {
"name": null,
"colonToken": null,
"byRefToken": null,
"dotDotDotToken": null,
"expression": {
Expand All @@ -151,6 +153,8 @@
},
{
"ArgumentExpression": {
"name": null,
"colonToken": null,
"byRefToken": null,
"dotDotDotToken": null,
"expression": {
Expand Down
2 changes: 2 additions & 0 deletions tests/cases/parser/namedArgument.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php
echo strlen(ordinaryName: 'foo');
1 change: 1 addition & 0 deletions tests/cases/parser/namedArgument.php.diag
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
96 changes: 96 additions & 0 deletions tests/cases/parser/namedArgument.php.tree
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
{
"SourceFileNode": {
"statementList": [
{
"InlineHtml": {
"scriptSectionEndTag": null,
"text": null,
"scriptSectionStartTag": {
"kind": "ScriptSectionStartTag",
"textLength": 6
}
}
},
{
"ExpressionStatement": {
"expression": {
"EchoExpression": {
"echoKeyword": {
"kind": "EchoKeyword",
"textLength": 4
},
"expressions": {
"ExpressionList": {
"children": [
{
"CallExpression": {
"callableExpression": {
"QualifiedName": {
"globalSpecifier": null,
"relativeSpecifier": null,
"nameParts": [
{
"kind": "Name",
"textLength": 6
}
]
}
},
"openParen": {
"kind": "OpenParenToken",
"textLength": 1
},
"argumentExpressionList": {
"ArgumentExpressionList": {
"children": [
{
"ArgumentExpression": {
"name": {
"kind": "Name",
"textLength": 12
},
"colonToken": {
"kind": "ColonToken",
"textLength": 1
},
"byRefToken": null,
"dotDotDotToken": null,
"expression": {
"StringLiteral": {
"startQuote": null,
"children": {
"kind": "StringLiteralToken",
"textLength": 5
},
"endQuote": null
}
}
}
}
]
}
},
"closeParen": {
"kind": "CloseParenToken",
"textLength": 1
}
}
}
]
}
}
}
},
"semicolon": {
"kind": "SemicolonToken",
"textLength": 1
}
}
}
],
"endOfFileToken": {
"kind": "EndOfFileToken",
"textLength": 0
}
}
}
3 changes: 3 additions & 0 deletions tests/cases/parser/namedArgument2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php
echo strlen(string: 'foo');
echo strlen(default: 'foo');
1 change: 1 addition & 0 deletions tests/cases/parser/namedArgument2.php.diag
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
Loading