summaryrefslogtreecommitdiffstats
path: root/Grammar
diff options
context:
space:
mode:
authorPablo Galindo <Pablogsal@gmail.com>2021-04-15 13:06:39 (GMT)
committerGitHub <noreply@github.com>2021-04-15 13:06:39 (GMT)
commitda7435017430671f86075449ff7bde96fd7700a5 (patch)
treeb287439992c61b96d5007b69d450ab0f237215d1 /Grammar
parent23acadcc1c75eb74b2459304af70d97a35001b34 (diff)
downloadcpython-da7435017430671f86075449ff7bde96fd7700a5.zip
cpython-da7435017430671f86075449ff7bde96fd7700a5.tar.gz
cpython-da7435017430671f86075449ff7bde96fd7700a5.tar.bz2
bpo-43823: Improve syntax errors for invalid dictionary literals (GH-25378)
Diffstat (limited to 'Grammar')
-rw-r--r--Grammar/python.gram11
1 files changed, 11 insertions, 0 deletions
diff --git a/Grammar/python.gram b/Grammar/python.gram
index 180d952..8e399f1 100644
--- a/Grammar/python.gram
+++ b/Grammar/python.gram
@@ -650,6 +650,8 @@ dict[expr_ty]:
CHECK(asdl_expr_seq*, _PyPegen_get_keys(p, a)),
CHECK(asdl_expr_seq*, _PyPegen_get_values(p, a)),
EXTRA) }
+ | '{' invalid_double_starred_kvpairs '}'
+
dictcomp[expr_ty]:
| '{' a=kvpair b=for_if_clauses '}' { _PyAST_DictComp(a->key, a->value, b, EXTRA) }
| invalid_dict_comprehension
@@ -882,3 +884,12 @@ invalid_elif_stmt:
| 'elif' named_expression NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }
invalid_while_stmt:
| 'while' named_expression NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }
+
+invalid_double_starred_kvpairs:
+ | ','.double_starred_kvpair+ ',' invalid_kvpair
+ | expression ':' a='*' bitwise_or { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot use a starred expression in a dictionary value") }
+ | expression a=':' &('}'|',') { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "expression expected after dictionary key and ':'") }
+invalid_kvpair:
+ | a=expression !(':') { RAISE_SYNTAX_ERROR("':' expected after dictionary key") }
+ | expression ':' a='*' bitwise_or { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot use a starred expression in a dictionary value") }
+ | expression a=':' {RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "expression expected after dictionary key and ':'") }