diff options
author | Pablo Galindo <Pablogsal@gmail.com> | 2021-04-15 20:38:45 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-15 20:38:45 (GMT) |
commit | b280248be8e648feb82f3f3ed0050e50b238df7b (patch) | |
tree | fee5117cd4e2111d701422c52e30f3b85349b1a9 /Grammar | |
parent | e692f55979980826a5281560c534ef399a8f9848 (diff) | |
download | cpython-b280248be8e648feb82f3f3ed0050e50b238df7b.zip cpython-b280248be8e648feb82f3f3ed0050e50b238df7b.tar.gz cpython-b280248be8e648feb82f3f3ed0050e50b238df7b.tar.bz2 |
bpo-43822: Improve syntax errors for missing commas (GH-25377)
Diffstat (limited to 'Grammar')
-rw-r--r-- | Grammar/Tokens | 1 | ||||
-rw-r--r-- | Grammar/python.gram | 9 |
2 files changed, 10 insertions, 0 deletions
diff --git a/Grammar/Tokens b/Grammar/Tokens index 9de2da5..1f3e3b0 100644 --- a/Grammar/Tokens +++ b/Grammar/Tokens @@ -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 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( |