diff options
author | Guido van Rossum <guido@python.org> | 2020-05-26 17:58:44 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-26 17:58:44 (GMT) |
commit | b45af1a5691e83b86321fc52d173f66cf891ce5f (patch) | |
tree | ff39a728c49995811eeca554bc7b2ce89be2644e /Parser | |
parent | 578c3955e0222ec7b3146197467fbb0fcfae12fe (diff) | |
download | cpython-b45af1a5691e83b86321fc52d173f66cf891ce5f.zip cpython-b45af1a5691e83b86321fc52d173f66cf891ce5f.tar.gz cpython-b45af1a5691e83b86321fc52d173f66cf891ce5f.tar.bz2 |
Add soft keywords (GH-20370)
These are like keywords but they only work in context; they are not reserved except when there is an exact match.
This would enable things like match statements without reserving `match` (which would be bad for the `re.match()` function and probably lots of other places).
Automerge-Triggered-By: @gvanrossum
Diffstat (limited to 'Parser')
-rw-r--r-- | Parser/pegen/pegen.c | 24 | ||||
-rw-r--r-- | Parser/pegen/pegen.h | 1 |
2 files changed, 25 insertions, 0 deletions
diff --git a/Parser/pegen/pegen.c b/Parser/pegen/pegen.c index cd87a9f..ee30c2c 100644 --- a/Parser/pegen/pegen.c +++ b/Parser/pegen/pegen.c @@ -753,6 +753,30 @@ _PyPegen_expect_token(Parser *p, int type) return t; } +expr_ty +_PyPegen_expect_soft_keyword(Parser *p, const char *keyword) +{ + 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 != NAME) { + return NULL; + } + char* s = PyBytes_AsString(t->bytes); + if (!s) { + return NULL; + } + if (strcmp(s, keyword) != 0) { + return NULL; + } + expr_ty res = _PyPegen_name_token(p); + return res; +} + Token * _PyPegen_get_last_nonnwhitespace_token(Parser *p) { diff --git a/Parser/pegen/pegen.h b/Parser/pegen/pegen.h index bd3056e..9507d99 100644 --- a/Parser/pegen/pegen.h +++ b/Parser/pegen/pegen.h @@ -122,6 +122,7 @@ int _PyPegen_lookahead_with_int(int, Token *(func)(Parser *, int), Parser *, int int _PyPegen_lookahead(int, void *(func)(Parser *), Parser *); Token *_PyPegen_expect_token(Parser *p, int type); +expr_ty _PyPegen_expect_soft_keyword(Parser *p, const char *keyword); Token *_PyPegen_get_last_nonnwhitespace_token(Parser *); int _PyPegen_fill_token(Parser *p); expr_ty _PyPegen_name_token(Parser *p); |