Skip to content

Commit 961edf7

Browse files
bpo-40939: Generate keyword.py using the new parser (GH-20800)
(cherry picked from commit 9727694) Co-authored-by: Lysandros Nikolaou <[email protected]>
1 parent f6428ba commit 961edf7

File tree

6 files changed

+88
-9
lines changed

6 files changed

+88
-9
lines changed

Lib/keyword.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
"""Keywords (from "Grammar/Grammar")
1+
"""Keywords (from "Grammar/python.gram")
22
33
This file is automatically generated; please don't muck it up!
44
55
To update the symbols in this file, 'cd' to the top directory of
66
the python source tree and run:
77
8-
python3 -m Parser.pgen.keywordgen Grammar/Grammar \
9-
Grammar/Tokens \
10-
Lib/keyword.py
8+
PYTHONPATH=Tools/peg_generator python3 -m pegen.keywordgen \
9+
Grammar/Grammar \
10+
Grammar/Tokens \
11+
Lib/keyword.py
1112
1213
Alternatively, you can run 'make regen-keyword'.
1314
"""
@@ -18,6 +19,7 @@
1819
'False',
1920
'None',
2021
'True',
22+
'__new_parser__',
2123
'and',
2224
'as',
2325
'assert',

Lib/pydoc.py

+1
Original file line numberDiff line numberDiff line change
@@ -1817,6 +1817,7 @@ class Helper:
18171817
'False': '',
18181818
'None': '',
18191819
'True': '',
1820+
'__new_parser__': '',
18201821
'and': 'BOOLEAN',
18211822
'as': 'with',
18221823
'assert': ('assert', ''),

Makefile.pre.in

+4-3
Original file line numberDiff line numberDiff line change
@@ -895,9 +895,10 @@ regen-token:
895895

896896
.PHONY: regen-keyword
897897
regen-keyword:
898-
# Regenerate Lib/keyword.py from Grammar/Grammar and Grammar/Tokens
899-
# using Parser/pgen
900-
PYTHONPATH=$(srcdir) $(PYTHON_FOR_REGEN) -m Parser.pgen.keywordgen $(srcdir)/Grammar/Grammar \
898+
# Regenerate Lib/keyword.py from Grammar/python.gram and Grammar/Tokens
899+
# using Tools/peg_generator/pegen
900+
PYTHONPATH=$(srcdir)/Tools/peg_generator $(PYTHON_FOR_REGEN) -m pegen.keywordgen \
901+
$(srcdir)/Grammar/python.gram \
901902
$(srcdir)/Grammar/Tokens \
902903
$(srcdir)/Lib/keyword.py.new
903904
$(UPDATE_FILE) $(srcdir)/Lib/keyword.py $(srcdir)/Lib/keyword.py.new
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Use the new PEG parser when generating the stdlib :mod:`keyword` module.

PCbuild/regen.vcxproj

+3-2
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,9 @@
205205
<Exec Command="&quot;$(PythonExe)&quot; $(PySourcePath)Tools\scripts\generate_token.py py &quot;$(PySourcePath)Grammar\Tokens&quot; &quot;$(PySourcePath)Lib\token.py&quot;" />
206206
</Target>
207207
<Target Name="_RegenKeywords" AfterTargets="_RegenTokens">
208-
<!-- Regenerate Lib/keyword.py from Grammar/Grammar and Grammar/Tokens using Parser/pgen-->
209-
<Exec Command="&quot;$(PythonExe)&quot; -m Parser.pgen.keywordgen &quot;$(PySourcePath)Grammar\Grammar&quot; &quot;$(PySourcePath)Grammar\Tokens&quot; &quot;$(IntDir)keyword.py&quot;" />
208+
<!-- Regenerate Lib/keyword.py from Grammar/python.gram and Grammar/Tokens using Tools/peg_generator/pegen-->
209+
<SetEnv Name="PYTHONPATH" Prefix="true" Value="$(PySourcePath)Tools\peg_generator\" />
210+
<Exec Command="&quot;$(PythonExe)&quot; -m pegen.keywordgen &quot;$(PySourcePath)Grammar\python.gram&quot; &quot;$(PySourcePath)Grammar\Tokens&quot; &quot;$(IntDir)keyword.py&quot;" />
210211
<Copy SourceFiles="$(IntDir)keyword.py" DestinationFiles="$(PySourcePath)Lib\keyword.py">
211212
<Output TaskParameter="CopiedFiles" ItemName="_Updated" />
212213
</Copy>
+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
"""Generate Lib/keyword.py from the Grammar and Tokens files using pgen"""
2+
3+
import argparse
4+
5+
from .build import build_parser, generate_token_definitions
6+
from .c_generator import CParserGenerator
7+
8+
TEMPLATE = r'''
9+
"""Keywords (from "Grammar/python.gram")
10+
11+
This file is automatically generated; please don't muck it up!
12+
13+
To update the symbols in this file, 'cd' to the top directory of
14+
the python source tree and run:
15+
16+
PYTHONPATH=Tools/peg_generator python3 -m pegen.keywordgen \
17+
Grammar/Grammar \
18+
Grammar/Tokens \
19+
Lib/keyword.py
20+
21+
Alternatively, you can run 'make regen-keyword'.
22+
"""
23+
24+
__all__ = ["iskeyword", "kwlist"]
25+
26+
kwlist = [
27+
{keywords}
28+
]
29+
30+
iskeyword = frozenset(kwlist).__contains__
31+
'''.lstrip()
32+
33+
EXTRA_KEYWORDS = ["async", "await"]
34+
35+
36+
def main():
37+
parser = argparse.ArgumentParser(
38+
description="Generate the Lib/keywords.py file from the grammar."
39+
)
40+
parser.add_argument(
41+
"grammar", type=str, help="The file with the grammar definition in PEG format"
42+
)
43+
parser.add_argument(
44+
"tokens_file",
45+
type=argparse.FileType("r"),
46+
help="The file with the token definitions"
47+
)
48+
parser.add_argument(
49+
"keyword_file",
50+
type=argparse.FileType("w"),
51+
help="The path to write the keyword definitions",
52+
)
53+
args = parser.parse_args()
54+
55+
grammar, _, _ = build_parser(args.grammar)
56+
with args.tokens_file as tok_file:
57+
all_tokens, exact_tok, non_exact_tok = generate_token_definitions(tok_file)
58+
gen: ParserGenerator = CParserGenerator(
59+
grammar, all_tokens, exact_tok, non_exact_tok, file=None
60+
)
61+
gen.collect_todo()
62+
63+
with args.keyword_file as thefile:
64+
all_keywords = sorted(
65+
list(gen.callmakervisitor.keyword_cache.keys()) + EXTRA_KEYWORDS
66+
)
67+
68+
keywords = ",\n ".join(map(repr, all_keywords))
69+
thefile.write(TEMPLATE.format(keywords=keywords))
70+
71+
72+
if __name__ == "__main__":
73+
main()

0 commit comments

Comments
 (0)