Skip to content

Commit e68b17c

Browse files
committed
Add --version flag to php-parse
1 parent f63081a commit e68b17c

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

bin/php-parse

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,10 @@ if (empty($files)) {
2626
showHelp("Must specify at least one file.");
2727
}
2828

29-
$lexer = new PhpParser\Lexer\Emulative(['usedAttributes' => [
29+
$lexerOptions = ['usedAttributes' => [
3030
'startLine', 'endLine', 'startFilePos', 'endFilePos', 'comments'
31-
]]);
32-
$parser = (new PhpParser\ParserFactory)->create(
33-
PhpParser\ParserFactory::PREFER_PHP7,
34-
$lexer
35-
);
31+
]];
32+
$parser = (new PhpParser\ParserFactory())->createForVersion($attributes['version'], $lexerOptions);
3633
$dumper = new PhpParser\NodeDumper([
3734
'dumpComments' => true,
3835
'dumpPositions' => $attributes['with-positions'],
@@ -108,7 +105,7 @@ function showHelp($error = '') {
108105
if ($error) {
109106
fwrite(STDERR, $error . "\n\n");
110107
}
111-
fwrite($error ? STDERR : STDOUT, <<<OUTPUT
108+
fwrite($error ? STDERR : STDOUT, <<<'OUTPUT'
112109
Usage: php-parse [operations] file1.php [file2.php ...]
113110
or: php-parse [operations] "<?php code"
114111
Turn PHP source code into an abstract syntax tree.
@@ -123,6 +120,7 @@ Operations is a list of the following options (--dump by default):
123120
-c, --with-column-info Show column-numbers for errors (if available)
124121
-P, --with-positions Show positions in node dumps
125122
-r, --with-recovery Use parsing with error recovery
123+
--version=VERSION Target specific PHP version (default: newest)
126124
-h, --help Display this page
127125
128126
Example:
@@ -143,6 +141,7 @@ function parseArgs($args) {
143141
'with-column-info' => false,
144142
'with-positions' => false,
145143
'with-recovery' => false,
144+
'version' => PhpParser\PhpVersion::getNewestSupported(),
146145
];
147146

148147
array_shift($args);
@@ -193,7 +192,9 @@ function parseArgs($args) {
193192
$parseOptions = false;
194193
break;
195194
default:
196-
if ($arg[0] === '-') {
195+
if (preg_match('/^--version=(.*)$/', $arg, $matches)) {
196+
$attributes['version'] = PhpParser\PhpVersion::fromString($matches[1]);
197+
} elseif ($arg[0] === '-') {
197198
showHelp("Invalid operation $arg.");
198199
} else {
199200
$files[] = $arg;

0 commit comments

Comments
 (0)