From e494c4f855e70584bdda648f38d000ed2f50347e Mon Sep 17 00:00:00 2001 From: premek Date: Sun, 20 Aug 2017 18:39:01 +0200 Subject: [PATCH] gather parser, todo: runtime, gather+divert --- pink/parser.lua | 8 +++++-- test/parser/gather.lua | 54 ++++++++++++++++++++++++++++++++++++++++++ test/parser/nested.lua | 4 ++++ test/test.lua | 6 +++-- 4 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 test/parser/gather.lua diff --git a/pink/parser.lua b/pink/parser.lua index 5568bea..d05253c 100644 --- a/pink/parser.lua +++ b/pink/parser.lua @@ -28,7 +28,10 @@ local stitch = Ct(P('=')^1/'stitch' * wh * C(id) * wh * P('=')^0) * wh local optDiv = '[' * C((P(1) - ']')^0) * ']' -local optStars = wh * Ct(C'*' * (sp * C'*')^0)/table.getn +local optStar = sp * C'*' +local optStars = wh * Ct(optStar * optStar^0)/table.getn +local gatherMark = sp * C'-' +local gatherMarks = wh * Ct(gatherMark * gatherMark^0)/table.getn local hash = P('#') local tag = hash * wh * V'text' @@ -48,6 +51,7 @@ local ink = P({ optAns = V'optAnsWithDiv' + V'optAnsWithoutDiv', option = Ct(Cc'option' * optStars * sp * V'optAns'), + gather = Ct(Cc'gather' * gatherMarks * sp * V'text'), @@ -56,7 +60,7 @@ local ink = P({ para = tagAbove^0 * Ct(Cc'para' * V'text') * tagEnd^0 * wh + tagGlobal, - line = V'stmt' + V'para', + line = V'stmt' + V'gather'+ V'para' , lines = Ct(V'line'^0) }) diff --git a/test/parser/gather.lua b/test/parser/gather.lua new file mode 100644 index 0000000..bdc6cb3 --- /dev/null +++ b/test/parser/gather.lua @@ -0,0 +1,54 @@ +return { +ink=[[ +I looked at Monsieur Fogg +* ... and I could contain myself no longer. + 'What is the purpose of our journey, Monsieur?' + 'A wager,' he replied. + * * 'A wager!'[] I returned. + He nodded. + * * * 'But surely that is foolishness!' + * * * 'A most serious matter then!' + - - - He nodded again. + * * * 'But can we win?' + 'That is what we will endeavour to find out,' he answered. + * * * 'A modest wager, I trust?' + 'Twenty thousand pounds,' he replied, quite flatly. + * * * I asked nothing further of him then[.], and after a final, polite cough, he offered nothing more to me. <> + * * 'Ah[.'],' I replied, uncertain what I thought. + - - After that, <> +* ... but I said nothing[] and <> +- we passed the day in silence. +- -> END +]], expected= { + {"para", "I looked at Monsieur Fogg"}, + {"option", 1, "... and I could contain myself no longer.", "", ""}, + {"para", "'What is the purpose of our journey, Monsieur?'"}, + {"para", "'A wager,' he replied."}, + {"option", 2, "'A wager!'", "", " I returned."}, + {"para", "He nodded."}, + {"option", 3, "'But surely that is foolishness!'", "", ""}, + {"option", 3, "'A most serious matter then!'", "", ""}, + {"gather", 3, "He nodded again."}, + {"option", 3, "'But can we win?'", "", ""}, + {"para", "'That is what we will endeavour to find out,' he answered."}, + {"option", 3, "'A modest wager, I trust?'", "", ""}, + {"para", "'Twenty thousand pounds,' he replied, quite flatly."}, + { + "option", + 3, + "I asked nothing further of him then", + ".", + ", and after a final, polite cough, he offered nothing more to me. " + }, + {"glue"}, + {"option", 2, "'Ah", ".'", ",' I replied, uncertain what I thought."}, + {"gather", 2, "After that, "}, + {"glue"}, + {"option", 1, "... but I said nothing", "", " and "}, + {"glue"}, + {"gather", 1, "we passed the day in silence."}, + {"gather", 2, "> END"} -- FIXME! + + +} +} diff --git a/test/parser/nested.lua b/test/parser/nested.lua index d4e0489..585de9f 100644 --- a/test/parser/nested.lua +++ b/test/parser/nested.lua @@ -3,12 +3,16 @@ ink=[[ * "Murder!" ** A * * A + * * * B + *** B * "Suicide!" ]], expected= { {"option", 1, '"Murder!"', "", ""}, {"option", 2, "A", "", ""}, {"option", 2, "A", "", ""}, + {"option", 3, "B", "", ""}, + {"option", 3, "B", "", ""}, {"option", 1, '"Suicide!"', "", ""} } diff --git a/test/test.lua b/test/test.lua index 6c2a07a..0d474bc 100644 --- a/test/test.lua +++ b/test/test.lua @@ -27,6 +27,7 @@ function testBranching() doTest('branching') end function testGlue() doTest('glue') end function testInclude() doTest('include') end function testTags() doTest('tags') end +function testGather() doTest('gather') end --- runtime --- @@ -40,7 +41,7 @@ end function testRChoices() local story = pink.getStory('test/runtime/branching.ink') - story.choosePathString('back_in_london'); + story.choosePathString('back_in_london'); story.continue() luaunit.assertEquals(story.continue(), 'exactly') luaunit.assertFalse(story.canContinue) @@ -49,7 +50,8 @@ function testRChoices() luaunit.assertEquals(story.continue(), 'My master clouted me firmly around the head') luaunit.assertEquals(#story.currentChoices, 2) story.chooseChoiceIndex(2) - luaunit.assertEquals(story.continue(), 'huhu') + luaunit.assertEquals(story.continue(), 'huhu') + luaunit.assertFalse(story.canContinue) end