Created
March 7, 2017 17:38
-
-
Save anonymous/2192a932c2e03b230f92cef5e5badfbc to your computer and use it in GitHub Desktop.
Perl6 Grammar example
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
<main> | |
key value | |
<dev> | |
key value |
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
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