Chris@13: JSON representation Chris@13: =================== Chris@13: Chris@13: Nodes (and comments) implement the `JsonSerializable` interface. As such, it is possible to JSON Chris@13: encode the AST directly using `json_encode()`: Chris@13: Chris@13: ```php Chris@13: create(ParserFactory::PREFER_PHP7); Chris@13: Chris@13: try { Chris@13: $stmts = $parser->parse($code); Chris@13: Chris@13: echo json_encode($stmts, JSON_PRETTY_PRINT), "\n"; Chris@13: } catch (PhpParser\Error $e) { Chris@13: echo 'Parse Error: ', $e->getMessage(); Chris@13: } Chris@13: ``` Chris@13: Chris@13: This will result in the following output (which includes attributes): Chris@13: Chris@13: ```json Chris@13: [ Chris@13: { Chris@13: "nodeType": "Stmt_Function", Chris@13: "byRef": false, Chris@13: "name": { Chris@13: "nodeType": "Identifier", Chris@13: "name": "printLine", Chris@13: "attributes": { Chris@13: "startLine": 4, Chris@13: "endLine": 4 Chris@13: } Chris@13: }, Chris@13: "params": [ Chris@13: { Chris@13: "nodeType": "Param", Chris@13: "type": null, Chris@13: "byRef": false, Chris@13: "variadic": false, Chris@13: "var": { Chris@13: "nodeType": "Expr_Variable", Chris@13: "name": "msg", Chris@13: "attributes": { Chris@13: "startLine": 4, Chris@13: "endLine": 4 Chris@13: } Chris@13: }, Chris@13: "default": null, Chris@13: "attributes": { Chris@13: "startLine": 4, Chris@13: "endLine": 4 Chris@13: } Chris@13: } Chris@13: ], Chris@13: "returnType": null, Chris@13: "stmts": [ Chris@13: { Chris@13: "nodeType": "Stmt_Echo", Chris@13: "exprs": [ Chris@13: { Chris@13: "nodeType": "Expr_Variable", Chris@13: "name": "msg", Chris@13: "attributes": { Chris@13: "startLine": 5, Chris@13: "endLine": 5 Chris@13: } Chris@13: }, Chris@13: { Chris@13: "nodeType": "Scalar_String", Chris@13: "value": "\n", Chris@13: "attributes": { Chris@13: "startLine": 5, Chris@13: "endLine": 5, Chris@13: "kind": 2 Chris@13: } Chris@13: } Chris@13: ], Chris@13: "attributes": { Chris@13: "startLine": 5, Chris@13: "endLine": 5 Chris@13: } Chris@13: } Chris@13: ], Chris@13: "attributes": { Chris@13: "startLine": 4, Chris@13: "comments": [ Chris@13: { Chris@13: "nodeType": "Comment_Doc", Chris@13: "text": "\/** @param string $msg *\/", Chris@13: "line": 3, Chris@13: "filePos": 9, Chris@13: "tokenPos": 2 Chris@13: } Chris@13: ], Chris@13: "endLine": 6 Chris@13: } Chris@13: } Chris@13: ] Chris@13: ``` Chris@13: Chris@13: The JSON representation may be converted back into an AST using the `JsonDecoder`: Chris@13: Chris@13: ```php Chris@13: decode($json); Chris@13: ``` Chris@13: Chris@13: Note that not all ASTs can be represented using JSON. In particular: Chris@13: Chris@13: * JSON only supports UTF-8 strings. Chris@13: * JSON does not support non-finite floating-point numbers. This can occur if the original source Chris@13: code contains non-representable floating-pointing literals such as `1e1000`. Chris@13: Chris@13: If the node tree is not representable in JSON, the initial `json_encode()` call will fail. Chris@13: Chris@17: From the command line, a JSON dump can be obtained using `vendor/bin/php-parse -j file.php`.