1 Other node tree representations
2 ===============================
4 It is possible to convert the AST into several textual representations, which serve different uses.
9 It is possible to serialize the node tree using `serialize()` and also unserialize it using
10 `unserialize()`. The output is not human readable and not easily processable from anything
11 but PHP, but it is compact and generates quickly. The main application thus is in caching.
13 Human readable dumping
14 ----------------------
16 Furthermore it is possible to dump nodes into a human readable format using the `dump` method of
17 `PhpParser\NodeDumper`. This can be used for debugging.
23 function printLine($msg) {
27 printLine('Hello World!!!');
30 $parser = (new PhpParser\ParserFactory)->create(PhpParser\ParserFactory::PREFER_PHP7);
31 $nodeDumper = new PhpParser\NodeDumper;
34 $stmts = $parser->parse($code);
36 echo $nodeDumper->dump($stmts), "\n";
37 } catch (PhpParser\Error $e) {
38 echo 'Parse Error: ', $e->getMessage();
42 The above script will have an output looking roughly like this:
92 Nodes (and comments) implement the `JsonSerializable` interface. As such, it is possible to JSON
93 encode the AST directly using `json_encode()`:
99 function printLine($msg) {
103 printLine('Hello World!!!');
106 $parser = (new PhpParser\ParserFactory)->create(PhpParser\ParserFactory::PREFER_PHP7);
107 $nodeDumper = new PhpParser\NodeDumper;
110 $stmts = $parser->parse($code);
112 echo json_encode($stmts, JSON_PRETTY_PRINT), "\n";
113 } catch (PhpParser\Error $e) {
114 echo 'Parse Error: ', $e->getMessage();
118 This will result in the following output (which includes attributes):
123 "nodeType": "Stmt_Function",
143 "nodeType": "Stmt_Echo",
146 "nodeType": "Expr_Variable",
154 "nodeType": "Scalar_String",
175 "nodeType": "Expr_FuncCall",
190 "nodeType": "Scalar_String",
191 "value": "Hello World!!!",
214 There is currently no mechanism to convert JSON back into a node tree. Furthermore, not all ASTs
215 can be JSON encoded. In particular, JSON only supports UTF-8 strings.
220 It is also possible to serialize the node tree to XML using `PhpParser\Serializer\XML->serialize()`
221 and to unserialize it using `PhpParser\Unserializer\XML->unserialize()`. This is useful for
222 interfacing with other languages and applications or for doing transformation using XSLT.
229 function printLine($msg) {
233 printLine('Hello World!!!');
236 $parser = (new PhpParser\ParserFactory)->create(PhpParser\ParserFactory::PREFER_PHP7);
237 $serializer = new PhpParser\Serializer\XML;
240 $stmts = $parser->parse($code);
242 echo $serializer->serialize($stmts);
243 } catch (PhpParser\Error $e) {
244 echo 'Parse Error: ', $e->getMessage();
251 <?xml version="1.0" encoding="UTF-8"?>
252 <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">
254 <node:Stmt_Function line="2">
260 <node:Param line="2">
262 <scalar:string>msg</scalar:string>
278 <node:Stmt_Echo line="3">
281 <node:Expr_Variable line="3">
283 <scalar:string>msg</scalar:string>
285 </node:Expr_Variable>
286 <node:Scalar_String line="3">
291 </node:Scalar_String>
298 <scalar:string>printLine</scalar:string>
300 </node:Stmt_Function>
301 <node:Expr_FuncCall line="6">
306 <scalar:string>printLine</scalar:string>
315 <node:Scalar_String line="6">
317 <scalar:string>Hello World!!!</scalar:string>
319 </node:Scalar_String>
327 </node:Expr_FuncCall>