Changeset 22875 for src

Show
Ignore:
Timestamp:
11/04/08 18:45:06 (2 months ago)
Author:
azawawi
Message:

[STD_syntax_highlight] smaller code, refactored --ansi-text

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • src/perl6/STD_syntax_highlight

    r22874 r22875  
    365365    close ANSI_FILE; 
    366366 
    367     my ($last_tree,$buffer) = ("",""); 
    368     for my $i (0 .. @loc-1) { 
    369         next unless defined $loc[$i]; 
    370         my $c = substr($src_text,$i,1); 
    371         my $tree = ""; 
    372         for my $action_ref (@{$loc[$i]}) { 
    373             $tree .= ${$action_ref} . " "; 
    374         } 
    375         if($tree ne $last_tree) { 
    376             my $rule; 
    377             my $rule_to_color = 0; 
    378             my @rules = (); 
    379             @rules = reverse(split / /,$last_tree) if $last_tree ne ''; 
    380             for $rule (@rules) { 
    381                 if($rule eq 'unv') { 
    382                     $rule_to_color = '_comment'; 
    383                     last; 
    384                 } elsif($colors{$rule} && $buffer ne '') { 
    385                     $rule_to_color = $rule; 
    386                     last; 
    387                 } 
    388             } 
    389             if($rule_to_color) { 
    390                 my $color = $colors{$rule_to_color}; 
    391                 if($last_tree =~ /identifier/) { 
    392                     if($parser->is_type($buffer)) { 
    393                         $str .= (color $color) . $buffer. (color 'reset'); 
    394                     } elsif($parser->is_routine($buffer)) { 
    395                         $str .= (color $color) . $buffer . (color 'reset'); 
    396                     } else { 
    397                         $str .= (color $color) . $buffer . (color 'reset'); 
    398                     } 
    399                 } else {               
    400                     $str .= (color $color) . $buffer . (color 'reset'); 
    401                 } 
    402             } else { 
    403                 $str .= qq{$buffer}; 
    404             } 
    405             $buffer = $c; 
     367    local *spit_ansi_text = sub { 
     368        my ($i, $buffer, $rule, $tree) = @ARG; 
     369        if($rule) { 
     370            my $color = $colors{$rule}; 
     371            $str .= (color $color) . $buffer. (color 'reset'); 
    406372        } else { 
    407             $buffer .= $c; 
    408         } 
    409         $last_tree = $tree; 
    410     } 
    411  
    412    $str; 
     373            $str .= $buffer; 
     374        } 
     375    }; 
     376 
     377    redspans_traverse(\&spit_ansi_text,%colors);  
     378    
     379    $str; 
    413380} 
    414381