• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""Generate Lib/keyword.py from the Grammar and Tokens files using pgen"""
2
3import argparse
4
5from .pgen import ParserGenerator
6
7TEMPLATE = r'''
8"""Keywords (from "Grammar/Grammar")
9
10This file is automatically generated; please don't muck it up!
11
12To update the symbols in this file, 'cd' to the top directory of
13the python source tree and run:
14
15    python3 -m Parser.pgen.keywordgen Grammar/Grammar \
16                                      Grammar/Tokens \
17                                      Lib/keyword.py
18
19Alternatively, you can run 'make regen-keyword'.
20"""
21
22__all__ = ["iskeyword", "kwlist"]
23
24kwlist = [
25    {keywords}
26]
27
28iskeyword = frozenset(kwlist).__contains__
29'''.lstrip()
30
31EXTRA_KEYWORDS = ["async", "await"]
32
33
34def main():
35    parser = argparse.ArgumentParser(
36        description="Generate the Lib/keywords.py " "file from the grammar."
37    )
38    parser.add_argument(
39        "grammar", type=str, help="The file with the grammar definition in EBNF format"
40    )
41    parser.add_argument("tokens", type=str, help="The file with the token definitions")
42    parser.add_argument(
43        "keyword_file",
44        type=argparse.FileType("w"),
45        help="The path to write the keyword definitions",
46    )
47    args = parser.parse_args()
48    p = ParserGenerator(args.grammar, args.tokens)
49    grammar = p.make_grammar()
50
51    with args.keyword_file as thefile:
52        all_keywords = sorted(list(grammar.keywords) + EXTRA_KEYWORDS)
53
54        keywords = ",\n    ".join(map(repr, all_keywords))
55        thefile.write(TEMPLATE.format(keywords=keywords))
56
57
58if __name__ == "__main__":
59    main()
60