-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlexer.py
More file actions
30 lines (24 loc) · 692 Bytes
/
Copy pathlexer.py
File metadata and controls
30 lines (24 loc) · 692 Bytes
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
import typing as t
OPERATOR_SET = frozenset(("(", ")", "!", "+", "|", "^", "=>", "<=>"))
def lex(stream: str) -> t.List[str]:
i = 0
tokens = []
N = len(stream)
while i < N:
for s in OPERATOR_SET:
if stream[i:].startswith(s):
i += len(s)
tokens.append(s)
break
else:
if stream[i] == "#":
return tokens
if stream[i].isspace():
pass
else:
tokens.append(stream[i])
i += 1
return tokens
def lex_file(filename: str) -> t.List[t.List[str]]:
with open(filename) as f:
return [*map(lex, f)]