AtteanX/Parser/Trig.pm

filter: Statementstmt 34.2% 51 / 149 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 64.0% 16 / 25 Podpod 100% 3 / 3 totaltotal 27.3% 70 / 256 CC 30 SCAR 39.5
CC
30
Cov
24%
_trigDoc
159.0
_block
56.0
_labelOrSubject
30.0
CRAP 52.1
« AtteanX/Parser/Turtle.pm ↑ Summary AtteanX/Parser/SPARQL.pm »
1 2 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 # AtteanX::Parser::Trig
5 # -----------------------------------------------------------------------------
6  
7 =head1 NAME
8  
9 AtteanX::Parser::Trig - Trig RDF Parser
10  
11 =head1 VERSION
12  
13 This document describes AtteanX::Parser::Trig version 0.040
14  
15 =head1 SYNOPSIS
16  
17 use Attean;
18 my $parser = AtteanX::Parser::Trig->new( handler => sub {...}, base => $base_iri );
19
20 # Parse data from a file-handle and handle triples in the 'handler' callback
21 $parser->parse_cb_from_io( $fh );
22
23 # Parse the given byte-string, and return an iterator of triples
24 my $iter = $parser->parse_iter_from_bytes('<s> <p> 1, 2, 3 .');
25 while (my $triple = $iter->next) {
26 print $triple->as_string;
27 }
28  
29 =head1 DESCRIPTION
30  
31 This module implements a parser for the Trig RDF format.
32  
33 =head1 ROLES
34  
35 This class consumes L<Attean::API::Parser>, L<Attean::API::PushParser>,
36 <Attean::API::AbbreviatingParser>, and <Attean::API::TripleParser>.
37  
38 =head1 ATTRIBUTES
39  
40 =over 4
41  
42 =item C<< canonical_media_type >>
43  
44 =item C<< media_types >>
45  
46 =item C<< file_extensions >>
47  
48 =item C<< canonicalize >>
49  
50 A boolean indicating whether term values should be canonicalized during parsing.
51  
52 =back
53  
54 =head1 METHODS
55  
56 =over 4
57  
58 =cut
59  
60 2 package AtteanX::Parser::Trig 0.040 {
61 2 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 2 ▶ use Types::Standard qw(Bool ArrayRef HashRef Str Maybe InstanceOf);
Subroutine BEGIN: called CC=1 SCAR=0.0
63 2 ▶ use Types::Namespace qw( NamespaceMap );
Subroutine BEGIN: called CC=1 SCAR=0.0
64 2 ▶ use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
65 2 ▶ use Carp qw(carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
66 2 ▶ use Encode qw(encode);
Subroutine BEGIN: called CC=1 SCAR=0.0
67 2 ▶ use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
68 2 ▶ use Attean::API::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
69 2 ▶ use AtteanX::Parser::Turtle;
Subroutine BEGIN: called CC=1 SCAR=0.0
70 2 ▶ use AtteanX::Parser::Turtle::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
71 2 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
72  
73 2 my $RDF = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
74 2 my $XSD = 'http://www.w3.org/2001/XMLSchema#';
75
76 2 extends 'AtteanX::Parser::Turtle';
77  
78 0 ▶ sub canonical_media_type { return "text/trig" }
Subroutine canonical_media_type: not called CC=1 SCAR=6.9
Pod documented
79  
80 3 ▶ sub media_types {
Subroutine media_types: called CC=1 SCAR=0.0
Pod documented
81 3 return [qw(text/trig)];
82 }
83  
84 1 ▶ sub file_extensions { return [qw(trig)] }
Subroutine file_extensions: called CC=1 SCAR=0.0
Pod documented
85  
86 2 has 'canonicalize' => (is => 'rw', isa => Bool, default => 0);
87 0 ▶ has '_map' => (is => 'ro', isa => HashRef[Str], default => sub { +{} });
Subroutine __ANON__: not called CC=1 SCAR=1.2
88  
89 2 with 'Attean::API::MixedStatementParser';
90
91 ################################################################################
92  
93 # this is the entry point where we change the rules from Turtle to Trig
94 0 ▶ sub _parse {
Subroutine _parse: not called CC=2 SCAR=17.9
95 0 my $self = shift;
96 0 my $l = shift;
97 0 $l->check_for_bom;
98 0 ▶ while (my $t = $self->_next_nonws($l)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $t = $self->_next_nonws($l)) 0 0 0
99 0 $self->_trigDoc($l, $t);
100 }
101 }
102  
103 0 ▶ sub _trigDoc {
Subroutine _trigDoc: not called CC=13 SCAR=50.7
104 0 my $self = shift;
105 0 my $l = shift;
106 0 my $t = shift;
107 0 my $type = $t->type;
108 0 ▶ if ($type == TURTLEPREFIX or $type == PREFIX) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type == 31 or $type == 20) { } 0 0 0
elsif ($type == 30 or $type == 3) { } 0 0 0
Condition0%0/3
$type == 31 or $type == 20
l!l&&r!l&&!r
000
MC/DC0%0/2
$type == 31 or $type == 20
$type == 31 $type == 20
Truth table0%0/3
A or B
A= $type == 31B= $type == 20
ABresult
1X1
011
000
109 0 $t = $self->_get_token_type($l, PREFIXNAME);
110 2 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
111 0 ▶ unless (defined($t->value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $t->value) 0 0 0
112 0 my $tname = AtteanX::Parser::Turtle::Constants::decrypt_constant($t->type);
113 0 Carp::confess "undefined $tname token value: " . Dumper($t);
114 }
115 0 my $name = $t->value;
116 0 ▶ chop($name) if (substr($name, -1) eq ':');
Branch: 1 branch
BranchTrueFalseTotal
if substr($name, -1) eq ":" 0 0 0
117 # $name =~ s/:$//;
118 0 $t = $self->_get_token_type($l, IRI);
119 0 my %args = (value => $t->value);
120 0 ▶ if ($self->has_base) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_base) 0 0 0
121 0 $args{base} = $self->base;
122 }
123 0 my $r = $self->new_iri(%args);
124 0 my $iri = $r->as_string;
125 0 ▶ if ($type == TURTLEPREFIX) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type == 31) 0 0 0
126 0 $t = $self->_get_token_type($l, DOT);
127 # $t = $self->_next_nonws($l);
128 # if ($t and $t->type != DOT) {
129 # $self->_unget_token($t);
130 # }
131 }
132 0 $self->_map->{$name} = $iri;
133 0 ▶ if ($self->has_namespaces) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_namespaces) 0 0 0
134 0 my $ns = $self->namespaces;
135 0 ▶ unless ($ns->namespace_uri($name)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ns->namespace_uri($name)) 0 0 0
136 0 $ns->add_mapping($name, $iri);
137 }
138 }
139 }
140 ▶ elsif ($type == TURTLEBASE or $type == BASE) {
Condition0%0/3
$type == 30 or $type == 3
l!l&&r!l&&!r
000
MC/DC0%0/2
$type == 30 or $type == 3
$type == 30 $type == 3
Truth table0%0/3
A or B
A= $type == 30B= $type == 3
ABresult
1X1
011
000
141 0 $t = $self->_get_token_type($l, IRI);
142 0 my %args = (value => $t->value);
143 0 ▶ if ($self->has_base) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_base) 0 0 0
144 0 $args{base} = $self->base;
145 }
146 0 my $r = $self->new_iri(%args);
147 0 my $iri = $r->as_string;
148 0 ▶ if ($type == TURTLEBASE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type == 30) 0 0 0
149 0 $t = $self->_get_token_type($l, DOT);
150 # $t = $self->_next_nonws($l);
151 # if ($t and $t->type != DOT) {
152 # $self->_unget_token($t);
153 # }
154 }
155 0 $self->base($iri);
156 }
157 else {
158 0 $self->_block( $l, $t );
159 }
160 # }
161 }
162  
163 0 ▶ sub _block {
Subroutine _block: not called CC=7 SCAR=40.3
164 0 my $self = shift;
165 0 my $l = shift;
166 0 my $t = shift;
167 0 my $type = $t->type;
168 0 ▶ if ($type == GRAPH) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type == 19) { } 0 0 0
elsif ($type == 16) { } 0 0 0
169 # "GRAPH" labelOrSubject wrappedGraph
170 0 my $graph = $self->_labelOrSubject($l);
171 0 local($self->{graph}) = $graph;
172 0 $t = $self->_get_token_type($l, LBRACE);
173 0 $self->_block($l, $t);
174 } elsif ($type == LBRACE) {
175 0 $t = $self->_next_nonws($l);
176 0 $type = $t->type;
177 0 ▶ while ($type != RBRACE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type != 22) 0 0 0
178 0 $self->_triples($l, $t);
179 0 $t = $self->_next_nonws($l);
180 0 $type = $t->type;
181 0 ▶ unless ($type == RBRACE or $type == DOT) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($type == 22 or $type == 9) 0 0 0
Condition0%0/3
$type == 22 or $type == 9
l!l&&r!l&&!r
000
MC/DC0%0/2
$type == 22 or $type == 9
$type == 22 $type == 9
Truth table0%0/3
A or B
A= $type == 22B= $type == 9
ABresult
1X1
011
000
182 # carp "Expected DOT or closing brace";
183 }
184 0 ▶ if ($type == DOT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type == 9) 0 0 0
185 0 $t = $self->_next_nonws($l);
186 0 $type = $t->type;
187 }
188 }
189 } else {
190 0 $self->_triplesOrGraph($l, $t);
191 }
192 }
193
194 0 ▶ sub _triplesOrGraph {
Subroutine _triplesOrGraph: not called CC=2 SCAR=17.9
195 0 my $self = shift;
196 0 my $l = shift;
197 0 my $t = shift;
198 0 my $next = $self->_peek_nonws($l);
199  
200 0 ▶ if ($next->type == LBRACE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next->type == 16) { } 0 0 0
201 0 my $graph = $self->_token_to_node($t);
202 0 local($self->{graph}) = $graph;
203 0 $t = $self->_get_token_type($l, LBRACE);
204 0 $self->_block($l, $t);
205 } else {
206 0 $self->_triples($l, $t);
207 0 $t = $self->_get_token_type($l, DOT);
208 }
209 }
210
211 0 ▶ sub _labelOrSubject {
Subroutine _labelOrSubject: not called CC=5 SCAR=34.0
212 0 my $self = shift;
213 0 my $l = shift;
214 0 my $t = $self->_next_nonws($l);
215 0 ▶ if ($t->type == IRI or $t->type == PREFIXNAME or $t->type == BNODE or $t->type == ANON) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t->type == 14 or $t->type == 21 or $t->type == 4 or $t->type == 2) { } 0 0 0
Condition0%0/9
$t->type == 14 or $t->type == 21 or $t->type == 4 or $t->type == 2
l!l&&r!l&&!r
000
$t->type == 14 or $t->type == 21 or $t->type == 4
l!l&&r!l&&!r
000
$t->type == 14 or $t->type == 21
l!l&&r!l&&!r
000
MC/DC0%0/4
$t->type == 14 or $t->type == 21 or $t->type == 4 or $t->type == 2
$t->type == 14 $t->type == 21 $t->type == 4 $t->type == 2
Truth table0%0/5
A or B or C or D
A= $t->type == 14B= $t->type == 21C= $t->type == 4D= $t->type == 2
ABCDresult
1XXX1
01XX1
001X1
00011
00000
216 0 return $self->_token_to_node($t);
217 } else {
218 0 $self->_throw_error(sprintf("Expecting graph name but got %s", decrypt_constant($t->type)), $t, $l);
219 }
220 }
221
222 0 ▶ sub _assert_triple {
Subroutine _assert_triple: not called CC=5 SCAR=34.0
223 0 my $self = shift;
224 0 my $subj = shift;
225 0 my $pred = shift;
226 0 my $obj = shift;
227 0 ▶ if ($self->canonicalize and blessed($obj) and $obj->does('Attean::API::Literal')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->canonicalize and builtin::blessed($obj) and $obj->does("Attean::API::Literal")) 0 0 0
Condition0%0/6
$self->canonicalize and builtin::blessed($obj) and $obj->does("Attean::API::Literal")
!ll&&!rl&&r
000
$self->canonicalize and builtin::blessed($obj)
!ll&&!rl&&r
000
MC/DC0%0/3
$self->canonicalize and builtin::blessed($obj) and $obj->does("Attean::API::Literal")
$self->canonicalize builtin::blessed($obj) $obj->does("Attean::API::Literal")
Truth table0%0/4
A and B and C
A= $self->canonicalizeB= builtin::blessed($obj)C= $obj->does("Attean::API::Literal")
ABCresult
0XX0
10X0
1100
1111
228 0 $obj = $obj->canonicalize;
229 }
230
231 0 my $graph = $self->{graph};
232 0 ▶ my $t = (defined($graph))
Branch: 1 branch
BranchTrueFalseTotal
defined $graph ? : 0 0 0
233 ? Attean::Quad->new($subj, $pred, $obj, $graph)
234 : Attean::Triple->new($subj, $pred, $obj);
235 0 $self->handler->($t);
236 0 return $t;
237 }
238  
239 0 ▶ sub _throw_error {
Subroutine _throw_error: not called CC=2 SCAR=17.9
240 0 my $self = shift;
241 0 my $message = shift;
242 0 my $t = shift;
243 0 my $l = shift;
244 0 my $line = $t->start_line;
245 0 my $col = $t->start_column;
246 # Carp::cluck "$message at $line:$col";
247 0 my $text = "$message at $line:$col";
248 0 ▶ if (defined($t->value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $t->value) 0 0 0
249 0 $text .= " (near '" . $t->value . "')";
250 }
251 # Carp::cluck "TriG parser error: $text";
252 0 die $text;
253 }
254 }
255  
256 2 1;
257  
258 __END__
« AtteanX/Parser/Turtle.pm ↑ Summary AtteanX/Parser/SPARQL.pm »