Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Refs #204: Support for the ... operator in function calls implemented
  • Loading branch information
Manuel Pichler committed Jan 6, 2017
1 parent e805725 commit 078e532
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/main/php/PDepend/Source/Language/PHP/AbstractPHPParser.php
Expand Up @@ -46,6 +46,7 @@
use PDepend\Source\AST\AbstractASTClassOrInterface;
use PDepend\Source\AST\AbstractASTType;
use PDepend\Source\AST\ASTAllocationExpression;
use PDepend\Source\AST\ASTArguments;
use PDepend\Source\AST\ASTArray;
use PDepend\Source\AST\ASTClass;
use PDepend\Source\AST\ASTDeclareStatement;
Expand Down Expand Up @@ -2532,12 +2533,11 @@ private function parseAlternativeScopeTermination($tokenType)
* This method parses multiple expressions and adds them as children to the
* given <b>$exprList</b> node.
*
* @param \PDepend\Source\AST\ASTNode $exprList Parent that accepts multiple expr.
*
* @param \PDepend\Source\AST\ASTNode
* @return \PDepend\Source\AST\ASTNode
* @since 1.0.0
*/
private function parseExpressionList(\PDepend\Source\AST\ASTNode $exprList)
private function parseExpressionList(ASTNode $exprList)
{
$this->consumeComments();
while ($expr = $this->parseOptionalExpression()) {
Expand Down Expand Up @@ -4225,13 +4225,22 @@ protected function parseArguments()
$this->consumeComments();

if (Tokens::T_PARENTHESIS_CLOSE !== $this->tokenizer->peek()) {
$arguments = $this->parseExpressionList($arguments);
$arguments = $this->parseArgumentList($arguments);
}
$this->consumeToken(Tokens::T_PARENTHESIS_CLOSE);

return $this->setNodePositionsAndReturn($arguments);
}

/**
* @param \PDepend\Source\AST\ASTArguments $arguments
* @return \PDepend\Source\AST\ASTArguments
*/
protected function parseArgumentList(ASTArguments $arguments)
{
return $this->parseExpressionList($arguments);
}

/**
* This method implements the parsing for various expression types like
* variables, object/static method. All these expressions are valid in
Expand Down
32 changes: 32 additions & 0 deletions src/main/php/PDepend/Source/Language/PHP/PHPParserVersion56.php
Expand Up @@ -43,6 +43,7 @@

namespace PDepend\Source\Language\PHP;

use PDepend\Source\AST\ASTArguments;
use PDepend\Source\AST\ASTValue;
use PDepend\Source\Parser\UnexpectedTokenException;
use PDepend\Source\Tokenizer\Tokenizer;
Expand Down Expand Up @@ -321,4 +322,35 @@ protected function parseExpressionVersion56()
return $expr;
}
}

/**
* @param \PDepend\Source\AST\ASTArguments $arguments
* @return \PDepend\Source\AST\ASTArguments
*/
protected function parseArgumentList(ASTArguments $arguments)
{
while (true) {
$this->consumeComments();
if (Tokens::T_ELLIPSIS === $this->tokenizer->peek()) {
$this->consumeToken(Tokens::T_ELLIPSIS);
}

$this->consumeComments();
if (null === ($expr = $this->parseOptionalExpression())) {
break;
}

$arguments->addChild($expr);

$this->consumeComments();
if (Tokens::T_COMMA === $this->tokenizer->peek()) {
$this->consumeToken(Tokens::T_COMMA);
$this->consumeComments();

continue;
}
}

return $arguments;
}
}
Expand Up @@ -231,4 +231,12 @@ public function testPowExpressionInFieldDeclaration()

$this->assertNotNull($node);
}

/**
* @return void
*/
public function testEllipsisOperatorInFunctionCall()
{
$this->assertNotNull($this->parseCodeResourceForTest());
}
}
Expand Up @@ -111,6 +111,16 @@ public function testListKeywordAsFunctionNameThrowsException()
$this->parseCodeResourceForTest();
}

/**
* @return void
* @expectedException \PDepend\Source\Parser\UnexpectedTokenException
* @expectedExceptionMessageRegExp (^Unexpected token: \.\.\., line: 6, col: 9, file: )
*/
public function testEllipsisOperatorInFunctionCallThrowsException()
{
$this->parseCodeResourceForTest();
}

/**
* @param \PDepend\Source\Tokenizer\Tokenizer $tokenizer
* @param \PDepend\Source\Builder\Builder $builder
Expand Down
Expand Up @@ -169,6 +169,14 @@ public function testGroupUseStatementThrowsException()
* @expectedExceptionMessageRegExp (^Unexpected token: ::, line: 8, col: 24, file: )
*/
public function testUniformVariableSyntaxThrowsException()
{
$this->parseCodeResourceForTest();
}

/**
* @return void
*/
public function testEllipsisOperatorInFunctionCall()
{
$this->assertNotNull($this->parseCodeResourceForTest());
}
Expand Down
@@ -0,0 +1,11 @@
<?php
function foo($a, $b) {
}

function bar($x, $y) {
foo(...[$x, $y]);
}

function baz() {
bar(42, ...[23]);
}
@@ -0,0 +1,11 @@
<?php
function foo($a, $b) {
}

function bar($x, $y) {
foo(...[$x, $y]);
}

function baz() {
bar(42, ...[23]);
}
@@ -0,0 +1,11 @@
<?php
function foo($a, $b) {
}

function bar($x, $y) {
foo(...[$x, $y]);
}

function baz() {
bar(42, ...[23]);
}

0 comments on commit 078e532

Please sign in to comment.