Skip to content

Instantly share code, notes, and snippets.

@lopnor
Created December 19, 2010 08:52
Show Gist options
  • Save lopnor/747207 to your computer and use it in GitHub Desktop.
Save lopnor/747207 to your computer and use it in GitHub Desktop.
use v6;
grammar Bar {
regex TOP { <header> [ <crlf> ** 2 <body> ]? }
token header { <pair> ** <crlf> }
regex crlf { \r?\n }
regex pair { <key=.body> ':'\s* <value=.body> }
token body { \w+ }
}
class Bar::Actions {
method TOP($/) {
if ($<body>) {
make {header => $<header>.ast, body => $<body>[0].ast};
} else {
make {header => $<header>.ast};
}
}
method header($/) { make Hash.new(|$<pair>».ast) }
method pair($/) { make $<key>.ast => $<value>.ast }
method body($/) { make ~$/ }
}
my $m = Bar.parse("foo: bar\na:b\r\n\r\nfoobar", actions => Bar::Actions) or die;
say $m.ast.perl;
# vim: ft=perl6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment