mirror of
https://github.com/python/cpython.git
synced 2025-12-04 00:30:19 +00:00
bpo-33306: Improve SyntaxError messages for unbalanced parentheses. (GH-6516)
This commit is contained in:
parent
bdabb0737c
commit
94cf308ee2
5 changed files with 47 additions and 7 deletions
|
|
@ -1842,12 +1842,44 @@ tok_get(struct tok_state *tok, char **p_start, char **p_end)
|
|||
case '(':
|
||||
case '[':
|
||||
case '{':
|
||||
#ifndef PGEN
|
||||
if (tok->level >= MAXLEVEL) {
|
||||
return syntaxerror(tok, "too many nested parentheses");
|
||||
}
|
||||
tok->parenstack[tok->level] = c;
|
||||
tok->parenlinenostack[tok->level] = tok->lineno;
|
||||
#endif
|
||||
tok->level++;
|
||||
break;
|
||||
case ')':
|
||||
case ']':
|
||||
case '}':
|
||||
#ifndef PGEN
|
||||
if (!tok->level) {
|
||||
return syntaxerror(tok, "unmatched '%c'", c);
|
||||
}
|
||||
#endif
|
||||
tok->level--;
|
||||
#ifndef PGEN
|
||||
int opening = tok->parenstack[tok->level];
|
||||
if (!((opening == '(' && c == ')') ||
|
||||
(opening == '[' && c == ']') ||
|
||||
(opening == '{' && c == '}')))
|
||||
{
|
||||
if (tok->parenlinenostack[tok->level] != tok->lineno) {
|
||||
return syntaxerror(tok,
|
||||
"closing parenthesis '%c' does not match "
|
||||
"opening parenthesis '%c' on line %d",
|
||||
c, opening, tok->parenlinenostack[tok->level]);
|
||||
}
|
||||
else {
|
||||
return syntaxerror(tok,
|
||||
"closing parenthesis '%c' does not match "
|
||||
"opening parenthesis '%c'",
|
||||
c, opening);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue