Circle/GlobalRules.pm

filter: Statementstmt 19.2% 43 / 223 Branchbran 0.0% 0 / 118 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 38.7% 12 / 31 Podpod 0.0% 0 / 20 totaltotal 13.8% 55 / 397 CC 72 SCAR 39.3
CC
72
Cov
12%
parse_action_rewrite
110.0
eval_action_rewrite
90.0
parse_action_format
90.0
CRAP 50.8
« Circle/Ruleable.pm ↑ Summary Circle/WindowItem.pm »
1 # You may distribute under the terms of the GNU General Public License
2 #
3 # (C) Paul Evans, 2008-2017 -- leonerd@leonerd.org.uk
4  
5 package Circle::GlobalRules;
6  
7 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 4 our $VERSION = '0.173320';
11  
12 4 ▶use Text::Balanced qw( extract_delimited extract_quotelike );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 4 ▶use base qw( Circle::Rule::Store ); # for the attributes
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 4 ▶use Circle::TaggedString;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 sub unquote_qr
19 0 ▶{
Subroutine unquote_qr: not called CC=3 SCAR=24.8
Pod undocumented
20 0 my $re = shift;
21  
22 0 $re = "$re";
23  
24 # Perl tries to put (?-xism:RE) around our pattern. Lets attempt to remove
25 # it if we can
26 # Recent perls use (?^:RE) instead
27 0 $re =~ s/^\(\?-xism:(.*)\)$/$1/;
28 0 $re =~ s/^\(\?\^:(.*)\)$/$1/;
29  
30 0 ▶ return ( $2, $1 ) if $re =~ m/^\(\?([ixsm]*)-?[xism]*:(.*)\)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $re =~ /^\(\?([ixsm]*)-?[xism]*:(.*)\)$/ 0 0 0
31 0 ▶ return ( $2, $1 ) if $re =~ m/^\(\?\^([ixsm]*):(.*)\)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $re =~ /^\(\?\^([ixsm]*):(.*)\)$/ 0 0 0
32  
33 # Failed. Lets just be safe then
34 0 return ( $re, "" );
35 }
36  
37 # Not an object class. Instead, just a store of rule subs
38  
39 sub register
40 3 ▶{
Subroutine register: called CC=1 SCAR=0.0
Pod undocumented
41 3 my ( $rulestore ) = @_;
42  
43 3 $rulestore->register_cond( matches => __PACKAGE__ );
44  
45 3 $rulestore->register_action( rewrite => __PACKAGE__ );
46 3 $rulestore->register_action( format => __PACKAGE__ );
47 3 $rulestore->register_action( unformat => __PACKAGE__ );
48 3 $rulestore->register_action( level => __PACKAGE__ );
49 3 $rulestore->register_action( highlight => __PACKAGE__ );
50 }
51  
52 ###### CONDITIONS
53  
54 ### MATCHES
55  
56 sub parse_cond_matches
57 0 ▶ : Rule_description("Look for regexp or substring matches in the text")
Subroutine parse_cond_matches: not called CC=6 SCAR=37.4
Pod undocumented
58 : Rule_format('/regexp/ or "literal"')
59 {
60 0 shift; # class
61 0 my ( $spec ) = @_;
62  
63 0 ▶ if( $spec =~ m{^/} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($spec =~ m[^/]) { } 0 0 0
elsif ($spec =~ /^"/) { } 0 0 0
64 # Try to pull the flags
65 0 ▶ my ( $content, $flags ) = $spec =~ m{^/(.*)/([i]*)$} or die "Unrecognised regexp string $spec\n";
Branch: 1 branch
BranchTrueFalseTotal
() = $spec =~ m[^/(.*)/([i]*)$] or die "Unrecognised regexp string $spec\n" 0 0 0
66  
67 0 ▶ return qr/$content/i if $flags eq "i";
Branch: 1 branch
BranchTrueFalseTotal
if $flags eq "i" 0 0 0
68 0 return qr/$content/;
69 }
70 elsif( $spec =~ m{^"} ) {
71 0 ▶ my ( $content ) = $spec =~ m{^"(.*)"$} or die "Unrecognised literal string $spec\n";
Branch: 1 branch
BranchTrueFalseTotal
my($content) = $spec =~ /^"(.*)"$/ or die "Unrecognised literal string $spec\n" 0 0 0
72  
73 0 return qr/\Q$content/;
74 }
75 else {
76 0 die "Unrecognised string type $spec";
77 }
78 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
79  
80 sub deparse_cond_matches
81 0 ▶{
Subroutine deparse_cond_matches: not called CC=1 SCAR=6.9
Pod undocumented
82 0 shift; # class
83 0 my ( $re ) = @_;
84  
85 0 my ( $pattern, $flags ) = unquote_qr( $re );
86 0 return "/$pattern/$flags";
87 }
88  
89 sub eval_cond_matches
90 0 ▶{
Subroutine eval_cond_matches: not called CC=4 SCAR=30.0
Pod undocumented
91 0 shift; # class
92 0 my ( $event, $results, $re ) = @_;
93  
94 0 ▶ defined( my $text = $event->{text} ) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
defined(my $text = $event->{"text"}) or return 0 0 0 0
95 0 $text = "$text"; # stringify a String::Tagged
96  
97 0 pos( $text ) = 0;
98  
99 0 my $matched;
100  
101 0 ▶ while( $text =~ m/$re/g ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /$re/g) 0 0 0
102 0 my @matchgroups;
103 0 for ( 0 .. $#+ ) {
104 0 my ( $start, $end ) = ( $-[$_], $+[$_] );
105 0 my $len = $end - $start;
106  
107 0 push @matchgroups, [ $start, $len ];
108 }
109  
110 0 $results->push_result( "matchgroups", \@matchgroups );
111 0 $matched = 1;
112 }
113  
114 0 return $matched;
115 }
116  
117 ###### ACTIONS
118  
119 ### REWRITE
120  
121 sub parse_action_rewrite
122 0 ▶ : Rule_description("Rewrite text of the line or matched parts")
Subroutine parse_action_rewrite: not called CC=10 SCAR=47.0
Pod undocumented
123 : Rule_format('line|matches|match(number) "string"|s/pattern/replacement/')
124 {
125 0 shift; # class
126 0 my ( $spec ) = @_;
127  
128 0 ▶ $spec =~ s/^(\w+)\s*// or die "Expected type as first argument\n";
Branch: 1 branch
BranchTrueFalseTotal
$spec =~ s/^(\w+)\s*// or die "Expected type as first argument\n" 0 0 0
129 0 my $type = $1;
130  
131 0 my $groupnum;
132  
133 0 ▶ if( $type eq "line" ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'line') { } 0 0 0
elsif ($type eq 'matches') { } 0 0 0
elsif ($type eq 'match') { } 0 0 0
134 0 $groupnum = -1;
135 }
136 elsif( $type eq "matches" ) {
137 0 $groupnum = 0;
138 }
139 elsif( $type eq "match" ) {
140 0 ▶ $spec =~ s/^\((\d+)\)\s*// or die "Expected match group number\n";
Branch: 1 branch
BranchTrueFalseTotal
$spec =~ s/^\((\d+)\)\s*// or die "Expected match group number\n" 0 0 0
141 0 $groupnum = $1;
142 }
143 else {
144 0 die "Unrecognised format type $type\n";
145 }
146  
147 0 ▶ my ( undef, $remains, undef, $op, $delim, $lhs, undef, undef, $rhs, undef, $mods ) = extract_quotelike( $spec )
Branch: 1 branch
BranchTrueFalseTotal
my(undef, $remains, undef, $op, $delim, $lhs, undef, undef, $rhs, undef, $mods) = &extract_quotelike($spec) or die "Expected \"string\" or s/pattern/replacement/" 0 0 0
148 or die 'Expected "string" or s/pattern/replacement/';
149 0 $spec = $remains;
150 0 ▶ $op = $delim if $op eq "";
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "" 0 0 0
151  
152 0 ▶ if( $op eq '"' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($op eq '"') { } 0 0 0
elsif ($op eq 's') { } 0 0 0
153 # Literal
154 0 return ( $groupnum, literal => $lhs );
155 }
156 elsif( $op eq "s" ) {
157 # s/foo/bar/
158 0 my $global = $mods =~ s/g//;
159 # TODO: Range check that mods contains only /ism
160 0 return ( $groupnum, subst => qr/(?$mods:$lhs)/, $rhs, $global );
161 }
162 else {
163 0 die 'Expected "string" or s/pattern/replacement/';
164 }
165 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
166  
167 sub deparse_action_rewrite
168 0 ▶{
Subroutine deparse_action_rewrite: not called CC=6 SCAR=37.4
Pod undocumented
169 0 shift; # class
170 0 my ( $groupnum, $kind, $lhs, $rhs, $global ) = @_;
171  
172 0 ▶ my $type = $groupnum == -1 ? "line" :
Branch: 2 branches
BranchTrueFalseTotal
$groupnum == -1 ? : 0 0 0
$groupnum == 0 ? : 0 0 0
173 $groupnum == 0 ? "matches" :
174 "match($groupnum)";
175  
176 0 ▶ if( $kind eq "literal" ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($kind eq 'literal') { } 0 0 0
elsif ($kind eq 'subst') { } 0 0 0
177 0 return "$type \"$lhs\"";
178 }
179 elsif( $kind eq "subst" ) {
180 0 my ( $pattern, $flags ) = unquote_qr( $lhs );
181 0 ▶ return "$type s/$pattern/$rhs/$flags" . ( $global ? "g" : "" );
Branch: 1 branch
BranchTrueFalseTotal
$global ? : 0 0 0
182 }
183 }
184  
185 sub eval_action_rewrite
186 0 ▶{
Subroutine eval_action_rewrite: not called CC=9 SCAR=45.0
Pod undocumented
187 0 shift; # class
188 0 my ( $event, $results, $groupnum, $kind, $lhs, $rhs, $global ) = @_;
189  
190 0 my @location;
191 0 ▶ if( $groupnum == -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($groupnum == -1) { } 0 0 0
192 0 @location = ( 0, -1 );
193 }
194 else {
195 0 foreach my $groups ( @{ $results->get_result( "matchgroups" ) } ) {
196 0 ▶ my $group = $groups->[$groupnum] or next;
Branch: 1 branch
BranchTrueFalseTotal
my $group = $groups->[$groupnum] or next 0 0 0
197 0 @location = @$group;
198 0 last; # can only do the first one
199 }
200 }
201  
202 0 ▶ ref $event->{text} or $event->{text} = Circle::TaggedString->new( $event->{text} );
Branch: 1 branch
BranchTrueFalseTotal
ref $event->{"text"} or $event->{"text"} = "Circle::TaggedString"->new($event->{"text"}) 0 0 0
203 0 my $text = $event->{text}->substr( $location[0], $location[1] );
204  
205 0 ▶ if( $kind eq "literal" ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($kind eq 'literal') { } 0 0 0
elsif ($kind eq 'subst') { } 0 0 0
206 0 $text = $lhs;
207 }
208 elsif( $kind eq "subst" ) {
209 0 ▶ $text =~ s/$lhs/$rhs/ if !$global;
Branch: 1 branch
BranchTrueFalseTotal
unless $global 0 0 0
210 0 ▶ $text =~ s/$lhs/$rhs/g if $global;
Branch: 1 branch
BranchTrueFalseTotal
if $global 0 0 0
211 }
212  
213 0 $event->{text}->set_substr( $location[0], $location[1], $text );
214 }
215  
216 ### FORMAT
217  
218 sub parse_action_format
219 0 ▶ : Rule_description("Apply formatting to the line or matched parts")
Subroutine parse_action_format: not called CC=9 SCAR=45.0
Pod undocumented
220 : Rule_format('line|matches|match(number) key="value" [key="value" ...]')
221 {
222 0 shift; # class
223 0 my ( $spec ) = @_;
224  
225 0 ▶ $spec =~ s/^(\w+)\s*// or die "Expected type as first argument\n";
Branch: 1 branch
BranchTrueFalseTotal
$spec =~ s/^(\w+)\s*// or die "Expected type as first argument\n" 0 0 0
226 0 my $type = $1;
227  
228 0 my $groupnum;
229  
230 0 ▶ if( $type eq "line" ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'line') { } 0 0 0
elsif ($type eq 'matches') { } 0 0 0
elsif ($type eq 'match') { } 0 0 0
231 0 $groupnum = -1;
232 }
233 elsif( $type eq "matches" ) {
234 0 $groupnum = 0;
235 }
236 elsif( $type eq "match" ) {
237 0 ▶ $spec =~ s/^\((\d+)\)\s*// or die "Expected match group number\n";
Branch: 1 branch
BranchTrueFalseTotal
$spec =~ s/^\((\d+)\)\s*// or die "Expected match group number\n" 0 0 0
238 0 $groupnum = $1;
239 }
240 else {
241 0 die "Unrecognised format type $type\n";
242 }
243  
244 0 my %format;
245 0 ▶ while( $spec =~ s/^(\w+)=// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec =~ s/^(\w+)=//) 0 0 0
246 0 my $name = $1;
247  
248 0 my $value = extract_delimited( $spec, q{"'} );
249 0 s/^["']//, s/["']$// for $value;
250  
251 0 $format{$name} = $value;
252  
253 0 $spec =~ s/^\s+//;
254 }
255  
256 0 ▶ if( length $spec ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $spec) 0 0 0
257 0 die "Unrecognised format spec $spec\n";
258 }
259  
260 0 return ( $groupnum, \%format );
261 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
262  
263 sub deparse_action_format
264 0 ▶{
Subroutine deparse_action_format: not called CC=4 SCAR=30.0
Pod undocumented
265 0 shift; # class
266 0 my ( $groupnum, $formathash ) = @_;
267  
268 0 ▶ return unless %$formathash;
Branch: 1 branch
BranchTrueFalseTotal
unless %$formathash 0 0 0
269  
270 0 ▶ my $type = $groupnum == -1 ? "line" :
Branch: 2 branches
BranchTrueFalseTotal
$groupnum == -1 ? : 0 0 0
$groupnum == 0 ? : 0 0 0
271 $groupnum == 0 ? "matches" :
272 "match($groupnum)";
273  
274 0 return "$type ".join( " ", map { qq($_="$formathash->{$_}") } sort keys %$formathash );
275 }
276  
277 sub eval_action_format
278 0 ▶{
Subroutine eval_action_format: not called CC=7 SCAR=40.3
Pod undocumented
279 0 shift; # class
280 0 my ( $event, $results, $groupnum, $formathash ) = @_;
281  
282 0 my $str = $event->{text};
283 0 ▶ ref $str or $str = Circle::TaggedString->new( $str );
Branch: 1 branch
BranchTrueFalseTotal
ref $str or $str = "Circle::TaggedString"->new($str) 0 0 0
284  
285 0 ▶ if( $groupnum == -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($groupnum == -1) { } 0 0 0
286 0 $str->apply_tag( 0, -1, $_, $formathash->{$_} ) for keys %$formathash;
287 }
288 else {
289 0 foreach my $groups ( @{ $results->get_result( "matchgroups" ) } ) {
290 0 ▶ my $group = $groups->[$groupnum] or next;
Branch: 1 branch
BranchTrueFalseTotal
my $group = $groups->[$groupnum] or next 0 0 0
291 0 my ( $start, $len ) = @$group;
292  
293 0 $str->apply_tag( $start, $len, $_, $formathash->{$_} ) for keys %$formathash;
294 }
295 }
296 }
297  
298 ### UNFORMAT
299  
300 sub parse_action_unformat
301 0 ▶ : Rule_description("Remove formatting from the line or matched parts")
Subroutine parse_action_unformat: not called CC=8 SCAR=42.8
Pod undocumented
302 : Rule_format('line|matches|match(number) key [key ...]')
303 {
304 0 shift; # class
305 0 my ( $spec ) = @_;
306  
307 0 ▶ $spec =~ s/^(\w+)\s*// or die "Expected type as first argument\n";
Branch: 1 branch
BranchTrueFalseTotal
$spec =~ s/^(\w+)\s*// or die "Expected type as first argument\n" 0 0 0
308 0 my $type = $1;
309  
310 0 my $groupnum;
311  
312 0 ▶ if( $type eq "line" ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'line') { } 0 0 0
elsif ($type eq 'matches') { } 0 0 0
elsif ($type eq 'match') { } 0 0 0
313 0 $groupnum = -1;
314 }
315 elsif( $type eq "matches" ) {
316 0 $groupnum = 0;
317 }
318 elsif( $type eq "match" ) {
319 0 ▶ $spec =~ s/^\((\d+)\)\s*// or die "Expected match group number\n";
Branch: 1 branch
BranchTrueFalseTotal
$spec =~ s/^\((\d+)\)\s*// or die "Expected match group number\n" 0 0 0
320 0 $groupnum = $1;
321 }
322 else {
323 0 die "Unrecognised format type $type\n";
324 }
325  
326 0 my @tags;
327 0 ▶ while( $spec =~ s/^(\w+)// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec =~ s/^(\w+)//) 0 0 0
328 0 my $name = $1;
329  
330 0 push @tags, $name;
331  
332 0 $spec =~ s/^\s+//;
333 }
334  
335 0 ▶ if( length $spec ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $spec) 0 0 0
336 0 die "Unrecognised format spec $spec\n";
337 }
338  
339 0 return ( $groupnum, \@tags );
340 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
341  
342 sub deparse_action_unformat
343 0 ▶{
Subroutine deparse_action_unformat: not called CC=4 SCAR=30.0
Pod undocumented
344 0 shift; # class
345 0 my ( $groupnum, $taglist ) = @_;
346  
347 0 ▶ my $type = $groupnum == -1 ? "line" :
Branch: 2 branches
BranchTrueFalseTotal
$groupnum == -1 ? : 0 0 0
$groupnum == 0 ? : 0 0 0
348 $groupnum == 0 ? "matches" :
349 "match($groupnum)";
350  
351 0 my $ret = $type;
352 0 $ret .= " $_" for @$taglist;
353  
354 0 return $ret;
355 }
356  
357 4 my @alltags = qw( fg bg b u i );
358  
359 sub eval_action_unformat
360 0 ▶{
Subroutine eval_action_unformat: not called CC=8 SCAR=42.8
Pod undocumented
361 0 shift; # class
362 0 my ( $event, $results, $groupnum, $taglist ) = @_;
363  
364 0 ▶ $taglist = \@alltags unless @$taglist;
Branch: 1 branch
BranchTrueFalseTotal
unless @$taglist 0 0 0
365  
366 0 my $str = $event->{text};
367 0 ▶ ref $str or $str = Circle::TaggedString->new( $str );
Branch: 1 branch
BranchTrueFalseTotal
ref $str or $str = "Circle::TaggedString"->new($str) 0 0 0
368  
369 0 ▶ if( $groupnum == -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($groupnum == -1) { } 0 0 0
370 0 $str->unapply_tag( 0, -1, $_ ) for @$taglist;
371 }
372 else {
373 0 foreach my $groups ( @{ $results->get_result( "matchgroups" ) } ) {
374 0 ▶ my $group = $groups->[$groupnum] or next;
Branch: 1 branch
BranchTrueFalseTotal
my $group = $groups->[$groupnum] or next 0 0 0
375 0 my ( $start, $len ) = @$group;
376  
377 0 $str->unapply_tag( $start, $len, $_ ) for @$taglist;
378 }
379 }
380 }
381  
382 ### LEVEL
383  
384 sub parse_action_level
385 0 ▶ : Rule_description("Set the activity level for the targetted item")
Subroutine parse_action_level: not called CC=4 SCAR=30.0
Pod undocumented
386 : Rule_format('$level')
387 {
388 0 shift; # class
389 0 my ( $spec ) = @_;
390  
391 0 ▶ $spec =~ s/^(\d)// or die "Expected level number as first argument\n";
Branch: 1 branch
BranchTrueFalseTotal
$spec =~ s/^(\d)// or die "Expected level number as first argument\n" 0 0 0
392 0 my $level = $1;
393  
394 0 ▶ $level >= 0 and $level <= 3 or die "Expected 'level' between 0 and 3\n";
Branch: 1 branch
BranchTrueFalseTotal
$level >= 0 and $level <= 3 or die "Expected 'level' between 0 and 3\n" 0 0 0
Condition0%0/3
$level >= 0 and $level <= 3
!ll&&!rl&&r
000
MC/DC0%0/2
$level >= 0 and $level <= 3
$level >= 0 $level <= 3
Truth table0%0/3
A and B
A= $level >= 0B= $level <= 3
ABresult
0X0
100
111
395  
396 0 return ( $level );
397 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
398  
399 sub deparse_action_level
400 0 ▶{
Subroutine deparse_action_level: not called CC=1 SCAR=6.9
Pod undocumented
401 0 shift; # class
402 0 my ( $level ) = @_;
403  
404 0 return "$level";
405 }
406  
407 sub eval_action_level
408 0 ▶{
Subroutine eval_action_level: not called CC=1 SCAR=6.9
Pod undocumented
409 0 shift; # class
410 0 my ( $event, $results, $level ) = @_;
411  
412 0 $event->{level} = $level;
413 }
414  
415 ## HIGHLIGHT
416  
417 sub parse_action_highlight
418 0 ▶ : Rule_description("Highlight matched regions and set activity level to 3")
Subroutine parse_action_highlight: not called CC=1 SCAR=6.9
Pod undocumented
419 : Rule_format('')
420 {
421 0 my $self = shift;
422 0 return;
423 4 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
424  
425 sub deparse_action_highlight
426 0 ▶{
Subroutine deparse_action_highlight: not called CC=1 SCAR=6.9
Pod undocumented
427 0 my $self = shift;
428 0 return;
429 }
430  
431 sub eval_action_highlight
432 0 ▶{
Subroutine eval_action_highlight: not called CC=3 SCAR=24.8
Pod undocumented
433 0 my $self = shift;
434 0 my ( $event, $results ) = @_;
435  
436 0 my $str = $event->{text};
437 0 ▶ ref $str or $str = Circle::TaggedString->new( $str );
Branch: 1 branch
BranchTrueFalseTotal
ref $str or $str = "Circle::TaggedString"->new($str) 0 0 0
438  
439 0 foreach my $matchgroup ( @{ $results->get_result( "matchgroups" ) } ) {
440 0 my ( $start, $len ) = @{$matchgroup->[0]}[0,1];
441  
442 0 $str->apply_tag( $start, $len, b => 1 );
443 0 $str->apply_tag( $start, $len, fg => "highlight" );
444 }
445  
446 0 $event->{level} = 3;
447 }
448  
449 4 0x55AA;
« Circle/Ruleable.pm ↑ Summary Circle/WindowItem.pm »