• 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(description="Generate the Lib/keywords.py "
36                                                 "file from the grammar.")
37    parser.add_argument(
38        "grammar", type=str, help="The file with the grammar definition in EBNF format"
39    )
40    parser.add_argument(
41        "tokens", type=str, help="The file with the token definitions"
42    )
43    parser.add_argument(
44        "keyword_file",
45        type=argparse.FileType('w'),
46        help="The path to write the keyword definitions",
47    )
48    args = parser.parse_args()
49    p = ParserGenerator(args.grammar, args.tokens)
50    grammar = p.make_grammar()
51
52    with args.keyword_file as thefile:
53        all_keywords = sorted(list(grammar.keywords) + EXTRA_KEYWORDS)
54
55        keywords = ",\n    ".join(map(repr, all_keywords))
56        thefile.write(TEMPLATE.format(keywords=keywords))
57
58
59if __name__ == "__main__":
60    main()
61