bpo-45727: Only trigger the 'did you forgot a comma' error suggestion if inside parentheses (GH-29757)

This commit is contained in:
Pablo Galindo Salgado 2021-11-24 22:21:23 +00:00 committed by GitHub
parent f4afc53bf6
commit 24c10d2943
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 15 additions and 8 deletions

View File

@ -1084,7 +1084,8 @@ invalid_expression:
# !(NAME STRING) is not matched so we don't show this error with some invalid string prefixes like: kf"dsfsdf" # !(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 # Soft keywords need to also be ignored because they can be parsed as NAME NAME
| !(NAME STRING | SOFT_KEYWORD) a=disjunction b=expression_without_invalid { | !(NAME STRING | SOFT_KEYWORD) a=disjunction b=expression_without_invalid {
_PyPegen_check_legacy_stmt(p, a) ? NULL : RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Perhaps you forgot a comma?") } _PyPegen_check_legacy_stmt(p, a) ? NULL : p->tokens[p->mark-1]->level == 0 ? NULL :
RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Perhaps you forgot a comma?") }
| a=disjunction 'if' b=disjunction !('else'|':') { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "expected 'else' after 'if' expression") } | a=disjunction 'if' b=disjunction !('else'|':') { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "expected 'else' after 'if' expression") }
invalid_named_expression: invalid_named_expression:

View File

@ -226,14 +226,14 @@ class ExceptionTests(unittest.TestCase):
check(b'Python = "\xcf\xb3\xf2\xee\xed" +', 1, 18) check(b'Python = "\xcf\xb3\xf2\xee\xed" +', 1, 18)
check('x = "a', 1, 5) check('x = "a', 1, 5)
check('lambda x: x = 2', 1, 1) check('lambda x: x = 2', 1, 1)
check('f{a + b + c}', 1, 1) check('f{a + b + c}', 1, 2)
check('[file for str(file) in []\n])', 1, 11) check('[file for str(file) in []\n])', 1, 11)
check('a = « hello » « world »', 1, 5) check('a = « hello » « world »', 1, 5)
check('[\nfile\nfor str(file)\nin\n[]\n]', 3, 5) check('[\nfile\nfor str(file)\nin\n[]\n]', 3, 5)
check('[file for\n str(file) in []]', 2, 2) check('[file for\n str(file) in []]', 2, 2)
check("ages = {'Alice'=22, 'Bob'=23}", 1, 16) check("ages = {'Alice'=22, 'Bob'=23}", 1, 16)
check('match ...:\n case {**rest, "key": value}:\n ...', 2, 19) check('match ...:\n case {**rest, "key": value}:\n ...', 2, 19)
check("a b c d e f", 1, 1) check("[a b c d e f]", 1, 2)
# Errors thrown by compile.c # Errors thrown by compile.c
check('class foo:return 1', 1, 11) check('class foo:return 1', 1, 11)

View File

@ -944,7 +944,7 @@ x = (
"Bf''", "Bf''",
"BF''",] "BF''",]
double_quote_cases = [case.replace("'", '"') for case in single_quote_cases] double_quote_cases = [case.replace("'", '"') for case in single_quote_cases]
self.assertAllRaise(SyntaxError, 'unexpected EOF while parsing', self.assertAllRaise(SyntaxError, 'invalid syntax',
single_quote_cases + double_quote_cases) single_quote_cases + double_quote_cases)
def test_leading_trailing_spaces(self): def test_leading_trailing_spaces(self):

View File

@ -0,0 +1,3 @@
Refine the custom syntax error that suggests that a comma may be missing to
trigger only when the expressions are detected between parentheses or
brackets. Patch by Pablo Galindo

View File

@ -18298,7 +18298,7 @@ invalid_expression_rule(Parser *p)
) )
{ {
D(fprintf(stderr, "%*c+ invalid_expression[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "!(NAME STRING | SOFT_KEYWORD) disjunction expression_without_invalid")); D(fprintf(stderr, "%*c+ invalid_expression[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "!(NAME STRING | SOFT_KEYWORD) disjunction expression_without_invalid"));
_res = _PyPegen_check_legacy_stmt ( p , a ) ? NULL : RAISE_SYNTAX_ERROR_KNOWN_RANGE ( a , b , "invalid syntax. Perhaps you forgot a comma?" ); _res = _PyPegen_check_legacy_stmt ( p , a ) ? NULL : p -> tokens [p -> mark - 1] -> level == 0 ? NULL : RAISE_SYNTAX_ERROR_KNOWN_RANGE ( a , b , "invalid syntax. Perhaps you forgot a comma?" );
if (_res == NULL && PyErr_Occurred()) { if (_res == NULL && PyErr_Occurred()) {
p->error_indicator = 1; p->error_indicator = 1;
D(p->level--); D(p->level--);

View File

@ -170,6 +170,8 @@ initialize_token(Parser *p, Token *token, const char *start, const char *end, in
return -1; return -1;
} }
token->level = p->tok->level;
const char *line_start = token_type == STRING ? p->tok->multi_line_start : p->tok->line_start; const char *line_start = token_type == STRING ? p->tok->multi_line_start : p->tok->line_start;
int lineno = token_type == STRING ? p->tok->first_lineno : p->tok->lineno; int lineno = token_type == STRING ? p->tok->first_lineno : p->tok->lineno;
int end_lineno = p->tok->lineno; int end_lineno = p->tok->lineno;
@ -946,4 +948,4 @@ _PyPegen_run_parser_from_string(const char *str, int start_rule, PyObject *filen
error: error:
_PyTokenizer_Free(tok); _PyTokenizer_Free(tok);
return result; return result;
} }

View File

@ -35,6 +35,7 @@ typedef struct _memo {
typedef struct { typedef struct {
int type; int type;
PyObject *bytes; PyObject *bytes;
int level;
int lineno, col_offset, end_lineno, end_col_offset; int lineno, col_offset, end_lineno, end_col_offset;
Memo *memo; Memo *memo;
} Token; } Token;

View File

@ -399,7 +399,7 @@ _Pypegen_set_syntax_error(Parser* p, Token* last_token) {
RAISE_SYNTAX_ERROR("error at start before reading any input"); RAISE_SYNTAX_ERROR("error at start before reading any input");
} }
// Parser encountered EOF (End of File) unexpectedtly // Parser encountered EOF (End of File) unexpectedtly
if (p->tok->done == E_EOF) { if (last_token->type == ERRORTOKEN && p->tok->done == E_EOF) {
if (p->tok->level) { if (p->tok->level) {
raise_unclosed_parentheses_error(p); raise_unclosed_parentheses_error(p);
} else { } else {
@ -422,4 +422,4 @@ _Pypegen_set_syntax_error(Parser* p, Token* last_token) {
// _PyPegen_tokenize_full_source_to_check_for_errors will override the existing // _PyPegen_tokenize_full_source_to_check_for_errors will override the existing
// generic SyntaxError we just raised if errors are found. // generic SyntaxError we just raised if errors are found.
_PyPegen_tokenize_full_source_to_check_for_errors(p); _PyPegen_tokenize_full_source_to_check_for_errors(p);
} }