diff options
author | Pablo Galindo <Pablogsal@gmail.com> | 2021-02-02 19:54:22 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-02-02 19:54:22 (GMT) |
commit | 58fb156edda1a0e924a38bfed494bd06cb09c9a3 (patch) | |
tree | 6d09348120225209479d011aa83be95d488e3380 /Parser/pegen.c | |
parent | 802b645e81a72399a7ef47ef000d468c775dcd3e (diff) | |
download | cpython-58fb156edda1a0e924a38bfed494bd06cb09c9a3.zip cpython-58fb156edda1a0e924a38bfed494bd06cb09c9a3.tar.gz cpython-58fb156edda1a0e924a38bfed494bd06cb09c9a3.tar.bz2 |
bpo-42997: Improve error message for missing : before suites (GH-24292)
* Add to the peg generator a new directive ('&&') that allows to expect
a token and hard fail the parsing if the token is not found. This
allows to quickly emmit syntax errors for missing tokens.
* Use the new grammar element to hard-fail if the ':' is missing before
suites.
Diffstat (limited to 'Parser/pegen.c')
-rw-r--r-- | Parser/pegen.c | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/Parser/pegen.c b/Parser/pegen.c index 2554273..68f0e32 100644 --- a/Parser/pegen.c +++ b/Parser/pegen.c @@ -782,7 +782,6 @@ _PyPegen_is_memoized(Parser *p, int type, void *pres) return 0; } - int _PyPegen_lookahead_with_name(int positive, expr_ty (func)(Parser *), Parser *p) { @@ -836,6 +835,28 @@ _PyPegen_expect_token(Parser *p, int type) return t; } +Token * +_PyPegen_expect_forced_token(Parser *p, int type, const char* expected) { + + if (p->error_indicator == 1) { + return NULL; + } + + if (p->mark == p->fill) { + if (_PyPegen_fill_token(p) < 0) { + p->error_indicator = 1; + return NULL; + } + } + Token *t = p->tokens[p->mark]; + if (t->type != type) { + RAISE_SYNTAX_ERROR_KNOWN_LOCATION(t, "expected '%s'", expected); + return NULL; + } + p->mark += 1; + return t; +} + expr_ty _PyPegen_expect_soft_keyword(Parser *p, const char *keyword) { |