blob: f0234a81b62da860bf60af7856ea28be3c5cb2bb [file] [log] [blame]
Pablo Galindo91759d92019-03-25 22:01:12 +00001"""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():
Pablo Galindo71876fa2019-08-22 02:38:39 +010035 parser = argparse.ArgumentParser(
36 description="Generate the Lib/keywords.py " "file from the grammar."
37 )
Pablo Galindo91759d92019-03-25 22:01:12 +000038 parser.add_argument(
39 "grammar", type=str, help="The file with the grammar definition in EBNF format"
40 )
Pablo Galindo71876fa2019-08-22 02:38:39 +010041 parser.add_argument("tokens", type=str, help="The file with the token definitions")
Pablo Galindo91759d92019-03-25 22:01:12 +000042 parser.add_argument(
43 "keyword_file",
Pablo Galindo71876fa2019-08-22 02:38:39 +010044 type=argparse.FileType("w"),
Pablo Galindo91759d92019-03-25 22:01:12 +000045 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()