blib/lib/Pugs/Compiler/Regex.pm

filter: Statementstmt 69.6% 85 / 122 Branchbran 39.1% 18 / 46 Conditioncond 51.2% 20 / 39 MC/DCmcdc 15.0% 3 / 20 Subroutinesub 62.5% 15 / 24 Podpod 87.5% 7 / 8 totaltotal 57.1% 148 / 259 CC 40 SCAR 33.8
CC
40
Cov
59%
compile
62.7
match
26.4
_install
19.5
CRAP 29.4
« blib/lib/Pugs/Grammar/Precedence.pm ↑ Summary blib/lib/Pugs/Emitter/Rule/Perl5/Regex.pm »
1 package Pugs::Compiler::Regex;
2  
3 #use Smart::Comments;
4  
5 17 our $VERSION = '0.28';
6 # Documentation in the __END__
7 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 17 ▶use Pugs::Grammar::Rule;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 17 ▶use Pugs::Compiler::RegexPerl5;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 17 ▶use Pugs::Emitter::Rule::Perl5;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 17 ▶use Pugs::Emitter::Rule::Perl5::Ratchet;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 17 ▶use Pugs::Runtime::Regex;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 # complete the dependency circularity
19 17 push @Pugs::Grammar::Rule::ISA, 'Pugs::Grammar::Base';
20  
21 17 ▶use Carp qw( croak carp );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 17 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 17 ▶use Symbol 'qualify_to_ref';
Subroutine BEGIN: called CC=1 SCAR=0.0
24 17 ▶use Digest::MD5 'md5_hex';
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 17 our $NoCache = $ENV{PCR_NO_CACHE}; # Depresses any caching if set to true
27  
28 17 my $cache;
29 17 eval {
30 17 require Cache::FileCache;
31 17 $cache = new Cache::FileCache( { 'namespace' => 'v6-rules' } );
32 };
33  
34 0 ▶sub new { $_[0] }
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
35  
36 15 ▶sub compile {
Subroutine compile: called CC=21 SCAR=41.4
Pod documented
37 15 local $::_V6_MATCH_; # avoid messing with global $/
38  
39 # $class->compile( $source )
40 # $class->compile( $source, { p=>1 } )
41 # $class->compile( $source, { signature => $sig } ) -- TODO
42  
43 15 my ( $class, $rule_source, $param ) = @_;
44  
45 return Pugs::Compiler::RegexPerl5->compile( $rule_source, $param )
46 15 ▶ if exists $param->{P5} || exists $param->{Perl5};
Branch: 1 branch
BranchTrueFalseTotal
if exists $param->{"P5"} or exists $param->{"Perl5"} 2 13 15
Condition66%2/3
exists $param->{"P5"} or exists $param->{"Perl5"}
l!l&&r!l&&!r
0213
MC/DC50%1/2
exists $param->{"P5"} or exists $param->{"Perl5"}
exists $param->{"P5"} exists $param->{"Perl5"}
Truth table66%2/3
A or B
A= exists $param->{"P5"}B= exists $param->{"Perl5"}
ABresult
1X1
011
000
47 #warn length($rule_source);
48  
49 13 my $self = { source => $rule_source };
50  
51 #print Dumper @_;
52  
53 # XXX - should use user's lexical pad instead of an explicit grammar?
54 $self->{grammar} = delete $param->{grammar} ||
55 13 ▶ 'Pugs::Grammar::Base';
Condition50%1/2
delete $param->{'grammar'} || 'Pugs::Grammar::Base'
l!l
013
MC/DC0%0/1
delete $param->{'grammar'} || 'Pugs::Grammar::Base'
delete $param->{'grammar'}
Truth table50%1/2
A
A= delete $param->{'grammar'}
Aresult
11
00
56 $self->{ratchet} = delete $param->{ratchet} ||
57 13 ▶ 0;
Condition100%2/2
delete $param->{'ratchet'} || 0
l!l
49
MC/DC100%1/1
delete $param->{'ratchet'} || 0
delete $param->{'ratchet'}
Truth table100%2/2
A
A= delete $param->{'ratchet'}
Aresult
11
00
58  
59 $self->{p} = delete $param->{pos} ||
60 13 ▶ delete $param->{p};
Condition33%1/3
delete $param->{'pos'} || delete $param->{'p'}
l!l&&r!l&&!r
0013
MC/DC0%0/2
delete $param->{'pos'} || delete $param->{'p'}
delete $param->{'pos'} delete $param->{'p'}
Truth table33%1/3
A || B
A= delete $param->{'pos'}B= delete $param->{'p'}
ABresult
1X1
011
000
61 # default = undef;
62 13 delete $param->{p};
63  
64 $self->{sigspace} = delete $param->{sigspace} ||
65 delete $param->{s} ||
66 13 ▶ 0;
Condition80%4/5
delete $param->{'sigspace'} || delete $param->{'s'} || 0
l!l
211
delete $param->{"sigspace"} or delete $param->{"s"}
l!l&&r!l&&!r
2011
MC/DC50%1/2
delete $param->{'sigspace'} || delete $param->{'s'} || 0
delete $param->{"sigspace"} delete $param->{"s"}
Truth table66%2/3
A or B
A= delete $param->{"sigspace"}B= delete $param->{"s"}
ABresult
1X1
011
000
67 13 delete $param->{s};
68  
69 $self->{continue} = delete $param->{continue} ||
70 delete $param->{c} ||
71 13 ▶ 0;
Condition40%2/5
delete $param->{'continue'} || delete $param->{'c'} || 0
l!l
013
delete $param->{"continue"} or delete $param->{"c"}
l!l&&r!l&&!r
0013
MC/DC0%0/2
delete $param->{'continue'} || delete $param->{'c'} || 0
delete $param->{"continue"} delete $param->{"c"}
Truth table33%1/3
A or B
A= delete $param->{"continue"}B= delete $param->{"c"}
ABresult
1X1
011
000
72 13 delete $param->{c};
73  
74 $self->{ignorecase} = delete $param->{ignorecase} ||
75 delete $param->{i} ||
76 13 ▶ 0;
Condition40%2/5
delete $param->{'ignorecase'} || delete $param->{'i'} || 0
l!l
013
delete $param->{"ignorecase"} or delete $param->{"i"}
l!l&&r!l&&!r
0013
MC/DC0%0/2
delete $param->{'ignorecase'} || delete $param->{'i'} || 0
delete $param->{"ignorecase"} delete $param->{"i"}
Truth table33%1/3
A or B
A= delete $param->{"ignorecase"}B= delete $param->{"i"}
ABresult
1X1
011
000
77 13 delete $param->{i};
78  
79 13 my $error;
80 $error .= "Error in rule: unknown parameter '$_'"
81 13 for keys %$param;
82 13 ▶ croak $error if %$param;
Branch: 1 branch
BranchTrueFalseTotal
if %$param 0 13 13
83  
84 13 my $digest = md5_hex(Dumper($self));
85 13 my $cached;
86  
87 13 ▶ if (!$NoCache && $cache && ($cached = $cache->get($digest))) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $Pugs::Compiler::Regex::NoCache and $cache and $cached = $cache->get($digest)) { } 0 13 13
Condition66%4/6
not $Pugs::Compiler::Regex::NoCache and $cache and $cached = $cache->get($digest)
!ll&&!rl&&r
490
not $Pugs::Compiler::Regex::NoCache and $cache
!ll&&!rl&&r
409
MC/DC0%0/3
not $Pugs::Compiler::Regex::NoCache and $cache and $cached = $cache->get($digest)
not $Pugs::Compiler::Regex::NoCache $cache $cached = $cache->get($digest)
Truth table50%2/4
not(A) and B and C
A= not $Pugs::Compiler::Regex::NoCacheB= $cacheC= $cached = $cache->get($digest)
ABCresult
0XX0
10X0
1100
1111
88 ### using cached rule...
89 0 $self->{perl5} = $cached;
90 }
91 else {
92 ### compiling rule...
93  
94 #print 'rule source: ', $self->{source}, "\n";
95 #print "match: ", Dumper( Pugs::Grammar::Rule->rule( $self->{source} ) );
96 my $ast = Pugs::Grammar::Rule->rule(
97 13 $self->{source} )->();
98 0 ▶ if (!defined $ast) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ast) 0 0 0
99 0 carp "Invalid regex syntax";
100 0 return undef;
101 }
102 ### rule AST: $ast
103  
104 # save the ast for debugging
105 0 $self->{ast} = $ast;
106  
107 #warn "ast: ",Dumper($ast),"\n";
108 #die "Error in rule: '$rule_source' at: '$ast->tail'\n" if $ast->tail;
109 #print 'rule ast: ', do{use Data::Dumper; Dumper($ast{capture})};
110  
111 #use Pugs::Emitter::Rule::Perl5::Preprocess;
112 #my $ast2 = Pugs::Emitter::Rule::Perl5::Preprocess::emit(
113 # $self->{grammar}, $ast, $self );
114  
115 0 ▶ if ( $self->{ratchet} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'ratchet'}) { } 0 0 0
116 $self->{perl5} = Pugs::Emitter::Rule::Perl5::Ratchet::emit(
117 0 $self->{grammar}, $ast, $self );
118 #print "token: ", $self->{perl5};
119 }
120 else {
121 $self->{perl5} = Pugs::Emitter::Rule::Perl5::emit(
122 0 $self->{grammar}, $ast, $self );
123 }
124 #print 'rule perl5: ', do{use Data::Dumper; Dumper($self->{perl5})};
125  
126 0 ▶ $cache->set($digest, $self->{perl5}, 'never') if $cache;
Branch: 1 branch
BranchTrueFalseTotal
if $cache 0 0 0
127 }
128  
129 #our $evals++;
130  
131 0 local $@;
132 $self->{code} = eval
133 # "\#line " . ($evals*1000) . "\n" .
134 0 $self->{perl5};
135 0 ▶ die "Error in evaluation: $@\nSource:\n$self->{perl5}\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
136  
137 #my $code = $self->{code};
138 #my $e = $evals;
139 #my $c = $self->{perl5};
140 #my $x = 1;
141 #$c =~ s/\n/"\n".++$x.": "/seg;
142 #$self->{code} = sub { print "calling #$e <<< $rule_source >>> compiles to <<< $c >>>\n"; $code->(@_); };
143  
144 0 bless $self, $class;
145 }
146  
147 156 ▶sub code {
Subroutine code: called CC=1 SCAR=0.4
Pod documented
148 156 my $rule = shift;
149 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
150 0 $rule->match( $_[1], $_[0], $_[2], $_[3] );
151 }
152 156 }
153  
154 10 ▶sub match {
Subroutine match: called CC=15 SCAR=32.7
Pod documented
155 10 my ( $rule, $str, $grammar, $flags, $state ) = @_;
156  
157 #print "match: ",Dumper($rule);
158 #print "match: ",Dumper(\@_);
159 #print "PCR::match: ",Dumper($_[2]);
160  
161 10 ▶ return Pugs::Runtime::Match->new( { bool => \0 } )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str 0 10 10
162 unless defined $str; # XXX - fix?
163  
164 10 ▶ if ( ref $grammar eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $grammar eq "HASH") 7 3 10
165 # backwards compatibility - grammar can now be specified in $flags
166 7 $state = $flags;
167 7 $flags = $grammar;
168 7 $grammar = $flags->{grammar};
169 }
170  
171 10 ▶ $grammar ||= $rule->{grammar};
Condition33%1/3
$grammar ||= $rule->{"grammar"}
l!l&&r!l&&!r
0010
MC/DC0%0/2
$grammar ||= $rule->{"grammar"}
$grammar $rule->{"grammar"}
Truth table33%1/3
A ||= B
A= $grammarB= $rule->{"grammar"}
ABresult
1X1
011
000
172 #print "match: grammar $rule->{grammar}, $_[0], $flags\n";
173 #print "match: Variables: ", Dumper ( $flags->{args} ) if defined $flags->{args};
174 #print "match: Flags: ", Dumper ( $flags ) if defined $flags;
175  
176 my $p = defined $flags->{p}
177 ? $flags->{p}
178 : defined $flags->{pos}
179 ? $flags->{pos}
180 10 ▶ : $rule->{p};
Branch: 2 branches
BranchTrueFalseTotal
defined $flags->{'p'} ? : 1 9 10
defined $flags->{'pos'} ? : 2 7 9
181  
182 my $continue = defined $flags->{c}
183 ? $flags->{c}
184 : defined $flags->{continue}
185 ? $flags->{continue}
186 10 ▶ : $rule->{continue};
Branch: 2 branches
BranchTrueFalseTotal
defined $flags->{'c'} ? : 0 10 10
defined $flags->{'continue'} ? : 4 6 10
187  
188 my $ignorecase = defined $flags->{i}
189 ? $flags->{i}
190 : defined $flags->{ignorecase}
191 ? $flags->{ignorecase}
192 10 ▶ : $rule->{ignorecase};
Branch: 2 branches
BranchTrueFalseTotal
defined $flags->{'i'} ? : 0 10 10
defined $flags->{'ignorecase'} ? : 0 10 10
193  
194 #print "flag p";
195 #print "match: grammar $rule->{grammar}, $str, %$flags\n";
196 #print $rule->{code};
197  
198 # XXX BUG! - $rule->{code} disappeared - in t/08-hash.t ???
199 10 ▶ unless ( defined $rule->{code} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rule->{"code"}) 0 10 10
200 0 local $@;
201 0 ▶ if (!defined $rule->{perl5}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rule->{"perl5"}) 0 0 0
202 0 croak "Error in evaluation: \$rule->{perl5} is missing";
203 }
204 $rule->{code} = eval
205 0 $rule->{perl5};
206 0 ▶ croak "Error in evaluation: $@\nSource:\n$rule->{perl5}" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
207 }
208  
209 10 my %args;
210 0 ▶ %args = %{$flags->{args}} if defined $flags && defined $flags->{args};
Branch: 1 branch
BranchTrueFalseTotal
if defined $flags and defined $flags->{"args"} 0 10 10
Condition33%1/3
defined $flags and defined $flags->{"args"}
!ll&&!rl&&r
0100
MC/DC0%0/2
defined $flags and defined $flags->{"args"}
defined $flags defined $flags->{"args"}
Truth table33%1/3
A and B
A= defined $flagsB= defined $flags->{"args"}
ABresult
0X0
100
111
211 10 $args{p} = $p;
212 10 $args{continue} = $continue;
213 10 $args{ignorecase} = $ignorecase;
214  
215 #print "calling code with ",Dumper([ $grammar,$str, $state,\%args ] );
216 my $match = $rule->{code}(
217 10 $grammar,
218 $_[1], # pass $str itself instead of a copy so as to make pos work
219 $state,
220 \%args,
221 );
222 #print __PACKAGE__ . ": match result: ", $match->perl;
223 10 return $match;
224 }
225  
226 0 ▶sub reinstall {
Subroutine reinstall: not called CC=1 SCAR=6.9
Pod documented
227 0 _install(0, scalar(caller), @_);
228 }
229  
230 0 ▶sub install {
Subroutine install: not called CC=1 SCAR=6.9
Pod documented
231 0 _install(1, scalar(caller), @_);
232 }
233  
234 0 ▶sub _install {
Subroutine _install: not called CC=5 SCAR=29.7
235 0 my($check, $caller, $class, $name, @etc) = @_;
236  
237 # If we have a fully qualified name, use that, otherwise extrapolate.
238 0 ▶ my $rule = index($name, '::') > -1 ? $name : $caller."::$name";
Branch: 1 branch
BranchTrueFalseTotal
index($name, '::') != -1 ? : 0 0 0
239 0 my $slot = qualify_to_ref($rule);
240  
241 croak "Can't install regex '$name' as '$rule' which already exists"
242 0 ▶ if $check && *$slot{CODE};
Branch: 1 branch
BranchTrueFalseTotal
if $check and *$slot{"CODE"} 0 0 0
Condition0%0/2
$check and *$slot{"CODE"}
!ll
00
MC/DC0%0/1
$check and *$slot{"CODE"}
$check
Truth table0%0/2
A
A= $check
Aresult
00
11
243  
244 0 eval {
245 17 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
246 0 *$slot = $class->compile(@etc)->code;
247 };
248 0 ▶ warn $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
249 }
250  
251 0 ▶sub _str { defined $_[0] ? $_[0] : 'undef' }
Subroutine _str: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 0 0 0
252 0 ▶sub _quot {
Subroutine _quot: not called CC=1 SCAR=6.9
253 0 my $s = $_[0];
254 0 $s =~ s/\\/\\\\/sg;
255 0 return $s;
256 }
257  
258 0 ▶sub perl5 {
Subroutine perl5: not called CC=1 SCAR=6.9
Pod documented
259 0 my $self = shift;
260 return "bless {\n" .
261 " grammar " . "=> q(" . _str( $self->{grammar} ) . "),\n" .
262 " ratchet " . "=> q(" . _str( $self->{ratchet} ) . "),\n" .
263 " p " . "=> " . _str( $self->{p} ) . ",\n" .
264 " sigspace " . "=> q(" . _str( $self->{sigspace} ) . "),\n" .
265 " ignorecase ". "=> q(" . _str( $self->{ignorecase} )."),\n" .
266 " code " . "=> " . $self->{perl5} . ",\n" .
267 0 " perl5 " . "=> q(" . _quot( $self->{perl5} ) . "), }, " .
268 "q(" . ref($self) . ")";
269 }
270  
271 0 ▶sub perl { perl5(@_) }
Subroutine perl: not called CC=1 SCAR=6.9
Pod documented
272  
273 17 1;
274  
275 __END__
« blib/lib/Pugs/Grammar/Precedence.pm ↑ Summary blib/lib/Pugs/Emitter/Rule/Perl5/Regex.pm »