blob: 3d358a3d7743f5dfdc39890ea6307cca9966baa6 [file] [log] [blame]
Guido van Rossum47478871996-08-21 14:32:37 +00001#! /usr/bin/env python
2#
3# Tokens (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# PYTHONPATH=./Lib ./python Lib/token.py
11#
12# (this path allows the import of string.py and regexmodule.so
13# for a site with no installation in place)
Guido van Rossumb31c7f71993-11-11 10:31:23 +000014
Guido van Rossum47478871996-08-21 14:32:37 +000015#--start constants--
Guido van Rossumb31c7f71993-11-11 10:31:23 +000016ENDMARKER = 0
17NAME = 1
18NUMBER = 2
19STRING = 3
20NEWLINE = 4
21INDENT = 5
22DEDENT = 6
23LPAR = 7
24RPAR = 8
25LSQB = 9
26RSQB = 10
27COLON = 11
28COMMA = 12
29SEMI = 13
30PLUS = 14
31MINUS = 15
32STAR = 16
33SLASH = 17
34VBAR = 18
35AMPER = 19
36LESS = 20
37GREATER = 21
38EQUAL = 22
39DOT = 23
40PERCENT = 24
41BACKQUOTE = 25
42LBRACE = 26
43RBRACE = 27
44EQEQUAL = 28
45NOTEQUAL = 29
46LESSEQUAL = 30
47GREATEREQUAL = 31
48TILDE = 32
49CIRCUMFLEX = 33
50LEFTSHIFT = 34
51RIGHTSHIFT = 35
Guido van Rossum154a5391996-07-21 02:17:52 +000052DOUBLESTAR = 36
53OP = 37
54ERRORTOKEN = 38
Guido van Rossum47478871996-08-21 14:32:37 +000055N_TOKENS = 39
56NT_OFFSET = 256
57#--end constants--
Guido van Rossumb31c7f71993-11-11 10:31:23 +000058
Guido van Rossumb31c7f71993-11-11 10:31:23 +000059tok_name = {}
Fred Drakee3dbc7e1997-10-06 21:28:04 +000060for _name, _value in globals().items():
61 if type(_value) is type(0):
62 tok_name[_value] = _name
63
Guido van Rossumb31c7f71993-11-11 10:31:23 +000064
Guido van Rossum154a5391996-07-21 02:17:52 +000065def ISTERMINAL(x):
66 return x < NT_OFFSET
67
68def ISNONTERMINAL(x):
69 return x >= NT_OFFSET
70
71def ISEOF(x):
72 return x == ENDMARKER
Guido van Rossum47478871996-08-21 14:32:37 +000073
74
75def main():
76 import regex
77 import string
78 import sys
79 args = sys.argv[1:]
80 inFileName = args and args[0] or "Include/token.h"
81 outFileName = "Lib/token.py"
82 if len(args) > 1:
83 outFileName = args[1]
84 try:
85 fp = open(inFileName)
86 except IOError, err:
87 sys.stdout.write("I/O error: %s\n" % str(err))
88 sys.exit(1)
89 lines = string.splitfields(fp.read(), "\n")
90 fp.close()
91 re = regex.compile(
92 "#define[ \t][ \t]*\([A-Z][A-Z_]*\)[ \t][ \t]*\([0-9][0-9]*\)",
93 regex.casefold)
94 tokens = {}
95 for line in lines:
96 if re.match(line) > -1:
97 name, val = re.group(1, 2)
98 val = string.atoi(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 = string.splitfields(fp.read(), "\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(string.joinfields(format, "\n"))
126 fp.close()
127
128
129if __name__ == "__main__":
130 main()