From dee7707befbf7c4f75a69bec289c2adf042d67e5 Mon Sep 17 00:00:00 2001 From: David Phillips Date: Tue, 11 Sep 2018 22:05:15 +1200 Subject: Map: Implement rough list parser --- Plugin/Map.pm | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file 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; -- cgit v1.1