-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlexit.lua
More file actions
377 lines (332 loc) · 9.21 KB
/
lexit.lua
File metadata and controls
377 lines (332 loc) · 9.21 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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
--lexit.lua
--Mark Underwood
--2/17/2019
--Based upon lexer.lua by Dr. Glenn Chappell
--performs lexical analysis for Jerboa programming language
local lexit = {}
lexit.KEY = 1
lexit.ID = 2
lexit.NUMLIT = 3
lexit.STRLIT = 4
lexit.OP = 5
lexit.PUNCT = 6
lexit.MAL = 7
lexit.catnames = {
"Keyword",
"Identifier",
"NumericLiteral",
"StringLiteral",
"Operator",
"Punctuation",
"Malformed"
}
local keywords = {
cr = true,
def = true,
["else"] = true,
["elseif"] = true,
["end"] = true,
["false"] = true,
["if"] = true,
readnum = true,
["return"] = true,
["true"] = true,
["while"] = true,
write = true
}
local operators = {
["*"] = true,
["&"] = true,
["%"] = true,
["!"] = true,
["/"] = true,
["["] = true,
["]"] = true,
["<"] = true,
[">"] = true,
["|"] = true,
["+"] = true,
["-"] = true,
["="] = true,
}
local doubleOperators = {
["&&"] = true,
["!="] = true,
["<="] = true,
[">="] = true,
["||"] = true,
["=="] = true,
}
-- isLetter
-- Returns true if string c is a letter character, false otherwise.
local function isLetter(c)
if c:len() ~= 1 then
return false
elseif c >= "A" and c <= "Z" then
return true
elseif c >= "a" and c <= "z" then
return true
else
return false
end
end
-- isDigit
-- Returns true if string c is a digit character, false otherwise.
local function isDigit(c)
if c:len() ~= 1 then
return false
elseif c >= "0" and c <= "9" then
return true
else
return false
end
end
-- isWhitespace
-- Returns true if string c is a whitespace character, false otherwise.
local function isWhitespace(c)
if c:len() ~= 1 then
return false
elseif c == " " or c == "\t" or c == "\n" or c == "\r"
or c == "\f" then
return true
else
return false
end
end
-- isIllegal
-- Returns true if string c is an illegal character, false otherwise.
local function isIllegal(c)
if c:len() ~= 1 then
return false
elseif isWhitespace(c) then
return false
elseif c >= " " and c <= "~" then
return false
else
return true
end
end
function lexit.lex(program)
-- ***** Variables (like class data members) *****
local pos -- Index of next character in program
-- INVARIANT: when getLexeme is called, pos is
-- EITHER the index of the first character of the
-- next lexeme OR program:len()+1
local state -- Current state for our state machine
local ch -- Current character
local lexstr -- The lexeme, so far
local category -- Category of lexeme, set when state set to DONE
local handlers -- Dispatch table; value created later
local quote --Either a single or a double quote, depending on the string
local lastLexemeCategory --The last lexeme to be parsed and its category
local lastLexeme --
-- ***** Character-Related Utility Functions *****
-- currChar
-- Return the current character, at index pos in program. Return
-- value is a single-character string, or the empty string if pos is
-- past the end.
local function currChar()
return program:sub(pos, pos)
end
-- nextChar
-- Return the next character, at index pos+1 in program. Return
-- value is a single-character string, or the empty string if pos+1
-- is past the end.
local function nextChar()
return program:sub(pos+1, pos+1)
end
--lastChar
--Return the last character in the string, or the empty string if
--pos-1 is before the first character
local function lastChar()
return program:sub(pos-1, pos-1)
end
--lookahead2
--Return the character after next in the string, or the empty string if pos+2 is past the end
local function lookahead2()
return program:sub(pos+2, pos+2)
end
-- drop1
-- Move pos to the next character.
local function drop1()
pos = pos+1
end
-- add1
-- Add the current character to the lexeme, moving pos to the next
-- character.
local function add1()
lexstr = lexstr .. currChar()
drop1()
end
-- skipWhitespace
-- Skip whitespace and comments, moving pos to the beginning of
-- the next lexeme, or to program:len()+1.
local function skipWhitespace()
while true do -- In whitespace
while isWhitespace(currChar()) do
drop1()
end
if currChar() ~= "#" then -- Comment?
break
end
drop1()
while true do -- In comment
if currChar() == "\n" then
drop1()
break
elseif currChar() == "" then -- End of input?
return
end
drop1()
end
end
end
-- ***** State-Handler Functions *****
-- A function with a name like handle_XYZ is the handler function
-- for state XYZ
local function handle_DONE()
io.write("ERROR: 'DONE' state should not be handled\n")
assert(0)
end
local function handle_START()
if isIllegal(ch) then
add1()
state = handlers.DONE
category = lexit.MAL
elseif isLetter(ch) or ch == "_" then
add1()
state = handlers.LETTER
elseif isDigit(ch) then
add1()
state = handlers.DIGIT
elseif ch == "+" then
state = handlers.PLUSMINUS
elseif ch == "-" then
state = handlers.PLUSMINUS
elseif operators[ch] then
add1()
state = handlers.OPERATOR
elseif ch == "'" or ch == [["]] then
delimiter = ch
add1()
state = handlers.STRING
else
add1()
state = handlers.DONE
category = lexit.PUNCT
end
end
local function handle_LETTER()
if isLetter(ch) or isDigit(ch) or ch == "_" then
add1()
else
state = handlers.DONE
if keywords[lexstr] then
category = lexit.KEY
else
category = lexit.ID
end
end
end
local function handle_DIGIT()
if isDigit(ch) then
add1()
elseif ch == "e" or ch == "E" then
if isDigit(nextChar()) or (nextChar() == "+" and isDigit(lookahead2())) then
add1()
add1()
state = handlers.EXP
else
state = handlers.DONE
category = lexit.NUMLIT
end
else
state = handlers.DONE
category = lexit.NUMLIT
end
end
local function handle_EXP()
if isDigit(ch) then
add1()
else
state = handlers.DONE
category = lexit.NUMLIT
end
end
local function handle_PLUSMINUS()
if lastLexemeCategory == lexit.ID or lastLexemeCategory == lexit.NUMLIT
or lastLexeme == "]" or lastLexeme == ")" or lastLexeme == "true" or lastLexeme == "false" then
state = handlers.DONE
category = lexit.OP
elseif (isDigit(nextChar()) and not (lastChar() == "e" or lastChar() == "E"))
or (lastLexemeCategory == lexit.KEY and (lastChar() == "e" or lastChar() == "E")) then
state = handlers.DIGIT
else
state = handlers.DONE
category = lexit.OP
end
add1()
end
local function handle_OPERATOR()
if operators[ch] and doubleOperators[lastChar()..ch] then
add1()
state = handlers.DONE
category = lexit.OP
elseif lastChar() ~= "&" and lastChar() ~= "|" then
state = handlers.DONE
category = lexit.OP
else
state = handlers.DONE
category = lexit.PUNCT
end
end
local function handle_STRING()
if ch == delimiter then
state = handlers.DONE
category = lexit.STRLIT
elseif ch == "\n" or ch == "" then
state = handlers.DONE
category = lexit.MAL
end
add1()
end
-- ***** Table of State-Handler Functions *****
handlers = {
DONE = handle_DONE,
START = handle_START,
LETTER = handle_LETTER,
DIGIT = handle_DIGIT,
EXP = handle_EXP,
DOT = handle_DOT,
PLUSMINUS = handle_PLUSMINUS,
MINUS = handle_MINUS,
OPERATOR = handle_OPERATOR,
STRING = handle_STRING
}
-- ***** Iterator Function *****
-- getLexeme
-- Called each time through the for-in loop.
-- Returns a pair: lexeme-string (string) and category (int), or
-- nil, nil if no more lexemes.
local function getLexeme(dummy1, dummy2)
if pos > program:len() then
return nil, nil
end
lexstr = ""
state = handlers.START
while state ~= handlers.DONE do
ch = currChar()
state()
end
skipWhitespace()
lastLexemeCategory = category
lastLexeme = lexstr
return lexstr, category
end
-- ***** Body of Function lex *****
-- Initialize & return the iterator function
pos = 1
skipWhitespace()
return getLexeme, nil, nil
end
return lexit