Skip to content

Commit 41f4ca9

Browse files
committed
Add token lib
1 parent 9c8d500 commit 41f4ca9

File tree

1 file changed

+143
-0
lines changed

1 file changed

+143
-0
lines changed

grumpy-runtime-src/lib/token.py

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

0 commit comments

Comments
 (0)