Parse template string variables, and the basic expressions that are allowed

This commit is contained in:
Rob Lourens 2017-08-10 11:26:27 -07:00
Родитель 5cff92320d
Коммит e40b820463
28 изменённых файлов: 1091 добавлений и 3 удалений

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

@ -1015,6 +1015,9 @@ class Parser {
case TokenKind::HeredocEnd:
$expression->endQuote = $this->eat($startQuoteKind, TokenKind::HeredocEnd);
return $expression;
case TokenKind::VariableName:
$expression->children[] = $this->parseTemplateStringExpression($expression);
continue;
default:
$expression->children[] = $this->getCurrentToken();
$this->advanceToken();
@ -1025,6 +1028,71 @@ class Parser {
return $expression;
}
/**
* Double-quoted and heredoc strings support a basic set of expression types, described in http://php.net/manual/en/language.types.string.php#language.types.string.parsing
* Supported: $x, $x->p, $x[0], $x[$y]
* Not supported: $x->p1->p2, $x[0][1], etc.
* Since there is a relatively small finite set of allowed forms, I implement it here rather than trying to reuse the general expression parsing code.
*/
private function parseTemplateStringExpression($parentNode) {
$token = $this->getCurrentToken();
if ($token->kind === TokenKind::VariableName) {
$var = $this->parseSimpleVariable($parentNode);
$token = $this->getCurrentToken();
if ($token->kind === TokenKind::OpenBracketToken) {
return $this->parseTemplateStringSubscriptExpression($var);
} else if ($token->kind === TokenKind::ArrowToken) {
return $this->parseTemplateStringMemberAccessExpression($var);
} else {
return $var;
}
}
return null;
}
private function parseTemplateStringSubscriptExpression($postfixExpression) : SubscriptExpression {
$subscriptExpression = new SubscriptExpression();
$subscriptExpression->parent = $postfixExpression->parent;
$postfixExpression->parent = $subscriptExpression;
$subscriptExpression->postfixExpression = $postfixExpression;
$subscriptExpression->openBracketOrBrace = $this->eat(TokenKind::OpenBracketToken); // Only [] syntax is supported, not {}
$token = $this->getCurrentToken();
if ($token->kind === TokenKind::VariableName) {
$subscriptExpression->accessExpression = $this->parseSimpleVariable($subscriptExpression);
} elseif ($token->kind === TokenKind::IntegerLiteralToken) {
$subscriptExpression->accessExpression = $this->parseNumericLiteralExpression($subscriptExpression);
} elseif ($token->kind === TokenKind::Name) {
$subscriptExpression->accessExpression = $this->parseTemplateStringSubscriptStringLiteral($subscriptExpression);
} else {
$subscriptExpression->accessExpression = new MissingToken(TokenKind::Expression, $token->fullStart);
}
$subscriptExpression->closeBracketOrBrace = $this->eat(TokenKind::CloseBracketToken);
return $subscriptExpression;
}
private function parseTemplateStringSubscriptStringLiteral($parentNode) : StringLiteral {
$expression = new StringLiteral();
$expression->parent = $parentNode;
$expression->children = $this->eat(TokenKind::Name);
return $expression;
}
private function parseTemplateStringMemberAccessExpression($expression) : MemberAccessExpression {
$memberAccessExpression = new MemberAccessExpression();
$memberAccessExpression->parent = $expression->parent;
$expression->parent = $memberAccessExpression;
$memberAccessExpression->dereferencableExpression = $expression;
$memberAccessExpression->arrowToken = $this->eat(TokenKind::ArrowToken);
$memberAccessExpression->memberName = $this->eat(TokenKind::Name);
return $memberAccessExpression;
}
private function parseNumericLiteralExpression($parentNode) {
$numericLiteral = new NumericLiteral();
$numericLiteral->parent = $parentNode;

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

@ -295,7 +295,7 @@ class PhpTokenizer implements TokenStreamProviderInterface {
T_STRING_VARNAME => TokenKind::StringVarname,
T_COMMENT => TokenKind::CommentToken,
T_DOC_COMMENT => TokenKind::DocCommentToken,
T_NUM_STRING => TokenKind::NumStringToken
T_NUM_STRING => TokenKind::IntegerLiteralToken
];
const PARSE_CONTEXT_TO_PREFIX = [

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

@ -214,7 +214,6 @@ class TokenKind {
const IntegerLiteralToken = 416;
const CommentToken = 417;
const DocCommentToken = 418;
const NumStringToken = 419;
// TODO type annotations - PHP7
}

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

@ -16,7 +16,7 @@
"textLength": 1
},
{
"kind": "NumStringToken",
"kind": "IntegerLiteralToken",
"textLength": 1
},
{

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

@ -0,0 +1,2 @@
<?php
"$x[a]"

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

@ -0,0 +1,34 @@
[
{
"kind": "ScriptSectionStartTag",
"textLength": 6
},
{
"kind": "DoubleQuoteToken",
"textLength": 1
},
{
"kind": "VariableName",
"textLength": 2
},
{
"kind": "OpenBracketToken",
"textLength": 1
},
{
"kind": "Name",
"textLength": 1
},
{
"kind": "CloseBracketToken",
"textLength": 1
},
{
"kind": "DoubleQuoteToken",
"textLength": 1
},
{
"kind": "EndOfFileToken",
"textLength": 0
}
]

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

@ -0,0 +1,3 @@
<?php
$a = "test";

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

@ -0,0 +1,56 @@
{
"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": {
"StringLiteral": {
"startQuote": null,
"children": {
"kind": "StringLiteralToken",
"textLength": 6
},
"endQuote": null
}
}
}
},
"semicolon": {
"kind": "SemicolonToken",
"textLength": 1
}
}
}
],
"endOfFileToken": {
"kind": "EndOfFileToken",
"textLength": 0
}
}
}

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

