AtteanX/Parser/NTuples.pm

filter: Statementstmt 75.3% 159 / 211 Branchbran 65.3% 81 / 124 Conditioncond 58.3% 7 / 12 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 100% 19 / 19 Podpod 80.0% 4 / 5 totaltotal 72.0% 273 / 379 CC 67 SCAR 36.2
CC
67
Cov
71%
_unescape
58.5
_eat_node
53.3
__ANON__
24.2
CRAP 37.4
« AtteanX/Serializer/SPARQL.pm ↑ Summary AtteanX/Parser/Turtle/Lexer.pm »
1 6 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 =head1 NAME
5  
6 AtteanX::Parser::NTuples - Shared functionality for N-Triples and N-Quads parsers
7  
8 =head1 VERSION
9  
10 This document describes AtteanX::Parser::NTuples version 0.040
11  
12 =head1 SYNOPSIS
13  
14 use Attean;
15  
16 =head1 DESCRIPTION
17  
18 This module provides a base class for RDF formats N-Triples and N-Quads.
19  
20 =head1 ATTRIBUTES
21  
22 =over 4
23  
24 =item C<< blank_nodes >>
25  
26 =item C<< has_blank_nodes_map >>
27  
28 =back
29  
30 =head1 METHODS
31  
32 =over 4
33  
34 =cut
35  
36 6 package AtteanX::Parser::NTuples 0.040 {
37 6 ▶ use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 6 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 6 ▶ use Attean;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 6 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 6 ▶ use Carp qw(carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
42 6 ▶ use Encode qw(decode);
Subroutine BEGIN: called CC=1 SCAR=0.0
43 6 ▶ use Types::Standard qw(Bool HashRef ArrayRef HashRef Str Maybe InstanceOf ConsumerOf);
Subroutine BEGIN: called CC=1 SCAR=0.0
44 6 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 8 ▶ has 'blank_nodes' => (is => 'ro', isa => HashRef[ConsumerOf['Attean::API::Blank']], predicate => 'has_blank_nodes_map', default => sub { +{} });
Subroutine __ANON__: called CC=1 SCAR=0.0
47  
48 6 my $r_PN_CHARS_U = qr/[_A-Za-z_\x{00C0}-\x{00D6}\x{00D8}-\x{00F6}\x{00F8}-\x{02FF}\x{0370}-\x{037D}\x{037F}-\x{1FFF}\x{200C}-\x{200D}\x{2070}-\x{218F}\x{2C00}-\x{2FEF}\x{3001}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFFD}\x{10000}-\x{EFFFF}]/o;
49 6 my $r_PN_CHARS = qr"${r_PN_CHARS_U}|[-0-9\x{00B7}\x{0300}-\x{036F}\x{203F}-\x{2040}]"o;
50  
51 =item C<< new_abs_iri( $value ) >>
52  
53 Delegates to C<< new_iri >> but throws an error if the resulting IRI is not absolute.
54  
55 =cut
56  
57 37 ▶ sub new_abs_iri {
Subroutine new_abs_iri: called CC=2 SCAR=7.0
Pod documented
58 37 my $self = shift;
59 37 my $iri = $self->new_iri(@_);
60 37 ▶ die "Not an absolute IRI" . Dumper($iri) unless $iri->is_absolute;
Branch: 1 branch
BranchTrueFalseTotal
unless $iri->is_absolute 0 37 37
61 37 return $iri;
62 }
63
64 =item C<< parse_term_from_bytes( $bytes ) >>
65  
66 Parses the given C<< $bytes >> and returns a corresponding L<Attean::API::Term> object.
67  
68 =cut
69  
70 1 ▶ sub parse_term_from_bytes {
Subroutine parse_term_from_bytes: called CC=2 SCAR=7.1
Pod documented
71 1 my $self = shift;
72 1 ▶ unless (ref($self)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self) 0 1 1
73 0 $self = $self->new();
74 }
75 1 my $bytes = shift;
76 1 my $string = Encode::decode("utf-8", $bytes);
77 1 my $n = $self->_eat_node( 0, $string );
78 1 return $n;
79 }
80
81 =item C<< parse_iter_from_bytes( $data ) >>
82  
83 Returns an iterator of L<Attean::API::Binding> objects that result from parsing
84 the data read from the UTF-8 encoded byte string C<< $data >>.
85  
86 =cut
87  
88 14 ▶ sub parse_iter_from_bytes {
Subroutine parse_iter_from_bytes: called CC=1 SCAR=0.0
Pod documented
89 14 my $self = shift;
90 14 my $data = shift;
91
92 14 open(my $fh, '<:encoding(UTF-8)', \$data);
93 14 return $self->parse_iter_from_io($fh);
94 }
95  
96 =item C<< parse_iter_from_io( $fh ) >>
97  
98 Returns an iterator of L<Attean::API::Binding> objects that result from parsing
99 the data read from the L<IO::Handle> object C<< $fh >>.
100  
101 =cut
102  
103 16 ▶ sub parse_iter_from_io {
Subroutine parse_iter_from_io: called CC=1 SCAR=0.4
Pod documented
104 16 my $self = shift;
105 16 my $fh = shift;
106
107 16 my $lineno = 0;
108 16 my $line;
109 52 ▶ my $gen = sub {
Subroutine __ANON__: called CC=14 SCAR=31.8
110 52 my $version;
111 52 ▶ while (defined($line = <$fh>)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = readline $fh)) 40 15 55
112 40 LINE: ($line, my @extra) = split(/\r\n|\r|\n/, $line, 2);
113 40 $lineno++;
114
115 40 ▶ next unless (defined($line) and length($line));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line and length $line 1 39 40
Condition66%2/3
defined $line and length $line
!ll&&!rl&&r
0139
MC/DC50%1/2
defined $line and length $line
defined $line length $line
Truth table66%2/3
A and B
A= defined $lineB= length $line
ABresult
0X0
100
111
116 39 ▶ next unless ($line =~ /\S/);
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /\S/u 1 38 39
117 38 chomp($line);
118 38 $line =~ s/^\s*//;
119 38 $line =~ s/\s*$//;
120 38 ▶ next if ($line =~ /^#/);
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^#/u 1 37 38
121
122 37 ▶ if ($line =~ /^VERSION\b/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^VERSION\b/u) 0 37 37
123 0 $line =~ s/^VERSION\s*//;
124 0 my $n = $self->_eat_node( $lineno, $line );
125 0 ▶ die "Unexpected term following VERSION at line $lineno" unless ($n->does('Attean::API::Literal'));
Branch: 1 branch
BranchTrueFalseTotal
unless $n->does("Attean::API::Literal") 0 0 0
126 0 ▶ die "Unexpected language-tagged literal following VERSION at line $lineno" if ($n->has_language);
Branch: 1 branch
BranchTrueFalseTotal
if $n->has_language 0 0 0
127 0 ▶ die "Unexpected datatyped literal following VERSION at line $lineno" unless ($n->datatype->value eq 'http://www.w3.org/2001/XMLSchema#string');
Branch: 1 branch
BranchTrueFalseTotal
unless $n->datatype->value eq "http://www.w3.org/2001/XMLSchema#string" 0 0 0
128 0 $version = $n->value;
129 0 next;
130 }
131  
132 37 my @nodes = ();
133 37 ▶ while (my $n = $self->_eat_node( $lineno, $line )) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $n = $self->_eat_node($lineno, $line)) 124 36 160
134 124 push(@nodes, $n);
135 124 $line =~ s/^\s*//;
136 }
137 36 $line =~ s/^\s+//g;
138 36 ▶ unless ($line =~ m'[.]\s*(#.*)?') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($line =~ /[.]\s*(#.*)?/u) 0 36 36
139 0 die "Missing expected '.' at line $lineno";
140 }
141
142 36 my $binding = $self->_binding( \@nodes, $lineno );
143
144 36 ▶ if (@extra and $extra[0] ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (@extra and $extra[0] ne "") 0 36 36
Condition33%1/3
@extra and $extra[0] ne ""
!ll&&!rl&&r
0360
MC/DC0%0/2
@extra and $extra[0] ne ""
@extra $extra[0] ne ""
Truth table33%1/3
A and B
A= @extraB= $extra[0] ne ""
ABresult
0X0
100
111
145 0 $line = shift(@extra);
146 0 goto LINE;
147 }
148 36 return $binding;
149 }
150 15 return;
151 16 };
152 16 return Attean::CodeIterator->new(
153 generator => $gen,
154 item_type => $self->handled_type->role,
155 );
156 }
157  
158 162 ▶ sub _eat_node {
Subroutine _eat_node: called CC=33 SCAR=39.8
159 162 my $self = shift;
160 162 my $lineno = shift;
161 162 $_[0] =~ s/^\s*//;
162 162 ▶ return unless length($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
unless length $_[0] 0 162 162
163 162 my $char = substr($_[0], 0, 1);
164 162 ▶ return if ($char eq '.');
Branch: 1 branch
BranchTrueFalseTotal
if $char eq "." 36 126 162
165  
166 126 ▶ if ($char eq '<') {
Branch: 3 branches
BranchTrueFalseTotal
if ($char eq '<') { } 86 40 126
elsif ($char eq '_') { } 22 18 40
elsif ($char eq '"') { } 18 0 18
167 86 ▶ if ($_[0] =~ m{^<<\(}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^<<\(/u) { } 0 86 86
168 0 substr($_[0], 0, 3, '');
169 0 my $s = $self->_eat_node($lineno, $_[0]);
170 0 my $p = $self->_eat_node($lineno, $_[0]);
171 0 my $o = $self->_eat_node($lineno, $_[0]);
172 0 $_[0] =~ s/^\s*//;
173 0 ▶ die qq[Expected end of triple term at line $lineno, near "$_[0]"] unless ($_[0] =~ m{^\)>>});
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] =~ /^\)>>/u 0 0 0
174 0 substr($_[0], 0, 3, '');
175 0 return Attean::Triple->new( $s, $p, $o );
176 } else {
177 86 my ($uri) = $_[0] =~ m/^<([^>]*)>/;
178 86 substr($_[0], 0, length($uri)+2) = '';
179 86 state %cache;
180 86 ▶ if (my $i = $cache{$uri}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $i = $cache{$uri}) { } 54 32 86
181 54 return $i;
182 } else {
183 32 ▶ if (rand() < 0.02) {
Branch: 1 branch
BranchTrueFalseTotal
if (rand() < "0.02") 0 32 32
184 # clear out the cache roughly every 50 IRIs
185 0 %cache = ();
186 }
187 32 my $iri = $self->new_abs_iri( value => _unescape($uri, $lineno) );
188 32 $cache{$uri} = $iri;
189 32 return $iri;
190 }
191 }
192 } elsif ($char eq '_') {
193 22 my ($name) = $_[0] =~ m/^_:((${r_PN_CHARS}|[0-9])((${r_PN_CHARS}|[.]})*${r_PN_CHARS})?)/;
194 22 ▶ unless (defined($name)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name) 0 22 22
195 0 die qq[Invalid blank-node label at line $lineno, near "$_[0]"];
196 }
197 22 substr($_[0], 0, length($name)+2) = '';
198 22 my $b = Attean::Blank->new($name);
199 22 $self->blank_nodes->{$name} = $b;
200 22 return $b;
201 } elsif ($char eq '"') {
202 18 substr($_[0], 0, 1) = '';
203 18 my $value = decode('utf8', '');
204 18 ▶ while (length($_[0]) and substr($_[0], 0, 1) ne '"') {
Branch: 1 branch
BranchTrueFalseTotal
if (length $_[0] and substr($_[0], 0, 1) ne "\"") 22 17 39
Condition66%2/3
length $_[0] and substr($_[0], 0, 1) ne "\""
!ll&&!rl&&r
01722
MC/DC50%1/2
length $_[0] and substr($_[0], 0, 1) ne "\""
length $_[0] substr($_[0], 0, 1) ne "\""
Truth table66%2/3
A and B
A= length $_[0]B= substr($_[0], 0, 1) ne "\""
ABresult
0X0
100
111
205 22 ▶ while ($_[0] =~ m/^([^"\\]+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^([^"\\]+)/u) 20 22 42
206 20 $value .= $1;
207 20 substr($_[0],0,length($1)) = '';
208 }
209 22 ▶ if (substr($_[0],0,1) eq '\\') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($_[0], 0, 1) eq "\\") 6 16 22
210 6 ▶ while ($_[0] =~ m/^\\(.)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^\\(.)/u) 7 5 12
211 7 ▶ if ($1 eq 't') {
Branch: 10 branches
BranchTrueFalseTotal
if ($1 eq 't') { } 1 6 7
elsif ($1 eq 'b') { } 0 6 6
elsif ($1 eq 'r') { } 0 6 6
elsif ($1 eq 'f') { } 0 6 6
elsif ($1 eq 'n') { } 1 5 6
elsif ($1 eq '"') { } 1 4 5
elsif ($1 eq q[']) { } 0 4 4
elsif ($1 eq '\\') { } 1 3 4
elsif ($1 eq 'u') { } 2 1 3
elsif ($1 eq 'U') { } 1 0 1
212 1 $value .= "\t";
213 1 substr($_[0],0,2) = '';
214 } elsif ($1 eq 'b') {
215 0 $value .= "\b";
216 0 substr($_[0],0,2) = '';
217 } elsif ($1 eq 'r') {
218 0 $value .= "\r";
219 0 substr($_[0],0,2) = '';
220 } elsif ($1 eq 'f') {
221 0 $value .= "\f";
222 0 substr($_[0],0,2) = '';
223 } elsif ($1 eq 'n') {
224 1 $value .= "\n";
225 1 substr($_[0],0,2) = '';
226 } elsif ($1 eq '"') {
227 1 $value .= '"';
228 1 substr($_[0],0,2) = '';
229 } elsif ($1 eq "'") {
230 0 $value .= "'";
231 0 substr($_[0],0,2) = '';
232 } elsif ($1 eq '\\') {
233 1 $value .= "\\";
234 1 substr($_[0],0,2) = '';
235 } elsif ($1 eq 'u') {
236 2 ▶ $_[0] =~ m/^\\u([0-9A-Fa-f]{4})/ or die qq[Bad N-Triples \\u escape at line $lineno, near "$_[0]"];
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /^\\u([0-9A-Fa-f]{4})/u or die qq[Bad N-Triples \\u escape at line $lineno, near "$_[0]"] 2 0 2
237 2 $value .= $self->unescape_hex($1);
238 1 substr($_[0],0,6) = '';
239 } elsif ($1 eq 'U') {
240 1 ▶ $_[0] =~ m/^\\U([0-9A-Fa-f]{8})/ or die qq[Bad N-Triples \\U escape at line $lineno, near "$_[0]"];
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /^\\U([0-9A-Fa-f]{8})/u or die qq[Bad N-Triples \\U escape at line $lineno, near "$_[0]"] 1 0 1
241 1 $value .= $self->unescape_hex($1);
242 1 substr($_[0],0,10) = '';
243 } else {
244 0 die qq[Not valid N-Triples escape character '\\$1' at line $lineno, near "$_[0]"];
245 }
246 }
247 }
248 }
249 17 ▶ if (substr($_[0],0,1) eq '"') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($_[0], 0, 1) eq '"') { } 17 0 17
250 17 substr($_[0],0,1) = '';
251 } else {
252 0 die qq[Ending double quote not found at line $lineno];
253 }
254
255 # if ($_[0] =~ m/^@(([a-z]+(?:-[a-zA-Z0-9]+)*)(?:--([a-zA-Z]+))?)/) {
256 17 ▶ if ($_[0] =~ m/^@(([a-z]{1,3}(?:-[a-zA-Z0-9]+)*)(?:--(ltr|rtl))?)/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($_[0] =~ /^@(([a-z]{1,3}(?:-[a-zA-Z0-9]+)*)(?:--(ltr|rtl))?)/u) { } 2 15 17
elsif (substr($_[0], 0, 3) eq '^^<') { } 5 10 15
257 2 my $lang = $1;
258 2 substr($_[0],0,1+length($lang)) = '';
259 2 return Attean::Literal->new( value => $value, language => $lang );
260 } elsif (substr($_[0],0,3) eq '^^<') {
261 5 substr($_[0],0,3) = '';
262 5 my ($uri) = $_[0] =~ m/^([^>]*)>/;
263 5 substr($_[0], 0, length($uri)+1) = '';
264 5 my $lex = _unescape($uri, $lineno);
265 5 ▶ die 'Explicit use of rdf:dirLangString as a literal datatype is not allowed' if ($lex eq 'http://www.w3.org/1999/02/22-rdf-syntax-ns#dirLangString');
Branch: 1 branch
BranchTrueFalseTotal
if $lex eq "http://www.w3.org/1999/02/22-rdf-syntax-ns#dirLangString" 0 5 5
266 5 ▶ die 'Explicit use of rdf:langString as a literal datatype is not allowed' if ($lex eq 'http://www.w3.org/1999/02/22-rdf-syntax-ns#langString');
Branch: 1 branch
BranchTrueFalseTotal
if $lex eq "http://www.w3.org/1999/02/22-rdf-syntax-ns#langString" 0 5 5
267 5 my $dt = $self->new_abs_iri(value => $lex);
268 5 return Attean::Literal->new( value => $value, datatype => $dt );
269 } else {
270 10 return Attean::Literal->new($value);
271 }
272 } else {
273 # Carp::cluck;
274 0 die qq[Not valid N-Triples node start character '$char' at line $lineno, near "$_[0]"];
275 }
276 }
277  
278 3 ▶ sub unescape_hex {
Subroutine unescape_hex: called CC=3 SCAR=11.0
Pod undocumented
279 3 my $self = shift;
280 3 my $codepoint = shift;
281 3 my $cp = hex($codepoint);
282 3 ▶ if ($cp >= 0xd800 && $cp <= 0xdfff) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cp >= 55296 and $cp <= 57343) 1 2 3
Condition66%2/3
$cp >= 55296 and $cp <= 57343
!ll&&!rl&&r
201
MC/DC50%1/2
$cp >= 55296 and $cp <= 57343
$cp >= 55296 $cp <= 57343
Truth table66%2/3
A and B
A= $cp >= 55296B= $cp <= 57343
ABresult
0X0
100
111
283 1 $self->_throw_error("Invalid use of surrogate codepoint in escape sequence: '$codepoint'", $codepoint);
284 }
285 2 return chr($cp);
286 }
287  
288 37 ▶ sub _unescape {
Subroutine _unescape: called CC=18 SCAR=40.7
289 37 my $string = shift;
290 37 my $lineno = shift;
291 37 my $value = '';
292 37 ▶ while (length($string)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $string) 37 37 74
293 37 ▶ while ($string =~ m/^([^\\]+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /^([^\\]+)/u) 37 37 74
294 37 $value .= $1;
295 37 substr($string,0,length($1)) = '';
296 }
297 37 ▶ if (length($string)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $string) 1 36 37
298 1 ▶ if ($string eq '\\') {
Branch: 1 branch
BranchTrueFalseTotal
if ($string eq "\\") 0 1 1
299 0 die qq[Backslash in N-Triples node without escaped character at line $lineno];
300 }
301 1 ▶ if ($string =~ m/^\\([tbnrf"'uU])/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /^\\([tbnrf"'uU])/u) { } 1 0 1
302 1 ▶ while ($string =~ m/^\\([tbnrf"'uU])/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /^\\([tbnrf"'uU])/u) 1 1 2
303 1 ▶ if ($1 eq 't') {
Branch: 9 branches
BranchTrueFalseTotal
if ($1 eq 't') { } 0 1 1
elsif ($1 eq 'b') { } 0 1 1
elsif ($1 eq 'n') { } 0 1 1
elsif ($1 eq 'r') { } 0 1 1
elsif ($1 eq 'f') { } 0 1 1
elsif ($1 eq '"') { } 0 1 1
elsif ($1 eq '\\') { } 0 1 1
elsif ($1 eq 'u') { } 1 0 1
elsif ($1 eq 'U') { } 0 0 0
304 0 $value .= "\t";
305 0 substr($string,0,2) = '';
306 } elsif ($1 eq 'b') {
307 0 $value .= "\b";
308 0 substr($string,0,2) = '';
309 } elsif ($1 eq 'n') {
310 0 $value .= "\n";
311 0 substr($string,0,2) = '';
312 } elsif ($1 eq 'r') {
313 0 $value .= "\r";
314 0 substr($string,0,2) = '';
315 } elsif ($1 eq 'f') {
316 0 $value .= "\f";
317 0 substr($string,0,2) = '';
318 } elsif ($1 eq '"') {
319 0 $value .= '"';
320 0 substr($string,0,2) = '';
321 } elsif ($1 eq '\\') {
322 0 $value .= "\\";
323 0 substr($string,0,2) = '';
324 } elsif ($1 eq 'u') {
325 1 ▶ $string =~ m/^\\u([0-9A-Fa-f]{4})/ or die qq[Bad N-Triples \\u escape at line $lineno, near "$string"];
Branch: 1 branch
BranchTrueFalseTotal
$string =~ /^\\u([0-9A-Fa-f]{4})/u or die qq[Bad N-Triples \\u escape at line $lineno, near "$string"] 1 0 1
326 1 $value .= chr(oct('0x' . $1));
327 1 substr($string,0,6) = '';
328 } elsif ($1 eq 'U') {
329 0 ▶ $string =~ m/^\\U([0-9A-F]{8})/ or die qq[Bad N-Triples \\U escape at line $lineno, near "$string"];
Branch: 1 branch
BranchTrueFalseTotal
$string =~ /^\\U([0-9A-F]{8})/u or die qq[Bad N-Triples \\U escape at line $lineno, near "$string"] 0 0 0
330 0 $value .= chr(oct('0x' . $1));
331 0 substr($string,0,10) = '';
332 }
333 }
334 } else {
335 0 my $esc = substr($string, 0, 2);
336 0 die qq[Not a valid N-Triples escape sequence '$esc' at line $lineno, near "$string"];
337 }
338 }
339 }
340 37 return $value;
341 }
342 }
343  
344 6 1;
345  
346 __END__
« AtteanX/Serializer/SPARQL.pm ↑ Summary AtteanX/Parser/Turtle/Lexer.pm »