Parser.pm

filter: Statementstmt 80.6% 154 / 191 Branchbran 76.0% 70 / 92 Conditioncond 50.0% 18 / 36 MC/DCmcdc 33.3% 7 / 21 Subroutinesub 82.3% 14 / 17 Podpod 70.0% 7 / 10 totaltotal 73.5% 270 / 367 CC 63 SCAR 25.1
CC
63
Cov
76%
_parse_next
20.6
debug_hash
20.0
_split_braced_string
14.1
CRAP 12.3
« Parser/Author.pm ↑ Summary Parser/Entry.pm »
1 package BibTeX::Parser;
2 {
3 20 $BibTeX::Parser::VERSION = '1.95';
4 }
5 # ABSTRACT: A pure perl BibTeX parser
6 20 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 20 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 20 require BibTeX::Parser::Entry; # mutual dependency, so use instead of require
10  
11  
12 20 my $re_namechar = qr/[a-zA-Z0-9\!\$\&\*\+\-\.\/\:\;\<\>\?\[\]\^\_\`\|\']/o;
13 20 my $re_name = qr/$re_namechar+/o;
14  
15  
16 16 ▶sub new {
Subroutine new: called CC=2 SCAR=7.2
Pod documented
17 16 my ( $class, $fh, $opts ) = @_;
18  
19 16 ▶ return bless {
Condition50%1/2
$opts || {}
l!l
016
MC/DC0%0/1
$opts || {}
$opts
Truth table50%1/2
A
A= $opts
Aresult
11
00
20 fh => $fh,
21 opts => $opts || {},
22 strings => {
23 jan => "January",
24 feb => "February",
25 mar => "March",
26 apr => "April",
27 may => "May",
28 jun => "June",
29 jul => "July",
30 aug => "August",
31 sep => "September",
32 oct => "October",
33 nov => "November",
34 dec => "December",
35 "j-tugboat" => "TUGboat", # missing definition is irritating
36 },
37 line => -1,
38 buffer => "",
39 entries => {}
40 }, $class;
41 }
42  
43 1 ▶sub read {
Subroutine read: called CC=6 SCAR=19.0
Pod documented
44 1 my $self = shift;
45 1 ▶ if (!exists $self->{opts}->{errorlevel}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"opts"}{"errorlevel"}) 1 0 1
46 1 $self->{opts}->{errorlevel} = 'warn';
47 }
48 1 ▶ while (my $entry=$self->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $entry = $self->next) 10 1 11
49 10 ▶ if ($entry->parse_ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry->parse_ok) { } 8 2 10
50 8 $self->{entries}->{$entry->key()} = $entry;
51 } else {
52 2 ▶ if ($self->{opts}->{errorlevel} eq 'warn') {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'opts'}{'errorlevel'} eq 'warn') { } 2 0 2
elsif ($self->{'opts'}{'errorlevel'} eq 'error') { } 0 0 0
53 2 warn "BibTeX Parser: Skipping entry in line $."
54 } elsif ($self->{opts}->{errorlevel} eq 'error') {
55 0 exit("BibTeX Parser: Error in line $.")
56 }
57 }
58 }
59 }
60  
61 1 ▶sub n {
Subroutine n: called CC=1 SCAR=0.0
Pod documented
62 1 my $self = shift;
63 1 return(scalar keys %{$self->{entries}});
64 }
65  
66 1 ▶sub entrykeys {
Subroutine entrykeys: called CC=1 SCAR=0.0
Pod documented
67 1 my $self = shift;
68 1 my @result = keys %{$self->{entries}};
69 1 return(\@result);
70 }
71  
72 2 ▶sub has {
Subroutine has: called CC=1 SCAR=0.0
Pod documented
73 2 my $self = shift;
74 2 my $key = shift;
75 2 return(exists($self->{entries}->{$key}));
76 }
77  
78 1 ▶sub entry {
Subroutine entry: called CC=1 SCAR=0.0
Pod documented
79 1 my $self = shift;
80 1 my $key = shift;
81 1 return($self->{entries}->{$key});
82 }
83  
84 sub _slurp_close_bracket;
85  
86 53 ▶sub _parse_next {
Subroutine _parse_next: called CC=20 SCAR=30.3
87 53 my $self = shift;
88  
89 53 while (1) { # loop until regular entry is finished
90 57 ▶ return 0 if $self->{fh}->eof;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"fh"}->eof 6 51 57
91 51 local $_ = $self->{buffer};
92  
93 51 ▶ until (/@/m) {
Branch: 1 branch
BranchTrueFalseTotal
unless (/@/m) 136 47 183
94 136 my $line = $self->{fh}->getline;
95 136 ▶ return 0 unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 4 132 136
96 132 $line =~ s/^%.*$//;
97 132 $_ .= $line;
98 }
99  
100 47 my $current_entry = new BibTeX::Parser::Entry;
101 47 ▶ if (/@($re_name)/cgo) {
Branch: 1 branch
BranchTrueFalseTotal
if (/@($re_name)/cog) { } 45 2 47
102 45 my $type = uc $1;
103 45 $current_entry->type( $type );
104 45 my $start_pos = pos($_) - length($type) - 1;
105  
106 # read rest of entry (matches braces)
107 45 my $bracelevel = 0;
108 45 $bracelevel += tr/\{/\{/; #count braces
109 45 $bracelevel -= tr/\}/\}/;
110 45 ▶ while ( $bracelevel != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bracelevel != 0) 186 45 231
111 186 my $position = pos($_);
112 186 my $line = $self->{fh}->getline;
113 186 ▶ last unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 0 186 186
114 186 $bracelevel =
115 $bracelevel + ( $line =~ tr/\{/\{/ ) - ( $line =~ tr/\}/\}/ );
116 186 $_ .= $line;
117 186 pos($_) = $position;
118 }
119  
120 # Remember text before the entry
121 45 my $pre = substr($_, 0, $start_pos-1);
122 45 ▶ if ($start_pos == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start_pos == 0) 10 35 45
123 10 $pre = '';
124 }
125 45 $current_entry->pre($pre);
126  
127  
128 # Remember raw bibtex code
129 45 my $raw = substr($_, $start_pos);
130 45 $raw =~ s/^\s+//;
131 45 $raw =~ s/\s+$//;
132 45 $current_entry->raw_bibtex($raw);
133  
134 45 my $pos = pos $_;
135 45 tr/\n/ /;
136 45 pos($_) = $pos;
137  
138 45 ▶ if ( $type eq "STRING" ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'STRING') { } 2 43 45
elsif ($type eq 'COMMENT' or $type eq 'PREAMBLE') { } 2 41 43
139 2 ▶ if (/\G\{\s*($re_name)\s*=\s*/cgo) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\G\{\s*($re_name)\s*=\s*/cog) { } 2 0 2
140 2 my $key = lc($1);
141 my $value = _parse_string( $self->{strings},
142 2 exists $self->{opts}->{"no-warn-ack"} );
143 # If redefining to the same value, don't worry.
144 # If redefining j-tugboat (predefined above), don't worry,
145 # people use \TUB vs. "TUGboat" arbitrarily.
146 2 my $old_value = $self->{strings}->{$key};
147 2 ▶ if ($key ne "j-tugboat"
Branch: 1 branch
BranchTrueFalseTotal
if ($key ne "j-tugboat" and defined $old_value and $old_value ne $value) 0 2 2
Condition33%2/6
$key ne "j-tugboat" and defined $old_value and $old_value ne $value
!ll&&!rl&&r
200
$key ne "j-tugboat" and defined $old_value
!ll&&!rl&&r
020
MC/DC0%0/3
$key ne "j-tugboat" and defined $old_value and $old_value ne $value
$key ne "j-tugboat" defined $old_value $old_value ne $value
Truth table25%1/4
A and B and C
A= $key ne "j-tugboat"B= defined $old_valueC= $old_value ne $value
ABCresult
0XX0
10X0
1100
1111
148 && defined $old_value && $old_value ne $value) {
149 0 warn("Redefining string $key ",
150 "(oldvalue=$old_value, newvalue=$value");
151 }
152 2 $self->{strings}->{$key} = $value;
153 2 /\G[\s\n]*\}/cg;
154 } else {
155 0 $current_entry->error("Malformed string! ($raw)");
156 0 return $current_entry;
157 }
158 ▶ } elsif ( $type eq "COMMENT" or $type eq "PREAMBLE" ) {
Condition100%3/3
$type eq "COMMENT" or $type eq "PREAMBLE"
l!l&&r!l&&!r
1141
MC/DC100%2/2
$type eq "COMMENT" or $type eq "PREAMBLE"
$type eq "COMMENT" $type eq "PREAMBLE"
Truth table100%3/3
A or B
A= $type eq "COMMENT"B= $type eq "PREAMBLE"
ABresult
1X1
011
000
159 2 /\G\{./cgo;
160 2 _slurp_close_bracket;
161 } else { # normal entry
162 41 $current_entry->parse_ok(1);
163  
164 # parse key
165 41 ▶ if (/\G\s*\{(?:\s*($re_name)\s*,[\s\n]*|\s+\r?\s*)/cgo) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\G\s*\{(?:\s*($re_name)\s*,[\s\n]*|\s+\r?\s*)/cog) { } 39 2 41
166 39 $current_entry->key($1);
167  
168 # fields
169 39 ▶ while (/\G[\s\n]*($re_name)[\s\n]*=[\s\n]*/cgo) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\G[\s\n]*($re_name)[\s\n]*=[\s\n]*/cog) 171 39 210
170 $current_entry->field(
171 $1 => _parse_string( $self->{strings},
172 171 exists $self->{opts}->{"no-warn-ack"} ) );
173 171 my $idx = index( $_, ',', pos($_) );
174 171 ▶ pos($_) = $idx + 1 if $idx > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $idx > 0 144 27 171
175 }
176  
177 39 return $current_entry;
178  
179 } else {
180  
181 2 ▶ $current_entry->error("Malformed entry (key contains invalid characters) at " . substr($_, pos($_) || 0, 20) . ", ignoring");
Condition50%1/2
pos $_ || 0
l!l
20
MC/DC0%0/1
pos $_ || 0
pos $_
Truth table50%1/2
A
A= pos $_
Aresult
11
00
182 2 _slurp_close_bracket;
183 2 return $current_entry;
184 }
185 }
186  
187 4 $self->{buffer} = substr $_, pos($_);
188  
189 } else {
190 2 ▶ $current_entry->error("Did not find type at " . substr($_, pos($_) || 0, 20));
Condition50%1/2
pos $_ || 0
l!l
02
MC/DC0%0/1
pos $_ || 0
pos $_
Truth table50%1/2
A
A= pos $_
Aresult
11
00
191 2 return $current_entry;
192 }
193  
194 }
195 }
196  
197  
198 53 ▶sub next {
Subroutine next: called CC=1 SCAR=0.0
Pod documented
199 53 my $self = shift;
200  
201 53 return $self->_parse_next;
202 }
203  
204 # slurp everything till the next closing brace. Handles
205 # nested brackets
206 4 ▶sub _slurp_close_bracket {
Subroutine _slurp_close_bracket: called CC=4 SCAR=15.8
207 4 my $bracelevel = 0;
208 BRACE: {
209 0 ▶ /\G[^\}]*\{/cg && do { $bracelevel++; redo BRACE };
Branch: 1 branch
BranchTrueFalseTotal
/\G[^\}]*\{/cg and do { ++$bracelevel; redo BRACE } 0 4 4
210 /\G[^\{]*\}/cg
211 4 ▶ && do {
Branch: 1 branch
BranchTrueFalseTotal
/\G[^\{]*\}/cg and do { if ($bracelevel > 0) { --$bracelevel; redo BRACE; } else { return; } } 2 2 4
212 2 ▶ if ( $bracelevel > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bracelevel > 0) { } 0 2 2
213 0 $bracelevel--;
214 0 redo BRACE;
215 } else {
216 2 return;
217 }
218 }
219 }
220 }
221  
222 # parse bibtex string in $_ and return. A BibTeX string is either enclosed
223 # in double quotes '""' or matching braces '{}'. The braced form may contain
224 # nested braces.
225 #
226 # Second argument NO_WARN_ACK says whether to emit the warning
227 # "Using undefined string" if the name of the undefined string starts
228 # with "ack-". The default is to warn. The TUGboat config file
229 # ltx2crossrefxml-tugboat.cfg sets this.
230 #
231 # It is an unfortunate fact that people routinely copy bib entries
232 # without copying the "ack-nhfb" or other "ack-..." @string definitions
233 # in Nelson Beebe's bibliography files, resulting in this warning. It is
234 # too irritating to have to define them (they are never used), and also
235 # too irritating to have to see the many warnings on every run.
236 #
237 # Similarly for j-TUGboat, which we predefine in the new() fn above.
238 #
239 180 ▶sub _parse_string {
Subroutine _parse_string: called CC=10 SCAR=24.5
240 180 my ($strings_ref, $no_warn_ack) = @_;
241 180 ▶ $no_warn_ack ||= 0;
Condition50%1/2
$no_warn_ack ||= 0
l!l
0180
MC/DC0%0/1
$no_warn_ack ||= 0
$no_warn_ack
Truth table50%1/2
A
A= $no_warn_ack
Aresult
11
00
242  
243 180 my $value = "";
244  
245 PART: {
246 180 ▶ if (/\G(\d+)/cg) {
Branch: 3 branches
BranchTrueFalseTotal
if (/\G(\d+)/cg) { } 12 175 187
elsif (/\G($re_name)/cog) { } 10 165 175
elsif (/\G"(([^"\\]*(\\.)*[^\\"]*)*)"/cgs) { } 117 48 165
247 12 $value .= $1;
248 } elsif (/\G($re_name)/cgo) {
249 10 my $key = lc($1);
250 10 ▶ if (! defined $strings_ref->{lc($1)}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $$strings_ref{lc $1}) 0 10 10
251 #debug_hash("looking up $key for $1", $strings_ref);
252 0 ▶ warn("Using undefined string $1 (", lc($1), ") in: $_")
Branch: 1 branch
BranchTrueFalseTotal
unless $no_warn_ack and $1 =~ /^ack-/ 0 0 0
Condition0%0/3
$no_warn_ack and $1 =~ /^ack-/
!ll&&!rl&&r
000
MC/DC0%0/2
$no_warn_ack and $1 =~ /^ack-/
$no_warn_ack $1 =~ /^ack-/
Truth table0%0/3
A and B
A= $no_warn_ackB= $1 =~ /^ack-/
ABresult
0X0
100
111
253 unless $no_warn_ack && $1 =~ /^ack-/;
254 }
255 10 ▶ $value .= $strings_ref->{$1} || "";
Condition50%1/2
$strings_ref->{$1} || ''
l!l
100
MC/DC0%0/1
$strings_ref->{$1} || ''
$strings_ref->{$1}
Truth table50%1/2
A
A= $strings_ref->{$1}
Aresult
11
00
256 } elsif (/\G"(([^"\\]*(\\.)*[^\\"]*)*)"/cgs)
257 { # quoted string with embedded escapes
258 117 $value .= $1;
259 } else {
260 48 my $part = _extract_bracketed( $_ );
261 48 $value .= substr $part, 1, length($part) - 2; # strip quotes
262 }
263  
264 187 ▶ if (/\G\s*#\s*/cg) { # string concatenation by #
Branch: 1 branch
BranchTrueFalseTotal
if (/\G\s*#\s*/cg) 7 180 187
265 7 redo PART;
266 }
267 }
268 180 $value =~ s/[\s\n]+/ /g;
269 180 return $value;
270 }
271  
272 sub _extract_bracketed
273 48 ▶{
Subroutine _extract_bracketed: called CC=7 SCAR=19.5
274 48 for($_[0]) # alias to $_
275 {
276 48 /\G\s+/cg;
277 48 my $start = pos($_);
278 48 my $depth = 0;
279 48 while(1)
280 {
281 175 ▶ /\G\\./cg && next;
Branch: 1 branch
BranchTrueFalseTotal
/\G\\./cg and next 2 173 175
282 173 ▶ /\G\{/cg && (++$depth, next);
Branch: 1 branch
BranchTrueFalseTotal
/\G\{/cg and ++$depth, next 55 118 173
283 118 ▶ /\G\}/cg && (--$depth > 0 ? next : last);
Branch: 2 branches
BranchTrueFalseTotal
/\G\}/cg and --$depth > 0 ? next : (last) 55 63 118
--$depth > 0 ? : 7 48 55
284 63 ▶ /\G([^\\\{\}]+)/cg && next;
Branch: 1 branch
BranchTrueFalseTotal
/\G([^\\\{\}]+)/cg and next 63 0 63
285 0 last; # end of string
286 }
287 48 return substr($_, $start, pos($_)-$start);
288 }
289 }
290  
291 # Split the $string using $pattern as a delimiter with
292 # each part having balanced braces (so "{$pattern}"
293 # does NOT split).
294 # Return empty list if unmatched braces
295  
296 248 ▶sub _split_braced_string {
Subroutine _split_braced_string: called CC=14 SCAR=26.5
297 248 my $string = shift;
298 248 my $pattern = shift;
299 248 my @tokens;
300 248 ▶ return () if $string eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $string eq "" 1 247 248
301 247 my $buffer;
302 247 ▶ while (!defined pos $string || pos $string < length $string) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined pos $string or pos $string < length $string) 422 16 438
Condition100%3/3
not defined pos $string or pos $string < length $string
l!l&&r!l&&!r
24717516
MC/DC100%2/2
not defined pos $string or pos $string < length $string
not defined pos $string pos $string < length $string
Truth table100%3/3
not(A) or B
A= not defined pos $stringB= pos $string < length $string
ABresult
1X1
011
000
303 422 ▶ if ( $string =~ /\G(.*?)(\{|$pattern)/cgi ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /\G(.*?)(\{|$pattern)/cig) { } 193 229 422
304 193 my $match = $1;
305 193 ▶ if ( $2 =~ /$pattern/i ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($2 =~ /$pattern/i) { } 164 29 193
elsif ($2 =~ /\{/) { } 29 0 29
306 164 $buffer .= $match;
307 164 push @tokens, $buffer;
308 164 $buffer = "";
309 } elsif ( $2 =~ /\{/ ) {
310 29 $buffer .= $match . "{";
311 29 my $numbraces=1;
312 29 ▶ while ($numbraces !=0 && pos $string < length $string) {
Branch: 1 branch
BranchTrueFalseTotal
if ($numbraces != 0 and pos $string < length $string) 345 29 374
Condition100%3/3
$numbraces != 0 and pos $string < length $string
!ll&&!rl&&r
272345
MC/DC100%2/2
$numbraces != 0 and pos $string < length $string
$numbraces != 0 pos $string < length $string
Truth table100%3/3
A and B
A= $numbraces != 0B= pos $string < length $string
ABresult
0X0
100
111
313 345 my $symbol = substr($string, pos $string, 1);
314 345 $buffer .= $symbol;
315 345 ▶ if ($symbol eq '{') {
Branch: 2 branches
BranchTrueFalseTotal
if ($symbol eq '{') { } 0 345 345
elsif ($symbol eq '}') { } 27 318 345
316 0 $numbraces ++;
317 } elsif ($symbol eq '}') {
318 27 $numbraces --;
319 }
320 345 pos($string) ++;
321 }
322 29 ▶ if ($numbraces != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($numbraces != 0) 2 27 29
323 2 return ();
324 }
325 } else {
326 0 $buffer .= $match;
327 }
328 } else {
329 229 ▶ $buffer .= substr $string, (pos $string || 0);
Condition100%2/2
pos $string || 0
l!l
113116
MC/DC100%1/1
pos $string || 0
pos $string
Truth table100%2/2
A
A= pos $string
Aresult
11
00
330 229 last;
331 }
332 }
333 245 ▶ push @tokens, $buffer if $buffer;
Branch: 1 branch
BranchTrueFalseTotal
if $buffer 245 0 245
334 245 return @tokens;
335 }
336  
337
338 #
339 0 ▶sub debug_hash {
Subroutine debug_hash: not called CC=4 SCAR=30.0
Pod undocumented
340 0 my ($label) = shift;
341 0 ▶ my (%hash) = (ref $_[0] && $_[0] =~ /.*HASH.*/) ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && $_[0] =~ /.*HASH.*/ ? : 0 0 0
Condition0%0/3
ref $_[0] and $_[0] =~ /.*HASH.*/
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_[0] and $_[0] =~ /.*HASH.*/
ref $_[0] $_[0] =~ /.*HASH.*/
Truth table0%0/3
A and B
A= ref $_[0]B= $_[0] =~ /.*HASH.*/
ABresult
0X0
100
111
342  
343 0 my $str = "$label: {";
344 0 my @items = ();
345 0 for my $key (sort keys %hash) {
346 0 my $val = $hash{$key};
347 0 $key =~ s/\n/\\n/g;
348 0 $val =~ s/\n/\\n/g;
349 0 push (@items, "$key:$val");
350 }
351 0 $str .= join (",", @items);
352 0 $str .= "}";
353  
354 0 warn ($str);
355 }
356  
357 #
358 0 ▶sub debug_list {
Subroutine debug_list: not called CC=3 SCAR=24.8
Pod undocumented
359 0 my ($label) = shift;
360 0 ▶ my (@list) = (ref $_[0] && $_[0] =~ /.*ARRAY.*/) ? @{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && $_[0] =~ /.*ARRAY.*/ ? : 0 0 0
Condition0%0/3
ref $_[0] and $_[0] =~ /.*ARRAY.*/
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_[0] and $_[0] =~ /.*ARRAY.*/
ref $_[0] $_[0] =~ /.*ARRAY.*/
Truth table0%0/3
A and B
A= ref $_[0]B= $_[0] =~ /.*ARRAY.*/
ABresult
0X0
100
111
361  
362 0 my $str = "$label [" . join (",", @list) . "]";
363 0 warn $str;
364 }
365  
366
367 # Return string representation of call stack for debugging.
368 #
369 0 ▶sub backtrace {
Subroutine backtrace: not called CC=2 SCAR=17.9
Pod undocumented
370 0 my $ret = "";
371  
372 0 my ($line, $subr);
373 0 my $stackframe = 1; # skip ourselves
374 0 ▶ while ((undef,undef,$line,$subr) = caller ($stackframe)) {
Branch: 1 branch
BranchTrueFalseTotal
if ((undef, undef, $line, $subr) = caller $stackframe) 0 0 0
375 0 $ret .= " -> $subr.$line";
376 0 $stackframe++;
377 }
378  
379 0 return $ret;
380 }
381  
382  
383 20 1; # End of BibTeX::Parser
384  
385  
386 __END__
« Parser/Author.pm ↑ Summary Parser/Entry.pm »