-
Notifications
You must be signed in to change notification settings - Fork 0
/
om_lexer
275 lines (235 loc) · 5.8 KB
/
om_lexer
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
local args = {...}
local api = args[1]
local om_lex = {}
om_lex.tokens = {
["//"] = "TOK_COMMENT",
["=="] = "TOK_EQUALS",
["="] = "TOK_ASSIGN",
[">"] = "TOK_GT",
[">="] = "TOK_GE",
["<"] = "TOK_LT",
["<="] = "TOK_LE",
["*"] = "TOK_MUL",
["/"] = "TOK_DIV",
["+"] = "TOK_ADD",
["-"] = "TOK_SUB",
["=="] = "TOK_EQUALS",
["{"] = "TOK_BRACKET2_OPEN",
["}"] = "TOK_BRACKET2_CLOSE",
["("] = "TOK_BRACKET_OPEN",
[")"] = "TOK_BRACKET_CLOSE",
["package"] = "TOK_PACKAGE",
["import"] = "TOK_IMPORT",
["extends"] = "TOK_EXTENDS",
["implements"] = "TOK_IMPLEMENTS",
["."] = "TOK_DOT",
[";"] = "TOK_SEMI",
["static"] = "TOK_STATIC",
["public"] = "TOK_PUBLIC",
["private"] = "TOK_PRIVATE",
["protected"] = "TOK_PRIVATE",
["void"] = "TOK_VOID",
["number"] = "TOK_NUMBER",
["string"] = "TOK_STRING",
["char"] = "TOK_CHAR",
["map"] = "TOK_MAP",
["nil"] = "TOK_NIL",
["boolean"] = "TOK_BOOLEAN",
["if"] = "TOK_IF",
["elseif"] = "TOK_ELSEIF",
["else"] = "TOK_ELSE"
}
om_lex.generictokens = {
"TOK_DIGIT", "TOK_NAME", "TOK_SPACE"
}
om_lex.lexerr = {
"ERR_UNEXPECTED_TOKEN"
}
om_lex.tokenlengths = {}
om_lex.tokensinvrt = {}
for a, b in pairs(om_lex.tokens) do
om_lex.tokensinvrt[b] = a
if om_lex.tokenlengths[string.len(a)] do
om_lex.tokenlengths[string.len(a)][#om_lex.tokenlengths[string.len(a)] + 1] = b
else
om_lex.tokenlengths[string.len(a)] = {b}
end
end
--[[
** BEGIN LEXER API **
]]
function om_lex:getToken(buffer)
local afterChar
for a = #self.tokenlengths, 1, -1 do
if self.tokenlengths[a] then
local tok = string.sub(buffer, 1, a)
local afterChar = string.sub(buffer, a + 1, a + 1) or " "
for b = 1, #self.tokenlengths[a] do
local tokenname = self.tokenlengths[a]
if tokenname == self.tokens[tok] then
if self:isStringAlphaNumeric(tok) then
if not self:isAlphaNumeric(afterChar) then
return self.tokens[tok]
end
end
end
end
end
end
end
function om_lex:isAlphaNumeric(character)
if self:isAlphabetic(character) or self:isDigit(character) then
return true
end
end
function om_lex:isAlphabetic(character)
local b = string.byte(character)
if (b >= 65 and b <= 90) or (b >= 97 and b <= 122) then
return true
end
return false
end
function om_lex:isNumeric(character)
local b = string.byte(character)
if b >= 48 and b <= 57 then
return true
end
return false
end
function om_lex:isStringNumeric(str)
for a = 1, #str do
if not self:isNumeric(str:sub(a, a)) then
return false
end
end
return true
end
function om_lex:isStringAlphabetic(str)
for a = 1, #str do
if not self:isAlphabetic(str:sub(a, a)) then
return false
end
end
return true
end
function om_lex:isStringAlphaNumeric(str)
for a = 1, #str do
if not (self:isAlphabetic(str:sub(a, a)) or self:isNumeric(str:sub(a, a))) then
return false
end
end
return true
end
function om_lex:readNumber()
self.lexstate.input:read()
local dotCount = 0
while self:isNumeric(self.lexstate.input:getLastBufferChar()) or self.lexstate.input:getLastBufferChar() == "." do
if self.lexstate.input:getLastBufferChar() == "." then
dotCount = dotCount + 1
if dotCount > 1 then
error("Could not parse number: Multiple dots")
end
end
self.lexstate.input:read()
end
self.lexstate.input:discardLast()
return self.lexstate.input:flushGetBuffer()
end
function om_lex:readName()
self.lexstate.input:read()
while self:isAlphaNumeric(self.lexstate.input:getLastBufferChar()) or (self.lexstate.input:getLastBufferChar() == "_") do
self.lexstate.input:read()
end
self.lexstate.input:discardLast()
return self.lexstate.input:flushGetBuffer()
end
function om_lex:readString()
local input = self.lexstate.input
input:read()
local startedWith = input:getLastBufferChar()
input:flush()
input:read()
local inString = true
local str = ""
while inString do
if input:getLastBufferChar() == startedWith then
inString = false
break
end
if input:getLastBufferChar() == "\\" then
input:read()
if input:getLastBufferChar() == "n" then
str = str .. "\n"
elseif input:getLastBufferChar() == "\\" then
str = str .. "\\"
elseif input:getLastBufferChar() == '"' then
str = str .. '"'
elseif input:getLastBufferChar() == "'" then
str = str .. "'"
end
else
str = str .. input:getLastBufferChar()
end
input:read()
if input.isClosed() then
error("Error parsing string: unfinished string")
end
end
input:flush()
return str
end
function om_lex:lexer()
assert(self:canRun(), "Lexer can't run: Not Initialized")
self.lexstate.input:flush()
self.lexstate.input:readChar()
local maxlength = self.lexstate.maxtokenlength
if self.lexstate.input:getBuffer() == " " or self.lexstate.input:getBuffer() == "\n" then
return "TOK_SPACE"
else
-- check tokens
for a = 1, maxlength do
self.lexstate.input:read()
end
self.lexstate.input:read()
local token = self:getToken(self.lexstate.input:getBuffer(), )
self.lexstate.input:discard()
if token then
return token
end
-- check is name
if self.isAlphabetic(self.lexstate.input:getBuffer()) then
self.lexstate.input:discard()
return "TOK_NAME", self:readName()
end
-- check is digit
self.lexstate.input:read()
if self:isDigit(self.lexstate.input:getBuffer()) then
self.lexstate.input:discard()
return "TOK_NUMBER", self:readNumber()
end
-- check is string
if self.lexstate.input:getBuffer() == '"' or self.lexstate.input:getBuffer() == "'" then
self.lexstate.input:discard()
return "TOK_STRING", self:readString()
end
return "ERR_UNEXPECTED_TOKEN"
end
end
function om_lex:canRun()
return self.lexstate.input ~= nil
end
function om_lex:initialize(input)
self.lexstate.input = input
end
function om_lex:resetState()
self.lexstate = {
c = "",
input = {},
characternum = 0,
linenum = 0,
}
end
--[[
** END LEXER API **
]]
return om_lex