AtteanX/Parser/Turtle/Token.pm

filter: Statementstmt 59.1% 58 / 98 Branchbran 5.8% 2 / 34 Conditioncond 20.0% 4 / 20 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 75.0% 12 / 16 Podpod 100% 5 / 5 totaltotal 43.7% 81 / 185 CC 110 SCAR 32.9
CC
110
Cov
42%
token_as_string
90.0
is_string
30.0
a
24.4
CRAP 26.7
« AtteanX/Parser/SPARQLXML/SAXHandler.pm ↑ Summary Attean/DatasetModel.pm »
1 50 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 50 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 =head1 NAME
5  
6 AtteanX::Parser::Turtle::Token - Token objects used for parsing of Turtle
7  
8 =head1 VERSION
9  
10 This document describes AtteanX::Parser::Turtle::Token version 0.040
11  
12 =head1 SYNOPSIS
13  
14 use v5.14;
15 use Attean;
16 my $term = Attean::Blank->new('b1');
17 $term->ntriples_string; # _:b1
18  
19 =head1 DESCRIPTION
20  
21 The AtteanX::Parser::Turtle::Token class represents tokens produced and used
22 during parsing of Turtle.
23  
24 =head1 ATTRIBUTES
25  
26 =over 4
27  
28 =item C<< type >>
29  
30 An integer indicating the token type, defined in L<AtteanX::Parser::Turtle::Constants>
31  
32 =item C<< start_line >>
33  
34 The line number in the source text that this token begins on.
35  
36 =item C<< start_column >>
37  
38 The column number in the source text that this token begins on.
39  
40 =item C<< line >>
41  
42 The line number in the source text that this token ends on.
43  
44 =item C<< column >>
45  
46 The column number in the source text that this token ends on.
47  
48 =item C<< args >>
49  
50 An array of values associated with the token (e.g. the integer value of an INT token).
51  
52 =back
53  
54 =head1 METHODS
55  
56 =over 4
57  
58 =cut
59  
60 package AtteanX::Parser::Turtle::Token;
61  
62 50 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 50 ▶use Types::Standard qw(ArrayRef Str);
Subroutine BEGIN: called CC=1 SCAR=0.0
64 50 ▶use List::Util qw(mesh);
Subroutine BEGIN: called CC=1 SCAR=0.0
65 50 ▶use Sub::Util qw(set_subname);
Subroutine BEGIN: called CC=1 SCAR=0.0
66 50 ▶use AtteanX::Parser::Turtle::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 50 ▶use Sub::Install;
Subroutine BEGIN: called CC=1 SCAR=0.0
68 50 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
69  
70 50 our $VERSION = '0.040';
71  
72 50 has type => ( is => 'ro', );
73 50 has start_line => ( is => 'ro', );
74 50 has start_column => ( is => 'ro', );
75 50 has line => ( is => 'ro', );
76 50 has column => ( is => 'ro', );
77 50 has args => ( is => 'ro', isa => ArrayRef[Str]);
78  
79 =item C<< value >>
80  
81 Returns the token value.
82  
83 =cut
84  
85 283 ▶sub value {
Subroutine value: called CC=1 SCAR=0.0
Pod documented
86 283 my $self = shift;
87 283 my $args = $self->args;
88 283 return $args->[0];
89 }
90  
91 =item C<< fast_constructor ( $type, $start_line, $start_col, $line, $col, \@args ) >>
92  
93 Returns a new token object.
94  
95 =cut
96  
97 50 my @KEYS = qw(type start_line start_column line column args);
98 529 ▶sub fast_constructor {
Subroutine fast_constructor: called CC=1 SCAR=0.0
Pod documented
99 529 my $class = shift;
100 529 return $class->new(
101 mesh \@KEYS, \@_
102 );
103 }
104  
105 50 my %token_strings;
106 {
107 50 my %tokens = (
108 a => [A, 'a'],
109 prefix => [PREFIX, '@prefix'],
110 base => [BASE, '@base'],
111 lparen => [LPAREN, '('],
112 rparen => [RPAREN, ')'],
113 lbracket => [LBRACKET, '['],
114 rbracket => [RBRACKET, ']'],
115 ltlt => [LTLT, '<<'],
116 gtgt => [GTGT, '>>'],
117 ltltp => [LTLT, '<<('],
118 pgtgt => [GTGT, ')>>'],
119 dot => [DOT, '.'],
120 comma => [COMMA, ','],
121 semicolon => [SEMICOLON, ';'],
122 hathat => [HATHAT, '^^'],
123 tilde => [TILDE, '~'],
124 );
125 50 for my $name (keys %tokens) {
126 800 my ($type, $value) = @{ $tokens{ $name } };
127 0 ▶ my $code = sub {
Subroutine ltlt: not called CC=7 SCAR=32.0
Subroutine semicolon: called CC=7 SCAR=32.0
128 0 my $class = shift;
129 0 ▶ my $sl = shift // -1;
Condition25%1/4
shift() // -1
l!l
00
shift() // -1
l!l
042
MC/DC0%0/1
shift() // -1
shift()
MC/DC0%0/1
shift() // -1
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
Truth table50%1/2
A
A= shift()
Aresult
11
00
130 0 ▶ my $sc = shift // -1;
Condition25%1/4
shift() // -1
l!l
00
shift() // -1
l!l
042
MC/DC0%0/1
shift() // -1
shift()
MC/DC0%0/1
shift() // -1
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
Truth table50%1/2
A
A= shift()
Aresult
11
00
131 0 ▶ my $l = shift // $sl;
Condition16%1/6
shift() // $sl
l!l&&r!l&&!r
000
shift() // $sl
l!l&&r!l&&!r
0420
MC/DC0%0/2
shift() // $sl
shift() $sl
MC/DC0%0/2
shift() // $sl
shift() $sl
Truth table0%0/3
A // B
A= shift()B= $sl
ABresult
1X1
011
000
Truth table33%1/3
A // B
A= shift()B= $sl
ABresult
1X1
011
000
132 0 ▶ my $c = shift // $sc;
Condition16%1/6
shift() // $sc
l!l&&r!l&&!r
000
shift() // $sc
l!l&&r!l&&!r
0420
MC/DC0%0/2
shift() // $sc
shift() $sc
MC/DC0%0/2
shift() // $sc
shift() $sc
Truth table0%0/3
A // B
A= shift()B= $sc
ABresult
1X1
011
000
Truth table33%1/3
A // B
A= shift()B= $sc
ABresult
1X1
011
000
133 0 ▶ if ($sl > $l) { die '$start_line cannot be greater than $line in AtteanX::Parser::Turtle::Token constructor' }
Branch: 2 branches
BranchTrueFalseTotal
if ($sl > $l) 0 0 0
if ($sl > $l) 0 42 42
134 0 ▶ if ($sc > $c) { die '$start_line cannot be greater than $line in AtteanX::Parser::Turtle::Token constructor' }
Branch: 2 branches
BranchTrueFalseTotal
if ($sc > $c) 0 0 0
if ($sc > $c) 0 42 42
135 0 return $class->fast_constructor($type, $sl, $sc, $l, $c, [$value]);
136 800 };
137 800 Sub::Install::install_sub({
138 code => set_subname($name, $code),
139 as => $name
140 });
141 800 $token_strings{$type} = $value;
142 }
143
144 }
145  
146 =item C<< token_as_string() >>
147  
148 Returns a string version of the token (without escaping).
149  
150 =cut
151  
152 0 ▶sub token_as_string {
Subroutine token_as_string: not called CC=9 SCAR=45.0
Pod documented
153 0 my $self = shift;
154 0 my $type = $self->type;
155 0 my @args = @{ $self->args };
156 0 my $value = $args[0];
157 0 ▶ if (defined(my $v = $token_strings{$type})) {
Branch: 8 branches
BranchTrueFalseTotal
if (defined(my $v = $token_strings{$type})) { } 0 0 0
elsif ($type == 26) { } 0 0 0
elsif ($type == 27) { } 0 0 0
elsif ($type == 28) { } 0 0 0
elsif ($type == 29) { } 0 0 0
elsif ($type == 14) { } 0 0 0
elsif ($type == 4) { } 0 0 0
elsif ($type == 15) { } 0 0 0
158 0 return $v;
159 } elsif ($type == STRING1D) {
160 0 return qq["$value"];
161 } elsif ($type == STRING1S) {
162 0 return qq["$value"];
163 } elsif ($type == STRING3D) {
164 0 return qq["""$value"""];
165 } elsif ($type == STRING3S) {
166 0 return qq['''$value'''];
167 } elsif ($type == IRI) {
168 0 return qq[<$value>];
169 } elsif ($type == BNODE) {
170 0 return qq[_:$value]
171 } elsif ($type == LANG) {
172 0 return qq[\@$value]
173 } else {
174 0 join(', ', @args);
175 }
176 }
177  
178 =item C<< is_string() >>
179  
180 Returns true if the token is one of the quoted string types (STRING1D, STRING3D,
181 STRING1S, or STRING3S), false otherwise.
182  
183 =cut
184  
185 0 ▶sub is_string {
Subroutine is_string: not called CC=5 SCAR=34.0
Pod documented
186 0 my $self = shift;
187 0 my $type = $self->type;
188 0 ▶ return 1 if ($type == STRING1D);
Branch: 1 branch
BranchTrueFalseTotal
if $type == 26 0 0 0
189 0 ▶ return 1 if ($type == STRING1S);
Branch: 1 branch
BranchTrueFalseTotal
if $type == 27 0 0 0
190 0 ▶ return 1 if ($type == STRING3D);
Branch: 1 branch
BranchTrueFalseTotal
if $type == 28 0 0 0
191 0 ▶ return 1 if ($type == STRING3S);
Branch: 1 branch
BranchTrueFalseTotal
if $type == 29 0 0 0
192 }
193  
194 =item C<< as_string >>
195  
196 Returns a string description of the token including the token type and any
197 associated values.
198  
199 =cut
200  
201 0 ▶sub as_string {
Subroutine as_string: not called CC=2 SCAR=17.9
Pod documented
202 0 my $self = shift;
203 0 my $type = decrypt_constant($self->type);
204 0 my @args = @{ $self->args };
205 0 ▶ if (scalar(@args)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @args) { } 0 0 0
206 0 return "$type(" . join(', ', @args) . ")";
207 } else {
208 0 return $type;
209 }
210 }
211  
212 50 __PACKAGE__->meta->make_immutable;
213  
214 50 1;
215  
216 =back
217  
218 =head1 BUGS
219  
220 Please report any bugs or feature requests to through the GitHub web interface
221 at L<https://github.com/kasei/attean/issues>.
222  
223 =head1 SEE ALSO
224  
225  
226  
227 =head1 AUTHOR
228  
229 Gregory Todd Williams C<< <gwilliams@cpan.org> >>
230  
231 =head1 COPYRIGHT
232  
233 Copyright (c) 2014--2026 Gregory Todd Williams.
234 This program is free software; you can redistribute it and/or modify it under
235 the same terms as Perl itself.
236  
237 =cut
« AtteanX/Parser/SPARQLXML/SAXHandler.pm ↑ Summary Attean/DatasetModel.pm »