Created
September 13, 2016 13:03
-
-
Save bduggan/ed64cfc6158214263f641e9009628878 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
#!/usr/bin/env perl6 | |
# parse a little slim (<http://slim-lang.com>) | |
grammar slim { | |
rule TOP { <line>+ %% <eol>} | |
token line { <indentation> <tag> [ ' ' <text> ]? } | |
token indentation { <indent>* } | |
token indent { ' ' } | |
token tag { \w+ } | |
token text { \V+ } | |
token eol { \n+ } | |
} | |
class slim-actions { | |
my @stack; | |
method TOP($/) { | |
while my $tag = @stack.pop { | |
say (" " x @stack ) ~ "</$tag>"; | |
} | |
} | |
method line($/) { | |
push @stack, ~$<tag>; | |
say (" " x @stack-1 ) ~ "<" ~ $<tag> ~ ">"; | |
say (" " x @stack-1 ) ~ $<text> if $<text>; | |
} | |
method indentation($/) { | |
my $level = @$<indent>; | |
for 1..(@stack - $level) { | |
my $tag = pop @stack; | |
say (" " x @stack) ~ "</$tag>"; | |
} | |
} | |
} | |
slim.parse(q:to/DONE/,actions => slim-actions.new) or die 'no parse'; | |
html | |
head | |
title Slim Examples | |
body | |
h1 Markup Examples | |
DONE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment