Changeset 531

Show
Ignore:
Timestamp:
10/19/07 12:20:32 (14 months ago)
Author:
nyarla
Message:

lang/perl/Text-Nyarlax: I moved global variables in the Start-up Actions.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • lang/perl/Text-Nyarlax/trunk/lib/Text/Nyarlax/Parser/PRD.pm

    r432 r531  
    55 
    66use Parse::RecDescent; 
    7 use List::MoreUtils qw( uniq ); 
    87use base qw( Text::Nyarlax::Parser ); 
    98 
    109our $VERSION = '0.1'; 
    1110 
    12 __PACKAGE__->mk_classdata( 'engine' ); 
    13  
    14 our %bracket = ( 
    15     Quote           =>  [qw(  " "  )], 
    16     Citation        =>  [qw( >> << )], 
    17     Notes           =>  ['(((',')))'], 
    18     Comparison      =>  [qw( // // )], 
    19     Emphasis        =>  [qw( ** ** )], 
    20     Definition      =>  [qw( ?? ?? )], 
    21     SubScript       =>  [qw( __ __ )], 
    22     SuperScript     =>  [qw( ~~ ~~ )], 
    23     Inserted        =>  [qw( ++ ++ )], 
    24     Deleted         =>  [qw( -- -- )], 
    25     InputText       =>  [qw( [| |] )], 
    26     InputCommand    =>  [qw( [$ $] )], 
    27     Keyboard        =>  [qw( <[ ]> )], 
    28     Sample          =>  [qw( [! !] )], 
    29     Code            =>  [qw(  ` `  )], 
    30     Abbreviation    =>  [  '((','))'], 
    31     Acronym         =>  [  '((','))'], 
    32     Ruby            =>  [  '((','))'], 
    33     Variable        =>  [qw(  { }  )], 
    34     Embedding       =>  [qw( {{ }} )], 
    35     HyperLink       =>  [qw( [[ ]] )], 
    36 ); 
    37  
    38 our $exclude_re; 
    39 our $text_re; 
    40  
    41 { 
    42     my @exclude = (); 
    43     for my $def ( values %bracket ) { 
    44         for ( @{ $def } ) { 
    45             $_ = quotemeta( $_ ); 
    46             push @exclude, $_; 
    47         } 
    48     } 
    49     $exclude_re = join q{|}, ( @exclude, quotemeta( '\\' ) ); 
    50     $text_re = qr{(?: (?! $exclude_re ) [^\n] )+}x; 
    51 } 
    52  
    53 our %variable = ( 
    54     '$' => 'string', 
    55     '#' => 'number', 
    56     '?' => 'boolean', 
    57     ':' => 'reference', 
    58     '*' => 'pointer', 
    59     '&' => 'function', 
    60     '@' => 'array', 
    61     '%' => 'hash', 
    62     '=' => 'structure', 
    63     '+' => 'object', 
    64     '!' => 'class', 
    65 ); 
    66  
    67 our $variable_re; 
    68 { 
    69     my @symbols = (); 
    70     for my $symbol ( keys %variable ) { 
    71         push @symbols, quotemeta( $symbol ); 
    72     } 
    73     $variable_re = join q{}, @symbols; 
    74     $variable_re = qr{[$variable_re]}; 
    75 } 
    76  
    77 our $grammer = <<'__GRAMMER__'; 
     11__PACKAGE__->mk_classdata( $_ ) for qw( engine grammer ); 
     12 
     13__PACKAGE__->grammer(<<'__GRAMMER__'); 
    7814    { 
    7915        use Text::Nyarlax::Element qw( element ); 
    8016        use Text::Nyarlax::Util qw( :all ); 
     17        use List::MoreUtils qw( uniq ); 
    8118        use Regexp::Common qw( URI ); 
     19        use vars qw( 
     20            %bracket %variable 
     21            $exclude_re $text_re $text_re_orig $variable_re 
     22        ); 
     23 
     24        %bracket = ( 
     25            Quote           =>  [qw(  " "  )], 
     26            Citation        =>  [qw( >> << )], 
     27            Notes           =>  ['(((',')))'], 
     28            Comparison      =>  [qw( // // )], 
     29            Emphasis        =>  [qw( ** ** )], 
     30            Definition      =>  [qw( ?? ?? )], 
     31            SubScript       =>  [qw( __ __ )], 
     32            SuperScript     =>  [qw( ~~ ~~ )], 
     33            Inserted        =>  [qw( ++ ++ )], 
     34            Deleted         =>  [qw( -- -- )], 
     35            InputText       =>  [qw( [| |] )], 
     36            InputCommand    =>  [qw( [$ $] )], 
     37            Keyboard        =>  [qw( <[ ]> )], 
     38            Sample          =>  [qw( [! !] )], 
     39            Code            =>  [qw(  ` `  )], 
     40            Abbreviation    =>  [  '((','))'], 
     41            Acronym         =>  [  '((','))'], 
     42            Ruby            =>  [  '((','))'], 
     43            Variable        =>  [qw(  { }  )], 
     44            Embedding       =>  [qw( {{ }} )], 
     45            HyperLink       =>  [qw( [[ ]] )], 
     46        ); 
     47 
     48        { 
     49            my @exclude = (); 
     50            for my $def ( values %bracket ) { 
     51                for ( @{ $def } ) { 
     52                    $_ = quotemeta( $_ ); 
     53                    push @exclude, $_; 
     54                } 
     55            } 
     56            $exclude_re = join q{|}, ( @exclude, quotemeta( '\\' ) ); 
     57            $text_re = qr{(?: (?! $exclude_re ) [^\n] )+}x; 
     58            $text_re_orig = $text_re; 
     59        } 
     60 
     61        %variable = ( 
     62            '$' => 'string', 
     63            '#' => 'number', 
     64            '?' => 'boolean', 
     65            ':' => 'reference', 
     66            '*' => 'pointer', 
     67            '&' => 'function', 
     68            '@' => 'array', 
     69            '%' => 'hash', 
     70            '=' => 'structure', 
     71            '+' => 'object', 
     72            '!' => 'class', 
     73        ); 
     74 
     75        { 
     76            my @symbols = (); 
     77            for my $symbol ( keys %variable ) { 
     78                push @symbols, quotemeta( $symbol ); 
     79            } 
     80            $variable_re = join q{}, @symbols; 
     81            $variable_re = qr{[$variable_re]}; 
     82        } 
     83 
    8284    } 
    83     parse               :   <rulevar: local %bracket    = %Text::Nyarlax::Parser::PRD::bracket      > 
    84                             <rulevar: local $text_re    = $Text::Nyarlax::Parser::PRD::text_re      > 
    85                             <rulevar: local $exclude_re = $Text::Nyarlax::Parser::PRD::exclude_re   > 
    86                             <rulevar: local %variable   = %Text::Nyarlax::Parser::PRD::variable     > 
    87                             <rulevar: local $variable_re= $Text::Nyarlax::Parser::PRD::variable_re  > 
    88                             <rulevar: local $h_level    = 1                                         > 
     85    parse               :   <rulevar: local $h_level = 1 > 
    8986 
    9087    parse               :   <skip: ''> Root 
     
    303300                        |   Variable 
    304301 
    305     InlineElement       :   <rulevar: local $text_re = $Text::Nyarlax::Parser::PRD::text_re > 
     302    InlineElement       :   <rulevar: local $text_re = $text_re_orig > 
    306303    InlineElement       :   /$bracket{$arg{'name'}}->[0]/ InlineNode(s) /$bracket{$arg{'name'}}->[1]/ 
    307304                        { 
     
    435432__GRAMMER__ 
    436433 
    437 __PACKAGE__->engine( Parse::RecDescent->new( $grammer ) ); 
    438  
    439 sub init { 
    440     my ( $self, $conf ) = @_; 
    441 } 
     434__PACKAGE__->engine( Parse::RecDescent->new( __PACKAGE__->grammer ) ); 
    442435 
    443436sub parse {