- Timestamp:
- 07/28/08 02:40:50 (6 months ago)
- Location:
- misc/elfish/rx_on_re
- Files:
-
- 2 modified
-
emit5.pm (modified) (1 diff)
-
remains_of_Regexp_ModuleA.pm (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
misc/elfish/rx_on_re/emit5.pm
r21586 r21587 675 675 676 676 eval_perl5( EmitRegex.regex_prelude() ); 677 678 679 #---------------------------------------------------------------------- 680 # AST to RMARE emitters 681 #---------------------------------------------------------------------- 682 683 package Regexp::ModuleA { 684 685 # any regexp 686 class AST::Pat5 { 687 method RMARE_emit () { 688 my $re = $.RMARE_wrap_re_with_mods(self.<pat>); 689 $.RMARE_eat_regexp($re); 690 } 691 } 692 693 # \Qabc\E 694 class AST::Exact { 695 method RMARE_emit () { 696 my $re = self.<text>; 697 $re.re_sub_g('([^\w\s])','\\\\$1'); 698 $re = $.RMARE_wrap_re_with_mods($re); 699 $.RMARE_eat_regexp($re); 700 } 701 } 702 703 # (?imsx-imsx:...) 704 class AST::Mod_expr { 705 method RMARE_emit () { 706 self.<expr>.RMARE_emit; 707 } 708 } 709 710 # (?imsx-imsx) 711 class AST::Mod_inline { 712 method RMARE_emit () { 713 $.RMARE_noop; 714 } 715 } 716 717 # ? * + {n,m} ?? *? etc 718 class AST::Quant { 719 method RMARE_emit () { 720 my $min = self.<min>; 721 my $max = self.<max>; 722 my $nongreedy = self.<nongreedy>; 723 $min = 0 if !defined $min; 724 $max = 1000**1000**1000 if !defined $max; #XXX inf 725 die "assert - Quant min <= max" if $min > $max; 726 my $f = self.<expr>.RMARE_emit; 727 my $f1 = $.RMARE_repeat($f,$min,$max,$nongreedy); 728 if self.<flags><ratchet> { 729 $.RMARE_concat([$f1,$.RMARE_commit_sequence]); 730 } else { 731 $f1; 732 } 733 } 734 } 735 736 # a|b 737 class AST::Alt { 738 method RMARE_emit { 739 my $f1 = $.RMARE_alt(self.<exprs>.map(sub($o){$o.RMARE_emit})); 740 if self.<flags><ratchet> { 741 $.RMARE_concat([$f1,$.RMARE_commit_sequence()]); 742 } else { 743 $f1; 744 } 745 } 746 } 747 748 # a&b 749 class AST::Conj { 750 method RMARE_emit { 751 $.RMARE_conj(self.<exprs>.map(sub($o){$o.RMARE_emit})) 752 } 753 } 754 755 # ab 756 class AST::Seq { 757 method RMARE_emit { 758 $.RMARE_concat(self.<exprs>.map(sub($o){$o.RMARE_emit})) 759 } 760 } 761 762 # .. := ... 763 class AST::Alias { 764 method RMARE_emit { 765 my $target_spec = self.<target_spec>; 766 my $construct_kind = self.<construct_kind>; 767 my $construct_in_quant = self.<construct_in_quant>; 768 my $f = self.<expr>.RMARE_emit; 769 if ($construct_kind eq 'group' 770 && $construct_in_quant 771 && $target_spec[0] =~ /^\$/) 772 { 773 my $cs = $.RMARE_capture_string($f); 774 $.RMARE_alias_wrap($cs,undef,1,0,0,$target_spec); 775 } 776 else { 777 $f; 778 } 779 } 780 } 781 782 # (?:a) 783 class AST::Grp { 784 method RMARE_emit { 785 my $target_spec = self.<target_spec>; 786 my $in_quant = self.<in_quant>; 787 $.RMARE_group(self.<expr>.RMARE_emit,$target_spec,$in_quant); 788 } 789 } 790 791 # (a) 792 class AST::Cap { 793 method RMARE_emit { 794 my $in_quant = {if self.<in_quant> { 1 } else { 0 }}; 795 my $target_spec = self.<target_spec>; 796 my $is6 = not self.<flags><p5>; 797 my $idx = {if $is6 798 { self.<cap6_idx> } else 799 { self.<cap5_idx> }}; 800 my $f = self.<expr>.RMARE_emit; 801 $.RMARE_capture($idx,$f,$is6,self.<nparen6>, 802 $in_quant,$target_spec); 803 } 804 } 805 806 # \1 807 class AST::Backref { 808 method RMARE_emit { 809 my $noop = $.RMARE_noop; 810 my $idx = self.<backref_n> -1; 811 $.RMARE_eat_backref($idx,'(?'~$.RMARE_imsx~')'); 812 } #XXX move imsx into eat 813 } 814 815 816 # gap here 817 818 # (?>) 819 class AST::Independent { 820 method RMARE_emit { 821 my $f = self.<expr>.RMARE_emit; 822 $.RMARE_independent($f); 823 } 824 } 825 826 # nonexistent 827 class AST::CommitSequence { 828 method RMARE_emit { 829 $.RMARE_commit_sequence 830 } 831 } 832 833 # :: 834 class AST::CommitGroup { 835 method RMARE_emit { 836 $.RMARE_commit_group 837 } 838 } 839 840 # ::: 841 class AST::CommitRegex { 842 method RMARE_emit { 843 $.RMARE_commit_regex 844 } 845 } 846 847 # <commit> 848 class AST::CommitMatch { 849 method RMARE_emit { 850 $.RMARE_commit_match 851 } 852 } 853 854 855 } -
misc/elfish/rx_on_re/remains_of_Regexp_ModuleA.pm
r21458 r21587 87 87 88 88 { 89 # any regexp90 package Regexp::ModuleA::AST::Pat5;91 sub RMARE_emit {92 my($o)=@_;93 my $re = $o->RMARE_wrap_re_with_mods($o->{pat});94 $o->RMARE_eat_regexp($re);95 }96 97 # \Qabc\E98 package Regexp::ModuleA::AST::Exact;99 sub RMARE_emit {100 my($o)=@_;101 my $re = $o->{text};102 $re =~ s/([^\w\s])/\\$1/g;103 $re = $o->RMARE_wrap_re_with_mods($re);104 $o->RMARE_eat_regexp($re);105 }106 107 # (?imsx-imsx:...)108 package Regexp::ModuleA::AST::Mod_expr;109 sub RMARE_emit {110 my($o)=@_;111 $o->{expr}->RMARE_emit;112 }113 114 # (?imsx-imsx)115 package Regexp::ModuleA::AST::Mod_inline;116 sub RMARE_emit {117 my($o)=@_;118 $o->RMARE_noop;119 }120 121 # ? * + {n,m} ?? *? etc122 package Regexp::ModuleA::AST::Quant;123 sub RMARE_emit {124 my($o)=@_;125 my($min,$max,$nongreedy)= (@$o{'min','max','nongreedy'});126 $min = 0 if !defined $min;127 $max = 1000**1000**1000 if !defined $max; #XXX inf128 die "assert - Quant min <= max" if $min > $max;129 my $f = $o->{expr}->RMARE_emit;130 my $f1 = $o->RMARE_repeat($f,$min,$max,$nongreedy);131 if($o->{flags}{ratchet}) {132 $o->RMARE_concat([$f1,$o->RMARE_commit_sequence()]);133 } else {134 $f1;135 }136 }137 138 # a|b139 package Regexp::ModuleA::AST::Alt;140 sub RMARE_emit {141 my($o)=@_;142 my $f1 = $o->RMARE_alt([map{$_->RMARE_emit}@{$o->{exprs}}]);143 if($o->{flags}{ratchet}) {144 $o->RMARE_concat([$f1,$o->RMARE_commit_sequence()]);145 } else {146 $f1;147 }148 }149 150 # a&b151 package Regexp::ModuleA::AST::Conj;152 sub RMARE_emit {153 my($o)=@_;154 $o->RMARE_conj([map{$_->RMARE_emit}@{$o->{exprs}}]);155 }156 157 # ab158 package Regexp::ModuleA::AST::Seq;159 sub RMARE_emit {160 my($o)=@_;161 $o->RMARE_concat([map{$_->RMARE_emit}@{$o->{exprs}}]);162 }163 164 # .. := ...165 package Regexp::ModuleA::AST::Alias;166 sub RMARE_emit {167 my($o)=@_;168 my $target_spec = $o->{target_spec};169 my $construct_kind = $o->{construct_kind};170 my $construct_in_quant = $o->{construct_in_quant};171 my $f = $o->{expr}->RMARE_emit;172 if($construct_kind eq 'group'173 && $construct_in_quant174 && $target_spec->[0] =~ /^\$/)175 {176 my $cs = $o->RMARE_capture_string($f);177 $o->RMARE_alias_wrap($cs,undef,1,0,0,$target_spec);178 }179 else {180 $f;181 }182 }183 184 # (?:a)185 package Regexp::ModuleA::AST::Grp;186 sub RMARE_emit {187 my($o)=@_;188 my $target_spec = $o->{target_spec};189 my $in_quant = $o->{in_quant};190 $o->RMARE_group($o->{expr}->RMARE_emit,$target_spec,$in_quant);191 }192 193 # (a)194 package Regexp::ModuleA::AST::Cap;195 sub RMARE_emit {196 my($o)=@_;197 my $in_quant = $o->{in_quant} ? 1 : 0;198 my $target_spec = $o->{target_spec};199 my $is6 = !$o->{flags}{'p5'};200 my $idx = ($is6201 ? $o->{cap6_idx}202 : $o->{cap5_idx});203 my $f = $o->{expr}->RMARE_emit;204 $o->RMARE_capture($idx,$f,$is6,$o->{nparen6},205 $in_quant,$target_spec);206 }207 208 # \1209 package Regexp::ModuleA::AST::Backref;210 sub RMARE_emit {211 my($o)=@_;212 my $noop = $o->RMARE_noop;213 my $idx = $o->{'backref_n'} -1;214 $o->RMARE_eat_backref($idx,'(?'.$o->RMARE_imsx.')');215 } #XXX move imsx into eat216 89 217 90 # <foo> … … 352 225 s/\$([1-9])/'$M->['.($1-1).']'/eg; #XXX more... 353 226 $_; 354 }355 356 # (?>)357 package Regexp::ModuleA::AST::Independent;358 sub RMARE_emit {359 my($o)=@_;360 my $f = $o->{expr}->RMARE_emit;361 $o->RMARE_independent($f);362 227 } 363 228 … … 455 320 } 456 321 return 0; 457 }458 459 # nonexistent460 package Regexp::ModuleA::AST::CommitSequence;461 sub RMARE_emit {462 my($o)=@_;463 $o->RMARE_commit_sequence();464 }465 466 # ::467 package Regexp::ModuleA::AST::CommitGroup;468 sub RMARE_emit {469 my($o)=@_;470 $o->RMARE_commit_group();471 }472 473 # :::474 package Regexp::ModuleA::AST::CommitRegex;475 sub RMARE_emit {476 my($o)=@_;477 $o->RMARE_commit_regex();478 }479 480 # <commit>481 package Regexp::ModuleA::AST::CommitMatch;482 sub RMARE_emit {483 my($o)=@_;484 $o->RMARE_commit_match();485 322 } 486 323
