diff --git a/README.md b/README.md index 67add62..0ae3be2 100644 --- a/README.md +++ b/README.md @@ -431,12 +431,14 @@ There are some useful extensions and configs for [VSCode](https://code.visualstu ### Testing -To run tests you need to install [busted](https://github.com/Olivine-Labs/busted) and lpeg. +To run tests you need to install [busted](https://github.com/Olivine-Labs/busted). + ```shell $ luarocks install busted -$ luarocks install lpeg ``` +Don't forget also to install [lpeg](http://www.inf.puc-rio.br/~roberto/lpeg/) as described in [Common case](#common-case-löve-pure-lua-etc) installation section. + After that you can run tests from the terminal: ```shell $ busted test/run.lua diff --git a/narrator/parser.lua b/narrator/parser.lua index b7302ec..65b1982 100644 --- a/narrator/parser.lua +++ b/narrator/parser.lua @@ -14,6 +14,7 @@ local lpeg = require(lpegName) local S, C, P, V = lpeg.S, lpeg.C, lpeg.P, lpeg.V local Cb, Ct, Cc, Cg = lpeg.Cb, lpeg.Ct, lpeg.Cc, lpeg.Cg +local Cmt = lpeg.Cmt lpeg.locale(lpeg) -- @@ -49,9 +50,13 @@ function Parser.parse(content) local id = (lpeg.alpha + '_') * (lpeg.alnum + '_') ^ 0 local label = Cg('(' * sp * C(id) * sp * ')', 'label') local address = id * ('.' * id) ^ -2 - local divert = Cg( Ct(divertSign * sp * Cg(address, "path") * sp * Cg(lpeg.Cmt(Cb("path"), function(s, i, a) local r = lpeg.match (sp * divertSign, s, i) return i, r~=nil end), "tunnel") * (sp * divertSign * nl)^-1), 'divert') + + -- TODO: Clean divert expression to divert and tunnel + -- this is too long right now + local divert = Cg( Ct(divertSign * sp * Cg(address, 'path') * sp * Cg(Cmt(Cb('path'), function(s, i, a) local r = lpeg.match (sp * divertSign, s, i) return i, r ~= nil end), 'tunnel') * (sp * divertSign * nl) ^ -1), 'divert') + local divertToNothing = divertSign * none - local exitTunnel = Cg(divertSign * divertSign, "exit") + local exitTunnel = Cg(divertSign * divertSign, 'exit') local tag = '#' * sp * V'text' local tags = Cg(Ct(tag * (sp * tag) ^ 0), 'tags') @@ -365,7 +370,7 @@ function Constructor:addStitch(stitch) if self.currentStitch == '_' then local rootStitchNode = self.book.tree[self.currentKnot]._ if #rootStitchNode == 0 then - local divertItem = { divert = {path = stitch} } + local divertItem = { divert = { path = stitch } } table.insert(rootStitchNode, divertItem) end end diff --git a/narrator/story.lua b/narrator/story.lua index a72ca1b..ffa03e3 100644 --- a/narrator/story.lua +++ b/narrator/story.lua @@ -36,10 +36,9 @@ function Story:new(book) self.paragraphs = { } self.output = { } self.visits = { } + self.tunnels = { } self.currentPath = nil self.isOver = false - - self.stack = { } end -- @@ -143,10 +142,13 @@ function Story:choose(index) end self:visit(choice.path) + if choice.divert ~= nil then if choice.divert.tunnel then - table.insert(self.stack, {path = choice.path}) + local tunnel = { path = choice.path } + table.insert(self.tunnels, tunnel) end + self:jumpTo(choice.divert.path) else self:readPath(choice.path) @@ -361,9 +363,15 @@ function Story:readItems(items, path, depth, mode, currentIndex) -- Iterate items for index = currentIndex or (deepIndex or 1), #items do - local pointer = {items = items, path = path, depth = depth, mode = mode, index = index + 1} local item = items[index] local skip = false + local pointer = { + items = items, + path = path, + depth = depth, + mode = mode, + index = index + 1 + } local itemType = enums.item.text if type(item) == 'table' then @@ -514,20 +522,24 @@ function Story:readText(item, pointer) end if item.exit ~= nil then - local state = assert(table.remove(self.stack), "Tunnel stack is empty.") + local state = assert(table.remove(self.tunnels), 'Tunnel stack is empty.') + if state.items == nil then self:readPath(state.path) else self:readItems(state.items, state.path, state.depth, state.mode, state.index) end + return enums.readMode.quit end if item.divert ~= nil then if item.divert.tunnel then - table.insert(self.stack, pointer) + table.insert(self.tunnels, pointer) end + self:jumpTo(item.divert.path) + return enums.readMode.quit end end @@ -581,7 +593,7 @@ function Story:readChoice(item, path, pointer) if #self.choices == 0 then if item.divert ~= nil then if item.divert.tunnel then - table.insert(self.stack, pointer) + table.insert(self.tunnels, pointer) end self:jumpTo(item.divert.path) else