This repository has been archived by the owner on Jul 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer.py
186 lines (156 loc) · 5.2 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
class Tokens:
def __init__(self, tokens):
self.tokens = tokens
def Len(self):
return len(self.tokens)
def Next(self):
if len(self.tokens) > 0:
return self.tokens.pop(0)
else:
#TODO(Noah): Fix this inconsistency. Where is the logger?
print("[ERROR]: No more tokens")
return False
def Query(self):
return self.tokens[0]
def QueryDistance(self, distance):
if distance < len(self.tokens):
return self.tokens[distance]
else:
return False
class Token:
def __init__(self, type, value, line):
self.type = type
self.value = value
self.line = line
def IsKeyword(buffer, line):
result = ()
tokenValue = "NULL"
if buffer == "struct":
tokenValue = buffer
elif buffer == "true":
tokenValue = "true"
elif buffer == "false":
tokenValue = "false"
elif buffer == "int":
tokenValue = buffer
elif buffer == "unsigned":
tokenValue = buffer
elif buffer == "continue":
tokenValue = buffer
elif buffer == "break":
tokenValue = buffer
elif buffer == "if":
tokenValue = buffer
elif buffer == "while":
tokenValue = buffer
elif buffer == "for":
tokenValue = buffer
elif buffer == "else":
tokenValue = buffer
elif buffer == "return":
tokenValue = buffer
if tokenValue != "NULL":
result = Token("KEY", tokenValue, line)
if result == ():
return False
return result
def searchUntil(str, index, term):
sum = 0
newStr = ""
for character in str[index:]:
if character == term:
break
newStr += character
sum += 1
return (sum, newStr)
def QueryForToken(character, cleanToken, test, label, line):
token = False
token2 = False
if character in test:
if cleanToken[:-1] != "":
token2 = Token("SYMBOL", cleanToken[:-1].strip(), line)
token = Token(label, character, line)
return (token, token2)
def Run(raw):
tokens = []
currentToken = ""
state = ""
currentLine = 1
for n in range(len(raw)):
foundToken = False
character = raw[n] #current character
if character != "\n":
currentToken += character #advance currentToken
else:
currentLine += 1
cT = currentToken.strip()
if state == "COMMENT":
if character == '\n':
state = ""
currentToken = ""
continue
elif character == '/' and raw[n+1] == '/':
state = "COMMENT"
if cT[:-1] != "":
tokens.append(Token("SYMBOL", cT[:-1].strip(), currentLine))
if state == "QUOTE":
if character == '"':
state = ""
tokens.append(Token("QUOTE", currentToken[:-1], currentLine))
currentToken = ""
continue
elif character == '"':
state = "QUOTE"
if cT[:-1] != "":
tokens.append(Token("SYMBOL", cT[:-1].strip(), currentLine))
currentToken = ""
if state == "WAIT_SPACE":
if character == " ":
state = ""
currentToken = ""
continue
#handle operators
token, token2 = QueryForToken(character, cT, "+-%*!<>=|&?", "OP", currentLine)
if token2:
tokens.append(token2)
if token:
tokens.append(token)
foundToken = True
#check for end lines
token, token2 = QueryForToken(character, cT, ";", "NULL", currentLine)
if token2:
tokens.append(token2)
if token:
tokens.append(Token("END", "END", currentLine))
foundToken = True
#check for seperators
token, token2 = QueryForToken(character, cT, "{}()[].,:", "PART", currentLine)
if token2:
tokens.append(token2)
if token:
tokens.append(token)
foundToken = True
#check for division statements
if character == '/' and raw[n+1] != '/':
if cT[:-1] != "":
tokens.append(Token("SYMBOL", cT[:-1].strip(), currentLine))
tokens.append(Token("OP", "/", currentLine))
foundToken = True
#check for preprocessor statements
token, token2 = QueryForToken(character, cT, "#", "PRE", currentLine)
if token2:
tokens.append(token2)
if token:
length, str = searchUntil(raw, n, " ")
tokens.append(Token("PRE", str, currentLine))
state = "WAIT_SPACE"
foundToken = True
#recognize keywords
#TODO(Noah): If I were to say int0, I would still recognize the int and 0 seperate
result = IsKeyword(cT, currentLine)
if result:
tokens.append(result)
foundToken = True
if foundToken:
currentToken = ""
return Tokens(tokens)