Skip to content

Instantly share code, notes, and snippets.

@Phrogz
Last active April 6, 2018 19:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Phrogz/f48c7f0fd541018735e8426cf565c81d to your computer and use it in GitHub Desktop.
Save Phrogz/f48c7f0fd541018735e8426cf565c81d to your computer and use it in GitHub Desktop.
{? 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.
{.}
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