1
0
Fork 0
mirror of https://github.com/Oreolek/ink-instead.git synced 2024-04-28 15:19:25 +03:00

flatparse wip

This commit is contained in:
premek 2017-08-08 16:28:55 +02:00
parent 668d2fba2e
commit 5a6afdb72f
4 changed files with 38 additions and 64 deletions

View file

@ -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)
})

View file

@ -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"}
}}

View file

@ -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'
}
}

View file

@ -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"}
}
}