document The Parser, take an emit function.
This commit is contained in:
parent
fd9866e963
commit
fd68ef88ec
@ -422,7 +422,25 @@ end
|
|||||||
parser = {}
|
parser = {}
|
||||||
mknew(parser)
|
mknew(parser)
|
||||||
|
|
||||||
function parser:ingest(str)
|
-- calls parse_into with a nop
|
||||||
|
-- emit function.
|
||||||
|
function parser:parse(str)
|
||||||
|
self:parse_into(str, function() end)
|
||||||
|
end
|
||||||
|
|
||||||
|
-- read a file of commands and
|
||||||
|
-- execute them, emitting the
|
||||||
|
-- results from each call into
|
||||||
|
-- `emit` as a table per row.
|
||||||
|
--
|
||||||
|
-- a "command" is a method on
|
||||||
|
-- self. a row alternates
|
||||||
|
-- commands with args. when
|
||||||
|
-- calling a command, it also
|
||||||
|
-- gets a table of previous
|
||||||
|
-- results as the first arg.
|
||||||
|
-- args are split on ','.
|
||||||
|
function parser:parse_into(str, emit)
|
||||||
for row in all(split(str, "\n")) do
|
for row in all(split(str, "\n")) do
|
||||||
local prev = {}
|
local prev = {}
|
||||||
local sectors = split(row, ":")
|
local sectors = split(row, ":")
|
||||||
@ -430,9 +448,13 @@ function parser:ingest(str)
|
|||||||
local x = self[sectors[i]](self, prev, usplit(sectors[i+1]))
|
local x = self[sectors[i]](self, prev, usplit(sectors[i+1]))
|
||||||
if (x) add(prev, x)
|
if (x) add(prev, x)
|
||||||
end
|
end
|
||||||
|
emit(prev)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- saves prev[sel] as self.name.
|
||||||
|
-- if sel is unspecified, saves
|
||||||
|
-- all of prev (as a table).
|
||||||
function parser:saveas(prev, name, sel)
|
function parser:saveas(prev, name, sel)
|
||||||
self[name] = sel and prev[sel] or prev
|
self[name] = sel and prev[sel] or prev
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user