Skip to content

Commit 10e901a

Browse files
authored
Merge pull request #210 from MaartenStaa/fix-new-static-and-instanceof-static
Fix #194 - oddities around the static keyword.
2 parents c884bce + 9382009 commit 10e901a

18 files changed

+488
-7
lines changed

src/Parser.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -961,8 +961,10 @@ private function parsePrimaryExpression($parentNode) {
961961

962962
// anonymous-function-creation-expression
963963
case TokenKind::StaticKeyword:
964-
// handle `static::`, `static(`
965-
if ($this->lookahead([TokenKind::ColonColonToken, TokenKind::OpenParenToken])) {
964+
// handle `static::`, `static(`, `new static;`, `instanceof static`
965+
if (($this->lookahead([TokenKind::ColonColonToken, TokenKind::OpenParenToken])) ||
966+
(!$this->lookahead(TokenKind::FunctionKeyword))
967+
) {
966968
return $this->parseQualifiedName($parentNode);
967969
}
968970
// Could be `static function` anonymous function creation expression, so flow through
@@ -2490,7 +2492,6 @@ private function parseObjectCreationExpression($parentNode) {
24902492
$this->isParsingObjectCreationExpression = true;
24912493
$objectCreationExpression->classTypeDesignator =
24922494
$this->eatOptional1(TokenKind::ClassKeyword) ??
2493-
$this->eatOptional1(TokenKind::StaticKeyword) ??
24942495
$this->parseExpression($objectCreationExpression);
24952496

24962497
$this->isParsingObjectCreationExpression = false;

tests/cases/parser/ifStatement1_6.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
3+
if (static::$a instanceof static) {
4+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[]
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
{
2+
"SourceFileNode": {
3+
"statementList": [
4+
{
5+
"InlineHtml": {
6+
"scriptSectionEndTag": null,
7+
"text": null,
8+
"scriptSectionStartTag": {
9+
"kind": "ScriptSectionStartTag",
10+
"textLength": 6
11+
}
12+
}
13+
},
14+
{
15+
"IfStatementNode": {
16+
"ifKeyword": {
17+
"kind": "IfKeyword",
18+
"textLength": 2
19+
},
20+
"openParen": {
21+
"kind": "OpenParenToken",
22+
"textLength": 1
23+
},
24+
"expression": {
25+
"BinaryExpression": {
26+
"leftOperand": {
27+
"ScopedPropertyAccessExpression": {
28+
"scopeResolutionQualifier": {
29+
"QualifiedName": {
30+
"globalSpecifier": null,
31+
"relativeSpecifier": null,
32+
"nameParts": [
33+
{
34+
"kind": "Name",
35+
"textLength": 6
36+
}
37+
]
38+
}
39+
},
40+
"doubleColon": {
41+
"kind": "ColonColonToken",
42+
"textLength": 2
43+
},
44+
"memberName": {
45+
"Variable": {
46+
"dollar": null,
47+
"name": {
48+
"kind": "VariableName",
49+
"textLength": 2
50+
}
51+
}
52+
}
53+
}
54+
},
55+
"operator": {
56+
"kind": "InstanceOfKeyword",
57+
"textLength": 10
58+
},
59+
"rightOperand": {
60+
"QualifiedName": {
61+
"globalSpecifier": null,
62+
"relativeSpecifier": null,
63+
"nameParts": [
64+
{
65+
"kind": "Name",
66+
"textLength": 6
67+
}
68+
]
69+
}
70+
}
71+
}
72+
},
73+
"closeParen": {
74+
"kind": "CloseParenToken",
75+
"textLength": 1
76+
},
77+
"colon": null,
78+
"statements": {
79+
"CompoundStatementNode": {
80+
"openBrace": {
81+
"kind": "OpenBraceToken",
82+
"textLength": 1
83+
},
84+
"statements": [],
85+
"closeBrace": {
86+
"kind": "CloseBraceToken",
87+
"textLength": 1
88+
}
89+
}
90+
},
91+
"elseIfClauses": [],
92+
"elseClause": null,
93+
"endifKeyword": null,
94+
"semicolon": null
95+
}
96+
}
97+
],
98+
"endOfFileToken": {
99+
"kind": "EndOfFileToken",
100+
"textLength": 0
101+
}
102+
}
103+
}

tests/cases/parser/objectCreationExpression13.php.tree

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,16 @@
3636
"textLength": 3
3737
},
3838
"classTypeDesignator": {
39-
"kind": "StaticKeyword",
40-
"textLength": 6
39+
"QualifiedName": {
40+
"globalSpecifier": null,
41+
"relativeSpecifier": null,
42+
"nameParts": [
43+
{
44+
"kind": "Name",
45+
"textLength": 6
46+
}
47+
]
48+
}
4149
},
4250
"openParen": null,
4351
"argumentExpressionList": null,

tests/cases/parser/objectCreationExpression14.php.tree

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,16 @@
3636
"textLength": 3
3737
},
3838
"classTypeDesignator": {
39-
"kind": "StaticKeyword",
40-
"textLength": 6
39+
"QualifiedName": {
40+
"globalSpecifier": null,
41+
"relativeSpecifier": null,
42+
"nameParts": [
43+
{
44+
"kind": "Name",
45+
"textLength": 6
46+
}
47+
]
48+
}
4149
},
4250
"openParen": {
4351
"kind": "OpenParenToken",
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
$a = new static::$b();
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[]
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
{
2+
"SourceFileNode": {
3+
"statementList": [
4+
{
5+
"InlineHtml": {
6+
"scriptSectionEndTag": null,
7+
"text": null,
8+
"scriptSectionStartTag": {
9+
"kind": "ScriptSectionStartTag",
10+
"textLength": 6
11+
}
12+
}
13+
},
14+
{
15+
"ExpressionStatement": {
16+
"expression": {
17+
"AssignmentExpression": {
18+
"leftOperand": {
19+
"Variable": {
20+
"dollar": null,
21+
"name": {
22+
"kind": "VariableName",
23+
"textLength": 2
24+
}
25+
}
26+
},
27+
"operator": {
28+
"kind": "EqualsToken",
29+
"textLength": 1
30+
},
31+
"byRef": null,
32+
"rightOperand": {
33+
"ObjectCreationExpression": {
34+
"newKeword": {
35+
"kind": "NewKeyword",
36+
"textLength": 3
37+
},
38+
"classTypeDesignator": {
39+
"ScopedPropertyAccessExpression": {
40+
"scopeResolutionQualifier": {
41+
"QualifiedName": {
42+
"globalSpecifier": null,
43+
"relativeSpecifier": null,
44+
"nameParts": [
45+
{
46+
"kind": "Name",
47+
"textLength": 6
48+
}
49+
]
50+
}
51+
},
52+
"doubleColon": {
53+
"kind": "ColonColonToken",
54+
"textLength": 2
55+
},
56+
"memberName": {
57+
"Variable": {
58+
"dollar": null,
59+
"name": {
60+
"kind": "VariableName",
61+
"textLength": 2
62+
}
63+
}
64+
}
65+
}
66+
},
67+
"openParen": {
68+
"kind": "OpenParenToken",
69+
"textLength": 1
70+
},
71+
"argumentExpressionList": null,
72+
"closeParen": {
73+
"kind": "CloseParenToken",
74+
"textLength": 1
75+
},
76+
"classBaseClause": null,
77+
"classInterfaceClause": null,
78+
"classMembers": null
79+
}
80+
}
81+
}
82+
},
83+
"semicolon": {
84+
"kind": "SemicolonToken",
85+
"textLength": 1
86+
}
87+
}
88+
}
89+
],
90+
"endOfFileToken": {
91+
"kind": "EndOfFileToken",
92+
"textLength": 0
93+
}
94+
}
95+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
$a = new self;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[]
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
{
2+
"SourceFileNode": {
3+
"statementList": [
4+
{
5+
"InlineHtml": {
6+
"scriptSectionEndTag": null,
7+
"text": null,
8+
"scriptSectionStartTag": {
9+
"kind": "ScriptSectionStartTag",
10+
"textLength": 6
11+
}
12+
}
13+
},
14+
{
15+
"ExpressionStatement": {
16+
"expression": {
17+
"AssignmentExpression": {
18+
"leftOperand": {
19+
"Variable": {
20+
"dollar": null,
21+
"name": {
22+
"kind": "VariableName",
23+
"textLength": 2
24+
}
25+
}
26+
},
27+
"operator": {
28+
"kind": "EqualsToken",
29+
"textLength": 1
30+
},
31+
"byRef": null,
32+
"rightOperand": {
33+
"ObjectCreationExpression": {
34+
"newKeword": {
35+
"kind": "NewKeyword",
36+
"textLength": 3
37+
},
38+
"classTypeDesignator": {
39+
"QualifiedName": {
40+
"globalSpecifier": null,
41+
"relativeSpecifier": null,
42+
"nameParts": [
43+
{
44+
"kind": "Name",
45+
"textLength": 4
46+
}
47+
]
48+
}
49+
},
50+
"openParen": null,
51+
"argumentExpressionList": null,
52+
"closeParen": null,
53+
"classBaseClause": null,
54+
"classInterfaceClause": null,
55+
"classMembers": null
56+
}
57+
}
58+
}
59+
},
60+
"semicolon": {
61+
"kind": "SemicolonToken",
62+
"textLength": 1
63+
}
64+
}
65+
}
66+
],
67+
"endOfFileToken": {
68+
"kind": "EndOfFileToken",
69+
"textLength": 0
70+
}
71+
}
72+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
$a = new self();
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[]

0 commit comments

Comments
 (0)