Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved regex on eventMacro plugin (Runner.pm) #1205

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 49 additions & 27 deletions plugins/eventMacro/eventMacro/Runner.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1455,25 +1455,47 @@ sub statement {
}

sub particle {
# I need to test this main code alot becoz it will be disastrous if something goes wrong
# in the if statement block below
#this sub is the first part to check statements on macros
#first it check if there is parentheses, if it has , then calls:
#sub txtposition (to split correctly statements inside parentheses
#sub bracket (to check if there is a macroKeyword
#sub multi to check if there is '&&' or '||' on statement

my ($self, $text) = @_;
my @brkt;

my (@brkt, $regex, $regexHolder, $lengthOfRegex);

if ($text =~ /(\/[^\/]+\/\w?)/) {
#here we remove the regex from the \$text , because if regex has '(' or ')'
#it will confuse sub txtPosition, so it is better to remove and set a temporary holder
$regex = $1;
$lengthOfRegex = length($regex);
for (my $i; $i < $lengthOfRegex; $i++) {
$regexHolder .= "x";
}
$text =~ s/\/[^\/]+\/\w?/$regexHolder/;
}

if ($text =~ /\(/) {
#if the statement has parentheses, it means that we have to split the statements and threat every one seperatedly
@brkt = $self->txtPosition($text);
$brkt[0] = $self->multi($brkt[0]) if !bracket($brkt[0]) && $brkt[0] =~ /[\(\)]/ eq "";
if (!bracket($brkt[0]) && $brkt[0] =~ /[\(\)]/ eq "") {
$brkt[0] =~ s/x{$lengthOfRegex}/$regex/;
$brkt[0] = $self->multi($brkt[0]);
}
$text =~ s/x{$lengthOfRegex}/$regex/;
$text = extracted($text, @brkt);
}
}

unless ($text =~ /\(/) {return $text}
unless ($text =~ /\(/) {
$text =~ s/x{$lengthOfRegex}/$regex/;
return $text;
}
$text = $self->particle($text);
}

sub multi {
my ($self, $text) = @_;
my ($i, $n, $ok, $ok2) = (0, 0, 1, 0);
my ($currentMulti, $lastMulti, $ok) = (0, 0, 1);
my %save;

while ($text =~ /(\|{2}|\&{2})/g) {
Expand All @@ -1482,20 +1504,19 @@ sub multi {
# cant use the simbol '&&' or '||' together. Infact, it must be saperated
# by using round brackets '(' and ')' in the 1st place

$save{$i} = $1;
if ($i > 0) {
$n = $i - 1;
if ($save{$i} ne $save{$n}) {
$save{$currentMulti} = $1; #$currentMulti saves the && or ||
if ($currentMulti > 0) {
$lastMulti = $currentMulti - 1;
if ($save{$currentMulti} ne $save{$lastMulti}) {
my $s = $text;
$ok = 0;
#$s =~ s/($save{$i})/\($1\) <-- HERE/g; # Later maybe? ;p
$self->error("Wrong Conditions: ($save{$n} vs $save{$i})");
$self->error("Wrong Conditions: ($save{$lastMulti} vs $save{$currentMulti})");
}
}
$i++
$currentMulti++
}

if ($save{$n} eq "||" && $ok && $i > 0) {
if ($save{$lastMulti} eq "||" && $ok && $currentMulti > 0) {
my @split = split(/\s*\|{2}\s*/, $text);
foreach my $e (@split) {
next if $e eq "0";
Expand All @@ -1504,7 +1525,7 @@ sub multi {
}
return 0
}
elsif ($save{$n} eq "&&" && $ok && $i > 0) {
elsif ($save{$lastMulti} eq "&&" && $ok && $currentMulti > 0) {
my @split = split(/\s*\&{2}\s*/, $text);
foreach my $e (@split) {
next if $e eq "1";
Expand All @@ -1514,7 +1535,7 @@ sub multi {
}
return 1
}
elsif ($i == 0) {
elsif ($currentMulti == 0) {
return $text if $text =~ /^[0-1]$/;
return $self->statement($text)
}
Expand All @@ -1529,25 +1550,25 @@ sub txtPosition {
my ($self, $text) = @_;
my ($start, $i) = (0, 0);
my (@save, @new, $first, $last);
my @w = split(//, $text);
my @characters = split(//, $text);

foreach my $e (@w) {
if ($e eq ")" && $start) {
foreach my $char (@characters) {
if ($char eq ")" && $start) {
$last = $i; last
}
elsif ($e eq "(") {
elsif ($char eq "(") {
if ($start) {undef @save; undef $first}
$start = 1; $first = $i;
}
else {if ($start) {push @save, $e}}
else {if ($start) {push @save, $char}}
$i++
}

$self->error("You probably missed 1 or more closing round-\nbracket ')' in the statement") if !defined $last;

$new[0] = join('', @save);
$new[1] = $first;
$new[2] = $last;
$new[0] = join('', @save); #stateement without parentheses
$new[1] = $first; #exact place where opening parentheses was (very important)
$new[2] = $last; #exact place where closeing parentheses was (very important)
return @new
}

Expand Down Expand Up @@ -1588,7 +1609,8 @@ sub refined_macroKeywords {

sub bracket {
# Scans $text for @special keywords

# ir returns 1 if it has, and 0 if don't have a &special keyword

my ($text, $dbg) = @_;
my @brkt; my $i = 0;

Expand Down
22 changes: 10 additions & 12 deletions plugins/eventMacro/eventMacro/Utilities.pm
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,8 @@ sub cmpr {
foreach my $member (split(/\s*,\s*/, $second)) {
return 1 if ($first eq lc($member));
}

} elsif ($cond eq "=~" && $second =~ /^\/.*?\/\w?\s*$/) {
return match($first, $second, 1);
} elsif ($cond eq "=~" && ($second =~ /^\/[^\/]+\/\w?\s*$/ || $second =~ /^"[^"]+"\s*$/)) {
return match($first, $second);
}

return 0;
Expand All @@ -98,7 +97,7 @@ sub q4rx {
}

sub q4rx2 {
# We let alone the original q4rx sub routine...
# We let alone the original q4rx sub routine...
# instead, we use this for our new @nick ;p
my $s = $_[0];
$s =~ s/([\/*+(){}\[\]\\\$\^?"'\. ])/\\$1/g;
Expand All @@ -118,17 +117,16 @@ sub match {
return $text eq $1
}

if ($kw =~ /^\/(.*?)\/(\w?)$/) {
if ($text =~ /$1/ || ($2 eq 'i' && $text =~ /$1/i)) {
if (!defined $cmpr) {
no strict;
foreach my $idx (1..$#-) {$eventMacro->set_scalar_var(".lastMatch$idx",${$idx})}
use strict;
}
if ($kw =~ /^\/([^\/]+)\/(\w?)$/) {
my ($match, $modifier) = ($1, $2);

if ($text =~ /$match/ || ($modifier eq 'i' && $text =~ /$match/i)) {
no strict;
foreach my $idx (1..$#-) {$eventMacro->set_scalar_var(".lastMatch$idx",${$idx})}
use strict;
return 1
}
}

return 0
}

Expand Down