From 5a6afdb72f10452c1486d6712125a966d2fa13b7 Mon Sep 17 00:00:00 2001 From: premek Date: Tue, 8 Aug 2017 16:28:55 +0200 Subject: [PATCH] flatparse wip --- pink/parser.lua | 37 ++++++++----------------------------- test/parser/basic.lua | 16 ++++++++-------- test/parser/glue.lua | 26 +++++++++----------------- test/parser/knot.lua | 23 +++++++++++++---------- 4 files changed, 38 insertions(+), 64 deletions(-) diff --git a/pink/parser.lua b/pink/parser.lua index 72c0f97..4351e6a 100644 --- a/pink/parser.lua +++ b/pink/parser.lua @@ -4,10 +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 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 @@ -21,27 +17,20 @@ local commOL = sp * '//' * sp * (1-nl)^0 * wh local commML = sp * '/*' * wh * (P(1)-'*/')^0 * '*/' * wh local comm = commOL + commML + todo -local glue = P'<>'/'glue' *wh -- FIXME do not consume spaces after glue +local glue = Ct(P'<>'/'glue') *wh -- FIXME do not consume spaces after glue local divertSym = '->' *wh -local divertEndSym = C('END') *wh +local divertEndSym = Ct(C('END')) *wh local divertEnd = divertSym * divertEndSym local divertJump = Ct(divertSym/'divert' * addr * wh) 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 knot = Ct(P('=')^2/'knot' * wh * C(id) * wh * P('=')^0) * wh +local stitch = Ct(P('=')^1/'stitch' * wh * C(id) * wh * P('=')^0) * wh 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 optStars = wh * C(P'*') * (sp * C'*')^0 local tag = Ct(wh * P('#')/'tag' * wh * V'text' * wh) @@ -50,11 +39,7 @@ local tag = Ct(wh * P('#')/'tag' * wh * V'text' * wh) local ink = P({ "lines", - 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'knot' + optDiv + comm + V'include' + tag, + stmt = glue + divert + knot + stitch + V'option' + optDiv + comm + V'include' + tag, text = C((1-nl-V'stmt')^1) *wh, textE = C((1-nl-V'stmt')^0) *wh, @@ -62,22 +47,16 @@ local ink = P({ 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), + option = Ct(Cc'option' * optStars * sp * V'optAns'), - opts = (V'opt'*V'optSameIndent'^0), - choice = Ct(Cc'choice' * V'opts')/function(t) t.indent=nil; return t end, include = Ct(P('INCLUDE')/'include' * wh * V'text' * wh), para = Ct(Cc'para' * V'text'), - line = V'stmt' + V'choice' + V'para', + line = V'stmt' + V'para', lines = Ct(V'line'^0) }) diff --git a/test/parser/basic.lua b/test/parser/basic.lua index 5034d45..dd6e941 100644 --- a/test/parser/basic.lua +++ b/test/parser/basic.lua @@ -33,14 +33,14 @@ expected= { {"para", '"What do you make of this?" she asked.'}, {"para", "\"I couldn't possibly comment,\" I replied."}, {"para", "we "}, - "glue", + {"glue"}, {"para", "hurr ied"}, {"divert", "to_savile_row"}, - { - "knot", - "to_savile_row", - {"para", "to Savile Row"}, - {"stitch", "st", {"para", "stiiii"}}, - {"stitch", "st2", {"para", "222stiiii "}, "END"} - } + {"knot", "to_savile_row"}, + {"para", "to Savile Row"}, + {"stitch", "st"}, + {"para", "stiiii"}, + {"stitch", "st2"}, + {"para", "222stiiii "}, + {"END"} }} diff --git a/test/parser/glue.lua b/test/parser/glue.lua index 0171984..624ac0e 100644 --- a/test/parser/glue.lua +++ b/test/parser/glue.lua @@ -9,22 +9,14 @@ ink=[[ ]], expected={ - { - "choice", - { - "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"}} - }, - {"knot", "as_fast_as_we_could", "glue", {"para", "as fast as we could."}} -- TODO should be space before 'as' + {"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"}, + {"knot", "as_fast_as_we_could"}, + {"glue"}, + {"para", "as fast as we could."} -- TODO should be space before 'as' } } diff --git a/test/parser/knot.lua b/test/parser/knot.lua index 872d6d9..e2ff435 100644 --- a/test/parser/knot.lua +++ b/test/parser/knot.lua @@ -16,15 +16,18 @@ ink=[[ ]], 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"}} + { "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"} } }