1
0
mirror of https://github.com/danog/PHP-Parser.git synced 2024-11-30 04:19:30 +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.
*
* @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
* 'uses' => array(): use()s
* 'byRef' => false : Whether to return by reference
* 'static' => false : Whether the closure is static
* 'stmts' => array(): Statements
* @param array $attributes Additional attributes
*/
public function __construct(array $subNodes = array(), array $attributes = array()) {
parent::__construct(
$subNodes + array(
'stmts' => array(),
'params' => array(),
'uses' => array(),
'byRef' => false,
'static' => false,
array(
'static' => isset($subNodes['static']) ? $subNodes['static'] : false,
'byRef' => isset($subNodes['byRef']) ? $subNodes['byRef'] : false,
'params' => isset($subNodes['params']) ? $subNodes['params'] : array(),
'uses' => isset($subNodes['uses']) ? $subNodes['uses'] : array(),
'stmts' => isset($subNodes['stmts']) ? $subNodes['stmts'] : array(),
),
$attributes
);

View File

@ -40,15 +40,15 @@ class Class_ extends Node\Stmt
*/
public function __construct($name, array $subNodes = array(), array $attributes = array()) {
parent::__construct(
$subNodes + array(
'type' => 0,
'extends' => null,
'implements' => array(),
'stmts' => array(),
array(
'type' => isset($subNodes['type']) ? $subNodes['type'] : 0,
'name' => $name,
'extends' => isset($subNodes['extends']) ? $subNodes['extends'] : null,
'implements' => isset($subNodes['implements']) ? $subNodes['implements'] : array(),
'stmts' => isset($subNodes['stmts']) ? $subNodes['stmts'] : array(),
),
$attributes
);
$this->name = $name;
if (isset(self::$specialNames[(string) $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()) {
parent::__construct(
$subNodes + array(
'type' => Class_::MODIFIER_PUBLIC,
'byRef' => false,
'params' => array(),
'stmts' => array(),
array(
'type' => isset($subNodes['type']) ? $subNodes['type'] : Class_::MODIFIER_PUBLIC,
'byRef' => isset($subNodes['byRef']) ? $subNodes['byRef'] : false,
'name' => $name,
'params' => isset($subNodes['params']) ? $subNodes['params'] : array(),
'stmts' => array_key_exists('stmts', $subNodes) ? $subNodes['stmts'] : array(),
),
$attributes
);
$this->name = $name;
if ($this->type & Class_::MODIFIER_STATIC) {
switch (strtolower($this->name)) {

View File

@ -24,11 +24,11 @@ class For_ extends Node\Stmt
*/
public function __construct(array $subNodes = array(), array $attributes = array()) {
parent::__construct(
$subNodes + array(
'init' => array(),
'cond' => array(),
'loop' => array(),
'stmts' => array(),
array(
'init' => isset($subNodes['init']) ? $subNodes['init'] : array(),
'cond' => isset($subNodes['cond']) ? $subNodes['cond'] : array(),
'loop' => isset($subNodes['loop']) ? $subNodes['loop'] : array(),
'stmts' => isset($subNodes['stmts']) ? $subNodes['stmts'] : array(),
),
$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()) {
parent::__construct(
$subNodes + array(
'keyVar' => null,
'byRef' => false,
'stmts' => array(),
array(
'expr' => $expr,
'keyVar' => isset($subNodes['keyVar']) ? $subNodes['keyVar'] : null,
'byRef' => isset($subNodes['byRef']) ? $subNodes['byRef'] : false,
'valueVar' => $valueVar,
'stmts' => isset($subNodes['stmts']) ? $subNodes['stmts'] : array(),
),
$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()) {
parent::__construct(
$subNodes + array(
'byRef' => false,
'params' => array(),
'stmts' => array(),
array(
'byRef' => isset($subNodes['byRef']) ? $subNodes['byRef'] : false,
'name' => $name,
'params' => isset($subNodes['params']) ? $subNodes['params'] : array(),
'stmts' => isset($subNodes['stmts']) ? $subNodes['stmts'] : array(),
),
$attributes
);

View File

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

View File

@ -29,13 +29,13 @@ class Interface_ extends Node\Stmt
*/
public function __construct($name, array $subNodes = array(), array $attributes = array()) {
parent::__construct(
$subNodes + array(
'extends' => array(),
'stmts' => array(),
array(
'name' => $name,
'extends' => isset($subNodes['extends']) ? $subNodes['extends'] : array(),
'stmts' => isset($subNodes['stmts']) ? $subNodes['stmts'] : array(),
),
$attributes
);
$this->name = $name;
if (isset(self::$specialNames[(string) $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>
<scalar:false/>
</subNode:byRef>
<subNode:name>
<scalar:string>functionName</scalar:string>
</subNode:name>
<subNode:params>
<scalar:array>
<node:Param>
@ -130,9 +133,6 @@ CODE;
</node:Stmt_Echo>
</scalar:array>
</subNode:stmts>
<subNode:name>
<scalar:string>functionName</scalar:string>
</subNode:name>
</node:Stmt_Function>
</scalar:array>
</AST>

View File

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

View File

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

View File

@ -8,20 +8,6 @@ if (true) {
-----
array(
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(
name: Name(
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(
0: Stmt_Class(
type: 32
name: A
extends: null
implements: array(
)
stmts: array(
)
name: A
)
)

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -8,19 +8,6 @@ if (true) {
-----
array(
0: Stmt_If(
stmts: array(
0: Stmt_Function(
byRef: false
params: array(
)
stmts: array(
)
name: A
)
)
elseifs: array(
)
else: null
cond: Expr_ConstFetch(
name: Name(
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(
0: Stmt_Function(
byRef: false
name: a
params: array(
0: Param(
name: b
@ -133,6 +134,5 @@ array(
)
stmts: array(
)
name: a
)
)

View File

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

View File

@ -10,6 +10,7 @@ function a() {
array(
0: Stmt_Function(
byRef: false
name: a
params: 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(
0: Stmt_Function(
byRef: false
name: a
params: array(
0: Param(
name: b
@ -39,6 +40,5 @@ array(
)
stmts: array(
)
name: a
)
)

View File

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

View File

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