summaryrefslogtreecommitdiffstats
path: root/Parser
diff options
context:
space:
mode:
authorLysandros Nikolaou <lisandrosnik@gmail.com>2020-05-27 20:20:07 (GMT)
committerGitHub <noreply@github.com>2020-05-27 20:20:07 (GMT)
commit1bfe659ee5c6f07c55487d9ef7c2e653cf697f72 (patch)
tree063fd718f1b57d61d03f0fc81271258ec2ff98d8 /Parser
parent788d7bfe189e715eab3855c20ea5d6da0d8bed70 (diff)
downloadcpython-1bfe659ee5c6f07c55487d9ef7c2e653cf697f72.zip
cpython-1bfe659ee5c6f07c55487d9ef7c2e653cf697f72.tar.gz
cpython-1bfe659ee5c6f07c55487d9ef7c2e653cf697f72.tar.bz2
[3.9] Backport GH-20370 and GH-20436: Soft keywords (GH-20458)
Diffstat (limited to 'Parser')
-rw-r--r--Parser/pegen/pegen.c34
-rw-r--r--Parser/pegen/pegen.h2
2 files changed, 35 insertions, 1 deletions
diff --git a/Parser/pegen/pegen.c b/Parser/pegen/pegen.c
index cd87a9f..b858b6b 100644
--- a/Parser/pegen/pegen.c
+++ b/Parser/pegen/pegen.c
@@ -708,7 +708,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)
{
@@ -719,6 +718,15 @@ _PyPegen_lookahead_with_name(int positive, expr_ty (func)(Parser *), Parser *p)
}
int
+_PyPegen_lookahead_with_string(int positive, expr_ty (func)(Parser *, const char*), Parser *p, const char* arg)
+{
+ int mark = p->mark;
+ void *res = func(p, arg);
+ p->mark = mark;
+ return (res != NULL) == positive;
+}
+
+int
_PyPegen_lookahead_with_int(int positive, Token *(func)(Parser *, int), Parser *p, int arg)
{
int mark = p->mark;
@@ -753,6 +761,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) {
+ p->error_indicator = 1;
+ return NULL;
+ }
+ if (strcmp(s, keyword) != 0) {
+ return NULL;
+ }
+ return _PyPegen_name_token(p);
+}
+
Token *
_PyPegen_get_last_nonnwhitespace_token(Parser *p)
{
diff --git a/Parser/pegen/pegen.h b/Parser/pegen/pegen.h
index bd3056e..2585392 100644
--- a/Parser/pegen/pegen.h
+++ b/Parser/pegen/pegen.h
@@ -118,10 +118,12 @@ int _PyPegen_update_memo(Parser *p, int mark, int type, void *node);
int _PyPegen_is_memoized(Parser *p, int type, void *pres);
int _PyPegen_lookahead_with_name(int, expr_ty (func)(Parser *), Parser *);
+int _PyPegen_lookahead_with_string(int , expr_ty (func)(Parser *, const char*), Parser *, const char*);
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);