Skip to content

Commit

Permalink
Include soft keywords in keyword.py (pythonGH-20877)
Browse files Browse the repository at this point in the history
  • Loading branch information
pablogsal authored Jun 15, 2020
1 parent 10c3b21 commit 78319e3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
7 changes: 6 additions & 1 deletion Lib/keyword.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
Alternatively, you can run 'make regen-keyword'.
"""

__all__ = ["iskeyword", "kwlist"]
__all__ = ["iskeyword", "issoftkeyword", "kwlist", "softkwlist"]

kwlist = [
'False',
Expand Down Expand Up @@ -53,4 +53,9 @@
'yield'
]

softkwlist = [

]

iskeyword = frozenset(kwlist).__contains__
issoftkeyword = frozenset(softkwlist).__contains__
2 changes: 2 additions & 0 deletions Tools/peg_generator/pegen/c_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ def __init__(
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:
Expand All @@ -119,6 +120,7 @@ def keyword_helper(self, keyword: str) -> FunctionCall:
)

def soft_keyword_helper(self, value: str) -> FunctionCall:
self.soft_keywords.add(value.replace('"', ""))
return FunctionCall(
assigned_variable="_keyword",
function="_PyPegen_expect_soft_keyword",
Expand Down
15 changes: 11 additions & 4 deletions Tools/peg_generator/pegen/keywordgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,18 @@
Alternatively, you can run 'make regen-keyword'.
"""
__all__ = ["iskeyword", "kwlist"]
__all__ = ["iskeyword", "issoftkeyword", "kwlist", "softkwlist"]
kwlist = [
{keywords}
{keywords}
]
softkwlist = [
{soft_keywords}
]
iskeyword = frozenset(kwlist).__contains__
issoftkeyword = frozenset(softkwlist).__contains__
'''.lstrip()

EXTRA_KEYWORDS = ["async", "await"]
Expand Down Expand Up @@ -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__":
Expand Down

0 comments on commit 78319e3

Please sign in to comment.