From 656ade95afc39a520f0ec8ee8a5f805e8e34f82f Mon Sep 17 00:00:00 2001 From: premek Date: Sun, 27 Nov 2016 22:58:43 +0100 Subject: [PATCH] knot,choices --- peg.lua | 50 +++++++++++++++++++++--------------------------- test/choices.ink | 6 +++--- test/content.ink | 2 +- util.lua | 2 +- 4 files changed, 27 insertions(+), 33 deletions(-) diff --git a/peg.lua b/peg.lua index 01b7140..5a09197 100644 --- a/peg.lua +++ b/peg.lua @@ -1,36 +1,30 @@ require "util" local lpeg = require "lpeg" -local S,C,P = lpeg.S, lpeg.C, lpeg.P - +lpeg.locale(lpeg) +local S,C,Ct,P = lpeg.S, lpeg.C, lpeg.Ct, lpeg.P local parserLogger = print -local function prefix(pref, p) - return p / function(i) - return pref..": "..i - end -end +local sp = S" \t" ^0 + -1 +local wh = S" \t\r\n" ^0 + -1 +local nl = S"\r\n" ^1 + -1 +local id = (lpeg.alpha + '_') * (lpeg.alnum + '_')^0 -local sp = S" \t" ^0 -local wh = S" \t\r\n" ^0 -local nl = S"\r\n" ^1 -local ch = P(1) - -local todo = 'TODO:' * sp * (ch-nl)^0 / parserLogger -- TODO log location -local commOL = '//' * sp * (ch-nl)^0 -- TODO comment that does not start at the line beginning -local commML = '/*' * wh * (ch-'*/')^0 * '*/' +local todo = sp * 'TODO:' * sp * (1-nl)^0 / parserLogger * wh -- TODO log location +local commOL = sp * '//' * sp * (1-nl)^0 * wh -- TODO comment that does not start at the line beginning +local commML = sp * '/*' * wh * (P(1)-'*/')^0 * '*/' * wh local comm = commOL + commML + todo +local para = C(((1-nl-'*') * (1-nl)^1)) *wh -- hm -local para = (C((ch-nl)^1) *nl^0)-comm +local knot = P('=')^2 * wh * C(id) * wh * P('=')^0 * wh +local choiceAnswer = '*' * sp * para/"CHOICE: %1" +local choiceBlock = Ct(choiceAnswer * para^0) +local choices = Ct(choiceBlock^1) +--local statement = * (comm + choices + para) *n +--local prog = ((n * lpeg.Ct((statement*n)^0)) )* -1 +--local ink = prog -local choiceAnswer = '*' * sp * prefix("CHOICE", para) -local choiceBlock = choiceAnswer * lpeg.Ct( (para- choiceAnswer)^0) -local choices = lpeg.Ct(choiceBlock^1) - -local statement = wh * (comm + choices + para) * wh - -local prog = ((wh * lpeg.Ct((statement*wh)^0)) )* -1 - -local ink = prog - -test(ink, 'content') -test(ink, 'choices') +--test(ink, 'content') +test(choices, 'choices') +--r=choices:match('*ans\nsss\nsss\n*second\n*second\n*second') +--print (to_string( (r))) +--tprint(r) diff --git a/test/choices.ink b/test/choices.ink index 8292588..68cce2a 100644 --- a/test/choices.ink +++ b/test/choices.ink @@ -1,10 +1,10 @@ -"What that's?" my master asked. * "I am somewhat tired[."]," I repeated. "Really," he responded. "How deleterious." * "Nothing, Monsieur!"[] I replied. - "Very good, then." + "Very good, *then." you hairy basterd * "I said, this journey is appalling[."] and I want no more of it." "Ah," he replied, not unkindly. "I see you are feeling frustrated. Tomorrow, things will improve." - // and then FIXME +// and then FIXME nothing + sdf diff --git a/test/content.ink b/test/content.ink index 1ed35eb..193c1af 100644 --- a/test/content.ink +++ b/test/content.ink @@ -1,7 +1,7 @@ Hello, world! Hello? Hello, are you there? -TODO: Write this section properly! +TODO:Write this section properly! "What do you make of this?" she asked. diff --git a/util.lua b/util.lua index 7baf6d6..43e94e5 100644 --- a/util.lua +++ b/util.lua @@ -40,7 +40,7 @@ function tprint (tbl, indent) for k, v in pairs(tbl) do formatting = string.rep(" ", indent) .. k .. ": " if type(v) == "table" then - print(formatting) + print(formatting .. 'table:') tprint(v, indent+1) elseif type(v) == 'boolean' then print(formatting .. tostring(v))