Changeset 22836 for src

Show
Ignore:
Timestamp:
10/31/08 07:55:25 (2 months ago)
Author:
lwall
Message:

[Cursor] {_to} is gone, {_from} now set only on bound cursors, unbound cursors

now only propagate {_pos} (will help optimization of simple traversals)

Location:
src/perl6
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • src/perl6/Cursor.pmc

    r22827 r22836  
    169169our %lexers;       # per language, the cache of lexers, keyed by rule name 
    170170 
    171 sub from { $_[0]->{_from} } 
    172 sub to { $_[0]->{_to} } 
    173 sub chars { $_[0]->{_to} - $_[0]->{_from} } 
    174 sub text { substr($::ORIG, $_[0]->{_from}, $_[0]->{_to} - $_[0]->{_from}) } 
     171sub from { $_[0]->{_from} // $_[0]->{_pos} } 
     172sub to { $_[0]->{_pos} } 
    175173sub pos { $_[0]->{_pos} } 
     174sub chars { $_[0]->{_pos} - ($_[0]->{_from} // $_[0]->{_pos}) } 
     175sub text { exists $_[0]->{_from} ? substr($::ORIG, $_[0]->{_from}, $_[0]->{_pos} - $_[0]->{_from}) : '' } 
    176176sub peek { $_[0]->{_peek} } 
    177177sub orig { \$::ORIG } 
     
    711711    my $lang = @_ && $_[0] ? shift() : ref $self; 
    712712    $self->deb("cursor_fresh lang $lang") if $DEBUG & DEBUG::cursors; 
    713     $r{_to} = $r{_from} = $r{_pos} = $self->{_pos}; 
     713   # $r{_from} = 
     714    $r{_pos} = $self->{_pos}; 
    714715    $r{_fate} = $self->{_fate}; 
    715716    $r{_herelang} = $self->{_herelang} if $self->{_herelang}; 
     
    798799    delete $copy{_fate}; 
    799800    my $text = STD::Dump(\%copy); 
    800     $text =~ s/^\s*_(?:pos|orig):.*\n//mg; 
    801801    $text; 
    802802} 
     
    819819        } 
    820820    } 
    821     $r{_pos} = $r{_to} = $submatch->{_to}; 
     821    $submatch->{_from} = $r{_from} = $r{_pos}; 
     822    $r{_pos} = $submatch->{_pos}; 
    822823    delete $r{_fate}; 
    823824    CORE::bless \%r, ref $self;         # return new match cursor for parent 
     
    871872    $self->deb("cursor_all from $fpos to $tpos") if $DEBUG & DEBUG::cursors; 
    872873    my %r = %$self; 
    873     $r{_from} = $fpos; 
    874     $r{_to} = $tpos; 
     874#    $r{_from} = $fpos; 
    875875    $r{_pos} = $tpos; 
    876876 
     
    887887        $self->deb("cursor to $tpos --------->$GREEN$peek$CLEAR"); 
    888888    } 
    889     $self->{_to} = $tpos; 
    890889    $self->{_pos} = $tpos; 
    891890 
     
    903902    } 
    904903    my %r = %$self; 
    905     $r{_from} = $self->{_pos} // 0; 
    906     $r{_to} = $tpos; 
     904#    $r{_from} = $self->{_pos} // 0; 
    907905    $r{_pos} = $tpos; 
    908906 
     
    921919    my %r = %$self; 
    922920    $r{_pos} = $fpos; 
    923     $r{_from} = $fpos; 
    924     $r{_to} = $self->{_from}; 
     921#    $r{_from} = $self->{_from}; 
    925922 
    926923    CORE::bless \%r, ref $self; 
     
    10121009 
    10131010    my \$C = \$self; 
     1011    my \$S = \$self->{_pos}; 
    10141012    \$C->{'sym'} = \$sym; 
    10151013 
    1016     \$self->_MATCHIFY( '$mangle', Cursor::lazymap sub { STD::$coercion->coerce(\$_[0]) }, 
     1014    \$self->_MATCHIFY(\$S, '$mangle', Cursor::lazymap sub { STD::$coercion->coerce(\$_[0]) }, 
    10171015        \$C->_SYM(\$sym, 0) 
    10181016    ); 
     
    10781076    warn "Returning non-Cursor: $self\n" unless exists $self->{_pos}; 
    10791077    my ($package, $file, $line, $subname, $hasargs) = caller(1); 
    1080     $self->deb($subname, " returning @{[$self->{_from}]}..@{[$self->{_to}]}"); 
     1078    $self->deb($subname, " returning @{[$self->{_pos}]}"); 
    10811079    $self; 
    10821080} 
    10831081 
    10841082sub _MATCHIFY { my $self = shift; 
     1083    my $S = shift; 
    10851084    my $name = shift; 
    10861085    return () unless @_; 
    1087     my @result = lazymap( sub { my $x = shift; $x->{_from} = $self->{_from}; $x->_REDUCE($name)->retm() }, @_); 
     1086    my @result = lazymap( sub { my $x = shift; $x->_REDUCE($S, $name)->retm() }, @_); 
    10881087    if (wantarray) { 
    10891088        @result; 
     
    10951094 
    10961095sub _MATCHIFYr { my $self = shift; 
     1096    my $S = shift; 
    10971097    my $name = shift; 
    10981098    return () unless @_; 
    10991099    my $var = shift; 
    1100     $var->{_from} = $self->{_from}; 
    1101     $var->_REDUCE($name)->retm(); 
     1100#    $var->{_from} = $self->{_from}; 
     1101    $var->_REDUCE($S, $name)->retm(); 
    11021102} 
    11031103 
     
    11461146 
    11471147    local $CTX = $self->callm if $DEBUG & DEBUG::trace_call; 
    1148     my $to = $self; 
    1149     my $prev_to = $to->{_to} // 0; 
     1148    my $pos = $self; 
     1149    my $prev_pos = $pos->{_pos} // 0; 
    11501150    my @all; 
    11511151    my $eos = length(${\$::ORIG}); 
    11521152    for (;;) { 
    1153       last if $to->{_pos} == $eos; 
    1154         my @matches = $block->($to);  # XXX shouldn't read whole list 
     1153      last if $pos->{_pos} == $eos; 
     1154        my @matches = $block->($pos);  # XXX shouldn't read whole list 
    11551155#            say @matches.perl; 
    11561156      last unless @matches; 
    11571157        my $first = $matches[0];  # no backtracking into block on ratchet 
    1158         last if $first->{_to} == $prev_to; 
    1159         $prev_to = $first->{_to}; 
     1158        last if $first->{_pos} == $prev_pos; 
     1159        $prev_pos = $first->{_pos}; 
    11601160        push @all, $first; 
    1161         $to = $first; 
    1162     } 
    1163     $self->cursor_tweak($to->{_pos})->retm(); 
     1161        $pos = $first; 
     1162    } 
     1163    $self->cursor_tweak($pos->{_pos})->retm(); 
    11641164} 
    11651165 
     
    11771177            lazymap( 
    11781178                sub { 
    1179                     $self->cursor($_[0]->{_to})->retm() 
     1179                    $self->cursor($_[0]->{_pos})->retm() 
    11801180                }, $x, LazyMap->new(sub { $x->_PLUSf($_[0]) }, $block) 
    11811181            ); 
     
    12261226        for my $x ($block->($self)) { 
    12271227            for my $s ($sep->($x)) { 
    1228                 push @result, lazymap(sub { $self->cursor($_[0]->{_to}) }, $x, $s->_REPSEPf($sep,$block)); 
     1228                push @result, lazymap(sub { $self->cursor($_[0]->{_pos}) }, $x, $s->_REPSEPf($sep,$block)); 
    12291229            } 
    12301230        } 
     
    13541354    local $CTX = $self->callm if $DEBUG & DEBUG::trace_call; 
    13551355    my $end = $self->cursor($self->{_pos}); 
    1356     my @all = $block->($end);          # Make sure $_->{_from} == $_->{_to} 
     1356    my @all = $block->($end);          # Make sure $_->{_from} == $_->{_pos} 
    13571357    if (@all and $all[0]) { 
    13581358        return $all[0]->cursor_all(($self->{_pos}) x 2)->retm(); 
     
    13761376 
    13771377    my $C = $self; 
    1378     my $startpos = $C->pos; 
    1379     $::MEMOS[$startpos]{ws} = undef;    # exists means we know, undef means no ws  before here 
    1380  
    1381     $self->_MATCHIFY( 'ws', 
     1378    my $S = $C->pos; 
     1379    $::MEMOS[$S]{ws} = undef;   # exists means we know, undef means no ws  before here 
     1380 
     1381    $self->_MATCHIFY($S, 'ws', 
    13821382        $C->_BRACKET( sub { my $C=shift; 
    13831383            do { my @gather; 
     
    14001400                    push @gather, (map { my $C=$_; 
    14011401                        (map { my $C=$_; 
    1402                             scalar(do { $::MEMOS[$C->{_pos}]{ws} = $startpos unless $C->{_pos} == $startpos }, $C) 
     1402                            scalar(do { $::MEMOS[$C->{_pos}]{ws} = $S unless $C->{_pos} == $S }, $C) 
    14031403                        } $C->_STARr(sub { my $C=shift; 
    14041404                            $C->_SPACE() 
     
    15311531    local $CTX = $self->callm if $DEBUG & DEBUG::trace_call; 
    15321532    my $len = length($s); 
    1533     my $from = $self->{_from} - $len; 
     1533    my $from = $self->{_pos} - $len; 
    15341534    if ($from >= 0 and substr($::ORIG, $from, $len) eq $s) { 
    15351535        my $r = $self->cursor_rev($from); 
     
    15641564    for my $s (@array) { 
    15651565        my $len = length($s); 
    1566         my $from = $self->{_from} = $len; 
     1566        my $from = $self->{_pos} = $len; 
    15671567        if (substr($::ORIG, $from, $len) eq $s) { 
    15681568            $self->deb("ARRAY_rev elem $s matched @{[substr($::ORIG,$from,$len)]} at $from $len") if $DEBUG & DEBUG::matchers; 
     
    15901590sub _DIGIT_rev { my $self = shift; 
    15911591    local $CTX = $self->callm if $DEBUG & DEBUG::trace_call; 
    1592     my $from = $self->{_from} - 1; 
     1592    my $from = $self->{_pos} - 1; 
    15931593    if ($from < 0) { 
    15941594#        say "DIGIT_rev didn't match $char at $from"; 
     
    16221622sub _ALNUM_rev { my $self = shift; 
    16231623    local $CTX = $self->callm if $DEBUG & DEBUG::trace_call; 
    1624     my $from = $self->{_from} - 1; 
     1624    my $from = $self->{_pos} - 1; 
    16251625    if ($from < 0) { 
    16261626#        say "ALNUM_rev didn't match $char at $from"; 
     
    16541654sub alpha_rev { my $self = shift; 
    16551655    local $CTX = $self->callm if $DEBUG & DEBUG::trace_call; 
    1656     my $from = $self->{_from} - 1; 
     1656    my $from = $self->{_pos} - 1; 
    16571657    if ($from < 0) { 
    16581658        return (); 
     
    16841684sub _SPACE_rev { my $self = shift; 
    16851685    local $CTX = $self->callm if $DEBUG & DEBUG::trace_call; 
    1686     my $from = $self->{_from} - 1; 
     1686    my $from = $self->{_pos} - 1; 
    16871687    if ($from < 0) { 
    16881688#        say "SPACE_rev didn't match $char at $from"; 
     
    17161716sub _HSPACE_rev { my $self = shift; 
    17171717    local $CTX = $self->callm if $DEBUG & DEBUG::trace_call; 
    1718     my $from = $self->{_from} - 1; 
     1718    my $from = $self->{_pos} - 1; 
    17191719    if ($from < 0) { 
    17201720#        say "HSPACE_rev didn't match $char at $from"; 
     
    17481748sub _VSPACE_rev { my $self = shift; 
    17491749    local $CTX = $self->callm if $DEBUG & DEBUG::trace_call; 
    1750     my $from = $self->{_from} - 1; 
     1750    my $from = $self->{_pos} - 1; 
    17511751    if ($from < 0) { 
    17521752#        say "VSPACE_rev didn't match $char at $from"; 
     
    17841784 
    17851785    local $CTX = $self->callm if $DEBUG & DEBUG::trace_call; 
    1786     my $from = $self->{_from} - 1; 
     1786    my $from = $self->{_pos} - 1; 
    17871787    if ($from < 0) { 
    17881788#        say "CCLASS didn't match $char at $from"; 
     
    18141814sub _ANY_rev { my $self = shift; 
    18151815    local $CTX = $self->callm if $DEBUG & DEBUG::trace_call; 
    1816     my $from = $self->{_from} - 1; 
     1816    my $from = $self->{_pos} - 1; 
    18171817    if ($from < 0) { 
    18181818        return (); 
     
    19091909 
    19101910sub _REDUCE { my $self = shift; 
     1911    my $S = shift; 
    19111912    my $tag = shift; 
    19121913 
    19131914    $self->{_reduced} = $tag; 
     1915    $self->{_from} = $S; 
    19141916    if ($::ACTIONS) { 
    19151917        eval { $::ACTIONS->$tag($self) }; 
    19161918        warn $@ if $@ and not $@ =~ /locate/; 
    19171919    } 
    1918     $self->deb("REDUCE $tag from " . $self->{_from} . " to " . $self->{_pos}) if $DEBUG & DEBUG::matchers; 
     1920    $self->deb("REDUCE $tag from " . $S . " to " . $self->{_pos}) if $DEBUG & DEBUG::matchers; 
    19191921    $self; 
    19201922} 
     
    19241926    local $CTX = $self->callm if $DEBUG & DEBUG::trace_call; 
    19251927    my $P = $self->{_pos}; 
    1926     my $F = $self->{_from}; 
    1927     $self->{FIRST} = substr($::ORIG, $F, $P - $F); 
    19281928    $self->deb("Commit branch to $P") if $DEBUG & DEBUG::matchers; 
    19291929    $self, LazyMap->new(sub { $self->deb("ABORTBRANCH") if $DEBUG & DEBUG::trace_call; die "ABORTBRANCH" }, $self); 
  • src/perl6/Makefile

    r22694 r22836  
    77STD.pmc: STD.pm gimme5 
    88        ./gimme5 $< >STD.pm5 
    9         perl -p -e 'next if /^---/../\A\w+\Z/;' -e 's/\A\s+//;' STD.pm5 >$@ 
     9        perl -p -e 'next if /^---/../\A\w+\Z/;' -e 's/\A[ \t]+//;' STD.pm5 >$@ 
    1010        rm -rf lex 
    1111 
  • src/perl6/STD.pm

    r22832 r22836  
    34063406 
    34073407    my $here = self; 
    3408     self.deb("In EXPR, at ", $here.pos) if $*DEBUG +& DEBUG::EXPR; 
     3408    my $S = $here.pos; 
     3409    self.deb("In EXPR, at $S") if $*DEBUG +& DEBUG::EXPR; 
    34093410 
    34103411    my &reduce := -> { 
     
    34253426                push @chain, pop(@termstack).cleanup; 
    34263427                @chain = reverse @chain if @chain > 1; 
     3428                my $startpos = @chain[0].pos; 
    34273429                my $nop = $op.cursor_fresh(); 
    34283430                $nop<chain> = [@chain]; 
    34293431                $nop<_arity> = 'CHAIN'; 
    3430                 push @termstack, $nop._REDUCE('EXPR'); 
     3432                push @termstack, $nop._REDUCE($startpos, 'EXPR'); 
    34313433            } 
    34323434            when 'list' { 
     
    34533455                } 
    34543456                @list = reverse @list if @list > 1; 
     3457                my $startpos = @list[0].pos; 
    34553458                @delims = reverse @delims if @delims > 1; 
    34563459                my $nop = $op.cursor_fresh(); 
     
    34603463                $nop<delims> = [@delims]; 
    34613464                $nop<_arity> = 'LIST'; 
    3462                 push @termstack, $nop._REDUCE('EXPR'); 
     3465                push @termstack, $nop._REDUCE($startpos, 'EXPR'); 
    34633466            } 
    34643467            when 'unary' { 
     
    34723475                    $op<_from> = $op<arg><_from>; 
    34733476                } 
    3474                 if ($op<arg><_to> > $op<_to>) { 
    3475                     $op<_to> = $op<arg><_to>; 
     3477                if ($op<arg><_pos> > $op<_pos>) { 
     3478                    $op<_pos> = $op<arg><_pos>; 
    34763479                } 
    34773480                $op<_arity> = 'UNARY'; 
    3478                 push @termstack, $op._REDUCE('EXPR'); 
     3481                push @termstack, $op._REDUCE($op<_from>, 'EXPR'); 
    34793482            } 
    34803483            default { 
     
    34833486                self.deb("Termstack size: ", +@termstack) if $*DEBUG +& DEBUG::EXPR; 
    34843487 
    3485                 self.deb($op.dump) if $*DEBUG +& DEBUG::EXPR; 
    34863488                $op<right> = (pop @termstack).cleanup; 
    34873489                $op<left> = (pop @termstack).cleanup; 
    34883490                $op<_from> = $op<left><_from>; 
    3489                 $op<_to> = $op<right><_to>; 
     3491                $op<_pos> = $op<right><_pos>; 
    34903492                $op<_arity> = 'BINARY'; 
    3491                 push @termstack, $op._REDUCE('EXPR'); 
     3493                self.deb($op.dump) if $*DEBUG +& DEBUG::EXPR; 
     3494                push @termstack, $op._REDUCE($op<_from>, 'EXPR'); 
    34923495            } 
    34933496        } 
     
    35993602        +@termstack == 1 or $here.panic("Internal operator parser error, termstack == " ~ (+@termstack)); 
    36003603        @termstack[0]<_from> = self.pos; 
    3601         @termstack[0]<_to> = $here.pos; 
    3602     } 
    3603     self._MATCHIFYr("EXPR", @termstack); 
     3604        @termstack[0]<_pos> = $here.pos; 
     3605    } 
     3606    self._MATCHIFYr($S, "EXPR", @termstack); 
    36043607} 
    36053608 
  • src/perl6/gimme5

    r22824 r22836  
    327327 
    328328    my \$C = \$self; 
    329     \$C->{_to} = \$C->{_from} = \$C->{_pos}; 
     329    my \$S = \$C->{_pos}; 
    330330 
    331331    my \@result = eval { 
     
    356356            push \@gather, \$C->\$try(\@_); 
    357357        } 
    358         \$self->_MATCHIFY("$name", \@gather); 
     358        \$self->_MATCHIFY(\$S, "$name", \@gather); 
    359359    }; 
    360360    if (\$@) { 
     
    597597 
    598598    my $C = $self; 
    599     $C->{_to} = $C->{_from} = $C->{_pos}; 
     599    my $S = $C->{_pos}; 
    600600END 
    601601 
     
    630630                $body .= <<"END"; 
    631631 
    632     \$self->_MATCHIFY$ratchet("$NAME", $coercion 
     632    \$self->_MATCHIFY$ratchet(\$S, "$NAME", $coercion 
    633633<<MEAT>> 
    634634    ); 
     
    12181218        ws(); 
    12191219        $key =~ s/(['\\])/\\$1/g; 
    1220         return bless { name => '_REDUCE', args => "'$key'", min => 0, max => 0}, 
     1220        return bless { name => '_REDUCE', args => "\$S, '$key'", min => 0, max => 0}, 
    12211221                     "RE_method_internal"; 
    12221222    }