diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2021-08-02 19:05:33 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-08-02 19:05:33 (GMT) |
commit | 567176249ea95c074eb80199aaf19f3a55aa3954 (patch) | |
tree | f480151241cd690cfea6c753645bb701243752ae /Parser | |
parent | c6e7c9860d5ac968c7a1073fdbfbd44e0e87f582 (diff) | |
download | cpython-567176249ea95c074eb80199aaf19f3a55aa3954.zip cpython-567176249ea95c074eb80199aaf19f3a55aa3954.tar.gz cpython-567176249ea95c074eb80199aaf19f3a55aa3954.tar.bz2 |
bpo-44792: Improve syntax errors for if expressions (GH-27506) (GH-27565)
(cherry picked from commit 28b6dc9dd5d1ce6f8aff7e06d4ef9afdc2bc8332)
Co-authored-by: Miguel Brito <5544985+miguendes@users.noreply.github.com>
Diffstat (limited to 'Parser')
-rw-r--r-- | Parser/parser.c | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/Parser/parser.c b/Parser/parser.c index c4a4eb6..b37e3d6 100644 --- a/Parser/parser.c +++ b/Parser/parser.c @@ -18229,6 +18229,7 @@ invalid_legacy_expression_rule(Parser *p) // invalid_expression: // | invalid_legacy_expression // | !(NAME STRING | SOFT_KEYWORD) disjunction expression_without_invalid +// | disjunction 'if' disjunction !'else' static void * invalid_expression_rule(Parser *p) { @@ -18287,6 +18288,38 @@ invalid_expression_rule(Parser *p) D(fprintf(stderr, "%*c%s invalid_expression[%d-%d]: %s failed!\n", p->level, ' ', p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "!(NAME STRING | SOFT_KEYWORD) disjunction expression_without_invalid")); } + { // disjunction 'if' disjunction !'else' + if (p->error_indicator) { + D(p->level--); + return NULL; + } + D(fprintf(stderr, "%*c> invalid_expression[%d-%d]: %s\n", p->level, ' ', _mark, p->mark, "disjunction 'if' disjunction !'else'")); + Token * _keyword; + expr_ty a; + expr_ty b; + if ( + (a = disjunction_rule(p)) // disjunction + && + (_keyword = _PyPegen_expect_token(p, 510)) // token='if' + && + (b = disjunction_rule(p)) // disjunction + && + _PyPegen_lookahead_with_int(0, _PyPegen_expect_token, p, 516) // token='else' + ) + { + D(fprintf(stderr, "%*c+ invalid_expression[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "disjunction 'if' disjunction !'else'")); + _res = RAISE_SYNTAX_ERROR_KNOWN_RANGE ( a , b , "expected 'else' after 'if' expression" ); + if (_res == NULL && PyErr_Occurred()) { + p->error_indicator = 1; + D(p->level--); + return NULL; + } + goto done; + } + p->mark = _mark; + D(fprintf(stderr, "%*c%s invalid_expression[%d-%d]: %s failed!\n", p->level, ' ', + p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "disjunction 'if' disjunction !'else'")); + } _res = NULL; done: D(p->level--); |