1
0
Fork 0
mirror of https://github.com/Oreolek/ink-instead.git synced 2024-05-12 14:08:24 +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.S, lpeg.C, lpeg.Ct, lpeg.Cc, lpeg.Cg, lpeg.Cb, lpeg.Cf, lpeg.Cmt,
lpeg.P, lpeg.V lpeg.P, lpeg.V
local concat = function (p)
return Cf(p, function (a,b) return a..b end)
end
local parserLogger = print local parserLogger = print
local eof = -1 local eof = -1
local sp = S" \t" ^0 + eof 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 commML = sp * '/*' * wh * (P(1)-'*/')^0 * '*/' * wh
local comm = commOL + commML + todo 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 divertSym = '->' *wh
local divertEndSym = C('END') *wh local divertEndSym = Ct(C('END')) *wh
local divertEnd = divertSym * divertEndSym local divertEnd = divertSym * divertEndSym
local divertJump = Ct(divertSym/'divert' * addr * wh) local divertJump = Ct(divertSym/'divert' * addr * wh)
local divert = divertEnd + divertJump local divert = divertEnd + divertJump
local knotHead = P('=')^2/'knot' * wh * C(id) * wh * P('=')^0 * wh local knot = Ct(P('=')^2/'knot' * wh * C(id) * wh * P('=')^0) * wh
local stitchHead = P('=')^1/'stitch' * 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 optDiv = '[' * C((P(1) - ']')^0) * ']'
local optStars = concat(wh * C(P'*') * (sp * C'*')^0) local optStars = 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 tag = Ct(wh * P('#')/'tag' * wh * V'text' * wh) 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({ local ink = P({
"lines", "lines",
knotKnot = Ct(knotHead * (V'line'-knotHead)^0 * wh), stmt = glue + divert + knot + stitch + V'option' + optDiv + comm + V'include' + tag,
knotStitch = Ct(stitchHead * (V'line'-stitchHead)^0 * wh),
knot = V'knotKnot' + V'knotStitch',
stmt = glue + divert + V'knot' + optDiv + comm + V'include' + tag,
text = C((1-nl-V'stmt')^1) *wh, text = C((1-nl-V'stmt')^1) *wh,
textE = C((1-nl-V'stmt')^0) *wh, textE = C((1-nl-V'stmt')^0) *wh,
@ -62,22 +47,16 @@ local ink = P({
optAnsWithoutDiv = V'textE' * Cc ''* Cc ''* wh, -- huh? optAnsWithoutDiv = V'textE' * Cc ''* Cc ''* wh, -- huh?
optAns = V'optAnsWithDiv' + V'optAnsWithoutDiv', optAns = V'optAnsWithDiv' + V'optAnsWithoutDiv',
-- TODO clean this option = Ct(Cc'option' * optStars * sp * V'optAns'),
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,
include = Ct(P('INCLUDE')/'include' * wh * V'text' * wh), include = Ct(P('INCLUDE')/'include' * wh * V'text' * wh),
para = Ct(Cc'para' * V'text'), para = Ct(Cc'para' * V'text'),
line = V'stmt' + V'choice' + V'para', line = V'stmt' + V'para',
lines = Ct(V'line'^0) lines = Ct(V'line'^0)
}) })

View file

@ -33,14 +33,14 @@ expected= {
{"para", '"What do you make of this?" she asked.'}, {"para", '"What do you make of this?" she asked.'},
{"para", "\"I couldn't possibly comment,\" I replied."}, {"para", "\"I couldn't possibly comment,\" I replied."},
{"para", "we "}, {"para", "we "},
"glue", {"glue"},
{"para", "hurr ied"}, {"para", "hurr ied"},
{"divert", "to_savile_row"}, {"divert", "to_savile_row"},
{ {"knot", "to_savile_row"},
"knot", {"para", "to Savile Row"},
"to_savile_row", {"stitch", "st"},
{"para", "to Savile Row"}, {"para", "stiiii"},
{"stitch", "st", {"para", "stiiii"}}, {"stitch", "st2"},
{"stitch", "st2", {"para", "222stiiii "}, "END"} {"para", "222stiiii "},
} {"END"}
}} }}

View file

@ -9,22 +9,14 @@ ink=[[
]], ]],
expected={ expected={
{ {"option", "*", '"Monsieur, let us savour this moment!"', "", " I declared."},
"choice", {"para", "My master clouted me firmly around the head and dragged me out of the door. " },
{ {"glue"},
"option", {"divert", "dragged_outside"},
'"Monsieur, let us savour this moment!"', {"option", "*", "", "We hurried home", " "},
"", {"divert", "hurry_outside"},
" I declared.", {"knot", "as_fast_as_we_could"},
{ {"glue"},
"para", {"para", "as fast as we could."} -- TODO should be space before 'as'
"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={ expected={
{ { "knot", "the_orient_express"},
"knot", {"stitch", "in_first_class"},
"the_orient_express", {"para", "..."},
{"stitch", "in_first_class", {"para", "..."}}, {"stitch", "in_third_class"},
{"stitch", "in_third_class", {"para", "..."}}, {"para", "..."},
{"stitch", "in_the_guards_van", {"para", "..."}, {"para", "..."}}, {"stitch", "in_the_guards_van"},
{"stitch", "missed_the_train", {"para", "..."}} {"para", "..."},
}, {"para", "..."},
{"knot", "the_orient_express"}, {"stitch", "missed_the_train"},
{"knot", "the_orient_express", {"stitch", "stitch"}} {"para", "..."},
{"knot", "the_orient_express"},
{"knot", "the_orient_express"},
{"stitch", "stitch"}
} }
} }