aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Phillips <david@sighup.nz>2018-09-11 22:05:15 +1200
committerDavid Phillips <david@sighup.nz>2018-09-11 22:05:19 +1200
commitdee7707befbf7c4f75a69bec289c2adf042d67e5 (patch)
tree9dada2f15317d56b5066f3bb62b6d14be114d958
parent49afc055b739fc25645f5d8da86f339d2b8a7525 (diff)
downloadidalius-dee7707befbf7c4f75a69bec289c2adf042d67e5.tar.xz
Map: Implement rough list parser
-rw-r--r--Plugin/Map.pm55
1 files changed, 51 insertions, 4 deletions
diff --git a/Plugin/Map.pm b/Plugin/Map.pm
index fd2c504..2e811cf 100644
--- a/Plugin/Map.pm
+++ b/Plugin/Map.pm
@@ -19,14 +19,61 @@ sub configure {
return $self;
}
+sub parse_list {
+ my ($input) = @_;
+ my @res;
+ my $i = 0;
+
+ # Index of the start of the current item
+ my $item_i = 0;
+
+ # Level of nested lists, 1 being the minimum
+ my $nest = 1;
+
+ # Are we currently lexing inside a string literal?
+ my $is_string = 0;
+
+ return ("Error: expected [", undef) unless substr($input, $i, 1) eq "[";
+ $i++;
+ $item_i = $i;
+
+ while ($nest != 0 && $i < length($input)) {
+ my $c = substr($input, $i, 1);
+
+ if ($c eq "[") {
+ $nest++;
+ } elsif ($c eq "]") {
+ $nest--;
+ }
+
+ if ($c eq "," || ($nest == 0 and $c eq "]")) {
+ my $item = substr($input, $item_i, $i - $item_i);
+ $item =~ s/^\s+|\s+$//g;
+ push @res, $item;
+ $item_i = $i+1;
+ }
+ $i++;
+ }
+
+ return ("Error: expected ], got end of line", undef) unless $nest == 0;
+
+ return (undef, @res);
+}
+
sub map {
my ($self, $irc, $logger, $who, $where, $rest, @arguments) = @_;
- my ($command, $subjects) = ($rest =~ /^(.+?)\s+(.*)$/);
+ my ($command, $subjects_raw) = ($rest =~ /^(.+?)\s+(.*)$/);
- return "[]" unless $subjects;
+ return "Syntax: map command [item1, item2, ...]" unless $command and $subjects_raw;
- my @array = map { $run_command->("$command $_", $who, $where) } (split /,/, $subjects);
+ my ($e, @subjects) = parse_list($subjects_raw);
+ if ($e) {
+ print "It's error";
+ }
+ $logger->("Error: $e");
+ return $e if $e;
- return "[" . (join ", ", @array). "]";
+ my @results = map { $run_command->("$command $_", $who, $where) } @subjects;
+ return "[" . (join ", ", @results). "]";
}
1;