Tiny/Tokenizer.pm

filter: Statementstmt 95.6% 132 / 138 Branchbran 95.8% 46 / 48 Conditioncond 84.3% 27 / 32 MC/DCmcdc 66.6% 12 / 18 Subroutinesub 88.8% 16 / 18 Podpod 0.0% 0 / 12 totaltotal 87.5% 233 / 266 CC 39 SCAR 26.6
CC
39
Cov
94%
next_token
27.0
tokenize_string
4.0
unescape_chars
4.0
CRAP 14.3
« Tiny/Writer.pm ↑ Summary Tiny/Util.pm »
1 package TOML::Tiny::Tokenizer;
2 # ABSTRACT: tokenizer used by TOML::Tiny
3 287 $TOML::Tiny::Tokenizer::VERSION = '0.20';
4 287 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 287 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 287 ▶no warnings qw(experimental);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 287 ▶use charnames qw(:full);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 287 ▶use v5.18;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 287 use TOML::Tiny::Grammar qw(
11 $Comment
12 $CRLF
13 $DateTime
14 $EOL
15 $Escape
16 $Float
17 $Integer
18 $Key
19 $SimpleKey
20 $String
21 $WS
22 287 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 sub new {
25 427 ▶ my ($class, %param) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
26  
27 my $self = bless{
28 source => $param{source},
29 last_position => length $param{source},
30 427 position => 0,
31 line => 1,
32 last_token => undef,
33 }, $class;
34  
35 427 return $self;
36 }
37  
38 sub last_token {
39 0 ▶ my $self = shift;
Subroutine last_token: not called CC=1 SCAR=6.9
Pod undocumented
40 0 return $self->{last_token};
41 }
42  
43 sub next_token {
44 6522 ▶ my $self = shift;
Subroutine next_token: called CC=27 SCAR=33.0
Pod undocumented
45  
46 return unless defined $self->{source}
47 6522 ▶ && $self->{position} < $self->{last_position};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"source"} and $self->{"position"} < $self->{"last_position"} 247 6275 6522
Condition66%2/3
defined $self->{"source"} and $self->{"position"} < $self->{"last_position"}
!ll&&!rl&&r
02476275
MC/DC50%1/2
defined $self->{"source"} and $self->{"position"} < $self->{"last_position"}
defined $self->{"source"} $self->{"position"} < $self->{"last_position"}
Truth table66%2/3
A and B
A= defined $self->{"source"}B= $self->{"position"} < $self->{"last_position"}
ABresult
0X0
100
111
48  
49 6275 ▶ if (!$self->{last_token}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"last_token"}) 425 5850 6275
50 425 return $self->{last_token} = {type => 'table', pos => 0, line => 1, value => []};
51 }
52  
53 # Update the regex engine's position marker in case some other regex
54 # attempted to match against the source string and reset it.
55 5850 pos($self->{source}) = $self->{position};
56  
57 5850 my $token;
58 my $type;
59 5850 my $value;
60  
61 5850 state $key_set = qr/\G ($Key) $WS* (?= =)/x;
62 5850 state $table = qr/\G \[ $WS* ($Key) $WS* \] $WS* (?:$EOL | $)/x;
63 5850 state $array_table = qr/\G \[\[ $WS* ($Key) $WS* \]\] $WS* (?:$EOL | $)/x;
64  
65 5850 state $simple = {
66 '[' => 'inline_array',
67 ']' => 'inline_array_close',
68 '{' => 'inline_table',
69 '}' => 'inline_table_close',
70 ',' => 'comma',
71 '=' => 'assign',
72 'true' => 'bool',
73 'false' => 'bool',
74 };
75  
76 # More complex matches with regexps
77 5850 ▶ while ($self->{position} < $self->{last_position} && !defined($type)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"position"} < $self->{"last_position"} and not defined $type) 6048 5736 11784
Condition100%3/3
$self->{"position"} < $self->{"last_position"} and not defined $type
!ll&&!rl&&r
25454826048
MC/DC100%2/2
$self->{"position"} < $self->{"last_position"} and not defined $type
$self->{"position"} < $self->{"last_position"} not defined $type
Truth table100%3/3
A and not(B)
A= $self->{"position"} < $self->{"last_position"}B= not defined $type
ABresult
0X0
100
111
78 6048 ▶ my $prev = $self->{last_token} ? $self->{last_token}{type} : 'EOL';
Branch: 1 branch
BranchTrueFalseTotal
$self->{'last_token'} ? : 6048 0 6048
79 6048 ▶ my $newline = !!($prev eq 'EOL' || $prev eq 'table' || $prev eq 'array_table');
Condition100%6/6
$prev eq 'EOL' || $prev eq 'table' || $prev eq 'array_table'
l!l&&r!l&&!r
1993723983
$prev eq "EOL" or $prev eq "table"
l!l&&r!l&&!r
13146794055
MC/DC100%3/3
$prev eq 'EOL' || $prev eq 'table' || $prev eq 'array_table'
$prev eq "EOL" $prev eq "table" $prev eq 'array_table'
Truth table100%4/4
A or B || C
A= $prev eq "EOL"B= $prev eq "table"C= $prev eq 'array_table'
ABCresult
1XX1
01X1
0011
0000
80  
81 6048 for ($self->{source}) {
82 6048 /\G$WS+/gc; # ignore whitespace
83 6048 ▶ /\G$Comment$/mgc && next; # ignore comments
Branch: 1 branch
BranchTrueFalseTotal
/\G$TOML::Tiny::Tokenizer::Comment$/cgmu and next 100 5948 6048
84  
85 5948 ▶ last if /\G$/gc;
Branch: 1 branch
BranchTrueFalseTotal
if /\G$/cgu 101 5847 5948
86  
87 5847 ▶ if (/\G$EOL/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\G$TOML::Tiny::Tokenizer::EOL/cgu) 1366 4481 5847
88 1366 ++$self->{line};
89 1366 $type = 'EOL';
90 1366 last;
91 }
92  
93 4481 ▶ if ($newline) {
Branch: 1 branch
BranchTrueFalseTotal
if ($newline) 1602 2879 4481
94 1602 ▶ if (/$table/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if (/$table/cgu) 244 1358 1602
95 244 $type = 'table';
96 244 $value = $self->tokenize_key($1);
97 244 last;
98 }
99  
100 1358 ▶ if (/$array_table/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if (/$array_table/cgu) 75 1283 1358
101 75 $type = 'array_table';
102 75 $value = $self->tokenize_key($1);
103 75 last;
104 }
105 }
106  
107 4162 ▶ if (/$key_set/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if (/$key_set/cgu) 1091 3071 4162
108 1091 $type = 'key';
109 1091 $value = $1;
110 1091 last;
111 }
112  
113 3071 ▶ if (/\G ( [\[\]{}=,] | true | false )/xgc) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\G ( [\[\]{}=,] | true | false )/cgux) 1894 1177 3071
114 1894 $value = $1;
115 1894 $type = $simple->{$value};
116 1894 last;
117 }
118  
119 1177 ▶ if (/\G($String)/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\G($TOML::Tiny::Tokenizer::String)/cgu) 480 697 1177
120 480 $type = 'string';
121 480 $value = $1;
122 480 last;
123 }
124  
125 697 ▶ if (/\G($DateTime)/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\G($TOML::Tiny::Tokenizer::DateTime)/cgu) 57 640 697
126 57 $type = 'datetime';
127 57 $value = $1;
128 57 last;
129 }
130  
131 640 ▶ if (/\G($Float)/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\G($TOML::Tiny::Tokenizer::Float)/cgu) 102 538 640
132 102 $type = 'float';
133 102 $value = $1;
134 102 last;
135 }
136  
137 538 ▶ if (/\G($Integer)/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\G($TOML::Tiny::Tokenizer::Integer)/cgu) 429 109 538
138 429 $type = 'integer';
139 429 $value = $1;
140 429 last;
141 }
142  
143 109 ▶ my $substr = substr($self->{source}, $self->{position}, 30) // 'undef';
Condition50%1/2
substr($self->{"source"}, $self->{"position"}, 30) // "undef"
l!l
1090
MC/DC0%0/1
substr($self->{"source"}, $self->{"position"}, 30) // "undef"
substr($self->{"source"}, $self->{"position"}, 30)
Truth table50%1/2
A
A= substr($self->{"source"}, $self->{"position"}, 30)
Aresult
11
00
144 109 die "toml syntax error on line $self->{line}\n\t-->|$substr|\n";
145 }
146  
147 5939 ▶ if ($type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type) 5738 201 5939
148 5738 state $tokenizers = {};
149 5738 ▶ my $tokenize = $tokenizers->{$type} //= $self->can("tokenize_$type") || 0;
Condition100%5/5
$tokenizers->{$type} //= $self->can("tokenize_$type") || 0
l!l&&r!l&&!r
4631461646
$self->can("tokenize_$type") || 0
l!l
461646
MC/DC100%2/2
$tokenizers->{$type} //= $self->can("tokenize_$type") || 0
$tokenizers->{$type} $self->can("tokenize_$type")
Truth table100%3/3
A //= B
A= $tokenizers->{$type}B= $self->can("tokenize_$type")
ABresult
1X1
011
000
150  
151 $token = {
152 line => $self->{line},
153 pos => $self->{pos},
154 type => $type,
155 value => $tokenize ? $tokenize->($self, $value) : $value,
156 prev => $self->{last_token},
157 5738 ▶ };
Branch: 1 branch
BranchTrueFalseTotal
$tokenize ? : 2102 3636 5738
158  
159 # Unset the previous token's 'prev' key to prevent keeping the entire
160 # chain of previously parsed tokens alive for the whole process.
161 5733 undef $self->{last_token}{prev};
162  
163 5733 $self->{last_token} = $token;
164 }
165  
166 5934 $self->update_position;
167 }
168  
169 5736 return $token;
170 }
171  
172 sub current_line {
173 0 ▶ my $self = shift;
Subroutine current_line: not called CC=1 SCAR=6.9
Pod undocumented
174 0 my $rest = substr $self->{source}, $self->{position};
175 0 my $stop = index $rest, "\n";
176 0 substr $rest, 0, $stop;
177 }
178  
179 sub update_position {
180 5934 ▶ my $self = shift;
Subroutine update_position: called CC=2 SCAR=7.2
Pod undocumented
181 5934 ▶ $self->{position} = pos($self->{source}) // 0;
Condition50%1/2
pos $self->{"source"} // 0
l!l
59340
MC/DC0%0/1
pos $self->{"source"} // 0
pos $self->{"source"}
Truth table50%1/2
A
A= pos $self->{"source"}
Aresult
11
00
182 }
183  
184 sub error {
185 5 ▶ my $self = shift;
Subroutine error: called CC=3 SCAR=11.3
Pod undocumented
186 5 my $token = shift;
187 5 ▶ my $msg = shift // 'unknown';
Condition50%1/2
shift() // "unknown"
l!l
50
MC/DC0%0/1
shift() // "unknown"
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
188 5 ▶ my $line = $token ? $token->{line} : $self->{line};
Branch: 1 branch
BranchTrueFalseTotal
$token ? : 0 5 5
189 5 die "toml: parse error at line $line: $msg\n";
190 }
191  
192 sub tokenize_key {
193 1410 ▶ my $self = shift;
Subroutine tokenize_key: called CC=3 SCAR=11.0
Pod undocumented
194 1410 my $toml = shift;
195 1410 my @segs = $toml =~ /($SimpleKey)\.?/g;
196 1410 my @keys;
197  
198 1410 for my $seg (@segs) {
199 1692 ▶ $seg = $self->tokenize_string($seg) if $seg =~ m/^['"]/;
Branch: 1 branch
BranchTrueFalseTotal
if $seg =~ /^['"]/u 102 1590 1692
200 1692 push @keys, $seg;
201 }
202  
203 1410 return \@keys;
204 }
205  
206 sub tokenize_float {
207 102 ▶ $_[1] =~ tr/_//d;
Subroutine tokenize_float: called CC=1 SCAR=0.0
Pod undocumented
208 102 $_[1];
209 }
210  
211 sub tokenize_integer {
212 429 ▶ $_[1] =~ tr/_+//d;
Subroutine tokenize_integer: called CC=1 SCAR=0.0
Pod undocumented
213 429 $_[1];
214 }
215  
216 sub tokenize_string {
217 582 ▶ my $self = shift;
Subroutine tokenize_string: called CC=4 SCAR=13.9
Pod undocumented
218 582 my $toml = shift;
219 582 ▶ my $ml = index($toml, q{'''}) == 0
Condition100%3/3
index($toml, q[''']) == 0 || index($toml, '"""') == 0
l!l&&r!l&&!r
1520547
MC/DC100%2/2
index($toml, q[''']) == 0 || index($toml, '"""') == 0
index($toml, q[''']) == 0 index($toml, '"""') == 0
Truth table100%3/3
A || B
A= index($toml, q[''']) == 0B= index($toml, '"""') == 0
ABresult
1X1
011
000
220 || index($toml, q{"""}) == 0;
221 582 my $lit = index($toml, q{'}) == 0;
222 582 my $str = '';
223  
224 582 ▶ if ($ml) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ml) { } 35 547 582
225 35 $str = substr $toml, 3, length($toml) - 6;
226 35 my @newlines = $str =~ /($CRLF)/g;
227 35 $self->{line} += scalar @newlines;
228 35 $str =~ s/^$WS* $CRLF//x; # trim leading whitespace
229 35 $str =~ s/\\$EOL\s*//xgs; # trim newlines from lines ending in backslash
230 } else {
231 547 $str = substr($toml, 1, length($toml) - 2);
232 }
233  
234 582 ▶ if (!$lit) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($lit) 521 61 582
235 521 $str = $self->unescape_str($str);
236 }
237  
238 577 return $str;
239 }
240  
241 sub unescape_chars {
242 136 ▶ state $esc = {
Subroutine unescape_chars: called CC=4 SCAR=13.9
Pod undocumented
243 '\b' => "\x08",
244 '\t' => "\x09",
245 '\n' => "\x0A",
246 '\f' => "\x0C",
247 '\r' => "\x0D",
248 '\"' => "\x22",
249 '\/' => "\x2F",
250 '\\\\' => "\x5C",
251 };
252  
253 136 ▶ if (exists $esc->{$_[0]}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$esc{$_[0]}) 120 16 136
254 120 return $esc->{$_[0]};
255 }
256  
257 16 my $hex = hex substr($_[0], 2);
258  
259 16 ▶ if ($hex < 0x10FFFF && charnames::viacode($hex)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hex < 1114111 and charnames::viacode($hex)) 11 5 16
Condition100%3/3
$hex < 1114111 and charnames::viacode($hex)
!ll&&!rl&&r
4111
MC/DC100%2/2
$hex < 1114111 and charnames::viacode($hex)
$hex < 1114111 charnames::viacode($hex)
Truth table100%3/3
A and B
A= $hex < 1114111B= charnames::viacode($hex)
ABresult
0X0
100
111
260 11 return chr $hex;
261 }
262  
263 5 return;
264 }
265  
266 sub unescape_str {
267 521 ▶ state $re = qr/($Escape)/;
Subroutine unescape_str: called CC=2 SCAR=7.0
Pod undocumented
268 521 ▶ $_[1] =~ s|$re|unescape_chars($1) // $_[0]->error(undef, "invalid unicode escape: $1")|xge;
Condition66%2/3
unescape_chars($1) // $_[0]->error(undef, "invalid unicode escape: $1")
l!l&&r!l&&!r
13105
MC/DC0%0/2
unescape_chars($1) // $_[0]->error(undef, "invalid unicode escape: $1")
unescape_chars($1) $_[0]->error(undef, "invalid unicode escape: $1")
Truth table33%1/3
A // B
A= unescape_chars($1)B= $_[0]->error(undef, "invalid unicode escape: $1")
ABresult
1X1
011
000
269 516 $_[1];
270 }
271  
272 287 1;
273  
274 __END__
275  
276 =pod
277  
278 =encoding UTF-8
279  
280 =head1 NAME
281  
282 TOML::Tiny::Tokenizer - tokenizer used by TOML::Tiny
283  
284 =head1 VERSION
285  
286 version 0.20
287  
288 =head1 AUTHOR
289  
290 Jeff Ober <sysread@fastmail.fm>
291  
292 =head1 COPYRIGHT AND LICENSE
293  
294 This software is copyright (c) 2025 by Jeff Ober.
295  
296 This is free software; you can redistribute it and/or modify it under
297 the same terms as the Perl 5 programming language system itself.
298  
299 =cut
« Tiny/Writer.pm ↑ Summary Tiny/Util.pm »