Mercurial > hg > isophonics-drupal-site
comparison vendor/nikic/php-parser/doc/3_Other_node_tree_representations.markdown @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 Other node tree representations | |
2 =============================== | |
3 | |
4 It is possible to convert the AST into several textual representations, which serve different uses. | |
5 | |
6 Simple serialization | |
7 -------------------- | |
8 | |
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. | |
12 | |
13 Human readable dumping | |
14 ---------------------- | |
15 | |
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. | |
18 | |
19 ```php | |
20 $code = <<<'CODE' | |
21 <?php | |
22 | |
23 function printLine($msg) { | |
24 echo $msg, "\n"; | |
25 } | |
26 | |
27 printLine('Hello World!!!'); | |
28 CODE; | |
29 | |
30 $parser = (new PhpParser\ParserFactory)->create(PhpParser\ParserFactory::PREFER_PHP7); | |
31 $nodeDumper = new PhpParser\NodeDumper; | |
32 | |
33 try { | |
34 $stmts = $parser->parse($code); | |
35 | |
36 echo $nodeDumper->dump($stmts), "\n"; | |
37 } catch (PhpParser\Error $e) { | |
38 echo 'Parse Error: ', $e->getMessage(); | |
39 } | |
40 ``` | |
41 | |
42 The above script will have an output looking roughly like this: | |
43 | |
44 ``` | |
45 array( | |
46 0: Stmt_Function( | |
47 byRef: false | |
48 params: array( | |
49 0: Param( | |
50 name: msg | |
51 default: null | |
52 type: null | |
53 byRef: false | |
54 ) | |
55 ) | |
56 stmts: array( | |
57 0: Stmt_Echo( | |
58 exprs: array( | |
59 0: Expr_Variable( | |
60 name: msg | |
61 ) | |
62 1: Scalar_String( | |
63 value: | |
64 | |
65 ) | |
66 ) | |
67 ) | |
68 ) | |
69 name: printLine | |
70 ) | |
71 1: Expr_FuncCall( | |
72 name: Name( | |
73 parts: array( | |
74 0: printLine | |
75 ) | |
76 ) | |
77 args: array( | |
78 0: Arg( | |
79 value: Scalar_String( | |
80 value: Hello World!!! | |
81 ) | |
82 byRef: false | |
83 ) | |
84 ) | |
85 ) | |
86 ) | |
87 ``` | |
88 | |
89 JSON encoding | |
90 ------------- | |
91 | |
92 Nodes (and comments) implement the `JsonSerializable` interface. As such, it is possible to JSON | |
93 encode the AST directly using `json_encode()`: | |
94 | |
95 ```php | |
96 $code = <<<'CODE' | |
97 <?php | |
98 | |
99 function printLine($msg) { | |
100 echo $msg, "\n"; | |
101 } | |
102 | |
103 printLine('Hello World!!!'); | |
104 CODE; | |
105 | |
106 $parser = (new PhpParser\ParserFactory)->create(PhpParser\ParserFactory::PREFER_PHP7); | |
107 $nodeDumper = new PhpParser\NodeDumper; | |
108 | |
109 try { | |
110 $stmts = $parser->parse($code); | |
111 | |
112 echo json_encode($stmts, JSON_PRETTY_PRINT), "\n"; | |
113 } catch (PhpParser\Error $e) { | |
114 echo 'Parse Error: ', $e->getMessage(); | |
115 } | |
116 ``` | |
117 | |
118 This will result in the following output (which includes attributes): | |
119 | |
120 ```json | |
121 [ | |
122 { | |
123 "nodeType": "Stmt_Function", | |
124 "byRef": false, | |
125 "name": "printLine", | |
126 "params": [ | |
127 { | |
128 "nodeType": "Param", | |
129 "type": null, | |
130 "byRef": false, | |
131 "variadic": false, | |
132 "name": "msg", | |
133 "default": null, | |
134 "attributes": { | |
135 "startLine": 3, | |
136 "endLine": 3 | |
137 } | |
138 } | |
139 ], | |
140 "returnType": null, | |
141 "stmts": [ | |
142 { | |
143 "nodeType": "Stmt_Echo", | |
144 "exprs": [ | |
145 { | |
146 "nodeType": "Expr_Variable", | |
147 "name": "msg", | |
148 "attributes": { | |
149 "startLine": 4, | |
150 "endLine": 4 | |
151 } | |
152 }, | |
153 { | |
154 "nodeType": "Scalar_String", | |
155 "value": "\n", | |
156 "attributes": { | |
157 "startLine": 4, | |
158 "endLine": 4, | |
159 "kind": 2 | |
160 } | |
161 } | |
162 ], | |
163 "attributes": { | |
164 "startLine": 4, | |
165 "endLine": 4 | |
166 } | |
167 } | |
168 ], | |
169 "attributes": { | |
170 "startLine": 3, | |
171 "endLine": 5 | |
172 } | |
173 }, | |
174 { | |
175 "nodeType": "Expr_FuncCall", | |
176 "name": { | |
177 "nodeType": "Name", | |
178 "parts": [ | |
179 "printLine" | |
180 ], | |
181 "attributes": { | |
182 "startLine": 7, | |
183 "endLine": 7 | |
184 } | |
185 }, | |
186 "args": [ | |
187 { | |
188 "nodeType": "Arg", | |
189 "value": { | |
190 "nodeType": "Scalar_String", | |
191 "value": "Hello World!!!", | |
192 "attributes": { | |
193 "startLine": 7, | |
194 "endLine": 7, | |
195 "kind": 1 | |
196 } | |
197 }, | |
198 "byRef": false, | |
199 "unpack": false, | |
200 "attributes": { | |
201 "startLine": 7, | |
202 "endLine": 7 | |
203 } | |
204 } | |
205 ], | |
206 "attributes": { | |
207 "startLine": 7, | |
208 "endLine": 7 | |
209 } | |
210 } | |
211 ] | |
212 ``` | |
213 | |
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. | |
216 | |
217 Serialization to XML | |
218 -------------------- | |
219 | |
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. | |
223 | |
224 ```php | |
225 <?php | |
226 $code = <<<'CODE' | |
227 <?php | |
228 | |
229 function printLine($msg) { | |
230 echo $msg, "\n"; | |
231 } | |
232 | |
233 printLine('Hello World!!!'); | |
234 CODE; | |
235 | |
236 $parser = (new PhpParser\ParserFactory)->create(PhpParser\ParserFactory::PREFER_PHP7); | |
237 $serializer = new PhpParser\Serializer\XML; | |
238 | |
239 try { | |
240 $stmts = $parser->parse($code); | |
241 | |
242 echo $serializer->serialize($stmts); | |
243 } catch (PhpParser\Error $e) { | |
244 echo 'Parse Error: ', $e->getMessage(); | |
245 } | |
246 ``` | |
247 | |
248 Produces: | |
249 | |
250 ```xml | |
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"> | |
253 <scalar:array> | |
254 <node:Stmt_Function line="2"> | |
255 <subNode:byRef> | |
256 <scalar:false/> | |
257 </subNode:byRef> | |
258 <subNode:params> | |
259 <scalar:array> | |
260 <node:Param line="2"> | |
261 <subNode:name> | |
262 <scalar:string>msg</scalar:string> | |
263 </subNode:name> | |
264 <subNode:default> | |
265 <scalar:null/> | |
266 </subNode:default> | |
267 <subNode:type> | |
268 <scalar:null/> | |
269 </subNode:type> | |
270 <subNode:byRef> | |
271 <scalar:false/> | |
272 </subNode:byRef> | |
273 </node:Param> | |
274 </scalar:array> | |
275 </subNode:params> | |
276 <subNode:stmts> | |
277 <scalar:array> | |
278 <node:Stmt_Echo line="3"> | |
279 <subNode:exprs> | |
280 <scalar:array> | |
281 <node:Expr_Variable line="3"> | |
282 <subNode:name> | |
283 <scalar:string>msg</scalar:string> | |
284 </subNode:name> | |
285 </node:Expr_Variable> | |
286 <node:Scalar_String line="3"> | |
287 <subNode:value> | |
288 <scalar:string> | |
289 </scalar:string> | |
290 </subNode:value> | |
291 </node:Scalar_String> | |
292 </scalar:array> | |
293 </subNode:exprs> | |
294 </node:Stmt_Echo> | |
295 </scalar:array> | |
296 </subNode:stmts> | |
297 <subNode:name> | |
298 <scalar:string>printLine</scalar:string> | |
299 </subNode:name> | |
300 </node:Stmt_Function> | |
301 <node:Expr_FuncCall line="6"> | |
302 <subNode:name> | |
303 <node:Name line="6"> | |
304 <subNode:parts> | |
305 <scalar:array> | |
306 <scalar:string>printLine</scalar:string> | |
307 </scalar:array> | |
308 </subNode:parts> | |
309 </node:Name> | |
310 </subNode:name> | |
311 <subNode:args> | |
312 <scalar:array> | |
313 <node:Arg line="6"> | |
314 <subNode:value> | |
315 <node:Scalar_String line="6"> | |
316 <subNode:value> | |
317 <scalar:string>Hello World!!!</scalar:string> | |
318 </subNode:value> | |
319 </node:Scalar_String> | |
320 </subNode:value> | |
321 <subNode:byRef> | |
322 <scalar:false/> | |
323 </subNode:byRef> | |
324 </node:Arg> | |
325 </scalar:array> | |
326 </subNode:args> | |
327 </node:Expr_FuncCall> | |
328 </scalar:array> | |
329 </AST> | |
330 ``` |