Pablo Galindo | 91759d9 | 2019-03-25 22:01:12 +0000 | [diff] [blame] | 1 | """Generate Lib/keyword.py from the Grammar and Tokens files using pgen""" |
| 2 | |
| 3 | import argparse |
| 4 | |
| 5 | from .pgen import ParserGenerator |
| 6 | |
| 7 | TEMPLATE = r''' |
| 8 | """Keywords (from "Grammar/Grammar") |
| 9 | |
| 10 | This file is automatically generated; please don't muck it up! |
| 11 | |
| 12 | To update the symbols in this file, 'cd' to the top directory of |
| 13 | the python source tree and run: |
| 14 | |
| 15 | python3 -m Parser.pgen.keywordgen Grammar/Grammar \ |
| 16 | Grammar/Tokens \ |
| 17 | Lib/keyword.py |
| 18 | |
| 19 | Alternatively, you can run 'make regen-keyword'. |
| 20 | """ |
| 21 | |
| 22 | __all__ = ["iskeyword", "kwlist"] |
| 23 | |
| 24 | kwlist = [ |
| 25 | {keywords} |
| 26 | ] |
| 27 | |
| 28 | iskeyword = frozenset(kwlist).__contains__ |
| 29 | '''.lstrip() |
| 30 | |
| 31 | EXTRA_KEYWORDS = ["async", "await"] |
| 32 | |
| 33 | |
| 34 | def main(): |
Pablo Galindo | 71876fa | 2019-08-22 02:38:39 +0100 | [diff] [blame^] | 35 | parser = argparse.ArgumentParser( |
| 36 | description="Generate the Lib/keywords.py " "file from the grammar." |
| 37 | ) |
Pablo Galindo | 91759d9 | 2019-03-25 22:01:12 +0000 | [diff] [blame] | 38 | parser.add_argument( |
| 39 | "grammar", type=str, help="The file with the grammar definition in EBNF format" |
| 40 | ) |
Pablo Galindo | 71876fa | 2019-08-22 02:38:39 +0100 | [diff] [blame^] | 41 | parser.add_argument("tokens", type=str, help="The file with the token definitions") |
Pablo Galindo | 91759d9 | 2019-03-25 22:01:12 +0000 | [diff] [blame] | 42 | parser.add_argument( |
| 43 | "keyword_file", |
Pablo Galindo | 71876fa | 2019-08-22 02:38:39 +0100 | [diff] [blame^] | 44 | type=argparse.FileType("w"), |
Pablo Galindo | 91759d9 | 2019-03-25 22:01:12 +0000 | [diff] [blame] | 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 | |
| 58 | if __name__ == "__main__": |
| 59 | main() |