@ -0,0 +1,3 @@
<?php
$a = "abc $foo->";

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

@ -0,0 +1,77 @@
{
"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": {
"StringLiteral": {
"startQuote": {
"kind": "DoubleQuoteToken",
"textLength": 1
},
"children": [
{
"kind": "EncapsedAndWhitespace",
"textLength": 4
},
{
"Variable": {
"dollar": null,
"name": {
"kind": "VariableName",
"textLength": 4
}
}
},
{
"kind": "EncapsedAndWhitespace",
"textLength": 2
}
],
"endQuote": {
"kind": "DoubleQuoteToken",
"textLength": 1
}
}
}
}
},
"semicolon": {
"kind": "SemicolonToken",
"textLength": 1
}
}
}
],
"endOfFileToken": {
"kind": "EndOfFileToken",
"textLength": 0
}
}
}

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

@ -0,0 +1,3 @@
<?php
$a = "$foo[]";

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

@ -0,0 +1,86 @@
{
"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": {
"StringLiteral": {
"startQuote": {
"kind": "DoubleQuoteToken",
"textLength": 1
},
"children": [
{
"SubscriptExpression": {
"postfixExpression": {
"Variable": {
"dollar": null,
"name": {
"kind": "VariableName",
"textLength": 4
}
}
},
"openBracketOrBrace": {
"kind": "OpenBracketToken",
"textLength": 1
},
"accessExpression": {
"error": "MissingToken",
"kind": "Expression",
"textLength": 0
},
"closeBracketOrBrace": {
"kind": "CloseBracketToken",
"textLength": 1
}
}
}
],
"endQuote": {
"kind": "DoubleQuoteToken",
"textLength": 1
}
}
}
}
},
"semicolon": {
"kind": "SemicolonToken",
"textLength": 1
}
}
}
],
"endOfFileToken": {
"kind": "EndOfFileToken",
"textLength": 0
}
}
}

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

@ -0,0 +1,3 @@
<?php
$a = "test $var";

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

@ -0,0 +1,73 @@
{
"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": {
"StringLiteral": {
"startQuote": {
"kind": "DoubleQuoteToken",
"textLength": 1
},
"children": [
{
"kind": "EncapsedAndWhitespace",
"textLength": 5
},
{
"Variable": {
"dollar": null,
"name": {
"kind": "VariableName",
"textLength": 4
}
}
}
],
"endQuote": {
"kind": "DoubleQuoteToken",
"textLength": 1
}
}
}
}
},
"semicolon": {
"kind": "SemicolonToken",
"textLength": 1
}
}
}
],
"endOfFileToken": {
"kind": "EndOfFileToken",
"textLength": 0
}
}
}

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

