aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChloe Kudryavtsev <toast@toast.cafe>2022-01-17 19:27:17 -0500
committerChloe Kudryavtsev <toast@toast.cafe>2022-01-17 19:27:17 -0500
commite6685ca6368f43b7fc381f40c1a22aceb31e4f97 (patch)
tree94ca4e12a6d0e810c55047888ebe5a01ed6bc061
parentupdate examples and readme for 1.1 (diff)
add eof, patch parse to work with it
-rw-r--r--piecemeal.lua19
1 files changed, 15 insertions, 4 deletions
diff --git a/piecemeal.lua b/piecemeal.lua
index bec9c04..0ddf8f9 100644
--- a/piecemeal.lua
+++ b/piecemeal.lua
@@ -5,6 +5,17 @@
]]--
return {
+ eof = function ()
+ return function (l, i)
+ -- we don't have to, but we advance the pointer by one
+ -- this prevents infinite looping
+ if #l+1 == i then
+ local pos = l[#l].stop + 1
+ return i+1, {type = 'EOF', value = '', start = pos, stop = pos}
+ end
+ return nil
+ end
+ end,
lexeme = function (type)
return function (l, i)
if l[i] and l[i].type == type then
@@ -102,11 +113,11 @@ return {
if next then table.insert(tokens, next) end
until next == nil
local eind, out = parser(tokens, 1)
- -- a successful parse means eind is >1 the length of tokens
- if tokens[eind] then -- so this is unsuccessful
+ -- a successful parse means eind is > the length of tokens
+ if eind > #tokens then
+ return out, tokens[#tokens].stop, nil
+ else -- TODO: what if eind == 1? can that happen?
return out, tokens[eind-1].stop, tokens[eind].start
- else -- and this is
- return out, tokens[eind-1].stop, nil
end
end,
}