Skip to content

Instantly share code, notes, and snippets.

@bduggan
Created September 13, 2016 13:03
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 bduggan/ed64cfc6158214263f641e9009628878 to your computer and use it in GitHub Desktop.
Save bduggan/ed64cfc6158214263f641e9009628878 to your computer and use it in GitHub Desktop.
#!/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