mirror of
https://github.com/Oreolek/ink-instead.git
synced 2024-04-28 15:19:25 +03:00
damn nested choices
This commit is contained in:
parent
2e2fd68e4f
commit
e9909003b0
|
@ -1,12 +1,19 @@
|
|||
require "util"
|
||||
local lpeg = require "lpeg"
|
||||
lpeg.locale(lpeg)
|
||||
local S,C,Ct,Cc,Cg,Cf,P,V = lpeg.S, lpeg.C, lpeg.Ct, lpeg.Cc, lpeg.Cg, lpeg.Cf, lpeg.P, lpeg.V
|
||||
local parserLogger = print
|
||||
local S,C,Ct,Cc,Cg,Cb,Cf,Cmt,P,V =
|
||||
lpeg.S, lpeg.C, lpeg.Ct, lpeg.Cc, lpeg.Cg, lpeg.Cb, lpeg.Cf, lpeg.Cmt,
|
||||
lpeg.P, lpeg.V
|
||||
|
||||
local sp = S" \t" ^0 + -1
|
||||
local wh = S" \t\r\n" ^0 + -1
|
||||
local nl = S"\r\n" ^1 + -1
|
||||
local concat = function (p)
|
||||
return Cf(p, function (a,b) return a..b end)
|
||||
end
|
||||
|
||||
local parserLogger = print
|
||||
local eof = -1
|
||||
local sp = S" \t" ^0 + eof
|
||||
local wh = S" \t\r\n" ^0 + eof
|
||||
local nl = S"\r\n" ^1 + eof
|
||||
local id = (lpeg.alpha + '_') * (lpeg.alnum + '_')^0
|
||||
local addr = C(id) * ('.' * C(id))^-1
|
||||
|
||||
|
@ -26,23 +33,41 @@ local divert = divertEnd + divertJump
|
|||
local knotHead = P('=')^2/'knot' * wh * C(id) * wh * P('=')^0 * wh
|
||||
local stitchHead = P('=')^1/'stitch' * wh * C(id) * wh * P('=')^0 * wh
|
||||
|
||||
local optionDiv = '[' * C((P(1) - ']')^0) * ']'
|
||||
local optDiv = '[' * C((P(1) - ']')^0) * ']'
|
||||
|
||||
local optStars = concat(wh * C(P'*') * (sp * C'*')^0)
|
||||
local optStarsSameIndent = Cmt(Cb("indent") * optStars,
|
||||
function (s, i, a, b) return a == b end)
|
||||
local optStarsLEIndent = Cmt(Cb("indent") * optStars,
|
||||
function (s, i, backtrack, this)
|
||||
return string.len(this) <= string.len(backtrack)
|
||||
end)
|
||||
|
||||
|
||||
local ink = P({
|
||||
"lines",
|
||||
|
||||
knott = Ct(knotHead * (V'line'-knotHead)^0 * wh),
|
||||
stitch = Ct(stitchHead * (V'line'-stitchHead)^0 * wh),
|
||||
knotKnot = Ct(knotHead * (V'line'-knotHead)^0 * wh),
|
||||
knotStitch = Ct(stitchHead * (V'line'-stitchHead)^0 * wh),
|
||||
knot = V'knotKnot' + V'knotStitch',
|
||||
|
||||
stmt = glue + divert + V'knott' + V'stitch' + optionDiv + comm,
|
||||
stmt = glue + divert + V'knot' + optDiv + comm,
|
||||
text = C((1-nl-V'stmt')^1) *wh,
|
||||
textE = C((1-nl-V'stmt')^0) *wh,
|
||||
|
||||
optionAnsWithDiv = V'textE' * optionDiv * V'textE' * wh,
|
||||
optionAnsWithoutDiv = V'textE' * Cc ''* Cc ''* wh, -- huh?
|
||||
optionHead = P'*'/'option' * sp * (V'optionAnsWithDiv' + V'optionAnsWithoutDiv'),
|
||||
option = Ct(V'optionHead' * (V'line'-V'optionHead'-V'knott'-V'stitch')^0 * wh), --TODO which can by toplevel only?
|
||||
choice = Ct(Cc'choice' * V'option'^1),
|
||||
optAnsWithDiv = V'textE' * optDiv * V'textE' * wh,
|
||||
optAnsWithoutDiv = V'textE' * Cc ''* Cc ''* wh, -- huh?
|
||||
optAns = V'optAnsWithDiv' + V'optAnsWithoutDiv',
|
||||
|
||||
-- TODO clean this
|
||||
opt = Cg(optStars,'indent') *
|
||||
Ct(Cc'option' * sp * V'optAns' * (V'line'-V'optLEIndent'-V'knot')^0 * wh), --TODO which can by toplevel only?
|
||||
optSameIndent = Ct(Cc'option' * optStarsSameIndent * sp * V'optAns' * (V'line'-V'optLEIndent'-V'knot')^0 * wh),
|
||||
optLEIndent = Ct(Cc'option' * optStarsLEIndent * sp * V'optAns' * (V'line'-V'optLEIndent'-V'knot')^0 * wh),
|
||||
|
||||
opts = (V'opt'*V'optSameIndent'^0),
|
||||
|
||||
choice = Ct(Cc'choice' * V'opts')/function(t) t.indent=nil; return t end,
|
||||
|
||||
para = Ct(Cc'para' * V'text'),
|
||||
|
||||
|
|
6
test.lua
6
test.lua
|
@ -12,13 +12,17 @@ function testText() doTestS(
|
|||
{{"para", "Hello world"}}
|
||||
) end
|
||||
|
||||
function testOptS() doTestS(
|
||||
function testOpt1() doTestS(
|
||||
'* "I am somewhat tired[."]," I repeated.',
|
||||
{{'choice', {"option", '"I am somewhat tired', '."', '," I repeated.'}}}
|
||||
) end
|
||||
|
||||
|
||||
function testBasic() doTest('basic') end
|
||||
function testChoices() doTest('choices') end
|
||||
function testNest() doTest('nested') end
|
||||
function testNest2() doTest('nested2') end
|
||||
function testKnot() doTest('knot') end
|
||||
|
||||
|
||||
|
||||
|
|
30
test/knot.lua
Normal file
30
test/knot.lua
Normal file
|
@ -0,0 +1,30 @@
|
|||
return {
|
||||
ink=[[
|
||||
=== the_orient_express ===
|
||||
= in_first_class
|
||||
...
|
||||
= in_third_class
|
||||
...
|
||||
= in_the_guards_van
|
||||
...
|
||||
...
|
||||
= missed_the_train
|
||||
...
|
||||
== the_orient_express =
|
||||
======== the_orient_express ===
|
||||
=stitch
|
||||
|
||||
]],
|
||||
expected={
|
||||
{
|
||||
"knot",
|
||||
"the_orient_express",
|
||||
{"stitch", "in_first_class", {"para", "..."}},
|
||||
{"stitch", "in_third_class", {"para", "..."}},
|
||||
{"stitch", "in_the_guards_van", {"para", "..."}, {"para", "..."}},
|
||||
{"stitch", "missed_the_train", {"para", "..."}}
|
||||
},
|
||||
{"knot", "the_orient_express"},
|
||||
{"knot", "the_orient_express", {"stitch", "stitch"}}
|
||||
}
|
||||
}
|
19
test/nested.lua
Normal file
19
test/nested.lua
Normal file
|
@ -0,0 +1,19 @@
|
|||
return {
|
||||
ink=[[
|
||||
* "Murder!"
|
||||
** A
|
||||
* "Suicide!"
|
||||
]], expected= {
|
||||
{
|
||||
"choice",
|
||||
{
|
||||
"option",
|
||||
'"Murder!"',
|
||||
"",
|
||||
"",
|
||||
{"choice", {"option", "A", "", ""}}
|
||||
},
|
||||
{"option", '"Suicide!"', "", ""}
|
||||
}
|
||||
}
|
||||
}
|
31
test/nested2.lua
Normal file
31
test/nested2.lua
Normal file
|
@ -0,0 +1,31 @@
|
|||
return {
|
||||
ink=[[
|
||||
"Well, Poirot? Murder or suicide?"
|
||||
* "Murder"
|
||||
"And who did it?"
|
||||
* * "Detective-Inspector Japp!"
|
||||
* * "Captain Hastings!"
|
||||
* * "Myself!"
|
||||
* "Suicide"
|
||||
]],
|
||||
expected = {
|
||||
{"para", '"Well, Poirot? Murder or suicide?"'},
|
||||
{
|
||||
"choice",
|
||||
{
|
||||
"option",
|
||||
'"Murder"',
|
||||
"",
|
||||
"",
|
||||
{"para", '"And who did it?"'},
|
||||
{
|
||||
"choice",
|
||||
{"option", '"Detective-Inspector Japp!"', "", ""},
|
||||
{"option", '"Captain Hastings!"', "", ""},
|
||||
{"option", '"Myself!"', "", ""}
|
||||
}
|
||||
},
|
||||
{"option", '"Suicide"', "", ""}
|
||||
}
|
||||
}
|
||||
}
|
2
util.lua
2
util.lua
|
@ -37,6 +37,7 @@ end
|
|||
|
||||
function tprint (tbl, indent)
|
||||
if not indent then indent = 0 end
|
||||
if type(tbl)=='string' then print(tbl); return end
|
||||
for k, v in pairs(tbl) do
|
||||
formatting = string.rep(" ", indent) .. k .. ": "
|
||||
if type(v) == "table" then
|
||||
|
@ -56,4 +57,3 @@ function read(file)
|
|||
f:close()
|
||||
return content
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue