Skip to content

Instantly share code, notes, and snippets.

Created March 7, 2017 17:38
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 anonymous/2192a932c2e03b230f92cef5e5badfbc to your computer and use it in GitHub Desktop.
Save anonymous/2192a932c2e03b230f92cef5e5badfbc to your computer and use it in GitHub Desktop.
Perl6 Grammar example
<main>
key value
<dev>
key value
class Law::Actions {
method key($/) { make $/.Str }
method value($/) { make $/.Str }
method object($/) { make $/[0].Str }
method law($/) { make $<key>.made => $<value>.made }
method block($/) { make $<law>.map({ .made }).hash }
method realm($/) { make $<object>.made => $<block>.made }
method TOP($/) { make $<realm>.map: { .made } }
}
grammar Law {
token key { \w+ }
token value { <!before \s> <-[\n #]>+ <!after \s> }
token law { <key> <space>+ <value> \n+ }
token object { '<' ( <-[ \< \> \n ]>+ ) '>' \n+ }
token comment { '#' \N*\n+ }
token block { [<law> | <comment>]* }
token realm { <object> <block> }
token TOP { <realm>* }
method create(Str $laws) {
my $m = self.parsefile($laws, :actions(Law::Actions));
die "Usage:" unless $m;
return $m.made;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment