Skip to content

Support parsing PHP 7.4's arrow functions #293

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
Jul 1, 2019
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
48 changes: 48 additions & 0 deletions src/Node/Expression/ArrowFunctionCreationExpression.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?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\Expression;

use Microsoft\PhpParser\FunctionLike;
use Microsoft\PhpParser\Node;
use Microsoft\PhpParser\Node\Expression;
use Microsoft\PhpParser\Node\FunctionHeader;
use Microsoft\PhpParser\Node\FunctionReturnType;
use Microsoft\PhpParser\Token;

class ArrowFunctionCreationExpression extends Expression implements FunctionLike {
/** @var Token|null */
public $staticModifier;

use FunctionHeader, FunctionReturnType;

/** @var Token `=>` */
public $arrowToken;

/** @var Node|Token */
public $resultExpression;

const CHILD_NAMES = [
'staticModifier',

// FunctionHeader
'functionKeyword',
'byRefToken',
'name',
'openParen',
'parameters',
'closeParen',

// FunctionReturnType
'colonToken',
'questionToken',
'returnType',

// body
'arrowToken',
'resultExpression',
];
}
2 changes: 2 additions & 0 deletions src/Node/QualifiedName.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Microsoft\PhpParser\NamespacedNameTrait;
use Microsoft\PhpParser\Node;
use Microsoft\PhpParser\Node\Expression\AnonymousFunctionCreationExpression;
use Microsoft\PhpParser\Node\Expression\ArrowFunctionCreationExpression;
use Microsoft\PhpParser\Node\Expression\CallExpression;
use Microsoft\PhpParser\Node\Expression\ObjectCreationExpression;
use Microsoft\PhpParser\ResolvedName;
Expand Down Expand Up @@ -180,6 +181,7 @@ private function isConstantName() : bool {
$this->parent instanceof Node\Expression\MemberAccessExpression || $this->parent instanceof CallExpression ||
$this->parent instanceof ObjectCreationExpression ||
$this->parent instanceof Node\Expression\ScopedPropertyAccessExpression || $this->parent instanceof AnonymousFunctionCreationExpression ||
$this->parent instanceof ArrowFunctionCreationExpression ||
($this->parent instanceof Node\Expression\BinaryExpression && $this->parent->operator->kind === TokenKind::InstanceOfKeyword)
);
}
Expand Down
50 changes: 47 additions & 3 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
AnonymousFunctionCreationExpression,
ArgumentExpression,
ArrayCreationExpression,
ArrowFunctionCreationExpression,
AssignmentExpression,
BinaryExpression,
BracedExpression,
Expand Down Expand Up @@ -563,7 +564,7 @@ private function parseStatementFn() {
// function-static-declaration
case TokenKind::StaticKeyword:
// Check that this is not an anonymous-function-creation-expression
if (!$this->lookahead([TokenKind::FunctionKeyword, TokenKind::OpenParenToken, TokenKind::ColonColonToken])) {
if (!$this->lookahead([TokenKind::FunctionKeyword, TokenKind::FnKeyword, TokenKind::OpenParenToken, TokenKind::ColonColonToken])) {
return $this->parseFunctionStaticDeclaration($parentNode);
}
break;
Expand Down Expand Up @@ -899,6 +900,7 @@ private function isExpressionStartFn() {
// anonymous-function-creation-expression
case TokenKind::StaticKeyword:
case TokenKind::FunctionKeyword:
case TokenKind::FnKeyword:
return true;
}
return \in_array($token->kind, $this->reservedWordTokens, true);
Expand Down Expand Up @@ -978,12 +980,13 @@ private function parsePrimaryExpression($parentNode) {
case TokenKind::StaticKeyword:
// handle `static::`, `static(`, `new static;`, `instanceof static`
if (($this->lookahead([TokenKind::ColonColonToken, TokenKind::OpenParenToken])) ||
(!$this->lookahead(TokenKind::FunctionKeyword))
(!$this->lookahead([TokenKind::FunctionKeyword, TokenKind::FnKeyword]))
) {
return $this->parseQualifiedName($parentNode);
}
// Could be `static function` anonymous function creation expression, so flow through
case TokenKind::FunctionKeyword:
case TokenKind::FnKeyword:
return $this->parseAnonymousFunctionCreationExpression($parentNode);

case TokenKind::TrueReservedWord:
Expand Down Expand Up @@ -3326,15 +3329,56 @@ private function parseCastExpressionGranular($parentNode) {
}

private function parseAnonymousFunctionCreationExpression($parentNode) {
$staticModifier = $this->eatOptional1(TokenKind::StaticKeyword);
if ($this->getCurrentToken()->kind === TokenKind::FnKeyword) {
return $this->parseArrowFunctionCreationExpression($parentNode, $staticModifier);
}
$anonymousFunctionCreationExpression = new AnonymousFunctionCreationExpression();
$anonymousFunctionCreationExpression->parent = $parentNode;

$anonymousFunctionCreationExpression->staticModifier = $this->eatOptional1(TokenKind::StaticKeyword);
$anonymousFunctionCreationExpression->staticModifier = $staticModifier;
$this->parseFunctionType($anonymousFunctionCreationExpression, false, true);

return $anonymousFunctionCreationExpression;
}

private function parseArrowFunctionCreationExpression($parentNode, $staticModifier) : ArrowFunctionCreationExpression {
$arrowFunction = new ArrowFunctionCreationExpression();
$arrowFunction->parent = $parentNode;
$arrowFunction->staticModifier = $staticModifier;

$arrowFunction->functionKeyword = $this->eat1(TokenKind::FnKeyword);
$arrowFunction->byRefToken = $this->eatOptional1(TokenKind::AmpersandToken);
$arrowFunction->name = $this->eatOptional($this->nameOrKeywordOrReservedWordTokens);

if (isset($arrowFunction->name)) {
// Anonymous functions should not have names.
// This is based on the code for AnonymousFunctionCreationExpression.
$arrowFunction->name->kind = TokenKind::Name;
$arrowFunction->name = new SkippedToken($arrowFunction->name); // TODO instead handle this during post-walk
}

$arrowFunction->openParen = $this->eat1(TokenKind::OpenParenToken);
$arrowFunction->parameters = $this->parseDelimitedList(
DelimitedList\ParameterDeclarationList::class,
TokenKind::CommaToken,
$this->isParameterStartFn(),
$this->parseParameterFn(),
$arrowFunction);
$arrowFunction->closeParen = $this->eat1(TokenKind::CloseParenToken);

if ($this->checkToken(TokenKind::ColonToken)) {
$arrowFunction->colonToken = $this->eat1(TokenKind::ColonToken);
$arrowFunction->questionToken = $this->eatOptional1(TokenKind::QuestionToken);
$arrowFunction->returnType = $this->parseReturnTypeDeclaration($arrowFunction);
}

$arrowFunction->arrowToken = $this->eat1(TokenKind::DoubleArrowToken);
$arrowFunction->resultExpression = $this->parseExpression($arrowFunction);

return $arrowFunction;
}

private function parseAnonymousFunctionUseClause($parentNode) {
$anonymousFunctionUseClause = new AnonymousFunctionUseClause();
$anonymousFunctionUseClause->parent = $parentNode;
Expand Down
4 changes: 3 additions & 1 deletion src/PhpTokenizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@

namespace Microsoft\PhpParser;

// If this predates PHP 7.4, T_COALESCE_EQUAL is unavailable.
// If this predates PHP 7.4, T_COALESCE_EQUAL and T_FN are unavailable.
// The replacement value is arbitrary - it just has to be different from other values of token constants.
define(__NAMESPACE__ . '\T_COALESCE_EQUAL', defined('T_COALESCE_EQUAL') ? constant('T_COALESCE_EQUAL') : 'T_COALESCE_EQUAL');
define(__NAMESPACE__ . '\T_FN', defined('T_FN') ? constant('T_FN') : 'T_FN');

/**
* Tokenizes content using PHP's built-in `token_get_all`, and converts to "lightweight" Token representation.
Expand Down Expand Up @@ -192,6 +193,7 @@ public static function getTokensArrayFromContent(
T_FINALLY => TokenKind::FinallyKeyword,
T_FOR => TokenKind::ForKeyword,
T_FOREACH => TokenKind::ForeachKeyword,
T_FN => TokenKind::FnKeyword,
T_FUNCTION => TokenKind::FunctionKeyword,
T_GLOBAL => TokenKind::GlobalKeyword,
T_GOTO => TokenKind::GotoKeyword,
Expand Down
1 change: 1 addition & 0 deletions src/TokenKind.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class TokenKind {
const XorKeyword = 165;
const YieldKeyword = 166;
const YieldFromKeyword = 167;
const FnKeyword = 168;

const OpenBracketToken = 201;
const CloseBracketToken = 202;
Expand Down
1 change: 1 addition & 0 deletions src/TokenStringMaps.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class TokenStringMaps {
"finally" => TokenKind::FinallyKeyword,
"for" => TokenKind::ForKeyword,
"foreach" => TokenKind::ForeachKeyword,
"fn" => TokenKind::FnKeyword,
"function" => TokenKind::FunctionKeyword,
"global" => TokenKind::GlobalKeyword,
"goto" => TokenKind::GotoKeyword,
Expand Down
9 changes: 9 additions & 0 deletions tests/cases/parser74/arrowFunction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
$f = fn(array $x) => $x;
fn(): int => $x;
fn($x = 42) => yield $x;
fn(&$x) => $x;
fn&($x) => $x;
fn($x, ...$rest) => $rest;
static fn() => 1;
$f = static fn() => 2;
1 change: 1 addition & 0 deletions tests/cases/parser74/arrowFunction.php.diag
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
Loading