Add VarLikeIdentifier

For representing Identifiers that have an implicit leading $.

With this done, maybe go one step further?
 * Rename VarLikeIdentifier -> VarIdentifier / VarName
 * Use VarIdentifier / VarName also as an inner node in Variable.
   Not sure if this adds any real value.
This commit is contained in:
Nikita Popov 2016-12-23 12:39:27 +01:00
parent d32d937d47
commit 0f582e1708
7 changed files with 32 additions and 11 deletions

View File

@ -517,7 +517,7 @@ property_declaration_list:
;
property_decl_name:
T_VARIABLE { $$ = maybeMakeIdent(parseVar($1)); }
T_VARIABLE { $$ = maybeMakeVarIdent(parseVar($1)); }
;
property_declaration:
@ -914,7 +914,7 @@ static_property:
static_property_simple_name:
T_VARIABLE
{ $var = parseVar($1); $$ = \is_string($var) ? maybeMakeIdent($var) : $var; }
{ $var = parseVar($1); $$ = \is_string($var) ? maybeMakeVarIdent($var) : $var; }
;
static_property_with_arrays:

View File

@ -522,7 +522,7 @@ property_declaration_list:
;
property_decl_name:
T_VARIABLE { $$ = maybeMakeIdent(parseVar($1)); }
T_VARIABLE { $$ = maybeMakeVarIdent(parseVar($1)); }
;
property_declaration:
@ -799,7 +799,7 @@ simple_variable:
static_member_prop_name:
simple_variable
{ $var = $1; $$ = \is_string($var) ? maybeMakeIdent($var) : $var; }
{ $var = $1; $$ = \is_string($var) ? maybeMakeVarIdent($var) : $var; }
;
static_member:

View File

@ -217,6 +217,13 @@ function resolveMacros($code) {
. '$this->startAttributeStack[#1] + $this->endAttributes) : ' . $args[0] . ')';
}
if ('maybeMakeVarIdent' == $name) {
assertArgs(1, $args, $name);
return '($this->useIdentifierNodes ? new Node\VarLikeIdentifier(' . $args[0] . ', '
. '$this->startAttributeStack[#1] + $this->endAttributes) : ' . $args[0] . ')';
}
if ('maybeMakeVar' == $name) {
assertArgs(1, $args, $name);

View File

@ -0,0 +1,14 @@
<?php
namespace PhpParser\Node;
/**
* Represents a name that is written in source code with a leading dollar,
* but is not a proper variable. The leading dollar is not stored as part of the name.
*
* Examples: Names in property declarations are formatted as variables. Names in static property
* lookups are also formatted as variables.
*/
class VarLikeIdentifier extends Identifier
{
}

View File

@ -2056,7 +2056,7 @@ class Php5 extends \PhpParser\ParserAbstract
}
protected function reduceRule285() {
$this->semValue = ($this->useIdentifierNodes ? new Node\Identifier(substr($this->semStack[$this->stackPos-(1-1)], 1), $this->startAttributeStack[$this->stackPos-(1-1)] + $this->endAttributes) : substr($this->semStack[$this->stackPos-(1-1)], 1));
$this->semValue = ($this->useIdentifierNodes ? new Node\VarLikeIdentifier(substr($this->semStack[$this->stackPos-(1-1)], 1), $this->startAttributeStack[$this->stackPos-(1-1)] + $this->endAttributes) : substr($this->semStack[$this->stackPos-(1-1)], 1));
}
protected function reduceRule286() {
@ -2973,7 +2973,7 @@ class Php5 extends \PhpParser\ParserAbstract
}
protected function reduceRule508() {
$var = substr($this->semStack[$this->stackPos-(1-1)], 1); $this->semValue = \is_string($var) ? ($this->useIdentifierNodes ? new Node\Identifier($var, $this->startAttributeStack[$this->stackPos-(1-1)] + $this->endAttributes) : $var) : $var;
$var = substr($this->semStack[$this->stackPos-(1-1)], 1); $this->semValue = \is_string($var) ? ($this->useIdentifierNodes ? new Node\VarLikeIdentifier($var, $this->startAttributeStack[$this->stackPos-(1-1)] + $this->endAttributes) : $var) : $var;
}
protected function reduceRule509() {

View File

@ -1951,7 +1951,7 @@ class Php7 extends \PhpParser\ParserAbstract
}
protected function reduceRule283() {
$this->semValue = ($this->useIdentifierNodes ? new Node\Identifier(substr($this->semStack[$this->stackPos-(1-1)], 1), $this->startAttributeStack[$this->stackPos-(1-1)] + $this->endAttributes) : substr($this->semStack[$this->stackPos-(1-1)], 1));
$this->semValue = ($this->useIdentifierNodes ? new Node\VarLikeIdentifier(substr($this->semStack[$this->stackPos-(1-1)], 1), $this->startAttributeStack[$this->stackPos-(1-1)] + $this->endAttributes) : substr($this->semStack[$this->stackPos-(1-1)], 1));
}
protected function reduceRule284() {
@ -2609,7 +2609,7 @@ class Php7 extends \PhpParser\ParserAbstract
}
protected function reduceRule445() {
$var = $this->semStack[$this->stackPos-(1-1)]; $this->semValue = \is_string($var) ? ($this->useIdentifierNodes ? new Node\Identifier($var, $this->startAttributeStack[$this->stackPos-(1-1)] + $this->endAttributes) : $var) : $var;
$var = $this->semStack[$this->stackPos-(1-1)]; $this->semValue = \is_string($var) ? ($this->useIdentifierNodes ? new Node\VarLikeIdentifier($var, $this->startAttributeStack[$this->stackPos-(1-1)] + $this->endAttributes) : $var) : $var;
}
protected function reduceRule446() {

View File

@ -133,13 +133,13 @@ array(
flags: MODIFIER_PUBLIC (1)
props: array(
0: Stmt_PropertyProperty(
name: Identifier(
name: VarLikeIdentifier(
name: foo
)
default: null
)
1: Stmt_PropertyProperty(
name: Identifier(
name: VarLikeIdentifier(
name: bar
)
default: Scalar_LNumber(
@ -284,7 +284,7 @@ array(
0: Foo
)
)
name: Identifier(
name: VarLikeIdentifier(
name: bar
)
)