RDF/Trine/Parser/Turtle.pm

filter: Statementstmt 97.1% 305 / 314 Branchbran 86.5% 109 / 126 Conditioncond 89.2% 75 / 84 MC/DCmcdc 75.6% 31 / 41 Subroutinesub 100% 30 / 30 Podpod 100% 4 / 4 totaltotal 92.4% 554 / 599 CC 94 SCAR 22.2
CC
94
Cov
93%
_object
19.1
_token_to_node
14.2
_triple
11.0
CRAP 9.3
« RDF/Trine/Parser/RDFa.pm ↑ Summary RDF/Trine/Node/Resource.pm »
1 # RDF::Trine::Parser::Turtle
2 # -----------------------------------------------------------------------------
3  
4 =head1 NAME
5  
6 RDF::Trine::Parser::Turtle - Turtle RDF Parser
7  
8 =head1 VERSION
9  
10 This document describes RDF::Trine::Parser::Turtle version 1.019
11  
12 =head1 SYNOPSIS
13  
14 use RDF::Trine::Parser;
15 my $parser = RDF::Trine::Parser->new( 'turtle' );
16 $parser->parse_into_model( $base_uri, $data, $model );
17  
18 =head1 DESCRIPTION
19  
20 This module implements a parser for the Turtle RDF format.
21  
22 =head1 METHODS
23  
24 Beyond the methods documented below, this class inherits methods from the
25 L<RDF::Trine::Parser> class.
26  
27 =over 4
28  
29 =cut
30  
31 package RDF::Trine::Parser::Turtle;
32  
33 68 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 68 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 68 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 68 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 68 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
38 68 ▶use base qw(RDF::Trine::Parser);
Subroutine BEGIN: called CC=1 SCAR=0.0
39 68 ▶use RDF::Trine::Error qw(:try);
Subroutine BEGIN: called CC=1 SCAR=0.0
40 68 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 68 ▶use RDF::Trine::Parser::Turtle::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 68 ▶use RDF::Trine::Parser::Turtle::Lexer;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 68 ▶use RDF::Trine::Parser::Turtle::Token;
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 68 our $VERSION;
46 68 ▶BEGIN {
Subroutine BEGIN: called CC=3 SCAR=11.0
47 68 $VERSION = '1.019';
48 68 foreach my $ext (qw(ttl)) {
49 68 $RDF::Trine::Parser::file_extensions{ $ext } = __PACKAGE__;
50 }
51 68 $RDF::Trine::Parser::parser_names{ 'turtle' } = __PACKAGE__;
52 68 my $class = __PACKAGE__;
53 68 $RDF::Trine::Parser::encodings{ $class } = 'utf8';
54 68 $RDF::Trine::Parser::format_uris{ 'http://www.w3.org/ns/formats/Turtle' } = __PACKAGE__;
55 68 $RDF::Trine::Parser::canonical_media_types{ $class } = 'text/turtle';
56 68 foreach my $type (qw(application/x-turtle application/turtle text/turtle)) {
57 204 $RDF::Trine::Parser::media_types{ $type } = __PACKAGE__;
58 }
59 }
60  
61 68 my $rdf = RDF::Trine::Namespace->new('http://www.w3.org/1999/02/22-rdf-syntax-ns#');
62 68 my $xsd = RDF::Trine::Namespace->new('http://www.w3.org/2001/XMLSchema#');
63  
64 =item C<< new ( [ namespaces => $map ] ) >>
65  
66 Returns a new Turtle parser.
67  
68 =cut
69  
70 386 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
71 386 my $class = shift;
72 386 my %args = @_;
73 386 return bless({ %args, stack => [] }, $class);
74 }
75  
76 =item C<< parse ( $base_uri, $rdf, \&handler ) >>
77  
78 Parses the bytes in C<< $data >>, using the given C<< $base_uri >>. Calls the
79 C<< triple >> method for each RDF triple parsed. This method does nothing by
80 default, but can be set by using one of the default C<< parse_* >> methods.
81  
82 =cut
83  
84 250 ▶sub parse {
Subroutine parse: called CC=1 SCAR=0.0
Pod documented
85 250 my $self = shift;
86 250 local($self->{baseURI}) = shift;
87 250 my $string = shift;
88 # warn 'parse() content: ' . Dumper($string); # XXX
89 250 local($self->{handle_triple}) = shift;
90 250 require Encode;
91 250 $string = Encode::encode("utf-8", $string);
92 250 open(my $fh, '<:encoding(UTF-8)', \$string);
93 250 my $l = RDF::Trine::Parser::Turtle::Lexer->new($fh);
94 250 $self->_parse($l);
95 }
96  
97 =item C<< parse_file ( $base_uri, $fh, $handler ) >>
98  
99 Parses all data read from the filehandle or file C<< $fh >>, using the given
100 C<< $base_uri >>. If C<< $fh >> is a filename, this method can guess the
101 associated parse. For each RDF statement parses C<< $handler >> is called.
102  
103 =cut
104  
105 137 ▶sub parse_file {
Subroutine parse_file: called CC=6 SCAR=20.7
Pod documented
106 137 my $self = shift;
107 137 local($self->{baseURI}) = shift;
108 137 my $fh = shift;
109 137 local($self->{handle_triple}) = shift;
110  
111 137 ▶ unless (ref($fh)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $fh) 5 132 137
112 5 my $filename = $fh;
113 5 undef $fh;
114 5 ▶ unless ($self->can('parse')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->can("parse")) 0 5 5
115 0 my $pclass = $self->guess_parser_by_filename( $filename );
116 0 ▶ $self = $pclass->new() if ($pclass and $pclass->can('new'));
Branch: 1 branch
BranchTrueFalseTotal
if $pclass and $pclass->can("new") 0 0 0
Condition0%0/3
$pclass and $pclass->can("new")
!ll&&!rl&&r
000
MC/DC0%0/2
$pclass and $pclass->can("new")
$pclass $pclass->can("new")
Truth table0%0/3
A and B
A= $pclassB= $pclass->can("new")
ABresult
0X0
100
111
117 }
118 5 ▶ open( $fh, '<:encoding(UTF-8)', $filename ) or throw RDF::Trine::Error::ParserError -text => $!;
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<:encoding(UTF-8)", $filename or "RDF::Trine::Error::ParserError"->throw("-text", $!) 5 0 5
119 }
120
121 137 my $l = RDF::Trine::Parser::Turtle::Lexer->new($fh);
122 137 $self->_parse($l);
123 }
124  
125 =item C<< parse_node ( $string, $base, [ token => \$token ] ) >>
126  
127 Returns the RDF::Trine::Node object corresponding to the node whose N-Triples
128 serialization is found at the beginning of C<< $string >>.
129 If a reference to C<< $token >> is given, it is dereferenced and set to the
130 RDF::Trine::Parser::Turtle::Token tokenizer object, allowing access to information such
131 as the token's position in the input string.
132  
133 =cut
134  
135 24 ▶sub parse_node {
Subroutine parse_node: called CC=4 SCAR=14.2
Pod documented
136 24 my $self = shift;
137 24 my $string = shift;
138 24 local($self->{baseURI}) = shift;
139 24 my %args = @_;
140 24 open(my $fh, '<:encoding(UTF-8)', \$string);
141 24 my $l = RDF::Trine::Parser::Turtle::Lexer->new($fh);
142 24 my $t = $self->_next_nonws($l);
143 24 ▶ return unless ($t);
Branch: 1 branch
BranchTrueFalseTotal
unless $t 0 24 24
144 24 my $node = $self->_term($l, $t);
145 24 my $token_ref = $args{token};
146 24 ▶ if (defined($token_ref) and ref($token_ref)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $token_ref and ref $token_ref) 24 0 24
Condition33%1/3
defined $token_ref and ref $token_ref
!ll&&!rl&&r
0024
MC/DC0%0/2
defined $token_ref and ref $token_ref
defined $token_ref ref $token_ref
Truth table33%1/3
A and B
A= defined $token_refB= ref $token_ref
ABresult
0X0
100
111
147 24 $$token_ref = $t;
148 }
149 24 return $node;
150 }
151  
152 387 ▶sub _parse {
Subroutine _parse: called CC=3 SCAR=11.0
153 387 my $self = shift;
154 387 my $l = shift;
155 387 $l->check_for_bom;
156 387 ▶ unless (exists($self->{map})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"map"}) 377 10 387
157 377 $self->{map} = RDF::Trine::NamespaceMap->new();
158 }
159 387 ▶ while (my $t = $self->_next_nonws($l)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $t = $self->_next_nonws($l)) 1062 289 1351
160 1062 $self->_statement($l, $t);
161 }
162 }
163  
164 ################################################################################
165  
166 3749 ▶sub _unget_token {
Subroutine _unget_token: called CC=1 SCAR=0.0
167 3749 my $self = shift;
168 3749 my $t = shift;
169 3749 push(@{ $self->{ stack } }, $t);
170 }
171  
172 13365 ▶sub _next_nonws {
Subroutine _next_nonws: called CC=3 SCAR=11.0
173 13365 my $self = shift;
174 13365 my $l = shift;
175 13365 ▶ if (scalar(@{ $self->{ stack } })) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$self->{"stack"};}) 3749 9616 13365
176 3749 return pop(@{ $self->{ stack } });
177 }
178 9616 while (1) {
179 9616 my $t = $l->get_token;
180 9583 ▶ return unless ($t);
Branch: 1 branch
BranchTrueFalseTotal
unless $t 301 9282 9583
181 9282 my $type = $t->type;
182 # next if ($type == WS or $type == COMMENT);
183 # warn decrypt_constant($type) . "\n";
184 9282 return $t;
185 }
186 }
187  
188 1538 ▶sub _get_token_type {
Subroutine _get_token_type: called CC=3 SCAR=11.0
189 1538 my $self = shift;
190 1538 my $l = shift;
191 1538 my $type = shift;
192 1538 my $t = $self->_next_nonws($l);
193 1537 ▶ unless ($t) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($t) 3 1534 1537
194 3 $l->_throw_error(sprintf("Expecting %s but got EOF", decrypt_constant($type)));
195 0 return;
196 }
197 1534 ▶ unless ($t->type eq $type) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($t->type eq $type) 12 1522 1534
198 12 $self->_throw_error(sprintf("Expecting %s but got %s", decrypt_constant($type), decrypt_constant($t->type)), $t, $l);
199 }
200 1522 return $t;
201 }
202  
203 1039 ▶sub _statement {
Subroutine _statement: called CC=9 SCAR=22.0
204 1039 my $self = shift;
205 1039 my $l = shift;
206 1039 my $t = shift;
207 1039 my $type = $t->type;
208 # when (WS) {}
209 1039 ▶ if ($type == PREFIX or $type == SPARQLPREFIX) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type == 24 or $type == 26) { } 245 794 1039
elsif ($type == 23 or $type == 25) { } 11 783 794
Condition100%3/3
$type == 24 or $type == 26
l!l&&r!l&&!r
2423794
MC/DC100%2/2
$type == 24 or $type == 26
$type == 24 $type == 26
Truth table100%3/3
A or B
A= $type == 24B= $type == 26
ABresult
1X1
011
000
210 245 $t = $self->_get_token_type($l, PREFIXNAME);
211 241 my $name = $t->value;
212 241 $name =~ s/:$//;
213 241 $t = $self->_get_token_type($l, IRI);
214 239 my $r = RDF::Trine::Node::Resource->new($t->value, $self->{baseURI});
215 239 my $iri = $r->uri_value;
216 239 ▶ if ($type == PREFIX) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type == 24) 236 3 239
217 236 $t = $self->_get_token_type($l, DOT);
218 # $t = $self->_next_nonws($l);
219 # if ($t and $t->type != DOT) {
220 # $self->_unget_token($t);
221 # }
222 }
223 239 $self->{map}->add_mapping( $name => $iri );
224 239 ▶ if (my $ns = $self->{namespaces}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ns = $self->{"namespaces"}) 2 237 239
225 2 ▶ unless ($ns->namespace_uri($name)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ns->namespace_uri($name)) 2 0 2
226 2 $ns->add_mapping( $name => $iri );
227 }
228 }
229 }
230 ▶ elsif ($type == BASE or $type == SPARQLBASE) {
Condition100%3/3
$type == 23 or $type == 25
l!l&&r!l&&!r
74783
MC/DC100%2/2
$type == 23 or $type == 25
$type == 23 $type == 25
Truth table100%3/3
A or B
A= $type == 23B= $type == 25
ABresult
1X1
011
000
231 11 $t = $self->_get_token_type($l, IRI);
232 10 my $r = RDF::Trine::Node::Resource->new($t->value, $self->{baseURI});
233 10 my $iri = $r->uri_value;
234 10 ▶ if ($type == BASE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type == 23) 6 4 10
235 6 $t = $self->_get_token_type($l, DOT);
236 # $t = $self->_next_nonws($l);
237 # if ($t and $t->type != DOT) {
238 # $self->_unget_token($t);
239 # }
240 }
241 10 $self->{baseURI} = $iri;
242 }
243 else {
244 783 $self->_triple( $l, $t );
245 709 $t = $self->_get_token_type($l, DOT);
246 }
247 # }
248 }
249  
250 783 ▶sub _triple {
Subroutine _triple: called CC=11 SCAR=24.0
251 783 my $self = shift;
252 783 my $l = shift;
253 783 my $t = shift;
254 783 my $type = $t->type;
255 # subject
256 783 my $subj;
257 783 my $bnode_plist = 0;
258 783 ▶ if ($type == LBRACKET) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type == 1) { } 40 743 783
elsif ($type == 3) { } 4 739 743
elsif (not $type == 12 || $type == 11 || $type == 13) { } 21 718 739
259 40 $bnode_plist = 1;
260 40 $subj = RDF::Trine::Node::Blank->new();
261 40 my $t = $self->_next_nonws($l);
262 40 ▶ if ($t->type != RBRACKET) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t->type != 2) 11 29 40
263 11 $self->_unget_token($t);
264 11 $self->_predicateObjectList( $l, $subj );
265 11 $t = $self->_get_token_type($l, RBRACKET);
266 }
267 } elsif ($type == LPAREN) {
268 4 my $t = $self->_next_nonws($l);
269 4 ▶ if ($t->type == RPAREN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t->type == 4) { } 0 4 4
270 0 $subj = RDF::Trine::Node::Resource->new('http://www.w3.org/1999/02/22-rdf-syntax-ns#nil');
271 } else {
272 4 $subj = RDF::Trine::Node::Blank->new();
273 4 my @objects = $self->_object($l, $t);
274
275 4 while (1) {
276 6 my $t = $self->_next_nonws($l);
277 6 ▶ if ($t->type == RPAREN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t->type == 4) { } 4 2 6
278 4 last;
279 } else {
280 2 push(@objects, $self->_object($l, $t));
281 }
282 }
283 4 $self->_assert_list($subj, @objects);
284 }
285 ▶ } elsif (not($type==IRI or $type==PREFIXNAME or $type==BNODE)) {
Condition100%6/6
$type == 12 || $type == 11 || $type == 13
l!l&&r!l&&!r
6784021
$type == 12 or $type == 11
l!l&&r!l&&!r
50117761
MC/DC100%3/3
$type == 12 || $type == 11 || $type == 13
$type == 12 $type == 11 $type == 13
Truth table100%4/4
A or B || C
A= $type == 12B= $type == 11C= $type == 13
ABCresult
1XX1
01X1
0011
0000
286 21 $self->_throw_error("Expecting resource or bnode but got " . decrypt_constant($type), $t, $l);
287 } else {
288 718 $subj = $self->_token_to_node($t);
289 }
290 # warn "Subject: $subj\n"; # XXX
291
292 758 ▶ if ($bnode_plist) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bnode_plist) { } 40 718 758
293 #predicateObjectList?
294 40 $t = $self->_next_nonws($l);
295 40 $self->_unget_token($t);
296 40 ▶ if ($t->type != DOT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t->type != 5) 34 6 40
297 34 $self->_predicateObjectList($l, $subj);
298 }
299 } else {
300 #predicateObjectList
301 718 $self->_predicateObjectList($l, $subj);
302 }
303 }
304  
305 24 ▶sub _assert_list {
Subroutine _assert_list: called CC=3 SCAR=11.0
306 24 my $self = shift;
307 24 my $subj = shift;
308 24 my @objects = @_;
309 24 my $head = $subj;
310 24 ▶ while (@objects) {
Branch: 1 branch
BranchTrueFalseTotal
if (@objects) 330 24 354
311 330 my $obj = shift(@objects);
312 330 $self->_assert_triple($head, $rdf->first, $obj);
313 330 ▶ my $next = scalar(@objects) ? RDF::Trine::Node::Blank->new() : $rdf->nil;
Branch: 1 branch
BranchTrueFalseTotal
scalar @objects ? : 306 24 330
314 330 $self->_assert_triple($head, $rdf->rest, $next);
315 330 $head = $next;
316 }
317 }
318  
319 817 ▶sub _predicateObjectList {
Subroutine _predicateObjectList: called CC=11 SCAR=24.0
320 817 my $self = shift;
321 817 my $l = shift;
322 817 my $subj = shift;
323 817 my $t = $self->_next_nonws($l);
324 811 while (1) {
325 2208 my $type = $t->type;
326 2208 ▶ unless ($type==IRI or $type==PREFIXNAME or $type==A) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($type == 12 or $type == 11 or $type == 9) 18 2190 2208
Condition100%6/6
$type == 12 or $type == 11 or $type == 9
l!l&&r!l&&!r
21385218
$type == 12 or $type == 11
l!l&&r!l&&!r
242189670
MC/DC100%3/3
$type == 12 or $type == 11 or $type == 9
$type == 12 $type == 11 $type == 9
Truth table100%4/4
A or B or C
A= $type == 12B= $type == 11C= $type == 9
ABCresult
1XX1
01X1
0011
0000
327 18 $self->_throw_error("Expecting verb but got " . decrypt_constant($type), $t, $l);
328 }
329 2190 my $pred = $self->_token_to_node($t);
330 2187 $self->_objectList($l, $subj, $pred);
331
332 2166 $t = $self->_next_nonws($l);
333 2166 ▶ last unless ($t);
Branch: 1 branch
BranchTrueFalseTotal
unless $t 3 2163 2166
334 2163 ▶ if ($t->type == SEMICOLON) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t->type == 6) { } 1690 473 2163
335 1690 my $sc = $t;
336 1694 SEMICOLON_REPEAT:
337 $t = $self->_next_nonws($l);
338 1694 ▶ unless ($t) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($t) 1 1693 1694
339 1 $l->_throw_error("Expecting token after semicolon, but got EOF");
340 }
341 1693 ▶ goto SEMICOLON_REPEAT if ($t->type == SEMICOLON);
Branch: 1 branch
BranchTrueFalseTotal
if $t->type == 6 4 1689 1693
342 1689 ▶ if ($t->type == IRI or $t->type == PREFIXNAME or $t->type == A) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t->type == 12 or $t->type == 11 or $t->type == 9) { } 1397 292 1689
Condition83%5/6
$t->type == 12 or $t->type == 11 or $t->type == 9
l!l&&r!l&&!r
13970292
$t->type == 12 or $t->type == 11
l!l&&r!l&&!r
81389292
MC/DC66%2/3
$t->type == 12 or $t->type == 11 or $t->type == 9
$t->type == 12 $t->type == 11 $t->type == 9
Truth table75%3/4
A or B or C
A= $t->type == 12B= $t->type == 11C= $t->type == 9
ABCresult
1XX1
01X1
0011
0000
343 1397 next;
344 } else {
345 292 $self->_unget_token($t);
346 292 return;
347 }
348 } else {
349 473 $self->_unget_token($t);
350 473 return;
351 }
352 }
353 }
354  
355 2187 ▶sub _objectList {
Subroutine _objectList: called CC=4 SCAR=13.9
356 2187 my $self = shift;
357 2187 my $l = shift;
358 2187 my $subj = shift;
359 2187 my $pred = shift;
360 # warn "objectList: " . Dumper($subj, $pred); # XXX
361 2187 while (1) {
362 2208 my $t = $self->_next_nonws($l);
363 2197 ▶ last unless ($t);
Branch: 1 branch
BranchTrueFalseTotal
unless $t 2 2195 2197
364 2195 my $obj = $self->_object($l, $t);
365 2188 $self->_assert_triple($subj, $pred, $obj);
366
367 2188 $t = $self->_next_nonws($l);
368 2185 ▶ if ($t and $t->type == COMMA) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t and $t->type == 7) { } 21 2164 2185
Condition100%3/3
$t and $t->type == 7
!ll&&!rl&&r
1216321
MC/DC100%2/2
$t and $t->type == 7
$t $t->type == 7
Truth table100%3/3
A and B
A= $tB= $t->type == 7
ABresult
0X0
100
111
369 21 next;
370 } else {
371 2164 $self->_unget_token($t);
372 2164 return;
373 }
374 }
375 }
376  
377 2817 ▶sub _assert_triple {
Subroutine _assert_triple: called CC=5 SCAR=16.1
378 2817 my $self = shift;
379 2817 my $subj = shift;
380 2817 my $pred = shift;
381 2817 my $obj = shift;
382 2817 ▶ if ($self->{canonicalize} and blessed($obj) and $obj->isa('RDF::Trine::Node::Literal')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"canonicalize"} and builtin::blessed($obj) and $obj->isa("RDF::Trine::Node::Literal")) 588 2229 2817
Condition83%5/6
$self->{"canonicalize"} and builtin::blessed($obj) and $obj->isa("RDF::Trine::Node::Literal")
!ll&&!rl&&r
6401589588
$self->{"canonicalize"} and builtin::blessed($obj)
!ll&&!rl&&r
64002177
MC/DC66%2/3
$self->{"canonicalize"} and builtin::blessed($obj) and $obj->isa("RDF::Trine::Node::Literal")
$self->{"canonicalize"} builtin::blessed($obj) $obj->isa("RDF::Trine::Node::Literal")
Truth table75%3/4
A and B and C
A= $self->{"canonicalize"}B= builtin::blessed($obj)C= $obj->isa("RDF::Trine::Node::Literal")
ABCresult
0XX0
10X0
1100
1111
383 588 $obj = $obj->canonicalize;
384 }
385
386 2817 my $t = RDF::Trine::Statement->new($subj, $pred, $obj);
387 2817 ▶ if ($self->{handle_triple}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"handle_triple"}) 2613 204 2817
388 2613 $self->{handle_triple}->( $t );
389 }
390 }
391  
392 2527 ▶sub _object {
Subroutine _object: called CC=19 SCAR=29.5
393 2527 my $self = shift;
394 2527 my $l = shift;
395 2527 my $t = shift;
396 2527 my $type = $t->type;
397 2527 my $tcopy = $t;
398 2527 my $obj;
399 2527 ▶ if ($type==LBRACKET) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type == 1) { } 36 2491 2527
elsif ($type == 3) { } 27 2464 2491
elsif (not $type == 12 || $type == 11 || $type == 21 || $type == 19 || $type == 22 || $type == 20 || $type == 13 || $type == 16 || $type == 15 || $type == 14 || $type == 10) { } 2 2462 2464
400 36 $obj = RDF::Trine::Node::Blank->new();
401 36 my $t = $self->_next_nonws($l);
402 36 ▶ unless ($t) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($t) 0 36 36
403 0 $self->_throw_error("Expecting object but got only opening bracket", $tcopy, $l);
404 }
405 36 ▶ if ($t->type != RBRACKET) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t->type != 2) 28 8 36
406 28 $self->_unget_token($t);
407 28 $self->_predicateObjectList( $l, $obj );
408 28 $t = $self->_get_token_type($l, RBRACKET);
409 }
410 } elsif ($type == LPAREN) {
411 27 my $t = $self->_next_nonws($l);
412 27 ▶ unless ($t) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($t) 0 27 27
413 0 $self->_throw_error("Expecting object but got only opening paren", $tcopy, $l);
414 }
415 27 ▶ if ($t->type == RPAREN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t->type == 4) { } 6 21 27
416 6 $obj = RDF::Trine::Node::Resource->new('http://www.w3.org/1999/02/22-rdf-syntax-ns#nil');
417 } else {
418 21 $obj = RDF::Trine::Node::Blank->new();
419 21 my @objects = $self->_object($l, $t);
420
421 21 while (1) {
422 325 my $t = $self->_next_nonws($l);
423 325 ▶ if ($t->type == RPAREN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t->type == 4) { } 20 305 325
424 20 last;
425 } else {
426 305 push(@objects, $self->_object($l, $t));
427 }
428 }
429 20 $self->_assert_list($obj, @objects);
430 }
431 ▶ } elsif (not($type==IRI or $type==PREFIXNAME or $type==STRING1D or $type==STRING3D or $type==STRING1S or $type==STRING3S or $type==BNODE or $type==INTEGER or $type==DECIMAL or $type==DOUBLE or $type==BOOLEAN)) {
Condition100%30/30
$type == 12 || $type == 11 || $type == 21 || $type == 19 || $type == 22 || $type == 20 || $type == 13 || $type == 16 || $type == 15 || $type == 14 || $type == 10
l!l&&r!l&&!r
245482
$type == 12 or $type == 11 or $type == 21 or $type == 19 or $type == 22 or $type == 20 or $type == 13 or $type == 16 or $type == 15 or $type == 14
l!l&&r!l&&!r
2445910
$type == 12 or $type == 11 or $type == 21 or $type == 19 or $type == 22 or $type == 20 or $type == 13 or $type == 16 or $type == 15
l!l&&r!l&&!r
24341119
$type == 12 or $type == 11 or $type == 21 or $type == 19 or $type == 22 or $type == 20 or $type == 13 or $type == 16
l!l&&r!l&&!r
23835130
$type == 12 or $type == 11 or $type == 21 or $type == 19 or $type == 22 or $type == 20 or $type == 13
l!l&&r!l&&!r
23572681
$type == 12 or $type == 11 or $type == 21 or $type == 19 or $type == 22 or $type == 20
l!l&&r!l&&!r
23489107
$type == 12 or $type == 11 or $type == 21 or $type == 19 or $type == 22
l!l&&r!l&&!r
232820116
$type == 12 or $type == 11 or $type == 21 or $type == 19
l!l&&r!l&&!r
230226136
$type == 12 or $type == 11 or $type == 21
l!l&&r!l&&!r
1576726162
$type == 12 or $type == 11
l!l&&r!l&&!r
813763888
MC/DC100%11/11
$type == 12 || $type == 11 || $type == 21 || $type == 19 || $type == 22 || $type == 20 || $type == 13 || $type == 16 || $type == 15 || $type == 14 || $type == 10
$type == 12 $type == 11 $type == 21 $type == 19 $type == 22 $type == 20 $type == 13 $type == 16 $type == 15 $type == 14 $type == 10
Truth table100%12/12
A or B or C or D or E or F or G or H or I or J || K
A= $type == 12B= $type == 11C= $type == 21D= $type == 19E= $type == 22F= $type == 20G= $type == 13H= $type == 16I= $type == 15J= $type == 14K= $type == 10
ABCDEFGHIJKresult
1XXXXXXXXXX1
01XXXXXXXXX1
001XXXXXXXX1
0001XXXXXXX1
00001XXXXXX1
000001XXXXX1
0000001XXXX1
00000001XXX1
000000001XX1
0000000001X1
000000000011
000000000000
432 2 $self->_throw_error("Expecting object but got " . decrypt_constant($type), $t, $l);
433 } else {
434 2462 $obj = $self->_term($l, $t);
435 }
436 2519 return $obj;
437 }
438  
439 2486 ▶sub _term {
Subroutine _term: called CC=10 SCAR=23.1
440 2486 my $self = shift;
441 2486 my $l = shift;
442 2486 my $t = shift;
443 2486 my $tcopy = $t;
444 2486 my $obj;
445 2486 my $type = $t->type;
446 2486 ▶ if ($type==STRING1D or $type==STRING3D or $type==STRING1S or $type==STRING3S) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type == 21 or $type == 19 or $type == 22 or $type == 20) { } 781 1705 2486
Condition100%9/9
$type == 21 or $type == 19 or $type == 22 or $type == 20
l!l&&r!l&&!r
77291705
$type == 21 or $type == 19 or $type == 22
l!l&&r!l&&!r
752201714
$type == 21 or $type == 19
l!l&&r!l&&!r
726261734
MC/DC100%4/4
$type == 21 or $type == 19 or $type == 22 or $type == 20
$type == 21 $type == 19 $type == 22 $type == 20
Truth table100%5/5
A or B or C or D
A= $type == 21B= $type == 19C= $type == 22D= $type == 20
ABCDresult
1XXX1
01XX1
001X1
00011
00000
447 781 my $value = $t->value;
448 781 my $t = $self->_next_nonws($l);
449 778 my $dt;
450 my $lang;
451 778 ▶ if ($t) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t) 778 0 778
452 778 ▶ if ($t->type == HATHAT) {
Branch: 2 branches
BranchTrueFalseTotal
if ($t->type == 8) { } 36 742 778
elsif ($t->type == 27) { } 15 727 742
453 36 my $t = $self->_next_nonws($l);
454 36 ▶ if ($t->type == IRI or $t->type == PREFIXNAME) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t->type == 12 or $t->type == 11) 36 0 36
Condition66%2/3
$t->type == 12 or $t->type == 11
l!l&&r!l&&!r
2970
MC/DC0%0/2
$t->type == 12 or $t->type == 11
$t->type == 12 $t->type == 11
Truth table66%2/3
A or B
A= $t->type == 12B= $t->type == 11
ABresult
1X1
011
000
455 36 $dt = $self->_token_to_node($t);
456 }
457 } elsif ($t->type == LANG) {
458 15 $lang = $t->value;
459 } else {
460 727 $self->_unget_token($t);
461 }
462 }
463 778 $obj = RDF::Trine::Node::Literal->new($value, $lang, $dt);
464 } else {
465 1705 $obj = $self->_token_to_node($t, $type);
466 }
467 2482 return $obj;
468 }
469  
470 4682 ▶sub _token_to_node {
Subroutine _token_to_node: called CC=13 SCAR=26.5
471 4682 my $self = shift;
472 4682 my $t = shift;
473 4682 ▶ my $type = shift || $t->type;
Condition66%2/3
shift() || $t->type
l!l&&r!l&&!r
170529770
MC/DC0%0/2
shift() || $t->type
shift() $t->type
Truth table66%2/3
A || B
A= shift()B= $t->type
ABresult
1X1
011
000
474 4682 ▶ if ($type eq A) {
Branch: 10 branches
BranchTrueFalseTotal
if ($type eq 9) { } 55 4627 4682
elsif ($type eq 12) { } 1599 3028 4627
elsif ($type eq 16) { } 59 2969 3028
elsif ($type eq 15) { } 11 2958 2969
elsif ($type eq 14) { } 9 2949 2958
elsif ($type eq 10) { } 8 2941 2949
elsif ($type eq 11) { } 2860 81 2941
elsif ($type eq 13) { } 81 0 81
elsif ($type eq 21) { } 0 0 0
elsif ($type eq 22) { } 0 0 0
475 55 return $rdf->type;
476 }
477 elsif ($type eq IRI) {
478 1599 return RDF::Trine::Node::Resource->new($t->value, $self->{baseURI});
479 }
480 elsif ($type eq INTEGER) {
481 59 return RDF::Trine::Node::Literal->new($t->value, undef, $xsd->integer);
482 }
483 elsif ($type eq DECIMAL) {
484 11 return RDF::Trine::Node::Literal->new($t->value, undef, $xsd->decimal);
485 }
486 elsif ($type eq DOUBLE) {
487 9 return RDF::Trine::Node::Literal->new($t->value, undef, $xsd->double);
488 }
489 elsif ($type eq BOOLEAN) {
490 8 return RDF::Trine::Node::Literal->new($t->value, undef, $xsd->boolean);
491 }
492 elsif ($type eq PREFIXNAME) {
493 2860 my ($ns, $local) = @{ $t->args };
494 2860 $ns =~ s/:$//;
495 2860 my $prefix = $self->{map}->namespace_uri($ns);
496 2860 ▶ unless (blessed($prefix)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($prefix)) 8 2852 2860
497 8 $self->_throw_error("Use of undeclared prefix '$ns'", $t);
498 }
499 2852 my $iri = $prefix->uri($local);
500 2852 return $iri;
501 }
502 elsif ($type eq BNODE) {
503 81 return RDF::Trine::Node::Blank->new($t->value);
504 }
505 elsif ($type eq STRING1D) {
506 0 return RDF::Trine::Node::Literal->new($t->value);
507 }
508 elsif ($type eq STRING1S) {
509 0 return RDF::Trine::Node::Literal->new($t->value);
510 }
511 else {
512 0 $self->_throw_error("Converting $type to node not implemented", $t);
513 }
514 }
515  
516 61 ▶sub _throw_error {
Subroutine _throw_error: called CC=2 SCAR=6.9
517 61 my $self = shift;
518 61 my $message = shift;
519 61 my $t = shift;
520 61 my $l = shift;
521 61 my $line = $t->start_line;
522 61 my $col = $t->start_column;
523 # Carp::cluck "$message at $line:$col";
524 61 my $text = "$message at $line:$col";
525 61 ▶ if (defined($t->value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $t->value) 30 31 61
526 30 $text .= " (near '" . $t->value . "')";
527 }
528 RDF::Trine::Error::ParserError::Tokenized->throw(
529 61 -text => $text,
530 -object => $t,
531 );
532 }
533  
534 68 1;
535  
536 __END__
« RDF/Trine/Parser/RDFa.pm ↑ Summary RDF/Trine/Node/Resource.pm »