1
- /*! php-parser - BSD3 License - 2017-02-17 */
1
+ /*! php-parser - BSD3 License - 2017-02-26 */
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
@@ -357,6 +357,21 @@ AST.prototype.prepare = function(kind, parser) {
357
357
}
358
358
var result = Object . create ( node . prototype ) ;
359
359
node . apply ( result , args ) ;
360
+ if (
361
+ result . kind === 'bin' &&
362
+ result . right &&
363
+ typeof result . right . precedence === 'function'
364
+ ) {
365
+ var out = result . right . precedence ( result ) ;
366
+ if ( out ) { // shift with precedence
367
+ result = out ;
368
+ }
369
+ } else if ( result . kind === 'unary' ) {
370
+ var out = result . precedence ( result . what ) ;
371
+ if ( out ) { // shift with precedence
372
+ result = out ;
373
+ }
374
+ }
360
375
return result ;
361
376
} ;
362
377
} ;
@@ -525,8 +540,8 @@ var binOperatorsPrecedence = [
525
540
[ 'or' ] ,
526
541
[ 'xor' ] ,
527
542
[ 'and' ] ,
528
- // TODO: assignment
529
- // TODO: ternary ? :
543
+ // TODO: assignment / not sure that PHP allows this with expressions
544
+ [ 'retif' ] ,
530
545
[ '??' ] ,
531
546
[ '||' ] ,
532
547
[ '&&' ] ,
@@ -538,21 +553,13 @@ var binOperatorsPrecedence = [
538
553
[ '<<' , '>>' ] ,
539
554
[ '+' , '-' , '.' ] ,
540
555
[ '*' , '/' , '%' ] ,
541
- // TODO: unary !
556
+ [ '!' ] ,
542
557
[ 'instanceof' ] ,
543
- // TODO: unary ++, --, ~, @, typecasts
558
+ // TODO: typecasts
544
559
// TODO: [ (array)
545
560
// TODO: clone, new
546
561
] ;
547
562
548
- // define nodes shifting
549
- var precedence = { } ;
550
- binOperatorsPrecedence . forEach ( function ( list , index ) {
551
- list . forEach ( function ( operator ) {
552
- precedence [ operator ] = index + 1 ;
553
- } ) ;
554
- } ) ;
555
-
556
563
/*
557
564
x OP1 (y OP2 z)
558
565
z OP1 (x OP2 y)
@@ -568,28 +575,30 @@ z OP2 (x OP1 y)
568
575
*/
569
576
var Bin = Operation . extends ( function Bin ( type , left , right , location ) {
570
577
Operation . apply ( this , [ KIND , location ] ) ;
571
- if ( right && right . kind === 'bin' ) {
572
- var lLevel = precedence [ type ] ;
573
- var rLevel = precedence [ right . type ] ;
574
- if ( lLevel && rLevel && rLevel < lLevel ) {
575
- // shift precedence
576
- var buffer = right . right ;
577
- right . right = right . left ;
578
- right . left = left ;
579
- left = buffer ;
580
- buffer = right . type ;
581
- right . type = type ;
582
- type = buffer ;
583
- buffer = left ;
584
- left = right ;
585
- right = buffer ;
586
- }
587
- }
588
578
this . type = type ;
589
579
this . left = left ;
590
580
this . right = right ;
591
581
} ) ;
592
582
583
+ Bin . prototype . precedence = function ( node ) {
584
+ var lLevel = Bin . precedence [ node . type ] ;
585
+ var rLevel = Bin . precedence [ this . type ] ;
586
+ if ( lLevel && rLevel && rLevel < lLevel ) {
587
+ // shift precedence
588
+ node . right = this . left ;
589
+ this . left = node ;
590
+ return this ;
591
+ }
592
+ } ;
593
+
594
+ // define nodes shifting
595
+ Bin . precedence = { } ;
596
+ binOperatorsPrecedence . forEach ( function ( list , index ) {
597
+ list . forEach ( function ( operator ) {
598
+ Bin . precedence [ operator ] = index + 1 ;
599
+ } ) ;
600
+ } ) ;
601
+
593
602
module . exports = Bin ;
594
603
595
604
} , { "./operation" :57 } ] , 6 :[ function ( require , module , exports ) {
@@ -1862,16 +1871,12 @@ var KIND = 'namespace';
1862
1871
* The main program node
1863
1872
* @constructor Namespace
1864
1873
* @extends {Block }
1865
- * @property {Identifier } name
1874
+ * @property {String } name
1866
1875
* @property {Boolean } withBrackets
1867
1876
*/
1868
1877
var Namespace = Block . extends ( function Namespace ( name , children , withBrackets , location ) {
1869
1878
Block . apply ( this , [ KIND , children , location ] ) ;
1870
- if ( name instanceof Identifier ) {
1871
- this . name = name ;
1872
- } else {
1873
- this . name = new Identifier ( name ) ;
1874
- }
1879
+ this . name = name ;
1875
1880
this . withBrackets = withBrackets || false ;
1876
1881
} ) ;
1877
1882
@@ -2256,6 +2261,8 @@ module.exports = PropertyLookup;
2256
2261
2257
2262
var Statement = require ( './statement' ) ;
2258
2263
var KIND = 'retif' ;
2264
+ var Bin = require ( './bin' ) ;
2265
+ var PRECEDENCE = Bin . precedence [ KIND ] ;
2259
2266
2260
2267
/**
2261
2268
* Defines a short if statement that returns a value
@@ -2272,9 +2279,26 @@ var RetIf = Statement.extends(function RetIf(test, trueExpr, falseExpr, location
2272
2279
this . falseExpr = falseExpr ;
2273
2280
} ) ;
2274
2281
2282
+ /**
2283
+ * Handles precedence over items
2284
+ */
2285
+ RetIf . prototype . precedence = function ( node ) {
2286
+ var what = node . kind === 'bin' ? node . type : node . kind ;
2287
+ var lLevel = Bin . precedence [ what ] ;
2288
+ if ( lLevel && PRECEDENCE < lLevel ) {
2289
+ if ( node . kind === 'bin' ) {
2290
+ node . right = this . test ;
2291
+ this . test = node ;
2292
+ return this ;
2293
+ } else {
2294
+ throw new Error ( '@todo ' + node . kind ) ;
2295
+ }
2296
+ }
2297
+ } ;
2298
+
2275
2299
module . exports = RetIf ;
2276
2300
2277
- } , { "./statement" :70 } ] , 68 :[ function ( require , module , exports ) {
2301
+ } , { "./bin" : 5 , "./ statement" :70 } ] , 68 :[ function ( require , module , exports ) {
2278
2302
/*!
2279
2303
* Copyright (C) 2017 Glayzzle (BSD3 License)
2280
2304
* @authors https://github.com/glayzzle/php-parser/graphs/contributors
@@ -2660,6 +2684,18 @@ var Unary = Operation.extends(function Unary(type, what, location) {
2660
2684
this . what = what ;
2661
2685
} ) ;
2662
2686
2687
+ Unary . prototype . precedence = function ( node ) {
2688
+ if ( node . kind === 'bin' ) {
2689
+ this . what = node . left ;
2690
+ node . left = this ;
2691
+ return node ;
2692
+ } else if ( node . kind === 'retif' ) {
2693
+ this . what = node . test ;
2694
+ node . test = this ;
2695
+ return node ;
2696
+ }
2697
+ } ;
2698
+
2663
2699
module . exports = Unary ;
2664
2700
2665
2701
} , { "./operation" :57 } ] , 83 :[ function ( require , module , exports ) {
@@ -2697,7 +2733,7 @@ var KIND = 'usegroup';
2697
2733
* Defines a use statement (with a list of use items)
2698
2734
* @constructor UseGroup
2699
2735
* @extends {Statement }
2700
- * @property {Identifier |null } name
2736
+ * @property {String |null } name
2701
2737
* @property {String|null } type - Possible value : function, const
2702
2738
* @property {UseItem[] } item
2703
2739
* @see {Namespace}
@@ -2726,7 +2762,7 @@ var KIND = 'useitem';
2726
2762
* Defines a use statement (from namespace)
2727
2763
* @constructor UseItem
2728
2764
* @extends {Statement }
2729
- * @property {Identifier } name
2765
+ * @property {String } name
2730
2766
* @property {String|null } type - Possible value : function, const
2731
2767
* @property {String|null } alias
2732
2768
* @see {Namespace}
@@ -6569,12 +6605,12 @@ module.exports = {
6569
6605
this . currentNamespace = name ;
6570
6606
var body = this . nextWithComments ( ) . read_top_statements ( ) ;
6571
6607
this . expect ( this . EOF ) ;
6572
- return result ( name , body , false ) ;
6608
+ return result ( name . name , body , false ) ;
6573
6609
} else if ( this . token == '{' ) {
6574
6610
this . currentNamespace = name ;
6575
6611
var body = this . nextWithComments ( ) . read_top_statements ( ) ;
6576
6612
this . expect ( '}' ) && this . nextWithComments ( ) ;
6577
- return result ( name , body , true ) ;
6613
+ return result ( name . name , body , true ) ;
6578
6614
} else if ( this . token === '(' ) {
6579
6615
// resolve ambuiguity between namespace & function call
6580
6616
name . resolution = this . ast . identifier . RELATIVE_NAME ;
@@ -6635,7 +6671,7 @@ module.exports = {
6635
6671
if ( this . token === ',' ) {
6636
6672
items = items . concat ( this . next ( ) . read_use_declarations ( false ) ) ;
6637
6673
} else if ( this . token === '{' ) {
6638
- name = items [ 0 ] . name ;
6674
+ name = items [ 0 ] . name . name ;
6639
6675
items = this . next ( ) . read_use_declarations ( type === null ) ;
6640
6676
this . expect ( '}' ) && this . next ( ) ;
6641
6677
}
@@ -6655,7 +6691,7 @@ module.exports = {
6655
6691
if ( typed ) type = this . read_use_type ( ) ;
6656
6692
var name = this . read_namespace_name ( ) ;
6657
6693
var alias = this . read_use_alias ( ) ;
6658
- return result ( name , alias , type ) ;
6694
+ return result ( name . name , alias , type ) ;
6659
6695
}
6660
6696
/**
6661
6697
* Reads a list of use declarations
@@ -7235,9 +7271,9 @@ module.exports = {
7235
7271
return result ( args ) ;
7236
7272
7237
7273
case this . tok . T_INLINE_HTML :
7238
- var result = this . node ( 'inline' ) ( this . text ( ) ) ;
7274
+ var result = this . node ( 'inline' ) , value = this . text ( ) ;
7239
7275
this . next ( ) ;
7240
- return result ;
7276
+ return result ( value ) ;
7241
7277
7242
7278
case this . tok . T_UNSET :
7243
7279
var result = this . node ( 'unset' ) ;
0 commit comments