blib/lib/YATT/Lite/CGen.pm

filter: Statementstmt 88.7% 126 / 142 Branchbran 74.2% 49 / 66 Conditioncond 56.2% 18 / 32 MC/DCmcdc 22.2% 4 / 18 Subroutinesub 82.3% 28 / 34 Podpod 0.0% 0 / 20 totaltotal 72.1% 225 / 312 CC 46 SCAR 16.1
CC
46
Cov
80%
ensure_generated
13.0
cut_next_nl
7.0
find_var
6.2
CRAP 5.0
« blib/lib/YATT/Lite/Object.pm ↑ Summary blib/lib/YATT/Lite/Response.pm »
1 package YATT::Lite::CGen; sub MY () {__PACKAGE__}
2 74 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 74 ▶use warnings qw(FATAL all NONFATAL misc);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 74 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 74 ▶use constant DEBUG_REBUILD => $ENV{DEBUG_YATT_REBUILD};
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 74 ▶use YATT::Lite::Util::AsBase qw/-as_base import/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 74 ▶use base qw(YATT::Lite::VarMaker);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 74 ▶use YATT::Lite::MFields qw/_curtmpl _curwidget _curtoks
Subroutine BEGIN: called CC=1 SCAR=0.0
12 _altgen _needs_escaping _depth
13 cgen_loader
14 only_parse
15 no_lineinfo check_lineno
16 yatt_no_last_newline
17 vfs parser sink
18 _scope
19 lcmsg_sink
20 prefer_call_for_entity
21 no_conditional_call
22 pagevars
23 /
24 74 ;
25  
26 74 ▶use YATT::Lite::Core qw(Template Part Folder);
Subroutine BEGIN: called CC=1 SCAR=0.0
27 74 ▶use YATT::Lite::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 74 ▶use YATT::Lite::Util qw(callerinfo numLines);
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 589 ▶sub ensure_generated_for_folders {
Subroutine ensure_generated_for_folders: called CC=3 SCAR=11.0
Pod undocumented
31 589 (my MY $self, my $spec) = splice @_, 0, 2;
32 589 foreach my Folder $folder (@_) {
33 605 ▶ if ($folder->can_generate_code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($folder->can_generate_code) 35 570 605
34 35 $self->ensure_generated($spec, $folder);
35 }
36 }
37 }
38  
39 880 ▶sub ensure_generated {
Subroutine ensure_generated: called CC=11 SCAR=25.7
Pod undocumented
40 880 (my MY $self, my $spec, my Template $tmpl) = @_;
41 880 ▶ my ($type, $kind) = ref $spec ? @$spec : $spec;
Branch: 1 branch
BranchTrueFalseTotal
ref $spec ? : 0 880 880
42 880 ▶ $self->{vfs}->error(q{sink is empty}) unless $self->{sink};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"sink"} 0 880 880
43 880 ▶ return if defined $tmpl->{_product}{$type};
Branch: 1 branch
BranchTrueFalseTotal
if defined $tmpl->{"_product"}{$type} 299 581 880
44 581 ▶ local $self->{_depth} = 1 + ($self->{_depth} // 0);
Condition100%2/2
$self->{"_depth"} // 0
l!l
34547
MC/DC100%1/1
$self->{"_depth"} // 0
$self->{"_depth"}
Truth table100%2/2
A
A= $self->{"_depth"}
Aresult
11
00
45 581 my $pkg = $tmpl->{_product}{$type} = $tmpl->{entns};
46 581 ▶ if (not defined $tmpl->{_product}{$type}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $tmpl->{'_product'}{$type}) { } 0 581 581
47 0 croak "package for product $type of $tmpl->{path} is not defined!";
48 } else {
49 print STDERR "# generating $pkg for $type code of "
50 581 . ($tmpl->{path} // "(undef)") . "\n"
51 if DEBUG_REBUILD;
52 }
53 $self->{parser}->parse_body($tmpl)
54 if not $kind or not $self->{only_parse}
55 581 ▶ or $self->{only_parse}{$kind};
Branch: 1 branch
BranchTrueFalseTotal
if not $kind && $self->{"only_parse"} or $self->{"only_parse"}{$kind} 581 0 581
Condition33%2/6
not $kind && $self->{"only_parse"} or $self->{"only_parse"}{$kind}
l!l&&r!l&&!r
58100
$kind && $self->{'only_parse'}
!ll&&!rl&&r
58100
MC/DC0%0/3
not $kind && $self->{"only_parse"} or $self->{"only_parse"}{$kind}
$kind $self->{'only_parse'} $self->{"only_parse"}{$kind}
Truth table25%1/4
not(A && B) or C
A= $kindB= $self->{'only_parse'}C= $self->{"only_parse"}{$kind}
ABCresult
0XX1
10X1
1111
1100
56 572 $self->setup_inheritance_for($spec, $tmpl);
57 # <!yatt:import> のソースも先に generate しておく。
58 # (子の glob 代入が eval される時点でソース側の sub が必要) GH-256
59 572 ▶ if (my @import_srcs = $tmpl->list_import_sources) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@import_srcs) = $tmpl->list_import_sources) 17 555 572
60 17 $self->ensure_generated_for_folders($spec, @import_srcs);
61 }
62 572 my @res = $self->generate($tmpl, $kind);
63 506 ▶ if (my $sub = $self->{sink}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sub = $self->{"sink"}) 506 0 506
64 $sub->({folder => $tmpl, package => $pkg, kind => 'body'
65 , depth => $self->{_depth}}
66 506 , @res);
67 }
68 504 $pkg;
69 }
70  
71 79 ▶sub with_template {
Subroutine with_template: called CC=4 SCAR=14.4
Pod undocumented
72 79 (my MY $self, my Template $tmpl, my ($task, @args)) = @_;
73 79 local $self->{_curtmpl} = $tmpl;
74 79 local $self->{_curline} = 1;
75 79 ▶ if (ref $task eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $task eq 'CODE') { } 48 31 79
76 48 $task->($self, @args);
77 } else {
78 31 my ($meth, @rest) = YATT::Lite::Util::lexpand($task);
79 31 ▶ unless ($meth) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($meth) 0 31 31
80 0 Carp::croak "meth is undef";
81 }
82 31 ▶ my $sub = $self->can($meth) or do {
Branch: 1 branch
BranchTrueFalseTotal
my $sub = $self->can($meth) or do { Carp::croak("No such method $meth") } 31 0 31
83 0 Carp::croak "No such method $meth";
84 };
85 31 $sub->($self, @rest, @args);
86 }
87 }
88  
89 572 ▶sub generate {
Subroutine generate: called CC=5 SCAR=17.8
Pod undocumented
90 572 (my MY $self, my Template $tmpl) = splice @_, 0, 2;
91 572 ▶ my $kind = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 572 0 572
92 # XXX: Rewrite this with with_template
93 572 local $self->{_curtmpl} = $tmpl;
94 572 local $self->{_curline} = 1;
95 572 local $self->{_scope} = $self->generate_page_scope($tmpl);
96 ($self->generate_preamble($self->{_curtmpl})
97 , map {
98 882 my Part $part = $_;
99 882 ▶ if (not $kind or not $self->{only_parse}
Branch: 1 branch
BranchTrueFalseTotal
if (not $kind && $self->{'only_parse'} or $kind eq $part->{'kind'}) { } 882 0 882
Condition33%2/6
not $kind && $self->{"only_parse"} or $kind eq $part->{"kind"}
l!l&&r!l&&!r
88200
$kind && $self->{'only_parse'}
!ll&&!rl&&r
88200
MC/DC0%0/3
not $kind && $self->{"only_parse"} or $kind eq $part->{"kind"}
$kind $self->{'only_parse'} $kind eq $part->{"kind"}
Truth table25%1/4
not(A && B) or C
A= $kindB= $self->{'only_parse'}C= $kind eq $part->{"kind"}
ABCresult
0XX1
10X1
1111
1100
100 or $kind eq $part->{kind}) {
101 882 $self->generate_part($tmpl, $part);
102 } else {
103 0 ();
104 }
105 572 } @{$tmpl->{_partlist}});
106 }
107  
108 882 ▶sub generate_part {
Subroutine generate_part: called CC=2 SCAR=7.0
Pod undocumented
109 882 (my MY $self, my Template $tmpl, my Part $part) = @_;
110 my $sub = $self->can("generate_$part->{kind}")
111 or die $self->generror("Can't generate part type: '%s'"
112 882 ▶ , $part->{kind});
Branch: 1 branch
BranchTrueFalseTotal
my $sub = $self->can(qq[generate_$part->{"kind"}]) or die $self->generror("Can't generate part type: '%s'", $part->{"kind"}) 882 0 882
113 882 local $self->{yatt_no_last_newline} = $part->{yatt_no_last_newline};
114 882 $sub->($self, $part, $part->{name}, $tmpl->{path});
115 }
116  
117 0 ▶sub generate_page_scope {
Subroutine generate_page_scope: not called CC=1 SCAR=6.9
Pod undocumented
118 +{}
119 0 }
120  
121 0 ▶sub setup_inheritance_for {
Subroutine setup_inheritance_for: not called CC=1 SCAR=6.9
Pod undocumented
122 0 (my MY $self, my $spec, my Template $tmpl) = @_;
123 0 $self->ensure_generated_for_folders($spec, $tmpl->list_base);
124 }
125  
126 #========================================
127 488 ▶sub altgen {
Subroutine altgen: called CC=3 SCAR=11.6
Pod undocumented
128 488 (my MY $self, my $ns) = @_;
129 # ns 一つに付き 高々 1回しか、can しないで済むように... と言っても、cgen 自体が複数個作られたら..
130 488 ▶ unless (exists $self->{_altgen}{$ns}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"_altgen"}{$ns}) 304 184 488
131 304 $self->{_altgen}{$ns} = do {
132 304 ▶ if (my $sub = $self->can("create_altgen_$ns")) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sub = $self->can("create_altgen_$ns")) 0 304 304
133 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
134 # 毎回, new し直す。
135 0 $sub->($self)->generate_node(@_);
136 0 };
137 }
138 };
139 }
140 488 $self->{_altgen}{$ns};
141 }
142 0 ▶sub create_altgen_js {
Subroutine create_altgen_js: not called CC=1 SCAR=6.9
Pod undocumented
143 0 require YATT::Lite::CGen::JS;
144 0 my MY $self = shift;
145 0 new YATT::Lite::CGen::JS
146 ($self->cf_delegate(qw(vfs parser no_lineinfo check_lineno)));
147 }
148 #========================================
149 1433 ▶sub find_var {
Subroutine find_var: called CC=6 SCAR=18.2
Pod undocumented
150 1433 (my MY $self, my $varName, my $check) = @_;
151 1433 ▶ confess "Undefined varName for find_var!" unless defined $varName;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $varName 0 1433 1433
152 1433 ▶ for (my $scope = $self->{_scope}; $scope; $scope = $scope->[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scope) 5811 760 6571
153 5811 ▶ if (defined (my $var = $scope->[0]{$varName})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $var = $scope->[0]{$varName})) 673 5138 5811
154 673 ▶ next if $check and not $check->($var);
Branch: 1 branch
BranchTrueFalseTotal
if $check and not &$check($var) 0 673 673
Condition66%2/3
$check and not &$check($var)
!ll&&!rl&&r
594790
MC/DC0%0/2
$check and not &$check($var)
$check not &$check($var)
Truth table66%2/3
A and not(B)
A= $checkB= not &$check($var)
ABresult
0X0
100
111
155 673 return $var;
156 }
157 }
158 }
159 732 ▶sub find_callable_var {
Subroutine find_callable_var: called CC=1 SCAR=0.0
Pod undocumented
160 732 (my MY $self, my $varName) = @_;
161 732 ▶ $self->find_var($varName, sub {shift->callable});
Subroutine __ANON__: called CC=1 SCAR=0.0
162 }
163 317 ▶sub lookup_widget {
Subroutine lookup_widget: called CC=2 SCAR=6.9
Pod undocumented
164 317 (my MY $self, my ($ns, @path)) = @_;
165 # ns 抜きと、有りで一回ずつ検索する
166 $self->{vfs}->find_part_from($self->{_curtmpl}, @path)
167 317 ▶ || $self->{vfs}->find_part_from($self->{_curtmpl}, $ns, @path);
Branch: 1 branch
BranchTrueFalseTotal
$self->{"vfs"}->find_part_from($self->{"_curtmpl"}, @path) or $self->{"vfs"}->find_part_from($self->{"_curtmpl"}, $ns, @path) 305 12 317
168 }
169  
170 53 ▶sub generror {
Subroutine generror: called CC=1 SCAR=0.0
Pod undocumented
171 53 my MY $self = shift;
172 53 my Template $tmpl = $self->{_curtmpl};
173 53 my ($pkg, $file, $line) = caller;
174 53 my %opts = ($self->_tmpl_file_line($self->{_curline}), callerinfo());
175 53 $self->_error(\%opts, @_);
176 }
177 # generror の行番号指定版 (LRXML の synerror_at に相当)。
178 # 例: die $cgen->generror_at($node->[NODE_LNO], ...)
179 12 ▶sub generror_at {
Subroutine generror_at: called CC=2 SCAR=7.6
Pod undocumented
180 12 (my MY $self, my $ln) = splice @_, 0, 2;
181 12 ▶ my %opts = ($self->_tmpl_file_line($ln // $self->{_curline}), callerinfo());
Condition33%1/3
$ln // $self->{"_curline"}
l!l&&r!l&&!r
1200
MC/DC0%0/2
$ln // $self->{"_curline"}
$ln $self->{"_curline"}
Truth table33%1/3
A // B
A= $lnB= $self->{"_curline"}
ABresult
1X1
011
000
182 12 $self->_error(\%opts, @_);
183 }
184 65 ▶sub _error {
Subroutine _error: called CC=1 SCAR=0.0
185 65 my MY $self = shift;
186 65 $self->{vfs}->error(@_);
187 }
188 65 ▶sub _tmpl_file_line {
Subroutine _tmpl_file_line: called CC=3 SCAR=11.4
189 65 (my MY $self, my $ln) = @_;
190 65 my Template $tmpl = $self->{_curtmpl};
191 (tmpl_file => $tmpl->{path} // $tmpl->{name}
192 65 ▶ , defined $ln ? (tmpl_line => $ln) : ());
Branch: 1 branch
BranchTrueFalseTotal
defined $ln ? : 65 0 65
Condition66%2/3
$tmpl->{"path"} // $tmpl->{"name"}
l!l&&r!l&&!r
19460
MC/DC0%0/2
$tmpl->{"path"} // $tmpl->{"name"}
$tmpl->{"path"} $tmpl->{"name"}
Truth table66%2/3
A // B
A= $tmpl->{"path"}B= $tmpl->{"name"}
ABresult
1X1
011
000
193 }
194  
195 198 ▶sub add_curline {
Subroutine add_curline: called CC=1 SCAR=0.0
Pod undocumented
196 198 (my MY $self, my $text) = @_;
197 198 $self->{_curline} += numLines($text);
198 198 $text;
199 }
200  
201 4532 ▶sub sync_curline {
Subroutine sync_curline: called CC=6 SCAR=18.2
Pod undocumented
202 4532 (my MY $self, my $lineno) = @_;
203 4532 ▶ return unless defined $lineno;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $lineno 637 3895 4532
204 3895 my $diff = $lineno - $self->{_curline};
205 3895 ▶ die "curline exceeds expected lineno! expect $lineno, curline=$self->{_curline}\n" if $self->{check_lineno} and $diff < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"check_lineno"} and $diff < 0 0 3895 3895
Condition33%1/3
$self->{"check_lineno"} and $diff < 0
!ll&&!rl&&r
389500
MC/DC0%0/2
$self->{"check_lineno"} and $diff < 0
$self->{"check_lineno"} $diff < 0
Truth table33%1/3
A and B
A= $self->{"check_lineno"}B= $diff < 0
ABresult
0X0
100
111
206 3895 ▶ $self->{_curline} = $lineno if $lineno > $self->{_curline};
Branch: 1 branch
BranchTrueFalseTotal
if $lineno > $self->{"_curline"} 260 3635 3895
207 3895 ▶ $diff > 0 ? "\n" x $diff : ();
Branch: 1 branch
BranchTrueFalseTotal
$diff > 0 ? : 260 3635 3895
208 }
209 # <!yatt:widget ...> や <yatt:call ...> の直後の改行を,
210 # ソース上のみの(出力しない)改行に変換する。
211 1518 ▶sub cut_next_nl {
Subroutine cut_next_nl: called CC=7 SCAR=19.5
Pod undocumented
212 1518 my MY $self = shift;
213 # undef は返したくないので。
214 return wantarray ? () : ''
215 unless $self->{_curtoks}
216 1518 ▶ and @{$self->{_curtoks}} and $self->{_curtoks}[0] =~ /^\r?\n$/;
Branch: 2 branches
BranchTrueFalseTotal
unless $self->{"_curtoks"} and @{$self->{"_curtoks"};} and $self->{"_curtoks"}[0] =~ /^\r?\n$/ 1069 449 1518
wantarray ? : 1027 42 1069
Condition100%6/6
$self->{"_curtoks"} and @{$self->{"_curtoks"};} and $self->{"_curtoks"}[0] =~ /^\r?\n$/
!ll&&!rl&&r
661003449
$self->{"_curtoks"} and @{$self->{"_curtoks"};}
!ll&&!rl&&r
28381452
MC/DC100%3/3
$self->{"_curtoks"} and @{$self->{"_curtoks"};} and $self->{"_curtoks"}[0] =~ /^\r?\n$/
$self->{"_curtoks"} @{$self->{"_curtoks"};} $self->{"_curtoks"}[0] =~ /^\r?\n$/
Truth table100%4/4
A and B and C
A= $self->{"_curtoks"}B= @{$self->{"_curtoks"};}C= $self->{"_curtoks"}[0] =~ /^\r?\n$/
ABCresult
0XX0
10X0
1100
1111
217 return wantarray ? () : ''
218 449 ▶ if @{$self->{_curtoks}} == 1; # 最後の一個の改行は、残す。これは "}\n" のため
Branch: 2 branches
BranchTrueFalseTotal
if @{$self->{"_curtoks"};} == 1 260 189 449
wantarray ? : 260 0 260
219 189 $self->{_curline}++;
220 189 shift @{$self->{_curtoks}};
221 }
222  
223 1040 ▶sub mkscope {
Subroutine mkscope: called CC=4 SCAR=13.9
Pod undocumented
224 1040 my MY $self = shift;
225 1040 ▶ return unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 1040 1040
226 1040 ▶ my $scope = ref $_[-1] eq 'ARRAY' ? pop : [pop];
Branch: 1 branch
BranchTrueFalseTotal
ref $_[-1] eq 'ARRAY' ? : 211 829 1040
227 1040 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 3603 1040 4643
228 3603 $scope = [pop, $scope];
229 }
230 1040 $scope;
231 }
232  
233 0 ▶sub terse_dump {
Subroutine terse_dump: not called CC=1 SCAR=6.9
Pod undocumented
234 0 my MY $self = shift;
235 0 YATT::Lite::Util::terse_dump(@_);
236 }
237  
238 0 ▶sub node_sync_curline {
Subroutine node_sync_curline: not called CC=1 SCAR=6.9
Pod undocumented
239 0 (my MY $self, my $node) = @_;
240 0 $self->sync_curline($node->[NODE_LNO]);
241 }
242  
243 74 1;
« blib/lib/YATT/Lite/Object.pm ↑ Summary blib/lib/YATT/Lite/Response.pm »