aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Phillips <david@sighup.nz>2018-09-24 21:11:59 +1200
committerDavid Phillips <david@sighup.nz>2018-09-24 21:22:54 +1200
commit368c962f722853ee0ba371d37e5ba0396e83b900 (patch)
treed4695371701c57a820364c871bf52a1354442efc
parentd77856c7e0ad3f00fd5d6a21b454511081815c21 (diff)
downloadidalius-368c962f722853ee0ba371d37e5ba0396e83b900.tar.xz
Remove magic positioning from parse_list and friends
-rw-r--r--IdaliusConfig.pm20
-rw-r--r--ListParser.pm51
-rw-r--r--Plugin/Map.pm6
3 files changed, 48 insertions, 29 deletions
diff --git a/IdaliusConfig.pm b/IdaliusConfig.pm
index d51a197..03adb11 100644
--- a/IdaliusConfig.pm
+++ b/IdaliusConfig.pm
@@ -66,11 +66,11 @@ sub check_config
}
sub parse_die {
- my ($supplementary, $from, $to, $line) = @_;
- my $pad = " " x ($from + 1);
- my $underline = "^" x ($to - $from + 1);
+ my ($parsed, $line) = @_;
+ my $pad = " " x ($parsed->{column_start} + 1);
+ my $underline = "^" x ($parsed->{column_end} - $parsed->{column_start} + 1);
- die "$supplementary:\n$line\n$pad$underline\n";
+ die "$parsed->{error}:\n$line\n$pad$underline\n";
}
sub parse_config
@@ -84,13 +84,13 @@ sub parse_config
# Detect list or hash config option
my $c = substr $config->{$section}->{$opt}, 0, 1;
if ($c eq "[") {
- my (($error, $from, $to), @listified) = ListParser::parse_list($config->{$section}->{$opt}, 0);
- parse_die ($error, $from, $to, $config->{$section}->{$opt}) if $error;
- $config->{$section}->{$opt} = \@listified;
+ my $parsed = ListParser::parse_list($config->{$section}->{$opt}, 0);
+ parse_die($parsed, $config->{$section}->{$opt}) if $parsed->{error};
+ $config->{$section}->{$opt} = $parsed->{array};
} elsif ($c eq "{") {
- my (($error, $from, $to), %hashified) = ListParser::parse_list($config->{$section}->{$opt}, 1);
- parse_die ($error, $from, $to, $config->{$section}->{$opt}) if $error;
- $config->{$section}->{$opt} = \%hashified;
+ my $parsed = ListParser::parse_list($config->{$section}->{$opt}, 1);
+ parse_die($parsed, $config->{$section}->{$opt}) if $parsed->{error};
+ $config->{$section}->{$opt} = $parsed->{hash};
}
}
}
diff --git a/ListParser.pm b/ListParser.pm
index e69eee3..099b1a1 100644
--- a/ListParser.pm
+++ b/ListParser.pm
@@ -30,25 +30,36 @@ sub parse_mapping {
if ($c =~ /\s/) {
# allow whitespace
} elsif ($c eq "'") {
- return (("Key/value must consist of single string", $string_start, $i)) if defined $string_end;
+ return { error => "Key/value must consist of single string",
+ column_start => $string_start,
+ column_end => $i } if defined $string_end;
$is_string = 1;
$string_start = $i + 1;
} elsif ($c eq "=") {
- return (("Expected > after =, got $lookahead", $i + 1, $i + 1)) unless $lookahead eq ">";
- return (("Unexpected '=>'.", $i, $i)) unless $is_key;
+ return { error => "Expected > after =, got $lookahead",
+ column_start => $i + 1,
+ column_end => $i + 1 } unless $lookahead eq ">";
+ return { error => "Unexpected '=>'.",
+ column_start => $i,
+ column_end => $i} unless $is_key;
$i++;
$key = substr($input, $string_start, $string_end - $string_start);
$string_start = $string_end = undef;
$is_key = 0;
} else {
- return (("Unexpected $c", $i, $i));
+ return { error => "Unexpected $c",
+ column_start => $i,
+ column_end => $i };
}
}
$i++;
}
- return ((undef, $i, $i), ($key, $value));
+ return { column_start => $i,
+ column_end => $i,
+ key => $key,
+ value => $value };
}
sub parse_list {
@@ -65,7 +76,9 @@ sub parse_list {
# Level of nested lists, 1 being the minimum
my $nest = 1;
- return (("Error: expected $c_start", $i, $i)) unless substr($input, $i, 1) eq $c_start;
+ return { error => "Error: expected $c_start",
+ column_start => $i,
+ column_end => $i } unless substr($input, $i, 1) eq $c_start;
$i++;
$item_i = $i;
@@ -83,12 +96,14 @@ sub parse_list {
my $item = substr($input, $item_i, $i - $item_i);
$item =~ s/^\s+|\s+$//g;
if ($is_hash) {
- my (($error, $from, $to), ($key, $value)) = parse_mapping($item);
- $from += $item_i;
- $to += $item_i;
- return (($error, $from, $to)) if $error;
- return (("Error: duplicate key \"$key\"", $item_i, $to)) if grep {$_ eq $key} (keys %h_res);
- $h_res{$key} = $value;
+ my $mapping = parse_mapping($item);
+ $mapping{column_start} += $item_i;
+ $mapping{column_end} += $item_i;
+ return $mapping if $mapping->{error};
+ return { error => "Error: duplicate key \"$mapping->{key}\"",
+ column_start => $item_i,
+ column_end => $mapping->{column_end} } if grep {$_ eq $mapping->{key}} (keys %h_res);
+ $h_res{$mapping->{key}} = $mapping->{value};
} else {
push @a_res, $item;
}
@@ -97,13 +112,17 @@ sub parse_list {
$i++;
}
- return (("Error: expected $c_end, got end of line", $i, $i)) unless $nest == 0;
+ return { error => "Error: expected $c_end, got end of line",
+ column_start => $i,
+ column_end => $i } unless $nest == 0;
if ($i != length($input)) {
- return (("Error: unexpected item in the bagging area (after '$c_end')", $i, $i));
+ return { error => "Error: unexpected item in the bagging area (after '$c_end')",
+ column_start => $i,
+ column_end => $i };
}
- return ((undef, undef, undef), %h_res) if $is_hash;
- return ((undef, undef, undef), @a_res);
+ return { hash => \%h_res } if $is_hash;
+ return { array => \@a_res };
}
1;
diff --git a/Plugin/Map.pm b/Plugin/Map.pm
index 3401f0f..c8708e2 100644
--- a/Plugin/Map.pm
+++ b/Plugin/Map.pm
@@ -24,10 +24,10 @@ sub map {
return "Syntax: map command [item1, item2, ...]" unless $command and $subjects_raw;
- my ($e, $from, $to, @subjects) = ListParser::parse_list($subjects_raw);
- return $e if $e;
+ my $parsed = ListParser::parse_list($subjects_raw);
+ return $parsed->{error} if $parsed->{error};
- my @results = map { $run_command->("$command $_", $who, $where, $ided) } @subjects;
+ my @results = map { $run_command->("$command $_", $who, $where, $ided) } @{$parsed->{array}};
return "[" . (join ", ", @results). "]";
}
1;