PageRenderTime 52ms CodeModel.GetById 20ms app.highlight 13ms RepoModel.GetById 17ms app.codeStats 0ms

/Lib/token.py

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