1
0
Fork 0
mirror of https://github.com/Oreolek/ink-instead.git synced 2024-06-30 22:04:58 +03:00

flat parser

This commit is contained in:
premek 2017-08-09 11:36:14 +02:00
parent 5a6afdb72f
commit 8a8856db1d
10 changed files with 99 additions and 117 deletions

View file

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

View file

@ -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 ===

View file

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

View file

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

34
test/parser/comments.lua Normal file
View file

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

View file

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

View file

@ -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!"', "", ""}
}
}

View file

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

View file

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

View file

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