mirror of
https://github.com/pragma-/pbot.git
synced 2025-04-03 14:28:04 +02:00
Interpreter: extract_bracketed: fix indentation, minor tweaks
This commit is contained in:
parent
d9c2c55efc
commit
fcb4404012
@ -346,8 +346,6 @@ sub extract_bracketed {
|
|||||||
@prefix_group = split //, $1;
|
@prefix_group = split //, $1;
|
||||||
}
|
}
|
||||||
|
|
||||||
#push @prefix_group, ' ' if $allow_whitespace;
|
|
||||||
|
|
||||||
my @prefixes = split //, $optional_prefix;
|
my @prefixes = split //, $optional_prefix;
|
||||||
my @opens = split //, $open_bracket;
|
my @opens = split //, $open_bracket;
|
||||||
my @closes = split //, $close_bracket;
|
my @closes = split //, $close_bracket;
|
||||||
@ -370,139 +368,139 @@ sub extract_bracketed {
|
|||||||
my $bracket_pos;
|
my $bracket_pos;
|
||||||
my $bracket_level = 0;
|
my $bracket_level = 0;
|
||||||
my $ignore_quote = 0;
|
my $ignore_quote = 0;
|
||||||
my $prefix_group_match = @prefix_group ? 0 : 1;
|
my $prefix_group_match = @prefix_group ? 0 : 1;
|
||||||
my $prefix_match = @prefixes ? 0 : 1;
|
my $prefix_match = @prefixes ? 0 : 1;
|
||||||
my $match = 0;
|
my $match = 0;
|
||||||
|
|
||||||
my @chars = split //, $string;
|
my @chars = split //, $string;
|
||||||
|
|
||||||
my $state = 'prefixgroup';
|
my $state = 'prefixgroup';
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
$last_ch = $ch;
|
$last_ch = $ch;
|
||||||
|
|
||||||
if ($i >= @chars) {
|
|
||||||
if (defined $quote) {
|
|
||||||
# reached end, but unbalanced quote... reset to beginning of quote and ignore it
|
|
||||||
$i = $quote_pos;
|
|
||||||
$ignore_quote = 1;
|
|
||||||
$quote = undef;
|
|
||||||
$last_ch = ' ';
|
|
||||||
$token = '';
|
|
||||||
} elsif ($extracting) {
|
|
||||||
# reached end, but unbalanced brackets... reset to beginning and ignore them
|
|
||||||
$i = $bracket_pos;
|
|
||||||
$bracket_level = 0;
|
|
||||||
$state = 'prefixgroup';
|
|
||||||
$extracting = 0;
|
|
||||||
$last_ch = ' ';
|
|
||||||
$token = '';
|
|
||||||
$result = '';
|
|
||||||
} else {
|
|
||||||
# add final token and exit
|
|
||||||
$rest .= $token if $extracted;
|
|
||||||
last;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$ch = $chars[$i++];
|
|
||||||
|
|
||||||
if ($escaped) {
|
|
||||||
$token .= "\\$ch" if $extracting or $extracted;
|
|
||||||
$escaped = 0;
|
|
||||||
next;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($ch eq '\\') {
|
|
||||||
$escaped = 1;
|
|
||||||
next;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if ($i >= @chars) {
|
||||||
if (defined $quote) {
|
if (defined $quote) {
|
||||||
if ($ch eq $quote) {
|
# reached end, but unbalanced quote... reset to beginning of quote and ignore it
|
||||||
# closing quote
|
$i = $quote_pos;
|
||||||
$token .= $ch if $extracting or $extracted;
|
$ignore_quote = 1;
|
||||||
$result .= $token if $extracting;
|
$quote = undef;
|
||||||
$rest .= $token if $extracted;
|
$last_ch = ' ';
|
||||||
$quote = undef;
|
$token = '';
|
||||||
$token = '';
|
} elsif ($extracting) {
|
||||||
} else {
|
# reached end, but unbalanced brackets... reset to beginning and ignore them
|
||||||
# still within quoted argument
|
$i = $bracket_pos;
|
||||||
$token .= $ch if $extracting or $extracted;
|
$bracket_level = 0;
|
||||||
}
|
$state = 'prefixgroup';
|
||||||
next;
|
$extracting = 0;
|
||||||
|
$last_ch = ' ';
|
||||||
|
$token = '';
|
||||||
|
$result = '';
|
||||||
|
} else {
|
||||||
|
# add final token and exit
|
||||||
|
$rest .= $token if $extracted;
|
||||||
|
last;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if ($last_ch eq ' ' and not defined $quote and ($ch eq "'" or $ch eq '"')) {
|
$ch = $chars[$i++];
|
||||||
if ($ignore_quote) {
|
|
||||||
# treat unbalanced quote as part of this argument
|
if ($escaped) {
|
||||||
$token .= $ch if $extracting or $extracted;
|
$token .= "\\$ch" if $extracting or $extracted;
|
||||||
$ignore_quote = 0;
|
$escaped = 0;
|
||||||
} else {
|
next;
|
||||||
# begin potential quoted argument
|
}
|
||||||
$quote_pos = $i - 1;
|
|
||||||
$quote = $ch;
|
if ($ch eq '\\') {
|
||||||
$token .= $ch if $extracting or $extracted;
|
$escaped = 1;
|
||||||
}
|
next;
|
||||||
next;
|
}
|
||||||
|
|
||||||
|
if (defined $quote) {
|
||||||
|
if ($ch eq $quote) {
|
||||||
|
# closing quote
|
||||||
|
$token .= $ch if $extracting or $extracted;
|
||||||
|
$result .= $token if $extracting;
|
||||||
|
$rest .= $token if $extracted;
|
||||||
|
$quote = undef;
|
||||||
|
$token = '';
|
||||||
|
} else {
|
||||||
|
# still within quoted argument
|
||||||
|
$token .= $ch if $extracting or $extracted;
|
||||||
}
|
}
|
||||||
|
next;
|
||||||
|
}
|
||||||
|
|
||||||
if (not $extracted) {
|
if ($last_ch eq ' ' and not defined $quote and ($ch eq "'" or $ch eq '"')) {
|
||||||
if ($state eq 'prefixgroup' and @prefix_group and not $extracting) {
|
if ($ignore_quote) {
|
||||||
foreach my $prefix_ch (@prefix_group) {
|
# treat unbalanced quote as part of this argument
|
||||||
if ($ch eq $prefix_ch) {
|
$token .= $ch if $extracting or $extracted;
|
||||||
$prefix_group_match = 1;
|
$ignore_quote = 0;
|
||||||
$state = 'prefixes';
|
} else {
|
||||||
last;
|
# begin potential quoted argument
|
||||||
} else {
|
$quote_pos = $i - 1;
|
||||||
$prefix_group_match = 0;
|
$quote = $ch;
|
||||||
}
|
$token .= $ch if $extracting or $extracted;
|
||||||
}
|
}
|
||||||
next if $prefix_group_match;
|
next;
|
||||||
} elsif ($state eq 'prefixgroup' and not @prefix_group) {
|
}
|
||||||
$state = 'prefixes';
|
|
||||||
$prefix_index = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($state eq 'prefixes') {
|
if (not $extracted) {
|
||||||
if (@prefixes and $ch eq $prefixes[$prefix_index]) {
|
if ($state eq 'prefixgroup' and @prefix_group and not $extracting) {
|
||||||
$token .= $ch if $extracting;
|
foreach my $prefix_ch (@prefix_group) {
|
||||||
$prefix_match = 1;
|
if ($ch eq $prefix_ch) {
|
||||||
$prefix_index++;
|
$prefix_group_match = 1;
|
||||||
$state = 'openbracket';
|
$state = 'prefixes';
|
||||||
next;
|
last;
|
||||||
} elsif ($state eq 'prefixes' and not @prefixes) {
|
|
||||||
$state = 'openbracket';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($state eq 'openbracket' and $prefix_group_match and $prefix_match) {
|
|
||||||
$prefix_index = 0;
|
|
||||||
if ($ch eq $opens[$open_index]) {
|
|
||||||
$match = 1;
|
|
||||||
$open_index++;
|
|
||||||
} else {
|
} else {
|
||||||
if ($allow_whitespace and $ch eq ' ') {
|
$prefix_group_match = 0;
|
||||||
next;
|
|
||||||
} else {
|
|
||||||
$state = 'prefixgroup';
|
|
||||||
next;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
next if $prefix_group_match;
|
||||||
|
} elsif ($state eq 'prefixgroup' and not @prefix_group) {
|
||||||
|
$state = 'prefixes';
|
||||||
|
$prefix_index = 0;
|
||||||
|
}
|
||||||
|
|
||||||
if ($match) {
|
if ($state eq 'prefixes') {
|
||||||
$state = 'prefixgroup';
|
if (@prefixes and $ch eq $prefixes[$prefix_index]) {
|
||||||
$prefix_group_match = 0 unless not @prefix_group;
|
$token .= $ch if $extracting;
|
||||||
$prefix_match = 0 unless not @prefixes;
|
$prefix_match = 1;
|
||||||
$match = 0;
|
$prefix_index++;
|
||||||
$bracket_pos = $i if not $extracting;
|
$state = 'openbracket';
|
||||||
if ($open_index == @opens) {
|
next;
|
||||||
$extracting = 1;
|
} elsif ($state eq 'prefixes' and not @prefixes) {
|
||||||
$token .= $ch if $bracket_level > 0;
|
$state = 'openbracket';
|
||||||
$bracket_level++;
|
}
|
||||||
$open_index = 0;
|
}
|
||||||
|
|
||||||
|
if ($extracting or ($state eq 'openbracket' and $prefix_group_match and $prefix_match)) {
|
||||||
|
$prefix_index = 0;
|
||||||
|
if ($ch eq $opens[$open_index]) {
|
||||||
|
$match = 1;
|
||||||
|
$open_index++;
|
||||||
|
} else {
|
||||||
|
if ($allow_whitespace and $ch eq ' ' and not $extracting) {
|
||||||
|
next;
|
||||||
|
} elsif (not $extracting) {
|
||||||
|
$state = 'prefixgroup';
|
||||||
|
next;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($match) {
|
||||||
|
$state = 'prefixgroup';
|
||||||
|
$prefix_group_match = 0 unless not @prefix_group;
|
||||||
|
$prefix_match = 0 unless not @prefixes;
|
||||||
|
$match = 0;
|
||||||
|
$bracket_pos = $i if not $extracting;
|
||||||
|
if ($open_index == @opens) {
|
||||||
|
$extracting = 1;
|
||||||
|
$token .= $ch if $bracket_level > 0;
|
||||||
|
$bracket_level++;
|
||||||
|
$open_index = 0;
|
||||||
|
}
|
||||||
next;
|
next;
|
||||||
} else {
|
} else {
|
||||||
$open_index = 0;
|
$open_index = 0;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user