blib/lib/MARC/Spec/Parser.pm

filter: Statementstmt 93.0% 173 / 186 Branchbran 77.6% 59 / 76 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 4 / 4 Subroutinesub 100% 17 / 17 Podpod 0.0% 0 / 2 totaltotal 89.0% 259 / 291 SCAR 38.9
CC
46
Cov
89%
_spec_context
19.9
_match_field
12.0
_match_subterms
10.3
CRAP 49.0
↑ Summary blib/lib/MARC/Spec/Structure.pm »
1 package MARC::Spec::Parser;
2
3 13 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use Const::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 13 ▶use MARC::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 13 ▶use MARC::Spec::Field;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 13 require MARC::Spec::Subfield;
9 13 require MARC::Spec::Comparisonstring;
10 13 require MARC::Spec::Subspec;
11 13 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
12
13 13 our $VERSION = '1.0.0';
14
15 13 has spec => (
16 is => 'rw',
17 required => 1
18 );
19
20 13 has marcspec => (
21 is => 'rwp'
22 );
23
24 13 const my $FIELDTAG => q{^(?<tag>(?:[a-z0-9\.]{3,3}|[A-Z0-9\.]{3,3}|[0-9\.]{3,3}))?};
25 13 const my $POSITIONORRANGE => q{(?:(?:(?:[0-9]+|#)\-(?:[0-9]+|#))|(?:[0-9]+|#))};
26 13 const my $INDEX => qq{(?:\\[(?<index>$POSITIONORRANGE)\\])?};
27 13 const my $CHARPOS => qq{\\/(?<charpos>$POSITIONORRANGE)};
28 13 const my $INDICATORS => q{_(?<indicators>(?:[_a-z0-9][_a-z0-9]{0,1}))};
29 13 const my $SUBSPECS => q{(?<subspecs>(?:\\{.+?(?<!(?<!(\$|\\\))(\$|\\\))\\})+)?};
30 13 const my $SUBFIELDS => q{(?<subfields>\$.+)?};
31 13 const my $FIELD => qr/(?<field>(?:$FIELDTAG$INDEX(?:$CHARPOS|$INDICATORS)?$SUBSPECS$SUBFIELDS))/s;
32 13 const my $SUBFIELDRANGE => q{(?<range>(?:[0-9a-z]\-[0-9a-z]))};
33 13 const my $SUBFIELDTAG => q{(?<code>[\!-\?\[-\\{\\}-~])};
34 13 const my $SUBFIELD => qr/(?<subfield>\$(?:$SUBFIELDRANGE|$SUBFIELDTAG)$INDEX(?:$CHARPOS)?$SUBSPECS)/s;
35 13 const my $LEFTSUBTERM => q{^(?<left>(?:\\\(?:(?<=\\\)[\!\=\~\?]|[^\!\=\~\?])+)|(?:(?<=\$)[\!\=\~\?]|[^\!\=\~\?])+)?};
36 13 const my $OPERATOR => q{(?<operator>\!\=|\!\~|\=|\~|\!|\?)};
37 13 const my $SUBTERMS => qq{(?:$LEFTSUBTERM$OPERATOR)?(?<right>.+)}.q{$};
38 13 const my $SUBSPEC => qr/(?:\{(.+?)\})/s;
39 13 const my $UNESCAPED => qr/(?<![\\\\\$])[\{\}]/s;
40
41 13 const my $MIN_LENGTH_FIELD => 3;
42 13 const my $MIN_LENGTH_SUBFIELD => 2;
43 13 const my $NO_LENGTH => -1;
44
45 13 my %cache;
46
47 sub BUILDARGS {
48 47 ▶ my ($class, @args) = @_;
Subroutine BUILDARGS: called CC=2 SCAR=7.0
Pod undocumented
49 47 ▶ if (@args % 2 == 1) { unshift @args, "spec" }
Branch: 1 branch
BranchTrueFalseTotal
if (@args % 2 == 1) 47 0 47
50 47 return { @args };
51 }
52
53 sub BUILD {
54 47 ▶ my ($self) = @_;
Subroutine BUILD: called CC=2 SCAR=6.9
Pod undocumented
55 47 my $field = $self->_match_field();
56 37 my $ms = MARC::Spec->new($field);
57 37 ▶ if($self->{_parsed}->{subfields}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_parsed"}{"subfields"}) 23 14 37
58 23 my $subfields = $self->_match_subfields();
59 22 $ms->add_subfields($subfields);
60 }
61 36 $self->_set_marcspec($ms);
62 }
63
64 sub _match_field {
65 47 ▶ my ($self) = @_;
Subroutine _match_field: called CC=12 SCAR=24.9
66
67 47 _do_checks($self->spec, $MIN_LENGTH_FIELD);
68
69 44 $self->spec =~ $FIELD;
70
71 44 %{$self->{_parsed}} = %+;
72
73 44 ▶ if(!$self->{_parsed}->{tag}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_parsed"}{"tag"}) 2 42 44
74 2 _throw("For fieldtag only '.', digits and lowercase alphabetic or digits and upper case alphabetics characters are allowed.", $self->spec);
75 }
76
77 42 ▶ if( length $self->{_parsed}->{field} != length $self->spec ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->{"_parsed"}{"field"} != length $self->spec) 3 39 42
78 3 _throw('Detected useless data fragment or invalid field spec.', $self->spec);
79 }
80
81 # create a new Field
82 39 my $field = MARC::Spec::Field->new($self->{_parsed}->{tag});
83
84 39 ▶ if(defined $self->{_parsed}->{indicators}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self->{'_parsed'}{'indicators'}) { } 1 38 39
elsif (defined $self->{'_parsed'}{'charpos'}) { } 7 31 38
85 1 my $ind1 = substr $self->{_parsed}->{indicators}, 0, 1;
86 1 ▶ if('_' ne $ind1) { $field->indicator1($ind1) }
Branch: 1 branch
BranchTrueFalseTotal
if ("_" ne $ind1) 1 0 1
87
88 1 ▶ if(2 == length($self->{_parsed}->{indicators})) {
Branch: 1 branch
BranchTrueFalseTotal
if (2 == length $self->{"_parsed"}{"indicators"}) 1 0 1
89 1 my $ind2 = substr $self->{_parsed}->{indicators}, 1, 1;
90 1 ▶ if('_' ne $ind2) { $field->indicator2($ind2) }
Branch: 1 branch
BranchTrueFalseTotal
if ("_" ne $ind2) 1 0 1
91 }
92 } elsif(defined $self->{_parsed}->{charpos}) {
93 7 $field->set_char_start_end($self->{_parsed}->{charpos});
94 }
95
96 38 ▶ if(defined $self->{_parsed}->{index}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"_parsed"}{"index"}) 9 29 38
97 9 $field->set_index_start_end($self->{_parsed}->{index});
98 }
99
100 38 ▶ if(defined $field->char_start && defined $self->{_parsed}->{subfields}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $field->char_start and defined $self->{"_parsed"}{"subfields"}) 1 37 38
Condition100%3/3
defined $field->char_start and defined $self->{"_parsed"}{"subfields"}
!ll&&!rl&&r
3251
MC/DC100%2/2
defined $field->char_start and defined $self->{"_parsed"}{"subfields"}
defined $field->char_start defined $self->{"_parsed"}{"subfields"}
Truth table100%3/3
A and B
A= defined $field->char_startB= defined $self->{"_parsed"}{"subfields"}
ABresult
0X0
100
111
101 1 _throw("Either characterSpec for field or subfields are allowed.", $self->spec);
102 }
103
104 37 $self->{field_base} = $field->base;
105
106 37 ▶ if($self->{_parsed}->{subspecs}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_parsed"}{"subspecs"}) 3 34 37
107 3 my $field_subspecs = $self->_match_subspecs($self->{_parsed}->{subspecs});
108 3 $self->_populate_subspecs($field, $field_subspecs, [$self->{field_base}]);
109 }
110 37 return $field;
111 }
112
113 sub _populate_subspecs {
114 33 ▶ my ($self, $spec, $subspecs, $base) = @_;
Subroutine _populate_subspecs: called CC=4 SCAR=13.9
115 33 foreach my $subspec (@{$subspecs}) {
116 # check if array length is above 1
117 62 ▶ if(1 < scalar @{$subspec}) {
Branch: 1 branch
BranchTrueFalseTotal
if (1 < scalar @{$subspec;}) { } 29 33 62
118 # alternatives to array (OR)
119 29 my @or = ();
120 29 foreach my $or_subspec (@{$subspec}) {
121 58 push @or, $self->_match_subterms($or_subspec, $base);
122 }
123 29 $spec->add_subspecs([\@or]);
124 }
125 else {
126 33 $spec->add_subspec( $self->_match_subterms($subspec->[0], $base ) );
127 }
128 }
129 }
130
131 sub _match_subfields {
132 23 ▶ my ($self) = @_;
Subroutine _match_subfields: called CC=5 SCAR=16.1
133
134 23 _do_checks($self->{_parsed}->{subfields}, $MIN_LENGTH_SUBFIELD);
135
136 23 my $subfields = [];
137 23 my $i = 0;
138 23 ▶ while($self->{_parsed}->{subfields} =~ /$SUBFIELD/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_parsed"}{"subfields"} =~ /$SUBFIELD/g) 26 23 49
139 26 ▶ if(defined $+{range}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $+{'range'}) { } 1 25 26
140 1 my $from = substr $+{range},0,1;
141 1 my $to = substr $+{range},2,1;
142 1 for my $code ( $from .. $to) {
143 26 push @{$subfields}, $self->_create_subfield($code,%+);
144 }
145 } else {
146 25 push @{$subfields}, $self->_create_subfield(undef,%+);
147 }
148 26 $i++;
149 }
150
151 23 ▶ if(0 == $i) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 == $i) 1 22 23
152 1 _throw("Invalid subfield spec detected.", $self->{_parsed}->{subfields});
153 }
154
155 22 return $subfields;
156 }
157
158 sub _create_subfield {
159 51 ▶ my ($self,$code,%args) = @_;
Subroutine _create_subfield: called CC=5 SCAR=16.1
160 # create a new Subfield
161 51 ▶ my $subfield = MARC::Spec::Subfield->new($code // $args{code});
Condition100%3/3
$code // $args{"code"}
l!l&&r!l&&!r
26232
MC/DC100%2/2
$code // $args{"code"}
$code $args{"code"}
Truth table100%3/3
A // B
A= $codeB= $args{"code"}
ABresult
1X1
011
000
162
163 51 ▶ if(defined $args{index}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"index"}) 4 47 51
164 4 $subfield->set_index_start_end($args{index});
165 }
166
167 51 ▶ if(defined $args{charpos}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"charpos"}) 1 50 51
168 1 $subfield->set_char_start_end($args{charpos});
169 }
170
171 # handle subspecs
172 51 ▶ if(defined $args{subspecs}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"subspecs"}) 30 21 51
173 30 my $subfield_subspecs = $self->_match_subspecs($args{subspecs});
174 30 $self->_populate_subspecs($subfield, $subfield_subspecs, [$self->{field_base}, $subfield->base]);
175 }
176 51 return $subfield;
177 }
178
179 sub _match_subspecs {
180 33 ▶ my ($self, $subspecs) = @_;
Subroutine _match_subspecs: called CC=2 SCAR=6.9
181 33 my @subspecs;
182
183 33 foreach ($subspecs =~ /$SUBSPEC/g) {
184 62 push @subspecs, [split /(?<!\\)\|/];
185 }
186 33 return \@subspecs;
187 }
188
189 sub _match_subterms {
190 91 ▶ my ($self,$subTerms,$context) = @_;
Subroutine _match_subterms: called CC=10 SCAR=23.3
191
192 91 ▶ if($subTerms =~ $UNESCAPED) {
Branch: 1 branch
BranchTrueFalseTotal
if ($subTerms =~ /$UNESCAPED/) 0 91 91
193 0 _throw("Unescaped character detected.", $subTerms);
194 }
195
196 91 ▶ if($subTerms !~ /$SUBTERMS/sg) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($subTerms =~ /$SUBTERMS/gs) 0 91 91
197 0 _throw("Assuming invalid spec.", $subTerms);
198 }
199
200 # create a new Subspec
201 91 my $subSpec = MARC::Spec::Subspec->new;
202
203 91 foreach my $side (('left', 'right')) {
204 182 ▶ if(defined $+{$side}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $+{$side}) { } 175 7 182
elsif ($side eq 'left') { } 7 0 7
205 175 ▶ if('\\' ne substr $+{$side},0,1) {
Branch: 1 branch
BranchTrueFalseTotal
if ('\\' ne substr($+{$side}, 0, 1)) { } 91 84 175
206 91 my $spec = _spec_context($+{$side},$context);
207
208 # this prevents the spec parsed again
209 91 ▶ if($cache{$spec}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cache{$spec}) { } 79 12 91
210 79 $subSpec->$side( $cache{$spec} );
211 } else {
212 12 $subSpec->$side( MARC::Spec::Parser->new($spec)->marcspec );
213
214 12 $cache{$spec} = $subSpec->$side;
215 }
216 } else {
217 84 $subSpec->$side( MARC::Spec::Comparisonstring->new(substr $+{$side},1) );
218 }
219 } elsif($side eq 'left') {
220 7 my $spec = _spec_context(@{$context}[$#{$context}],$context);
221 7 ▶ if($cache{$spec}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cache{$spec}) { } 2 5 7
222 2 $subSpec->left( $cache{$spec} );
223 } else {
224 5 $subSpec->left( MARC::Spec::Parser->new($spec)->marcspec );
225 5 $cache{$spec} = $subSpec->left;
226 }
227 } else {
228 0 _throw("Right hand subTerm is missing.", $subTerms);
229 }
230 }
231
232 91 ▶ if(defined $+{operator}) { $subSpec->operator( $+{operator} )}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $+{"operator"}) 84 7 91
233
234 91 return $subSpec;
235 }
236
237 sub _spec_context {
238 98 ▶ my ($spec, $context) = @_;
Subroutine _spec_context: called CC=9 SCAR=29.9
239 98 my $fieldContext = @{$context}[0];
240 98 my $fullcontext = join '', @{$context};
241
242 98 ▶ if($spec eq $fullcontext) { return $spec }
Branch: 1 branch
BranchTrueFalseTotal
if ($spec eq $fullcontext) 2 96 98
243
244 96 my $firstChar = substr $spec,0,1;
245 96 ▶ if($firstChar eq '_') {
Branch: 1 branch
BranchTrueFalseTotal
if ($firstChar eq "_") 0 96 96
246 0 my $refPos = index $fullcontext, $firstChar;
247
248 0 ▶ if(0 <= $refPos) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 <= $refPos) 0 0 0
249 0 ▶ if('$' ne substr $fullcontext,$refPos - 1,1) {
Branch: 1 branch
BranchTrueFalseTotal
if ("\$" ne substr($fullcontext, $refPos - 1, 1)) 0 0 0
250 0 return substr($fullcontext,0,$refPos).$spec;
251 }
252 }
253 0 return $fullcontext.$spec;
254 }
255
256 96 ▶ if($firstChar eq '$') { return $fieldContext.$spec }
Branch: 1 branch
BranchTrueFalseTotal
if ($firstChar eq "\$") 9 87 96
257
258 87 ▶ if($firstChar =~ /\[|\//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($firstChar =~ m(\[|/)) 0 87 87
259 0 my $refPos = rindex $fullcontext, $firstChar;
260
261 0 ▶ if(0 <= $refPos) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 <= $refPos) 0 0 0
262 0 ▶ if('$' ne substr $fullcontext,$refPos - 1,1) {
Branch: 1 branch
BranchTrueFalseTotal
if ("\$" ne substr($fullcontext, $refPos - 1, 1)) 0 0 0
263 0 return substr($fullcontext,0,$refPos).$spec;
264 }
265 }
266 0 return $fullcontext.$spec;
267 }
268
269 87 return $spec;
270 }
271
272 sub _do_checks {
273 70 ▶ my ($spec, $min_length) = @_;
Subroutine _do_checks: called CC=4 SCAR=13.9
274
275 70 ▶ if(ref \$spec ne 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref \$spec ne "SCALAR") 1 69 70
276 1 _throw("Argument must be of type SCALAR.", ref \$spec);
277 }
278
279 69 ▶ if($spec =~ /\s/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec =~ /\s/s) 1 68 69
280 1 _throw("Whitespaces are not allowed.", $spec);
281 }
282
283 68 ▶ if($min_length > length $spec) {
Branch: 1 branch
BranchTrueFalseTotal
if ($min_length > length $spec) 1 67 68
284 1 _throw("Spec must be at least ".$min_length." chracters long.", $spec);
285 }
286
287 67 return;
288 }
289
290 sub _throw {
291 10 ▶ my ($message, $hint) = @_;
Subroutine _throw: called CC=1 SCAR=0.0
292 10 croak 'MARCspec Parser exception. '.$message.' Tried to parse: '.$hint;
293 }
294
295 13 1;
296 __END__
↑ Summary blib/lib/MARC/Spec/Structure.pm »