Skip to content

Instantly share code, notes, and snippets.

@Mouq
Last active December 27, 2015 12:49
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 Mouq/7328659 to your computer and use it in GitHub Desktop.
Save Mouq/7328659 to your computer and use it in GitHub Desktop.
use v6;
grammar UnixPath {
token TOP { ^ [<context> '/']? (<name>|<glob>) +%% '/' $ }
proto token context {*}
token context:sym<~> { <sym> <?[/]> }
token context:sym</> { <?[/]> }
token name { <-[ * / ]>+ }
token glob { $<initial>=['*'?] [<name> '*']+ <final=.name>? }
}
class ExpandPath {
method TOP($/) {
my @ex = ~$<context>;
for @() {
if .keys eq 'name' {
@ex .= map: *~'/'~$_.values[0]
}
elsif .keys eq 'glob' {
@ex .= map: ->\so_far{
(so_far ~ '/') <<~<< dir(so_far).grep($_.values[0].ast)
}
}
}
make @ex;
}
method context:sym<~>($/) { make qx[echo ~].chomp ~ '/' }
method glob ($/) {
# make regex that matches possible files/directories
my $regex = $<initial> ?? rx/.*/ !! rx/''/;
for $<name> {
$regex = rx/<$regex> $_ .*/;
}
$regex = rx/<$regex> $_/ for $<final>;
make $regex
}
}
.say for UnixPath.parse(:actions(ExpandPath), '/usr/bin').ast
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment