AtteanX/Serializer/TurtleTokens.pm

filter: Statementstmt 95.8% 161 / 168 Branchbran 87.1% 61 / 70 Conditioncond 53.3% 8 / 15 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 100% 17 / 17 Podpod 100% 6 / 6 totaltotal 90.1% 256 / 284 CC 42 SCAR 34.8
CC
42
Cov
91%
serialize_iter_to_io
44.5
BEGIN
1.0
BEGIN
1.0
CRAP 32.5
« AtteanX/Parser/Turtle/Lexer.pm ↑ Summary AtteanX/Parser/SPARQLLex.pm »
1 =head1 NAME
2  
3 AtteanX::Serializer::TurtleTokens - Turtle Serializer
4  
5 =head1 VERSION
6  
7 This document describes AtteanX::Serializer::TurtleTokens version 0.040
8  
9 =head1 SYNOPSIS
10  
11 use Attean;
12 my $serializer = Attean->get_serializer('Turtle')->new();
13 $serializer->serialize_iter_to_io( $io, $fh );
14  
15 =head1 DESCRIPTION
16  
17 ...
18  
19 =head1 ATTRIBUTES
20  
21 =over 4
22  
23 =item C<< canonical_media_type >>
24  
25 =item C<< media_types >>
26  
27 =item C<< handled_type >>
28  
29 =item C<< file_extensions >>
30  
31 =back
32  
33 =head1 METHODS
34  
35 =over 4
36  
37 =cut
38  
39 50 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 50 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 49 package AtteanX::Serializer::TurtleTokens 0.040 {
43 50 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 50 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 50 ▶ use Types::Standard qw(Bool ArrayRef HashRef ConsumerOf);
Subroutine BEGIN: called CC=1 SCAR=0.0
46 50 ▶ use Encode qw(encode);
Subroutine BEGIN: called CC=1 SCAR=0.0
47 50 ▶ use Attean::ListIterator;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 50 ▶ use List::Util qw(any);
Subroutine BEGIN: called CC=1 SCAR=0.0
49 50 ▶ use AtteanX::Parser::Turtle::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 50 ▶ use AtteanX::Parser::Turtle::Lexer;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 50 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 49 with 'Attean::API::AbbreviatingSerializer';
53 49 with 'Attean::API::AppendableSerializer';
54  
55 49 has suppress_whitespace => (is => 'rw', isa => Bool, default => 0);
56
57 1 ▶ sub canonical_media_type { return "text/turtle" }
Subroutine canonical_media_type: called CC=1 SCAR=0.0
Pod documented
58  
59 13 ▶ sub media_types {
Subroutine media_types: called CC=1 SCAR=0.0
Pod documented
60 13 return [qw(text/turtle)];
61 }
62
63 1 ▶ sub handled_type {
Subroutine handled_type: called CC=1 SCAR=0.0
Pod documented
64 1 state $ITEM_TYPE = Type::Tiny::Role->new(role => 'AtteanX::Parser::Turtle::Token');
65 1 return $ITEM_TYPE;
66 }
67  
68 1 ▶ sub file_extensions { return [qw(ttl)] }
Subroutine file_extensions: called CC=1 SCAR=0.0
Pod documented
69  
70  
71 =item C<< serialize_iter_to_io( $fh, $iterator ) >>
72  
73 Serializes the Turtle token objects from C<< $iterator >> to the
74 L<IO::Handle> object C<< $fh >>.
75  
76 =cut
77  
78 13 ▶ sub serialize_iter_to_io {
Subroutine serialize_iter_to_io: called CC=42 SCAR=38.0
Pod documented
79 13 my $self = shift;
80 13 my $io = shift;
81 13 my $iter = shift;
82 13 my $indent = 0;
83 13 my $newline = 1;
84 13 my $semicolon = 0;
85 13 my $need_space = 0;
86  
87 13 my $map = $self->namespaces;
88 13 my %namespace_map;
89 13 ▶ if ($map) {
Branch: 1 branch
BranchTrueFalseTotal
if ($map) 4 9 13
90 4 foreach my $p ($map->list_prefixes) {
91 5 my $prefix = $map->namespace_uri($p)->as_string;
92 5 $namespace_map{$prefix} = $p;
93 }
94 }
95
96 13 ▶ while (my $t = $iter->next()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $t = $iter->next) 109 13 122
97 109 my $type = $t->type;
98
99 109 ▶ if ($map) {
Branch: 1 branch
BranchTrueFalseTotal
if ($map) 36 73 109
100 36 ▶ if ($type == IRI) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type == 14) 10 26 36
101 10 my $value = $t->value;
102 10 ▶ if ($value =~ /^(?<namespace>.*?)(?<local>$AtteanX::Parser::Turtle::Lexer::r_PN_LOCAL)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^(?<namespace>.*?)(?<local>$AtteanX::Parser::Turtle::Lexer::r_PN_LOCAL)$/u) 10 0 10
103 10 ▶ if (my $ns = $namespace_map{$+{namespace}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ns = $namespace_map{$+{"namespace"}}) 5 5 10
104 5 $type = PREFIXNAME;
105 5 $t = AtteanX::SPARQL::Token->fast_constructor( $type, $t->start_line, $t->start_column, $t->line, $t->column, ["${ns}:", $+{local}] );
106 }
107 }
108 }
109 }
110
111 109 ▶ if ($type == LANG or $type == HATHAT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type == 15 or $type == 12) 3 106 109
Condition100%3/3
$type == 15 or $type == 12
l!l&&r!l&&!r
21106
MC/DC100%2/2
$type == 15 or $type == 12
$type == 15 $type == 12
Truth table100%3/3
A or B
A= $type == 15B= $type == 12
ABresult
1X1
011
000
112 3 $need_space= 0;
113 }
114
115 109 ▶ unless ($newline) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($newline) 85 24 109
116 85 ▶ if ($type == BASE or $type == PREFIX or $type == TURTLEBASE or $type == TURTLEPREFIX) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type == 3 or $type == 20 or $type == 30 or $type == 31) 0 85 85
Condition33%3/9
$type == 3 or $type == 20 or $type == 30 or $type == 31
l!l&&r!l&&!r
0085
$type == 3 or $type == 20 or $type == 30
l!l&&r!l&&!r
0085
$type == 3 or $type == 20
l!l&&r!l&&!r
0085
MC/DC0%0/4
$type == 3 or $type == 20 or $type == 30 or $type == 31
$type == 3 $type == 20 $type == 30 $type == 31
Truth table20%1/5
A or B or C or D
A= $type == 3B= $type == 20C= $type == 30D= $type == 31
ABCDresult
1XXX1
01XX1
001X1
00011
00000
117 0 $io->print("\n");
118 0 $newline = 1;
119 }
120 }
121
122 109 ▶ unless ($self->suppress_whitespace) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->suppress_whitespace) 109 0 109
123 109 ▶ if ($newline) {
Branch: 2 branches
BranchTrueFalseTotal
if ($newline) { } 24 85 109
elsif ($need_space) { } 80 5 85
124 24 $io->print(' ' x $indent);
125 24 $newline = 0;
126 } elsif ($need_space) {
127 80 $io->print(' ');
128 80 $need_space = 0;
129 }
130 }
131
132 109 ▶ if ($type == PREFIX or $type == TURTLEPREFIX) {
Branch: 13 branches
BranchTrueFalseTotal
if ($type == 20 or $type == 31) { } 5 104 109
elsif ($type == 21) { } 6 98 104
elsif ($type == 4) { } 11 87 98
elsif ($type == 14) { } 18 69 87
elsif ($type == 15) { } 2 67 69
elsif ($type == 27) { } 1 66 67
elsif ($type == 26) { } 9 57 66
elsif ($type == 29) { } 1 56 57
elsif ($type == 28) { } 2 54 56
elsif ($type == 1) { } 1 53 54
elsif ($type == 41) { } 0 53 53
elsif ($type == 7) { } 1 52 53
elsif ($type == 12) { } 1 51 52
Condition66%2/3
$type == 20 or $type == 31
l!l&&r!l&&!r
50104
MC/DC50%1/2
$type == 20 or $type == 31
$type == 20 $type == 31
Truth table66%2/3
A or B
A= $type == 20B= $type == 31
ABresult
1X1
011
000
133 # If we're serializing a PREFIX, also serialize the PREFIXNAME
134 # and IRI that must follow it so that we don't accidentally
135 # shorten the prefix IRI with its own namespace. For example,
136 # if we didn't serialize the PREFIXNAME and IRI here, we might
137 # end up with this:
138 #
139 # @prefix foaf: foaf:
140 #
141 # instead of:
142 #
143 # @prefix foaf: <http://xmlns.com/foaf/0.1/>
144 5 $io->print($t->value);
145 5 $io->print(' ');
146 5 my $pname = $iter->next();
147 5 ▶ unless ($pname->type == PREFIXNAME) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pname->type == 21) 0 5 5
148 0 die "PREFIX namespace not found during Turtle serialization";
149 }
150 5 my $args = $pname->args;
151 5 $io->print(join('', @$args));
152 5 $io->print(' ');
153
154 5 my $iri = $iter->next();
155 5 ▶ unless ($iri->type == IRI) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($iri->type == 14) 0 5 5
156 0 die "PREFIX IRI not found during Turtle serialization";
157 }
158 5 $io->print('<');
159 5 $io->print($iri->value);
160 5 $io->print('>');
161 5 $need_space++;
162 } elsif ($type == PREFIXNAME) {
163 6 my $args = $t->args;
164 6 $io->print(join('', @$args));
165 6 $need_space++;
166 } elsif ($type == BNODE) {
167 11 $io->print('_:');
168 11 $io->print($t->value);
169 11 $need_space++;
170 } elsif ($type == IRI) {
171 # TODO: escape
172 18 $io->print('<');
173 18 $io->print($t->value);
174 18 $io->print('>');
175 18 $need_space++;
176 } elsif ($type == LANG) {
177 2 $io->print('@');
178 2 $io->print($t->value);
179 2 $need_space++;
180 } elsif ($type == STRING1S) {
181 1 my $value = $t->value;
182 1 $value =~ s/'/\\'/g;
183 1 $io->print("'");
184 1 $io->print($value);
185 1 $io->print("'");
186 1 $need_space++;
187 } elsif ($type == STRING1D) {
188 9 my $value = $t->value;
189 9 $value =~ s/"/\\"/g;
190 9 $io->print('"');
191 9 $io->print($value);
192 9 $io->print('"');
193 9 $need_space++;
194 } elsif ($type == STRING3S) {
195 1 my $value = $t->value;
196 1 $value =~ s/'''/''\\'/g;
197 1 $io->print("'''");
198 1 ▶ if ($value =~ /'$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /'$/u) 0 1 1
199 0 chop($value);
200 0 $value .= "\\'";
201 }
202 1 $io->print($value);
203 1 $io->print("'''");
204 1 $need_space++;
205 } elsif ($type == STRING3D) {
206 2 my $value = $t->value;
207 2 $value =~ s/"""/""\\"/g;
208 2 $io->print('"""');
209 2 ▶ if ($value =~ /"$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /"$/u) 1 1 2
210 1 chop($value);
211 1 $value .= "\\\"";
212 }
213 2 $io->print($value);
214 2 $io->print('"""');
215 2 $need_space++;
216 } elsif ($type == A) {
217 1 $io->print('a');
218 1 $need_space++;
219 } elsif ($type == WS) {
220 } elsif ($type == COMMENT) {
221 1 ▶ if ($t->value =~ /\n/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t->value =~ /\n/u) 0 1 1
222 0 die "Unexpected newline found in Turtle comment token";
223 }
224 1 $io->print('# ');
225 1 $io->print($t->value);
226 1 $io->print("\n");
227 } elsif ($type == HATHAT) {
228 1 $io->print($t->value);
229 } else {
230 51 $io->print($t->value);
231 51 $need_space++;
232 }
233
234 109 ▶ if ($type == DOT) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type == 9) { } 19 90 109
elsif ($type == 25) { } 4 86 90
235 19 ▶ if ($semicolon) {
Branch: 1 branch
BranchTrueFalseTotal
if ($semicolon) 4 15 19
236 4 $indent--;
237 4 $semicolon = 0;
238 }
239 19 $need_space = 0;
240 19 $io->print("\n");
241 19 $newline = 1;
242 } elsif ($type == SEMICOLON) {
243 4 $io->print("\n");
244 4 $need_space = 0;
245 4 $newline = 1;
246 4 ▶ unless ($semicolon) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($semicolon) 4 0 4
247 4 $indent++;
248 }
249 4 $semicolon = 1;
250 }
251 }
252 13 ▶ unless ($newline) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($newline) 1 12 13
253 1 $io->print("\n");
254 }
255 13 return;
256 }
257
258 =item C<< serialize_iter_to_bytes( $iterator ) >>
259  
260 Serializes the Turtle token objects from C<< $iterator >>
261 and returns the serialization as a UTF-8 encoded byte string.
262  
263 =cut
264  
265 1 ▶ sub serialize_iter_to_bytes {
Subroutine serialize_iter_to_bytes: called CC=1 SCAR=0.0
Pod documented
266 1 my $self = shift;
267 1 my $iter = shift;
268 1 my $data = '';
269 1 open(my $fh, '>:utf8', \$data);
270 1 $self->serialize_iter_to_io($fh, $iter);
271 1 close($fh);
272 1 return $data;
273 }
274 }
275  
276 49 1;
277  
278 __END__
« AtteanX/Parser/Turtle/Lexer.pm ↑ Summary AtteanX/Parser/SPARQLLex.pm »