1
0
mirror of https://github.com/danog/PHP-Parser.git synced 2024-12-02 09:17:58 +01:00

Guarantee that subnodes are always in the right order

This commit is contained in:
nikic 2014-03-22 14:49:56 +01:00
parent c8c233f900
commit f5be0d30f7
25 changed files with 164 additions and 162 deletions

View File

@ -18,21 +18,21 @@ class Closure extends Expr
* Constructs a lambda function node. * Constructs a lambda function node.
* *
* @param array $subNodes Array of the following optional subnodes: * @param array $subNodes Array of the following optional subnodes:
* 'stmts' => array(): Statements * 'static' => false : Whether the closure is static
* 'byRef' => false : Whether to return by reference
* 'params' => array(): Parameters * 'params' => array(): Parameters
* 'uses' => array(): use()s * 'uses' => array(): use()s
* 'byRef' => false : Whether to return by reference * 'stmts' => array(): Statements
* 'static' => false : Whether the closure is static
* @param array $attributes Additional attributes * @param array $attributes Additional attributes
*/ */
public function __construct(array $subNodes = array(), array $attributes = array()) { public function __construct(array $subNodes = array(), array $attributes = array()) {
parent::__construct( parent::__construct(
$subNodes + array( array(
'stmts' => array(), 'static' => isset($subNodes['static']) ? $subNodes['static'] : false,
'params' => array(), 'byRef' => isset($subNodes['byRef']) ? $subNodes['byRef'] : false,
'uses' => array(), 'params' => isset($subNodes['params']) ? $subNodes['params'] : array(),
'byRef' => false, 'uses' => isset($subNodes['uses']) ? $subNodes['uses'] : array(),
'static' => false, 'stmts' => isset($subNodes['stmts']) ? $subNodes['stmts'] : array(),
), ),
$attributes $attributes
); );

View File

@ -40,15 +40,15 @@ class Class_ extends Node\Stmt
*/ */
public function __construct($name, array $subNodes = array(), array $attributes = array()) { public function __construct($name, array $subNodes = array(), array $attributes = array()) {
parent::__construct( parent::__construct(
$subNodes + array( array(
'type' => 0, 'type' => isset($subNodes['type']) ? $subNodes['type'] : 0,
'extends' => null, 'name' => $name,
'implements' => array(), 'extends' => isset($subNodes['extends']) ? $subNodes['extends'] : null,
'stmts' => array(), 'implements' => isset($subNodes['implements']) ? $subNodes['implements'] : array(),
'stmts' => isset($subNodes['stmts']) ? $subNodes['stmts'] : array(),
), ),
$attributes $attributes
); );
$this->name = $name;
if (isset(self::$specialNames[(string) $this->name])) { if (isset(self::$specialNames[(string) $this->name])) {
throw new Error(sprintf('Cannot use \'%s\' as class name as it is reserved', $this->name)); throw new Error(sprintf('Cannot use \'%s\' as class name as it is reserved', $this->name));

View File

@ -28,15 +28,15 @@ class ClassMethod extends Node\Stmt
*/ */
public function __construct($name, array $subNodes = array(), array $attributes = array()) { public function __construct($name, array $subNodes = array(), array $attributes = array()) {
parent::__construct( parent::__construct(
$subNodes + array( array(
'type' => Class_::MODIFIER_PUBLIC, 'type' => isset($subNodes['type']) ? $subNodes['type'] : Class_::MODIFIER_PUBLIC,
'byRef' => false, 'byRef' => isset($subNodes['byRef']) ? $subNodes['byRef'] : false,
'params' => array(), 'name' => $name,
'stmts' => array(), 'params' => isset($subNodes['params']) ? $subNodes['params'] : array(),
'stmts' => array_key_exists('stmts', $subNodes) ? $subNodes['stmts'] : array(),
), ),
$attributes $attributes
); );
$this->name = $name;
if ($this->type & Class_::MODIFIER_STATIC) { if ($this->type & Class_::MODIFIER_STATIC) {
switch (strtolower($this->name)) { switch (strtolower($this->name)) {

View File

@ -24,11 +24,11 @@ class For_ extends Node\Stmt
*/ */
public function __construct(array $subNodes = array(), array $attributes = array()) { public function __construct(array $subNodes = array(), array $attributes = array()) {
parent::__construct( parent::__construct(
$subNodes + array( array(
'init' => array(), 'init' => isset($subNodes['init']) ? $subNodes['init'] : array(),
'cond' => array(), 'cond' => isset($subNodes['cond']) ? $subNodes['cond'] : array(),
'loop' => array(), 'loop' => isset($subNodes['loop']) ? $subNodes['loop'] : array(),
'stmts' => array(), 'stmts' => isset($subNodes['stmts']) ? $subNodes['stmts'] : array(),
), ),
$attributes $attributes
); );

View File

@ -26,14 +26,14 @@ class Foreach_ extends Node\Stmt
*/ */
public function __construct(Node\Expr $expr, Node\Expr $valueVar, array $subNodes = array(), array $attributes = array()) { public function __construct(Node\Expr $expr, Node\Expr $valueVar, array $subNodes = array(), array $attributes = array()) {
parent::__construct( parent::__construct(
$subNodes + array( array(
'keyVar' => null, 'expr' => $expr,
'byRef' => false, 'keyVar' => isset($subNodes['keyVar']) ? $subNodes['keyVar'] : null,
'stmts' => array(), 'byRef' => isset($subNodes['byRef']) ? $subNodes['byRef'] : false,
'valueVar' => $valueVar,
'stmts' => isset($subNodes['stmts']) ? $subNodes['stmts'] : array(),
), ),
$attributes $attributes
); );
$this->expr = $expr;
$this->valueVar = $valueVar;
} }
} }

View File

@ -24,10 +24,11 @@ class Function_ extends Node\Stmt
*/ */
public function __construct($name, array $subNodes = array(), array $attributes = array()) { public function __construct($name, array $subNodes = array(), array $attributes = array()) {
parent::__construct( parent::__construct(
$subNodes + array( array(
'byRef' => false, 'byRef' => isset($subNodes['byRef']) ? $subNodes['byRef'] : false,
'params' => array(), 'name' => $name,
'stmts' => array(), 'params' => isset($subNodes['params']) ? $subNodes['params'] : array(),
'stmts' => isset($subNodes['stmts']) ? $subNodes['stmts'] : array(),
), ),
$attributes $attributes
); );

View File

@ -25,10 +25,11 @@ class If_ extends Node\Stmt
*/ */
public function __construct(Node\Expr $cond, array $subNodes = array(), array $attributes = array()) { public function __construct(Node\Expr $cond, array $subNodes = array(), array $attributes = array()) {
parent::__construct( parent::__construct(
$subNodes + array( array(
'stmts' => array(), 'cond' => $cond,
'elseifs' => array(), 'stmts' => isset($subNodes['stmts']) ? $subNodes['stmts'] : array(),
'else' => null, 'elseifs' => isset($subNodes['elseifs']) ? $subNodes['elseifs'] : array(),
'else' => isset($subNodes['else']) ? $subNodes['else'] : null,
), ),
$attributes $attributes
); );

View File

@ -29,13 +29,13 @@ class Interface_ extends Node\Stmt
*/ */
public function __construct($name, array $subNodes = array(), array $attributes = array()) { public function __construct($name, array $subNodes = array(), array $attributes = array()) {
parent::__construct( parent::__construct(
$subNodes + array( array(
'extends' => array(), 'name' => $name,
'stmts' => array(), 'extends' => isset($subNodes['extends']) ? $subNodes['extends'] : array(),
'stmts' => isset($subNodes['stmts']) ? $subNodes['stmts'] : array(),
), ),
$attributes $attributes
); );
$this->name = $name;
if (isset(self::$specialNames[(string) $this->name])) { if (isset(self::$specialNames[(string) $this->name])) {
throw new Error(sprintf('Cannot use \'%s\' as class name as it is reserved', $this->name)); throw new Error(sprintf('Cannot use \'%s\' as class name as it is reserved', $this->name));

View File

@ -39,6 +39,9 @@ CODE;
<subNode:byRef> <subNode:byRef>
<scalar:false/> <scalar:false/>
</subNode:byRef> </subNode:byRef>
<subNode:name>
<scalar:string>functionName</scalar:string>
</subNode:name>
<subNode:params> <subNode:params>
<scalar:array> <scalar:array>
<node:Param> <node:Param>
@ -130,9 +133,6 @@ CODE;
</node:Stmt_Echo> </node:Stmt_Echo>
</scalar:array> </scalar:array>
</subNode:stmts> </subNode:stmts>
<subNode:name>
<scalar:string>functionName</scalar:string>
</subNode:name>
</node:Stmt_Function> </node:Stmt_Function>
</scalar:array> </scalar:array>
</AST> </AST>

View File

@ -18,6 +18,9 @@ declare (a='b') $C;
----- -----
array( array(
0: Stmt_If( 0: Stmt_If(
cond: Expr_Variable(
name: a
)
stmts: array( stmts: array(
0: Expr_Variable( 0: Expr_Variable(
name: A name: A
@ -42,9 +45,6 @@ array(
) )
) )
) )
cond: Expr_Variable(
name: a
)
) )
1: Stmt_For( 1: Stmt_For(
init: array( init: array(
@ -60,19 +60,19 @@ array(
) )
) )
2: Stmt_Foreach( 2: Stmt_Foreach(
expr: Expr_Variable(
name: a
)
keyVar: null keyVar: null
byRef: false byRef: false
valueVar: Expr_Variable(
name: b
)
stmts: array( stmts: array(
0: Expr_Variable( 0: Expr_Variable(
name: AB name: AB
) )
) )
expr: Expr_Variable(
name: a
)
valueVar: Expr_Variable(
name: b
)
) )
3: Stmt_While( 3: Stmt_While(
cond: Expr_Variable( cond: Expr_Variable(

View File

@ -10,6 +10,7 @@ abstract class A {
array( array(
0: Stmt_Class( 0: Stmt_Class(
type: 16 type: 16
name: A
extends: null extends: null
implements: array( implements: array(
) )
@ -17,21 +18,20 @@ array(
0: Stmt_ClassMethod( 0: Stmt_ClassMethod(
type: 1 type: 1
byRef: false byRef: false
name: a
params: array( params: array(
) )
stmts: array( stmts: array(
) )
name: a
) )
1: Stmt_ClassMethod( 1: Stmt_ClassMethod(
type: 17 type: 17
byRef: false byRef: false
name: b
params: array( params: array(
) )
stmts: null stmts: null
name: b
) )
) )
name: A
) )
) )

View File

@ -8,20 +8,6 @@ if (true) {
----- -----
array( array(
0: Stmt_If( 0: Stmt_If(
stmts: array(
0: Stmt_Class(
type: 0
extends: null
implements: array(
)
stmts: array(
)
name: A
)
)
elseifs: array(
)
else: null
cond: Expr_ConstFetch( cond: Expr_ConstFetch(
name: Name( name: Name(
parts: array( parts: array(
@ -29,5 +15,19 @@ array(
) )
) )
) )
stmts: array(
0: Stmt_Class(
type: 0
name: A
extends: null
implements: array(
)
stmts: array(
)
)
)
elseifs: array(
)
else: null
) )
) )

View File

@ -7,11 +7,11 @@ final class A {}
array( array(
0: Stmt_Class( 0: Stmt_Class(
type: 32 type: 32
name: A
extends: null extends: null
implements: array( implements: array(
) )
stmts: array( stmts: array(
) )
name: A
) )
) )

View File

@ -8,6 +8,7 @@ interface A extends C, D {
----- -----
array( array(
0: Stmt_Interface( 0: Stmt_Interface(
name: A
extends: array( extends: array(
0: Name( 0: Name(
parts: array( parts: array(
@ -24,12 +25,11 @@ array(
0: Stmt_ClassMethod( 0: Stmt_ClassMethod(
type: 1 type: 1
byRef: false byRef: false
name: a
params: array( params: array(
) )
stmts: null stmts: null
name: a
) )
) )
name: A
) )
) )

View File

@ -10,6 +10,7 @@ class A {
array( array(
0: Stmt_Class( 0: Stmt_Class(
type: 0 type: 0
name: A
extends: null extends: null
implements: array( implements: array(
) )
@ -26,13 +27,12 @@ array(
1: Stmt_ClassMethod( 1: Stmt_ClassMethod(
type: 1 type: 1
byRef: false byRef: false
name: bar
params: array( params: array(
) )
stmts: array( stmts: array(
) )
name: bar
) )
) )
name: A
) )
) )

View File

@ -19,6 +19,7 @@ class A extends B implements C, D {
array( array(
0: Stmt_Class( 0: Stmt_Class(
type: 0 type: 0
name: A
extends: Name( extends: Name(
parts: array( parts: array(
0: B 0: B
@ -91,49 +92,48 @@ array(
4: Stmt_ClassMethod( 4: Stmt_ClassMethod(
type: 1 type: 1
byRef: false byRef: false
name: a
params: array( params: array(
) )
stmts: array( stmts: array(
) )
name: a
) )
5: Stmt_ClassMethod( 5: Stmt_ClassMethod(
type: 9 type: 9
byRef: false byRef: false
name: b
params: array( params: array(
) )
stmts: array( stmts: array(
) )
name: b
) )
6: Stmt_ClassMethod( 6: Stmt_ClassMethod(
type: 33 type: 33
byRef: false byRef: false
name: c
params: array( params: array(
) )
stmts: array( stmts: array(
) )
name: c
) )
7: Stmt_ClassMethod( 7: Stmt_ClassMethod(
type: 2 type: 2
byRef: false byRef: false
name: d
params: array( params: array(
) )
stmts: array( stmts: array(
) )
name: d
) )
8: Stmt_ClassMethod( 8: Stmt_ClassMethod(
type: 4 type: 4
byRef: false byRef: false
name: e
params: array( params: array(
) )
stmts: array( stmts: array(
) )
name: e
) )
) )
name: A
) )
) )

View File

@ -28,16 +28,17 @@ array(
0: Stmt_ClassMethod( 0: Stmt_ClassMethod(
type: 1 type: 1
byRef: false byRef: false
name: a
params: array( params: array(
) )
stmts: array( stmts: array(
) )
name: a
) )
) )
) )
1: Stmt_Class( 1: Stmt_Class(
type: 0 type: 0
name: B
extends: null extends: null
implements: array( implements: array(
) )
@ -154,6 +155,5 @@ array(
) )
) )
) )
name: B
) )
) )

View File

@ -8,6 +8,7 @@ function &a($b) {}
array( array(
0: Stmt_Function( 0: Stmt_Function(
byRef: false byRef: false
name: a
params: array( params: array(
0: Param( 0: Param(
name: b name: b
@ -18,10 +19,10 @@ array(
) )
stmts: array( stmts: array(
) )
name: a
) )
1: Stmt_Function( 1: Stmt_Function(
byRef: true byRef: true
name: a
params: array( params: array(
0: Param( 0: Param(
name: b name: b
@ -32,6 +33,5 @@ array(
) )
stmts: array( stmts: array(
) )
name: a
) )
) )

View File

@ -8,19 +8,6 @@ if (true) {
----- -----
array( array(
0: Stmt_If( 0: Stmt_If(
stmts: array(
0: Stmt_Function(
byRef: false
params: array(
)
stmts: array(
)
name: A
)
)
elseifs: array(
)
else: null
cond: Expr_ConstFetch( cond: Expr_ConstFetch(
name: Name( name: Name(
parts: array( parts: array(
@ -28,5 +15,18 @@ array(
) )
) )
) )
stmts: array(
0: Stmt_Function(
byRef: false
name: A
params: array(
)
stmts: array(
)
)
)
elseifs: array(
)
else: null
) )
) )

View File

@ -17,6 +17,7 @@ function a(
array( array(
0: Stmt_Function( 0: Stmt_Function(
byRef: false byRef: false
name: a
params: array( params: array(
0: Param( 0: Param(
name: b name: b
@ -133,6 +134,5 @@ array(
) )
stmts: array( stmts: array(
) )
name: a
) )
) )

View File

@ -30,6 +30,7 @@ function gen() {
array( array(
0: Stmt_Function( 0: Stmt_Function(
byRef: false byRef: false
name: gen
params: array( params: array(
) )
stmts: array( stmts: array(
@ -85,6 +86,12 @@ array(
) )
) )
6: Stmt_If( 6: Stmt_If(
cond: Expr_Yield(
key: null
value: Expr_Variable(
name: foo
)
)
stmts: array( stmts: array(
) )
elseifs: array( elseifs: array(
@ -100,14 +107,14 @@ array(
) )
) )
else: null else: null
cond: Expr_Yield(
key: null
value: Expr_Variable(
name: foo
)
)
) )
7: Stmt_If( 7: Stmt_If(
cond: Expr_Yield(
key: null
value: Expr_Variable(
name: foo
)
)
stmts: array( stmts: array(
) )
elseifs: array( elseifs: array(
@ -123,12 +130,6 @@ array(
) )
) )
else: null else: null
cond: Expr_Yield(
key: null
value: Expr_Variable(
name: foo
)
)
) )
8: Stmt_While( 8: Stmt_While(
cond: Expr_Yield( cond: Expr_Yield(
@ -222,6 +223,5 @@ array(
) )
) )
) )
name: gen
) )
) )

View File

@ -10,6 +10,7 @@ function a() {
array( array(
0: Stmt_Function( 0: Stmt_Function(
byRef: false byRef: false
name: a
params: array( params: array(
) )
stmts: array( stmts: array(
@ -45,6 +46,5 @@ array(
) )
) )
) )
name: a
) )
) )

View File

@ -7,6 +7,7 @@ function a($b, array $c, callable $d, E $f) {}
array( array(
0: Stmt_Function( 0: Stmt_Function(
byRef: false byRef: false
name: a
params: array( params: array(
0: Param( 0: Param(
name: b name: b
@ -39,6 +40,5 @@ array(
) )
stmts: array( stmts: array(
) )
name: a
) )
) )

View File

@ -19,6 +19,9 @@ if ($a): endif; // without else
----- -----
array( array(
0: Stmt_If( 0: Stmt_If(
cond: Expr_Variable(
name: a
)
stmts: array( stmts: array(
) )
elseifs: array( elseifs: array(
@ -41,21 +44,21 @@ array(
stmts: array( stmts: array(
) )
) )
cond: Expr_Variable(
name: a
)
) )
1: Stmt_If( 1: Stmt_If(
cond: Expr_Variable(
name: a
)
stmts: array( stmts: array(
) )
elseifs: array( elseifs: array(
) )
else: null else: null
)
2: Stmt_If(
cond: Expr_Variable( cond: Expr_Variable(
name: a name: a
) )
)
2: Stmt_If(
stmts: array( stmts: array(
) )
elseifs: array( elseifs: array(
@ -78,18 +81,15 @@ array(
stmts: array( stmts: array(
) )
) )
)
3: Stmt_If(
cond: Expr_Variable( cond: Expr_Variable(
name: a name: a
) )
)
3: Stmt_If(
stmts: array( stmts: array(
) )
elseifs: array( elseifs: array(
) )
else: null else: null
cond: Expr_Variable(
name: a
)
) )
) )

View File

@ -19,65 +19,63 @@ endforeach;
----- -----
array( array(
0: Stmt_Foreach( 0: Stmt_Foreach(
keyVar: null
byRef: false
stmts: array(
)
expr: Expr_Variable( expr: Expr_Variable(
name: a name: a
) )
keyVar: null
byRef: false
valueVar: Expr_Variable( valueVar: Expr_Variable(
name: b name: b
) )
stmts: array(
)
) )
1: Stmt_Foreach( 1: Stmt_Foreach(
keyVar: null
byRef: true
stmts: array(
)
expr: Expr_Variable( expr: Expr_Variable(
name: a name: a
) )
keyVar: null
byRef: true
valueVar: Expr_Variable( valueVar: Expr_Variable(
name: b name: b
) )
stmts: array(
)
) )
2: Stmt_Foreach( 2: Stmt_Foreach(
expr: Expr_Variable(
name: a
)
keyVar: Expr_Variable( keyVar: Expr_Variable(
name: b name: b
) )
byRef: false byRef: false
stmts: array(
)
expr: Expr_Variable(
name: a
)
valueVar: Expr_Variable( valueVar: Expr_Variable(
name: c name: c
) )
stmts: array(
)
) )
3: Stmt_Foreach( 3: Stmt_Foreach(
expr: Expr_Variable(
name: a
)
keyVar: Expr_Variable( keyVar: Expr_Variable(
name: b name: b
) )
byRef: true byRef: true
stmts: array(
)
expr: Expr_Variable(
name: a
)
valueVar: Expr_Variable( valueVar: Expr_Variable(
name: c name: c
) )
)
4: Stmt_Foreach(
keyVar: null
byRef: false
stmts: array( stmts: array(
) )
)
4: Stmt_Foreach(
expr: Expr_Variable( expr: Expr_Variable(
name: a name: a
) )
keyVar: null
byRef: false
valueVar: Expr_List( valueVar: Expr_List(
vars: array( vars: array(
0: Expr_Variable( 0: Expr_Variable(
@ -88,17 +86,17 @@ array(
) )
) )
) )
stmts: array(
)
) )
5: Stmt_Foreach( 5: Stmt_Foreach(
expr: Expr_Variable(
name: a
)
keyVar: Expr_Variable( keyVar: Expr_Variable(
name: a name: a
) )
byRef: false byRef: false
stmts: array(
)
expr: Expr_Variable(
name: a
)
valueVar: Expr_List( valueVar: Expr_List(
vars: array( vars: array(
0: Expr_Variable( 0: Expr_Variable(
@ -110,30 +108,32 @@ array(
) )
) )
) )
)
6: Stmt_Foreach(
keyVar: null
byRef: false
stmts: array( stmts: array(
) )
)
6: Stmt_Foreach(
expr: Expr_Array( expr: Expr_Array(
items: array( items: array(
) )
) )
keyVar: null
byRef: false
valueVar: Expr_Variable( valueVar: Expr_Variable(
name: b name: b
) )
stmts: array(
)
) )
7: Stmt_Foreach( 7: Stmt_Foreach(
keyVar: null
byRef: false
stmts: array(
)
expr: Expr_Variable( expr: Expr_Variable(
name: a name: a
) )
keyVar: null
byRef: false
valueVar: Expr_Variable( valueVar: Expr_Variable(
name: b name: b
) )
stmts: array(
)
) )
) )