Include soft keywords in keyword.py (GH-20877)

This commit is contained in:
Pablo Galindo 2020-06-15 03:55:15 +01:00 committed by GitHub
parent 10c3b2120a
commit 78319e373d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 5 deletions

View File

@ -13,7 +13,7 @@ the python source tree and run:
Alternatively, you can run 'make regen-keyword'.
"""
__all__ = ["iskeyword", "kwlist"]
__all__ = ["iskeyword", "issoftkeyword", "kwlist", "softkwlist"]
kwlist = [
'False',
@ -53,4 +53,9 @@ kwlist = [
'yield'
]
softkwlist = [
]
iskeyword = frozenset(kwlist).__contains__
issoftkeyword = frozenset(softkwlist).__contains__

View File

@ -105,6 +105,7 @@ class CCallMakerVisitor(GrammarVisitor):
self.non_exact_tokens = non_exact_tokens
self.cache: Dict[Any, FunctionCall] = {}
self.keyword_cache: Dict[str, int] = {}
self.soft_keywords: Set[str] = set()
def keyword_helper(self, keyword: str) -> FunctionCall:
if keyword not in self.keyword_cache:
@ -119,6 +120,7 @@ class CCallMakerVisitor(GrammarVisitor):
)
def soft_keyword_helper(self, value: str) -> FunctionCall:
self.soft_keywords.add(value.replace('"', ""))
return FunctionCall(
assigned_variable="_keyword",
function="_PyPegen_expect_soft_keyword",

View File

@ -21,13 +21,18 @@ the python source tree and run:
Alternatively, you can run 'make regen-keyword'.
"""
__all__ = ["iskeyword", "kwlist"]
__all__ = ["iskeyword", "issoftkeyword", "kwlist", "softkwlist"]
kwlist = [
{keywords}
]
softkwlist = [
{soft_keywords}
]
iskeyword = frozenset(kwlist).__contains__
issoftkeyword = frozenset(softkwlist).__contains__
'''.lstrip()
EXTRA_KEYWORDS = ["async", "await"]
@ -60,9 +65,11 @@ def main():
with args.keyword_file as thefile:
all_keywords = sorted(list(gen.callmakervisitor.keyword_cache.keys()) + EXTRA_KEYWORDS)
all_soft_keywords = sorted(gen.callmakervisitor.soft_keywords)
keywords = ",\n ".join(map(repr, all_keywords))
thefile.write(TEMPLATE.format(keywords=keywords))
keywords = " " + ",\n ".join(map(repr, all_keywords))
soft_keywords = " " + ",\n ".join(map(repr, all_soft_keywords))
thefile.write(TEMPLATE.format(keywords=keywords, soft_keywords=soft_keywords))
if __name__ == "__main__":