summaryrefslogtreecommitdiffstats
path: root/Grammar/python.gram
diff options
context:
space:
mode:
authorPablo Galindo <Pablogsal@gmail.com>2021-04-15 20:38:45 (GMT)
committerGitHub <noreply@github.com>2021-04-15 20:38:45 (GMT)
commitb280248be8e648feb82f3f3ed0050e50b238df7b (patch)
treefee5117cd4e2111d701422c52e30f3b85349b1a9 /Grammar/python.gram
parente692f55979980826a5281560c534ef399a8f9848 (diff)
downloadcpython-b280248be8e648feb82f3f3ed0050e50b238df7b.zip
cpython-b280248be8e648feb82f3f3ed0050e50b238df7b.tar.gz
cpython-b280248be8e648feb82f3f3ed0050e50b238df7b.tar.bz2
bpo-43822: Improve syntax errors for missing commas (GH-25377)
Diffstat (limited to 'Grammar/python.gram')
-rw-r--r--Grammar/python.gram9
1 files changed, 9 insertions, 0 deletions
diff --git a/Grammar/python.gram b/Grammar/python.gram
index 8e399f1..e5baac3 100644
--- a/Grammar/python.gram
+++ b/Grammar/python.gram
@@ -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;
@@ -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
@@ -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(