@ -0,0 +1,3 @@
<?php
$a = "test ${var}";

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

@ -0,0 +1,86 @@
{
"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": {
"StringLiteral": {
"startQuote": {
"kind": "DoubleQuoteToken",
"textLength": 1
},
"children": [
{
"kind": "EncapsedAndWhitespace",
"textLength": 5
},
{
"kind": "DollarOpenBraceToken",
"textLength": 2
},
{
"error": "MissingToken",
"kind": "Expression",
"textLength": 0
},
{
"error": "MissingToken",
"kind": "CloseBraceToken",
"textLength": 0
},
{
"kind": "StringVarname",
"textLength": 3
},
{
"kind": "CloseBraceToken",
"textLength": 1
}
],
"endQuote": {
"kind": "DoubleQuoteToken",
"textLength": 1
}
}
}
}
},
"semicolon": {
"kind": "SemicolonToken",
"textLength": 1
}
}
}
],
"endOfFileToken": {
"kind": "EndOfFileToken",
"textLength": 0
}
}
}

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

@ -0,0 +1,3 @@
<?php
$a = "test ${$var}";

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

@ -0,0 +1,81 @@
{
"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": {
"StringLiteral": {
"startQuote": {
"kind": "DoubleQuoteToken",
"textLength": 1
},
"children": [
{
"kind": "EncapsedAndWhitespace",
"textLength": 5
},
{
"kind": "DollarOpenBraceToken",
"textLength": 2
},
{
"Variable": {
"dollar": null,
"name": {
"kind": "VariableName",
"textLength": 4
}
}
},
{
"kind": "CloseBraceToken",
"textLength": 1
}
],
"endQuote": {
"kind": "DoubleQuoteToken",
"textLength": 1
}
}
}
}
},
"semicolon": {
"kind": "SemicolonToken",
"textLength": 1
}
}
}
],
"endOfFileToken": {
"kind": "EndOfFileToken",
"textLength": 0
}
}
}

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

@ -0,0 +1,3 @@
<?php
$a = "test ${$foo->bar[0]}";

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

@ -0,0 +1,113 @@
{
"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": {
"StringLiteral": {
"startQuote": {
"kind": "DoubleQuoteToken",
"textLength": 1
},
"children": [
{
"kind": "EncapsedAndWhitespace",
"textLength": 5
},
{
"kind": "DollarOpenBraceToken",
"textLength": 2
},
{
"SubscriptExpression": {
"postfixExpression": {
"MemberAccessExpression": {
"dereferencableExpression": {
"Variable": {
"dollar": null,
"name": {
"kind": "VariableName",
"textLength": 4
}
}
},
"arrowToken": {
"kind": "ArrowToken",
"textLength": 2
},
"memberName": {
"kind": "Name",
"textLength": 3
}
}
},
"openBracketOrBrace": {
"kind": "OpenBracketToken",
"textLength": 1
},
"accessExpression": {
"NumericLiteral": {
"children": {
"kind": "IntegerLiteralToken",
"textLength": 1
}
}
},
"closeBracketOrBrace": {
"kind": "CloseBracketToken",
"textLength": 1
}
}
},
{
"kind": "CloseBraceToken",
"textLength": 1
}
],
"endQuote": {
"kind": "DoubleQuoteToken",
"textLength": 1
}
}
}
}
},
"semicolon": {
"kind": "SemicolonToken",
"textLength": 1
}
}
}
],
"endOfFileToken": {
"kind": "EndOfFileToken",
"textLength": 0
}
}
}

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

@ -0,0 +1,3 @@
<?php
$a = "test {$foo}";

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

@ -0,0 +1,81 @@
{
"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": {
"StringLiteral": {
"startQuote": {
"kind": "DoubleQuoteToken",
"textLength": 1
},
"children": [
{
"kind": "EncapsedAndWhitespace",
"textLength": 5
},
{
"kind": "OpenBraceDollarToken",
"textLength": 1
},
{
"Variable": {
"dollar": null,
"name": {
"kind": "VariableName",
"textLength": 4
}
}
},
{
"kind": "CloseBraceToken",
"textLength": 1
}
],
"endQuote": {
"kind": "DoubleQuoteToken",
"textLength": 1
}
}
}
}
},
"semicolon": {
"kind": "SemicolonToken",
"textLength": 1
}
}
}
],
"endOfFileToken": {
"kind": "EndOfFileToken",
"textLength": 0
}
}
}

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

