diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2018-12-17 15:34:14 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-12-17 15:34:14 (GMT) |
commit | 94cf308ee231bfbfaa9ddc50b9764545a1318773 (patch) | |
tree | adc068a6567b6712226436ad399951ad888cb53c /Parser/tokenizer.c | |
parent | bdabb0737c631835b246c9823852d20331243315 (diff) | |
download | cpython-94cf308ee231bfbfaa9ddc50b9764545a1318773.zip cpython-94cf308ee231bfbfaa9ddc50b9764545a1318773.tar.gz cpython-94cf308ee231bfbfaa9ddc50b9764545a1318773.tar.bz2 |
bpo-33306: Improve SyntaxError messages for unbalanced parentheses. (GH-6516)
Diffstat (limited to 'Parser/tokenizer.c')
-rw-r--r-- | Parser/tokenizer.c | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/Parser/tokenizer.c b/Parser/tokenizer.c index d319a4c..c246ee2 100644 --- a/Parser/tokenizer.c +++ b/Parser/tokenizer.c @@ -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; } |