-
Notifications
You must be signed in to change notification settings - Fork 2
/
lexer.py
223 lines (199 loc) · 7.62 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
from token import *
import re
class Lexer:
def __init__(self, file) -> None:
self.file = file
self.fileSize = len(self.file)
self.currentFilePosition = 0
self.currentCodeFileLinePosition = 0
self.currentCodeFileColumnPosition = 0
self.state = 0
self.lexeme = ""
self.currentChar = self.file[self.currentFilePosition]
self.currentCharIsChecked = 0
self.tokens = []
self.brackets = Brackets()
def nextChar(self) -> None:
self.currentCodeFileColumnPosition += 1
self.currentFilePosition += 1
if self.currentFilePosition < self.fileSize:
self.currentChar = self.file[self.currentFilePosition]
self.currentCharIsChecked = 0
def nextToken(self) -> None:
token = Token(self.lexeme, self.currentCodeFileColumnPosition-len(self.lexeme), self.currentCodeFileLinePosition)
self.tokens.append(token)
self.lexeme = ""
self.state = 0
if self.currentCharIsChecked == 1:
self.nextChar()
def recognized(self, next):
self.currentCharIsChecked = 1
self.lexeme += self.currentChar
if next:
self.nextChar()
else:
self.nextToken()
def isDigit(self) -> None:
if self.currentCharIsChecked == 1:
return
elif re.fullmatch(r'[0-9]', self.currentChar):
self.state = 2
self.recognized(next=True)
def isDollar(self) -> None:
if self.currentCharIsChecked == 1:
return
elif self.currentChar == '$':
self.state = 1
self.recognized(next=True)
def isLetter(self) -> None:
if self.currentCharIsChecked == 1:
return
elif re.fullmatch(r'[a-z]', self.currentChar):
self.state = 3
self.recognized(next=True)
elif re.fullmatch(r'[A-Z]', self.currentChar):
self.state = 1
self.recognized(next=True)
def isReservedWord(self) -> None:
if self.currentCharIsChecked == 1:
return
elif re.fullmatch(r'[a-z]', self.currentChar):
self.recognized(next=True)
elif re.fullmatch(r'[a-zA-z0-9_]', self.currentChar):
self.state = 1
self.recognized(next=True)
else:
self.nextToken()
def isNumber(self) -> None:
if self.currentCharIsChecked == 1:
return
elif re.fullmatch(r'[0-9]|(\.)', self.currentChar):
self.recognized(next=True)
else:
self.nextToken()
def isIdentifier(self) -> None:
if self.currentCharIsChecked == 1:
return
elif re.fullmatch(r'[a-zA-Z0-9_]', self.currentChar):
self.recognized(next=True)
else:
self.nextToken()
def isPass(self) -> None:
if self.currentCharIsChecked == 1:
return
elif self.currentChar == ' ' or self.currentChar == '\n' or self.currentChar == '\t':
if self.currentChar == '\n':
self.currentCodeFileLinePosition += 1
self.currentCodeFileColumnPosition = 0
self.currentCharIsChecked = 1
self.nextChar()
def isSign(self):
if self.currentCharIsChecked == 1:
return
elif self.currentChar == '@':
self.state = 1
self.recognized(next=True)
def isOperator(self):
if self.currentCharIsChecked == 1:
return
elif re.fullmatch(r'[\|\*\%\/]', self.currentChar):
if self.lexeme != '':
self.nextToken()
else:
self.recognized(next=False)
elif re.fullmatch(r'[<>\&\|\%\+\-=!]', self.currentChar):
if self.lexeme != '':
self.nextToken()
elif re.search(r'NUMBER|IDENTIFIER', self.tokens[len(self.tokens)-1].category) and self.currentFilePosition+1 < self.fileSize and re.fullmatch(r'[^<>\&\|\%\+\-=!]', self.file[self.currentFilePosition+1]):
self.recognized(next=False)
else:
self.state = 4
self.recognized(next=True)
def isDoubleOperator(self):
if self.currentCharIsChecked == 1:
return
elif re.fullmatch(r'[<>\&\|\%\+\-=!]', self.currentChar):
self.recognized(next=False)
else:
self.state = 0
self.nextToken()
def isQuote(self):
if self.currentCharIsChecked == 1:
return
elif re.fullmatch(r'["\']', self.currentChar):
self.state = 6
self.recognized(next=True)
def isString(self):
if self.currentCharIsChecked == 1:
return
elif self.currentChar == self.lexeme[0]:
self.recognized(next=False)
else:
self.recognized(next=True)
def isCommentLine(self):
if self.currentCharIsChecked == 1:
return
elif self.currentChar == '#' and self.lexeme == '':
j = self.currentFilePosition
newLexeme = ''
while j < self.fileSize:
if self.file[j] == '\n':
break
newLexeme += self.file[j]
j += 1
j = j - 1
self.currentFilePosition = j
self.lexeme = newLexeme
self.currentChar = self.file[j]
self.currentCharIsChecked = 1
self.nextToken()
def isScope(self):
if self.currentCharIsChecked == 1:
return
elif re.fullmatch(r'[{}]', self.currentChar):
self.recognized(next=False)
def isBrackets(self):
if self.currentCharIsChecked == 1:
return
elif re.fullmatch(r'\[', self.currentChar) or re.fullmatch(r'\]', self.currentChar) :
self.recognized(next=False)
def isSemicolon(self):
if self.currentCharIsChecked == 1:
return
elif self.currentChar == ';':
if self.lexeme == '':
self.recognized(next=False)
else:
self.nextToken()
def isComma(self):
if self.currentCharIsChecked == 1:
return
elif re.fullmatch(r'\,', self.currentChar) :
self.recognized(next=False)
def isCurvyBracket(self):
if self.currentCharIsChecked == 1:
return
elif self.currentChar == '(':
if self.lexeme != '':
self.nextToken()
else:
j = self.currentFilePosition
newLexeme = ''
while j < self.fileSize:
newLexeme += self.file[j]
if self.file[j] == ')':
break
j += 1
if re.fullmatch(tkCategories['ARRAY_OF_NUMBERS'], newLexeme) or (re.fullmatch(tkCategories['ARRAY_OF_STRINGS'], newLexeme) and len(newLexeme)>2):
self.lexeme = newLexeme
self.currentFilePosition = j
self.currentCharIsChecked = 1
self.nextToken()
elif re.fullmatch(r'[\(\)]', self.currentChar):
self.recognized(next=False)
elif self.currentChar == ')':
self.recognized(next=False)
# def lexerError(self, token):
# if token.category == 'UNKNOW':
# print("\033[1;31m", f"ERRO! Impossível definir {token.lexema} na posição {self.currentCodeFileColumnPosition+1} da linha {self.currentCodeFileLinePosition+1}" ,"\033[0;0m")
# exit()