Skip to content

Instantly share code, notes, and snippets.

@zoffixznet

zoffixznet/p6.p6 Secret

Created March 7, 2017 17:48
Show Gist options
  • Save zoffixznet/1e35289973657e00f4d78f17b4a68513 to your computer and use it in GitHub Desktop.
Save zoffixznet/1e35289973657e00f4d78f17b4a68513 to your computer and use it in GitHub Desktop.
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 ]>+ )
[ <?{ $/[0] eq 'dev' }> || {die "Don't know what $/[0] category is"} ]
'>' \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;
}
}
say Law.parse: q:to/END/
<dev>
key value
END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment