Skip to content

Commit

Permalink
Improve error recovery quality
Browse files Browse the repository at this point in the history
In particular, support recovering from a missing trailing semicolon,
while keeping the rest of the expression.
  • Loading branch information
nikic committed Apr 20, 2016
1 parent e45e31c commit 96cbd48
Show file tree
Hide file tree
Showing 7 changed files with 1,435 additions and 1,337 deletions.
1 change: 1 addition & 0 deletions grammar/php5.y
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ non_empty_statement:
| T_THROW expr ';' { $$ = Stmt\Throw_[$2]; }
| T_GOTO T_STRING ';' { $$ = Stmt\Goto_[$2]; }
| T_STRING ':' { $$ = Stmt\Label[$1]; }
| expr error { $$ = $1; }
| error { $$ = array(); /* means: no statement */ }
;

Expand Down
1 change: 1 addition & 0 deletions grammar/php7.y
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ non_empty_statement:
| T_THROW expr ';' { $$ = Stmt\Throw_[$2]; }
| T_GOTO T_STRING ';' { $$ = Stmt\Goto_[$2]; }
| T_STRING ':' { $$ = Stmt\Label[$1]; }
| expr error { $$ = $1; }
| error { $$ = array(); /* means: no statement */ }
;

Expand Down
1,426 changes: 716 additions & 710 deletions lib/PhpParser/Parser/Php5.php

Large diffs are not rendered by default.

1,252 changes: 628 additions & 624 deletions lib/PhpParser/Parser/Php7.php

Large diffs are not rendered by default.

18 changes: 16 additions & 2 deletions test/code/parser/errorHandling/eofError.test
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,29 @@ Error positions
-----
Syntax error, unexpected EOF from 1:10 to 1:10
array(
0: Expr_ConstFetch(
name: Name(
parts: array(
0: foo
)
)
)
)
-----
<?php foo /* bar */
-----
Syntax error, unexpected EOF from 1:20 to 1:20
array(
0: Stmt_Nop(
0: Expr_ConstFetch(
name: Name(
parts: array(
0: foo
)
)
)
1: Stmt_Nop(
comments: array(
0: /* bar */
)
)
)
)
53 changes: 52 additions & 1 deletion test/code/parser/errorHandling/recovery.test
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,33 @@ Syntax error, unexpected T_STRING from 4:1 to 4:3
Syntax error, unexpected T_STRING from 5:1 to 5:3
Syntax error, unexpected EOF from 5:6 to 5:6
array(
0: Expr_FuncCall(
name: Name(
parts: array(
0: foo
)
)
args: array(
)
)
1: Expr_FuncCall(
name: Name(
parts: array(
0: bar
)
)
args: array(
)
)
2: Expr_FuncCall(
name: Name(
parts: array(
0: baz
)
)
args: array(
)
)
)
-----
<?php
Expand All @@ -23,13 +50,22 @@ array(
0: Expr_FuncCall(
name: Name(
parts: array(
0: bar
0: foo
)
)
args: array(
)
)
1: Expr_FuncCall(
name: Name(
parts: array(
0: bar
)
)
args: array(
)
)
2: Expr_FuncCall(
name: Name(
parts: array(
0: baz
Expand Down Expand Up @@ -58,6 +94,15 @@ array(
)
)
1: Expr_FuncCall(
name: Name(
parts: array(
0: bar
)
)
args: array(
)
)
2: Expr_FuncCall(
name: Name(
parts: array(
0: baz
Expand All @@ -81,6 +126,9 @@ array(
)
)
)
1: Scalar_LNumber(
value: 1
)
)
-----
<?php
Expand All @@ -97,6 +145,9 @@ array(
)
returnType: null
stmts: array(
0: Scalar_LNumber(
value: 1
)
)
)
)
Expand Down
21 changes: 21 additions & 0 deletions test/code/parser/stmt/namespace/groupUseErrors.test
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,20 @@ use Foo {Bar, Baz};
-----
Syntax error, unexpected '{', expecting ',' or ';' from 3:9 to 3:9
array(
0: Expr_ConstFetch(
name: Name(
parts: array(
0: Bar
)
)
)
1: Expr_ConstFetch(
name: Name(
parts: array(
0: Baz
)
)
)
)
-----
<?php
Expand All @@ -42,4 +56,11 @@ use Foo\{\Bar};
-----
Syntax error, unexpected T_NS_SEPARATOR, expecting T_STRING or T_FUNCTION or T_CONST from 3:10 to 3:10
array(
0: Expr_ConstFetch(
name: Name_FullyQualified(
parts: array(
0: Bar
)
)
)
)

0 comments on commit 96cbd48

Please sign in to comment.