Fixes #188: Fix parsing empty variables

Previously, the fallback case when something beginning
with the token `$` wasn't a simple variable
would be to assume it would be a complex variable
such as `$$x`

Check that the first token would be valid before recursing.
This commit is contained in:
Tyson Andre 2018-02-15 20:59:42 -08:00
Родитель 59351ce2a6
Коммит 0360257ceb
4 изменённых файлов: 83 добавлений и 5 удалений

Просмотреть файл

@ -2066,10 +2066,19 @@ class Parser {
$variable->dollar = $this->eat1(TokenKind::DollarToken);
$token = $this->getCurrentToken();
$variable->name =
$token->kind === TokenKind::OpenBraceToken ?
$this->parseBracedExpression($variable) :
$this->parseSimpleVariable($variable);
switch ($token->kind) {
case TokenKind::OpenBraceToken:
$variable->name = $this->parseBracedExpression($variable);
break;
case TokenKind::VariableName:
case TokenKind::StringVarname:
case TokenKind::DollarToken:
$variable->name = $this->parseSimpleVariable($variable);
break;
default:
$variable->name = new MissingToken(TokenKind::VariableName, $token->fullStart);
break;
}
} elseif ($token->kind === TokenKind::VariableName || $token->kind === TokenKind::StringVarname) {
// TODO consider splitting into dollar and name.
// StringVarname is the variable name without $, used in a template string e.g. `"${foo}"`
@ -2089,7 +2098,6 @@ class Parser {
TokenKind::YieldFromKeyword,
TokenKind::YieldKeyword
);
$yieldExpression->arrayElement = $this->parseArrayElement($yieldExpression);
return $yieldExpression;
}

Просмотреть файл

@ -0,0 +1,3 @@
<?php
$x = $;

Просмотреть файл

@ -0,0 +1,8 @@
[
{
"kind": 0,
"message": "'VariableName' expected.",
"start": 13,
"length": 0
}
]

Просмотреть файл

@ -0,0 +1,59 @@
{
"SourceFileNode": {
"statementList": [
{
"InlineHtml": {
"scriptSectionEndTag": null,
"text": null,
"scriptSectionStartTag": {
"kind": "ScriptSectionStartTag",
"textLength": 6
}
}
},
{
"ExpressionStatement": {
"expression": {
"AssignmentExpression": {
"leftOperand": {
"Variable": {
"dollar": null,
"name": {
"kind": "VariableName",
"textLength": 2
}
}
},
"operator": {
"kind": "EqualsToken",
"textLength": 1
},
"byRef": null,
"rightOperand": {
"Variable": {
"dollar": {
"kind": "DollarToken",
"textLength": 1
},
"name": {
"error": "MissingToken",
"kind": "VariableName",
"textLength": 0
}
}
}
}
},
"semicolon": {
"kind": "SemicolonToken",
"textLength": 1
}
}
}
],
"endOfFileToken": {
"kind": "EndOfFileToken",
"textLength": 0
}
}
}