X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=vendor%2Fnikic%2Fphp-parser%2Fdoc%2F3_Other_node_tree_representations.markdown;fp=vendor%2Fnikic%2Fphp-parser%2Fdoc%2F3_Other_node_tree_representations.markdown;h=0830f399c4b2b0b2cf0c93d51416448a897b4de1;hp=0000000000000000000000000000000000000000;hb=9917807b03b64faf00f6a1f29dcb6eafc454efa5;hpb=aea91e65e895364e460983b890e295aa5d5540a5 diff --git a/vendor/nikic/php-parser/doc/3_Other_node_tree_representations.markdown b/vendor/nikic/php-parser/doc/3_Other_node_tree_representations.markdown new file mode 100644 index 000000000..0830f399c --- /dev/null +++ b/vendor/nikic/php-parser/doc/3_Other_node_tree_representations.markdown @@ -0,0 +1,330 @@ +Other node tree representations +=============================== + +It is possible to convert the AST into several textual representations, which serve different uses. + +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 quickly. The main application thus is in caching. + +Human readable dumping +---------------------- + +Furthermore it is possible to dump nodes into a human readable format using the `dump` method of +`PhpParser\NodeDumper`. This can be used for debugging. + +```php +$code = <<<'CODE' +create(PhpParser\ParserFactory::PREFER_PHP7); +$nodeDumper = new PhpParser\NodeDumper; + +try { + $stmts = $parser->parse($code); + + echo $nodeDumper->dump($stmts), "\n"; +} catch (PhpParser\Error $e) { + echo 'Parse Error: ', $e->getMessage(); +} +``` + +The above script will have an output looking roughly like this: + +``` +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( + value: Hello World!!! + ) + byRef: false + ) + ) + ) +) +``` + +JSON encoding +------------- + +Nodes (and comments) implement the `JsonSerializable` interface. As such, it is possible to JSON +encode the AST directly using `json_encode()`: + +```php +$code = <<<'CODE' +create(PhpParser\ParserFactory::PREFER_PHP7); +$nodeDumper = new PhpParser\NodeDumper; + +try { + $stmts = $parser->parse($code); + + echo json_encode($stmts, JSON_PRETTY_PRINT), "\n"; +} catch (PhpParser\Error $e) { + echo 'Parse Error: ', $e->getMessage(); +} +``` + +This will result in the following output (which includes attributes): + +```json +[ + { + "nodeType": "Stmt_Function", + "byRef": false, + "name": "printLine", + "params": [ + { + "nodeType": "Param", + "type": null, + "byRef": false, + "variadic": false, + "name": "msg", + "default": null, + "attributes": { + "startLine": 3, + "endLine": 3 + } + } + ], + "returnType": null, + "stmts": [ + { + "nodeType": "Stmt_Echo", + "exprs": [ + { + "nodeType": "Expr_Variable", + "name": "msg", + "attributes": { + "startLine": 4, + "endLine": 4 + } + }, + { + "nodeType": "Scalar_String", + "value": "\n", + "attributes": { + "startLine": 4, + "endLine": 4, + "kind": 2 + } + } + ], + "attributes": { + "startLine": 4, + "endLine": 4 + } + } + ], + "attributes": { + "startLine": 3, + "endLine": 5 + } + }, + { + "nodeType": "Expr_FuncCall", + "name": { + "nodeType": "Name", + "parts": [ + "printLine" + ], + "attributes": { + "startLine": 7, + "endLine": 7 + } + }, + "args": [ + { + "nodeType": "Arg", + "value": { + "nodeType": "Scalar_String", + "value": "Hello World!!!", + "attributes": { + "startLine": 7, + "endLine": 7, + "kind": 1 + } + }, + "byRef": false, + "unpack": false, + "attributes": { + "startLine": 7, + "endLine": 7 + } + } + ], + "attributes": { + "startLine": 7, + "endLine": 7 + } + } +] +``` + +There is currently no mechanism to convert JSON back into a node tree. Furthermore, not all ASTs +can be JSON encoded. In particular, JSON only supports UTF-8 strings. + +Serialization to XML +-------------------- + +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 +interfacing with other languages and applications or for doing transformation using XSLT. + +```php +create(PhpParser\ParserFactory::PREFER_PHP7); +$serializer = new PhpParser\Serializer\XML; + +try { + $stmts = $parser->parse($code); + + echo $serializer->serialize($stmts); +} catch (PhpParser\Error $e) { + echo 'Parse Error: ', $e->getMessage(); +} +``` + +Produces: + +```xml + + + + + + + + + + + + msg + + + + + + + + + + + + + + + + + + + + + msg + + + + + + + + + + + + + + + printLine + + + + + + + + printLine + + + + + + + + + + + Hello World!!! + + + + + + + + + + + + +``` \ No newline at end of file