Skip to content

Commit bca7014

Browse files
authored
bpo-42123: Run the parser two times and only enable invalid rules on the second run (GH-22111)
* Implement running the parser a second time for the errors messages The first parser run is only responsible for detecting whether there is a `SyntaxError` or not. If there isn't the AST gets returned. Otherwise, the parser is run a second time with all the `invalid_*` rules enabled so that all the customized error messages get produced.
1 parent c8c4200 commit bca7014

File tree

6 files changed

+70
-50
lines changed

6 files changed

+70
-50
lines changed

Grammar/python.gram

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ yield_expr[expr_ty]:
551551

552552
arguments[expr_ty] (memo):
553553
| a=args [','] &')' { a }
554-
| incorrect_arguments
554+
| invalid_arguments
555555
args[expr_ty]:
556556
| a[asdl_expr_seq*]=','.(starred_expression | named_expression !'=')+ b=[',' k=kwargs {k}] { _PyPegen_collect_call_seqs(p, a, b, EXTRA) }
557557
| a=kwargs { _Py_Call(_PyPegen_dummy_name(p),
@@ -637,7 +637,7 @@ t_atom[expr_ty]:
637637

638638

639639
# From here on, there are rules for invalid syntax with specialised error messages
640-
incorrect_arguments:
640+
invalid_arguments:
641641
| args ',' '*' { RAISE_SYNTAX_ERROR("iterable argument unpacking follows keyword argument unpacking") }
642642
| a=expression for_if_clauses ',' [args | expression for_if_clauses] {
643643
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "Generator expression must be parenthesized") }
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Run the parser two times. On the first run, disable all the rules that only
2+
generate better error messages to gain performance. If there's a parse
3+
failure, run the parser a second time with those enabled.

0 commit comments

Comments
 (0)