keyword.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. 'break',
  18. 'class',
  19. 'continue',
  20. 'def',
  21. 'del',
  22. 'elif',
  23. 'else',
  24. 'except',
  25. 'finally',
  26. 'for',
  27. 'from',
  28. 'global',
  29. 'if',
  30. 'import',
  31. 'in',
  32. 'is',
  33. 'lambda',
  34. 'nonlocal',
  35. 'not',
  36. 'or',
  37. 'pass',
  38. 'raise',
  39. 'return',
  40. 'try',
  41. 'while',
  42. 'with',
  43. 'yield',
  44. #--end keywords--
  45. ]
  46. iskeyword = frozenset(kwlist).__contains__
  47. def main():
  48. import sys, re
  49. args = sys.argv[1:]
  50. iptfile = args and args[0] or "Python/graminit.c"
  51. if len(args) > 1: optfile = args[1]
  52. else: optfile = "Lib/keyword.py"
  53. # load the output skeleton from the target, taking care to preserve its
  54. # newline convention.
  55. with open(optfile, newline='') as fp:
  56. format = fp.readlines()
  57. nl = format[0][len(format[0].strip()):] if format else '\n'
  58. # scan the source file for keywords
  59. with open(iptfile) as fp:
  60. strprog = re.compile('"([^"]+)"')
  61. lines = []
  62. for line in fp:
  63. if '{1, "' in line:
  64. match = strprog.search(line)
  65. if match:
  66. lines.append(" '" + match.group(1) + "'," + nl)
  67. lines.sort()
  68. # insert the lines of keywords into the skeleton
  69. try:
  70. start = format.index("#--start keywords--" + nl) + 1
  71. end = format.index("#--end keywords--" + nl)
  72. format[start:end] = lines
  73. except ValueError:
  74. sys.stderr.write("target does not contain format markers\n")
  75. sys.exit(1)
  76. # write the output file
  77. with open(optfile, 'w', newline='') as fp:
  78. fp.writelines(format)
  79. if __name__ == "__main__":
  80. main()