From 8a8856db1d36fa6c891c8607b13298c9bedebc6c Mon Sep 17 00:00:00 2001 From: premek Date: Wed, 9 Aug 2017 11:36:14 +0200 Subject: [PATCH] flat parser --- pink/parser.lua | 9 +++--- test/parser/basic.lua | 15 ++-------- test/parser/branching.lua | 59 +++++++++++++-------------------------- test/parser/choices.lua | 34 +++++++--------------- test/parser/comments.lua | 34 ++++++++++++++++++++++ test/parser/glue.lua | 4 +-- test/parser/nested.lua | 18 +++++------- test/parser/nested2.lua | 23 ++++----------- test/parser/tags.lua | 13 ++++++++- test/test.lua | 7 ++--- 10 files changed, 99 insertions(+), 117 deletions(-) create mode 100644 test/parser/comments.lua diff --git a/pink/parser.lua b/pink/parser.lua index 4351e6a..3a9b3f9 100644 --- a/pink/parser.lua +++ b/pink/parser.lua @@ -4,7 +4,6 @@ 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 parserLogger = print local eof = -1 local sp = S" \t" ^0 + eof local wh = S" \t\r\n" ^0 + eof @@ -12,9 +11,9 @@ local nl = S"\r\n" ^1 + eof local id = (lpeg.alpha + '_') * (lpeg.alnum + '_')^0 local addr = C(id) * ('.' * C(id))^-1 -local todo = sp * 'TODO:' * sp * (1-nl)^0 / parserLogger * wh -- TODO log location -local commOL = sp * '//' * sp * (1-nl)^0 * wh -local commML = sp * '/*' * wh * (P(1)-'*/')^0 * '*/' * wh +local todo = Ct(sp * 'TODO:'/"todo" * sp * C((1-nl)^0)) * wh +local commOL = Ct(sp * '//'/"comment" * sp * C((1-nl)^0)) * wh +local commML = Ct(sp * '/*'/"comment" * wh * C((P(1)-'*/')^0)) * '*/' * wh local comm = commOL + commML + todo local glue = Ct(P'<>'/'glue') *wh -- FIXME do not consume spaces after glue @@ -30,7 +29,7 @@ local stitch = Ct(P('=')^1/'stitch' * wh * C(id) * wh * P('=')^0) * wh local optDiv = '[' * C((P(1) - ']')^0) * ']' -local optStars = wh * C(P'*') * (sp * C'*')^0 +local optStars = wh * Ct(C'*' * (sp * C'*')^0)/table.getn local tag = Ct(wh * P('#')/'tag' * wh * V'text' * wh) diff --git a/test/parser/basic.lua b/test/parser/basic.lua index dd6e941..77df0d6 100644 --- a/test/parser/basic.lua +++ b/test/parser/basic.lua @@ -2,21 +2,10 @@ return { ink=[[ Hello, world! Hello? -//TODO: this is a test todo-item - "What do you make of this?" she asked. - -// Something unprintable... - "I couldn't possibly comment," I replied. - -/* - ... or an unlimited block of text -*/ -we <> /* he -asdf -*/ hurr ied-> to_savile_row // comm - +we <> +hurr ied-> to_savile_row === to_savile_row === diff --git a/test/parser/branching.lua b/test/parser/branching.lua index 59f6429..bfcc0ab 100644 --- a/test/parser/branching.lua +++ b/test/parser/branching.lua @@ -27,46 +27,27 @@ He insisted that we hurried home to Savile Row <> as fast as we could. ]], expected= { + {"knot", "back_in_london"}, + {"para", "We arrived into London at 9.45pm exactly."}, + {"option", 1, '"There is not a moment to lose!"', "", " I declared."}, + {"divert", "hurry_outside"}, + {"option", 1, '"Monsieur, let us savour this moment!"', "", " I declared."}, { - "knot", - "back_in_london", - {"para", "We arrived into London at 9.45pm exactly."}, - { - "choice", - { - "option", - '"There is not a moment to lose!"', - "", - " I declared.", - {"divert", "hurry_outside"} - }, - { - "option", - '"Monsieur, let us savour this moment!"', - "", - " I declared.", - { - "para", - "My master clouted me firmly around the head and dragged me out of the door." - }, - 'glue', - {"divert", "dragged_outside"} - }, - {"option", "", "We hurried home", " ", {"divert", "hurry_outside"}} - } + "para", + "My master clouted me firmly around the head and dragged me out of the door." }, - { - "knot", - "hurry_outside", - {"para", "We hurried home to Savile Row "}, - {"divert", "as_fast_as_we_could"} - }, - { - "knot", - "dragged_outside", - {"para", "He insisted that we hurried home to Savile Row"}, - {"divert", "as_fast_as_we_could"} - }, - {"knot", "as_fast_as_we_could", "glue", {"para", "as fast as we could."}} + {"glue"}, + {"divert", "dragged_outside"}, + {"option", 1, "", "We hurried home", " "}, + {"divert", "hurry_outside"}, + {"knot", "hurry_outside"}, + {"para", "We hurried home to Savile Row "}, + {"divert", "as_fast_as_we_could"}, + {"knot", "dragged_outside"}, + {"para", "He insisted that we hurried home to Savile Row"}, + {"divert", "as_fast_as_we_could"}, + {"knot", "as_fast_as_we_could"}, + {"glue"}, + {"para", "as fast as we could."} } } diff --git a/test/parser/choices.lua b/test/parser/choices.lua index d11c1ae..dcd107f 100644 --- a/test/parser/choices.lua +++ b/test/parser/choices.lua @@ -12,30 +12,16 @@ ink=[[ == finale == ]], expected= { - { - "knot", - "start", - { - "choice", - {"option", "I dont know", "", ""}, - { - "option", - '"I am somewhat tired', - '."', - '," I repeated.', - {"para", '"Really," he responded.'}, - {"para", '"How deleterious."'} - }, - { - "option", - '"Nothing, Monsieur!"', - "", - " I replied.", - {"para", '"Very good, *then."'} - }, - {"option", "I said no more", "", "", {"para", '"Ah,". "I see you"'}} - } - }, + {"knot", "start"}, + {"option", 1, "I dont know", "", ""}, + {"option", 1, '"I am somewhat tired', '."', '," I repeated.'}, + {"para", '"Really," he responded.'}, + {"para", '"How deleterious."'}, + {"option", 1, '"Nothing, Monsieur!"', "", " I replied."}, + {"para", '"Very good,'}, + {"option", 1, 'then."', "", ""}, + {"option", 1, "I said no more", "", ""}, + {"para", '"Ah,". "I see you"'}, {"knot", "finale"} } } diff --git a/test/parser/comments.lua b/test/parser/comments.lua new file mode 100644 index 0000000..156a198 --- /dev/null +++ b/test/parser/comments.lua @@ -0,0 +1,34 @@ +return { +ink=[[ +Hello? +TODO: this is a test todo-item + +"What do you make of this?" she asked. + +// Something unprintable... + +"I couldn't possibly comment," I replied. + +/* + ... or an unlimited block of text +*/ +we <> /* he +asdf +*/ hurr ied-> to_savile_row // comm + + +]], +expected= { + {"para", "Hello?"}, + {"todo", "this is a test todo-item"}, + {"para", '"What do you make of this?" she asked.'}, + {"comment", "Something unprintable..."}, + {"para", "\"I couldn't possibly comment,\" I replied."}, + {"comment", "... or an unlimited block of text\n"}, + {"para", "we "}, + {"glue"}, + {"comment", "he\nasdf\n"}, + {"para", "hurr ied"}, + {"divert", "to_savile_row"}, + {"comment", "comm"}, +}} diff --git a/test/parser/glue.lua b/test/parser/glue.lua index 624ac0e..f8bbb58 100644 --- a/test/parser/glue.lua +++ b/test/parser/glue.lua @@ -9,11 +9,11 @@ ink=[[ ]], expected={ - {"option", "*", '"Monsieur, let us savour this moment!"', "", " I declared."}, + {"option", 1, '"Monsieur, let us savour this moment!"', "", " I declared."}, {"para", "My master clouted me firmly around the head and dragged me out of the door. " }, {"glue"}, {"divert", "dragged_outside"}, - {"option", "*", "", "We hurried home", " "}, + {"option", 1, "", "We hurried home", " "}, {"divert", "hurry_outside"}, {"knot", "as_fast_as_we_could"}, {"glue"}, diff --git a/test/parser/nested.lua b/test/parser/nested.lua index 76d9a53..d4e0489 100644 --- a/test/parser/nested.lua +++ b/test/parser/nested.lua @@ -2,18 +2,14 @@ return { ink=[[ * "Murder!" ** A + * * A * "Suicide!" ]], expected= { - { - "choice", - { - "option", - '"Murder!"', - "", - "", - {"choice", {"option", "A", "", ""}} - }, - {"option", '"Suicide!"', "", ""} - } + + {"option", 1, '"Murder!"', "", ""}, + {"option", 2, "A", "", ""}, + {"option", 2, "A", "", ""}, + {"option", 1, '"Suicide!"', "", ""} + } } diff --git a/test/parser/nested2.lua b/test/parser/nested2.lua index df89d2e..a90f390 100644 --- a/test/parser/nested2.lua +++ b/test/parser/nested2.lua @@ -10,22 +10,11 @@ ink=[[ ]], 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"', "", ""} - } + {"option", 1, '"Murder"', "", ""}, + {"para", '"And who did it?"'}, + {"option", 2, '"Detective-Inspector Japp!"', "", ""}, + {"option", 2, '"Captain Hastings!"', "", ""}, + {"option", 2, '"Myself!"', "", ""}, + {"option", 1, '"Suicide"', "", ""} } } diff --git a/test/parser/tags.lua b/test/parser/tags.lua index b348374..305a175 100644 --- a/test/parser/tags.lua +++ b/test/parser/tags.lua @@ -9,10 +9,21 @@ ink=[[ # require: Train ticket This is the line of content. # the third tag # really_monsieur.ogg - +#tag aaa ]], expected={ + {"tag", "author: Joseph Humfrey"}, + {"tag", "title: My Wonderful Ink Story"}, + {"knot", "content"}, + {"tag", "location: Germany"}, + {"tag", "overview: munich.ogg"}, + {"tag", "require: Train ticket"}, + {"para", "This is the line of content."}, + {"tag", "the third tag"}, + {"tag", "really_monsieur.ogg"}, + {"tag", "tag"}, + {"para", "aaa"} } } diff --git a/test/test.lua b/test/test.lua index 8ec7f53..ba33444 100644 --- a/test/test.lua +++ b/test/test.lua @@ -15,13 +15,10 @@ function testText() doTestS( {{"para", "Hello world"}} ) end -function testOpt1() doTestS( - '* "I am somewhat tired[."]," I repeated.', - {{'choice', {"option", '"I am somewhat tired', '."', '," I repeated.'}}} -) end function testBasic() doTest('basic') end +function testComments() doTest('comments') end function testChoices() doTest('choices') end function testNest() doTest('nested') end function testNest2() doTest('nested2') end @@ -29,7 +26,7 @@ function testKnot() doTest('knot') end function testBranching() doTest('branching') end function testGlue() doTest('glue') end function testInclude() doTest('include') end -function testInclude() doTest('tags') end +function testTagsP() doTest('tags') end --- runtime ---