Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 78319e3

Browse files
authored
Include soft keywords in keyword.py (GH-20877)
1 parent 10c3b21 commit 78319e3

3 files changed

Lines changed: 19 additions & 5 deletions

File tree

Lib/keyword.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
Alternatively, you can run 'make regen-keyword'.
1414
"""
1515

16-
__all__ = ["iskeyword", "kwlist"]
16+
__all__ = ["iskeyword", "issoftkeyword", "kwlist", "softkwlist"]
1717

1818
kwlist = [
1919
'False',
@@ -53,4 +53,9 @@
5353
'yield'
5454
]
5555

56+
softkwlist = [
57+
58+
]
59+
5660
iskeyword = frozenset(kwlist).__contains__
61+
issoftkeyword = frozenset(softkwlist).__contains__

Tools/peg_generator/pegen/c_generator.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ def __init__(
105105
self.non_exact_tokens = non_exact_tokens
106106
self.cache: Dict[Any, FunctionCall] = {}
107107
self.keyword_cache: Dict[str, int] = {}
108+
self.soft_keywords: Set[str] = set()
108109

109110
def keyword_helper(self, keyword: str) -> FunctionCall:
110111
if keyword not in self.keyword_cache:
@@ -119,6 +120,7 @@ def keyword_helper(self, keyword: str) -> FunctionCall:
119120
)
120121

121122
def soft_keyword_helper(self, value: str) -> FunctionCall:
123+
self.soft_keywords.add(value.replace('"', ""))
122124
return FunctionCall(
123125
assigned_variable="_keyword",
124126
function="_PyPegen_expect_soft_keyword",

Tools/peg_generator/pegen/keywordgen.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,18 @@
2121
Alternatively, you can run 'make regen-keyword'.
2222
"""
2323
24-
__all__ = ["iskeyword", "kwlist"]
24+
__all__ = ["iskeyword", "issoftkeyword", "kwlist", "softkwlist"]
2525
2626
kwlist = [
27-
{keywords}
27+
{keywords}
28+
]
29+
30+
softkwlist = [
31+
{soft_keywords}
2832
]
2933
3034
iskeyword = frozenset(kwlist).__contains__
35+
issoftkeyword = frozenset(softkwlist).__contains__
3136
'''.lstrip()
3237

3338
EXTRA_KEYWORDS = ["async", "await"]
@@ -60,9 +65,11 @@ def main():
6065

6166
with args.keyword_file as thefile:
6267
all_keywords = sorted(list(gen.callmakervisitor.keyword_cache.keys()) + EXTRA_KEYWORDS)
68+
all_soft_keywords = sorted(gen.callmakervisitor.soft_keywords)
6369

64-
keywords = ",\n ".join(map(repr, all_keywords))
65-
thefile.write(TEMPLATE.format(keywords=keywords))
70+
keywords = " " + ",\n ".join(map(repr, all_keywords))
71+
soft_keywords = " " + ",\n ".join(map(repr, all_soft_keywords))
72+
thefile.write(TEMPLATE.format(keywords=keywords, soft_keywords=soft_keywords))
6673

6774

6875
if __name__ == "__main__":

0 commit comments

Comments
 (0)