You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
2.2 KiB

  1. """Generate Lib/keyword.py from the Grammar and Tokens files using pgen"""
  2. import argparse
  3. from .build import build_parser, generate_token_definitions
  4. from .c_generator import CParserGenerator
  5. TEMPLATE = r'''
  6. """Keywords (from "Grammar/python.gram")
  7. This file is automatically generated; please don't muck it up!
  8. To update the symbols in this file, 'cd' to the top directory of
  9. the python source tree and run:
  10. PYTHONPATH=Tools/peg_generator python3 -m pegen.keywordgen \
  11. Grammar/python.gram \
  12. Grammar/Tokens \
  13. Lib/keyword.py
  14. Alternatively, you can run 'make regen-keyword'.
  15. """
  16. __all__ = ["iskeyword", "issoftkeyword", "kwlist", "softkwlist"]
  17. kwlist = [
  18. {keywords}
  19. ]
  20. softkwlist = [
  21. {soft_keywords}
  22. ]
  23. iskeyword = frozenset(kwlist).__contains__
  24. issoftkeyword = frozenset(softkwlist).__contains__
  25. '''.lstrip()
  26. EXTRA_KEYWORDS = ["async", "await"]
  27. def main() -> None:
  28. parser = argparse.ArgumentParser(
  29. description="Generate the Lib/keywords.py file from the grammar."
  30. )
  31. parser.add_argument(
  32. "grammar", type=str, help="The file with the grammar definition in PEG format"
  33. )
  34. parser.add_argument(
  35. "tokens_file", type=argparse.FileType("r"), help="The file with the token definitions"
  36. )
  37. parser.add_argument(
  38. "keyword_file",
  39. type=argparse.FileType("w"),
  40. help="The path to write the keyword definitions",
  41. )
  42. args = parser.parse_args()
  43. grammar, _, _ = build_parser(args.grammar)
  44. with args.tokens_file as tok_file:
  45. all_tokens, exact_tok, non_exact_tok = generate_token_definitions(tok_file)
  46. gen = CParserGenerator(grammar, all_tokens, exact_tok, non_exact_tok, file=None)
  47. gen.collect_rules()
  48. with args.keyword_file as thefile:
  49. all_keywords = sorted(list(gen.keywords.keys()) + EXTRA_KEYWORDS)
  50. all_soft_keywords = sorted(gen.soft_keywords)
  51. keywords = "" if not all_keywords else " " + ",\n ".join(map(repr, all_keywords))
  52. soft_keywords = (
  53. "" if not all_soft_keywords else " " + ",\n ".join(map(repr, all_soft_keywords))
  54. )
  55. thefile.write(TEMPLATE.format(keywords=keywords, soft_keywords=soft_keywords))
  56. if __name__ == "__main__":
  57. main()