Changeset 22828 for src

Show
Ignore:
Timestamp:
10/30/08 17:59:32 (2 months ago)
Author:
azawawi
Message:

[STD_syntax_highlight] main(ARGV) refactoring

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • src/perl6/STD_syntax_highlight

    r22826 r22828  
    1616use DumpMatch; 
    1717 
    18 # These are needed for redspan 
     18=head1 NAME 
     19 
     20STD_syntax_highlight - Highlights Perl 6 source code using STD.pm 
     21 
     22=head1 SYNOPSIS 
     23 
     24    # print html output with inlined resources for 'comp_unit' 
     25    STD_syntax_highlight foo.pl 
     26 
     27    # print separate html, css and javascript files 
     28    STD_syntax_highlight --clean-html foo.pl 
     29 
     30    # print html for with 'statementlist' as the top-level rule 
     31    STD_syntax_highlight foo.pl statementlist 
     32 
     33    # write only simple html/css to simple.html 
     34    STD_syntax_highlight --simple-html=foo.simple.html foo.pl 
     35 
     36    # write redspans output to foo.redspans.html 
     37    STD_syntax_highlight --redspans-html=foo.redspans.html foo.pl 
     38 
     39=head1 SUBROUTINES 
     40 
     41=over 
     42 
     43=cut 
     44 
     45my ($clean_html,$help) = (0,0); 
     46my ($simple_html,$full_html,$redspans_html) = (0,0,'-'); 
     47my ($file, $parser, $src_text);  
     48 
     49# These are needed for redspans 
    1950$::ACTIONS = 'Actions'; 
    2051my @loc; 
    2152 
    22 =head1 NAME 
    23  
    24 STD_syntax_highlight - Highlights Perl 6 source code using STD.pm 
    25  
    26 =head1 SYNOPSIS 
    27  
    28     # print html output with inlined resources for 'comp_unit' 
    29     STD_syntax_highlight foo.pl 
    30  
    31     # print separate html, css and javascript files 
    32     STD_syntax_highlight --clean-html foo.pl 
    33  
    34     # print html for with 'statementlist' as the top-level rule 
    35     STD_syntax_highlight foo.pl statementlist 
    36  
    37     # write only simple html/css to simple.html 
    38     STD_syntax_highlight --simple-html=foo.simple.html foo.pl 
    39  
    40     # write redspans output to foo.redspans.html 
    41     STD_syntax_highlight --redspans-html=foo.redspans.html foo.pl 
    42  
    43 =head1 SUBROUTINES 
    44  
    45 =over 
    46  
    47 =cut 
    48  
    49 #process the command line 
    50 my ($clean_html,$help) = (0,0); 
    51 my ($simple_html,$full_html,$redspans_html) = (0,0,'-'); 
    52 GetOptions( 
    53     "clean-html"=>\$clean_html, 
    54     "simple-html=s"=>\$simple_html, 
    55     "full-html=s"=>\$full_html, 
    56     "redspans-html=s"=>\$redspans_html, 
    57     "help"=>\$help 
    58 ); 
    59  
    60 if ($#ARGV < 0 || $help) { 
    61     die <<"HELP"; 
     53=item main 
     54 
     55Your standard main method 
     56=cut 
     57sub main { 
     58     
     59    #process the command line 
     60    GetOptions( 
     61        "clean-html"=>\$clean_html, 
     62        "simple-html=s"=>\$simple_html, 
     63        "full-html=s"=>\$full_html, 
     64        "redspans-html=s"=>\$redspans_html, 
     65        "help"=>\$help 
     66    ); 
     67 
     68    if ($#ARGV < 0 || $help) { 
     69        die <<"HELP"; 
    6270USAGE:  
    6371    $PROGRAM_NAME [options] filename [rule] 
     
    7886     
    7987HELP 
    80 } 
    81  
    82 #start parsing... 
    83 my $file = shift; 
    84 my $what = shift // 'comp_unit'; 
    85  
    86 unless(-r $file) { 
    87     die "Could not open '$file' for reading\n"; 
    88 } 
    89  
    90 # slurp the file for redspans 
    91 my $txt = read_file($file); 
    92 $loc[length($txt) - 1] = []; 
    93  
    94 my $r = STD->parsefile($file,$what); 
     88    } 
     89 
     90    #start parsing... 
     91    $file = shift @ARGV; 
     92    my $what = shift @ARGV // 'comp_unit'; 
     93 
     94    unless(-r $file) { 
     95        die "Could not open '$file' for reading\n"; 
     96    } 
     97 
     98    # slurp the file for redspans 
     99    $src_text = read_file($file); 
     100    $loc[length($src_text) - 1] = []; 
     101 
     102    $parser = STD->parsefile($file,$what); 
     103 
     104    # and finally print out the html code 
     105    highlight_match($what=>$parser,{}); 
     106} 
    95107 
    96108=item write_html_file 
     
    124136    if($simple_html) { 
    125137        traverse_match($r,$name,0,$events,$opt); 
    126         my $html = highlight_perl6_simple($::ORIG,$events,$opt); 
     138        my $html = highlight_perl6_simple($r,$::ORIG,$events,$opt); 
    127139        write_html_file $simple_html, $html; 
    128140    } 
    129141    if($full_html) { 
    130142        traverse_match($r,$name,0,$events,$opt); 
    131         my $html = highlight_perl6($::ORIG,$events,$opt); 
     143        my $html = highlight_perl6_full($r,$::ORIG,$events,$opt); 
    132144        write_html_file $full_html, $html; 
    133145    } 
     
    138150} 
    139151 
    140 =item highlight_perl6 
     152=item highlight_perl6_full 
    141153 
    142154Generates the Perl6 highlighted HTML string for STD parse tree provided.  
    143155The resources can be inlined (by default) or externalized (--clean-html).  
    144156=cut 
    145 sub highlight_perl6 { 
    146     my ($orig,$events,$opt) = @_; 
     157sub highlight_perl6_full { 
     158    my ($r,$orig,$events,$opt,$file) = @ARG, 
    147159    my $str = ""; 
    148160    my $at = 0; 
     
    204216 
    205217        if($curr_rule eq 'identifier') { 
    206             if($r->is_type($text)) { 
     218            if($parser->is_type($text)) { 
    207219                $str .= qq{<span class="_type">$esc_text</span>}; 
    208             } elsif($r->is_routine($text)) { 
     220            } elsif($parser->is_routine($text)) { 
    209221                $str .= qq{<span class="_routine">$esc_text</span>}; 
    210222            } else { 
     
    239251=cut 
    240252sub highlight_perl6_simple { 
    241     my ($orig,$events,$opt) = @_; 
     253    my ($orig,$events,$opt) = @ARG; 
    242254    my $str = ""; 
    243255    my $at = 0; 
     
    292304        # process types, routines and identifiers 
    293305        if($curr_rule eq 'identifier') { 
    294             if($r->is_type($text)) { 
     306            if($parser->is_type($text)) { 
    295307                $code .= qq{<span class="_type">$esc_text</span>}; 
    296             } elsif($r->is_routine($text)) { 
     308            } elsif($parser->is_routine($text)) { 
    297309                $code .= qq{<span class="_routine">$esc_text</span>}; 
    298310            } else { 
     
    378390    for my $i (0 .. @loc-1) { 
    379391        say("Undefined $i"),next unless defined $loc[$i]; 
    380         my $c = substr($txt,$i,1); 
     392        my $c = substr($src_text,$i,1); 
    381393        my $tree = "@{$loc[$i]}"; 
    382394        if($tree ne $last_tree) { 
     
    415427################################################################### 
    416428# R E D S P A N S 
    417 my @locs; 
    418429{  
    419430    package Actions; 
     
    473484=cut 
    474485 
    475 # and finally print out the html code 
    476 highlight_match($what=>$r,{}); 
    477  
     486main @ARGV;