forked from rjpcomputing/luaforwindows
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlexer.lua
More file actions
executable file
·54 lines (45 loc) · 1.13 KB
/
lexer.lua
File metadata and controls
executable file
·54 lines (45 loc) · 1.13 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
local lexis = {}
local function lexeme(name)
return function(pattern)
lexis[#lexis+1] = { name=name, pattern="^"..pattern }
end
end
lexeme (false) "%s+" -- whitespace
lexeme "table" "(%b{})"
lexeme "group" "(%b())"
lexeme "name_atom" "([%a_][%w_]*)%:(%a)([%d.]*)"
lexeme "name_table" "([%a_][%w_]*)%:(%b{})"
lexeme "prerepeat" "(%d+)%s*%*"
lexeme "postrepeat" "%*%s*(%d+)"
lexeme "control" "([-+@<>=ax])([%d.]*)"
lexeme "atom" "(%a)([%d.]*)"
return function(source)
local orig = source
local index = 1
local function iter()
if #source == 0 then return nil end
for _,lexeme in ipairs(lexis) do
if source:match(lexeme.pattern) then
local result = { source:find(lexeme.pattern) }
local eof = table.remove(result, 2)
table.remove(result, 1)
source = source:sub(eof+1, -1)
index = index+eof
if lexeme.name then
result.type = lexeme.name
coroutine.yield(result)
end
return iter()
end
end
error (function() return "Error lexing format string [["
..(orig)
.."]] at char "
..index
.." ("
..(source:sub(1,1))
..")"
end)
end
return coroutine.wrap(iter)
end