bin/expand-tail

filter: Statementstmt 85.9% 104 / 121 Branchbran 69.0% 29 / 42 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 76.1% 16 / 21 Podpod n/a totaltotal 79.3% 150 / 189 CC 21 SCAR 14.3
CC
21
Cov
81%
translate
8.0
expand_tail_at_pos
7.1
get_line_position_and_indents
5.3
CRAP 4.2
« website/gen-config.pl ↑ Summary lib/FP/Optional.pm »
1 #!/usr/bin/env perl
2  
3 # Copyright (c) 2015 Christian Jaeger, copying@christianjaeger.ch
4 # This is free software. See the file COPYING.md that came bundled
5 # with this file.
6  
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 17 ▶use warnings FATAL => 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # find modules from functional-perl working directory (not installed)
12 17 ▶use Cwd 'abs_path';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 17 our ($mydir, $myname);
14  
15 17 ▶BEGIN {
Subroutine BEGIN: called CC=3 SCAR=11.7
16 17 ▶ my $location = (-l $0) ? abs_path($0) : $0;
Branch: 1 branch
BranchTrueFalseTotal
-l $0 ? : 0 17 17
17 17 ▶ $location =~ /(.*?)([^\/]+?)_?\z/s or die "?";
Branch: 1 branch
BranchTrueFalseTotal
$location =~ m[(.*?)([^/]+?)_?\z]s or die "?" 17 0 17
18 17 ($mydir, $myname) = ($1, $2);
19 }
20 17 ▶use lib "$mydir/../lib";
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 17 ▶use Chj::xopen qw(xopen_read xopen_write);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 17 ▶use Chj::TEST ":all";
Subroutine BEGIN: called CC=1 SCAR=0.0
24 17 ▶use Chj::xIOUtil qw(xgetfile_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 #use FP::Repl::Trap; $SIG{INT}=sub{ die "SIGINT" };
27  
28 0 ▶sub usage {
Subroutine usage: not called CC=1 SCAR=6.9
29 0 print "usage: $myname in out
30 expand `tail ` syntax in Perl code using `Sub::Call::Tail`, so that it
31 doesn't depend on that module anymore.
32  
33 This is just a crude hack (totally imprecise).
34 ";
35 0 exit 1;
36 }
37  
38 17 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 17 our $verbose = 0;
40 17 our $opt_repl;
41 GetOptions(
42 "verbose" => \$verbose,
43 0 ▶ "help" => sub {usage},
Subroutine __ANON__: not called CC=1 SCAR=6.9
44 17 ▶ "repl" => \$opt_repl,
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("verbose", \$main::verbose, "help", sub { usage(); } , "repl", \$main::opt_repl) or exit 1 17 0 17
45  
46 #"dry-run"=> \$opt_dry,
47 ) or exit 1;
48 17 ▶usage unless @ARGV == 2;
Branch: 1 branch
BranchTrueFalseTotal
unless @ARGV == 2 0 17 17
49  
50 17 our ($inpath, $outpath) = @ARGV;
51  
52 17 our $code = xgetfile_utf8($inpath);
53  
54 17 our $IDENT = qr/\w+(?:::\w+)*/;
55  
56 13 ▶sub translate {
Subroutine translate: called CC=7 SCAR=20.7
57 13 my ($c) = @_;
58  
59 #warn "translate: <$c>";
60 13 $c =~ s/\s+\z//s; # XX killing line numbering
61 13 $c =~ s/^\s+//s; # dito?
62 13 my @p;
63 13 ▶ if ($c =~ /\#/) {
Branch: 4 branches
BranchTrueFalseTotal
if ($c =~ /\#/) { } 1 12 13
elsif (@p = split(/->/, $c, 0) and @p == 2) { } 0 12 12
elsif ($c =~ s/^\&//) { } 8 4 12
elsif (() = $c =~ /^($main::IDENT)\s*(\(.*)/s) { } 2 2 4
64 undef
65 1 ▶ } elsif (@p = split /->/, $c and @p == 2) {
Condition33%1/3
@p = split(/->/, $c, 0) and @p == 2
!ll&&!rl&&r
0120
MC/DC0%0/2
@p = split(/->/, $c, 0) and @p == 2
@p = split(/->/, $c, 0) @p == 2
Truth table33%1/3
A and B
A= @p = split(/->/, $c, 0)B= @p == 2
ABresult
0X0
100
111
66 0 my ($before, $after) = @p;
67 0 '@_=' . $after . '; goto &{' . $before . '}'
68 } elsif ($c =~ s/^\&//) {
69 8 ▶ if (my ($ident, $args) = $c =~ m/^(\$${IDENT})\s*(\(.*)/s) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $c =~ /^(\$$main::IDENT)\s*(\(.*)/s) { } 8 0 8
70 8 '@_=' . $args . '; goto &' . $ident
71 } else {
72 0 die "dunno about '$c'";
73 }
74 } elsif (my ($ident, $args) = $c =~ m/^($IDENT)\s*(\(.*)/s) {
75 2 '@_=' . $args . '; goto \&' . $ident
76 } else {
77 undef
78 2 }
79 }
80  
81 0 ▶TEST { translate '&$odd ($n - 1)' . "\n\t " }
Subroutine __ANON__: not called CC=1 SCAR=6.9
82 17 '@_=($n - 1); goto &$odd';
83 0 ▶TEST { translate 'Weakened($even)->($n)' }
Subroutine __ANON__: not called CC=1 SCAR=6.9
84 17 '@_=($n); goto &{Weakened($even)}';
85 0 ▶TEST {
Subroutine __ANON__: not called CC=1 SCAR=6.9
86 0 translate ' &$then
87 ($$config{downcaps} && is_allcaps ($2) ? $1.lc($2).".xhtml"
88 : $path0)'
89 }
90 17 '@_=($$config{downcaps} && is_allcaps ($2) ? $1.lc($2).".xhtml"
91 : $path0); goto &$then';
92  
93 17 ▶use FP::Div 'min';
Subroutine BEGIN: called CC=1 SCAR=0.0
94  
95 13 ▶sub min_maybe {
Subroutine min_maybe: called CC=1 SCAR=0.0
96 13 min grep { defined $_ } @_
97 }
98  
99 # register positions of the lines, and their indentation
100 27 ▶sub get_line_position_and_indents {
Subroutine get_line_position_and_indents: called CC=5 SCAR=16.7
101 27 my $line_position_and_indents = [];
102 27 my $lineno = -1; # 0-based index, *not* what editors expect
103 27 ▶ while ($code =~ /(?:^|\n)([ \t]*)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main::code =~ /(?:^|\n)([ \t]*)/g) 5592 27 5619
104 5592 $lineno++;
105 5592 my $indentstr = $1;
106 5592 my $pos = pos($code);
107  
108 # the pos where that line starts:
109 5592 my $pos0 = $pos - length($indentstr);
110 5592 my $i = 0;
111 5592 for (split //, $indentstr) {
112 23187 ▶ if ($_ eq ' ') {
Branch: 2 branches
BranchTrueFalseTotal
if ($_ eq ' ') { } 23187 0 23187
elsif ($_ eq "\t") { } 0 0 0
113 23187 $i++
114 } elsif ($_ eq "\t") {
115  
116 # 8-based tabs
117 0 $i = (int($i / 8) + 1) * 8
118 } else {
119 0 die "??"
120 }
121 }
122 5592 push @$line_position_and_indents, [$lineno, $pos0, $i];
123 }
124 $line_position_and_indents
125 27 }
126  
127 17 our $line_position_and_indents = get_line_position_and_indents;
128  
129 13 ▶sub find_line_by_pos {
Subroutine find_line_by_pos: called CC=3 SCAR=11.0
130 13 my ($pos) = @_;
131  
132 # XX would need binary search for efficiency.
133 13 my $prevline = $$line_position_and_indents[0];
134 13 for (@$line_position_and_indents[1 .. $#$line_position_and_indents]) {
135 1910 my ($lineno, $pos0, $i) = @$_;
136 1910 ▶ return $prevline if $pos < $pos0;
Branch: 1 branch
BranchTrueFalseTotal
if $pos < $pos0 13 1897 1910
137 1897 $prevline = $_;
138 }
139 0 return $prevline # (don't have len of that line to check, thus trust)
140 }
141  
142 # expand the 'tail' keyword right before pos in $code, set pos to
143 # afterwards.
144 13 ▶sub expand_tail_at_pos {
Subroutine expand_tail_at_pos: called CC=7 SCAR=19.6
145 13 my $pos = pos($code);
146  
147 #warn "expand_tail_at_pos $pos";
148  
149 # Where is the end of the arguments? Either when encountering a
150 # ";", or a line with indent the same or smaller than the current
151 # line, whichever comes first.
152  
153 13 ▶ my $maybe_endpos_semicolon = pos($code) - 1 if $code =~ /;/g;
Branch: 1 branch
BranchTrueFalseTotal
if $main::code =~ /;/g 13 0 13
154  
155 # -1 so as to leave the ';' in *afterwards*.
156  
157 my ($tailline_lineno, $tailline_pos0, $tailline_i)
158 13 = @{ find_line_by_pos $pos};
159 13 my $afterline;
160 13 for my $lineno ($tailline_lineno + 1 .. $#$line_position_and_indents) {
161 21 $afterline = $$line_position_and_indents[$lineno];
162 21 ▶ last if $$afterline[2] <= $tailline_i;
Branch: 1 branch
BranchTrueFalseTotal
if $afterline->[2] <= $tailline_i 13 8 21
163 }
164 13 ▶ my $maybe_endpos_indent = $$afterline[1] - 1 if $afterline;
Branch: 1 branch
BranchTrueFalseTotal
if $afterline 13 0 13
165  
166 # -1 so as to leave the "\n" in.
167  
168 13 my $maybe_endpos = min_maybe($maybe_endpos_semicolon, $maybe_endpos_indent);
169  
170 13 ▶ if (defined $maybe_endpos) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $maybe_endpos) { } 13 0 13
171 13 my $endpos = $maybe_endpos;
172 13 my $args = substr $code, $pos, $endpos - $pos;
173 13 ▶ if (defined(my $replacement = translate $args)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $replacement = translate($args))) { } 10 3 13
174 10 my $startpos = $pos - 4;
175  
176 10 substr $code, $startpos, $endpos - $startpos, $replacement;
177  
178 # re-init index. XX nonscalable of course.
179 10 $line_position_and_indents = get_line_position_and_indents;
180  
181 10 pos($code) = $startpos + length $replacement;
182  
183 #warn "right: '$args'";
184 } else {
185  
186 #warn "wrong1: '$args'";
187 3 pos($code) = $pos + 1;
188 }
189 } else {
190 0 warn "wrong2"
191 }
192 }
193  
194 17 ▶if ($opt_repl) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main::opt_repl) { } 0 17 17
195 0 require Chj::Backtrace;
196 0 require FP::Repl;
197 0 FP::Repl::repl();
198 0 exit;
199  
200 } else {
201  
202 # be insensitive to 'tail ' mentioned in comments; so bad. but
203 # lookbehind are not variable width, and setting pos($code) from
204 # within a substitution does not work.
205 17 $code =~ s=\n[\t ]*#[^\n]*\btail [^\n]*[^\n]=\n\n=sg;
206  
207 # Instead of writing a various kinds of parens and various kinds
208 # of quoting parser, look at indentation: after newlines allow
209 # only more indentation than the line where the tail statement is
210 # found on has.
211 17 ▶ while ($code =~ m/(?<!\$)\btail\b/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main::code =~ /(?<!\$)\btail\b/g) 13 17 30
212 13 expand_tail_at_pos
213 }
214  
215 17 $code =~ s/\buse\s*Sub::Call::Tail\b.*?;//s; # XX kills line numbering
216  
217 17 my $o = xopen_write $outpath;
218 17 $o->xprint($code);
219 17 $o->xclose;
220  
221 17 ▶ chmod 0755, $outpath if -x $inpath;
Branch: 1 branch
BranchTrueFalseTotal
if -x $main::inpath 2 15 17
222  
223 }
« website/gen-config.pl ↑ Summary lib/FP/Optional.pm »