-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadaptivetokenstream.lua
More file actions
292 lines (255 loc) · 7.3 KB
/
adaptivetokenstream.lua
File metadata and controls
292 lines (255 loc) · 7.3 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
--[[
Adaptive Token Stream is made as a file-reader that converts input to tokens
in a streamed fashion. This tokenization process can be customized using
custom regex-like patterns and therefore is expandable to many different
user-defined tokens
]]
local util = require("util")
local dbg = require("debugger")
local module = {}
-- ==========================================
--
-- INTERNAL: creates a new node of type
--
-- ==========================================
local function _make_node(stream, type, content)
local n = {}
n.content = content
n.type = type
n.line_pos = stream.line_pos
n.line_nr = stream.line_nr
n.line_txt = stream.line_txt:gsub("\r\n",""):gsub("\n","")
n.file = stream.file
return n
end
-- ==========================================
--
-- INTERNAL: Tries to match pattern and moves stream forward if successful
--
-- ==========================================
local function _stream_try_pattern(stream, pat, typ, move_stream)
line, match, len, offset = match_pattern(stream.line_unparsed, pat)
if match == nil then return nil end
node = _make_node(stream, typ, match)
node.line_pos = (node.line_pos + offset) or 0
if move_stream ~= nil and move_stream then
stream.line_pos = stream.line_pos + len
stream.file_pos = stream.file_pos + len
stream.line_unparsed = line
end
return node
end
-- Functions in stream
local next,
peek,
consume,
push,
pop,
has_next,
eat_newline,
eat_whitespace,
close,
add_token_match,
next_unparsed_line
-- ==========================================
--
-- Create a new tokenstream from file
--
-- ==========================================
module.new = function(filename)
-- stream object returned to the user
-- it contains state information of the stream
-- as well as functions to advance the stream
local stream = {
line_txt = "", -- Current line as a whole
line_pos = 1, -- Where in the line are we currently, offset from beginning
line_unparsed = "", -- Current line but contains only unparsed data
line_nr = 1, -- Line number in the file
file = filename, -- Location of the file
file_pos = 0, -- Offset into the file where we currently are at
file_txt = "", --
file_unparsed = "", --
states = {}, -- Internal state objects used for push/pop
token_matches = {}
}
stream.filehandle = io.open(filename, "r")
if stream.filehandle == nil then
error("Couldn't read file "..filename)
end
stream.file_txt = stream.filehandle:read("*a")
stream.file_unparsed = stream.file_txt
stream.filehandle:close()
stream.filehandle = nil
stream.next = next
stream.peek = peek
stream.consume = consume
stream.push = push
stream.pop = pop
stream.has_next = has_next
stream.eat_newline = eat_newline
stream.eat_whitespace = eat_whitespace
stream.close = close
stream.next_unparsed_line = next_unparsed_line
stream.add_token_match = add_token_match
return readonly(stream)
end
-- ==========================================
--
-- Adds a pattern for a token
--
-- ==========================================
add_token_match = function(stream, pattern, token_type, matched_func)
table.insert(stream.token_matches, {pattern = pattern,
type = token_type,
matched_callback = matched_func})
end
-- ==========================================
--
-- Returns wether there are tokens left to be parsed
--
-- ==========================================
has_next = function(stream)
return stream:peek().type ~= "EOF"
end
next_unparsed_line = function(stream)
local s,m,e = stream.file_unparsed:match("()[^\r\n]*()[\r\n]?()")
local line = stream.file_unparsed:sub(s,m)
if stream.file_unparsed == "" then line = nil end
return line, e
end
-- ==========================================
--
-- INTERNAL: Moves stream forward by 'n' and sets line_unparsed
--
-- ==========================================
local function _move_stream(stream, n, unparsed)
stream.line_pos = stream.line_pos + n
stream.file_pos = stream.file_pos + n
stream.line_unparsed = unparsed
end
-- ==========================================
--
-- INTERNAL: Shared parse method that doesn't move stream forward
--
-- ==========================================
local function _internal_parse(stream)
assert(stream)
local node
if trim(stream.line_unparsed) == "" then
local line, end_cursor = stream:next_unparsed_line()
if line == nil then
-- eof
node = _make_node(stream, "EOF", "")
else
-- newline
if stream.file_pos > 0 then
node = _make_node(stream, "newline", "\n")
stream.line_nr = stream.line_nr + 1
end
_move_stream(stream, 1, line)
stream.line_txt = line or "nil"
stream.line_pos = 0
stream.file_unparsed = stream.file_unparsed:sub(end_cursor)
stream.file_pos = stream.file_pos + end_cursor
end
end
if node == nil then
for k,v in pairs(stream.token_matches) do
local a = _stream_try_pattern(stream, v.pattern, v.type, true)
if a ~= nil then
if v.matched_callback ~= nil then a = v.matched_callback(a) end
return a
end
end
end
if node == nil then
print("ERROR; No matching pattern found for '"..stream.line_unparsed.."'")
--dbg()
end
return node
end
-- ==========================================
--
-- Parse next token and move stream forward
--
-- ==========================================
next = function(stream)
local n = _internal_parse(stream)
return n
end
-- ==========================================
--
-- Parse next token but leave stream where it's at
--
-- ==========================================
peek = function(stream)
stream:push()
local n = _internal_parse(stream)
stream:pop()
return n
end
-- ==========================================
--
-- Returns true when given token matches current and moves stream forward, false otherwise
--
-- ==========================================
consume = function(stream, token)
if stream:peek().type == token then
stream:next()
return true
end
return false
end
-- ==========================================
--
-- Eats tokens until non-newline token is found
--
-- ==========================================
eat_newline = function(stream, include_whitespace)
while stream:peek().type == "newline" or (include_whitespace and stream:peek().type == "whitespace" or not include_whitespace or include_whitespace == nil) do
stream:next()
end
end
eat_whitespace = function(stream)
local tok = stream:peek().type
while tok == "whitespace" or tok == "newline" do
stream:next()
tok = stream:peek().type
end
end
-- ==========================================
--
-- Push the stream state, used to revert stream back using pop
--
-- ==========================================
push = function(stream)
local copy = {}
for k,v in pairs(stream) do
if type(v) ~= "function" then
copy[k] = v
end
end
table.insert(stream.states, copy)
end
-- ==========================================
--
-- Pop the state stack and revert stream to that state
--
-- ==========================================
pop = function(stream)
assert(#stream.states > 0)
local state = stream.states[#stream.states]
for k,v in pairs(state) do
stream[k] = v
end
table.remove(stream.states)
end
-- ==========================================
--
-- Closes filehandle for this stream, stream cannot be used after this
--
-- ==========================================
close = function(stream)
--if stream.filehandle ~= nil then stream.filehandle:close(); stream.filehandle = nil end
end
return module