Skip to content

Commit

Permalink
bpo-43822: Improve syntax errors for missing commas (pythonGH-25377)
Browse files Browse the repository at this point in the history
  • Loading branch information
pablogsal authored Apr 15, 2021
1 parent e692f55 commit b280248
Show file tree
Hide file tree
Showing 13 changed files with 851 additions and 650 deletions.
2 changes: 2 additions & 0 deletions Doc/library/token-list.inc

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Grammar/Tokens
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ AWAIT
ASYNC
TYPE_IGNORE
TYPE_COMMENT
SOFT_KEYWORD
ERRORTOKEN

# These aren't used by the C tokenizer but are needed for tokenize.py
Expand Down
9 changes: 9 additions & 0 deletions Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ _PyPegen_parse(Parser *p)
// Initialize keywords
p->keywords = reserved_keywords;
p->n_keyword_lists = n_keyword_lists;
p->soft_keywords = soft_keywords;

// Run parser
void *result = NULL;
Expand Down Expand Up @@ -459,6 +460,7 @@ expressions[expr_ty]:
| a=expression ',' { _PyAST_Tuple(CHECK(asdl_expr_seq*, _PyPegen_singleton_seq(p, a)), Load, EXTRA) }
| expression
expression[expr_ty] (memo):
| invalid_expression
| a=disjunction 'if' b=disjunction 'else' c=expression { _PyAST_IfExp(b, a, c, EXTRA) }
| disjunction
| lambdef
Expand Down Expand Up @@ -778,6 +780,13 @@ invalid_kwarg:
| expression a='=' {
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
a, "expression cannot contain assignment, perhaps you meant \"==\"?") }

invalid_expression:
# !(NAME STRING) is not matched so we don't show this error with some invalid string prefixes like: kf"dsfsdf"
# Soft keywords need to also be ignored because they can be parsed as NAME NAME
| !(NAME STRING | SOFT_KEYWORD) a=disjunction expression {
RAISE_ERROR_KNOWN_LOCATION(p, PyExc_SyntaxError, a->lineno, a->end_col_offset - 1, "invalid syntax. Perhaps you forgot a comma?") }

invalid_named_expression:
| a=expression ':=' expression {
RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
Expand Down
5 changes: 3 additions & 2 deletions Include/token.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Lib/test/test_genexps.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
>>> dict(a = i for i in range(10))
Traceback (most recent call last):
...
SyntaxError: invalid syntax
SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
Verify that parenthesis are required when used as a keyword argument value
Expand Down
24 changes: 19 additions & 5 deletions Lib/test/test_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,22 +248,36 @@
# Missing commas in literals collections should not
# produce special error messages regarding missing
# parentheses
# parentheses, but about missing commas instead
>>> [1, 2 3]
Traceback (most recent call last):
SyntaxError: invalid syntax
SyntaxError: invalid syntax. Perhaps you forgot a comma?
>>> {1, 2 3}
Traceback (most recent call last):
SyntaxError: invalid syntax
SyntaxError: invalid syntax. Perhaps you forgot a comma?
>>> {1:2, 2:5 3:12}
Traceback (most recent call last):
SyntaxError: invalid syntax
SyntaxError: invalid syntax. Perhaps you forgot a comma?
>>> (1, 2 3)
Traceback (most recent call last):
SyntaxError: invalid syntax. Perhaps you forgot a comma?
# Make sure soft keywords constructs don't raise specialized
# errors regarding missing commas
>>> match x:
... y = 3
Traceback (most recent call last):
SyntaxError: invalid syntax
>>> match x:
... case y:
... 3 $ 3
Traceback (most recent call last):
SyntaxError: invalid syntax
From compiler_complex_args():
Expand Down Expand Up @@ -864,7 +878,7 @@
SyntaxError: cannot assign to attribute here. Maybe you meant '==' instead of '='?
Ensure that early = are not matched by the parser as invalid comparisons
>>> f(2, 4, x=34); {1,2 a}
>>> f(2, 4, x=34); 1 $ 2
Traceback (most recent call last):
SyntaxError: invalid syntax
Expand Down
11 changes: 6 additions & 5 deletions Lib/token.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Improve syntax errors in the parser for missing commas between expressions.
Patch by Pablo Galindo.
Loading

0 comments on commit b280248

Please sign in to comment.