2
2
*
3
3
* Package: php-parser
4
4
* Parse PHP code and returns its AST
5
- * Build: 3a77549cb15446749700 - 2018-9-3
5
+ * Build: 800b5c0abc57980b33d2 - 2018-9-30
6
6
* License: BSD-3-Clause
7
7
* Author: Ioan CHIRIAC
8
8
*
@@ -3099,6 +3099,23 @@ AST.prototype.prepare = function (kind, docs, parser) {
3099
3099
result . setKind = function ( newKind ) {
3100
3100
kind = newKind ;
3101
3101
} ;
3102
+ /**
3103
+ * Release a node without using it on the AST
3104
+ */
3105
+ result . destroy = function ( target ) {
3106
+ if ( docs ) {
3107
+ // release current docs stack
3108
+ if ( target ) {
3109
+ if ( ! target . leadingComments ) {
3110
+ target . leadingComments = docs ;
3111
+ } else {
3112
+ target . leadingComments = docs . concat ( target . leadingComments ) ;
3113
+ }
3114
+ } else {
3115
+ parser . _docIndex = parser . _docs . length - docs . length ;
3116
+ }
3117
+ }
3118
+ } ;
3102
3119
return result ;
3103
3120
} ;
3104
3121
@@ -3455,6 +3472,7 @@ module.exports = {
3455
3472
result = result ( "constref" , name ) ;
3456
3473
}
3457
3474
} else {
3475
+ // @fixme possible #193 bug
3458
3476
result = name ;
3459
3477
}
3460
3478
} else if ( this . token === this . tok . T_STATIC ) {
@@ -3496,7 +3514,7 @@ module.exports = {
3496
3514
return result ( what , offset ) ;
3497
3515
} ,
3498
3516
3499
- recursive_variable_chain_scan : function recursive_variable_chain_scan ( result , read_only , encapsed ) {
3517
+ recursive_variable_chain_scan : function recursive_variable_chain_scan ( result , read_only , encapsed , dereferencable ) {
3500
3518
var name = void 0 ,
3501
3519
node = void 0 ,
3502
3520
offset = void 0 ;
@@ -3551,6 +3569,10 @@ module.exports = {
3551
3569
this . next ( ) ;
3552
3570
}
3553
3571
result = node ( result , offset ) ;
3572
+ // static lookup dereferencables are limited to staticlookup over functions
3573
+ if ( dereferencable && this . token !== "(" ) {
3574
+ this . error ( "(" ) ;
3575
+ }
3554
3576
break ;
3555
3577
case this . tok . T_OBJECT_OPERATOR :
3556
3578
{
@@ -3673,7 +3695,10 @@ module.exports = {
3673
3695
offset = this . next ( ) . read_expr ( ) ;
3674
3696
this . expect ( "}" ) && this . next ( ) ;
3675
3697
result = node ( "offsetlookup" , result , offset ) ;
3676
- } else break ;
3698
+ } else {
3699
+ node . destroy ( ) ;
3700
+ break ;
3701
+ }
3677
3702
}
3678
3703
return result ;
3679
3704
} ,
@@ -4604,6 +4629,7 @@ module.exports = {
4604
4629
} else if ( this . token === this . tok . T_CURLY_OPEN ) {
4605
4630
// expression
4606
4631
// https://github.com/php/php-src/blob/master/Zend/zend_language_parser.y#L1246
4632
+ result . destroy ( ) ;
4607
4633
result = this . next ( ) . read_variable ( false , false , false ) ;
4608
4634
if ( result . kind === "variable" ) {
4609
4635
result . curly = true ;
@@ -4612,6 +4638,7 @@ module.exports = {
4612
4638
} else if ( this . token === this . tok . T_VARIABLE ) {
4613
4639
// plain variable
4614
4640
// https://github.com/php/php-src/blob/master/Zend/zend_language_parser.y#L1231
4641
+ result . destroy ( ) ;
4615
4642
result = this . read_simple_variable ( false ) ;
4616
4643
4617
4644
// https://github.com/php/php-src/blob/master/Zend/zend_language_parser.y#L1233
@@ -4625,8 +4652,8 @@ module.exports = {
4625
4652
// https://github.com/php/php-src/blob/master/Zend/zend_language_parser.y#L1236
4626
4653
if ( this . token === this . tok . T_OBJECT_OPERATOR ) {
4627
4654
node = this . node ( "propertylookup" ) ;
4628
- var what = this . node ( "constref" ) ;
4629
4655
this . next ( ) . expect ( this . tok . T_STRING ) ;
4656
+ var what = this . node ( "constref" ) ;
4630
4657
name = this . text ( ) ;
4631
4658
this . next ( ) ;
4632
4659
result = node ( result , what ( name ) ) ;
@@ -4638,6 +4665,7 @@ module.exports = {
4638
4665
var value = this . text ( ) ;
4639
4666
this . next ( ) ;
4640
4667
// consider it as string
4668
+ result . destroy ( ) ;
4641
4669
result = result ( "string" , false , value , false , value ) ;
4642
4670
}
4643
4671
@@ -5482,6 +5510,9 @@ module.exports = {
5482
5510
}
5483
5511
this . expect ( ":" ) && this . next ( ) ;
5484
5512
return result ( "retif" , expr , trueArg , this . read_expr ( ) ) ;
5513
+ } else {
5514
+ // see #193
5515
+ result . destroy ( expr ) ;
5485
5516
}
5486
5517
5487
5518
return expr ;
@@ -5516,9 +5547,8 @@ module.exports = {
5516
5547
result = result ( "number" , "-" + this . text ( ) , null ) ;
5517
5548
this . next ( ) ;
5518
5549
return result ;
5519
- } else {
5520
- return result ( "unary" , "-" , this . read_expr ( ) ) ;
5521
5550
}
5551
+ return result ( "unary" , "-" , this . read_expr ( ) ) ;
5522
5552
}
5523
5553
5524
5554
if ( this . token === "(" ) {
@@ -5804,6 +5834,9 @@ module.exports = {
5804
5834
if ( isConst ) this . error ( "VARIABLE" ) ;
5805
5835
this . next ( ) ;
5806
5836
return result ( "post" , "-" , expr ) ;
5837
+ default :
5838
+ // see #193
5839
+ result . destroy ( expr ) ;
5807
5840
}
5808
5841
} else if ( this . is ( "SCALAR" ) ) {
5809
5842
result = this . node ( ) ;
@@ -5814,6 +5847,9 @@ module.exports = {
5814
5847
if ( expr . loc ) list . loc = expr . loc ;
5815
5848
var _right = this . next ( ) . read_expr ( ) ;
5816
5849
return result ( "assign" , list , _right , "=" ) ;
5850
+ } else {
5851
+ // see #189 - swap docs on nodes
5852
+ result . destroy ( expr ) ;
5817
5853
}
5818
5854
// classic array
5819
5855
return this . handleDereferencable ( expr ) ;
@@ -5882,8 +5918,8 @@ module.exports = {
5882
5918
} ,
5883
5919
handleDereferencable : function handleDereferencable ( expr ) {
5884
5920
while ( this . token !== this . EOF ) {
5885
- if ( this . token === this . tok . T_OBJECT_OPERATOR ) {
5886
- expr = this . recursive_variable_chain_scan ( expr , false ) ;
5921
+ if ( this . token === this . tok . T_OBJECT_OPERATOR || this . token === this . tok . T_DOUBLE_COLON ) {
5922
+ expr = this . recursive_variable_chain_scan ( expr , false , false , true ) ;
5887
5923
} else if ( this . token === this . tok . T_CURLY_OPEN || this . token === "[" ) {
5888
5924
expr = this . read_dereferencable ( expr ) ;
5889
5925
} else if ( this . token === "(" ) {
@@ -6658,6 +6694,10 @@ parser.prototype.node = function (name) {
6658
6694
if ( this . _docIndex < this . _docs . length ) {
6659
6695
var docs = this . _docs . slice ( this . _docIndex ) ;
6660
6696
this . _docIndex = this . _docs . length ;
6697
+ if ( this . debug ) {
6698
+ console . log ( new Error ( "Append docs on " + name ) ) ;
6699
+ console . log ( docs ) ;
6700
+ }
6661
6701
return this . ast . prepare ( name , docs , this ) ;
6662
6702
}
6663
6703
}
@@ -6684,7 +6724,7 @@ parser.prototype.expectEndOfStatement = function (node) {
6684
6724
} ;
6685
6725
6686
6726
/** outputs some debug information on current token **/
6687
- var ignoreStack = [ "parser.next" ] ;
6727
+ var ignoreStack = [ "parser.next" , "parser.node" , "parser.showlog" ] ;
6688
6728
parser . prototype . showlog = function ( ) {
6689
6729
var stack = new Error ( ) . stack . split ( "\n" ) ;
6690
6730
var line = void 0 ;
0 commit comments