mirror of
https://github.com/danog/PHP-Parser.git
synced 2024-11-27 04:14:44 +01:00
Add kind attribute for arrays
To distinguish array() and [] syntax. The pretty printer respects this attribute. The shortArraySyntax pretty printer option acts as a default in case the attribute is not specified.
This commit is contained in:
parent
ae30f97af6
commit
aa199120c7
@ -613,8 +613,12 @@ yield_expr:
|
|||||||
;
|
;
|
||||||
|
|
||||||
array_expr:
|
array_expr:
|
||||||
T_ARRAY '(' array_pair_list ')' { $$ = Expr\Array_[$3]; }
|
T_ARRAY '(' array_pair_list ')'
|
||||||
| '[' array_pair_list ']' { $$ = Expr\Array_[$2]; }
|
{ $attrs = attributes(); $attrs['kind'] = Expr\Array_::KIND_LONG;
|
||||||
|
$$ = new Expr\Array_($3, $attrs); }
|
||||||
|
| '[' array_pair_list ']'
|
||||||
|
{ $attrs = attributes(); $attrs['kind'] = Expr\Array_::KIND_SHORT;
|
||||||
|
$$ = new Expr\Array_($2, $attrs); }
|
||||||
;
|
;
|
||||||
|
|
||||||
scalar_dereference:
|
scalar_dereference:
|
||||||
|
@ -669,8 +669,12 @@ constant:
|
|||||||
;
|
;
|
||||||
|
|
||||||
dereferencable_scalar:
|
dereferencable_scalar:
|
||||||
T_ARRAY '(' array_pair_list ')' { $$ = Expr\Array_[$3]; }
|
T_ARRAY '(' array_pair_list ')'
|
||||||
| '[' array_pair_list ']' { $$ = Expr\Array_[$2]; }
|
{ $attrs = attributes(); $attrs['kind'] = Expr\Array_::KIND_LONG;
|
||||||
|
$$ = new Expr\Array_($3, $attrs); }
|
||||||
|
| '[' array_pair_list ']'
|
||||||
|
{ $attrs = attributes(); $attrs['kind'] = Expr\Array_::KIND_SHORT;
|
||||||
|
$$ = new Expr\Array_($2, $attrs); }
|
||||||
| T_CONSTANT_ENCAPSED_STRING { $$ = Scalar\String_[Scalar\String_::parse($1)]; }
|
| T_CONSTANT_ENCAPSED_STRING { $$ = Scalar\String_[Scalar\String_::parse($1)]; }
|
||||||
;
|
;
|
||||||
|
|
||||||
|
@ -6,6 +6,10 @@ use PhpParser\Node\Expr;
|
|||||||
|
|
||||||
class Array_ extends Expr
|
class Array_ extends Expr
|
||||||
{
|
{
|
||||||
|
// For use in "kind" attribute
|
||||||
|
const KIND_LONG = 1; // array() syntax
|
||||||
|
const KIND_SHORT = 2; // [] syntax
|
||||||
|
|
||||||
/** @var ArrayItem[] Items */
|
/** @var ArrayItem[] Items */
|
||||||
public $items;
|
public $items;
|
||||||
|
|
||||||
|
@ -2412,11 +2412,13 @@ class Php5 extends \PhpParser\ParserAbstract
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected function reduceRule376() {
|
protected function reduceRule376() {
|
||||||
$this->semValue = new Expr\Array_($this->semStack[$this->stackPos-(4-3)], $this->startAttributeStack[$this->stackPos-(4-1)] + $this->endAttributes);
|
$attrs = $this->startAttributeStack[$this->stackPos-(4-1)] + $this->endAttributes; $attrs['kind'] = Expr\Array_::KIND_LONG;
|
||||||
|
$this->semValue = new Expr\Array_($this->semStack[$this->stackPos-(4-3)], $attrs);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function reduceRule377() {
|
protected function reduceRule377() {
|
||||||
$this->semValue = new Expr\Array_($this->semStack[$this->stackPos-(3-2)], $this->startAttributeStack[$this->stackPos-(3-1)] + $this->endAttributes);
|
$attrs = $this->startAttributeStack[$this->stackPos-(3-1)] + $this->endAttributes; $attrs['kind'] = Expr\Array_::KIND_SHORT;
|
||||||
|
$this->semValue = new Expr\Array_($this->semStack[$this->stackPos-(3-2)], $attrs);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function reduceRule378() {
|
protected function reduceRule378() {
|
||||||
|
@ -2362,11 +2362,13 @@ class Php7 extends \PhpParser\ParserAbstract
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected function reduceRule393() {
|
protected function reduceRule393() {
|
||||||
$this->semValue = new Expr\Array_($this->semStack[$this->stackPos-(4-3)], $this->startAttributeStack[$this->stackPos-(4-1)] + $this->endAttributes);
|
$attrs = $this->startAttributeStack[$this->stackPos-(4-1)] + $this->endAttributes; $attrs['kind'] = Expr\Array_::KIND_LONG;
|
||||||
|
$this->semValue = new Expr\Array_($this->semStack[$this->stackPos-(4-3)], $attrs);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function reduceRule394() {
|
protected function reduceRule394() {
|
||||||
$this->semValue = new Expr\Array_($this->semStack[$this->stackPos-(3-2)], $this->startAttributeStack[$this->stackPos-(3-1)] + $this->endAttributes);
|
$attrs = $this->startAttributeStack[$this->stackPos-(3-1)] + $this->endAttributes; $attrs['kind'] = Expr\Array_::KIND_SHORT;
|
||||||
|
$this->semValue = new Expr\Array_($this->semStack[$this->stackPos-(3-2)], $attrs);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function reduceRule395() {
|
protected function reduceRule395() {
|
||||||
|
@ -432,7 +432,9 @@ class Standard extends PrettyPrinterAbstract
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function pExpr_Array(Expr\Array_ $node) {
|
public function pExpr_Array(Expr\Array_ $node) {
|
||||||
if ($this->options['shortArraySyntax']) {
|
$syntax = $node->getAttribute('kind',
|
||||||
|
$this->options['shortArraySyntax'] ? Expr\Array_::KIND_SHORT : Expr\Array_::KIND_LONG);
|
||||||
|
if ($syntax === Expr\Array_::KIND_SHORT) {
|
||||||
return '[' . $this->pCommaSeparated($node->items) . ']';
|
return '[' . $this->pCommaSeparated($node->items) . ']';
|
||||||
} else {
|
} else {
|
||||||
return 'array(' . $this->pCommaSeparated($node->items) . ')';
|
return 'array(' . $this->pCommaSeparated($node->items) . ')';
|
||||||
|
@ -82,7 +82,8 @@ abstract class PrettyPrinterAbstract
|
|||||||
* Creates a pretty printer instance using the given options.
|
* Creates a pretty printer instance using the given options.
|
||||||
*
|
*
|
||||||
* Supported options:
|
* Supported options:
|
||||||
* * bool $shortArraySyntax = false: Whether to use [] instead of array()
|
* * bool $shortArraySyntax = false: Whether to use [] instead of array() as the default array
|
||||||
|
* syntax, if the node does not specify a format.
|
||||||
*
|
*
|
||||||
* @param array $options Dictionary of formatting options
|
* @param array $options Dictionary of formatting options
|
||||||
*/
|
*/
|
||||||
|
@ -102,4 +102,13 @@ class PrettyPrinterTest extends CodeTestAbstract
|
|||||||
$options = isset($parts[1]) ? json_decode($parts[1], true) : [];
|
$options = isset($parts[1]) ? json_decode($parts[1], true) : [];
|
||||||
return [$version, $options];
|
return [$version, $options];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testArraySyntaxDefault() {
|
||||||
|
$prettyPrinter = new Standard(['shortArraySyntax' => true]);
|
||||||
|
$expr = new Expr\Array_([
|
||||||
|
new Expr\ArrayItem(new String_('val'), new String_('key'))
|
||||||
|
]);
|
||||||
|
$expected = "['key' => 'val']";
|
||||||
|
$this->assertSame($expected, $prettyPrinter->prettyPrintExpr($expr));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,6 @@ Short array syntax
|
|||||||
array(1, 2, 3);
|
array(1, 2, 3);
|
||||||
['a' => 'b', 'c' => 'd'];
|
['a' => 'b', 'c' => 'd'];
|
||||||
-----
|
-----
|
||||||
!!both {"shortArraySyntax": true}
|
|
||||||
[];
|
[];
|
||||||
[1, 2, 3];
|
array(1, 2, 3);
|
||||||
['a' => 'b', 'c' => 'd'];
|
['a' => 'b', 'c' => 'd'];
|
Loading…
Reference in New Issue
Block a user