summaryrefslogtreecommitdiffstats
path: root/Tools
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2020-06-11 13:02:37 (GMT)
committerGitHub <noreply@github.com>2020-06-11 13:02:37 (GMT)
commit961edf7979ca34d6fe104a1cce005aa8cac35821 (patch)
treea5c62b1fea64a1fa70a4566e306cf9e95ffedcac /Tools
parentf6428babb1b9172cc01145d4c091b91dc601d969 (diff)
downloadcpython-961edf7979ca34d6fe104a1cce005aa8cac35821.zip
cpython-961edf7979ca34d6fe104a1cce005aa8cac35821.tar.gz
cpython-961edf7979ca34d6fe104a1cce005aa8cac35821.tar.bz2
bpo-40939: Generate keyword.py using the new parser (GH-20800)
(cherry picked from commit 9727694f08cad4b019d2939224e3416312b1c0e1) Co-authored-by: Lysandros Nikolaou <lisandrosnik@gmail.com>
Diffstat (limited to 'Tools')
-rw-r--r--Tools/peg_generator/pegen/keywordgen.py73
1 files changed, 73 insertions, 0 deletions
diff --git a/Tools/peg_generator/pegen/keywordgen.py b/Tools/peg_generator/pegen/keywordgen.py
new file mode 100644
index 0000000..279c34b
--- /dev/null
+++ b/Tools/peg_generator/pegen/keywordgen.py
@@ -0,0 +1,73 @@
+"""Generate Lib/keyword.py from the Grammar and Tokens files using pgen"""
+
+import argparse
+
+from .build import build_parser, generate_token_definitions
+from .c_generator import CParserGenerator
+
+TEMPLATE = r'''
+"""Keywords (from "Grammar/python.gram")
+
+This file is automatically generated; please don't muck it up!
+
+To update the symbols in this file, 'cd' to the top directory of
+the python source tree and run:
+
+ PYTHONPATH=Tools/peg_generator python3 -m pegen.keywordgen \
+ Grammar/Grammar \
+ Grammar/Tokens \
+ Lib/keyword.py
+
+Alternatively, you can run 'make regen-keyword'.
+"""
+
+__all__ = ["iskeyword", "kwlist"]
+
+kwlist = [
+ {keywords}
+]
+
+iskeyword = frozenset(kwlist).__contains__
+'''.lstrip()
+
+EXTRA_KEYWORDS = ["async", "await"]
+
+
+def main():
+ parser = argparse.ArgumentParser(
+ description="Generate the Lib/keywords.py file from the grammar."
+ )
+ parser.add_argument(
+ "grammar", type=str, help="The file with the grammar definition in PEG format"
+ )
+ parser.add_argument(
+ "tokens_file",
+ type=argparse.FileType("r"),
+ help="The file with the token definitions"
+ )
+ parser.add_argument(
+ "keyword_file",
+ type=argparse.FileType("w"),
+ help="The path to write the keyword definitions",
+ )
+ args = parser.parse_args()
+
+ grammar, _, _ = build_parser(args.grammar)
+ with args.tokens_file as tok_file:
+ all_tokens, exact_tok, non_exact_tok = generate_token_definitions(tok_file)
+ gen: ParserGenerator = CParserGenerator(
+ grammar, all_tokens, exact_tok, non_exact_tok, file=None
+ )
+ gen.collect_todo()
+
+ with args.keyword_file as thefile:
+ all_keywords = sorted(
+ list(gen.callmakervisitor.keyword_cache.keys()) + EXTRA_KEYWORDS
+ )
+
+ keywords = ",\n ".join(map(repr, all_keywords))
+ thefile.write(TEMPLATE.format(keywords=keywords))
+
+
+if __name__ == "__main__":
+ main()