| 1 | package Pugs::Emitter::Rule::Perl5::Regex; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | # p6-rule perl5 emitter for emitting perl5 regexes | ||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | =for TODO | ||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | plug into the :ratchet emitter | ||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | @ (non-)interpolation (test) | ||||||||||||||||||||
| 10 | aliased, named captures | ||||||||||||||||||||
| 11 | nested captures | ||||||||||||||||||||
| 12 | quantified captures | ||||||||||||||||||||
| 13 | ranges | ||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | die() on captures that would have wrong numbering | ||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | BUGS: | ||||||||||||||||||||
| 18 | - nested captures are not detected | ||||||||||||||||||||
| 19 | - set operations on character classes are not detected | ||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | =cut | ||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 24 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 25 | 1 | ▶ | use Data::Dumper; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 26 | 1 | $Data::Dumper::Indent = 1; | |||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | 1 | our $capture_count; | |||||||||||||||||||
| 29 | 1 | our $capture_to_array; | |||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | 0 | ▶ | sub emit { | ||||||||||||||||||
|
Subroutine
emit: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 32 | 0 | my ($grammar, $ast, $param) = @_; | |||||||||||||||||||
| 33 | 0 | my $sigspace = $param->{sigspace}; | |||||||||||||||||||
| 34 | 0 | local $capture_count = -1; | |||||||||||||||||||
| 35 | 0 | local $capture_to_array = 0; | |||||||||||||||||||
| 36 | #print "rule: ", Dumper( $ast ); | ||||||||||||||||||||
| 37 | 0 | ▶ | die "sigspace not supported in P5 mode (can't call <ws> subrule)" | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 38 | if $sigspace; | ||||||||||||||||||||
| 39 | 0 | my $p5regex = '(?m)' . emit_rule( $ast ); | |||||||||||||||||||
| 40 | # print ":P5/$p5regex/ \n"; | ||||||||||||||||||||
| 41 | 0 | return $p5regex; | |||||||||||||||||||
| 42 | } | ||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | 0 | ▶ | sub emit_rule { | ||||||||||||||||||
|
Subroutine
emit_rule: not called CC=2 SCAR=12.6
Pod
undocumented
| |||||||||||||||||||||
| 45 | 0 | my $n = $_[0]; | |||||||||||||||||||
| 46 | 0 | ▶ | die "unknown node: ", Dumper( $n ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 47 | unless ref( $n ) eq 'HASH'; | ||||||||||||||||||||
| 48 | #print "NODE ", Dumper($n); | ||||||||||||||||||||
| 49 | 0 | my ($k) = keys %$n; | |||||||||||||||||||
| 50 | 0 | my $v = $$n{$k}; | |||||||||||||||||||
| 51 | # XXX - use real references | ||||||||||||||||||||
| 52 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 53 | #print "NODE ", Dumper($k), ", ", Dumper($v); | ||||||||||||||||||||
| 54 | 0 | my $code = &$k( $v, '' ); | |||||||||||||||||||
| 55 | 0 | return $code; | |||||||||||||||||||
| 56 | } | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | #rule nodes | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | 0 | ▶ | sub non_capturing_group { | ||||||||||||||||||
|
Subroutine
non_capturing_group: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 61 | 0 | return "(?:" . emit_rule( $_[0] ) . ")"; | |||||||||||||||||||
| 62 | } | ||||||||||||||||||||
| 63 | 0 | ▶ | sub quant { | ||||||||||||||||||
|
Subroutine
quant: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||||||||||
| 64 | 0 | my $term = $_[0]->{'term'}; | |||||||||||||||||||
| 65 | 0 | ▶ | my $quantifier = $_[0]->{quant} || ''; | ||||||||||||||||||
|
Condition0%0/2
$_[0]{'quant'} || ''
MC/DC0%0/1
$_[0]{'quant'} || ''
$_[0]{'quant'}
Truth table0%0/2
A
A= $_[0]{'quant'}
| |||||||||||||||||||||
| 66 | 0 | ▶ | my $greedy = $_[0]->{greedy} || ''; # + ? | ||||||||||||||||||
|
Condition0%0/2
$_[0]{'greedy'} || ''
MC/DC0%0/1
$_[0]{'greedy'} || ''
$_[0]{'greedy'}
Truth table0%0/2
A
A= $_[0]{'greedy'}
| |||||||||||||||||||||
| 67 | 0 | ▶ | $greedy = '' if $greedy eq '+'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 68 | #print "QUANT: ",Dumper($_[0]); | ||||||||||||||||||||
| 69 | # TODO: fix grammar to not emit empty quantifier | ||||||||||||||||||||
| 70 | 0 | ▶ | die "ranges not implemented" | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 71 | if ref( $quantifier ); | ||||||||||||||||||||
| 72 | 0 | my $rul; | |||||||||||||||||||
| 73 | { | ||||||||||||||||||||
| 74 | #print "Term: ", Dumper($term), "\n"; | ||||||||||||||||||||
| 75 | 0 | my $cap = $capture_to_array; | |||||||||||||||||||
| 76 | 0 | ▶ | local $capture_to_array = $cap || ( $quantifier ne '' ); | ||||||||||||||||||
|
Condition0%0/3
$cap || $quantifier ne ''
MC/DC0%0/2
$cap || $quantifier ne ''
$cap
$quantifier ne ''
Truth table0%0/3
A || B
A= $capB= $quantifier ne ''
| |||||||||||||||||||||
| 77 | 0 | $rul = emit_rule( $term ); | |||||||||||||||||||
| 78 | } | ||||||||||||||||||||
| 79 | 0 | my $quant = $quantifier . $greedy; | |||||||||||||||||||
| 80 | 0 | ▶ | return "(?:$rul)$quant" if $quant; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 81 | 0 | return $rul; | |||||||||||||||||||
| 82 | } | ||||||||||||||||||||
| 83 | 0 | ▶ | sub alt { | ||||||||||||||||||
|
Subroutine
alt: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||
| 84 | 0 | my @s; | |||||||||||||||||||
| 85 | 0 | my $count = $capture_count; | |||||||||||||||||||
| 86 | 0 | my $max = -1; | |||||||||||||||||||
| 87 | 0 | for ( @{$_[0]} ) { | |||||||||||||||||||
| 88 | 0 | $capture_count = $count; | |||||||||||||||||||
| 89 | 0 | my $tmp = emit_rule( $_ ); | |||||||||||||||||||
| 90 | # print ' ',$capture_count; | ||||||||||||||||||||
| 91 | 0 | ▶ | $max = $capture_count | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 92 | if $capture_count > $max; | ||||||||||||||||||||
| 93 | 0 | push @s, $tmp; # if $tmp; | |||||||||||||||||||
| 94 | } | ||||||||||||||||||||
| 95 | 0 | $capture_count = $max; | |||||||||||||||||||
| 96 | # print " max = $capture_count\n"; | ||||||||||||||||||||
| 97 | return | ||||||||||||||||||||
| 98 | 0 | "(?:" . join( "|", @s ) . ")"; | |||||||||||||||||||
| 99 | } | ||||||||||||||||||||
| 100 | 0 | ▶ | sub alt1 { &alt } | ||||||||||||||||||
|
Subroutine
alt1: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 101 | 0 | ▶ | sub concat { | ||||||||||||||||||
|
Subroutine
concat: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 102 | return join( "", | ||||||||||||||||||||
| 103 | 0 | map { emit_rule( $_ ) } @{$_[0]} | |||||||||||||||||||
| 104 | ); | ||||||||||||||||||||
| 105 | } | ||||||||||||||||||||
| 106 | 0 | ▶ | sub code { | ||||||||||||||||||
|
Subroutine
code: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 107 | 0 | die "code not implemented"; | |||||||||||||||||||
| 108 | } | ||||||||||||||||||||
| 109 | 0 | ▶ | sub dot { | ||||||||||||||||||
|
Subroutine
dot: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 110 | 0 | '(?:\n\r?|\r\n?|.)' | |||||||||||||||||||
| 111 | } | ||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | 0 | ▶ | sub variable { | ||||||||||||||||||
|
Subroutine
variable: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 114 | 0 | die "variable interpolation not implemented"; | |||||||||||||||||||
| 115 | } | ||||||||||||||||||||
| 116 | 0 | ▶ | sub special_char { | ||||||||||||||||||
|
Subroutine
special_char: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||
| 117 | 0 | my $char = substr($_[0],1); | |||||||||||||||||||
| 118 | 0 | ▶ | return '(?:\n\r?|\r\n?)' | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 119 | if $char eq 'n'; | ||||||||||||||||||||
| 120 | 0 | ▶ | return '(?!\n\r?|\r\n?).' | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 121 | if $char eq 'N'; | ||||||||||||||||||||
| 122 | 0 | for ( qw( r t e f w d s ) ) { | |||||||||||||||||||
| 123 | 0 | ▶ | return "\\$_" if $char eq $_; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 124 | 0 | ▶ | return "[^\\$_]" if $char eq uc($_); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 125 | } | ||||||||||||||||||||
| 126 | 0 | return '\\' . $char; | |||||||||||||||||||
| 127 | } | ||||||||||||||||||||
| 128 | 0 | ▶ | sub match_variable { | ||||||||||||||||||
|
Subroutine
match_variable: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 129 | 0 | die "no match variables yet"; | |||||||||||||||||||
| 130 | } | ||||||||||||||||||||
| 131 | 0 | ▶ | sub closure { | ||||||||||||||||||
|
Subroutine
closure: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 132 | 0 | die "no closures"; | |||||||||||||||||||
| 133 | } | ||||||||||||||||||||
| 134 | 0 | ▶ | sub capturing_group { | ||||||||||||||||||
|
Subroutine
capturing_group: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||
| 135 | 0 | my $program = $_[0]; | |||||||||||||||||||
| 136 | 0 | ▶ | die "capture to array not implemented" | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 137 | if $capture_to_array; | ||||||||||||||||||||
| 138 | 0 | $capture_count++; | |||||||||||||||||||
| 139 | { | ||||||||||||||||||||
| 140 | 0 | local $capture_count = -1; | |||||||||||||||||||
| 141 | 0 | local $capture_to_array = 0; | |||||||||||||||||||
| 142 | 0 | ▶ | $program = emit_rule( $program ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 143 | if ref( $program ); | ||||||||||||||||||||
| 144 | } | ||||||||||||||||||||
| 145 | 0 | return "(" . $program . ")" | |||||||||||||||||||
| 146 | } | ||||||||||||||||||||
| 147 | 0 | ▶ | sub capture_as_result { | ||||||||||||||||||
|
Subroutine
capture_as_result: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 148 | 0 | die "return objects not implemented"; | |||||||||||||||||||
| 149 | } | ||||||||||||||||||||
| 150 | 0 | ▶ | sub named_capture { | ||||||||||||||||||
|
Subroutine
named_capture: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 151 | 0 | die "no named captures"; | |||||||||||||||||||
| 152 | } | ||||||||||||||||||||
| 153 | 0 | ▶ | sub negate { | ||||||||||||||||||
|
Subroutine
negate: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 154 | 0 | die "no negate"; | |||||||||||||||||||
| 155 | } | ||||||||||||||||||||
| 156 | 0 | ▶ | sub before { | ||||||||||||||||||
|
Subroutine
before: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 157 | 0 | my $program = $_[0]{rule}; | |||||||||||||||||||
| 158 | 0 | ▶ | $program = emit_rule( $program ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 159 | if ref( $program ); | ||||||||||||||||||||
| 160 | 0 | return "(?=" . $program . ")"; | |||||||||||||||||||
| 161 | } | ||||||||||||||||||||
| 162 | 0 | ▶ | sub not_before { | ||||||||||||||||||
|
Subroutine
not_before: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 163 | 0 | my $program = $_[0]{rule}; | |||||||||||||||||||
| 164 | 0 | ▶ | $program = emit_rule( $program ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 165 | if ref( $program ); | ||||||||||||||||||||
| 166 | 0 | return "(?!" . $program . ")"; | |||||||||||||||||||
| 167 | } | ||||||||||||||||||||
| 168 | 0 | ▶ | sub after { | ||||||||||||||||||
|
Subroutine
after: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 169 | 0 | my $program = $_[0]{rule}; | |||||||||||||||||||
| 170 | 0 | ▶ | $program = emit_rule( $program ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 171 | if ref( $program ); | ||||||||||||||||||||
| 172 | 0 | return "(?<=" . $program . ")"; | |||||||||||||||||||
| 173 | } | ||||||||||||||||||||
| 174 | 0 | ▶ | sub not_after { | ||||||||||||||||||
|
Subroutine
not_after: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 175 | 0 | my $program = $_[0]{rule}; | |||||||||||||||||||
| 176 | 0 | ▶ | $program = emit_rule( $program ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 177 | if ref( $program ); | ||||||||||||||||||||
| 178 | 0 | return "(?<!" . $program . ")"; | |||||||||||||||||||
| 179 | } | ||||||||||||||||||||
| 180 | 0 | ▶ | sub colon { | ||||||||||||||||||
|
Subroutine
colon: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||
| 181 | 0 | my $str = $_[0]; | |||||||||||||||||||
| 182 | 0 | ▶ | return '\z' | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 183 | if $str eq '$'; | ||||||||||||||||||||
| 184 | 0 | ▶ | return '\A' | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 185 | if $str eq '^'; | ||||||||||||||||||||
| 186 | 0 | ▶ | return '$' | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 187 | if $str eq '$$'; | ||||||||||||||||||||
| 188 | 0 | ▶ | return '^' | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 189 | if $str eq '^^'; | ||||||||||||||||||||
| 190 | 0 | die "'$str' not implemented"; | |||||||||||||||||||
| 191 | } | ||||||||||||||||||||
| 192 | 0 | ▶ | sub modifier { | ||||||||||||||||||
|
Subroutine
modifier: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 193 | 0 | my $str = $_[0]; | |||||||||||||||||||
| 194 | 0 | die "modifier '$str' not implemented"; | |||||||||||||||||||
| 195 | } | ||||||||||||||||||||
| 196 | 0 | ▶ | sub constant { | ||||||||||||||||||
|
Subroutine
constant: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||
| 197 | 0 | ▶ | return "" | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 198 | unless length($_[0]); | ||||||||||||||||||||
| 199 | 0 | ▶ | return '\\/' if $_[0] eq '/'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 200 | 0 | return $_[0]; | |||||||||||||||||||
| 201 | } | ||||||||||||||||||||
| 202 | |||||||||||||||||||||
| 203 | 1 | ▶ | use vars qw( %char_class ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 204 | 1 | ▶ | BEGIN { | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 205 | 1 | %char_class = map { $_ => 1 } qw( | |||||||||||||||||||
| 206 | alpha alnum ascii blank | ||||||||||||||||||||
| 207 | cntrl digit graph lower | ||||||||||||||||||||
| 208 | print punct space upper | ||||||||||||||||||||
| 209 | word xdigit | ||||||||||||||||||||
| 210 | ); | ||||||||||||||||||||
| 211 | } | ||||||||||||||||||||
| 212 | |||||||||||||||||||||
| 213 | 0 | ▶ | sub char_class { | ||||||||||||||||||
|
Subroutine
char_class: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 214 | 0 | my $cmd = Pugs::Emitter::Rule::Perl5::CharClass::emit( $_[0] ); | |||||||||||||||||||
| 215 | 0 | return $cmd; | |||||||||||||||||||
| 216 | } | ||||||||||||||||||||
| 217 | |||||||||||||||||||||
| 218 | 0 | ▶ | sub metasyntax { | ||||||||||||||||||
|
Subroutine
metasyntax: not called CC=8 SCAR=42.8
Pod
undocumented
| |||||||||||||||||||||
| 219 | # <cmd> | ||||||||||||||||||||
| 220 | 0 | my $cmd = $_[0]; | |||||||||||||||||||
| 221 | 0 | my $prefix = substr( $cmd, 0, 1 ); | |||||||||||||||||||
| 222 | 0 | ▶ | if ( $prefix eq q(') ) { # single quoted literal ' | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 223 | 0 | $cmd = substr( $cmd, 1, -1 ); | |||||||||||||||||||
| 224 | 0 | $cmd =~ s/([\$\@\%\[\]\+\*\(\)\?\/])/\\$1/g; | |||||||||||||||||||
| 225 | 0 | return $cmd; | |||||||||||||||||||
| 226 | } | ||||||||||||||||||||
| 227 | 0 | ▶ | if ( $prefix eq '.' ) { # non_capturing_subrule / code assertion | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 228 | 0 | $cmd = substr( $cmd, 1 ); | |||||||||||||||||||
| 229 | 0 | ▶ | if ( exists $char_class{$cmd} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 230 | # XXX - inlined char classes are not inheritable, but this should be ok | ||||||||||||||||||||
| 231 | 0 | return "[[:$cmd:]]"; | |||||||||||||||||||
| 232 | } | ||||||||||||||||||||
| 233 | } | ||||||||||||||||||||
| 234 | 0 | ▶ | if ( $prefix eq '?' ) { # non_capturing_subrule / code assertion | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 235 | # XXX FIXME | ||||||||||||||||||||
| 236 | 0 | $cmd = substr( $cmd, 1 ); | |||||||||||||||||||
| 237 | 0 | ▶ | if ( exists $char_class{$cmd} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 238 | # XXX - inlined char classes are not inheritable, but this should be ok | ||||||||||||||||||||
| 239 | 0 | return "[[:$cmd:]]"; | |||||||||||||||||||
| 240 | } | ||||||||||||||||||||
| 241 | } | ||||||||||||||||||||
| 242 | 0 | ▶ | if ( $prefix =~ /[_[:alnum:]]/ ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 243 | 0 | ▶ | if ( $cmd eq 'null' ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 244 | 0 | return "" | |||||||||||||||||||
| 245 | } | ||||||||||||||||||||
| 246 | } | ||||||||||||||||||||
| 247 | 0 | die "<$cmd> not implemented"; | |||||||||||||||||||
| 248 | } | ||||||||||||||||||||
| 249 | |||||||||||||||||||||
| 250 | 1 | 1; | |||||||||||||||||||