Created
November 28, 2020 12:24
-
-
Save samebchase/82df3d17341898e054828ea1f7728f87 to your computer and use it in GitHub Desktop.
realistic-ns-with-actions.raku
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
#!/usr/bin/env raku | |
use v6.d; | |
#use Grammar::Tracer; | |
grammar RealisticNs { | |
token TOP { <realistic-ns> } | |
token realistic-ns { <lparen> | |
<ns-keyword> <.ws> <ns-name> <.ws> | |
<require-form> | |
<rparen> } | |
token ns-keyword { 'ns' } | |
token ns-name { <.ns-name-component>+ % '.' } | |
token ns-name-component { ( <.alnum> | '-' )+ } | |
token require-form { <lparen> | |
<require-keyword> <ws>? <ns-imports> | |
<rparen> } | |
token require-keyword { ':require' } | |
token ns-imports { <ns-import>+ % <.ws> } | |
token ns-import { <lsquare> | |
<imported-ns-name> <.ws> ':as' <.ws> <ns-nickname> | |
<rsquare> } | |
token imported-ns-name { <.ns-name-component>+ % '.' } | |
token ns-nickname { <.alnum>+ } | |
token lsquare { '[' } | |
token rsquare { ']' } | |
token lparen { '(' } | |
token rparen { ')' } | |
} | |
class RealisticNsActions { | |
has $!ns-name; | |
has $!imported-namespaces = SetHash.new; | |
has %!ns-nicknames; | |
method TOP($/) { | |
make { | |
ns-name => $!ns-name, | |
ns-imports => $!imported-namespaces, | |
ns-nicknames => %!ns-nicknames | |
} | |
} | |
method ns-name($/) { | |
$!ns-name = $/.Str; | |
} | |
method ns-import($match) { | |
#say $match; | |
my $imported-ns-name = $match<imported-ns-name>.Str; | |
my $ns-nickname = $match<ns-nickname>.Str; | |
$!imported-namespaces{$imported-ns-name}++; | |
%!ns-nicknames{$imported-ns-name} = $ns-nickname; | |
} | |
} | |
sub MAIN() { | |
my $s = RealisticNs.parse(slurp("realistic.clj"), actions => RealisticNsActions.new); | |
#say $s; | |
say $s.made; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment