token.py 2.84 KB
Newer Older
Guido van Rossum's avatar
Guido van Rossum committed
1
#! /usr/bin/env python
2 3 4

"""Token constants (from "token.h")."""

Guido van Rossum's avatar
Guido van Rossum committed
5 6 7 8 9
#  This file is automatically generated; please don't muck it up!
#
#  To update the symbols in this file, 'cd' to the top directory of
#  the python source tree after building the interpreter and run:
#
10
#    python Lib/token.py
11

Guido van Rossum's avatar
Guido van Rossum committed
12
#--start constants--
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
ENDMARKER = 0
NAME = 1
NUMBER = 2
STRING = 3
NEWLINE = 4
INDENT = 5
DEDENT = 6
LPAR = 7
RPAR = 8
LSQB = 9
RSQB = 10
COLON = 11
COMMA = 12
SEMI = 13
PLUS = 14
MINUS = 15
STAR = 16
SLASH = 17
VBAR = 18
AMPER = 19
LESS = 20
GREATER = 21
EQUAL = 22
DOT = 23
PERCENT = 24
BACKQUOTE = 25
LBRACE = 26
RBRACE = 27
EQEQUAL = 28
NOTEQUAL = 29
LESSEQUAL = 30
GREATEREQUAL = 31
TILDE = 32
CIRCUMFLEX = 33
LEFTSHIFT = 34
RIGHTSHIFT = 35
49
DOUBLESTAR = 36
50 51 52 53 54 55 56 57 58 59 60
PLUSEQUAL = 37
MINEQUAL = 38
STAREQUAL = 39
SLASHEQUAL = 40
PERCENTEQUAL = 41
AMPEREQUAL = 42
VBAREQUAL = 43
CIRCUMFLEXEQUAL = 44
LEFTSHIFTEQUAL = 45
RIGHTSHIFTEQUAL = 46
DOUBLESTAREQUAL = 47
61 62 63 64 65
DOUBLESLASH = 48
DOUBLESLASHEQUAL = 49
OP = 50
ERRORTOKEN = 51
N_TOKENS = 52
Guido van Rossum's avatar
Guido van Rossum committed
66 67
NT_OFFSET = 256
#--end constants--
68 69

tok_name = {}
70 71
for _name, _value in globals().items():
    if type(_value) is type(0):
72
        tok_name[_value] = _name
73

74

75 76 77 78 79 80 81 82
def ISTERMINAL(x):
    return x < NT_OFFSET

def ISNONTERMINAL(x):
    return x >= NT_OFFSET

def ISEOF(x):
    return x == ENDMARKER
Guido van Rossum's avatar
Guido van Rossum committed
83 84 85


def main():
86
    import re
Guido van Rossum's avatar
Guido van Rossum committed
87 88 89 90 91
    import sys
    args = sys.argv[1:]
    inFileName = args and args[0] or "Include/token.h"
    outFileName = "Lib/token.py"
    if len(args) > 1:
92
        outFileName = args[1]
Guido van Rossum's avatar
Guido van Rossum committed
93
    try:
94
        fp = open(inFileName)
Guido van Rossum's avatar
Guido van Rossum committed
95
    except IOError, err:
96 97
        sys.stdout.write("I/O error: %s\n" % str(err))
        sys.exit(1)
98
    lines = fp.read().split("\n")
Guido van Rossum's avatar
Guido van Rossum committed
99
    fp.close()
100
    prog = re.compile(
101 102
        "#define[ \t][ \t]*([A-Z][A-Z_]*)[ \t][ \t]*([0-9][0-9]*)",
        re.IGNORECASE)
Guido van Rossum's avatar
Guido van Rossum committed
103 104
    tokens = {}
    for line in lines:
105 106 107
        match = prog.match(line)
        if match:
            name, val = match.group(1, 2)
Eric S. Raymond's avatar
Eric S. Raymond committed
108
            val = int(val)
109
            tokens[val] = name          # reverse so we can sort them...
Guido van Rossum's avatar
Guido van Rossum committed
110 111 112 113
    keys = tokens.keys()
    keys.sort()
    # load the output skeleton from the target:
    try:
114
        fp = open(outFileName)
Guido van Rossum's avatar
Guido van Rossum committed
115
    except IOError, err:
116 117
        sys.stderr.write("I/O error: %s\n" % str(err))
        sys.exit(2)
118
    format = fp.read().split("\n")
Guido van Rossum's avatar
Guido van Rossum committed
119 120
    fp.close()
    try:
121 122
        start = format.index("#--start constants--") + 1
        end = format.index("#--end constants--")
Guido van Rossum's avatar
Guido van Rossum committed
123
    except ValueError:
124 125
        sys.stderr.write("target does not contain format markers")
        sys.exit(3)
Guido van Rossum's avatar
Guido van Rossum committed
126 127
    lines = []
    for val in keys:
128
        lines.append("%s = %d" % (tokens[val], val))
Guido van Rossum's avatar
Guido van Rossum committed
129 130
    format[start:end] = lines
    try:
131
        fp = open(outFileName, 'w')
Guido van Rossum's avatar
Guido van Rossum committed
132
    except IOError, err:
133 134
        sys.stderr.write("I/O error: %s\n" % str(err))
        sys.exit(4)
135
    fp.write("\n".join(format))
Guido van Rossum's avatar
Guido van Rossum committed
136 137 138 139 140
    fp.close()


if __name__ == "__main__":
    main()