keyword.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #! /usr/bin/env python3
  2. """Keywords (from "graminit.c")
  3. This file is automatically generated; please don't muck it up!
  4. To update the symbols in this file, 'cd' to the top directory of
  5. the python source tree after building the interpreter and run:
  6. ./python Lib/keyword.py
  7. """
  8. __all__ = ["iskeyword", "kwlist"]
  9. kwlist = [
  10. #--start keywords--
  11. 'False',
  12. 'None',
  13. 'True',
  14. 'and',
  15. 'as',
  16. 'assert',
  17. 'async',
  18. 'await',
  19. 'break',
  20. 'class',
  21. 'continue',
  22. 'def',
  23. 'del',
  24. 'elif',
  25. 'else',
  26. 'except',
  27. 'finally',
  28. 'for',
  29. 'from',
  30. 'global',
  31. 'if',
  32. 'import',
  33. 'in',
  34. 'is',
  35. 'lambda',
  36. 'nonlocal',
  37. 'not',
  38. 'or',
  39. 'pass',
  40. 'raise',
  41. 'return',
  42. 'try',
  43. 'while',
  44. 'with',
  45. 'yield',
  46. #--end keywords--
  47. ]
  48. iskeyword = frozenset(kwlist).__contains__
  49. def main():
  50. import sys, re
  51. args = sys.argv[1:]
  52. iptfile = args and args[0] or "Python/graminit.c"
  53. if len(args) > 1: optfile = args[1]
  54. else: optfile = "Lib/keyword.py"
  55. # load the output skeleton from the target, taking care to preserve its
  56. # newline convention.
  57. with open(optfile, newline='') as fp:
  58. format = fp.readlines()
  59. nl = format[0][len(format[0].strip()):] if format else '\n'
  60. # scan the source file for keywords
  61. with open(iptfile) as fp:
  62. strprog = re.compile('"([^"]+)"')
  63. lines = []
  64. for line in fp:
  65. if '{1, "' in line:
  66. match = strprog.search(line)
  67. if match:
  68. lines.append(" '" + match.group(1) + "'," + nl)
  69. lines.sort()
  70. # insert the lines of keywords into the skeleton
  71. try:
  72. start = format.index("#--start keywords--" + nl) + 1
  73. end = format.index("#--end keywords--" + nl)
  74. format[start:end] = lines
  75. except ValueError:
  76. sys.stderr.write("target does not contain format markers\n")
  77. sys.exit(1)
  78. # write the output file
  79. with open(optfile, 'w', newline='') as fp:
  80. fp.writelines(format)
  81. if __name__ == "__main__":
  82. main()