-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathvar_export.php
More file actions
123 lines (111 loc) · 4.35 KB
/
Copy pathvar_export.php
File metadata and controls
123 lines (111 loc) · 4.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php declare(strict_types=1);
/**
* The MIT License (MIT)
*
* Copyright (c) 2017 Tyson Andre
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
if (file_exists(__DIR__ . "/../../../vendor/autoload.php")) {
require __DIR__ . "/../../../vendor/autoload.php";
} else {
require __DIR__ . "/vendor/autoload.php";
}
use Microsoft\PhpParser\{DiagnosticsProvider, Node, Token, Parser, PositionUtilities};
dump_main();
// Dumps a snippet provided on stdin.
function dump_main() {
error_reporting(E_ALL);
global $argv;
if (count($argv) !== 2) {
$help = <<<"EOB"
Usage: {$argv[0]} 'snippet'
E.g.
{$argv[0]} '2+2;'
{$argv[0]} '<?php function test() {}'
EOB;
echo $help;
exit(1);
}
$expr = $argv[1];
// Guess if this is a snippet or file contents
if (($expr[0] ?? '') !== '<') {
echo "Guessing\n";
$expr = '<' . '?php ' . $expr;
}
echo "Going to parse: " . var_dump($expr);
dump_expr($expr);
}
function dump_expr(string $expr) {
// Instantiate new parser instance
$parser = new Parser();
// Return and print an AST from string contents
$ast_node = $parser->parseSourceFile($expr);
foreach ($ast_node->getDescendantNodes() as $descendant) {
// echo "unsetting " . get_class($descendant) . $descendant->getStart() . "\n";
unset($descendant->parent);
}
$ast_node->parent = null;
var_export($ast_node);
// var_export($ast_node->statementList);
echo "\n";
}
class ASTDumper {
/** @var string */
private $file_contents;
/** @var bool */
private $include_offset;
/** @var bool */
private $include_token_kind;
/** @var string */
private $indent;
public function __construct(string $file_contents, bool $include_offset = false, bool $include_token_kind = false, string $indent = ' ') {
$this->file_contents = $file_contents;
$this->include_offset = $include_offset;
$this->include_token_kind = $include_token_kind;
$this->indent = $indent;
}
public function dumpClassName(Node $ast_node) : string {
$name = get_class($ast_node);
if (stripos($name, 'Microsoft\\PhpParser\\') === 0) {
$name = substr($name, 20);
}
return $name;
}
/**
* @param Node|Token $ast_node
* @param string $padding (to be echoed before the current node
* @return void
*/
public function dumpTree($ast_node, string $key = '', string $padding = '') {
if ($ast_node instanceof Node) {
$offset = $ast_node->getStart();
echo $padding . ($key !== '' ? $key . ': ' : '') . $this->dumpClassName($ast_node) . ($this->include_offset ? " (@" . $offset . ")" : "") . "\n";
foreach ($ast_node->getChildNodesAndTokens() as $name => $child) {
$this->dumpTree($child, $name, $padding . $this->indent);
}
} else if ($ast_node instanceof Token) {
echo $padding . ($key !== '' ? $key . ': ' : '') . "Token: " . $ast_node->getTokenKindNameFromValue($ast_node->kind) . ($this->include_token_kind ? '(' . $ast_node->kind . ')' : '') . ': ' . json_encode(substr($this->file_contents, $ast_node->fullStart, $ast_node->length)) . "\n";
} else {
echo "Unexpected type of $ast_node was seen\n";
var_export($ast_node);
exit(2);
}
}
}