@ -0,0 +1,3 @@
<?php
$a = "test {$foo->bar[0]}";

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

@ -0,0 +1,113 @@
{
"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": {
"StringLiteral": {
"startQuote": {
"kind": "DoubleQuoteToken",
"textLength": 1
},
"children": [
{
"kind": "EncapsedAndWhitespace",
"textLength": 5
},
{
"kind": "OpenBraceDollarToken",
"textLength": 1
},
{
"SubscriptExpression": {
"postfixExpression": {
"MemberAccessExpression": {
"dereferencableExpression": {
"Variable": {
"dollar": null,
"name": {
"kind": "VariableName",
"textLength": 4
}
}
},
"arrowToken": {
"kind": "ArrowToken",
"textLength": 2
},
"memberName": {
"kind": "Name",
"textLength": 3
}
}
},
"openBracketOrBrace": {
"kind": "OpenBracketToken",
"textLength": 1
},
"accessExpression": {
"NumericLiteral": {
"children": {
"kind": "IntegerLiteralToken",
"textLength": 1
}
}
},
"closeBracketOrBrace": {
"kind": "CloseBracketToken",
"textLength": 1
}
}
},
{
"kind": "CloseBraceToken",
"textLength": 1
}
],
"endQuote": {
"kind": "DoubleQuoteToken",
"textLength": 1
}
}
}
}
},
"semicolon": {
"kind": "SemicolonToken",
"textLength": 1
}
}
}
],
"endOfFileToken": {
"kind": "EndOfFileToken",
"textLength": 0
}
}
}

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

@ -0,0 +1,3 @@
<?php
$a = "abc $foo->bar def";

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

@ -0,0 +1,89 @@
{
"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": {
"StringLiteral": {
"startQuote": {
"kind": "DoubleQuoteToken",
"textLength": 1
},
"children": [
{
"kind": "EncapsedAndWhitespace",
"textLength": 4
},
{
"MemberAccessExpression": {
"dereferencableExpression": {
"Variable": {
"dollar": null,
"name": {
"kind": "VariableName",
"textLength": 4
}
}
},
"arrowToken": {
"kind": "ArrowToken",
"textLength": 2
},
"memberName": {
"kind": "Name",
"textLength": 3
}
}
},
{
"kind": "EncapsedAndWhitespace",
"textLength": 4
}
],
"endQuote": {
"kind": "DoubleQuoteToken",
"textLength": 1
}
}
}
}
},
"semicolon": {
"kind": "SemicolonToken",
"textLength": 1
}
}
}
],
"endOfFileToken": {
"kind": "EndOfFileToken",
"textLength": 0
}
}
}

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

@ -0,0 +1,3 @@
<?php
$a = "abc$foo[0]def";

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

@ -0,0 +1,97 @@
{
"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": {
"StringLiteral": {
"startQuote": {
"kind": "DoubleQuoteToken",
"textLength": 1
},
"children": [
{
"kind": "EncapsedAndWhitespace",
"textLength": 3
},
{
"SubscriptExpression": {
"postfixExpression": {
"Variable": {
"dollar": null,
"name": {
"kind": "VariableName",
"textLength": 4
}
}
},
"openBracketOrBrace": {
"kind": "OpenBracketToken",
"textLength": 1
},
"accessExpression": {
"NumericLiteral": {
"children": {
"kind": "IntegerLiteralToken",
"textLength": 1
}
}
},
"closeBracketOrBrace": {
"kind": "CloseBracketToken",
"textLength": 1
}
}
},
{
"kind": "EncapsedAndWhitespace",
"textLength": 3
}
],
"endQuote": {
"kind": "DoubleQuoteToken",
"textLength": 1
}
}
}
}
},
"semicolon": {
"kind": "SemicolonToken",
"textLength": 1
}
}
}
],
"endOfFileToken": {
"kind": "EndOfFileToken",
"textLength": 0
}
}
}