2011-11-12 19:28:53 +01:00
|
|
|
Other node tree representations
|
|
|
|
===============================
|
|
|
|
|
2014-09-12 00:20:22 +02:00
|
|
|
It is possible to convert the AST into several textual representations, which serve different uses.
|
2011-11-12 19:28:53 +01:00
|
|
|
|
|
|
|
Simple serialization
|
|
|
|
--------------------
|
|
|
|
|
|
|
|
It is possible to serialize the node tree using `serialize()` and also unserialize it using
|
|
|
|
`unserialize()`. The output is not human readable and not easily processable from anything
|
|
|
|
but PHP, but it is compact and generates fast. The main application thus is in caching.
|
|
|
|
|
|
|
|
Human readable dumping
|
|
|
|
----------------------
|
|
|
|
|
2014-09-12 00:20:22 +02:00
|
|
|
Furthermore it is possible to dump nodes into a human readable format using the `dump` method of
|
2014-02-06 20:50:01 +01:00
|
|
|
`PhpParser\NodeDumper`. This can be used for debugging.
|
2011-11-12 19:28:53 +01:00
|
|
|
|
|
|
|
```php
|
|
|
|
<?php
|
|
|
|
$code = <<<'CODE'
|
|
|
|
<?php
|
|
|
|
|
2014-09-12 00:20:22 +02:00
|
|
|
function printLine($msg) {
|
|
|
|
echo $msg, "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
printLine('Hello World!!!');
|
2011-11-12 19:28:53 +01:00
|
|
|
CODE;
|
|
|
|
|
2014-09-12 00:20:22 +02:00
|
|
|
$parser = new PhpParser\Parser(new PhpParser\Lexer);
|
2014-02-06 20:50:01 +01:00
|
|
|
$nodeDumper = new PhpParser\NodeDumper;
|
2011-11-12 19:28:53 +01:00
|
|
|
|
|
|
|
try {
|
2012-05-11 16:44:13 +02:00
|
|
|
$stmts = $parser->parse($code);
|
2011-11-12 19:28:53 +01:00
|
|
|
|
2014-09-12 00:20:22 +02:00
|
|
|
echo $nodeDumper->dump($stmts), "\n";
|
2014-02-06 20:50:01 +01:00
|
|
|
} catch (PhpParser\Error $e) {
|
2011-11-12 19:28:53 +01:00
|
|
|
echo 'Parse Error: ', $e->getMessage();
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2014-09-12 00:20:22 +02:00
|
|
|
The above script will have an output looking roughly like this:
|
2011-11-12 19:28:53 +01:00
|
|
|
|
|
|
|
```
|
|
|
|
array(
|
|
|
|
0: Stmt_Function(
|
|
|
|
byRef: false
|
|
|
|
params: array(
|
|
|
|
0: Param(
|
|
|
|
name: msg
|
|
|
|
default: null
|
|
|
|
type: null
|
|
|
|
byRef: false
|
|
|
|
)
|
|
|
|
)
|
|
|
|
stmts: array(
|
|
|
|
0: Stmt_Echo(
|
|
|
|
exprs: array(
|
|
|
|
0: Expr_Variable(
|
|
|
|
name: msg
|
|
|
|
)
|
|
|
|
1: Scalar_String(
|
|
|
|
value:
|
|
|
|
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
name: printLine
|
|
|
|
)
|
|
|
|
1: Expr_FuncCall(
|
|
|
|
name: Name(
|
|
|
|
parts: array(
|
|
|
|
0: printLine
|
|
|
|
)
|
|
|
|
)
|
|
|
|
args: array(
|
|
|
|
0: Arg(
|
|
|
|
value: Scalar_String(
|
2014-09-12 00:20:22 +02:00
|
|
|
value: Hello World!!!
|
2011-11-12 19:28:53 +01:00
|
|
|
)
|
|
|
|
byRef: false
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
```
|
|
|
|
|
|
|
|
Serialization to XML
|
|
|
|
--------------------
|
|
|
|
|
2014-02-06 20:50:01 +01:00
|
|
|
It is also possible to serialize the node tree to XML using `PhpParser\Serializer\XML->serialize()`
|
|
|
|
and to unserialize it using `PhpParser\Unserializer\XML->unserialize()`. This is useful for
|
2011-11-12 19:28:53 +01:00
|
|
|
interfacing with other languages and applications or for doing transformation using XSLT.
|
|
|
|
|
|
|
|
```php
|
|
|
|
<?php
|
|
|
|
$code = <<<'CODE'
|
|
|
|
<?php
|
|
|
|
|
2014-09-12 00:20:22 +02:00
|
|
|
function printLine($msg) {
|
|
|
|
echo $msg, "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
printLine('Hello World!!!');
|
2011-11-12 19:28:53 +01:00
|
|
|
CODE;
|
|
|
|
|
2014-09-12 00:20:22 +02:00
|
|
|
$parser = new PhpParser\Parser(new PhpParser\Lexer);
|
2014-02-06 20:50:01 +01:00
|
|
|
$serializer = new PhpParser\Serializer\XML;
|
2011-11-12 19:28:53 +01:00
|
|
|
|
|
|
|
try {
|
2012-05-11 16:44:13 +02:00
|
|
|
$stmts = $parser->parse($code);
|
2011-11-12 19:28:53 +01:00
|
|
|
|
2014-09-12 00:20:22 +02:00
|
|
|
echo $serializer->serialize($stmts);
|
2014-02-06 20:50:01 +01:00
|
|
|
} catch (PhpParser\Error $e) {
|
2011-11-12 19:28:53 +01:00
|
|
|
echo 'Parse Error: ', $e->getMessage();
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
Produces:
|
|
|
|
|
2011-11-12 19:30:40 +01:00
|
|
|
```xml
|
2011-11-12 19:28:53 +01:00
|
|
|
<?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
<AST xmlns:node="http://nikic.github.com/PHPParser/XML/node" xmlns:subNode="http://nikic.github.com/PHPParser/XML/subNode" xmlns:scalar="http://nikic.github.com/PHPParser/XML/scalar">
|
|
|
|
<scalar:array>
|
|
|
|
<node:Stmt_Function line="2">
|
|
|
|
<subNode:byRef>
|
|
|
|
<scalar:false/>
|
|
|
|
</subNode:byRef>
|
|
|
|
<subNode:params>
|
|
|
|
<scalar:array>
|
|
|
|
<node:Param line="2">
|
|
|
|
<subNode:name>
|
|
|
|
<scalar:string>msg</scalar:string>
|
|
|
|
</subNode:name>
|
|
|
|
<subNode:default>
|
|
|
|
<scalar:null/>
|
|
|
|
</subNode:default>
|
|
|
|
<subNode:type>
|
|
|
|
<scalar:null/>
|
|
|
|
</subNode:type>
|
|
|
|
<subNode:byRef>
|
|
|
|
<scalar:false/>
|
|
|
|
</subNode:byRef>
|
|
|
|
</node:Param>
|
|
|
|
</scalar:array>
|
|
|
|
</subNode:params>
|
|
|
|
<subNode:stmts>
|
|
|
|
<scalar:array>
|
|
|
|
<node:Stmt_Echo line="3">
|
|
|
|
<subNode:exprs>
|
|
|
|
<scalar:array>
|
|
|
|
<node:Expr_Variable line="3">
|
|
|
|
<subNode:name>
|
|
|
|
<scalar:string>msg</scalar:string>
|
|
|
|
</subNode:name>
|
|
|
|
</node:Expr_Variable>
|
|
|
|
<node:Scalar_String line="3">
|
|
|
|
<subNode:value>
|
|
|
|
<scalar:string>
|
|
|
|
</scalar:string>
|
|
|
|
</subNode:value>
|
|
|
|
</node:Scalar_String>
|
|
|
|
</scalar:array>
|
|
|
|
</subNode:exprs>
|
|
|
|
</node:Stmt_Echo>
|
|
|
|
</scalar:array>
|
|
|
|
</subNode:stmts>
|
|
|
|
<subNode:name>
|
|
|
|
<scalar:string>printLine</scalar:string>
|
|
|
|
</subNode:name>
|
|
|
|
</node:Stmt_Function>
|
|
|
|
<node:Expr_FuncCall line="6">
|
|
|
|
<subNode:name>
|
|
|
|
<node:Name line="6">
|
|
|
|
<subNode:parts>
|
|
|
|
<scalar:array>
|
|
|
|
<scalar:string>printLine</scalar:string>
|
|
|
|
</scalar:array>
|
|
|
|
</subNode:parts>
|
|
|
|
</node:Name>
|
|
|
|
</subNode:name>
|
|
|
|
<subNode:args>
|
|
|
|
<scalar:array>
|
|
|
|
<node:Arg line="6">
|
|
|
|
<subNode:value>
|
|
|
|
<node:Scalar_String line="6">
|
|
|
|
<subNode:value>
|
2014-09-12 00:20:22 +02:00
|
|
|
<scalar:string>Hello World!!!</scalar:string>
|
2011-11-12 19:28:53 +01:00
|
|
|
</subNode:value>
|
|
|
|
</node:Scalar_String>
|
|
|
|
</subNode:value>
|
|
|
|
<subNode:byRef>
|
|
|
|
<scalar:false/>
|
|
|
|
</subNode:byRef>
|
|
|
|
</node:Arg>
|
|
|
|
</scalar:array>
|
|
|
|
</subNode:args>
|
|
|
|
</node:Expr_FuncCall>
|
|
|
|
</scalar:array>
|
|
|
|
</AST>
|
|
|
|
```
|