-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.py
197 lines (183 loc) · 4.35 KB
/
lexer.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
class Token:
def __init__(self, token, typef="identifer", start=0, end=0):
self.value = token
self.start = start
self.end = end
self.type = typef
def equals(self, tokOrType, value=""):
if type(tokOrType) == Token:
return tokOrType.type == self.type and tokOrType.value == self.value
return tokOrType == self.type and value == self.value
def not_equals(self, tokOrType, value=""):
return not self.equals(tokOrType, value)
def __repr__(self):
return f"Token({self.type}, {self.value}, from {self.start} to {self.end})"
return f"\n-----------\nTOKEN (\n\tType:{self.type},\n\tValue:{self.value},\n\tfrom {self.start} to {self.end}\n)\n-----------"
from error import Error
import string
class Lexer:
def __init__(self, code):
self.code = code
self.ind = 0
self.cur = self.code[self.ind] if self.ind < len(self.code) else None
self.DIGITS = "01234567890"
self.LETTERS = string.ascii_letters + "_"
self.SPECIAL_KEYWORD_LETTERS = "?"
self.keywords = [
"refard",
"farding",
"fards?",
"fard",
"farded"
]
self.other = [
{
"name": "BOOL",
"match": "true"
},
{
"name": "BOOL",
"match": "false"
},
{
"name": "LOGICAL_OP",
"match": "=="
},
{
"name": "LOGICAL_OP",
"match": ">="
},
{
"name": "LOGICAL_OP",
"match": "<="
},{
"name": "LOGICAL_OP",
"match": ">"
},
{
"name": "LOGICAL_OP",
"match": "<"
},
{
"name": "LOGICAL_OP",
"match": "!="
},
{
"name": "SPECIAL",
"match": "&"
},
{
"name": "VARIABLE_MODIFY",
"match": "+="
},
{
"name": "VARIABLE_MODIFY",
"match": "-="
},
{
"name": "VARIABLE_MODIFY",
"match": "*="
},
{
"name": "VARIABLE_MODIFY",
"match": "/="
},
{
"name": "PLUS",
"match": "+"
},
{
"name": "MINUS",
"match": "-"
},
{
"name": "TIMES",
"match": "*"
},
{
"name": "DIV",
"match": "/"
},
{
"name": "LPAREN",
"match": "("
},
{
"name": "RPAREN",
"match": ")"
},
{
"name": "EQUALS",
"match": "="
}
]
def advance(self):
self.ind += 1
self.cur = self.code[self.ind] if self.ind < len(self.code) else None
def back(self):
self.ind -= 1
self.cur = self.code[self.ind] if self.ind >= 0 else None
def tokenizeFard(self):
toks = []
while self.cur:
broke = False
for rule in self.other:
if rule["match"].startswith(self.cur):
text = ""
start = self.ind
for _ in range(len(rule["match"])):
text += self.code[self.ind + _]
if text == rule["match"]:
toks.append(Token(text, rule["name"], start, self.ind + len(rule["match"]) - 1))
for _ in range(len(rule["match"]) - 1): self.advance()
broke = True
break
if broke:
pass
elif self.cur == '"':
strng = ""
start = self.ind
self.advance()
while self.cur:
if self.cur == '"':
if len(strng) > 0 and strng[-1] == "\\":
pass
else:
break
strng += self.cur
self.advance()
toks.append(Token(strng, "STRING", start, self.ind))
elif self.cur in self.DIGITS:
num = self.cur
start = self.ind
self.advance()
while self.cur and self.cur in self.DIGITS:
num += self.cur
self.advance()
toks.append(Token(int(num), "NUMBER", start, self.ind))
self.back()
elif self.cur in self.LETTERS:
iden = ""
start = self.ind
while self.cur and self.cur in self.LETTERS + self.DIGITS + self.SPECIAL_KEYWORD_LETTERS:
iden += self.cur
self.advance()
if iden not in self.keywords and True in [char in self.SPECIAL_KEYWORD_LETTERS for char in iden]:
err = Error(self.code)
err.error("Lexer", "Identifiers cant contain special characters.", start, len(iden))
if iden not in self.keywords:
toks.append(Token(iden, "IDENTIFIER", start, self.ind))
else:
toks.append(Token(iden, "KEYWORD", start, self.ind))
self.back()
elif self.cur == "#":
while self.ind < len(self.code) and self.cur != "\n":
self.advance()
self.back()
elif self.cur.isspace():
pass
else:
err = Error(self.code)
err.error("Lexer", f"Unexpected character '{self.cur}'", self.ind, 1)
self.advance()
return toks