mirror of
https://github.com/nikic/PHP-Parser.git
synced 2025-07-20 05:41:35 +02:00
Move "kind" attribute handling out of lexer
Doesn't belong there and will cause issue with multiple assignments.
This commit is contained in:
@@ -27,7 +27,7 @@ if (empty($files)) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$lexer = new PhpParser\Lexer\Emulative(array('usedAttributes' => array(
|
$lexer = new PhpParser\Lexer\Emulative(array('usedAttributes' => array(
|
||||||
'startLine', 'endLine', 'startFilePos', 'endFilePos', 'comments', 'kind'
|
'startLine', 'endLine', 'startFilePos', 'endFilePos', 'comments'
|
||||||
)));
|
)));
|
||||||
$parser = (new PhpParser\ParserFactory)->create(PhpParser\ParserFactory::PREFER_PHP7, $lexer);
|
$parser = (new PhpParser\ParserFactory)->create(PhpParser\ParserFactory::PREFER_PHP7, $lexer);
|
||||||
$dumper = new PhpParser\NodeDumper(['dumpComments' => true]);
|
$dumper = new PhpParser\NodeDumper(['dumpComments' => true]);
|
||||||
|
@@ -585,7 +585,8 @@ expr:
|
|||||||
| T_OBJECT_CAST expr { $$ = Expr\Cast\Object_ [$2]; }
|
| T_OBJECT_CAST expr { $$ = Expr\Cast\Object_ [$2]; }
|
||||||
| T_BOOL_CAST expr { $$ = Expr\Cast\Bool_ [$2]; }
|
| T_BOOL_CAST expr { $$ = Expr\Cast\Bool_ [$2]; }
|
||||||
| T_UNSET_CAST expr { $$ = Expr\Cast\Unset_ [$2]; }
|
| T_UNSET_CAST expr { $$ = Expr\Cast\Unset_ [$2]; }
|
||||||
| T_EXIT exit_expr { $$ = Expr\Exit_ [$2]; }
|
| T_EXIT exit_expr
|
||||||
|
{ $attrs = attributes(); $attrs['kind'] = strtolower($1); $$ = new Expr\Exit_($2, $attrs); }
|
||||||
| '@' expr { $$ = Expr\ErrorSuppress[$2]; }
|
| '@' expr { $$ = Expr\ErrorSuppress[$2]; }
|
||||||
| scalar { $$ = $1; }
|
| scalar { $$ = $1; }
|
||||||
| array_expr { $$ = $1; }
|
| array_expr { $$ = $1; }
|
||||||
@@ -735,7 +736,7 @@ ctor_arguments:
|
|||||||
;
|
;
|
||||||
|
|
||||||
common_scalar:
|
common_scalar:
|
||||||
T_LNUMBER { $$ = Scalar\LNumber[Scalar\LNumber::parse($1)]; }
|
T_LNUMBER { $$ = Scalar\LNumber::fromString($1, attributes()); }
|
||||||
| T_DNUMBER { $$ = Scalar\DNumber[Scalar\DNumber::parse($1)]; }
|
| T_DNUMBER { $$ = Scalar\DNumber[Scalar\DNumber::parse($1)]; }
|
||||||
| T_CONSTANT_ENCAPSED_STRING { $$ = Scalar\String_[Scalar\String_::parse($1, false)]; }
|
| T_CONSTANT_ENCAPSED_STRING { $$ = Scalar\String_[Scalar\String_::parse($1, false)]; }
|
||||||
| T_LINE { $$ = Scalar\MagicConst\Line[]; }
|
| T_LINE { $$ = Scalar\MagicConst\Line[]; }
|
||||||
|
@@ -576,7 +576,8 @@ expr:
|
|||||||
| T_OBJECT_CAST expr { $$ = Expr\Cast\Object_ [$2]; }
|
| T_OBJECT_CAST expr { $$ = Expr\Cast\Object_ [$2]; }
|
||||||
| T_BOOL_CAST expr { $$ = Expr\Cast\Bool_ [$2]; }
|
| T_BOOL_CAST expr { $$ = Expr\Cast\Bool_ [$2]; }
|
||||||
| T_UNSET_CAST expr { $$ = Expr\Cast\Unset_ [$2]; }
|
| T_UNSET_CAST expr { $$ = Expr\Cast\Unset_ [$2]; }
|
||||||
| T_EXIT exit_expr { $$ = Expr\Exit_ [$2]; }
|
| T_EXIT exit_expr
|
||||||
|
{ $attrs = attributes(); $attrs['kind'] = strtolower($1); $$ = new Expr\Exit_($2, $attrs); }
|
||||||
| '@' expr { $$ = Expr\ErrorSuppress[$2]; }
|
| '@' expr { $$ = Expr\ErrorSuppress[$2]; }
|
||||||
| scalar { $$ = $1; }
|
| scalar { $$ = $1; }
|
||||||
| '`' backticks_expr '`' { $$ = Expr\ShellExec[$2]; }
|
| '`' backticks_expr '`' { $$ = Expr\ShellExec[$2]; }
|
||||||
@@ -679,7 +680,7 @@ dereferencable_scalar:
|
|||||||
;
|
;
|
||||||
|
|
||||||
scalar:
|
scalar:
|
||||||
T_LNUMBER { $$ = Scalar\LNumber[Scalar\LNumber::parse($1)]; }
|
T_LNUMBER { $$ = Scalar\LNumber::fromString($1, attributes()); }
|
||||||
| T_DNUMBER { $$ = Scalar\DNumber[Scalar\DNumber::parse($1)]; }
|
| T_DNUMBER { $$ = Scalar\DNumber[Scalar\DNumber::parse($1)]; }
|
||||||
| T_LINE { $$ = Scalar\MagicConst\Line[]; }
|
| T_LINE { $$ = Scalar\MagicConst\Line[]; }
|
||||||
| T_FILE { $$ = Scalar\MagicConst\File[]; }
|
| T_FILE { $$ = Scalar\MagicConst\File[]; }
|
||||||
|
@@ -23,9 +23,9 @@ class Lexer
|
|||||||
*
|
*
|
||||||
* @param array $options Options array. Currently only the 'usedAttributes' option is supported,
|
* @param array $options Options array. Currently only the 'usedAttributes' option is supported,
|
||||||
* which is an array of attributes to add to the AST nodes. Possible
|
* which is an array of attributes to add to the AST nodes. Possible
|
||||||
* attributes are: 'comments', 'kind', 'startLine', 'endLine',
|
* attributes are: 'comments', 'startLine', 'endLine', 'startTokenPos',
|
||||||
* 'startTokenPos', 'endTokenPos', 'startFilePos', 'endFilePos'. The
|
* 'endTokenPos', 'startFilePos', 'endFilePos'. The option defaults to the
|
||||||
* option defaults to the first four. For more info see getNextToken() docs.
|
* first three. For more info see getNextToken() docs.
|
||||||
*/
|
*/
|
||||||
public function __construct(array $options = array()) {
|
public function __construct(array $options = array()) {
|
||||||
// map from internal tokens to PhpParser tokens
|
// map from internal tokens to PhpParser tokens
|
||||||
@@ -38,7 +38,7 @@ class Lexer
|
|||||||
// the usedAttributes member is a map of the used attribute names to a dummy
|
// the usedAttributes member is a map of the used attribute names to a dummy
|
||||||
// value (here "true")
|
// value (here "true")
|
||||||
$options += array(
|
$options += array(
|
||||||
'usedAttributes' => array('comments', 'kind', 'startLine', 'endLine'),
|
'usedAttributes' => array('comments', 'startLine', 'endLine'),
|
||||||
);
|
);
|
||||||
$this->usedAttributes = array_fill_keys($options['usedAttributes'], true);
|
$this->usedAttributes = array_fill_keys($options['usedAttributes'], true);
|
||||||
}
|
}
|
||||||
@@ -116,8 +116,6 @@ class Lexer
|
|||||||
* * 'comments' => Array of PhpParser\Comment or PhpParser\Comment\Doc instances,
|
* * 'comments' => Array of PhpParser\Comment or PhpParser\Comment\Doc instances,
|
||||||
* representing all comments that occurred between the previous
|
* representing all comments that occurred between the previous
|
||||||
* non-discarded token and the current one.
|
* non-discarded token and the current one.
|
||||||
* * 'kind' => For some tokens, this will provide additional information about the
|
|
||||||
* formatting of the node (e.g. whether a numeric literal is binary/...)
|
|
||||||
* * 'startLine' => Line in which the node starts.
|
* * 'startLine' => Line in which the node starts.
|
||||||
* * 'endLine' => Line in which the node ends.
|
* * 'endLine' => Line in which the node ends.
|
||||||
* * 'startTokenPos' => Offset into the token array of the first token in the node.
|
* * 'startTokenPos' => Offset into the token array of the first token in the node.
|
||||||
@@ -204,22 +202,6 @@ class Lexer
|
|||||||
$endAttributes['endFilePos'] = $this->filePos - 1;
|
$endAttributes['endFilePos'] = $this->filePos - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($this->usedAttributes['kind'])) {
|
|
||||||
if ($token[0] === T_EXIT) {
|
|
||||||
$startAttributes['kind'] = strtolower($value);
|
|
||||||
} elseif ($token[0] === T_LNUMBER) {
|
|
||||||
if ($value === '0' || $value[0] !== '0') {
|
|
||||||
$startAttributes['kind'] = LNumber::KIND_DEC;
|
|
||||||
} elseif ($value[1] === 'x' || $value[1] === 'X') {
|
|
||||||
$startAttributes['kind'] = LNumber::KIND_HEX;
|
|
||||||
} elseif ($value[1] === 'b' || $value[1] === 'B') {
|
|
||||||
$startAttributes['kind'] = LNumber::KIND_BIN;
|
|
||||||
} else {
|
|
||||||
$startAttributes['kind'] = LNumber::KIND_OCT;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this->tokenMap[$token[0]];
|
return $this->tokenMap[$token[0]];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -30,6 +30,27 @@ class LNumber extends Scalar
|
|||||||
return array('value');
|
return array('value');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs an LNumber node from a string number literal.
|
||||||
|
*
|
||||||
|
* @param string $str String number literal (decimal, octal, hex or binary)
|
||||||
|
* @param array $attributes Additional attributes
|
||||||
|
*
|
||||||
|
* @return LNumber The constructed LNumber, including kind attribute
|
||||||
|
*/
|
||||||
|
public static function fromString($str, array $attributes = array()) {
|
||||||
|
if ($str === '0' || $str[0] !== '0') {
|
||||||
|
$attributes['kind'] = LNumber::KIND_DEC;
|
||||||
|
} elseif ($str[1] === 'x' || $str[1] === 'X') {
|
||||||
|
$attributes['kind'] = LNumber::KIND_HEX;
|
||||||
|
} elseif ($str[1] === 'b' || $str[1] === 'B') {
|
||||||
|
$attributes['kind'] = LNumber::KIND_BIN;
|
||||||
|
} else {
|
||||||
|
$attributes['kind'] = LNumber::KIND_OCT;
|
||||||
|
}
|
||||||
|
return new self(self::parse($str), $attributes);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @internal
|
* @internal
|
||||||
*
|
*
|
||||||
|
@@ -2352,7 +2352,7 @@ class Php5 extends \PhpParser\ParserAbstract
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected function reduceRule361() {
|
protected function reduceRule361() {
|
||||||
$this->semValue = new Expr\Exit_($this->semStack[$this->stackPos-(2-2)], $this->startAttributeStack[$this->stackPos-(2-1)] + $this->endAttributes);
|
$attrs = $this->startAttributeStack[$this->stackPos-(2-1)] + $this->endAttributes; $attrs['kind'] = strtolower($this->semStack[$this->stackPos-(2-1)]); $this->semValue = new Expr\Exit_($this->semStack[$this->stackPos-(2-2)], $attrs);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function reduceRule362() {
|
protected function reduceRule362() {
|
||||||
@@ -2600,7 +2600,7 @@ class Php5 extends \PhpParser\ParserAbstract
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected function reduceRule419() {
|
protected function reduceRule419() {
|
||||||
$this->semValue = new Scalar\LNumber(Scalar\LNumber::parse($this->semStack[$this->stackPos-(1-1)]), $this->startAttributeStack[$this->stackPos-(1-1)] + $this->endAttributes);
|
$this->semValue = Scalar\LNumber::fromString($this->semStack[$this->stackPos-(1-1)], $this->startAttributeStack[$this->stackPos-(1-1)] + $this->endAttributes);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function reduceRule420() {
|
protected function reduceRule420() {
|
||||||
|
@@ -2202,7 +2202,7 @@ class Php7 extends \PhpParser\ParserAbstract
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected function reduceRule353() {
|
protected function reduceRule353() {
|
||||||
$this->semValue = new Expr\Exit_($this->semStack[$this->stackPos-(2-2)], $this->startAttributeStack[$this->stackPos-(2-1)] + $this->endAttributes);
|
$attrs = $this->startAttributeStack[$this->stackPos-(2-1)] + $this->endAttributes; $attrs['kind'] = strtolower($this->semStack[$this->stackPos-(2-1)]); $this->semValue = new Expr\Exit_($this->semStack[$this->stackPos-(2-2)], $attrs);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function reduceRule354() {
|
protected function reduceRule354() {
|
||||||
@@ -2376,7 +2376,7 @@ class Php7 extends \PhpParser\ParserAbstract
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected function reduceRule396() {
|
protected function reduceRule396() {
|
||||||
$this->semValue = new Scalar\LNumber(Scalar\LNumber::parse($this->semStack[$this->stackPos-(1-1)]), $this->startAttributeStack[$this->stackPos-(1-1)] + $this->endAttributes);
|
$this->semValue = Scalar\LNumber::fromString($this->semStack[$this->stackPos-(1-1)], $this->startAttributeStack[$this->stackPos-(1-1)] + $this->endAttributes);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function reduceRule397() {
|
protected function reduceRule397() {
|
||||||
|
@@ -61,7 +61,7 @@ class MultipleTest extends ParserTest {
|
|||||||
$this->getPrefer5(),
|
$this->getPrefer5(),
|
||||||
[
|
[
|
||||||
new Expr\Variable(
|
new Expr\Variable(
|
||||||
new Expr\ArrayDimFetch(new Expr\Variable('a'), new LNumber(0))
|
new Expr\ArrayDimFetch(new Expr\Variable('a'), LNumber::fromString('0'))
|
||||||
)
|
)
|
||||||
]
|
]
|
||||||
],
|
],
|
||||||
@@ -71,7 +71,7 @@ class MultipleTest extends ParserTest {
|
|||||||
$this->getPrefer7(),
|
$this->getPrefer7(),
|
||||||
[
|
[
|
||||||
new Expr\ArrayDimFetch(
|
new Expr\ArrayDimFetch(
|
||||||
new Expr\Variable(new Expr\Variable('a')), new LNumber(0)
|
new Expr\Variable(new Expr\Variable('a')), LNumber::fromString('0')
|
||||||
)
|
)
|
||||||
]
|
]
|
||||||
],
|
],
|
||||||
|
@@ -68,12 +68,12 @@ CODE;
|
|||||||
<attribute:startLine>
|
<attribute:startLine>
|
||||||
<scalar:int>4</scalar:int>
|
<scalar:int>4</scalar:int>
|
||||||
</attribute:startLine>
|
</attribute:startLine>
|
||||||
<attribute:kind>
|
|
||||||
<scalar:int>10</scalar:int>
|
|
||||||
</attribute:kind>
|
|
||||||
<attribute:endLine>
|
<attribute:endLine>
|
||||||
<scalar:int>4</scalar:int>
|
<scalar:int>4</scalar:int>
|
||||||
</attribute:endLine>
|
</attribute:endLine>
|
||||||
|
<attribute:kind>
|
||||||
|
<scalar:int>10</scalar:int>
|
||||||
|
</attribute:kind>
|
||||||
<subNode:value>
|
<subNode:value>
|
||||||
<scalar:int>0</scalar:int>
|
<scalar:int>0</scalar:int>
|
||||||
</subNode:value>
|
</subNode:value>
|
||||||
|
Reference in New Issue
Block a user