1
- /*! php-parser - BSD3 License - 2016-12-17 */
1
+ /*! php-parser - BSD3 License - 2016-12-20 */
2
2
3
3
require = ( function e ( t , n , r ) { function s ( o , u ) { if ( ! n [ o ] ) { if ( ! t [ o ] ) { var a = typeof require == "function" && require ; if ( ! u && a ) return a ( o , ! 0 ) ; if ( i ) return i ( o , ! 0 ) ; var f = new Error ( "Cannot find module '" + o + "'" ) ; throw f . code = "MODULE_NOT_FOUND" , f } var l = n [ o ] = { exports :{ } } ; t [ o ] [ 0 ] . call ( l . exports , function ( e ) { var n = t [ o ] [ 1 ] [ e ] ; return s ( n ?n :e ) } , l , l . exports , e , t , n , r ) } return n [ o ] . exports } var i = typeof require == "function" && require ; for ( var o = 0 ; o < r . length ; o ++ ) s ( r [ o ] ) ; return s } ) ( { 1 :[ function ( require , module , exports ) {
4
4
// shim for using process in browser
@@ -1790,38 +1790,6 @@ function isNumber(n) {
1790
1790
}
1791
1791
1792
1792
1793
- /**
1794
- * Graceful decorator
1795
- */
1796
- var _gracefulDecorator = function ( fn ) {
1797
- try {
1798
- this . _currentNode = this . _gracefulProxy [ fn ] . apply (
1799
- this ,
1800
- Array . prototype . slice . call ( arguments , 1 )
1801
- ) ;
1802
- return this . _currentNode ;
1803
- } catch ( e ) {
1804
- if ( this . lastError ) {
1805
- this . next ( ) ; // ignore token & go next
1806
- var errorNode = [
1807
- 'error' ,
1808
- this . lastError . tokenName ,
1809
- this . lastError . expected ,
1810
- this . lastError . line
1811
- ] ;
1812
- // force to append the error node
1813
- if ( this . ast . length < 3 ) {
1814
- this . ast . push ( [ ] ) ;
1815
- }
1816
- this . ast [ 2 ] . push ( errorNode ) ;
1817
- // return the node
1818
- return errorNode ;
1819
- } else {
1820
- throw e ; // not a parsing error
1821
- }
1822
- }
1823
- } ;
1824
-
1825
1793
/**
1826
1794
* The PHP Parser class
1827
1795
*
@@ -1962,40 +1930,12 @@ parser.prototype.getTokenName = function(token) {
1962
1930
}
1963
1931
} ;
1964
1932
1965
- /**
1966
- * enable / disable the graceful mode
1967
- */
1968
- parser . prototype . graceful = function ( mode ) {
1969
- if ( this . _graceful !== mode ) {
1970
- if ( mode ) {
1971
- // enable the graceful mode
1972
- this . _gracefulProxy = { } ;
1973
- for ( var i in this ) {
1974
- var cb = this [ i ] ;
1975
- if ( typeof cb === 'function' ) {
1976
- this . _gracefulProxy [ i ] = cb ;
1977
- this [ i ] = _gracefulDecorator . bind ( this , i ) ;
1978
- }
1979
- }
1980
- } else {
1981
- // disable the graceful mode
1982
- for ( var i in this . _gracefulProxy ) {
1983
- this [ i ] = this . _gracefulProxy [ i ] ;
1984
- }
1985
- }
1986
- this . _graceful = mode ;
1987
- }
1988
- return this ;
1989
- } ;
1990
-
1991
1933
/**
1992
1934
* main entry point : converts a source code to AST
1993
1935
*/
1994
1936
parser . prototype . parse = function ( code ) {
1937
+ this . firstError = false ;
1995
1938
this . lastError = false ;
1996
- if ( this . suppressErrors ) {
1997
- this . graceful ( this . suppressErrors ) ;
1998
- }
1999
1939
this . currentNamespace = [ '' ] ;
2000
1940
this . lexer . setInput ( code ) ;
2001
1941
this . lexer . comment_tokens = this . extractDoc ;
@@ -2030,9 +1970,24 @@ parser.prototype.raiseError = function(message, msgExpect, expect, token) {
2030
1970
message : message ,
2031
1971
line : this . lexer . yylloc . first_line
2032
1972
} ;
1973
+ if ( ! this . firstError ) {
1974
+ this . firstError = this . lastError ;
1975
+ }
2033
1976
if ( ! this . suppressErrors ) {
2034
- throw new Error ( this . lastError . message ) ;
1977
+ throw new Error ( message ) ;
1978
+ }
1979
+ if ( this . ast . length === 2 ) {
1980
+ this . ast . push ( [ ] ) ;
2035
1981
}
1982
+ // Error node :
1983
+ var node = [
1984
+ 'error' ,
1985
+ this . token ,
1986
+ message ,
1987
+ this . lexer . yylloc . first_line
1988
+ ] ;
1989
+ this . ast [ 2 ] . push ( node ) ;
1990
+ return node ;
2036
1991
} ;
2037
1992
2038
1993
/**
@@ -2133,7 +2088,7 @@ parser.prototype.expectEndOfStatement = function() {
2133
2088
} ;
2134
2089
2135
2090
/** outputs some debug information on current token **/
2136
- var ignoreStack = [ 'parser.next' , '_gracefulDecorator ' ] ;
2091
+ var ignoreStack = [ 'parser.next' , 'parser.nextWithComments ' ] ;
2137
2092
parser . prototype . showlog = function ( ) {
2138
2093
var stack = ( new Error ( ) ) . stack . split ( '\n' ) ;
2139
2094
var line ;
@@ -2164,14 +2119,12 @@ parser.prototype.showlog = function() {
2164
2119
2165
2120
/** force to expect specified token **/
2166
2121
parser . prototype . expect = function ( token ) {
2167
- if ( ! this . lastError ) {
2168
- if ( Array . isArray ( token ) ) {
2169
- if ( token . indexOf ( this . token ) === - 1 ) {
2170
- this . error ( token ) ;
2171
- }
2172
- } else if ( this . token != token ) {
2122
+ if ( Array . isArray ( token ) ) {
2123
+ if ( token . indexOf ( this . token ) === - 1 ) {
2173
2124
this . error ( token ) ;
2174
2125
}
2126
+ } else if ( this . token != token ) {
2127
+ this . error ( token ) ;
2175
2128
}
2176
2129
return this ;
2177
2130
} ;
@@ -2544,6 +2497,7 @@ module.exports = {
2544
2497
this . tok . T_VARIABLE ,
2545
2498
this . tok . T_FUNCTION
2546
2499
] ) ;
2500
+ this . next ( ) ; // ignore token
2547
2501
}
2548
2502
}
2549
2503
this . expect ( '}' ) . nextWithComments ( ) ;
@@ -2622,7 +2576,6 @@ module.exports = {
2622
2576
if ( this . is ( 'T_MEMBER_FLAGS' ) ) {
2623
2577
var idx = 0 , val = 0 ;
2624
2578
do {
2625
-
2626
2579
switch ( this . token ) {
2627
2580
case this . tok . T_PUBLIC : idx = 0 ; val = 0 ; break ;
2628
2581
case this . tok . T_PROTECTED : idx = 0 ; val = 1 ; break ;
@@ -2633,13 +2586,21 @@ module.exports = {
2633
2586
}
2634
2587
if ( asInterface ) {
2635
2588
if ( idx == 0 && val == 2 ) {
2589
+ // an interface can't be private
2636
2590
this . expect ( [ this . tok . T_PUBLIC , this . tok . T_PROTECTED ] ) ;
2591
+ val = - 1 ;
2637
2592
} else if ( idx == 2 && val == 1 ) {
2593
+ // an interface cant be abstract
2638
2594
this . error ( ) ;
2595
+ val = - 1 ;
2639
2596
}
2640
2597
}
2641
- if ( result [ idx ] != - 1 ) this . error ( ) ;
2642
- result [ idx ] = val ;
2598
+ if ( result [ idx ] !== - 1 ) {
2599
+ // already defined flag
2600
+ this . error ( ) ;
2601
+ } else if ( val !== - 1 ) {
2602
+ result [ idx ] = val ;
2603
+ }
2643
2604
} while ( this . next ( ) . is ( 'T_MEMBER_FLAGS' ) ) ;
2644
2605
}
2645
2606
@@ -2751,6 +2712,7 @@ module.exports = {
2751
2712
this . tok . T_CONST ,
2752
2713
this . tok . T_FUNCTION
2753
2714
] ) ;
2715
+ this . next ( ) ;
2754
2716
}
2755
2717
}
2756
2718
this . expect ( '}' ) . next ( ) ;
@@ -3210,7 +3172,8 @@ module.exports = {
3210
3172
}
3211
3173
}
3212
3174
} else {
3213
- this . error ( 'EXPR' ) ;
3175
+ expr = this . error ( 'EXPR' ) ;
3176
+ this . next ( ) ;
3214
3177
}
3215
3178
3216
3179
// returns variable | scalar
@@ -3415,6 +3378,7 @@ module.exports = {
3415
3378
break ;
3416
3379
} else {
3417
3380
this . error ( [ ',' , ')' ] ) ;
3381
+ break ;
3418
3382
}
3419
3383
}
3420
3384
}
@@ -3744,7 +3708,10 @@ module.exports = {
3744
3708
this . currentNamespace = [ '' ] ;
3745
3709
return result ( [ '' ] , this . read_code_block ( true ) ) ;
3746
3710
} else {
3747
- if ( this . token === this . tok . T_NAMESPACE ) this . error ( [ '{' , this . tok . T_STRING ] ) ;
3711
+ if ( this . token === this . tok . T_NAMESPACE ) {
3712
+ this . error ( [ '{' , this . tok . T_STRING ] ) ;
3713
+ this . next ( ) ; // ignore namespace token
3714
+ }
3748
3715
var name = this . read_namespace_name ( ) ;
3749
3716
if ( this . token == ';' ) {
3750
3717
this . currentNamespace = name ;
@@ -3762,6 +3729,11 @@ module.exports = {
3762
3729
) ;
3763
3730
} else {
3764
3731
this . error ( [ '{' , ';' ] ) ;
3732
+ // graceful mode :
3733
+ this . currentNamespace = name ;
3734
+ var body = this . read_top_statements ( ) ;
3735
+ this . expect ( this . EOF ) ;
3736
+ return result ( name , body ) ;
3765
3737
}
3766
3738
}
3767
3739
}
@@ -3983,7 +3955,10 @@ module.exports = {
3983
3955
case '[' : // short array format
3984
3956
return this . read_array ( ) ;
3985
3957
default :
3986
- this . error ( 'SCALAR' ) ;
3958
+ var err = this . error ( 'SCALAR' ) ;
3959
+ // graceful mode : ignore token & return error node
3960
+ this . next ( ) ;
3961
+ return err ;
3987
3962
}
3988
3963
}
3989
3964
}
@@ -4131,7 +4106,9 @@ module.exports = {
4131
4106
case this . tok . T_INTERFACE :
4132
4107
return this . read_interface ( flag ) ;
4133
4108
default :
4134
- this . error ( [ this . tok . T_CLASS , this . tok . T_INTERFACE ] ) ;
4109
+ var err = this . error ( [ this . tok . T_CLASS , this . tok . T_INTERFACE ] ) ;
4110
+ this . next ( ) ;
4111
+ return err ;
4135
4112
}
4136
4113
case this . tok . T_CLASS :
4137
4114
return this . read_class ( 0 ) ;
@@ -4228,7 +4205,10 @@ module.exports = {
4228
4205
case this . tok . T_INTERFACE :
4229
4206
return this . read_interface ( flag ) ;
4230
4207
default :
4231
- this . error ( [ this . tok . T_CLASS , this . tok . T_INTERFACE ] ) ;
4208
+ var err = this . error ( [ this . tok . T_CLASS , this . tok . T_INTERFACE ] ) ;
4209
+ // graceful mode : ignore token & go next
4210
+ this . next ( ) ;
4211
+ return err ;
4232
4212
}
4233
4213
case this . tok . T_CLASS :
4234
4214
return this . read_class ( 0 ) ;
@@ -4597,7 +4577,9 @@ module.exports = {
4597
4577
getter = this . text ( ) ;
4598
4578
this . next ( ) ;
4599
4579
} else {
4600
- this . error ( [ this . tok . T_VARIABLE , this . tok . T_STRING ] ) ;
4580
+ getter = this . error ( [ this . tok . T_VARIABLE , this . tok . T_STRING ] ) ;
4581
+ // graceful mode : set getter as error node and continue
4582
+ this . next ( ) ;
4601
4583
}
4602
4584
if ( from [ 0 ] != 'ns' ) {
4603
4585
from = [ 'lookup' , 'class' , from ] ;
@@ -4669,7 +4651,10 @@ module.exports = {
4669
4651
this . expect ( '}' ) . next ( ) ;
4670
4652
break ;
4671
4653
default :
4672
- this . error ( [ this . tok . T_STRING , this . tok . T_VARIABLE ] ) ;
4654
+ what = this . error ( [ this . tok . T_STRING , this . tok . T_VARIABLE ] ) ;
4655
+ // graceful mode : set what as error mode & continue
4656
+ this . next ( ) ;
4657
+ break ;
4673
4658
}
4674
4659
result = [ 'prop' , result , what ] ;
4675
4660
break ;
@@ -4755,7 +4740,9 @@ module.exports = {
4755
4740
this . next ( ) ;
4756
4741
break ;
4757
4742
default :
4758
- this . error ( [ '{' , '$' , this . tok . T_VARIABLE ] ) ;
4743
+ result = this . error ( [ '{' , '$' , this . tok . T_VARIABLE ] ) ;
4744
+ // graceful mode
4745
+ this . next ( ) ;
4759
4746
}
4760
4747
result = [ 'lookup' , 'var' , result ] ;
4761
4748
}
0 commit comments