Last active
April 6, 2018 19:36
-
-
Save Phrogz/f48c7f0fd541018735e8426cf565c81d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{? trollLocation="cave" } | |
There is a vicious troll glaring at you. | |
{|} | |
The air smells bad here, like rotting meat. | |
{.} | |
{? canteenLocation="cave" } | |
There is a canteen on the ground. | |
{.} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SafeTemplate < Parslet::Parser | |
# ... | |
rule(:cond) do | |
test.as(:test) >> markup.as(:out) | |
>> (elif.as(:test) >> markup.as(:out)).repeat.as(:elifs) | |
>> (ells >> markup.as(:elseout)).maybe | |
>> stop | |
end | |
# ... | |
end | |
class Transform < Parslet::Transform | |
rule(test:simple(:test), out:simple(:out), elifs:subtree(:elifs)) do | |
if test | |
out | |
elsif valid = elifs.find{ |h| h[:test] } | |
valid[:out] | |
end.to_s | |
end | |
rule(test:simple(:test), out:simple(:out), elifs:subtree(:elifs), elseout:simple(:elseout)) do | |
if test | |
out | |
elsif valid = elifs.find{ |h| h[:test] } | |
valid[:out] | |
else | |
elseout | |
end.to_s | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment