MatchPattern

5091 단어 MatchPattern

package Estring::Function::MatchPattern;

use Exporter; our @ISA = qw(Exporter); our @EXPORT_OK = qw(match_pattern);
use 5.016; use YAML qw(Dump);
sub puts { say Dump(@); } sub debug { say Dump(@); exit; }
sub match_pattern { my $args = shift;

my ($nums, $patt, $nums_pos, $from_pos) = @_ ;


$args->{nums_pos} = $args->{nums_pos} || 0; $args->{from_pos} = $args->{from_pos} || -1; my $patt = $args->{patt}; my $state; foreach my $sub_patt (@{ $patt }) {
  $args->{patt} = $sub_patt;
  $state = match_branch_pattern($args);
  # puts $state;
  return $state if ( $state->{flag} == 1 );

} return $state; }
sub match_branch_pattern { my $args = shift;
my $nums = $args->{nums};

my ($nums, $patt, $nums_pos, $from_pos) = @_ ;


my $length_nums = scalar @{ $nums }; my $length_patt = scalar @{ $args->{patt} };

주 순환에서 끊임없이 변화하는 매개 변수


my $nums_pos = $args->{nums_pos}; my $from_pos = $args->{from_pos}; my $patt_pos = 0; my $matched = 0;

기본 반환된 태그 값


my $branch_flag = 0;

마지막 문자도 훑어봐야 돼요.


while ($nums_pos < $length_nums) {
  say $nums_pos;
  # say $patt_pos;
  my $args = {
     nums     => $nums,
     patt     => $args->{patt},
     nums_pos => $nums_pos,
     patt_pos => $patt_pos,
     from_pos => $from_pos,
     matched  => $matched,
  };
  # puts $args;
  my $state = match_meta_patt($args);
  # puts $state;
  my $flag = $state->{flag};
  # say $flag;
  $nums_pos = $state->{nums_pos} + 1;
  $from_pos = $state->{from_pos};
  # matched  
  $matched = $state->{matched};

  #  
  if ($flag == 0) {
     #  , 
     $patt_pos = 0;
     #  
     $matched = 0;
  }
  #  
  elsif ($flag == 1) {
     $matched = 0;
     #   length_patt    
     #  ,  patt_pos  
     $patt_pos++;
     last if ($patt_pos == $length_patt);
  }
  elsif ($flag == 2) {
     $patt_pos++;
     last if ($patt_pos == $length_patt);
  }
  elsif ($flag == 3) {
     $matched = $matched + 1;
  }
  #  
  elsif ($flag == 4) {
     # nums_pos  
     # patt_pos  
     #  
     $matched = $matched + 1;
  }
  #  
  elsif ($flag == 5) {
     #  , 
     $nums_pos = $state->{last_pos};
     $patt_pos++;
     last if ($patt_pos == $length_patt);
  }

} return {
  flag     => $branch_flag,
  from_pos => $from_pos,
  nums_pos => $nums_pos,

}; }

define a piece of regexp is passed or missed, or waitting for input


sub match_meta_patt { my $args = shift;

puts $args;


my $state = new_state($args);

puts $state;


my $nums = $args->{nums}; my $nums_pos = $args->{nums_pos}; my $from_pos = $args->{from_pos}; my $num = $nums->[$nums_pos];

say $num;


puts $state->{class};


my $flag = 0; my $last_pos = 0;
my $lazy_mode = $state->{lazy_mode}; my $min = $state->{min}; my $max = $state->{max}; my $matched = $state->{matched};
my $pass_flag = 0; if ( ref($state->{class}) eq ref([]) ) {
  $args->{patt} = $state->{class};
  my $pass_state = match_pattern($args);
  $pass_flag = $pass_state->{flag};
  $nums_pos  = $pass_state->{nums_pos};
  $from_pos  = $pass_state->{from_pos};

} else {
  # say "reach here";
  if (exists $state->{class}{$num}){
     $pass_flag = 1;
  } else { $pass_flag = 0 }

}

say $pass_flag;


if ($pass_flag == 1) {
  if ($nums_pos - $from_pos > $matched) { $from_pos = $nums_pos }
  $matched++;
  # {1,1}  
  if ($matched < $min) { $flag = 1; }
  elsif ($matched == $min) {
     if ($lazy_mode == 1) { 
        $flag = 2;
        $matched = 0;
     } else {
        $flag = 3;
        #   
        $state->{last_pos} = $nums_pos;
     }
  }
  #   -1  
  elsif ($matched != $max ) { 
     $flag = 4;
     #  
     $state->{last_pos} = $nums_pos;
  }
  elsif ($matched == $max) {
     $flag = 2;
     $matched = 0;
  }

} else {
  if ($matched >= $min) {
     $flag = 5;
  } 
  else { 
     $flag = 0;
  }
  $matched = 0; # reset

}

say $flag;


$state->{flag} = $flag; $state->{matched} = $matched; $state->{nums_pos} = $nums_pos; $state->{from_pos} = $from_pos; $state->{last_pos} = $last_pos;

puts $state;


return $state; }
sub new_state { my $args = shift; my $patt = $args->{patt}; my $patt_pos = $args->{patt_pos};

상태 간에 전달된 그리고 지난번에 일치된 성과.


my $matched = $args->{matched} || 0; return {
  class     => $patt->[$patt_pos][0], 
  min       => $patt->[$patt_pos][1][0],
  max       => $patt->[$patt_pos][1][1],
  lazy_mode => $patt->[$patt_pos][2],
  matched   => $matched,
  nums_pos  => $args->{nums_pos},

}; }
일;

좋은 웹페이지 즐겨찾기