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(): |
| 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 | |
| 59 | if __name__ == "__main__": |
| 60 | main() |