Changeset 23018 for src

Show
Ignore:
Timestamp:
11/15/08 04:11:54 (8 weeks ago)
Author:
lwall
Message:

[Cursor] more duplicate rule linking
save STD.pm snapshot via storable for booting all other parses faster

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • src/perl6/Cursor.pmc

    r23015 r23018  
    1616our $DEPTH = 0; 
    1717our %LEXERS;       # per language, the cache of lexers, keyed by rule name 
    18  
    19 my %HIST; 
     18our %FATECACHE; # fates we've already turned into linked lists 
     19our %ANON;      # knowledge about what can be borrowed from supergrammar 
     20 
     21#my %HIST; 
    2022 
    2123sub ::init_globals { 
     
    154156        close FILE; 
    155157    } 
    156     $class->new($text)->$rule(); 
     158 
     159    my $result; 
     160    if ($STORABLE) { 
     161        my $store = "lex/STD.pm.store"; 
     162        if (-f $store and -M $file > -M $store) { 
     163            *::LEXERS = retrieve($store); 
     164        } 
     165        $result = $class->new($text)->$rule(); 
     166        if (defined $result and $file eq 'STD.pm') { 
     167            store(\%::LEXERS, $store); 
     168        } 
     169    } 
     170    else { 
     171        $result = $class->new($text)->$rule(); 
     172    } 
     173 
    157174#    for my $key (sort {$HIST{$a} <=> $HIST{$b}} keys(%HIST)) { 
    158175#       warn "$HIST{$key}\t$key\n"; 
    159176#    } 
     177    $result; 
    160178} 
    161179 
     
    256274} 
    257275 
    258 our %FATECACHE; 
    259  
    260276sub _AUTOLEXgen { my $self = shift; 
    261277    my $key = shift; 
     
    263279 
    264280    my $lang = ref $self; 
     281    if ($lang =~ /^ANON/) { 
     282        my $anon = $ANON{$lang}; 
     283        my $super = $anon->{SUP}; 
     284        my $category = $anon->{CAT}; 
     285        print STDERR "AUTOLEXgen $key in $lang from $super without $category\n" if $DEBUG & DEBUG::mixins; 
     286        my $superlexer = $self->cursor_fresh($super)->_AUTOLEXpeek($key,$retree); 
     287        my $same = 1; 
     288        for my $pat (@{$superlexer->{PATS}}) { 
     289            if ($pat =~ / $category /) { 
     290                print STDERR "\tNope: $pat\n" if $DEBUG & DEBUG::mixins; 
     291                $same = 0; 
     292                last; 
     293            } 
     294        } 
     295        # no need to regen a sublexer that will turn out the same 
     296        return $superlexer if $same; 
     297    } 
    265298    $self->deb("AUTOLEXgen $key in $lang") if $DEBUG & DEBUG::autolexer; 
    266299    my $lexer = {}; 
     
    491524                system "cp $dir/termish.store $dir/EXPR.store"; 
    492525            } 
    493             return $lexer; 
    494526        } 
    495527 
     
    497529        print $cache $name,"\n"; 
    498530        print $cache join("\n",@pat),"\n\n" or die "Can't print: $!"; 
    499         print $cache Dump($T) if $T; 
     531        print $cache Dump($T) if $T and not $STORABLE; 
    500532        close($cache) or die "Can't close: $!"; 
    501533        $self->deb("regenerated $dir/$file") if $DEBUG & DEBUG::autolexer; 
     
    790822        for (;;) { 
    791823            if ($state->{'~~'}) { 
    792                 if (not $state->{''}++) { 
    793                     for (@{$state->{'~~'}}) { 
    794                         next if /^\d/; 
    795                         next unless $_; 
    796                         next if ref $_ eq 'CODE'; 
    797                         our %RXCACHE; 
    798                         $_ = $RXCACHE{$_} //= eval "sub { \$::ORIG =~ /\\G$_/xsgc }"; 
    799                     } 
    800                 } 
     824                our %RXCACHE; 
    801825                my @x = @{$state->{'~~'}}; 
    802826                while (my ($final,$fnum) = splice(@x,0,2)) { 
     
    805829                        print STDERR $p,"     probing $fnum\n" if $DEBUG & DEBUG::autolexer; 
    806830                        pos($::ORIG) = $p; 
    807                         next unless &$final; 
     831                        next unless &{$RXCACHE{$final} //= eval "sub { \$::ORIG =~ /\\G$final/xsgc }"}; 
    808832                        $pend = pos($::ORIG); 
    809833                    } 
     
    10571081    if ($name =~ s/^(\w+):(?=[«<({[])/$1:sym/) { 
    10581082        my ($sym) = $name =~ /:sym(.*)/; 
     1083 
     1084        # unfortunately p5 doesn't understand q«...» 
    10591085        if ($sym =~ s/^«(.*)»$/$1/) { 
    10601086            my $ok = "'"; 
     
    10671093        my $rule = "token $name { <sym> }"; 
    10681094 
     1095        # produce p5 method name 
    10691096        my $mangle = $name; 
    10701097        $mangle =~ s/^(\w*):(sym)?//; 
     
    10861113        $mangle = $category . '__S_' . sprintf("%03d",$GEN++) . $mangle; 
    10871114 
    1088         # XXX assuming no equiv 
     1115        # XXX assuming no equiv specified 
    10891116        my $coercion = 'Additive'; 
    10901117        if ($name =~ /^prefix:/) { 
     
    11441171        eval $e or die "Can't create $name: $@\n"; 
    11451172        $::PARSER = $lang->cursor_fresh($genpkg); 
     1173        my $oldlexer = $::LEXERS{$WHAT}; 
     1174        my $newlexer = $::LEXERS{$genpkg} //= {}; 
     1175        $ANON{$genpkg}{SUP} = $WHAT; 
     1176        $ANON{$genpkg}{CAT} = $category; 
     1177        print STDERR "=====================================\nADD $rule => $mangle\n" if $DEBUG & DEBUG::mixins;; 
     1178        for my $name (sort keys %{$oldlexer}) { 
     1179            print STDERR "  $name:\n" if $DEBUG & DEBUG::mixins; 
     1180            my $same = 1; 
     1181            for my $pat (@{$oldlexer->{$name}->{PATS}}) { 
     1182                if ($pat =~ / $category /) { 
     1183                    print STDERR "\t$pat\n" if $DEBUG & DEBUG::mixins; 
     1184                    $same = 0; 
     1185                    last; 
     1186                } 
     1187            } 
     1188            # no need to regen a sublexer that will turn out the same 
     1189            $newlexer->{$name} = $oldlexer->{$name} if $same; 
     1190        } 
    11461191    } 
    11471192    $lang;