Mercurial > hg > isophonics-drupal-site
comparison vendor/nikic/php-parser/bin/php-parse @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 5fb285c0d0e3 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 #!/usr/bin/env php | |
2 <?php | |
3 | |
4 foreach ([__DIR__ . '/../../../autoload.php', __DIR__ . '/../vendor/autoload.php'] as $file) { | |
5 if (file_exists($file)) { | |
6 require $file; | |
7 break; | |
8 } | |
9 } | |
10 | |
11 ini_set('xdebug.max_nesting_level', 3000); | |
12 | |
13 // Disable XDebug var_dump() output truncation | |
14 ini_set('xdebug.var_display_max_children', -1); | |
15 ini_set('xdebug.var_display_max_data', -1); | |
16 ini_set('xdebug.var_display_max_depth', -1); | |
17 | |
18 list($operations, $files, $attributes) = parseArgs($argv); | |
19 | |
20 /* Dump nodes by default */ | |
21 if (empty($operations)) { | |
22 $operations[] = 'dump'; | |
23 } | |
24 | |
25 if (empty($files)) { | |
26 showHelp("Must specify at least one file."); | |
27 } | |
28 | |
29 $lexer = new PhpParser\Lexer\Emulative(array('usedAttributes' => array( | |
30 'startLine', 'endLine', 'startFilePos', 'endFilePos', 'comments' | |
31 ))); | |
32 $parser = (new PhpParser\ParserFactory)->create( | |
33 PhpParser\ParserFactory::PREFER_PHP7, | |
34 $lexer | |
35 ); | |
36 $dumper = new PhpParser\NodeDumper([ | |
37 'dumpComments' => true, | |
38 'dumpPositions' => $attributes['with-positions'], | |
39 ]); | |
40 $prettyPrinter = new PhpParser\PrettyPrinter\Standard; | |
41 $serializer = new PhpParser\Serializer\XML; | |
42 | |
43 $traverser = new PhpParser\NodeTraverser(); | |
44 $traverser->addVisitor(new PhpParser\NodeVisitor\NameResolver); | |
45 | |
46 foreach ($files as $file) { | |
47 if (strpos($file, '<?php') === 0) { | |
48 $code = $file; | |
49 echo "====> Code $code\n"; | |
50 } else { | |
51 if (!file_exists($file)) { | |
52 die("File $file does not exist.\n"); | |
53 } | |
54 | |
55 $code = file_get_contents($file); | |
56 echo "====> File $file:\n"; | |
57 } | |
58 | |
59 if ($attributes['with-recovery']) { | |
60 $errorHandler = new PhpParser\ErrorHandler\Collecting; | |
61 $stmts = $parser->parse($code, $errorHandler); | |
62 foreach ($errorHandler->getErrors() as $error) { | |
63 $message = formatErrorMessage($error, $code, $attributes['with-column-info']); | |
64 echo $message . "\n"; | |
65 } | |
66 if (null === $stmts) { | |
67 continue; | |
68 } | |
69 } else { | |
70 try { | |
71 $stmts = $parser->parse($code); | |
72 } catch (PhpParser\Error $error) { | |
73 $message = formatErrorMessage($error, $code, $attributes['with-column-info']); | |
74 die($message . "\n"); | |
75 } | |
76 } | |
77 | |
78 foreach ($operations as $operation) { | |
79 if ('dump' === $operation) { | |
80 echo "==> Node dump:\n"; | |
81 echo $dumper->dump($stmts, $code), "\n"; | |
82 } elseif ('pretty-print' === $operation) { | |
83 echo "==> Pretty print:\n"; | |
84 echo $prettyPrinter->prettyPrintFile($stmts), "\n"; | |
85 } elseif ('serialize-xml' === $operation) { | |
86 echo "==> Serialized XML:\n"; | |
87 echo $serializer->serialize($stmts), "\n"; | |
88 } elseif ('var-dump' === $operation) { | |
89 echo "==> var_dump():\n"; | |
90 var_dump($stmts); | |
91 } elseif ('resolve-names' === $operation) { | |
92 echo "==> Resolved names.\n"; | |
93 $stmts = $traverser->traverse($stmts); | |
94 } | |
95 } | |
96 } | |
97 | |
98 function formatErrorMessage(PhpParser\Error $e, $code, $withColumnInfo) { | |
99 if ($withColumnInfo && $e->hasColumnInfo()) { | |
100 return $e->getMessageWithColumnInfo($code); | |
101 } else { | |
102 return $e->getMessage(); | |
103 } | |
104 } | |
105 | |
106 function showHelp($error = '') { | |
107 if ($error) { | |
108 echo $error . "\n\n"; | |
109 } | |
110 die(<<<OUTPUT | |
111 Usage: php-parse [operations] file1.php [file2.php ...] | |
112 or: php-parse [operations] "<?php code" | |
113 Turn PHP source code into an abstract syntax tree. | |
114 | |
115 Operations is a list of the following options (--dump by default): | |
116 | |
117 -d, --dump Dump nodes using NodeDumper | |
118 -p, --pretty-print Pretty print file using PrettyPrinter\Standard | |
119 --serialize-xml Serialize nodes using Serializer\XML | |
120 --var-dump var_dump() nodes (for exact structure) | |
121 -N, --resolve-names Resolve names using NodeVisitor\NameResolver | |
122 -c, --with-column-info Show column-numbers for errors (if available) | |
123 -P, --with-positions Show positions in node dumps | |
124 -r, --with-recovery Use parsing with error recovery | |
125 -h, --help Display this page | |
126 | |
127 Example: | |
128 php-parse -d -p -N -d file.php | |
129 | |
130 Dumps nodes, pretty prints them, then resolves names and dumps them again. | |
131 | |
132 | |
133 OUTPUT | |
134 ); | |
135 } | |
136 | |
137 function parseArgs($args) { | |
138 $operations = array(); | |
139 $files = array(); | |
140 $attributes = array( | |
141 'with-column-info' => false, | |
142 'with-positions' => false, | |
143 'with-recovery' => false, | |
144 ); | |
145 | |
146 array_shift($args); | |
147 $parseOptions = true; | |
148 foreach ($args as $arg) { | |
149 if (!$parseOptions) { | |
150 $files[] = $arg; | |
151 continue; | |
152 } | |
153 | |
154 switch ($arg) { | |
155 case '--dump': | |
156 case '-d': | |
157 $operations[] = 'dump'; | |
158 break; | |
159 case '--pretty-print': | |
160 case '-p': | |
161 $operations[] = 'pretty-print'; | |
162 break; | |
163 case '--serialize-xml': | |
164 $operations[] = 'serialize-xml'; | |
165 break; | |
166 case '--var-dump': | |
167 $operations[] = 'var-dump'; | |
168 break; | |
169 case '--resolve-names': | |
170 case '-N'; | |
171 $operations[] = 'resolve-names'; | |
172 break; | |
173 case '--with-column-info': | |
174 case '-c'; | |
175 $attributes['with-column-info'] = true; | |
176 break; | |
177 case '--with-positions': | |
178 case '-P': | |
179 $attributes['with-positions'] = true; | |
180 break; | |
181 case '--with-recovery': | |
182 case '-r': | |
183 $attributes['with-recovery'] = true; | |
184 break; | |
185 case '--help': | |
186 case '-h'; | |
187 showHelp(); | |
188 break; | |
189 case '--': | |
190 $parseOptions = false; | |
191 break; | |
192 default: | |
193 if ($arg[0] === '-') { | |
194 showHelp("Invalid operation $arg."); | |
195 } else { | |
196 $files[] = $arg; | |
197 } | |
198 } | |
199 } | |
200 | |
201 return array($operations, $files, $attributes); | |
202 } |