Data/Sah/Compiler/human.pm

filter: Statementstmt 85.4% 218 / 255 Branchbran 75.9% 82 / 108 Conditioncond 78.2% 54 / 69 MC/DCmcdc 57.5% 23 / 40 Subroutinesub 96.5% 28 / 29 Podpod 46.1% 6 / 13 totaltotal 79.9% 411 / 514 CC 89 SCAR 31.0
CC
89
Cov
82%
_add_ccl
48.9
add_ccl
23.3
_format_ccls_itext
11.0
CRAP 22.3
« Data/Sah/Compiler.pm ↑ Summary Data/Sah/Compiler/human/TH/HasElems.pm »
1 package Data::Sah::Compiler::human;
2  
3 25 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 25 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 25 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 #use Log::Any::IfLOG qw($log);
7  
8 25 ▶use Data::Dmp qw(dmp);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 25 ▶use Mo qw(build default);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 25 ▶use POSIX qw(locale_h);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 25 ▶use Text::sprintfn;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 25 extends 'Data::Sah::Compiler';
14  
15 25 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
16 25 our $DATE = '2024-02-16'; # DATE
17 25 our $DIST = 'Data-Sah'; # DIST
18 25 our $VERSION = '0.917'; # VERSION
19  
20 # every type extension is registered here
21 25 our %typex; # key = type, val = [clause, ...]
22  
23 10335 ▶sub name { "human" }
Subroutine name: called CC=1 SCAR=0.0
Pod undocumented
24  
25 9615 ▶sub _add_msg_catalog {
Subroutine _add_msg_catalog: called CC=2 SCAR=6.9
26 9615 my ($self, $cd, $msg) = @_;
27 9615 ▶ return unless $cd->{args}{format} eq 'msg_catalog';
Branch: 1 branch
BranchTrueFalseTotal
unless $cd->{"args"}{"format"} eq "msg_catalog" 43 9572 9615
28  
29 9572 my $spath = join("/", @{ $cd->{spath} });
30 9572 $cd->{_msg_catalog}{$spath} = $msg;
31 }
32  
33 5070 ▶sub check_compile_args {
Subroutine check_compile_args: called CC=3 SCAR=12.0
Pod undocumented
34 5070 my ($self, $args) = @_;
35  
36 5070 $self->SUPER::check_compile_args($args);
37  
38 5070 my @fmts = ('inline_text', 'inline_err_text', 'markdown', 'msg_catalog');
39 5070 ▶ $args->{format} //= $fmts[0];
Condition33%1/3
$args->{"format"} //= $fmts[0]
l!l&&r!l&&!r
507000
MC/DC0%0/2
$args->{"format"} //= $fmts[0]
$args->{"format"} $fmts[0]
Truth table33%1/3
A //= B
A= $args->{"format"}B= $fmts[0]
ABresult
1X1
011
000
40 5070 ▶ unless (grep { $_ eq $args->{format} } @fmts) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ eq $args->{"format"};} @fmts) 0 5070 5070
41 0 $self->_die({}, "Unsupported format, use one of: ".join(", ", @fmts));
42 }
43 }
44  
45 5070 ▶sub init_cd {
Subroutine init_cd: called CC=5 SCAR=16.2
Pod undocumented
46 5070 my ($self, %args) = @_;
47  
48 5070 my $cd = $self->SUPER::init_cd(%args);
49 5070 ▶ if (($cd->{args}{format} // '') eq 'msg_catalog') {
Branch: 1 branch
BranchTrueFalseTotal
if (($cd->{"args"}{"format"} // "") eq "msg_catalog") 5046 24 5070
Condition50%1/2
$cd->{"args"}{"format"} // ""
l!l
50700
MC/DC0%0/1
$cd->{"args"}{"format"} // ""
$cd->{"args"}{"format"}
Truth table50%1/2
A
A= $cd->{"args"}{"format"}
Aresult
11
00
50 5046 ▶ $cd->{_msg_catalog} //= $cd->{outer_cd}{_msg_catalog};
Condition66%2/3
$cd->{"_msg_catalog"} //= $cd->{"outer_cd"}{"_msg_catalog"}
l!l&&r!l&&!r
03174729
MC/DC50%1/2
$cd->{"_msg_catalog"} //= $cd->{"outer_cd"}{"_msg_catalog"}
$cd->{"_msg_catalog"} $cd->{"outer_cd"}{"_msg_catalog"}
Truth table66%2/3
A //= B
A= $cd->{"_msg_catalog"}B= $cd->{"outer_cd"}{"_msg_catalog"}
ABresult
1X1
011
000
51 5046 ▶ $cd->{_msg_catalog} //= {};
Condition100%2/2
$cd->{"_msg_catalog"} //= {}
l!l
3174729
MC/DC100%1/1
$cd->{"_msg_catalog"} //= {}
$cd->{"_msg_catalog"}
Truth table100%2/2
A
A= $cd->{"_msg_catalog"}
Aresult
11
00
52 }
53 5070 $cd;
54 }
55  
56 4 ▶sub expr {
Subroutine expr: called CC=1 SCAR=0.0
Pod undocumented
57 4 my ($self, $cd, $expr) = @_;
58  
59 # for now we dump expression as is. we should probably parse it first to
60 # localize number, e.g. "1.1 + 2" should become "1,1 + 2" in id_ID.
61  
62 # XXX for nicer output, perhaps say "the expression X" instead of just "X",
63 # especially if X has a variable or rather complex.
64 4 $expr;
65 }
66  
67 12821 ▶sub literal {
Subroutine literal: called CC=2 SCAR=6.9
Pod undocumented
68 12821 my ($self, $val) = @_;
69  
70 12821 ▶ return $val unless ref($val);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $val 6773 6048 12821
71 6048 dmp($val);
72 }
73  
74 # translate
75 98035 ▶sub _xlt {
Subroutine _xlt: called CC=4 SCAR=14.9
76 98035 my ($self, $cd, $text) = @_;
77  
78 98035 my $lang = $cd->{args}{lang};
79  
80 #$log->tracef("translating text '%s' to '%s'", $text, $lang);
81  
82 98035 ▶ return $text if $lang eq 'en_US';
Branch: 1 branch
BranchTrueFalseTotal
if $lang eq "en_US" 97999 36 98035
83 36 my $translations;
84 {
85 25 ▶ no strict 'refs'; ## no critic: TestingAndDebugging::ProhibitNoStrict
Subroutine BEGIN: called CC=1 SCAR=0.0
86 36 $translations = \%{"Data::Sah::Lang::$lang\::translations"};
87 }
88 36 ▶ return $translations->{$text} if defined($translations->{$text});
Branch: 1 branch
BranchTrueFalseTotal
if defined $translations->{$text} 36 0 36
89 0 ▶ if ($cd->{args}{mark_missing_translation}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cd->{'args'}{'mark_missing_translation'}) { } 0 0 0
90 0 return "(no $lang text:$text)";
91 } else {
92 0 return $text;
93 }
94 }
95  
96 # ($cd, 3, "element") -> "3rd element"
97 62 ▶sub _ordinate {
Subroutine _ordinate: called CC=2 SCAR=6.9
98 62 my ($self, $cd, $n, $noun) = @_;
99  
100 62 my $lang = $cd->{args}{lang};
101  
102 # we assume _xlt() has been called (and thus the appropriate
103 # Data::Sah::Lang::* has been loaded)
104  
105 62 ▶ if ($lang eq 'en_US') {
Branch: 1 branch
BranchTrueFalseTotal
if ($lang eq 'en_US') { } 61 1 62
106 61 require Lingua::EN::Numbers::Ordinate;
107 61 return Lingua::EN::Numbers::Ordinate::ordinate($n) . " $noun";
108 } else {
109 25 ▶ no strict 'refs'; ## no critic: TestingAndDebugging::ProhibitNoStrict
Subroutine BEGIN: called CC=1 SCAR=0.0
110 1 return "Data::Sah::Lang::$lang\::ordinate"->($n, $noun);
111 }
112 }
113  
114 9589 ▶sub _add_ccl {
Subroutine _add_ccl: called CC=42 SCAR=38.9
115 9589 my ($self, $cd, $ccl) = @_;
116 #$log->errorf("TMP: add_ccl %s", $ccl);
117  
118 9589 ▶ $ccl->{xlt} //= 1;
Condition100%2/2
$ccl->{"xlt"} //= 1
l!l
39586
MC/DC100%1/1
$ccl->{"xlt"} //= 1
$ccl->{"xlt"}
Truth table100%2/2
A
A= $ccl->{"xlt"}
Aresult
11
00
119  
120 9589 ▶ my $clause = $cd->{clause} // "";
Condition100%2/2
$cd->{"clause"} // ""
l!l
45485041
MC/DC100%1/1
$cd->{"clause"} // ""
$cd->{"clause"}
Truth table100%2/2
A
A= $cd->{"clause"}
Aresult
11
00
121 9589 ▶ $ccl->{type} //= "clause";
Condition100%2/2
$ccl->{"type"} //= "clause"
l!l
53124277
MC/DC100%1/1
$ccl->{"type"} //= "clause"
$ccl->{"type"}
Truth table100%2/2
A
A= $ccl->{"type"}
Aresult
11
00
122  
123 9589 my $do_xlt = 1;
124  
125 my $hvals = {
126 modal_verb => $self->_xlt($cd, "must"),
127 modal_verb_neg => $self->_xlt($cd, "must not"),
128  
129 # so they can overriden through hash_values
130 field => $self->_xlt($cd, "field"),
131 fields => $self->_xlt($cd, "fields"),
132  
133 9589 ▶ %{ $cd->{args}{hash_values} // {} },
Condition50%1/2
$cd->{"args"}{"hash_values"} // {}
l!l
09589
MC/DC0%0/1
$cd->{"args"}{"hash_values"} // {}
$cd->{"args"}{"hash_values"}
Truth table50%1/2
A
A= $cd->{"args"}{"hash_values"}
Aresult
11
00
134 };
135 9589 my $mod="";
136  
137 # is .human for desired language specified? if yes, use that instead
138  
139 {
140 9589 my $lang = $cd->{args}{lang};
141 9589 ▶ my $dlang = $cd->{clset_dlang} // "en_US"; # undef if not in clause
Condition100%2/2
$cd->{"clset_dlang"} // "en_US"
l!l
45485041
MC/DC100%1/1
$cd->{"clset_dlang"} // "en_US"
$cd->{"clset_dlang"}
Truth table100%2/2
A
A= $cd->{"clset_dlang"}
Aresult
11
00
142 9589 ▶ my $suffix = $lang eq $dlang ? "" : ".alt.lang.$lang";
Branch: 1 branch
BranchTrueFalseTotal
$lang eq $dlang ? : 9585 4 9589
143 9589 ▶ if ($clause) {
Branch: 1 branch
BranchTrueFalseTotal
if ($clause) { } 4548 5041 9589
144 4548 delete $cd->{uclset}{$_} for
145 261 grep {/\A\Q$clause.human\E(\.|\z)/} keys %{$cd->{uclset}};
146 4548 ▶ if (defined $cd->{clset}{"$clause.human$suffix"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cd->{"clset"}{"$clause.human$suffix"}) 0 4548 4548
147 0 $ccl->{type} = 'clause';
148 0 $ccl->{fmt} = $cd->{clset}{"$clause.human$suffix"};
149 0 goto FILL_FORMAT;
150 }
151 } else {
152 5041 delete $cd->{uclset}{$_} for
153 0 grep {/\A\.name(\.|\z)/} keys %{$cd->{uclset}};
154 5041 ▶ if (defined $cd->{clset}{".name$suffix"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cd->{"clset"}{".name$suffix"}) 0 5041 5041
155 0 $ccl->{type} = 'noun';
156 0 $ccl->{fmt} = $cd->{clset}{".name$suffix"};
157 0 $ccl->{vals} = undef;
158 0 goto FILL_FORMAT;
159 }
160 }
161 }
162  
163 9589 ▶ goto TRANSLATE unless $clause;
Branch: 1 branch
BranchTrueFalseTotal
unless $clause 5041 4548 9589
164  
165 4548 my $ie = $cd->{cl_is_expr};
166 4548 my $im = $cd->{cl_is_multi};
167 4548 ▶ my $op = $cd->{cl_op} // "";
Condition100%2/2
$cd->{"cl_op"} // ""
l!l
24222126
MC/DC100%1/1
$cd->{"cl_op"} // ""
$cd->{"cl_op"}
Truth table100%2/2
A
A= $cd->{"cl_op"}
Aresult
11
00
168 4548 my $cv = $cd->{clset}{$clause};
169 4548 ▶ my $vals = $ccl->{vals} // [$cv];
Condition100%2/2
$ccl->{"vals"} // [$cv]
l!l
15203028
MC/DC100%1/1
$ccl->{"vals"} // [$cv]
$ccl->{"vals"}
Truth table100%2/2
A
A= $ccl->{"vals"}
Aresult
11
00
170  
171 # handle .is_expr
172  
173 4548 ▶ if ($ie) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ie) 4 4544 4548
174 4 ▶ if (!$ccl->{expr}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ccl->{"expr"}) 0 4 4
175 0 ▶ $ccl->{fmt} = "($clause -> %s" . ($op ? " op=$op" : "") . ")";
Branch: 1 branch
BranchTrueFalseTotal
$op ? : 0 0 0
176 0 $do_xlt = 0;
177 0 $vals = [$self->expr($cd, $vals)];
178 }
179 4 goto ERR_LEVEL;
180 }
181  
182 # handle .op
183  
184 4544 ▶ if ($op eq 'not') {
Branch: 4 branches
BranchTrueFalseTotal
if ($op eq 'not') { } 305 4239 4544
elsif ($im and $op eq 'and') { } 559 3680 4239
elsif ($im and $op eq 'or') { } 558 3122 3680
elsif ($im and $op eq 'none') { } 270 2852 3122
185 ($hvals->{modal_verb}, $hvals->{modal_verb_neg}) =
186 305 ($hvals->{modal_verb_neg}, $hvals->{modal_verb});
187 305 $vals = [map {$self->literal($_)} @$vals];
188 ▶ } elsif ($im && $op eq 'and') {
Condition100%3/3
$im and $op eq "and"
!ll&&!rl&&r
2852828559
MC/DC100%2/2
$im and $op eq "and"
$im $op eq "and"
Truth table100%3/3
A and B
A= $imB= $op eq "and"
ABresult
0X0
100
111
189 559 ▶ if (@$cv == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$cv == 2) { } 450 109 559
190 450 $vals = [sprintf($self->_xlt($cd, "%s and %s"),
191 $self->literal($cv->[0]),
192 $self->literal($cv->[1]))];
193 } else {
194 109 $vals = [sprintf($self->_xlt($cd, "all of %s"),
195 $self->literal($cv))];
196 }
197 ▶ } elsif ($im && $op eq 'or') {
Condition100%3/3
$im and $op eq "or"
!ll&&!rl&&r
2852270558
MC/DC100%2/2
$im and $op eq "or"
$im $op eq "or"
Truth table100%3/3
A and B
A= $imB= $op eq "or"
ABresult
0X0
100
111
198 558 ▶ if (@$cv == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$cv == 2) { } 449 109 558
199 449 $vals = [sprintf($self->_xlt($cd, "%s or %s"),
200 $self->literal($cv->[0]),
201 $self->literal($cv->[1]))];
202 } else {
203 109 $vals = [sprintf($self->_xlt($cd, "one of %s"),
204 $self->literal($cv))];
205 }
206 ▶ } elsif ($im && $op eq 'none') {
Condition66%2/3
$im and $op eq "none"
!ll&&!rl&&r
28520270
MC/DC50%1/2
$im and $op eq "none"
$im $op eq "none"
Truth table66%2/3
A and B
A= $imB= $op eq "none"
ABresult
0X0
100
111
207 ($hvals->{modal_verb}, $hvals->{modal_verbneg}) =
208 270 ($hvals->{modal_verb_neg}, $hvals->{modal_verb});
209 270 ▶ if (@$cv == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$cv == 2) { } 216 54 270
210 216 $vals = [sprintf($self->_xlt($cd, "%s nor %s"),
211 $self->literal($cv->[0]),
212 $self->literal($cv->[1]))];
213 } else {
214 54 $vals = [sprintf($self->_xlt($cd, "any of %s"),
215 $self->literal($cv))];
216 }
217 } else {
218 2852 $vals = [map {$self->literal($_)} @$vals];
219 }
220  
221 ERR_LEVEL:
222  
223 # handle .err_level
224 4548 ▶ if ($ccl->{type} eq 'clause' && grep { $_ eq 'constraint' } @{ $cd->{cl_meta}{tags} // [] }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ccl->{"type"} eq "clause" and grep {$_ eq "constraint";} @{$cd->{"cl_meta"}{"tags"} // [];}) 4187 361 4548
Condition80%4/5
$ccl->{"type"} eq "clause" and grep {$_ eq "constraint";} @{$cd->{"cl_meta"}{"tags"} // [];}
!ll&&!rl&&r
271904187
$cd->{"cl_meta"}{"tags"} // []
l!l
42770
MC/DC100%2/2
$ccl->{"type"} eq "clause" and grep {$_ eq "constraint";} @{$cd->{"cl_meta"}{"tags"} // [];}
$ccl->{"type"} eq "clause" grep {$_ eq "constraint";} @{$cd->{"cl_meta"}{"tags"} // [];}
MC/DC0%0/1
$cd->{"cl_meta"}{"tags"} // []
$cd->{"cl_meta"}{"tags"}
Truth table100%3/3
A and B
A= $ccl->{"type"} eq "clause"B= grep {$_ eq "constraint";} @{$cd->{"cl_meta"}{"tags"} // [];}
ABresult
0X0
100
111
Truth table50%1/2
A
A= $cd->{"cl_meta"}{"tags"}
Aresult
11
00
225 4187 ▶ if (($cd->{clset}{"$clause.err_level"}//'error') eq 'warn') {
Branch: 1 branch
BranchTrueFalseTotal
if (($cd->{"clset"}{"$clause.err_level"} // "error") eq "warn") 54 4133 4187
Condition100%2/2
$cd->{"clset"}{"$clause.err_level"} // "error"
l!l
544133
MC/DC100%1/1
$cd->{"clset"}{"$clause.err_level"} // "error"
$cd->{"clset"}{"$clause.err_level"}
Truth table100%2/2
A
A= $cd->{"clset"}{"$clause.err_level"}
Aresult
11
00
226 54 ▶ if ($op eq 'not') {
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq 'not') { } 0 54 54
227 0 $hvals->{modal_verb} = $self->_xlt($cd, "should not");
228 0 $hvals->{modal_verb_neg} = $self->_xlt($cd, "should");
229 } else {
230 54 $hvals->{modal_verb} = $self->_xlt($cd, "should");
231 54 $hvals->{modal_verb_neg} = $self->_xlt($cd, "should not");
232 }
233 }
234 }
235 4548 delete $cd->{uclset}{"$clause.err_level"};
236  
237 TRANSLATE:
238  
239 9589 ▶ if ($ccl->{xlt}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ccl->{"xlt"}) 9586 3 9589
240 9586 ▶ if (ref($ccl->{fmt}) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $ccl->{'fmt'} eq 'ARRAY') { } 5041 4545 9586
elsif (not ref $ccl->{'fmt'}) { } 4545 0 4545
241 5041 $ccl->{fmt} = [map {$self->_xlt($cd, $_)} @{$ccl->{fmt}}];
242 } elsif (!ref($ccl->{fmt})) {
243 4545 $ccl->{fmt} = $self->_xlt($cd, $ccl->{fmt});
244 }
245 }
246  
247 FILL_FORMAT:
248  
249 9589 ▶ if (ref($ccl->{fmt}) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $ccl->{'fmt'} eq 'ARRAY') { } 5041 4548 9589
elsif (not ref $ccl->{'fmt'}) { } 4548 0 4548
250 10082 ▶ $ccl->{text} = [map {sprintfn($_, (map {$_//""} ($hvals, @$vals)))}
Condition50%1/2
$_ // ""
l!l
100820
MC/DC0%0/1
$_ // ""
$_
Truth table50%1/2
A
A= $_
Aresult
11
00
251 5041 @{$ccl->{fmt}}];
252 } elsif (!ref($ccl->{fmt})) {
253 4548 ▶ $ccl->{text} = sprintfn($ccl->{fmt}, (map {$_//""} ($hvals, @$vals)));
Condition100%2/2
$_ // ""
l!l
102026
MC/DC100%1/1
$_ // ""
$_
Truth table100%2/2
A
A= $_
Aresult
11
00
254 }
255 9589 ▶ delete $ccl->{fmt} unless $cd->{args}{debug};
Branch: 1 branch
BranchTrueFalseTotal
unless $cd->{"args"}{"debug"} 9589 0 9589
256  
257 PUSH:
258 9589 push @{$cd->{ccls}}, $ccl;
259  
260 9589 $self->_add_msg_catalog($cd, $ccl);
261 }
262  
263 # add a compiled clause (ccl), which will be combined at the end of compilation
264 # to be the final result. args is a hashref with these keys:
265 #
266 # * type* - str (default 'clause'). either 'noun', 'clause', 'list' (bulleted
267 # list, a clause followed by a list of items, each of them is also a ccl)
268 #
269 # * fmt* - str/2-element array. human text which can be used as the first
270 # argument to sprintf. string. if type=noun, can be a two-element arrayref to
271 # contain singular and plural version of noun.
272 #
273 # * expr - bool. fmt can handle .is_expr=1. for example, 'len=' => '1+1' can be
274 # compiled into 'length must be 1+1'. other clauses cannot handle expression,
275 # e.g. 'between=' => '[2, 2*2]'. this clause will be using the generic message
276 # 'between must [2, 2*2]'
277 #
278 # * vals - arrayref (default [clause value]). values to fill fmt with.
279 #
280 # * items - arrayref. required if type=list. a single ccl or a list of ccls.
281 #
282 # * xlt - bool (default 1). set to 0 if fmt has been translated, and should not
283 # be translated again.
284 #
285 # add_ccl() is called by clause handlers and handles using .human, translating
286 # fmt, sprintf(fmt, vals) into 'text', .err_level (adding 'must be %s', 'should
287 # not be %s'), .is_expr, .op.
288 9589 ▶sub add_ccl {
Subroutine add_ccl: called CC=7 SCAR=31.5
Pod undocumented
289 9589 my ($self, $cd, @ccls) = @_;
290  
291 9589 ▶ my $op = $cd->{cl_op} // '';
Condition100%2/2
$cd->{"cl_op"} // ""
l!l
24227167
MC/DC100%1/1
$cd->{"cl_op"} // ""
$cd->{"cl_op"}
Truth table100%2/2
A
A= $cd->{"cl_op"}
Aresult
11
00
292  
293 9589 my $ccl;
294 9589 ▶ if (@ccls == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ccls == 1) { } 9589 0 9589
295 9589 $self->_add_ccl($cd, $ccls[0]);
296 } else {
297 0 my $inner_cd = $self->init_cd(outer_cd => $cd);
298 0 $inner_cd->{args} = $cd->{args};
299 0 $inner_cd->{clause} = $cd->{clause};
300 0 for (@ccls) {
301 0 $self->_add_ccl($inner_cd, $_);
302 }
303  
304 $ccl = {
305 type => 'list',
306 vals => [],
307 items => $inner_cd->{ccls},
308 0 multi => 0,
309 };
310 0 ▶ if ($op eq 'or') {
Branch: 3 branches
BranchTrueFalseTotal
if ($op eq 'or') { } 0 0 0
elsif ($op eq 'and') { } 0 0 0
elsif ($op eq 'none') { } 0 0 0
311 0 $ccl->{fmt} = 'any of the following %(modal_verb)s be true';
312 } elsif ($op eq 'and') {
313 0 $ccl->{fmt} = 'all of the following %(modal_verb)s be true';
314 } elsif ($op eq 'none') {
315 0 $ccl->{fmt} = 'none of the following %(modal_verb)s be true';
316 # or perhaps, fmt = 'All of the following ...' but set op to 'not'?
317 }
318 0 $self->_add_ccl($cd, $ccl);
319 }
320 }
321  
322 # format ccls to form final result. at the end of compilation, we have a tree of
323 # ccls. this method accept a single ccl (of type either noun/clause) or an array
324 # of ccls (which it will join together).
325 10822 ▶sub format_ccls {
Subroutine format_ccls: called CC=8 SCAR=20.9
Pod undocumented
326 10822 my ($self, $cd, $ccls) = @_;
327  
328 # used internally to determine if the result is a single noun, in which case
329 # when format is inline_err_text, we add 'Not of type '. XXX: currently this
330 # is the wrong way to count? we shouldn't count children? perhaps count from
331 # msg_catalog instead?
332 10822 local $cd->{_fmt_noun_count} = 0;
333 10822 local $cd->{_fmt_etc_count} = 0;
334  
335 10822 my $f = $cd->{args}{format};
336 10822 my $res;
337 10822 ▶ if ($f eq 'inline_text' || $f eq 'inline_err_text' || $f eq 'msg_catalog') {
Branch: 1 branch
BranchTrueFalseTotal
if ($f eq 'inline_text' or $f eq 'inline_err_text' or $f eq 'msg_catalog') { } 10822 0 10822
Condition83%5/6
$f eq "inline_text" or $f eq "inline_err_text" or $f eq "msg_catalog"
l!l&&r!l&&!r
577950430
$f eq "inline_text" or $f eq "inline_err_text"
l!l&&r!l&&!r
2457555043
MC/DC0%0/3
$f eq "inline_text" or $f eq "inline_err_text" or $f eq "msg_catalog"
$f eq "inline_text" $f eq "inline_err_text" $f eq "msg_catalog"
Truth table75%3/4
A or B or C
A= $f eq "inline_text"B= $f eq "inline_err_text"C= $f eq "msg_catalog"
ABCresult
1XX1
01X1
0011
0000
338 10822 $res = $self->_format_ccls_itext($cd, $ccls);
339 10822 ▶ if ($f eq 'inline_err_text') {
Branch: 1 branch
BranchTrueFalseTotal
if ($f eq "inline_err_text") 5755 5067 10822
340 #$log->errorf("TMP: noun=%d, etc=%d", $cd->{_fmt_noun_count}, $cd->{_fmt_etc_count});
341 5755 ▶ if ($cd->{_fmt_noun_count} == 1 && $cd->{_fmt_etc_count} == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($cd->{'_fmt_noun_count'} == 1 and $cd->{'_fmt_etc_count'} == 0) { } 221 5534 5755
elsif (not $cd->{'_fmt_noun_count'}) { } 5396 138 5534
Condition100%3/3
$cd->{"_fmt_noun_count"} == 1 and $cd->{"_fmt_etc_count"} == 0
!ll&&!rl&&r
5434100221
MC/DC100%2/2
$cd->{"_fmt_noun_count"} == 1 and $cd->{"_fmt_etc_count"} == 0
$cd->{"_fmt_noun_count"} == 1 $cd->{"_fmt_etc_count"} == 0
Truth table100%3/3
A and B
A= $cd->{"_fmt_noun_count"} == 1B= $cd->{"_fmt_etc_count"} == 0
ABresult
0X0
100
111
342 # a single noun (type name), we should add some preamble
343 221 $res = sprintf(
344 $self->_xlt($cd, "Not of type %s"),
345 $res
346 );
347 } elsif (!$cd->{_fmt_noun_count}) {
348 # a clause (e.g. "must be >= 10"), already looks like errmsg
349 } else {
350 # a noun + clauses (e.g. "integer, must be even"). add preamble
351 138 $res = sprintf(
352 $self->_xlt(
353 $cd, "Does not satisfy the following schema: %s"),
354 $res
355 );
356 }
357 }
358 } else {
359 0 $res = $self->_format_ccls_markdown($cd, $ccls);
360 }
361 10822 $res;
362 }
363  
364 21957 ▶sub _format_ccls_itext {
Subroutine _format_ccls_itext: called CC=11 SCAR=24.0
365 21957 my ($self, $cd, $ccls) = @_;
366  
367 21957 local $cd->{args}{mark_missing_translation} = 0;
368 21957 my $c_comma = $self->_xlt($cd, ", ");
369  
370 21957 ▶ if (ref($ccls) eq 'HASH' && $ccls->{type} =~ /^(noun|clause)$/) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $ccls eq 'HASH' and $ccls->{'type'} =~ /^(noun|clause)$/) { } 15703 6254 21957
elsif (ref $ccls eq 'HASH' and $ccls->{'type'} eq 'list') { } 551 5703 6254
elsif (ref $ccls eq 'ARRAY') { } 5703 0 5703
Condition100%3/3
ref $ccls eq "HASH" and $ccls->{"type"} =~ /^(noun|clause)$/
!ll&&!rl&&r
570355115703
MC/DC100%2/2
ref $ccls eq "HASH" and $ccls->{"type"} =~ /^(noun|clause)$/
ref $ccls eq "HASH" $ccls->{"type"} =~ /^(noun|clause)$/
Truth table100%3/3
A and B
A= ref $ccls eq "HASH"B= $ccls->{"type"} =~ /^(noun|clause)$/
ABresult
0X0
100
111
371 15703 ▶ if ($ccls->{type} eq 'noun') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ccls->{'type'} eq 'noun') { } 5731 9972 15703
372 5731 $cd->{_fmt_noun_count}++;
373 } else {
374 9972 $cd->{_fmt_etc_count}++;
375 }
376 # handle a single noun/clause ccl
377 15703 my $ccl = $ccls;
378 15703 ▶ return ref($ccl->{text}) eq 'ARRAY' ? $ccl->{text}[0] : $ccl->{text};
Branch: 1 branch
BranchTrueFalseTotal
ref $ccl->{'text'} eq 'ARRAY' ? : 5722 9981 15703
379 ▶ } elsif (ref($ccls) eq 'HASH' && $ccls->{type} eq 'list') {
Condition66%2/3
ref $ccls eq "HASH" and $ccls->{"type"} eq "list"
!ll&&!rl&&r
57030551
MC/DC50%1/2
ref $ccls eq "HASH" and $ccls->{"type"} eq "list"
ref $ccls eq "HASH" $ccls->{"type"} eq "list"
Truth table66%2/3
A and B
A= ref $ccls eq "HASH"B= $ccls->{"type"} eq "list"
ABresult
0X0
100
111
380 # handle a single list ccl
381 551 my $c_openpar = $self->_xlt($cd, "(");
382 551 my $c_closepar = $self->_xlt($cd, ")");
383 551 my $c_colon = $self->_xlt($cd, ": ");
384 551 my $ccl = $ccls;
385  
386 551 my $txt = $ccl->{text}; $txt =~ s/\s+$//;
387 551 my @t = ($txt, $c_colon);
388 551 my $i = 0;
389 551 for (@{ $ccl->{items} }) {
390 612 ▶ push @t, $c_comma if $i;
Branch: 1 branch
BranchTrueFalseTotal
if $i 61 551 612
391 612 my $it = $self->_format_ccls_itext($cd, $_);
392 612 ▶ if ($it =~ /\Q$c_comma/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($it =~ /\Q$c_comma\E/) { } 284 328 612
393 284 push @t, $c_openpar, $it, $c_closepar;
394 } else {
395 328 push @t, $it;
396 }
397 612 $i++;
398 }
399 551 return join("", @t);
400 } elsif (ref($ccls) eq 'ARRAY') {
401 # handle an array of ccls
402 5703 return join($c_comma, map {$self->_format_ccls_itext($cd, $_)} @$ccls);
403 } else {
404 0 $self->_die($cd, "Can't format $ccls");
405 }
406 }
407  
408 0 ▶sub _format_ccls_markdown {
Subroutine _format_ccls_markdown: not called CC=1 SCAR=6.9
409 0 my ($self, $cd, $ccls) = @_;
410  
411 0 $self->_die($cd, "Sorry, markdown not yet implemented");
412 }
413  
414 5070 ▶sub _load_lang_modules {
Subroutine _load_lang_modules: called CC=9 SCAR=23.5
415 5070 my ($self, $cd) = @_;
416  
417 5070 my $lang = $cd->{args}{lang};
418 5070 ▶ die "Invalid language '$lang', please use letters only"
Branch: 1 branch
BranchTrueFalseTotal
unless $lang =~ /\A\w+\z/ 0 5070 5070
419 unless $lang =~ /\A\w+\z/;
420  
421 5070 my @modp;
422 5070 ▶ unless ($lang eq 'en_US') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($lang eq "en_US") 3 5067 5070
423 3 push @modp, "Data/Sah/Lang/$lang.pm";
424 3 ▶ for my $cl (@{ $typex{$cd->{type}} // []}) {
Condition50%1/2
$Data::Sah::Compiler::human::typex{$cd->{"type"}} // []
l!l
03
MC/DC0%0/1
$Data::Sah::Compiler::human::typex{$cd->{"type"}} // []
$Data::Sah::Compiler::human::typex{$cd->{"type"}}
Truth table50%1/2
A
A= $Data::Sah::Compiler::human::typex{$cd->{"type"}}
Aresult
11
00
425 0 my $modp = "Data/Sah/Lang/$lang/TypeX/$cd->{type}/$cl.pm";
426 0 $modp =~ s!::!/!g; # $cd->{type} might still contain '::'
427 0 push @modp, $modp;
428 }
429 }
430 5070 my $i;
431 5070 for my $modp (@modp) {
432 3 $i++;
433 3 ▶ unless (exists $INC{$modp}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $INC{$modp}) 1 2 3
434 1 ▶ if ($i == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == 1) 1 0 1
435 # test to check whether Data::Sah::Lang::$lang exists. if it
436 # does not, we fallback to en_US.
437 1 require Module::Installed::Tiny;
438 1 ▶ if (!Module::Installed::Tiny::module_installed($modp)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (Module::Installed::Tiny::module_installed($modp)) 0 1 1
439 #$log->debug("$mod cannot be found, falling back to en_US");
440 0 $cd->{args}{lang} = 'en_US';
441 0 last;
442 }
443 }
444 #$log->trace("Loading $modp ...");
445 1 require $modp;
446  
447 # negative-cache, so we don't have to try again
448 1 $INC{$modp} = undef;
449 }
450 }
451 }
452  
453 5070 ▶sub before_compile {
Subroutine before_compile: called CC=4 SCAR=16.4
Pod documented
454 5070 my ($self, $cd) = @_;
455  
456 # set locale so that numbers etc are printed according to locale (e.g.
457 # sprintf("%s", 1.2) prints '1,2' in id_ID).
458 5070 $cd->{_orig_locale} = setlocale(LC_ALL);
459  
460 # XXX do we need to set everything? LC_ADDRESS, LC_TELEPHONE, LC_PAPER, ...
461 5070 ▶ my $res = setlocale(LC_ALL, $cd->{args}{locale} // $cd->{args}{lang});
Condition33%1/3
$cd->{"args"}{"locale"} // $cd->{"args"}{"lang"}
l!l&&r!l&&!r
050700
MC/DC0%0/2
$cd->{"args"}{"locale"} // $cd->{"args"}{"lang"}
$cd->{"args"}{"locale"} $cd->{"args"}{"lang"}
Truth table33%1/3
A // B
A= $cd->{"args"}{"locale"}B= $cd->{"args"}{"lang"}
ABresult
1X1
011
000
462 warn "Unsupported locale $cd->{args}{lang}"
463 5070 ▶ if $cd->{args}{debug} && !defined($res);
Branch: 1 branch
BranchTrueFalseTotal
if $cd->{"args"}{"debug"} and not defined $res 0 5070 5070
Condition33%1/3
$cd->{"args"}{"debug"} and not defined $res
!ll&&!rl&&r
507000
MC/DC0%0/2
$cd->{"args"}{"debug"} and not defined $res
$cd->{"args"}{"debug"} not defined $res
Truth table33%1/3
A and not(B)
A= $cd->{"args"}{"debug"}B= not defined $res
ABresult
0X0
100
111
464 }
465  
466 5070 ▶sub before_handle_type {
Subroutine before_handle_type: called CC=1 SCAR=0.0
Pod documented
467 5070 my ($self, $cd) = @_;
468  
469 5070 $self->_load_lang_modules($cd);
470 }
471  
472 5324 ▶sub before_clause {
Subroutine before_clause: called CC=1 SCAR=0.0
Pod documented
473 5324 my ($self, $cd) = @_;
474  
475 # by default, human clause handler can handle multiple values (e.g.
476 # "div_by&"=>[2, 3] becomes "must be divisible by 2 and 3" instead of having
477 # to be ["must be divisible by 2", "must be divisible by 3"]. some clauses
478 # that don't can override this value to 0.
479 5324 $cd->{CLAUSE_DO_MULTI} = 1;
480 }
481  
482 5321 ▶sub after_clause {
Subroutine after_clause: called CC=1 SCAR=0.0
Pod documented
483 5321 my ($self, $cd) = @_;
484  
485 # reset what we set in before_clause()
486 5321 delete $cd->{CLAUSE_DO_MULTI};
487 }
488  
489 5067 ▶sub after_all_clauses {
Subroutine after_all_clauses: called CC=1 SCAR=0.0
Pod documented
490 5067 my ($self, $cd) = @_;
491  
492 # quantify NOUN (e.g. integer) into 'required integer', 'optional integer',
493 # or 'forbidden integer'.
494  
495 # my $q;
496 # if (!$cd->{clset}{'required.is_expr'} &&
497 # !(grep {$_ eq 'required'} @{ $cd->{args}{skip_clause} })) {
498 # if ($cd->{clset}{required}) {
499 # $q = 'required %s';
500 # } else {
501 # $q = 'optional %s';
502 # }
503 # } elsif ($cd->{clset}{forbidden} && !$cd->{clset}{'forbidden.is_expr'} &&
504 # !(grep { $_ eq 'forbidden' } @{ $cd->{args}{skip_clause} })) {
505 # $q = 'forbidden %s';
506 # }
507 # if ($q && @{$cd->{ccls}} && $cd->{ccls}[0]{type} eq 'noun') {
508 # $q = $self->_xlt($cd, $q);
509 # for (ref($cd->{ccls}[0]{text}) eq 'ARRAY' ?
510 # @{ $cd->{ccls}[0]{text} } : $cd->{ccls}[0]{text}) {
511 # $_ = sprintf($q, $_);
512 # }
513 # }
514  
515 5067 $cd->{result} = $self->format_ccls($cd, $cd->{ccls});
516 }
517  
518 5067 ▶sub after_compile {
Subroutine after_compile: called CC=2 SCAR=6.9
Pod documented
519 5067 my ($self, $cd) = @_;
520  
521 5067 setlocale(LC_ALL, $cd->{_orig_locale});
522  
523 5067 ▶ if ($cd->{args}{format} eq 'msg_catalog') {
Branch: 1 branch
BranchTrueFalseTotal
if ($cd->{"args"}{"format"} eq "msg_catalog") 5043 24 5067
524 5043 $cd->{result} = $cd->{_msg_catalog};
525 }
526 }
527  
528 25 1;
529 # ABSTRACT: Compile Sah schema to human language
530  
531 __END__
« Data/Sah/Compiler.pm ↑ Summary Data/Sah/Compiler/human/TH/HasElems.pm »