@@ -29,7 +29,11 @@ if (empty($files)) {
29
29
$ lexer = new PhpParser \Lexer \Emulative (array ('usedAttributes ' => array (
30
30
'startLine ' , 'endLine ' , 'startFilePos ' , 'endFilePos ' , 'comments '
31
31
)));
32
- $ parser = (new PhpParser \ParserFactory )->create (PhpParser \ParserFactory::PREFER_PHP7 , $ lexer );
32
+ $ parser = (new PhpParser \ParserFactory )->create (
33
+ PhpParser \ParserFactory::PREFER_PHP7 ,
34
+ $ lexer ,
35
+ array ('throwOnError ' => !$ attributes ['with-recovery ' ])
36
+ );
33
37
$ dumper = new PhpParser \NodeDumper (['dumpComments ' => true ]);
34
38
$ prettyPrinter = new PhpParser \PrettyPrinter \Standard ;
35
39
$ serializer = new PhpParser \Serializer \XML ;
@@ -52,17 +56,15 @@ foreach ($files as $file) {
52
56
53
57
try {
54
58
$ stmts = $ parser ->parse ($ code );
55
- } catch (PhpParser \Error $ e ) {
56
- if ($ attributes ['with-column-info ' ] && $ e ->hasColumnInfo ()) {
57
- $ startLine = $ e ->getStartLine ();
58
- $ endLine = $ e ->getEndLine ();
59
- $ startColumn = $ e ->getStartColumn ($ code );
60
- $ endColumn = $ e ->getEndColumn ($ code );
61
- $ message .= $ e ->getRawMessage () . " from $ startLine: $ startColumn to $ endLine: $ endColumn " ;
62
- } else {
63
- $ message = $ e ->getMessage ();
59
+ foreach ($ parser ->getErrors () as $ error ) {
60
+ $ message = formatErrorMessage ($ error , $ code , $ attributes ['with-column-info ' ]);
61
+ echo $ message . "\n" ;
64
62
}
65
-
63
+ if (null === $ stmts ) {
64
+ continue ;
65
+ }
66
+ } catch (PhpParser \Error $ error ) {
67
+ $ message = formatErrorMessage ($ error , $ code , $ attributes ['with-column-info ' ]);
66
68
die ($ message . "\n" );
67
69
}
68
70
@@ -86,6 +88,18 @@ foreach ($files as $file) {
86
88
}
87
89
}
88
90
91
+ function formatErrorMessage (PhpParser \Error $ e , $ code , $ withColumnInfo ) {
92
+ if ($ withColumnInfo && $ e ->hasColumnInfo ()) {
93
+ $ startLine = $ e ->getStartLine ();
94
+ $ endLine = $ e ->getEndLine ();
95
+ $ startColumn = $ e ->getStartColumn ($ code );
96
+ $ endColumn = $ e ->getEndColumn ($ code );
97
+ return $ e ->getRawMessage () . " from $ startLine: $ startColumn to $ endLine: $ endColumn " ;
98
+ } else {
99
+ return $ e ->getMessage ();
100
+ }
101
+ }
102
+
89
103
function showHelp ($ error = '' ) {
90
104
if ($ error ) {
91
105
echo $ error . "\n\n" ;
@@ -103,6 +117,7 @@ Operations is a list of the following options (--dump by default):
103
117
--var-dump var_dump() nodes (for exact structure)
104
118
-N, --resolve-names Resolve names using NodeVisitor\NameResolver
105
119
-c, --with-column-info Show column-numbers for errors (if available)
120
+ -r, --with-recovery Use parsing with error recovery
106
121
-h, --help Display this page
107
122
108
123
Example:
@@ -119,7 +134,8 @@ function parseArgs($args) {
119
134
$ operations = array ();
120
135
$ files = array ();
121
136
$ attributes = array (
122
- 'with-column-info ' => false ,
137
+ 'with-column-info ' => false ,
138
+ 'with-recovery ' => false ,
123
139
);
124
140
125
141
array_shift ($ args );
@@ -153,6 +169,10 @@ function parseArgs($args) {
153
169
case '-c ' ;
154
170
$ attributes ['with-column-info ' ] = true ;
155
171
break ;
172
+ case '--with-recovery ' :
173
+ case '-r ' :
174
+ $ attributes ['with-recovery ' ] = true ;
175
+ break ;
156
176
case '--help ' :
157
177
case '-h ' ;
158
178
showHelp ();
0 commit comments