Skip to content

Instantly share code, notes, and snippets.

@tene
Created August 13, 2010 22:45
Show Gist options
  • Select an option

  • Save tene/523661 to your computer and use it in GitHub Desktop.

Select an option

Save tene/523661 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use Text::Balanced qw/extract_bracketed/;
use Data::Dumper;
my $in = $ARGV[0] || '(a.b.(foo|bar|baz).(lol|wtf|bbq))|frob';
my @all_assets;
print "given: $in\n\n";
sub set_intersection {
die "NYI\n";
}
sub set_union {
die "NYI\n";
}
sub set_subtraction {
die "NYI\n";
}
my $handler = {
'.' => sub {
my $i = shift;
set_intersection(handle($i->{'left'}), handle($i->{'right'}));
},
'|' => sub {
my $i = shift;
set_union(handle($i->{'left'}), handle($i->{'right'}));
},
'!' => sub {
my $i = shift;
set_subtraction(@all_assets, handle($i->{'right'}));
}
};
sub fetch_assets_for {
die "NYI\n";
}
sub handle {
my $i = shift;
if (ref $i) {
return $handler->{$i->{'op'}}->($i);
}
else {
return fetch_assets_for($i);
}
}
sub consume_item {
my $text = shift;
my $neg;
if ($text =~ /^!(.*)/) {
$neg = 1;
$text = $1;
}
my ($item, $rest) = extract_bracketed($text, '()');
if ($item) {
print STDERR "bracketed: $item\n";
$item =~ m/^\((.*)\)$/; # unwrap outer parens
$item = walk($1);
print STDERR "got item: " . Dumper($item) . "\n";
}
else {
($item, $rest) = ($text =~ m/^(\w+)([|.].*)?$/);
print STDERR "got item: $item\n";
}
if ($neg) {
$item = {
item => $item,
op => '!',
};
}
return ($item, $rest);
}
sub consume_op {
my $text = shift;
return (split(/ */, $text, 2)); # first char, rest of the string
}
sub walk {
my $text = shift;
my $ret;
(my $item, $text) = consume_item($text);
$ret = $item;
while ($text) {
(my $op, $text) = consume_op($text);
print STDERR "got op: $op\n";
(my $next, $text) = consume_item($text);
print STDERR "still left: $text\n\n";
$ret = {
left => $ret,
right => $next,
op => $op,
};
}
return $ret;
}
my $a = walk($in);
print Dumper($a);
#!/usr/bin/perl
use strict;
use Text::Balanced qw/extract_bracketed/;
use Data::Dumper;
my $in = $ARGV[0] || '(a.b.(foo|bar|baz).(lol|wtf|bbq))|frob';
print "given: $in\n\n";
sub consume_item {
my $text = shift;
my ($item, $rest) = extract_bracketed($text, '()');
if ($item) {
print STDERR "bracketed: $item\n";
$item =~ m/^\((.*)\)$/; # unwrap outer parens
$item = walk($1);
print STDERR "got item: " . Dumper($item) . "\n";
}
else {
($item, $rest) = ($text =~ m/^(\w+)([|.].*)?$/);
print STDERR "got item: $item\n";
}
return ($item, $rest);
}
sub consume_op {
my $text = shift;
return (split(/ */, $text, 2)); # first char, rest of the string
}
sub walk {
my $text = shift;
my $ret;
(my $item, $text) = consume_item($text);
$ret = $item;
while ($text) {
(my $op, $text) = consume_op($text);
print STDERR "got op: $op\n";
(my $next, $text) = consume_item($text);
print STDERR "still left: $text\n\n";
$ret = {
left => $ret,
right => $next,
op => $op,
};
}
return $ret;
}
my $a = walk($in);
print Dumper($a);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment