-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLexer.lua
More file actions
230 lines (210 loc) · 7 KB
/
Copy pathLexer.lua
File metadata and controls
230 lines (210 loc) · 7 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
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
local Token = require(script.Parent.Token)
local TokenTypes = require(script.Parent.TokenTypes)
local LETTERS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
local NUMBERS = "0123456789"
local SYMBOLS = "-!@#$%^&*()+]}|\\ \';[{:><,/?.~`"
local IDENTIFIER_LETTERS = LETTERS .. NUMBERS .. "_"
local STRING = IDENTIFIER_LETTERS .. SYMBOLS
local BOOL_VAL = {"true", "false"}
local NULL_VAL = "null"
local KEYWORDS = {"and", "or"}
local Lexer = {}
Lexer.__index = Lexer
function Lexer.new(content)
local self = setmetatable({}, Lexer)
self.text = content
self.currentNum = 0
self.currentChar = self.text:sub(self.currentNum, self.currentNum)
self.line = 1
return self
end
function Lexer:Advance()
self.currentNum += 1
if self.currentNum <= #self.text then
self.currentChar = self.text:sub(self.currentNum, self.currentNum)
else
self.currentChar = nil
end
end
function Lexer:Lex()
local tokens = {}
local actionMap = {
['+'] = function()
table.insert(tokens, Token.new(TokenTypes.PLUS));
self:Advance();
end,
['-'] = function()
table.insert(tokens, Token.new(TokenTypes.MINUS));
self:Advance();
end,
['*'] = function()
table.insert(tokens, Token.new(TokenTypes.MULTIPLY));
self:Advance();
end,
['/'] = function()
table.insert(tokens, Token.new(TokenTypes.DIVIDE));
self:Advance();
end,
['^'] = function()
table.insert(tokens, Token.new(TokenTypes.POWER));
self:Advance();
end,
[' '] = function()
self:Advance();
end,
['\t'] = function()
self:Advance();
end,
['\n'] = function()
self.line += 1;
self:Advance();
end,
['"'] = function()
table.insert(tokens, Token.new(TokenTypes.STRING, self:LexString()))
self:Advance()
end,
['='] = function()
if self:CheckNext('=') then
table.insert(tokens, Token.new(TokenTypes.EEQ, 0))
self:Advance()
self:Advance()
else
self:Advance()
end
end,
["!"] = function()
if self:CheckNext("=") then
table.insert(tokens, Token.new(TokenTypes.NQ, 0))
self:Advance()
self:Advance()
else
table.insert(tokens, Token.new(TokenTypes.NEG, 0))
self:Advance()
end
end,
[">"] = function()
if self:CheckNext("=") then
table.insert(tokens, Token.new(TokenTypes.GTE, 0))
self:Advance()
self:Advance()
else
table.insert(tokens, Token.new(TokenTypes.GT, 0))
self:Advance()
end
end,
["<"] = function()
if self:CheckNext("=") then
table.insert(tokens, Token.new(TokenTypes.LTE, 0))
self:Advance()
self:Advance()
else
table.insert(tokens, Token.new(TokenTypes.LT, 0))
self:Advance()
end
end
}
while self.currentChar do
local action = actionMap[self.currentChar] or (self.currentChar:match("[%a]") and function()
table.insert(tokens, self:LexID())
end)
if action then
action()
else
error("InvalidCharacterError on line " .. self.line .. ": Unexpected Character " .. self.currentChar .. " appeared.")
end
end
return tokens
end
function Lexer:LexNumber()
local nstr = ""
local dotcount = 0
while self.currentChar and self.currentChar:match("%d") or self.currentChar == "." do
if self.currentChar == "." then
if dotcount == 1 then
error("InvalidCharacterError on line " .. self.line .. ": Unexpected Character " .. self.currentChar .. " appeared.")
end
dotcount += 1
end
nstr = nstr .. self.currentChar
self:Advance()
end
if nstr:sub(-1) == "." then
nstr = nstr:sub(1, -2)
end
return tonumber(nstr)
end
function Lexer:LexID()
local identifier = ''
while self.currentChar and self.currentChar:match("[%w]") do
identifier = identifier .. self.currentChar
self:Advance()
end
if identifier == NULL_VAL then
return Token.new(TokenTypes.NULL, 0)
elseif identifier == BOOL_VAL[1] then
return Token.new(TokenTypes.BOOL, true)
elseif identifier == BOOL_VAL[2] then
return Token.new(TokenTypes.BOOL, false)
elseif table.contains(KEYWORDS, identifier) then
return Token.new(TokenTypes.KEYWORD, identifier)
else
return Token.new(TokenTypes.IDENTIFIER, identifier)
end
end
function Lexer:LexString()
local string = ''
local modifiers = {}
local is_f_string = false
local idx = 0
while self.currentChar ~= nil and STRING:find(self.currentChar, 1, true) do
local increment = 1
if self.currentChar == '\\' then
self:Advance()
if SYMBOLS:find(self.currentChar, 1, true) or self.currentChar == '"' then
string = string .. self.currentChar
self:Advance()
increment = 2
end
elseif self.currentChar == '{' then
self:Advance()
local inputstream = ''
while self.currentChar ~= nil and not (self.currentChar:find('{}', 1, true)) do
inputstream = inputstream .. self.currentChar
self:Advance()
end
idx = idx + 1
is_f_string = true
modifiers[tostring(idx - 1)] = Lexer.new(inputstream):Lex()
self:Advance()
else
string = string .. self.currentChar
idx = idx + 1
self:Advance()
end
idx = idx + increment
end
local type = is_f_string and TokenTypes.FSTRING or TokenTypes.STRING
return Token.new(type, string, modifiers)
end
function Lexer:CheckNext(char, get)
get = get or false
local baseRet = 'NULL'
if self.currentNum + 1 < #self.text then
baseRet = self.text:sub(self.currentNum + 2, self.currentNum + 2)
end
if self.currentNum + 1 < #self.text and self.text:sub(self.currentNum + 2, self.currentNum + 2) == char then
if not get then
return true
else
return self.text:sub(self.currentNum + 2, self.currentNum + 2)
end
else
if not get then
return false
else
return baseRet
end
end
end
Lexer.KEYWORDS = KEYWORDS
return Lexer