Skip to content

Instantly share code, notes, and snippets.

@samebchase
Created November 28, 2020 12:24
Show Gist options
  • Save samebchase/82df3d17341898e054828ea1f7728f87 to your computer and use it in GitHub Desktop.
Save samebchase/82df3d17341898e054828ea1f7728f87 to your computer and use it in GitHub Desktop.
realistic-ns-with-actions.raku
#!/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