@@ -26,13 +26,10 @@ if (empty($files)) {
26
26
showHelp ("Must specify at least one file. " );
27
27
}
28
28
29
- $ lexer = new PhpParser \ Lexer \ Emulative ( ['usedAttributes ' => [
29
+ $ lexerOptions = ['usedAttributes ' => [
30
30
'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 );
36
33
$ dumper = new PhpParser \NodeDumper ([
37
34
'dumpComments ' => true ,
38
35
'dumpPositions ' => $ attributes ['with-positions ' ],
@@ -108,7 +105,7 @@ function showHelp($error = '') {
108
105
if ($ error ) {
109
106
fwrite (STDERR , $ error . "\n\n" );
110
107
}
111
- fwrite ($ error ? STDERR : STDOUT , <<<OUTPUT
108
+ fwrite ($ error ? STDERR : STDOUT , <<<' OUTPUT'
112
109
Usage: php-parse [operations] file1.php [file2.php ...]
113
110
or: php-parse [operations] "<?php code"
114
111
Turn PHP source code into an abstract syntax tree.
@@ -123,6 +120,7 @@ Operations is a list of the following options (--dump by default):
123
120
-c, --with-column-info Show column-numbers for errors (if available)
124
121
-P, --with-positions Show positions in node dumps
125
122
-r, --with-recovery Use parsing with error recovery
123
+ --version=VERSION Target specific PHP version (default: newest)
126
124
-h, --help Display this page
127
125
128
126
Example:
@@ -143,6 +141,7 @@ function parseArgs($args) {
143
141
'with-column-info ' => false ,
144
142
'with-positions ' => false ,
145
143
'with-recovery ' => false ,
144
+ 'version ' => PhpParser \PhpVersion::getNewestSupported (),
146
145
];
147
146
148
147
array_shift ($ args );
@@ -193,7 +192,9 @@ function parseArgs($args) {
193
192
$ parseOptions = false ;
194
193
break ;
195
194
default :
196
- if ($ arg [0 ] === '- ' ) {
195
+ if (preg_match ('/^--version=(.*)$/ ' , $ arg , $ matches )) {
196
+ $ attributes ['version ' ] = PhpParser \PhpVersion::fromString ($ matches [1 ]);
197
+ } elseif ($ arg [0 ] === '- ' ) {
197
198
showHelp ("Invalid operation $ arg. " );
198
199
} else {
199
200
$ files [] = $ arg ;
0 commit comments