-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtokenizer.py
More file actions
62 lines (55 loc) · 1.73 KB
/
tokenizer.py
File metadata and controls
62 lines (55 loc) · 1.73 KB
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
import collections
import enum
import re
from datatypes import sym
class Token(enum.Enum):
left_paren = '('
right_paren = ')'
quote = '\''
quasiquote = '`'
unquote = ','
def tokenize_str(string):
return string[1:-1].encode('utf8').decode('unicode-escape')
token_types = collections.OrderedDict([
(float, r'(-|\+)?((\d+\.\d*)|(\d*\.\d+))'),
(int, r'(-|\+)?\d+'),
(tokenize_str, r'"(\\"|[^"])*"'),
(lambda x: x=='#t', r'#t|#f'),
(Token, r'[\(\)\',`]'),
(None, r'(\s+|;.*)'),
(sym, r'[^"\'`,\.#;\s\(\)\[\]\{\}]+'),])
def compile_(tokens):
for token, regex in list(tokens.items()):
tokens[token] = re.compile(regex)
compile_(token_types)
def tokenize(string, tokens=None):
if tokens is None:
tokens = token_types
out = []
index = 0
line = 0
while index < len(string):
head = string[index:]
for token, regex in list(tokens.items()):
match = regex.match(head)
if match:
group = match.group()
index += len(group)
line += group.count('\n')
if token is not None:
out.append((token(group), line))
else:
out.append(None)
break
else:
raise SyntaxError("Syntax Error at line %s: %s..."%(line, head[:10]))
for a, b in zip(out, out[1:]):
if a is None or isinstance(a[0], Token):
continue
if b is None or isinstance(b[0], Token):
continue
raise SyntaxError("Syntax Error at line %s: %s..."%(b[1], a[0]))
return list(filter(None, out))
if __name__ == '__main__':
while True:
print((tokenize(raw_input("tokenize> "))))