PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/Python/system/token.py

https://bitbucket.org/cwalther/moulscript-dlanor
Python | 140 lines | 135 code | 3 blank | 2 comment | 11 complexity | d41afb21392bf9a2f43c54eea21e723b MD5 | raw file
Possible License(s): AGPL-1.0, GPL-3.0
  1. #! /usr/bin/env python
  2. """Token constants (from "token.h")."""
  3. # This file is automatically generated; please don't muck it up!
  4. #
  5. # To update the symbols in this file, 'cd' to the top directory of
  6. # the python source tree after building the interpreter and run:
  7. #
  8. # python Lib/token.py
  9. #--start constants--
  10. ENDMARKER = 0
  11. NAME = 1
  12. NUMBER = 2
  13. STRING = 3
  14. NEWLINE = 4
  15. INDENT = 5
  16. DEDENT = 6
  17. LPAR = 7
  18. RPAR = 8
  19. LSQB = 9
  20. RSQB = 10
  21. COLON = 11
  22. COMMA = 12
  23. SEMI = 13
  24. PLUS = 14
  25. MINUS = 15
  26. STAR = 16
  27. SLASH = 17
  28. VBAR = 18
  29. AMPER = 19
  30. LESS = 20
  31. GREATER = 21
  32. EQUAL = 22
  33. DOT = 23
  34. PERCENT = 24
  35. BACKQUOTE = 25
  36. LBRACE = 26
  37. RBRACE = 27
  38. EQEQUAL = 28
  39. NOTEQUAL = 29
  40. LESSEQUAL = 30
  41. GREATEREQUAL = 31
  42. TILDE = 32
  43. CIRCUMFLEX = 33
  44. LEFTSHIFT = 34
  45. RIGHTSHIFT = 35
  46. DOUBLESTAR = 36
  47. PLUSEQUAL = 37
  48. MINEQUAL = 38
  49. STAREQUAL = 39
  50. SLASHEQUAL = 40
  51. PERCENTEQUAL = 41
  52. AMPEREQUAL = 42
  53. VBAREQUAL = 43
  54. CIRCUMFLEXEQUAL = 44
  55. LEFTSHIFTEQUAL = 45
  56. RIGHTSHIFTEQUAL = 46
  57. DOUBLESTAREQUAL = 47
  58. DOUBLESLASH = 48
  59. DOUBLESLASHEQUAL = 49
  60. OP = 50
  61. ERRORTOKEN = 51
  62. N_TOKENS = 52
  63. NT_OFFSET = 256
  64. #--end constants--
  65. tok_name = {}
  66. for _name, _value in globals().items():
  67. if type(_value) is type(0):
  68. tok_name[_value] = _name
  69. def ISTERMINAL(x):
  70. return x < NT_OFFSET
  71. def ISNONTERMINAL(x):
  72. return x >= NT_OFFSET
  73. def ISEOF(x):
  74. return x == ENDMARKER
  75. def main():
  76. import re
  77. import sys
  78. args = sys.argv[1:]
  79. inFileName = args and args[0] or "Include/token.h"
  80. outFileName = "Lib/token.py"
  81. if len(args) > 1:
  82. outFileName = args[1]
  83. try:
  84. fp = open(inFileName)
  85. except IOError, err:
  86. sys.stdout.write("I/O error: %s\n" % str(err))
  87. sys.exit(1)
  88. lines = fp.read().split("\n")
  89. fp.close()
  90. prog = re.compile(
  91. "#define[ \t][ \t]*([A-Z0-9][A-Z0-9_]*)[ \t][ \t]*([0-9][0-9]*)",
  92. re.IGNORECASE)
  93. tokens = {}
  94. for line in lines:
  95. match = prog.match(line)
  96. if match:
  97. name, val = match.group(1, 2)
  98. val = int(val)
  99. tokens[val] = name # reverse so we can sort them...
  100. keys = tokens.keys()
  101. keys.sort()
  102. # load the output skeleton from the target:
  103. try:
  104. fp = open(outFileName)
  105. except IOError, err:
  106. sys.stderr.write("I/O error: %s\n" % str(err))
  107. sys.exit(2)
  108. format = fp.read().split("\n")
  109. fp.close()
  110. try:
  111. start = format.index("#--start constants--") + 1
  112. end = format.index("#--end constants--")
  113. except ValueError:
  114. sys.stderr.write("target does not contain format markers")
  115. sys.exit(3)
  116. lines = []
  117. for val in keys:
  118. lines.append("%s = %d" % (tokens[val], val))
  119. format[start:end] = lines
  120. try:
  121. fp = open(outFileName, 'w')
  122. except IOError, err:
  123. sys.stderr.write("I/O error: %s\n" % str(err))
  124. sys.exit(4)
  125. fp.write("\n".join(format))
  126. fp.close()
  127. if __name__ == "__main__":
  128. main()