-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsptokenizer.py
39 lines (29 loc) · 860 Bytes
/
sptokenizer.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
def tokenize(source):
tokens = []
head = 0
def is_blank(char):
return char in (' ', '\n')
def is_delimiter(char):
return char in ('(', ')', '\'')
def next_token():
nonlocal head
drop_whitespace()
begin = head
if head >= len(source):
return None
if is_delimiter(source[head]):
head += 1
return source[head - 1]
else:
while head < len(source) and not is_blank(source[head]) and not is_delimiter(source[head]):
head += 1
return source[begin:head]
def drop_whitespace():
nonlocal head
while head < len(source) and is_blank(source[head]):
head += 1
t = next_token()
while t is not None:
tokens.append(t)
t = next_token()
return tokens