forked from JCMoure/RVCAT
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlexer.py
More file actions
86 lines (67 loc) · 2.12 KB
/
Copy pathlexer.py
File metadata and controls
86 lines (67 loc) · 2.12 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from enum import Enum, auto
class TokenType(Enum):
LF = auto()
EOF = auto()
COMMA = auto()
DOT = auto()
COLON = auto()
HASH = auto()
PERCENT = auto()
STR1 = auto()
STR2 = auto()
SYMBOL = auto()
UNDEF = auto()
class Token:
def __init__(self, value: str, type: TokenType) -> None:
self.value = value
self.type = type
def __repr__(self) -> str:
return f"<{self.type}> {self.value}"
def __eq__(self, tk_type: TokenType) -> bool:
return self.type == tk_type
def __add__(self, other) -> str:
return self.value + other.value
class Lexer:
def __init__(self, text: str) -> None:
self.text = text
self.idx = 0
def peek(self) -> str:
if self.idx >= len(self.text):
return "\0"
else:
return self.text[self.idx]
def advance(self) -> None:
self.idx += 1
def next_token(self) -> Token:
while self.peek().isspace() and self.peek() != "\n":
self.advance()
tk_val = self.peek()
tk_type = TokenType.UNDEF
# symbol = re.compile("[a-z,A-Z,0-9,-,%,_,.,(,)]")
if tk_val == "\n":
tk_type = TokenType.LF
elif tk_val == "\0":
tk_type = TokenType.EOF
elif tk_val == ",":
tk_type = TokenType.COMMA
elif tk_val == ".":
tk_type = TokenType.DOT
elif tk_val == ":":
tk_type = TokenType.COLON
elif tk_val == "#":
tk_type = TokenType.HASH
elif tk_val == "%":
tk_type = TokenType.PERCENT
elif tk_val == '"':
tk_type = TokenType.STR1
elif tk_val == "'":
tk_type = TokenType.STR2
elif tk_val.isalnum() or tk_val in ["-", "_", "[", "]", "(", ")"]:
tk_type = TokenType.SYMBOL
self.advance()
while self.peek().isalnum() or self.peek() in ["(", ")", ".", "_", "]", "["]:
tk_val += self.peek()
self.advance()
if tk_type != TokenType.SYMBOL:
self.advance()
return Token(tk_val, tk_type)