diff --git a/bin/php-parse b/bin/php-parse index c3a46cfdaa..db6c411da9 100755 --- a/bin/php-parse +++ b/bin/php-parse @@ -27,10 +27,10 @@ if (empty($files)) { } $lexer = new PhpParser\Lexer\Emulative(array('usedAttributes' => array( - 'startLine', 'endLine', 'startFilePos', 'endFilePos' + 'startLine', 'endLine', 'startFilePos', 'endFilePos', 'comments' ))); $parser = (new PhpParser\ParserFactory)->create(PhpParser\ParserFactory::PREFER_PHP7, $lexer); -$dumper = new PhpParser\NodeDumper; +$dumper = new PhpParser\NodeDumper(['dumpComments' => true]); $prettyPrinter = new PhpParser\PrettyPrinter\Standard; $serializer = new PhpParser\Serializer\XML; diff --git a/lib/PhpParser/NodeDumper.php b/lib/PhpParser/NodeDumper.php index 57a6fe7f74..b931adaa11 100644 --- a/lib/PhpParser/NodeDumper.php +++ b/lib/PhpParser/NodeDumper.php @@ -4,6 +4,18 @@ class NodeDumper { + private $dumpComments; + + /** + * Constructs a NodeDumper. + * + * @param array $options Boolean option 'dumpComments' controls whether comments should be + * dumped + */ + public function __construct(array $options = []) { + $this->dumpComments = !empty($options['dumpComments']); + } + /** * Dumps a node or array. * @@ -31,6 +43,10 @@ public function dump($node) { $r .= str_replace("\n", "\n ", $this->dump($value)); } } + + if ($this->dumpComments && $comments = $node->getAttribute('comments')) { + $r .= "\n comments: " . str_replace("\n", "\n ", $this->dump($comments)); + } } elseif (is_array($node)) { $r = 'array('; @@ -49,6 +65,8 @@ public function dump($node) { $r .= str_replace("\n", "\n ", $this->dump($value)); } } + } elseif ($node instanceof Comment) { + return $node->getReformattedText(); } else { throw new \InvalidArgumentException('Can only dump nodes and arrays.'); } diff --git a/test/PhpParser/CodeParsingTest.php b/test/PhpParser/CodeParsingTest.php index 0b75342072..5eb695ea49 100644 --- a/test/PhpParser/CodeParsingTest.php +++ b/test/PhpParser/CodeParsingTest.php @@ -13,7 +13,7 @@ class CodeParsingTest extends CodeTestAbstract */ public function testParse($name, $code, $expected, $mode) { $lexer = new Lexer\Emulative(array('usedAttributes' => array( - 'startLine', 'endLine', 'startFilePos', 'endFilePos' + 'startLine', 'endLine', 'startFilePos', 'endFilePos', 'comments' ))); $parser5 = new Parser\Php5($lexer, array( 'throwOnError' => false, @@ -47,7 +47,7 @@ private function getParseOutput(Parser $parser, $code) { } if (null !== $stmts) { - $dumper = new NodeDumper; + $dumper = new NodeDumper(['dumpComments' => true]); $output .= $dumper->dump($stmts); } diff --git a/test/code/parser/expr/arrayDef.test b/test/code/parser/expr/arrayDef.test index 7ea11c712f..28baf2664f 100644 --- a/test/code/parser/expr/arrayDef.test +++ b/test/code/parser/expr/arrayDef.test @@ -97,6 +97,9 @@ array( 5: Expr_Array( items: array( ) + comments: array( + 0: // short array syntax + ) ) 6: Expr_Array( items: array( diff --git a/test/code/parser/expr/assign.test b/test/code/parser/expr/assign.test index 66ae18f498..c2b185727f 100644 --- a/test/code/parser/expr/assign.test +++ b/test/code/parser/expr/assign.test @@ -40,18 +40,30 @@ array( 0: Expr_Assign( var: Expr_Variable( name: a + comments: array( + 0: // simple assign + ) ) expr: Expr_Variable( name: b ) + comments: array( + 0: // simple assign + ) ) 1: Expr_AssignOp_BitwiseAnd( var: Expr_Variable( name: a + comments: array( + 0: // combined assign + ) ) expr: Expr_Variable( name: b ) + comments: array( + 0: // combined assign + ) ) 2: Expr_AssignOp_BitwiseOr( var: Expr_Variable( @@ -144,6 +156,9 @@ array( 13: Expr_Assign( var: Expr_Variable( name: a + comments: array( + 0: // chained assign + ) ) expr: Expr_AssignOp_Mul( var: Expr_Variable( @@ -158,14 +173,23 @@ array( ) ) ) + comments: array( + 0: // chained assign + ) ) 14: Expr_AssignRef( var: Expr_Variable( name: a + comments: array( + 0: // by ref assign + ) ) expr: Expr_Variable( name: b ) + comments: array( + 0: // by ref assign + ) ) 15: Expr_AssignRef( var: Expr_Variable( @@ -188,10 +212,16 @@ array( name: a ) ) + comments: array( + 0: // list() assign + ) ) expr: Expr_Variable( name: b ) + comments: array( + 0: // list() assign + ) ) 17: Expr_Assign( var: Expr_List( @@ -236,6 +266,9 @@ array( var: Expr_Variable( name: a ) + comments: array( + 0: // inc/dec + ) ) 20: Expr_PostInc( var: Expr_Variable( diff --git a/test/code/parser/expr/fetchAndCall/funcCall.test b/test/code/parser/expr/fetchAndCall/funcCall.test index b5b8be5d8e..62f69e3474 100644 --- a/test/code/parser/expr/fetchAndCall/funcCall.test +++ b/test/code/parser/expr/fetchAndCall/funcCall.test @@ -21,9 +21,15 @@ array( parts: array( 0: a ) + comments: array( + 0: // function name variations + ) ) args: array( ) + comments: array( + 0: // function name variations + ) ) 1: Expr_FuncCall( name: Expr_Variable( @@ -106,12 +112,21 @@ array( parts: array( 0: a ) + comments: array( + 0: // array dereferencing + ) ) args: array( ) + comments: array( + 0: // array dereferencing + ) ) dim: Scalar_String( value: b ) + comments: array( + 0: // array dereferencing + ) ) ) \ No newline at end of file diff --git a/test/code/parser/expr/fetchAndCall/objectAccess.test b/test/code/parser/expr/fetchAndCall/objectAccess.test index 74f8080bd7..3cdefea117 100644 --- a/test/code/parser/expr/fetchAndCall/objectAccess.test +++ b/test/code/parser/expr/fetchAndCall/objectAccess.test @@ -22,8 +22,14 @@ array( 0: Expr_PropertyFetch( var: Expr_Variable( name: a + comments: array( + 0: // property fetch variations + ) ) name: b + comments: array( + 0: // property fetch variations + ) ) 1: Expr_ArrayDimFetch( var: Expr_PropertyFetch( @@ -50,10 +56,16 @@ array( 3: Expr_MethodCall( var: Expr_Variable( name: a + comments: array( + 0: // method call variations + ) ) name: b args: array( ) + comments: array( + 0: // method call variations + ) ) 4: Expr_MethodCall( var: Expr_Variable( @@ -94,14 +106,23 @@ array( var: Expr_MethodCall( var: Expr_Variable( name: a + comments: array( + 0: // array dereferencing + ) ) name: b args: array( ) + comments: array( + 0: // array dereferencing + ) ) dim: Scalar_String( value: c ) + comments: array( + 0: // array dereferencing + ) ) 8: Expr_ArrayDimFetch( var: Expr_MethodCall( @@ -116,4 +137,4 @@ array( value: c ) ) -) +) \ No newline at end of file diff --git a/test/code/parser/expr/fetchAndCall/staticCall.test b/test/code/parser/expr/fetchAndCall/staticCall.test index 443489bacc..b806f7de41 100644 --- a/test/code/parser/expr/fetchAndCall/staticCall.test +++ b/test/code/parser/expr/fetchAndCall/staticCall.test @@ -25,10 +25,16 @@ array( parts: array( 0: A ) + comments: array( + 0: // method name variations + ) ) name: b args: array( ) + comments: array( + 0: // method name variations + ) ) 1: Expr_StaticCall( class: Name( @@ -99,24 +105,39 @@ array( parts: array( 0: A ) + comments: array( + 0: // array dereferencing + ) ) name: b args: array( ) + comments: array( + 0: // array dereferencing + ) ) dim: Scalar_String( value: c ) + comments: array( + 0: // array dereferencing + ) ) 6: Expr_StaticCall( class: Name( parts: array( 0: static ) + comments: array( + 0: // class name variations + ) ) name: b args: array( ) + comments: array( + 0: // class name variations + ) ) 7: Expr_StaticCall( class: Expr_Variable( @@ -149,4 +170,4 @@ array( args: array( ) ) -) +) \ No newline at end of file diff --git a/test/code/parser/expr/fetchAndCall/staticPropertyFetch.test b/test/code/parser/expr/fetchAndCall/staticPropertyFetch.test index f1b08ebaa7..1d56af90e2 100644 --- a/test/code/parser/expr/fetchAndCall/staticPropertyFetch.test +++ b/test/code/parser/expr/fetchAndCall/staticPropertyFetch.test @@ -19,8 +19,14 @@ array( parts: array( 0: A ) + comments: array( + 0: // property name variations + ) ) name: b + comments: array( + 0: // property name variations + ) ) 1: Expr_StaticPropertyFetch( class: Name( @@ -48,12 +54,21 @@ array( parts: array( 0: A ) + comments: array( + 0: // array access + ) ) name: b + comments: array( + 0: // array access + ) ) dim: Scalar_String( value: c ) + comments: array( + 0: // array access + ) ) 4: Expr_ArrayDimFetch( var: Expr_StaticPropertyFetch( diff --git a/test/code/parser/expr/logic.test b/test/code/parser/expr/logic.test index 967afdd2cf..b3634e91ab 100644 --- a/test/code/parser/expr/logic.test +++ b/test/code/parser/expr/logic.test @@ -24,10 +24,16 @@ array( 0: Expr_BinaryOp_BooleanAnd( left: Expr_Variable( name: a + comments: array( + 0: // boolean ops + ) ) right: Expr_Variable( name: b ) + comments: array( + 0: // boolean ops + ) ) 1: Expr_BinaryOp_BooleanOr( left: Expr_Variable( @@ -52,10 +58,16 @@ array( 4: Expr_BinaryOp_LogicalAnd( left: Expr_Variable( name: a + comments: array( + 0: // logical ops + ) ) right: Expr_Variable( name: b ) + comments: array( + 0: // logical ops + ) ) 5: Expr_BinaryOp_LogicalOr( left: Expr_Variable( @@ -77,10 +89,16 @@ array( left: Expr_BinaryOp_BooleanAnd( left: Expr_Variable( name: a + comments: array( + 0: // precedence + ) ) right: Expr_Variable( name: b ) + comments: array( + 0: // precedence + ) ) right: Expr_BinaryOp_BooleanAnd( left: Expr_Variable( @@ -90,6 +108,9 @@ array( name: d ) ) + comments: array( + 0: // precedence + ) ) 8: Expr_BinaryOp_BooleanAnd( left: Expr_BinaryOp_BooleanAnd( diff --git a/test/code/parser/expr/math.test b/test/code/parser/expr/math.test index 7a2c744cc4..3c00ebc70c 100644 --- a/test/code/parser/expr/math.test +++ b/test/code/parser/expr/math.test @@ -38,6 +38,9 @@ array( expr: Expr_Variable( name: a ) + comments: array( + 0: // unary ops + ) ) 1: Expr_UnaryPlus( expr: Expr_Variable( @@ -52,10 +55,16 @@ array( 3: Expr_BinaryOp_BitwiseAnd( left: Expr_Variable( name: a + comments: array( + 0: // binary ops + ) ) right: Expr_Variable( name: b ) + comments: array( + 0: // binary ops + ) ) 4: Expr_BinaryOp_BitwiseOr( left: Expr_Variable( @@ -149,14 +158,23 @@ array( left: Expr_BinaryOp_Mul( left: Expr_Variable( name: a + comments: array( + 0: // associativity + ) ) right: Expr_Variable( name: b ) + comments: array( + 0: // associativity + ) ) right: Expr_Variable( name: c ) + comments: array( + 0: // associativity + ) ) 16: Expr_BinaryOp_Mul( left: Expr_Variable( @@ -174,6 +192,9 @@ array( 17: Expr_BinaryOp_Plus( left: Expr_Variable( name: a + comments: array( + 0: // precedence + ) ) right: Expr_BinaryOp_Mul( left: Expr_Variable( @@ -183,6 +204,9 @@ array( name: c ) ) + comments: array( + 0: // precedence + ) ) 18: Expr_BinaryOp_Mul( left: Expr_BinaryOp_Plus( @@ -200,6 +224,9 @@ array( 19: Expr_BinaryOp_Pow( left: Expr_Variable( name: a + comments: array( + 0: // pow is special + ) ) right: Expr_BinaryOp_Pow( left: Expr_Variable( @@ -209,6 +236,9 @@ array( name: c ) ) + comments: array( + 0: // pow is special + ) ) 20: Expr_BinaryOp_Pow( left: Expr_BinaryOp_Pow( diff --git a/test/code/parser/expr/ternaryAndCoalesce.test b/test/code/parser/expr/ternaryAndCoalesce.test index 5ebf5fe19e..268935dbdc 100644 --- a/test/code/parser/expr/ternaryAndCoalesce.test +++ b/test/code/parser/expr/ternaryAndCoalesce.test @@ -20,6 +20,9 @@ array( 0: Expr_Ternary( cond: Expr_Variable( name: a + comments: array( + 0: // ternary + ) ) if: Expr_Variable( name: b @@ -27,6 +30,9 @@ array( else: Expr_Variable( name: c ) + comments: array( + 0: // ternary + ) ) 1: Expr_Ternary( cond: Expr_Variable( @@ -41,6 +47,9 @@ array( cond: Expr_Ternary( cond: Expr_Variable( name: a + comments: array( + 0: // precedence + ) ) if: Expr_Variable( name: b @@ -48,6 +57,9 @@ array( else: Expr_Variable( name: c ) + comments: array( + 0: // precedence + ) ) if: Expr_Variable( name: d @@ -55,6 +67,9 @@ array( else: Expr_Variable( name: e ) + comments: array( + 0: // precedence + ) ) 3: Expr_Ternary( cond: Expr_Variable( @@ -78,10 +93,16 @@ array( 4: Expr_BinaryOp_Coalesce( left: Expr_Variable( name: a + comments: array( + 0: // null coalesce + ) ) right: Expr_Variable( name: b ) + comments: array( + 0: // null coalesce + ) ) 5: Expr_BinaryOp_Coalesce( left: Expr_Variable( @@ -125,4 +146,4 @@ array( name: c ) ) -) +) \ No newline at end of file diff --git a/test/code/parser/scalar/docString.test b/test/code/parser/scalar/docString.test index 09e367af94..a57fc37be4 100644 --- a/test/code/parser/scalar/docString.test +++ b/test/code/parser/scalar/docString.test @@ -29,12 +29,18 @@ EOS; array( 0: Scalar_String( value: + comments: array( + 0: // empty strings + ) ) 1: Scalar_String( value: ) 2: Scalar_String( value: Test '" $a \n + comments: array( + 0: // constant encapsed strings + ) ) 3: Scalar_String( value: Test '" $a @@ -49,6 +55,9 @@ array( name: a ) ) + comments: array( + 0: // encapsed strings + ) ) 5: Scalar_Encapsed( parts: array( diff --git a/test/code/parser/scalar/float.test b/test/code/parser/scalar/float.test index c91b7ac7b8..a16028e2a3 100644 --- a/test/code/parser/scalar/float.test +++ b/test/code/parser/scalar/float.test @@ -53,18 +53,22 @@ array( value: INF ) 10: Scalar_DNumber( - value: @@{ 0xFFFFFFFFFFFFFFFF }@@ + value: 1.844674407371E+19 + comments: array( + 0: // various integer -> float overflows + 1: // (all are actually the same number, just in different representations) + ) ) 11: Scalar_DNumber( - value: @@{ 0xFFFFFFFFFFFFFFFF }@@ + value: 1.844674407371E+19 ) 12: Scalar_DNumber( - value: @@{ 0xFFFFFFFFFFFFFFFF }@@ + value: 1.844674407371E+19 ) 13: Scalar_DNumber( - value: @@{ 0xFFFFFFFFFFFFFFFF }@@ + value: 1.844674407371E+19 ) 14: Scalar_DNumber( - value: @@{ 0xFFFFFFFFFFFFFFFF }@@ + value: 1.844674407371E+19 ) ) \ No newline at end of file diff --git a/test/code/parser/stmt/generator/basic.test b/test/code/parser/stmt/generator/basic.test index f18b4b7da4..8a184aaf68 100644 --- a/test/code/parser/stmt/generator/basic.test +++ b/test/code/parser/stmt/generator/basic.test @@ -42,6 +42,9 @@ array( 0: Expr_Yield( key: null value: null + comments: array( + 0: // statements + ) ) 1: Expr_Yield( key: null @@ -60,11 +63,17 @@ array( 3: Expr_Assign( var: Expr_Variable( name: data + comments: array( + 0: // expressions + ) ) expr: Expr_Yield( key: null value: null ) + comments: array( + 0: // expressions + ) ) 4: Expr_Assign( var: Expr_Variable( @@ -112,6 +121,9 @@ array( ) ) else: null + comments: array( + 0: // yield in language constructs with their own parentheses + ) ) 7: Stmt_If( cond: Expr_Yield( @@ -179,6 +191,9 @@ array( parts: array( 0: func ) + comments: array( + 0: // yield in function calls + ) ) args: array( 0: Arg( @@ -192,6 +207,9 @@ array( unpack: false ) ) + comments: array( + 0: // yield in function calls + ) ) 13: Expr_MethodCall( var: Expr_Variable( @@ -259,4 +277,4 @@ array( ) ) ) -) +) \ No newline at end of file diff --git a/test/code/parser/stmt/if.test b/test/code/parser/stmt/if.test index 6ae1e1652c..dee61157a1 100644 --- a/test/code/parser/stmt/if.test +++ b/test/code/parser/stmt/if.test @@ -81,6 +81,9 @@ array( stmts: array( ) ) + comments: array( + 0: // without else + ) ) 3: Stmt_If( cond: Expr_Variable( diff --git a/test/code/parser/stmt/loop/for.test b/test/code/parser/stmt/loop/for.test index 6ffc2c549e..c942d311e6 100644 --- a/test/code/parser/stmt/loop/for.test +++ b/test/code/parser/stmt/loop/for.test @@ -46,6 +46,9 @@ array( ) stmts: array( ) + comments: array( + 0: // "classical" loop + ) ) 1: Stmt_For( init: array( @@ -74,6 +77,9 @@ array( ) stmts: array( ) + comments: array( + 0: // multiple expressions + ) ) 2: Stmt_For( init: array( @@ -84,6 +90,9 @@ array( ) stmts: array( ) + comments: array( + 0: // infinite loop + ) ) 3: Stmt_For( init: array( @@ -94,5 +103,8 @@ array( ) stmts: array( ) + comments: array( + 0: // alternative syntax + ) ) ) \ No newline at end of file diff --git a/test/code/parser/stmt/loop/foreach.test b/test/code/parser/stmt/loop/foreach.test index d1c729576e..166b5fdab1 100644 --- a/test/code/parser/stmt/loop/foreach.test +++ b/test/code/parser/stmt/loop/foreach.test @@ -29,6 +29,9 @@ array( ) stmts: array( ) + comments: array( + 0: // foreach on variable + ) ) 1: Stmt_Foreach( expr: Expr_Variable( @@ -123,6 +126,9 @@ array( ) stmts: array( ) + comments: array( + 0: // foreach on expression + ) ) 7: Stmt_Foreach( expr: Expr_Variable( @@ -135,5 +141,8 @@ array( ) stmts: array( ) + comments: array( + 0: // alternative syntax + ) ) ) \ No newline at end of file diff --git a/test/code/parser/stmt/namespace/alias.test b/test/code/parser/stmt/namespace/alias.test index b396a9b197..1a1b623de7 100644 --- a/test/code/parser/stmt/namespace/alias.test +++ b/test/code/parser/stmt/namespace/alias.test @@ -84,6 +84,9 @@ array( alias: A ) ) + comments: array( + 0: // evil alias notation - Do Not Use! + ) ) 4: Stmt_Use( type: 1 @@ -113,6 +116,9 @@ array( alias: bar ) ) + comments: array( + 0: // function and constant aliases + ) ) 6: Stmt_Use( type: 2 @@ -159,4 +165,4 @@ array( ) ) ) -) +) \ No newline at end of file diff --git a/test/code/parser/stmt/switch.test b/test/code/parser/stmt/switch.test index 8799a7a98e..4e094013dd 100644 --- a/test/code/parser/stmt/switch.test +++ b/test/code/parser/stmt/switch.test @@ -49,6 +49,9 @@ array( ) cases: array( ) + comments: array( + 0: // alternative syntax + ) ) 2: Stmt_Switch( cond: Expr_Variable( @@ -56,6 +59,9 @@ array( ) cases: array( ) + comments: array( + 0: // leading semicolon + ) ) 3: Stmt_Switch( cond: Expr_Variable( diff --git a/test/code/parser/stmt/tryCatch.test b/test/code/parser/stmt/tryCatch.test index da67fe38fc..5945414bde 100644 --- a/test/code/parser/stmt/tryCatch.test +++ b/test/code/parser/stmt/tryCatch.test @@ -102,6 +102,9 @@ array( ) ) finallyStmts: null + comments: array( + 0: // no finally + ) ) 2: Stmt_TryCatch( stmts: array( @@ -110,5 +113,8 @@ array( ) finallyStmts: array( ) + comments: array( + 0: // no catch + ) ) ) \ No newline at end of file