Chris@13
|
1 JSON representation
|
Chris@13
|
2 ===================
|
Chris@13
|
3
|
Chris@13
|
4 Nodes (and comments) implement the `JsonSerializable` interface. As such, it is possible to JSON
|
Chris@13
|
5 encode the AST directly using `json_encode()`:
|
Chris@13
|
6
|
Chris@13
|
7 ```php
|
Chris@13
|
8 <?php
|
Chris@13
|
9
|
Chris@13
|
10 use PhpParser\ParserFactory;
|
Chris@13
|
11
|
Chris@13
|
12 $code = <<<'CODE'
|
Chris@13
|
13 <?php
|
Chris@13
|
14
|
Chris@13
|
15 /** @param string $msg */
|
Chris@13
|
16 function printLine($msg) {
|
Chris@13
|
17 echo $msg, "\n";
|
Chris@13
|
18 }
|
Chris@13
|
19 CODE;
|
Chris@13
|
20
|
Chris@13
|
21 $parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
|
Chris@13
|
22
|
Chris@13
|
23 try {
|
Chris@13
|
24 $stmts = $parser->parse($code);
|
Chris@13
|
25
|
Chris@13
|
26 echo json_encode($stmts, JSON_PRETTY_PRINT), "\n";
|
Chris@13
|
27 } catch (PhpParser\Error $e) {
|
Chris@13
|
28 echo 'Parse Error: ', $e->getMessage();
|
Chris@13
|
29 }
|
Chris@13
|
30 ```
|
Chris@13
|
31
|
Chris@13
|
32 This will result in the following output (which includes attributes):
|
Chris@13
|
33
|
Chris@13
|
34 ```json
|
Chris@13
|
35 [
|
Chris@13
|
36 {
|
Chris@13
|
37 "nodeType": "Stmt_Function",
|
Chris@13
|
38 "byRef": false,
|
Chris@13
|
39 "name": {
|
Chris@13
|
40 "nodeType": "Identifier",
|
Chris@13
|
41 "name": "printLine",
|
Chris@13
|
42 "attributes": {
|
Chris@13
|
43 "startLine": 4,
|
Chris@13
|
44 "endLine": 4
|
Chris@13
|
45 }
|
Chris@13
|
46 },
|
Chris@13
|
47 "params": [
|
Chris@13
|
48 {
|
Chris@13
|
49 "nodeType": "Param",
|
Chris@13
|
50 "type": null,
|
Chris@13
|
51 "byRef": false,
|
Chris@13
|
52 "variadic": false,
|
Chris@13
|
53 "var": {
|
Chris@13
|
54 "nodeType": "Expr_Variable",
|
Chris@13
|
55 "name": "msg",
|
Chris@13
|
56 "attributes": {
|
Chris@13
|
57 "startLine": 4,
|
Chris@13
|
58 "endLine": 4
|
Chris@13
|
59 }
|
Chris@13
|
60 },
|
Chris@13
|
61 "default": null,
|
Chris@13
|
62 "attributes": {
|
Chris@13
|
63 "startLine": 4,
|
Chris@13
|
64 "endLine": 4
|
Chris@13
|
65 }
|
Chris@13
|
66 }
|
Chris@13
|
67 ],
|
Chris@13
|
68 "returnType": null,
|
Chris@13
|
69 "stmts": [
|
Chris@13
|
70 {
|
Chris@13
|
71 "nodeType": "Stmt_Echo",
|
Chris@13
|
72 "exprs": [
|
Chris@13
|
73 {
|
Chris@13
|
74 "nodeType": "Expr_Variable",
|
Chris@13
|
75 "name": "msg",
|
Chris@13
|
76 "attributes": {
|
Chris@13
|
77 "startLine": 5,
|
Chris@13
|
78 "endLine": 5
|
Chris@13
|
79 }
|
Chris@13
|
80 },
|
Chris@13
|
81 {
|
Chris@13
|
82 "nodeType": "Scalar_String",
|
Chris@13
|
83 "value": "\n",
|
Chris@13
|
84 "attributes": {
|
Chris@13
|
85 "startLine": 5,
|
Chris@13
|
86 "endLine": 5,
|
Chris@13
|
87 "kind": 2
|
Chris@13
|
88 }
|
Chris@13
|
89 }
|
Chris@13
|
90 ],
|
Chris@13
|
91 "attributes": {
|
Chris@13
|
92 "startLine": 5,
|
Chris@13
|
93 "endLine": 5
|
Chris@13
|
94 }
|
Chris@13
|
95 }
|
Chris@13
|
96 ],
|
Chris@13
|
97 "attributes": {
|
Chris@13
|
98 "startLine": 4,
|
Chris@13
|
99 "comments": [
|
Chris@13
|
100 {
|
Chris@13
|
101 "nodeType": "Comment_Doc",
|
Chris@13
|
102 "text": "\/** @param string $msg *\/",
|
Chris@13
|
103 "line": 3,
|
Chris@13
|
104 "filePos": 9,
|
Chris@13
|
105 "tokenPos": 2
|
Chris@13
|
106 }
|
Chris@13
|
107 ],
|
Chris@13
|
108 "endLine": 6
|
Chris@13
|
109 }
|
Chris@13
|
110 }
|
Chris@13
|
111 ]
|
Chris@13
|
112 ```
|
Chris@13
|
113
|
Chris@13
|
114 The JSON representation may be converted back into an AST using the `JsonDecoder`:
|
Chris@13
|
115
|
Chris@13
|
116 ```php
|
Chris@13
|
117 <?php
|
Chris@13
|
118
|
Chris@17
|
119 $jsonDecoder = new PhpParser\JsonDecoder();
|
Chris@17
|
120 $ast = $jsonDecoder->decode($json);
|
Chris@13
|
121 ```
|
Chris@13
|
122
|
Chris@13
|
123 Note that not all ASTs can be represented using JSON. In particular:
|
Chris@13
|
124
|
Chris@13
|
125 * JSON only supports UTF-8 strings.
|
Chris@13
|
126 * JSON does not support non-finite floating-point numbers. This can occur if the original source
|
Chris@13
|
127 code contains non-representable floating-pointing literals such as `1e1000`.
|
Chris@13
|
128
|
Chris@13
|
129 If the node tree is not representable in JSON, the initial `json_encode()` call will fail.
|
Chris@13
|
130
|
Chris@17
|
131 From the command line, a JSON dump can be obtained using `vendor/bin/php-parse -j file.php`.
|