-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtokenize.py
executable file
·160 lines (152 loc) · 4.82 KB
/
tokenize.py
1
2
3
4
5
6
7
8
9
10
11
12
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
from collections import OrderedDict
from process import ROPSyntaxError
import re
def linesToTokens():
pass
def has(data, offset, pattern):
'''
Returns true if, at the given offset, you can read 'pattern'
from data.
'''
if offset + len(pattern) > len(data): return False
return data[offset:offset+len(pattern)] == pattern
def grabWhileIn(data, offset, pattern):
'''
Returns a variable length substring of data, starting from offset,
containing only the characters in pattern.
'''
return grabUntil(data, offset, lambda data,i: data[i] not in pattern)
def grabUntil(data, offset, fun):
'''
Returns a variable length substring of data, starting from offset,
containing only the characters in pattern.
'''
max_len = len(data)-offset
end = len(data)
for i in range(offset, offset + max_len):
if fun(data, i):
end = i
break
return data[offset:end]
def mktok(tok, char, location=None, value=None,force=False):
base = {"name" : tok, "chr" : char, }
if location: base["location"] = {"line" : location[0], "char" : location[1]}
if value or force: base["value"] = value
return base
IDENTIFIER_TOKENS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_"
NUMBER_TOKENS = "0123456789"
HEX_TOKENS = NUMBER_TOKENS + "abcdefABCDEF"
def tokenize(corpus):
# Load tokens, in order.
collected_tokens = []
i = 0
line = 0
char = 0
while i < len(corpus):
if corpus[i] == '\n':
char, line = 0, line + 1
i = i + 1
elif corpus[i] == ";":
collected_tokens.append(mktok("EOL", i, location=(line,char)))
i = i + 1
char = char + 1
elif corpus[i] == "$":
collected_tokens.append(mktok("deref", i, location=(line,char)))
i = i + 1
char = char + 1
elif corpus[i] == '(':
collected_tokens.append(mktok("start_apply", i, location=(line,char)))
i = i + 1
char = char + 1
elif corpus[i] == ')':
collected_tokens.append(mktok("end_apply", i, location=(line,char)))
i = i + 1
char = char + 1
elif corpus[i] == ',':
collected_tokens.append(mktok("arglist_separator", i, location=(line,char)))
i = i + 1
char = char + 1
elif corpus[i] == "=" and not has(corpus, i, "=="):
collected_tokens.append(mktok("assign", i, location=(line,char)))
i = i + 1
char = char + 1
elif corpus[i] == "+":
collected_tokens.append(mktok("ma_add", i, location=(line,char)))
i = i + 1
char = char + 1
elif corpus[i] == "-":
collected_tokens.append(mktok("ma_subtract", i, location=(line,char)))
i = i + 1
char = char + 1
elif corpus[i] == "*":
collected_tokens.append(mktok("ma_multiply", i, location=(line,char)))
i = i + 1
char = char + 1
elif has(corpus, i, "=="):
collected_tokens.append(mktok("equals", i, location=(line,char)))
i = i + 2
char = char + 2
elif has(corpus, i, "//"):
i = i + 2 # advance past the comment marker.
char = char + 2
val = grabUntil(corpus, i, lambda data,i: data[i] == "\n")
collected_tokens.append(mktok("single_line_comment", i, location=(line,char), value=val))
i = i + len(val)
char = char + len(val)
elif has(corpus, i, "/*"):
i = i + 2
char = char + 2
val = grabUntil(corpus, i, lambda data,i: data[i] == "/" and data[i-1] == "*")[:-1]
collected_tokens.append(mktok("multi_line_comment", i, location=(line,char), value=val))
i = i + len(val) + 2
char = char + len(val) + 2
elif has(corpus, i, "let"):
collected_tokens.append(mktok("let",i,location=(line,char)))
i = i + 3
char = char + 3
elif has(corpus, i, "if"):
collected_tokens.append(mktok("if",i,location=(line,char)))
i = i + 2
char = char + 2
elif has(corpus, i, "while"):
collected_tokens.append(mktok("while",i,location=(line,char)))
i = i + 5
char = char + 5
elif not corpus[i].isspace():
# Read constants + identifiers.
val = None
val_class = None
val_len = 0
if corpus[i] in IDENTIFIER_TOKENS:
val = grabWhileIn(corpus, i, IDENTIFIER_TOKENS)
val_class = "identifier"
val_len = len(val)
elif has(corpus, i, "0x"):
i = i + 2
val = grabWhileIn(corpus, i, HEX_TOKENS)
val_class = "constant_hexadecimal"
val_len = len(val)
val = int(val, 16)
elif corpus[i] in NUMBER_TOKENS:
val = grabWhileIn(corpus, i, NUMBER_TOKENS)
val_len = len(val)
val = int(val)
val_class = "constant_numerical"
elif corpus[i] == "\"":
i = i + 1
char = char + 1
val = grabUntil(corpus, i, lambda data, i: data[i] == '"' and data[i-1] != "\\")
val_len = len(val)
val_class = "constant_string"
i = i + 1
char = char + 1
else:
raise ROPSyntaxError("Unexpected token {}".format(corpus[i]), {"line":line,"char":char})
i = i + val_len
collected_tokens.append(mktok(val_class,i,location=(line,char),value=val,force=True))
char = char + val_len
else:
i = i + 1
char = char + 1
collected_tokens.append(mktok("EOF", i, location=(line,char)))
return collected_tokens