Skip to content

Commit 1bfbd22

Browse files
authored
Merge pull request #233 from TysonAndre/fix-empty-var
Fixes #188: Fix parsing empty variables
2 parents 59351ce + 0360257 commit 1bfbd22

File tree

4 files changed

+83
-5
lines changed

4 files changed

+83
-5
lines changed

src/Parser.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2066,10 +2066,19 @@ private function parseSimpleVariableFn() {
20662066
$variable->dollar = $this->eat1(TokenKind::DollarToken);
20672067
$token = $this->getCurrentToken();
20682068

2069-
$variable->name =
2070-
$token->kind === TokenKind::OpenBraceToken ?
2071-
$this->parseBracedExpression($variable) :
2072-
$this->parseSimpleVariable($variable);
2069+
switch ($token->kind) {
2070+
case TokenKind::OpenBraceToken:
2071+
$variable->name = $this->parseBracedExpression($variable);
2072+
break;
2073+
case TokenKind::VariableName:
2074+
case TokenKind::StringVarname:
2075+
case TokenKind::DollarToken:
2076+
$variable->name = $this->parseSimpleVariable($variable);
2077+
break;
2078+
default:
2079+
$variable->name = new MissingToken(TokenKind::VariableName, $token->fullStart);
2080+
break;
2081+
}
20732082
} elseif ($token->kind === TokenKind::VariableName || $token->kind === TokenKind::StringVarname) {
20742083
// TODO consider splitting into dollar and name.
20752084
// StringVarname is the variable name without $, used in a template string e.g. `"${foo}"`
@@ -2089,7 +2098,6 @@ private function parseYieldExpression($parentNode) {
20892098
TokenKind::YieldFromKeyword,
20902099
TokenKind::YieldKeyword
20912100
);
2092-
20932101
$yieldExpression->arrayElement = $this->parseArrayElement($yieldExpression);
20942102
return $yieldExpression;
20952103
}

tests/cases/parser/variables6.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
$x = $;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[
2+
{
3+
"kind": 0,
4+
"message": "'VariableName' expected.",
5+
"start": 13,
6+
"length": 0
7+
}
8+
]
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
"Variable": {
34+
"dollar": {
35+
"kind": "DollarToken",
36+
"textLength": 1
37+
},
38+
"name": {
39+
"error": "MissingToken",
40+
"kind": "VariableName",
41+
"textLength": 0
42+
}
43+
}
44+
}
45+
}
46+
},
47+
"semicolon": {
48+
"kind": "SemicolonToken",
49+
"textLength": 1
50+
}
51+
}
52+
}
53+
],
54+
"endOfFileToken": {
55+
"kind": "EndOfFileToken",
56+
"textLength": 0
57+
}
58+
}
59+
}

0 commit comments

Comments
 (0)