-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.lua
207 lines (185 loc) · 4.79 KB
/
parser.lua
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
local interop = require("interop")
local Parser = {}
local function lines_of(input)
local lines = {}
for line in input:gmatch("([^\r\n]*)\r?\n?") do
table.insert(lines, line)
end
return lines
end
function Parser.new(source, dict)
local obj = {index = 1,
line_number = 1,
source = source,
lines = lines_of(source),
dict = dict}
setmetatable(obj, {__index = Parser})
return obj
end
function Parser:parse_all()
local result = {}
local item = self:next_item()
while item do
table.insert(result, item)
item = self:next_item()
end
return result
end
local function is_quote(chr)
return chr:match('"')
end
local function is_escape(chr)
return chr:match("\\")
end
local function lua_func_call(token, res)
return {
token = token,
kind = "lua_func_call",
name = res.name,
arity = res.arity,
vararg = res.vararg,
void = res.void
}
end
local function lua_method_call(token, res)
return {
token = token,
kind = "lua_method_call",
name = res.name,
arity = res.arity,
vararg = res.vararg,
void = res.void
}
end
local function lua_table_lookup(token, resolved)
return {token = token, kind = "lua_table_lookup", resolved = resolved}
end
local function lua_array_lookup(token, resolved)
return {token = token, kind = "lua_array_lookup", resolved = resolved}
end
local function literal(token, subtype)
return {token = token, kind = "literal", subtype = subtype}
end
local function unknown(token)
return {token = token, kind = "unknown"}
end
local function is_whitespace(chr)
return chr:match("%s")
end
function Parser:check_line_ending(chr)
end
function Parser:next_item()
local token = ""
local begin_str = false
local stop = false
local kind = "word"
while not self:ended() and not stop do
local chr = self:read_chr()
if is_quote(chr) then
if begin_str then
stop = true
else
kind = "string"
begin_str = true
end
token = token .. chr
elseif begin_str
and is_escape(chr)
and is_quote(self:peek_chr()) then
token = token .. chr .. self:read_chr() -- consume \"
elseif is_whitespace(chr) and not begin_str then -- TODO how does this handle multiline strings
if #token > 0 then
self.index = self.index -1 -- don't consume next WS as it breaks single line comment
stop = true
else
self:update_line_number(chr)
end
else
token = token .. chr
end
end
if token == "" then
return nil
end
if token:match("^&.+") then kind = "symbol" end
local result = self:parse_word(token, kind)
result.line_number = self.line_number -- TODO comments breaks line number as they consume the input
return result
end
function Parser:update_line_number(chr)
if chr == '\r' then
if self:peek_chr() == '\n' then self:read_chr() end
self.line_number = self.line_number +1
elseif chr == '\n' then
self.line_number = self.line_number +1
end
end
function Parser:next_chr()
local chr = self:read_chr()
self:update_line_number(chr)
return chr
end
function Parser:read_chr()
local chr = self:peek_chr()
self.index = self.index + 1
return chr
end
function Parser:peek_chr()
return self.source:sub(self.index, self.index)
end
function Parser:parse_word(token, kind)
local word = self.dict:find(token)
if kind == "word" and word and word.immediate
then
return {token = token, kind = "macro"}
end
if kind == "string" then
return literal(token, "string")
end
if kind == "symbol" then
return literal(token, "symbol")
end
if word then
if word.is_lua_alias then
-- Known lua alias
local res = interop.resolve_lua_func_with_arity(word.lua_name)
return lua_func_call(token, res)
else
-- Known Forth word
return {token = token, kind = "word"}
end
end
local num = tonumber(token)
if num then
return literal(token, "number")
end
local res = interop.resolve_lua_method_call(token)
if res then
-- Lua method call such as obj:method/3
return lua_method_call(token, res)
end
local res = interop.resolve_lua_func_with_arity(token)
if res then
-- Lua function call such as math.max/2
return lua_func_call(token, res)
end
if interop.is_lua_prop_lookup(token) then
-- Table lookup
local lua_obj = interop.resolve_lua_obj(token)
-- best effort to check if it's valid lookup
if lua_obj or self.dict:find(token:match("^[^.]+")) then
return lua_table_lookup(token, true)
else
return lua_table_lookup(token, false)
end
end
if interop.is_lua_array_lookup(token) then
-- TODO try to resolve
return lua_array_lookup(token, true)
end
return unknown(token)
end
function Parser:ended()
return self.index > #self.source
end
return Parser