Added the Search API Synonym module to deal specifically with licence and license...
[yaffs-website] / vendor / nikic / php-parser / bin / php-parse
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(['usedAttributes' => [
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
42 $traverser = new PhpParser\NodeTraverser();
43 $traverser->addVisitor(new PhpParser\NodeVisitor\NameResolver);
44
45 foreach ($files as $file) {
46     if (strpos($file, '<?php') === 0) {
47         $code = $file;
48         echo "====> Code $code\n";
49     } else {
50         if (!file_exists($file)) {
51             die("File $file does not exist.\n");
52         }
53
54         $code = file_get_contents($file);
55         echo "====> File $file:\n";
56     }
57
58     if ($attributes['with-recovery']) {
59         $errorHandler = new PhpParser\ErrorHandler\Collecting;
60         $stmts = $parser->parse($code, $errorHandler);
61         foreach ($errorHandler->getErrors() as $error) {
62             $message = formatErrorMessage($error, $code, $attributes['with-column-info']);
63             echo $message . "\n";
64         }
65         if (null === $stmts) {
66             continue;
67         }
68     } else {
69         try {
70             $stmts = $parser->parse($code);
71         } catch (PhpParser\Error $error) {
72             $message = formatErrorMessage($error, $code, $attributes['with-column-info']);
73             die($message . "\n");
74         }
75     }
76
77     foreach ($operations as $operation) {
78         if ('dump' === $operation) {
79             echo "==> Node dump:\n";
80             echo $dumper->dump($stmts, $code), "\n";
81         } elseif ('pretty-print' === $operation) {
82             echo "==> Pretty print:\n";
83             echo $prettyPrinter->prettyPrintFile($stmts), "\n";
84         } elseif ('json-dump' === $operation) {
85             echo "==> JSON dump:\n";
86             echo json_encode($stmts, JSON_PRETTY_PRINT), "\n";
87         } elseif ('var-dump' === $operation) {
88             echo "==> var_dump():\n";
89             var_dump($stmts);
90         } elseif ('resolve-names' === $operation) {
91             echo "==> Resolved names.\n";
92             $stmts = $traverser->traverse($stmts);
93         }
94     }
95 }
96
97 function formatErrorMessage(PhpParser\Error $e, $code, $withColumnInfo) {
98     if ($withColumnInfo && $e->hasColumnInfo()) {
99         return $e->getMessageWithColumnInfo($code);
100     } else {
101         return $e->getMessage();
102     }
103 }
104
105 function showHelp($error = '') {
106     if ($error) {
107         echo $error . "\n\n";
108     }
109     die(<<<OUTPUT
110 Usage: php-parse [operations] file1.php [file2.php ...]
111    or: php-parse [operations] "<?php code"
112 Turn PHP source code into an abstract syntax tree.
113
114 Operations is a list of the following options (--dump by default):
115
116     -d, --dump              Dump nodes using NodeDumper
117     -p, --pretty-print      Pretty print file using PrettyPrinter\Standard
118     -j, --json-dump         Print json_encode() result
119         --var-dump          var_dump() nodes (for exact structure)
120     -N, --resolve-names     Resolve names using NodeVisitor\NameResolver
121     -c, --with-column-info  Show column-numbers for errors (if available)
122     -P, --with-positions    Show positions in node dumps
123     -r, --with-recovery     Use parsing with error recovery
124     -h, --help              Display this page
125
126 Example:
127     php-parse -d -p -N -d file.php
128
129     Dumps nodes, pretty prints them, then resolves names and dumps them again.
130
131
132 OUTPUT
133     );
134 }
135
136 function parseArgs($args) {
137     $operations = [];
138     $files = [];
139     $attributes = [
140         'with-column-info' => false,
141         'with-positions' => false,
142         'with-recovery' => false,
143     ];
144
145     array_shift($args);
146     $parseOptions = true;
147     foreach ($args as $arg) {
148         if (!$parseOptions) {
149             $files[] = $arg;
150             continue;
151         }
152
153         switch ($arg) {
154             case '--dump':
155             case '-d':
156                 $operations[] = 'dump';
157                 break;
158             case '--pretty-print':
159             case '-p':
160                 $operations[] = 'pretty-print';
161                 break;
162             case '--json-dump':
163             case '-j':
164                 $operations[] = 'json-dump';
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 [$operations, $files, $attributes];
202 }