annotate vendor/nikic/php-parser/doc/component/JSON_representation.markdown @ 0:c75dbcec494b

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