Changeset 21840 for perl5

Show
Ignore:
Timestamp:
08/07/08 23:17:55 (4 months ago)
Author:
fglock
Message:

[PCR] Runtime::Regex with dfa-ish "parallel alternation" committed as a separate file;
- low-level tests pass.
- pending integration with the main runtime.
Runtime::Match: added clone()

Location:
perl5/Pugs-Compiler-Rule
Files:
1 added
2 modified

Legend:

Unmodified
Added
Removed
  • perl5/Pugs-Compiler-Rule/lib/Pugs/Runtime/Match.pm

    r15793 r21840  
    3030    my $obj = bless \$_[1], $_[0]; 
    3131    $_data{ refaddr $obj } = $_[1]; 
     32    return $obj; 
     33} 
     34 
     35sub clone { 
     36    my $data = { %{ $_[0]->data } }; 
     37    my $obj = bless $data, ref $_[0]; 
     38    $_data{ refaddr $obj } = $data; 
    3239    return $obj; 
    3340} 
  • perl5/Pugs-Compiler-Rule/t/01-iterator_engine_step.t

    r21812 r21840  
    44use warnings; 
    55 
    6 use Test::More tests => 12; 
    7 # use Data::Dumper; 
    8 # $Data::Dumper::Indent = 1; 
    9 # $Data::Dumper::Pad = '# '; 
     6use Test::More tests => 28; 
     7use Data::Dumper; 
     8$Data::Dumper::Indent   = 1; 
     9$Data::Dumper::Pad      = '# '; 
     10$Data::Dumper::Sortkeys = 1; 
    1011 
    1112use_ok( 'Pugs::Runtime::Regex' ); 
    1213use Pugs::Runtime::Match; 
    1314 
    14 my ( $rule, $match ); 
    15  
    16 { 
    17   $rule = Pugs::Runtime::Regex::constant( 'a' ); 
     15{ 
     16  my $match; 
     17  my $rule = Pugs::Runtime::Regex::constant( 'a' ); 
     18   
    1819  $rule->( 'a123', undef, {capture=>1, single_step=>1}, $match ); 
    19   #print Dumper( $match ); 
    20   ok ( $match->bool, "a =~ /a/ #1" ); 
    21   is ( $match->tail, '123', "tail is ok" ); 
     20    #print Dumper( $match ); 
     21    ok ( $match->bool, "a =~ /a/ #1" ); 
     22    is ( $match->tail, '123', "tail is ok" ); 
     23    ok ( !defined($match->state), "no more states" ); 
     24     
    2225  $rule->( 'c', undef, {capture=>1}, $match ); 
    23   ok ( ! $match->bool, "c =~ /a/ #2" ); 
    24   #is ( $match->tail, 'c123', "tail is ok" ); 
    25   #print Dumper( $match ); 
     26    ok ( ! $match->bool, "c =~ /a/ #2" ); 
     27    #is ( $match->tail, 'c123', "tail is ok" ); 
     28    #print Dumper( $match ); 
     29     
    2630  $rule->( 'ca', undef, {}, $match); 
    27   ok( !$match->bool, "anchored match" ); 
     31    ok( !$match->bool, "anchored match" ); 
     32 
    2833} 
    2934 
    3035{ 
    3136  # -- continuations in alternation() 
    32   $rule =  
     37  my $match; 
     38  my $rule =  
    3339      Pugs::Runtime::Regex::alternation( [ 
    3440        Pugs::Runtime::Regex::constant( 'x' ),  
     
    3642        Pugs::Runtime::Regex::constant( 'ab' ),  
    3743      ] ); 
     44 
    3845  $rule->( 'ab', undef, {single_step => 1}, $match ); 
    39   #print "state: ", Dumper($match->state), "\n"; 
    40   is ( $match->str, '', "/[a|ab]/ multi-match continuation state #0 - no match" ); 
     46    #print "state: ", Dumper($match->state), "\n"; 
     47    is ( $match->str, '', "/[a|ab]/ multi-match continuation state #0 - no match" ); 
     48    ok ( defined($match->state), "more states" ); 
     49 
    4150  $rule->( 'ab', $match->state, {single_step => 1}, $match ); 
    42   #print "state: ", Dumper($match->state), "\n"; 
    43   is ( $match->str, 'a', "/[a|ab]/ multi-match continuation state #1" ); 
     51    #print "# state: ", Dumper($match->state), "\n"; 
     52    is ( $match->str, 'a', "/[a|ab]/ multi-match continuation state #1" ); 
     53    ok ( defined($match->state), "more states" ); 
     54 
    4455  $rule->( 'ab', $match->state, {single_step => 1}, $match ); 
    45   #print "state: ", Dumper($match->state), "\n"; 
    46   is ( $match->str, 'ab', "/[a|ab]/ multi-match continuation state #2" ); 
    47   #$rule->( 'ab', $match->state, {single_step => 1}, $match ); 
    48   #print "state: ", Dumper($match->state), "\n"; 
    49   #is ( $match->str, '', "/[a|ab]/ multi-match state #2" ); 
    50   #print Dumper( $match ); 
     56    #print "# state: ", Dumper($match->state), "\n"; 
     57    is ( $match->str, 'ab', "/[a|ab]/ multi-match continuation state #2" ); 
     58    ok ( !defined($match->state), "no more states" ); 
     59 
    5160} 
    5261 
    5362{ 
    5463  # -- continuations in concat() 
    55   $rule =  
     64  my $match; 
     65  my $rule =  
    5666    Pugs::Runtime::Regex::concat( [ 
    5767      Pugs::Runtime::Regex::alternation( [ 
     
    6575    ] ); 
    6676  my $str = 'abbb'; 
    67   # expected: () (a,bb) () (ab,bb) 
     77  # expected: (a) (fail) (a,bb) (ab) (fail) (ab,bb) 
     78   
    6879  $rule->( $str, undef, {single_step => 1}, $match ); 
    69   #print "state 1: ", Dumper($match->state), "\n"; 
    70   is ( $match->str, '', "/[a|ab][b|bb]/ continuation state #0" ); 
    71  
    72   $rule->( $str, $match->state, {single_step => 1}, $match ); 
    73   #print "state 2: ", Dumper($match->state), "\n"; 
    74   is ( $match->str, 'abb', "state #1" ); 
    75  
    76 TODO: { 
    77   local $TODO = 'concat single-step not implemented'; 
    78  
    79   $rule->( $str, $match->state, {single_step => 1}, $match ); 
    80   #print "state 3: ", Dumper($match->state), "\n"; 
    81   is ( $match->str, '', "state #2" ); 
    82  
    83   $rule->( $str, $match->state, {single_step => 1}, $match ); 
    84   #print "state 4: ", Dumper($match->state), "\n"; 
    85   is ( $match->str, 'abbb', "state #3" ); 
    86 } 
     80    #print "state 1: ", Dumper($match->state), "\n"; 
     81    is ( $match->str, 'a', "$str ~~ /[a|ab][x|bb]/ continuation state #0" ); 
     82    ok ( defined($match->state), "more states" ); 
     83 
     84  $rule->( $str, $match->state, {single_step => 1}, $match ); 
     85    #print "state 2: ", Dumper($match->state), "\n"; 
     86    is ( $match->str, '', "state #2" ); 
     87    ok ( defined($match->state), "more states" ); 
     88 
     89  $rule->( $str, $match->state, {single_step => 1}, $match ); 
     90    #print "state 3: ", Dumper($match->state), "\n"; 
     91    is ( $match->str, 'abb', "state #3" ); 
     92    ok ( defined($match->state), "more states" ); 
     93 
     94  $rule->( $str, $match->state, {single_step => 1}, $match ); 
     95    #print "state 4: ", Dumper($match->state), "\n"; 
     96    is ( $match->str, 'ab', "state #4" ); 
     97    ok ( defined($match->state), "more states" ); 
     98 
     99  $rule->( $str, $match->state, {single_step => 1}, $match ); 
     100    #print "state 5: ", Dumper($match->state), "\n"; 
     101    is ( $match->str, '', "state #5" ); 
     102    ok ( defined($match->state), "more states" ); 
     103 
     104  $rule->( $str, $match->state, {single_step => 1}, $match ); 
     105    #print "state 6: ", Dumper($match->state), "\n"; 
     106    is ( $match->str, 'abbb', "state #6" ); 
     107    ok ( !defined($match->state), "no more states" ); 
     108 
     109} 
     110 
     111{ 
     112  # -- continuations in parallel_alternation() 
     113  my $match; 
     114  my $rule =  
     115    Pugs::Runtime::Regex::parallel_alternation( [ 
     116      Pugs::Runtime::Regex::concat( [ 
     117        Pugs::Runtime::Regex::constant( 'a' ),  
     118        Pugs::Runtime::Regex::constant( 'bb' ),  
     119      ] ), 
     120      Pugs::Runtime::Regex::concat( [ 
     121        Pugs::Runtime::Regex::constant( 'ab' ),  
     122        Pugs::Runtime::Regex::constant( 'bb' ),  
     123      ] ), 
     124    ] ); 
     125  my $str = 'abbb'; 
     126  # expected: (a|ab) (abb|abbb) -> longest token = abbb 
     127   
     128  $rule->( $str, undef, {single_step => 1}, $match ); 
     129    #print "state 1: ", Dumper($match->state), "\n"; 
     130    is ( $match->str, '', "$str ~~ /a bb | ab bb/ parallel_alternation state #0" ); 
     131    ok ( defined($match->state), "more states" ); 
     132 
     133  $rule->( $str, $match->state, {single_step => 1}, $match ); 
     134    #print "state 2: ", Dumper($match->state), "\n"; 
     135    is ( $match->str, 'abbb', "state #2" ); 
     136    ok ( !defined($match->state), "no more states" ); 
    87137 
    88138}