Skip to content
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
12 changes: 12 additions & 0 deletions src/Node/DelimitedList/DeclareDirectiveList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?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\DelimitedList;

use Microsoft\PhpParser\Node\DelimitedList;

class DeclareDirectiveList extends DelimitedList {
}
4 changes: 4 additions & 0 deletions src/Node/Statement/DeclareStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace Microsoft\PhpParser\Node\Statement;

use Microsoft\PhpParser\Node;
use Microsoft\PhpParser\Node\DelimitedList;
use Microsoft\PhpParser\Node\StatementNode;
use Microsoft\PhpParser\Token;

Expand All @@ -17,6 +18,8 @@ class DeclareStatement extends StatementNode {
public $openParen;
/** @var Node */
public $declareDirective;
/** @var DelimitedList\DeclareDirectiveList|null TODO: Merge with $declareDirective in a future backwards incompatible release. */
public $otherDeclareDirectives;
/** @var Token */
public $closeParen;
/** @var Token|null */
Expand All @@ -32,6 +35,7 @@ class DeclareStatement extends StatementNode {
'declareKeyword',
'openParen',
'declareDirective',
'otherDeclareDirectives',
'closeParen',
'colon',
'statements',
Expand Down
90 changes: 70 additions & 20 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2308,7 +2308,7 @@ private function parseDeclareStatement($parentNode) {
$declareStatement->parent = $parentNode;
$declareStatement->declareKeyword = $this->eat1(TokenKind::DeclareKeyword);
$declareStatement->openParen = $this->eat1(TokenKind::OpenParenToken);
$declareStatement->declareDirective = $this->parseDeclareDirective($declareStatement);
$this->parseAndSetDeclareDirectiveList($declareStatement);
$declareStatement->closeParen = $this->eat1(TokenKind::CloseParenToken);

if ($this->checkToken(TokenKind::SemicolonToken)) {
Expand All @@ -2325,26 +2325,76 @@ private function parseDeclareStatement($parentNode) {
return $declareStatement;
}

private function parseDeclareDirective($parentNode) {
$declareDirective = new DeclareDirective();
/**
* @param DeclareStatement $parentNode
*/
private function parseAndSetDeclareDirectiveList($parentNode) {
$declareDirectiveList = $this->parseDeclareDirectiveList($parentNode);

if (!$declareDirectiveList) {
$declareDirective = new DeclareDirective();
$declareDirective->parent = $parentNode;

$declareDirective->name = new MissingToken(TokenKind::Name, $this->token->fullStart);
$declareDirective->equals = new MissingToken(TokenKind::EqualsToken, $this->token->fullStart);
// TODO: This is matching the first token in $this::parseDeclareDirectiveFn.
// Probably best to emit a more general "literal error".
$declareDirective->literal = new MissingToken(TokenKind::FloatingLiteralToken, $this->token->fullStart);

$parentNode->declareDirective = $declareDirective;
return;
}

$declareDirective = array_shift($declareDirectiveList->children);
$parentNode->declareDirective = $declareDirective;
$declareDirective->parent = $parentNode;
$declareDirective->name = $this->eat1(TokenKind::Name);
$declareDirective->equals = $this->eat1(TokenKind::EqualsToken);
$declareDirective->literal =
$this->eat(
TokenKind::FloatingLiteralToken,
TokenKind::IntegerLiteralToken,
TokenKind::DecimalLiteralToken,
TokenKind::OctalLiteralToken,
TokenKind::HexadecimalLiteralToken,
TokenKind::BinaryLiteralToken,
TokenKind::InvalidOctalLiteralToken,
TokenKind::InvalidHexadecimalLiteral,
TokenKind::InvalidBinaryLiteral,
TokenKind::StringLiteralToken
); // TODO simplify

return $declareDirective;

if ($declareDirectiveList->children) {
$parentNode->otherDeclareDirectives = $declareDirectiveList;
}
}

/**
* @param DeclareStatement $parentNode
* @return DelimitedList\DeclareDirectiveList|null
*/
private function parseDeclareDirectiveList($parentNode) {
$declareDirectiveList = $this->parseDelimitedList(
DelimitedList\DeclareDirectiveList::class,
TokenKind::CommaToken,
function ($token) {
return $token->kind === TokenKind::Name;
},
$this->parseDeclareDirectiveFn(),
$parentNode,
false
);

return $declareDirectiveList;
}

private function parseDeclareDirectiveFn() {
return function ($parentNode) {
$declareDirective = new DeclareDirective();
$declareDirective->parent = $parentNode;
$declareDirective->name = $this->eat1(TokenKind::Name);
$declareDirective->equals = $this->eat1(TokenKind::EqualsToken);
$declareDirective->literal =
$this->eat(
TokenKind::FloatingLiteralToken,
TokenKind::IntegerLiteralToken,
TokenKind::DecimalLiteralToken,
TokenKind::OctalLiteralToken,
TokenKind::HexadecimalLiteralToken,
TokenKind::BinaryLiteralToken,
TokenKind::InvalidOctalLiteralToken,
TokenKind::InvalidHexadecimalLiteral,
TokenKind::InvalidBinaryLiteral,
TokenKind::StringLiteralToken
); // TODO simplify

return $declareDirective;
};
}

private function parseSimpleVariable($parentNode) {
Expand Down
3 changes: 1 addition & 2 deletions tests/ParserGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

use Microsoft\PhpParser\Token;
use Microsoft\PhpParser\DiagnosticsProvider;
use PHPUnit\Framework\Test;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -123,7 +122,7 @@ public function testSpecOutputTreeClassificationAndLength($testCaseFile, $expect
}

public function outTreeProvider() {
$testCases = glob(__dir__ . "/cases/php-langspec/**/*.php");
$testCases = glob(__DIR__ . "/cases/php-langspec/**/*.php");
$skipped = json_decode(file_get_contents(__DIR__ . "/skipped.json"));

$testProviderArray = array();
Expand Down
1 change: 1 addition & 0 deletions tests/cases/parser/declareStatement1.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
}
}
},
"otherDeclareDirectives": null,
"closeParen": {
"kind": "CloseParenToken",
"textLength": 1
Expand Down
1 change: 1 addition & 0 deletions tests/cases/parser/declareStatement10.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
}
}
},
"otherDeclareDirectives": null,
"closeParen": {
"kind": "CloseParenToken",
"textLength": 1
Expand Down
1 change: 1 addition & 0 deletions tests/cases/parser/declareStatement11.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
}
}
},
"otherDeclareDirectives": null,
"closeParen": {
"kind": "CloseParenToken",
"textLength": 1
Expand Down
1 change: 1 addition & 0 deletions tests/cases/parser/declareStatement12.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
}
}
},
"otherDeclareDirectives": null,
"closeParen": {
"kind": "CloseParenToken",
"textLength": 1
Expand Down
1 change: 1 addition & 0 deletions tests/cases/parser/declareStatement13.php.tree
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
}
}
},
"otherDeclareDirectives": null,
"closeParen": {
"kind": "CloseParenToken",
"textLength": 1
Expand Down
3 changes: 3 additions & 0 deletions tests/cases/parser/declareStatement14.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

declare(strict_types=1, ticks=1);
1 change: 1 addition & 0 deletions tests/cases/parser/declareStatement14.php.diag
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
85 changes: 85 additions & 0 deletions tests/cases/parser/declareStatement14.php.tree
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
{
"SourceFileNode": {
"statementList": [
{
"InlineHtml": {
"scriptSectionEndTag": null,
"text": null,
"scriptSectionStartTag": {
"kind": "ScriptSectionStartTag",
"textLength": 6
}
}
},
{
"DeclareStatement": {
"declareKeyword": {
"kind": "DeclareKeyword",
"textLength": 7
},
"openParen": {
"kind": "OpenParenToken",
"textLength": 1
},
"declareDirective": {
"DeclareDirective": {
"name": {
"kind": "Name",
"textLength": 12
},
"equals": {
"kind": "EqualsToken",
"textLength": 1
},
"literal": {
"kind": "IntegerLiteralToken",
"textLength": 1
}
}
},
"otherDeclareDirectives": {
"DeclareDirectiveList": {
"children": [
{
"kind": "CommaToken",
"textLength": 1
},
{
"DeclareDirective": {
"name": {
"kind": "Name",
"textLength": 5
},
"equals": {
"kind": "EqualsToken",
"textLength": 1
},
"literal": {
"kind": "IntegerLiteralToken",
"textLength": 1
}
}
}
]
}
},
"closeParen": {
"kind": "CloseParenToken",
"textLength": 1
},
"colon": null,
"statements": null,
"enddeclareKeyword": null,
"semicolon": {
"kind": "SemicolonToken",
"textLength": 1
}
}
}
],
"endOfFileToken": {
"kind": "EndOfFileToken",
"textLength": 0
}
}
}
3 changes: 3 additions & 0 deletions tests/cases/parser/declareStatement15.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

declare(strict_types=1, ticks /** */);
14 changes: 14 additions & 0 deletions tests/cases/parser/declareStatement15.php.diag
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"kind": 0,
"message": "'=' expected.",
"start": 36,
"length": 0
},
{
"kind": 0,
"message": "'FloatingLiteralToken' expected.",
"start": 36,
"length": 0
}
]
Loading