Last active
December 27, 2015 12:49
-
-
Save Mouq/7328659 to your computer and use it in GitHub Desktop.
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
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