Parser.pm

filter: Statementstmt 92.2% 131 / 142 Branchbran 84.2% 64 / 76 Conditioncond 66.6% 16 / 24 MC/DCmcdc 50.0% 7 / 14 Subroutinesub 100% 9 / 9 Podpod 100% 2 / 2 totaltotal 85.7% 229 / 267 CC 50 SCAR 25.2
CC
50
Cov
87%
_parse_next
18.2
_split_braced_string
14.1
_parse_string
11.7
CRAP 12.4
« Parser/Author.pm ↑ Summary Parser/Entry.pm »
1 package BibTeX::Parser;
2 {
3 19 $BibTeX::Parser::VERSION = '1.93';
4 }
5 # ABSTRACT: A pure perl BibTeX parser
6 19 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 19 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 19 require BibTeX::Parser::Entry; # mutual dependency, so use instead of require
10  
11  
12 19 my $re_namechar = qr/[a-zA-Z0-9\!\$\&\*\+\-\.\/\:\;\<\>\?\[\]\^\_\`\|\']/o;
13 19 my $re_name = qr/$re_namechar+/o;
14  
15  
16 sub new {
17 15 ▶ my ( $class, $fh, $opts ) = @_;
Subroutine new: called CC=2 SCAR=7.2
Pod documented
18  
19 15 ▶ return bless {
Condition50%1/2
$opts || {}
l!l
015
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  
36 },
37 line => -1,
38 buffer => "",
39 }, $class;
40 }
41  
42 sub _slurp_close_bracket;
43  
44 sub _parse_next {
45 42 ▶ my $self = shift;
Subroutine _parse_next: called CC=18 SCAR=29.0
46  
47 42 while (1) { # loop until regular entry is finished
48 46 ▶ return 0 if $self->{fh}->eof;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"fh"}->eof 5 41 46
49 41 local $_ = $self->{buffer};
50  
51 41 ▶ until (/@/m) {
Branch: 1 branch
BranchTrueFalseTotal
unless (/@/m) 111 37 148
52 111 my $line = $self->{fh}->getline;
53 111 ▶ return 0 unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 4 107 111
54 107 $line =~ s/^%.*$//;
55 107 $_ .= $line;
56 }
57  
58 37 my $current_entry = new BibTeX::Parser::Entry;
59 37 ▶ if (/@($re_name)/cgo) {
Branch: 1 branch
BranchTrueFalseTotal
if (/@($re_name)/cog) { } 36 1 37
60 36 my $type = uc $1;
61 36 $current_entry->type( $type );
62 36 my $start_pos = pos($_) - length($type) - 1;
63  
64 # read rest of entry (matches braces)
65 36 my $bracelevel = 0;
66 36 $bracelevel += tr/\{/\{/; #count braces
67 36 $bracelevel -= tr/\}/\}/;
68 36 ▶ while ( $bracelevel != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bracelevel != 0) 156 36 192
69 156 my $position = pos($_);
70 156 my $line = $self->{fh}->getline;
71 156 ▶ last unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 0 156 156
72 156 $bracelevel =
73 $bracelevel + ( $line =~ tr/\{/\{/ ) - ( $line =~ tr/\}/\}/ );
74 156 $_ .= $line;
75 156 pos($_) = $position;
76 }
77  
78 # Remember text before the entry
79 36 my $pre = substr($_, 0, $start_pos-1);
80 36 ▶ if ($start_pos == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start_pos == 0) 10 26 36
81 10 $pre = '';
82 }
83 36 $current_entry->pre($pre);
84  
85  
86 # Remember raw bibtex code
87 36 my $raw = substr($_, $start_pos);
88 36 $raw =~ s/^\s+//;
89 36 $raw =~ s/\s+$//;
90 36 $current_entry->raw_bibtex($raw);
91  
92 36 my $pos = pos $_;
93 36 tr/\n/ /;
94 36 pos($_) = $pos;
95  
96 36 ▶ if ( $type eq "STRING" ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'STRING') { } 2 34 36
elsif ($type eq 'COMMENT' or $type eq 'PREAMBLE') { } 2 32 34
97 2 ▶ if (/\G\{\s*($re_name)\s*=\s*/cgo) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\G\{\s*($re_name)\s*=\s*/cog) { } 2 0 2
98 2 my $key = lc($1);
99 my $value = _parse_string( $self->{strings},
100 2 exists $self->{opts}->{"no-warn-ack"} );
101 2 ▶ if ( defined $self->{strings}->{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"strings"}{$key}) 0 2 2
102 0 warn("Redefining string $key!");
103 }
104 2 $self->{strings}->{$key} = $value;
105 2 /\G[\s\n]*\}/cg;
106 } else {
107 0 $current_entry->error("Malformed string! ($raw)");
108 0 return $current_entry;
109 }
110 ▶ } elsif ( $type eq "COMMENT" or $type eq "PREAMBLE" ) {
Condition100%3/3
$type eq "COMMENT" or $type eq "PREAMBLE"
l!l&&r!l&&!r
1132
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
111 2 /\G\{./cgo;
112 2 _slurp_close_bracket;
113 } else { # normal entry
114 32 $current_entry->parse_ok(1);
115  
116 # parse key
117 32 ▶ 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) { } 31 1 32
118 31 $current_entry->key($1);
119  
120 # fields
121 31 ▶ 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) 135 31 166
122 $current_entry->field(
123 $1 => _parse_string( $self->{strings},
124 135 exists $self->{opts}->{"no-warn-ack"} ) );
125 135 my $idx = index( $_, ',', pos($_) );
126 135 ▶ pos($_) = $idx + 1 if $idx > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $idx > 0 116 19 135
127 }
128  
129 31 return $current_entry;
130  
131 } else {
132  
133 1 ▶ $current_entry->error("Malformed entry (key contains invalid characters) at " . substr($_, pos($_) || 0, 20) . ", ignoring");
Condition50%1/2
pos $_ || 0
l!l
10
MC/DC0%0/1
pos $_ || 0
pos $_
Truth table50%1/2
A
A= pos $_
Aresult
11
00
134 1 _slurp_close_bracket;
135 1 return $current_entry;
136 }
137 }
138  
139 4 $self->{buffer} = substr $_, pos($_);
140  
141 } else {
142 1 ▶ $current_entry->error("Did not find type at " . substr($_, pos($_) || 0, 20));
Condition50%1/2
pos $_ || 0
l!l
01
MC/DC0%0/1
pos $_ || 0
pos $_
Truth table50%1/2
A
A= pos $_
Aresult
11
00
143 1 return $current_entry;
144 }
145  
146 }
147 }
148  
149  
150 sub next {
151 42 ▶ my $self = shift;
Subroutine next: called CC=1 SCAR=0.0
Pod documented
152  
153 42 return $self->_parse_next;
154 }
155  
156 # slurp everything till the next closing brace. Handles
157 # nested brackets
158 sub _slurp_close_bracket {
159 3 ▶ my $bracelevel = 0;
Subroutine _slurp_close_bracket: called CC=4 SCAR=15.8
160 BRACE: {
161 3 ▶ /\G[^\}]*\{/cg && do { $bracelevel++; redo BRACE };
Branch: 1 branch
BranchTrueFalseTotal
/\G[^\}]*\{/cg and do { ++$bracelevel; redo BRACE } 0 3 3
162 /\G[^\{]*\}/cg
163 3 ▶ && do {
Branch: 1 branch
BranchTrueFalseTotal
/\G[^\{]*\}/cg and do { if ($bracelevel > 0) { --$bracelevel; redo BRACE; } else { return; } } 2 1 3
164 2 ▶ if ( $bracelevel > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bracelevel > 0) { } 0 2 2
165 0 $bracelevel--;
166 0 redo BRACE;
167 } else {
168 2 return;
169 }
170 }
171 }
172 }
173  
174 # parse bibtex string in $_ and return. A BibTeX string is either enclosed
175 # in double quotes '""' or matching braces '{}'. The braced form may contain
176 # nested braces.
177 #
178 # Second argument NO_WARN_ACK says whether to emit the warning
179 # "Using undefined string" if the name of the undefined string starts
180 # with "ack-". The default is to warn. The TUGboat config file
181 # ltx2crossrefxml-tugboat.cfg sets this.
182 #
183 # It is an unfortunate fact that people routinely copy bib entries
184 # without copying the "ack-nhfb" or other "ack-..." @string definitions
185 # in Nelson Beebe's bibliography files, resulting in this warning. It is
186 # too irritating to have to define them (they are never used), and also
187 # too irritating to have to see the many warnings on every run.
188 #
189 sub _parse_string {
190 144 ▶ my ($strings_ref, $no_warn_ack) = @_;
Subroutine _parse_string: called CC=10 SCAR=24.6
191 144 ▶ $no_warn_ack ||= 0;
Condition50%1/2
$no_warn_ack ||= 0
l!l
0144
MC/DC0%0/1
$no_warn_ack ||= 0
$no_warn_ack
Truth table50%1/2
A
A= $no_warn_ack
Aresult
11
00
192  
193 144 my $value = "";
194  
195 PART: {
196 144 ▶ if (/\G(\d+)/cg) {
Branch: 3 branches
BranchTrueFalseTotal
if (/\G(\d+)/cg) { } 12 139 151
elsif (/\G($re_name)/cog) { } 10 129 139
elsif (/\G"(([^"\\]*(\\.)*[^\\"]*)*)"/cgs) { } 81 48 129
197 12 $value .= $1;
198 } elsif (/\G($re_name)/cgo) {
199 10 ▶ if (! defined $strings_ref->{lc($1)}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $$strings_ref{lc $1}) 0 10 10
200 0 ▶ warn("Using undefined string $1 (", lc($1), ")")
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
201 unless $no_warn_ack && $1 =~ /^ack-/;
202 }
203 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
204 } elsif (/\G"(([^"\\]*(\\.)*[^\\"]*)*)"/cgs)
205 { # quoted string with embedded escapes
206 81 $value .= $1;
207 } else {
208 48 my $part = _extract_bracketed( $_ );
209 48 $value .= substr $part, 1, length($part) - 2; # strip quotes
210 }
211  
212 151 ▶ if (/\G\s*#\s*/cg) { # string concatenation by #
Branch: 1 branch
BranchTrueFalseTotal
if (/\G\s*#\s*/cg) 7 144 151
213 7 redo PART;
214 }
215 }
216 144 $value =~ s/[\s\n]+/ /g;
217 144 return $value;
218 }
219  
220 sub _extract_bracketed
221 {
222 48 ▶ for($_[0]) # alias to $_
Subroutine _extract_bracketed: called CC=7 SCAR=19.5
223 {
224 48 /\G\s+/cg;
225 48 my $start = pos($_);
226 48 my $depth = 0;
227 48 while(1)
228 {
229 175 ▶ /\G\\./cg && next;
Branch: 1 branch
BranchTrueFalseTotal
/\G\\./cg and next 2 173 175
230 173 ▶ /\G\{/cg && (++$depth, next);
Branch: 1 branch
BranchTrueFalseTotal
/\G\{/cg and ++$depth, next 55 118 173
231 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
232 63 ▶ /\G([^\\\{\}]+)/cg && next;
Branch: 1 branch
BranchTrueFalseTotal
/\G([^\\\{\}]+)/cg and next 63 0 63
233 0 last; # end of string
234 }
235 48 return substr($_, $start, pos($_)-$start);
236 }
237 }
238  
239 # Split the $string using $pattern as a delimiter with
240 # each part having balanced braces (so "{$pattern}"
241 # does NOT split).
242 # Return empty list if unmatched braces
243  
244 sub _split_braced_string {
245 239 ▶ my $string = shift;
Subroutine _split_braced_string: called CC=14 SCAR=26.5
246 239 my $pattern = shift;
247 239 my @tokens;
248 239 ▶ return () if $string eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $string eq "" 1 238 239
249 238 my $buffer;
250 238 ▶ while (!defined pos $string || pos $string < length $string) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined pos $string or pos $string < length $string) 406 16 422
Condition100%3/3
not defined pos $string or pos $string < length $string
l!l&&r!l&&!r
23816816
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
251 406 ▶ if ( $string =~ /\G(.*?)(\{|$pattern)/cgi ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /\G(.*?)(\{|$pattern)/cig) { } 186 220 406
252 186 my $match = $1;
253 186 ▶ if ( $2 =~ /$pattern/i ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($2 =~ /$pattern/i) { } 157 29 186
elsif ($2 =~ /\{/) { } 29 0 29
254 157 $buffer .= $match;
255 157 push @tokens, $buffer;
256 157 $buffer = "";
257 } elsif ( $2 =~ /\{/ ) {
258 29 $buffer .= $match . "{";
259 29 my $numbraces=1;
260 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
261 345 my $symbol = substr($string, pos $string, 1);
262 345 $buffer .= $symbol;
263 345 ▶ if ($symbol eq '{') {
Branch: 2 branches
BranchTrueFalseTotal
if ($symbol eq '{') { } 0 345 345
elsif ($symbol eq '}') { } 27 318 345
264 0 $numbraces ++;
265 } elsif ($symbol eq '}') {
266 27 $numbraces --;
267 }
268 345 pos($string) ++;
269 }
270 29 ▶ if ($numbraces != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($numbraces != 0) 2 27 29
271 2 return ();
272 }
273 } else {
274 0 $buffer .= $match;
275 }
276 } else {
277 220 ▶ $buffer .= substr $string, (pos $string || 0);
Condition100%2/2
pos $string || 0
l!l
108112
MC/DC100%1/1
pos $string || 0
pos $string
Truth table100%2/2
A
A= pos $string
Aresult
11
00
278 220 last;
279 }
280 }
281 236 ▶ push @tokens, $buffer if $buffer;
Branch: 1 branch
BranchTrueFalseTotal
if $buffer 236 0 236
282 236 return @tokens;
283 }
284  
285  
286 19 1; # End of BibTeX::Parser
287  
288  
289 __END__
« Parser/Author.pm ↑ Summary Parser/Entry.pm »