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