Skip to content

Commit ec691a0

Browse files
author
Felix Siebeneicker
committed
Making changes backwards compatible
1 parent a562154 commit ec691a0

24 files changed

+464
-307
lines changed

src/Node/Statement/DeclareStatement.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
namespace Microsoft\PhpParser\Node\Statement;
88

9-
use Microsoft\PhpParser\Node\DelimitedList\DeclareDirectiveList;
9+
use Microsoft\PhpParser\Node;
10+
use Microsoft\PhpParser\Node\DelimitedList;
1011
use Microsoft\PhpParser\Node\StatementNode;
1112
use Microsoft\PhpParser\Token;
1213

@@ -15,8 +16,10 @@ class DeclareStatement extends StatementNode {
1516
public $declareKeyword;
1617
/** @var Token */
1718
public $openParen;
18-
/** @var DeclareDirectiveList */
19-
public $declareDirectives;
19+
/** @var Node */
20+
public $declareDirective;
21+
/** @var DelimitedList\DeclareDirectiveList|null TODO: Merge with $declareDirective in a future backwards incompatible release. */
22+
public $otherDeclareDirectives;
2023
/** @var Token */
2124
public $closeParen;
2225
/** @var Token|null */
@@ -31,7 +34,8 @@ class DeclareStatement extends StatementNode {
3134
const CHILD_NAMES = [
3235
'declareKeyword',
3336
'openParen',
34-
'declareDirectives',
37+
'declareDirective',
38+
'otherDeclareDirectives',
3539
'closeParen',
3640
'colon',
3741
'statements',

src/Parser.php

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2308,14 +2308,7 @@ private function parseDeclareStatement($parentNode) {
23082308
$declareStatement->parent = $parentNode;
23092309
$declareStatement->declareKeyword = $this->eat1(TokenKind::DeclareKeyword);
23102310
$declareStatement->openParen = $this->eat1(TokenKind::OpenParenToken);
2311-
$declareStatement->declareDirectives = $this->parseDelimitedList(
2312-
DelimitedList\DeclareDirectiveList::class,
2313-
TokenKind::CommaToken,
2314-
function ($token) {
2315-
return $token->kind === TokenKind::Name;
2316-
},
2317-
$this->parseDeclareDirectiveFn(),
2318-
$declareStatement);
2311+
$this->parseAndSetDeclareDirectiveList($declareStatement);
23192312
$declareStatement->closeParen = $this->eat1(TokenKind::CloseParenToken);
23202313

23212314
if ($this->checkToken(TokenKind::SemicolonToken)) {
@@ -2332,6 +2325,54 @@ function ($token) {
23322325
return $declareStatement;
23332326
}
23342327

2328+
/**
2329+
* @param DeclareStatement $parentNode
2330+
*/
2331+
private function parseAndSetDeclareDirectiveList($parentNode) {
2332+
$declareDirectiveList = $this->parseDeclareDirectiveList($parentNode);
2333+
2334+
if (!$declareDirectiveList) {
2335+
$declareDirective = new DeclareDirective();
2336+
$declareDirective->parent = $parentNode;
2337+
2338+
$declareDirective->name = new MissingToken(TokenKind::Name, $this->token->fullStart);
2339+
$declareDirective->equals = new MissingToken(TokenKind::EqualsToken, $this->token->fullStart);
2340+
// TODO: This is matching the first token in $this::parseDeclareDirectiveFn.
2341+
// Probably best to emit a more general "literal error".
2342+
$declareDirective->literal = new MissingToken(TokenKind::FloatingLiteralToken, $this->token->fullStart);
2343+
2344+
$parentNode->declareDirective = $declareDirective;
2345+
return;
2346+
}
2347+
2348+
$declareDirective = array_shift($declareDirectiveList->children);
2349+
$parentNode->declareDirective = $declareDirective;
2350+
$declareDirective->parent = $parentNode;
2351+
2352+
if ($declareDirectiveList->children) {
2353+
$parentNode->otherDeclareDirectives = $declareDirectiveList;
2354+
}
2355+
}
2356+
2357+
/**
2358+
* @param DeclareStatement $parentNode
2359+
* @return DelimitedList\DeclareDirectiveList|null
2360+
*/
2361+
private function parseDeclareDirectiveList($parentNode) {
2362+
$declareDirectiveList = $this->parseDelimitedList(
2363+
DelimitedList\DeclareDirectiveList::class,
2364+
TokenKind::CommaToken,
2365+
function ($token) {
2366+
return $token->kind === TokenKind::Name;
2367+
},
2368+
$this->parseDeclareDirectiveFn(),
2369+
$parentNode,
2370+
false
2371+
);
2372+
2373+
return $declareDirectiveList;
2374+
}
2375+
23352376
private function parseDeclareDirectiveFn() {
23362377
return function ($parentNode) {
23372378
$declareDirective = new DeclareDirective();

tests/cases/parser/declareStatement1.php.tree

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,23 @@
2121
"kind": "OpenParenToken",
2222
"textLength": 1
2323
},
24-
"declareDirectives": {
25-
"DeclareDirectiveList": {
26-
"children": [
27-
{
28-
"DeclareDirective": {
29-
"name": {
30-
"kind": "Name",
31-
"textLength": 5
32-
},
33-
"equals": {
34-
"kind": "EqualsToken",
35-
"textLength": 1
36-
},
37-
"literal": {
38-
"kind": "IntegerLiteralToken",
39-
"textLength": 1
40-
}
41-
}
42-
}
43-
]
24+
"declareDirective": {
25+
"DeclareDirective": {
26+
"name": {
27+
"kind": "Name",
28+
"textLength": 5
29+
},
30+
"equals": {
31+
"kind": "EqualsToken",
32+
"textLength": 1
33+
},
34+
"literal": {
35+
"kind": "IntegerLiteralToken",
36+
"textLength": 1
37+
}
4438
}
4539
},
40+
"otherDeclareDirectives": null,
4641
"closeParen": {
4742
"kind": "CloseParenToken",
4843
"textLength": 1

tests/cases/parser/declareStatement10.php.tree

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,23 @@
2121
"kind": "OpenParenToken",
2222
"textLength": 1
2323
},
24-
"declareDirectives": {
25-
"DeclareDirectiveList": {
26-
"children": [
27-
{
28-
"DeclareDirective": {
29-
"name": {
30-
"kind": "Name",
31-
"textLength": 5
32-
},
33-
"equals": {
34-
"kind": "EqualsToken",
35-
"textLength": 1
36-
},
37-
"literal": {
38-
"kind": "FloatingLiteralToken",
39-
"textLength": 3
40-
}
41-
}
42-
}
43-
]
24+
"declareDirective": {
25+
"DeclareDirective": {
26+
"name": {
27+
"kind": "Name",
28+
"textLength": 5
29+
},
30+
"equals": {
31+
"kind": "EqualsToken",
32+
"textLength": 1
33+
},
34+
"literal": {
35+
"kind": "FloatingLiteralToken",
36+
"textLength": 3
37+
}
4438
}
4539
},
40+
"otherDeclareDirectives": null,
4641
"closeParen": {
4742
"kind": "CloseParenToken",
4843
"textLength": 1

tests/cases/parser/declareStatement11.php.tree

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,23 @@
2121
"kind": "OpenParenToken",
2222
"textLength": 1
2323
},
24-
"declareDirectives": {
25-
"DeclareDirectiveList": {
26-
"children": [
27-
{
28-
"DeclareDirective": {
29-
"name": {
30-
"kind": "Name",
31-
"textLength": 5
32-
},
33-
"equals": {
34-
"kind": "EqualsToken",
35-
"textLength": 1
36-
},
37-
"literal": {
38-
"kind": "FloatingLiteralToken",
39-
"textLength": 3
40-
}
41-
}
42-
}
43-
]
24+
"declareDirective": {
25+
"DeclareDirective": {
26+
"name": {
27+
"kind": "Name",
28+
"textLength": 5
29+
},
30+
"equals": {
31+
"kind": "EqualsToken",
32+
"textLength": 1
33+
},
34+
"literal": {
35+
"kind": "FloatingLiteralToken",
36+
"textLength": 3
37+
}
4438
}
4539
},
40+
"otherDeclareDirectives": null,
4641
"closeParen": {
4742
"kind": "CloseParenToken",
4843
"textLength": 1
Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,20 @@
1-
[]
1+
[
2+
{
3+
"kind": 0,
4+
"message": "'Name' expected.",
5+
"start": 16,
6+
"length": 0
7+
},
8+
{
9+
"kind": 0,
10+
"message": "'=' expected.",
11+
"start": 16,
12+
"length": 0
13+
},
14+
{
15+
"kind": 0,
16+
"message": "'FloatingLiteralToken' expected.",
17+
"start": 16,
18+
"length": 0
19+
}
20+
]

tests/cases/parser/declareStatement12.php.tree

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,26 @@
2121
"kind": "OpenParenToken",
2222
"textLength": 1
2323
},
24-
"declareDirectives": null,
24+
"declareDirective": {
25+
"DeclareDirective": {
26+
"name": {
27+
"error": "MissingToken",
28+
"kind": "Name",
29+
"textLength": 0
30+
},
31+
"equals": {
32+
"error": "MissingToken",
33+
"kind": "EqualsToken",
34+
"textLength": 0
35+
},
36+
"literal": {
37+
"error": "MissingToken",
38+
"kind": "FloatingLiteralToken",
39+
"textLength": 0
40+
}
41+
}
42+
},
43+
"otherDeclareDirectives": null,
2544
"closeParen": {
2645
"kind": "CloseParenToken",
2746
"textLength": 1

tests/cases/parser/declareStatement13.php.tree

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,23 @@
2121
"kind": "OpenParenToken",
2222
"textLength": 1
2323
},
24-
"declareDirectives": {
25-
"DeclareDirectiveList": {
26-
"children": [
27-
{
28-
"DeclareDirective": {
29-
"name": {
30-
"kind": "Name",
31-
"textLength": 5
32-
},
33-
"equals": {
34-
"kind": "EqualsToken",
35-
"textLength": 1
36-
},
37-
"literal": {
38-
"kind": "IntegerLiteralToken",
39-
"textLength": 1
40-
}
41-
}
42-
}
43-
]
24+
"declareDirective": {
25+
"DeclareDirective": {
26+
"name": {
27+
"kind": "Name",
28+
"textLength": 5
29+
},
30+
"equals": {
31+
"kind": "EqualsToken",
32+
"textLength": 1
33+
},
34+
"literal": {
35+
"kind": "IntegerLiteralToken",
36+
"textLength": 1
37+
}
4438
}
4539
},
40+
"otherDeclareDirectives": null,
4641
"closeParen": {
4742
"kind": "CloseParenToken",
4843
"textLength": 1
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
<?php
22

33
declare(strict_types=1, ticks=1);
4-
5-
echo "This can be parsed\n";

0 commit comments

Comments
 (0)