blib/lib/YATT/Lite/ArgMacro.pm

filter: Statementstmt 94.1% 128 / 136 Branchbran 73.6% 28 / 38 Conditioncond 53.3% 16 / 30 MC/DCmcdc 22.2% 4 / 18 Subroutinesub 87.5% 14 / 16 Podpod 28.5% 2 / 7 totaltotal 78.3% 192 / 245 CC 34 SCAR 19.7
CC
34
Cov
84%
declare_into
12.1
parse_arg_spec
11.7
expand_element
7.1
CRAP 7.1
« blib/lib/YATT/Lite/Util/AsBase.pm ↑ Summary blib/lib/YATT/Lite/MFields.pm »
1 package YATT::Lite::ArgMacro;
2 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use warnings qw(FATAL all NONFATAL misc);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 sub MY () {__PACKAGE__}
7  
8 5 ▶use YATT::Lite::Util qw(globref define_const);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use YATT::Lite::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 #
12 # package MyApp::ArgMacro::Enum;
13 # use YATT::Lite::ArgMacro
14 # out => [qw(list=list)],
15 # in => ['first=value/0', 'last=value!'];
16 #
17 # sub on_expand {
18 # (my MY $macro, my CGen $cgen, my Args $args, my Vars $vars) = @_;
19 # my Result $result = {};
20 # ...
21 # $result;
22 # }
23 #
24 12 ▶sub import {
Subroutine import: called CC=2 SCAR=7.0
25 12 my ($pack, %opts) = @_;
26 # Subclasses (ie. argmacro modules) inherit this import. Using them
27 # must not redefine anything.
28 12 ▶ return if $pack ne MY;
Branch: 1 branch
BranchTrueFalseTotal
if $pack ne "YATT::Lite::ArgMacro" 0 12 12
29 12 my $callpack = caller;
30 12 MY->declare_into($callpack, %opts);
31 }
32  
33 17 ▶sub declare_into {
Subroutine declare_into: called CC=12 SCAR=24.9
Pod undocumented
34 17 my ($pack, $destpkg, %opts) = @_;
35  
36 my $out = delete $opts{out}
37 17 ▶ or croak "ArgMacro requires 'out' (list of output args)";
Branch: 1 branch
BranchTrueFalseTotal
my $out = delete $opts{"out"} or croak("ArgMacro requires 'out' (list of output args)") 16 1 17
38 16 ▶ my $in = delete $opts{in} // [];
Condition100%2/2
delete $opts{"in"} // []
l!l
151
MC/DC100%1/1
delete $opts{"in"} // []
delete $opts{"in"}
Truth table100%2/2
A
A= delete $opts{"in"}
Aresult
11
00
39 16 ▶ my $refer = delete $opts{refer} // [];
Condition100%2/2
delete $opts{"refer"} // []
l!l
412
MC/DC100%1/1
delete $opts{"refer"} // []
delete $opts{"refer"}
Truth table100%2/2
A
A= delete $opts{"refer"}
Aresult
11
00
40 16 ▶ if (keys %opts) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %opts) 0 16 16
41 0 croak "Unknown options for ArgMacro: ".join(", ", sort keys %opts);
42 }
43  
44 # refer は widget 側で宣言する引数を参照するだけなので、名前のみ
45 16 foreach my $name (@$refer) {
46 4 ▶ croak "ArgMacro refer accepts only arg names: $name"
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /^\w+\z/ 1 3 4
47 unless $name =~ /^\w+\z/;
48 }
49  
50 my $spec = +{
51 16 out => [map {$pack->parse_arg_spec($_)} @$out],
52 15 in => [map {$pack->parse_arg_spec($_)} @$in],
53 refer => [@$refer],
54 };
55 14 ▶ croak "ArgMacro requires at least one output arg" unless @{$spec->{out}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$spec->{"out"};} 0 14 14
56  
57 14 my %seen;
58 14 foreach my $name ((map {$_->[0]} @{$spec->{out}}, @{$spec->{in}})
59 14 , @{$spec->{refer}}) {
60 36 ▶ croak "Duplicate arg name in ArgMacro: $name" if $seen{$name}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$name}++ 2 34 36
61 }
62  
63 12 require YATT::Lite::MFields;
64 12 YATT::Lite::MFields->add_isa_to($destpkg, MY);
65  
66 # my CGen $cgen 等の型付き lexical のため、型のクラスを先に読み込んでおく
67 # (perl -c で単独コンパイルできるように)
68 12 require YATT::Lite::CGen::Perl;
69 12 require YATT::Lite::Core;
70  
71 12 my @in_names = ((map {$_->[0]} @{$spec->{in}}), @{$spec->{refer}});
72 12 my @out_names = map {$_->[0]} @{$spec->{out}};
73  
74 12 my %types = (
75 MY => $destpkg,
76 Args => $pack->define_record_class("${destpkg}::Args", @in_names),
77 Vars => $pack->define_record_class("${destpkg}::Vars", @in_names),
78 Result => $pack->define_record_class("${destpkg}::Result", @out_names),
79 CGen => 'YATT::Lite::CGen::Perl',
80 ArgMacro => 'YATT::Lite::Core::ArgMacro',
81 );
82 12 foreach my $name (sort keys %types) {
83 72 my $glob = globref($destpkg, $name);
84 72 ▶ next if *{$glob}{CODE};
Branch: 1 branch
BranchTrueFalseTotal
if *{$glob;}{"CODE"} 0 72 72
85 72 define_const($glob, $types{$name});
86 }
87  
88 12 define_const(globref($destpkg, 'macro_spec'), $spec);
89  
90 12 $destpkg;
91 }
92  
93 # "name", "name=type", "name=type/default" ... (same as argument spec
94 # of <!yatt:argmacro>). Returns [name, spec_or_undef].
95 35 ▶sub parse_arg_spec {
Subroutine parse_arg_spec: called CC=9 SCAR=24.6
Pod undocumented
96 35 my ($pack, $item) = @_;
97 35 ▶ my ($name, $desc) = ref $item ? @$item : split /=/, $item, 2;
Branch: 1 branch
BranchTrueFalseTotal
ref $item ? : 0 35 35
98 35 ▶ unless (defined $name and $name =~ /^[[:alpha:]_]\w*\z/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name and $name =~ /^[[:alpha:]_]\w*\z/) 0 35 35
Condition33%1/3
defined $name and $name =~ /^[[:alpha:]_]\w*\z/
!ll&&!rl&&r
0035
MC/DC0%0/2
defined $name and $name =~ /^[[:alpha:]_]\w*\z/
defined $name $name =~ /^[[:alpha:]_]\w*\z/
Truth table33%1/3
A and B
A= defined $nameB= $name =~ /^[[:alpha:]_]\w*\z/
ABresult
0X0
100
111
99 0 ▶ croak "Invalid arg name in ArgMacro: ".($name // '(undef)');
Condition0%0/2
$name // "(undef)"
l!l
00
MC/DC0%0/1
$name // "(undef)"
$name
Truth table0%0/2
A
A= $name
Aresult
11
00
100 }
101 35 ▶ if (defined $desc) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $desc) 9 26 35
102 9 my ($type) = split m{[|/?!:]}, $desc, 2;
103 9 ▶ if (defined $type and $type =~ /^(?:code|delegate)\z/
Branch: 1 branch
BranchTrueFalseTotal
if (defined $type and $type =~ /^(?:code|delegate)\z/ or $desc =~ /^\[/) 1 8 9
Condition66%4/6
defined $type and $type =~ /^(?:code|delegate)\z/ or $desc =~ /^\[/
l!l&&r!l&&!r
108
defined $type and $type =~ /^(?:code|delegate)\z/
!ll&&!rl&&r
081
MC/DC33%1/3
defined $type and $type =~ /^(?:code|delegate)\z/ or $desc =~ /^\[/
defined $type $type =~ /^(?:code|delegate)\z/ $desc =~ /^\[/
Truth table40%2/5
A and B or C
A= defined $typeB= $type =~ /^(?:code|delegate)\z/C= $desc =~ /^\[/
ABCresult
11X1
0X11
1011
0X00
1000
104 or $desc =~ /^\[/) {
105 1 croak "ArgMacro arg type '$desc' is not supported in modules: $name";
106 }
107 }
108 34 [$name, $desc];
109 }
110  
111 # Defines a class whose %FIELDS is exactly @names. This enables compile time
112 # checking of field names on typed lexicals: my Result $r; $r->{typo} dies.
113 # Idempotent, so templates can be recompiled.
114 90 ▶sub define_record_class {
Subroutine define_record_class: called CC=2 SCAR=7.9
Pod undocumented
115 90 my ($pack, $class, @names) = @_;
116 90 my $glob = globref($class, 'FIELDS');
117 90 %{*$glob} = map {$_ => 1} @names;
118 90 my $new = globref($class, 'new');
119 0 ▶ *$new = sub { require fields; fields::new(ref $_[0] || $_[0]) }
Subroutine __ANON__: not called CC=2 SCAR=17.9
Condition0%0/3
ref $_[0] || $_[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0] $_[0]
Truth table0%0/3
A || B
A= ref $_[0]B= $_[0]
ABresult
1X1
011
000
120 90 ▶ unless *{$new}{CODE};
Branch: 1 branch
BranchTrueFalseTotal
unless *{$new;}{"CODE"} 90 0 90
121 90 $class;
122 }
123  
124 #========================================
125 # Bridge to YATT::Lite::Core::ArgMacro
126  
127 17 ▶sub as_argmacro_part {
Subroutine as_argmacro_part: called CC=2 SCAR=7.0
Pod undocumented
128 17 my ($class, $parser, $name, $ns) = @_;
129  
130 17 my $spec = $class->macro_spec;
131 17 ▶ $ns //= $parser->primary_ns;
Condition66%2/3
$ns //= $parser->primary_ns
l!l&&r!l&&!r
1520
MC/DC0%0/2
$ns //= $parser->primary_ns
$ns $parser->primary_ns
Truth table66%2/3
A //= B
A= $nsB= $parser->primary_ns
ABresult
1X1
011
000
132  
133 # Default values (eg. value/0) are parsed as text with entities,
134 # which needs position info of the parser.
135 17 local $parser->{_startpos} = 0;
136 17 local $parser->{_curpos} = 0;
137 17 local $parser->{_startln} = 1;
138 17 local $parser->{_endln} = 1;
139  
140 my $argmacro = $parser->ArgMacro->new(
141 name => $name, kind => 'argmacro', decl => 'argmacro',
142 namespace => $ns,
143 20 output_args => [map {$class->_mk_arg_node(@$_)} @{$spec->{out}}],
144 17 refer_names => [@{$spec->{refer}}],
145 );
146  
147 17 $parser->add_args($argmacro, map {$class->_mk_arg_node(@$_)} @{$spec->{in}});
148  
149 30 ▶ $argmacro->{_on_expand} = sub {
Subroutine __ANON__: called CC=3 SCAR=12.7
150 30 my ($cgen, $args, $vars, $macro, $node) = @_;
151 30 my $result = $class->on_expand($cgen, $args, $vars, $macro, $node);
152 27 ▶ unless (ref $result eq 'HASH' or UNIVERSAL::isa($result, 'HASH')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $result eq "HASH" or UNIVERSAL::isa($result, "HASH")) 0 27 27
Condition33%1/3
ref $result eq "HASH" or UNIVERSAL::isa($result, "HASH")
l!l&&r!l&&!r
2700
MC/DC0%0/2
ref $result eq "HASH" or UNIVERSAL::isa($result, "HASH")
ref $result eq "HASH" UNIVERSAL::isa($result, "HASH")
Truth table33%1/3
A or B
A= ref $result eq "HASH"B= UNIVERSAL::isa($result, "HASH")
ABresult
1X1
011
000
153 0 croak "$class->on_expand must return a hash";
154 }
155 27 $result;
156 17 };
157  
158 17 require YATT::Lite::CGen::ArgMacro;
159 $argmacro->{_on_declare}
160 17 = YATT::Lite::CGen::ArgMacro->make_on_declare($argmacro);
161  
162 17 $argmacro;
163 }
164  
165 41 ▶sub _mk_arg_node {
Subroutine _mk_arg_node: called CC=2 SCAR=6.9
166 41 my ($class, $name, $desc) = @_;
167 41 my $node = [];
168 41 ▶ if (defined $desc) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $desc) { } 8 33 41
169 8 $node->[NODE_TYPE] = TYPE_ATT_TEXT;
170 8 $node->[NODE_BODY] = $desc;
171 } else {
172 33 $node->[NODE_TYPE] = TYPE_ATT_NAMEONLY;
173 }
174 41 $node->[NODE_PATH] = $name;
175 41 $node;
176 }
177  
178 0 ▶sub on_expand {
Subroutine on_expand: not called CC=1 SCAR=6.9
Pod documented
179 0 my ($class) = @_;
180 0 croak "$class must implement on_expand";
181 }
182  
183 #
184 # 通常のマクロ (macro_foreach 等) の中で、要素 $node の属性にこの argmacro を
185 # 展開する (旧 YATT の YATT::ArgMacro->create_from に相当)。
186 #
187 # $node = MyApp::ArgMacro::Foo->expand_element($cgen, $node, rename => 'x=foo');
188 #
189 # トリガー (in) が有れば、それを出力属性に置き換えた新しい node を返す。
190 # 無ければ元の $node を返す。リストコンテキストでは ($node, $result)。
191 #
192 12 ▶sub expand_element {
Subroutine expand_element: called CC=7 SCAR=19.6
Pod documented
193 12 my ($class, $cgen, $node, %opts) = @_;
194 12 my ($toName, $fromName) = do {
195 12 ▶ if (defined(my $rename = delete $opts{rename})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $rename = delete $opts{'rename'})) { } 1 11 12
196 1 ▶ my @match = $rename =~ m{^(\w+)=(\w+)\z}
Branch: 1 branch
BranchTrueFalseTotal
my(@match) = $rename =~ /^(\w+)=(\w+)\z/ or croak("Invalid rename spec '${rename}'") 1 0 1
197 or croak "Invalid rename spec '$rename'";
198 1 @match;
199 } else {
200 11 ();
201 }
202 };
203 12 ▶ if (keys %opts) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %opts) 0 12 12
204 0 croak "Unknown options for expand_element: ".join(", ", sort keys %opts);
205 }
206  
207 12 require YATT::Lite::CGen::ArgMacro;
208 12 my ($instName, $instance, $triggers, $outputs)
209 = YATT::Lite::CGen::ArgMacro->instantiate(
210 $class->argmacro_part_for($cgen), $toName, $fromName
211 );
212  
213 my ($newPrimary, $results) = YATT::Lite::CGen::ArgMacro->expand_args(
214 $cgen, $cgen->node_unwrap_attlist($node->[NODE_ATTLIST]), $node, undef,
215 [$instName], {$instName => $instance},
216 18 {map {$_ => [$instName, $triggers->{$_}]} keys %$triggers},
217 12 {map {$_ => [$instName, $outputs->{$_}]} keys %$outputs},
218 );
219  
220 11 ▶ my $result = $results && $results->{$instName};
Condition66%2/3
$results && $results->{$instName}
!ll&&!rl&&r
506
MC/DC50%1/2
$results && $results->{$instName}
$results $results->{$instName}
Truth table66%2/3
A && B
A= $resultsB= $results->{$instName}
ABresult
0X0
100
111
221 11 my $newNode = do {
222 11 ▶ if ($result) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result) { } 6 5 11
223 6 my $copy = [@$node];
224 6 $copy->[NODE_ATTLIST] = $newPrimary;
225 6 $copy;
226 } else {
227 5 $node;
228 }
229 };
230  
231 11 ▶ wantarray ? ($newNode, $result) : $newNode;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 10 11
232 }
233  
234 # この module の Core::ArgMacro (VFS 毎に一度だけ作る)
235 12 ▶sub argmacro_part_for {
Subroutine argmacro_part_for: called CC=2 SCAR=7.0
Pod undocumented
236 12 my ($class, $cgen) = @_;
237 12 my $vfs = $cgen->{vfs};
238 12 ▶ $vfs->{_argmacro_module_cache}{"class:$class"}
Condition66%2/3
$vfs->{"_argmacro_module_cache"}{"class:$class"} //= $class->as_argmacro_part($vfs->get_parser, $class)
l!l&&r!l&&!r
1020
MC/DC0%0/2
$vfs->{"_argmacro_module_cache"}{"class:$class"} //= $class->as_argmacro_part($vfs->get_parser, $class)
$vfs->{"_argmacro_module_cache"}{"class:$class"} $class->as_argmacro_part($vfs->get_parser, $class)
Truth table66%2/3
A //= B
A= $vfs->{"_argmacro_module_cache"}{"class:$class"}B= $class->as_argmacro_part($vfs->get_parser, $class)
ABresult
1X1
011
000
239 //= $class->as_argmacro_part($vfs->get_parser, $class);
240 }
241  
242 5 1;
« blib/lib/YATT/Lite/Util/AsBase.pm ↑ Summary blib/lib/YATT/Lite/MFields.pm »