Tiny/Parser.pm

filter: Statementstmt 96.1% 300 / 312 Branchbran 92.5% 111 / 120 Conditioncond 70.2% 26 / 37 MC/DCmcdc 50.0% 11 / 22 Subroutinesub 97.3% 37 / 38 Podpod 0.0% 0 / 21 totaltotal 88.1% 485 / 550 CC 78 SCAR 18.8
CC
78
Cov
93%
declare_key
14.1
inflate_integer
9.0
parse_table
9.0
CRAP 6.5
« Tiny/Util.pm ↑ Summary Tiny.pm »
1 package TOML::Tiny::Parser;
2 # ABSTRACT: parser used by TOML::Tiny
3 287 $TOML::Tiny::Parser::VERSION = '0.21';
4 287 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 287 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 287 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 287 ▶no warnings qw(experimental);
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 Carp qw(confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 287 ▶use Data::Dumper qw(Dumper);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 287 ▶use Encode qw(decode FB_CROAK);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 287 ▶use Math::BigFloat ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 287 ▶use Math::BigInt ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 287 ▶use TOML::Tiny::Grammar qw($TimeOffset);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 287 ▶use TOML::Tiny::Tokenizer ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 287 our $TRUE = 1;
19 287 our $FALSE = 0;
20  
21 287 eval{
22 287 require Types::Serialiser;
23 287 $TRUE = Types::Serialiser::true();
24 287 $FALSE = Types::Serialiser::false();
25 };
26  
27 sub new {
28 430 ▶ my ($class, %param) = @_;
Subroutine new: called CC=3 SCAR=11.4
Pod undocumented
29 bless{
30 inflate_integer => $param{inflate_integer},
31 inflate_float => $param{inflate_float},
32 51 ▶ inflate_datetime => $param{inflate_datetime} || sub{ shift },
Subroutine __ANON__: called CC=1 SCAR=0.0
33 33 ▶ inflate_boolean => $param{inflate_boolean} || sub{ shift eq 'true' ? $TRUE : $FALSE },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
shift() eq 'true' ? : 17 16 33
34 strict => $param{strict},
35 430 ▶ }, $class;
Condition50%3/6
$param{'inflate_datetime'} || sub {
l!l&&r!l&&!r
14290
$param{'inflate_boolean'} || sub {
l!l&&r!l&&!r
04300
MC/DC0%0/2
$param{'inflate_datetime'} || sub { shift(); }
$param{'inflate_datetime'} sub { shift(); }
MC/DC0%0/2
$param{'inflate_boolean'} || sub { shift() eq 'true' ? $TRUE : $FALSE; }
$param{'inflate_boolean'} sub { shift() eq 'true' ? $TRUE : $FALSE; }
Truth table66%2/3
A || B
A= $param{'inflate_datetime'}B= sub { shift(); }
ABresult
1X1
011
000
Truth table33%1/3
A || B
A= $param{'inflate_boolean'}B= sub { shift() eq 'true' ? $TRUE : $FALSE; }
ABresult
1X1
011
000
36 }
37  
38 sub next_token {
39 6132 ▶ my $self = shift;
Subroutine next_token: called CC=2 SCAR=7.0
Pod undocumented
40 6132 ▶ my $token = $self->{tokenizer} && $self->{tokenizer}->next_token;
Condition66%2/3
$self->{'tokenizer'} && $self->{'tokenizer'}->next_token
!ll&&!rl&&r
03615771
MC/DC50%1/2
$self->{'tokenizer'} && $self->{'tokenizer'}->next_token
$self->{'tokenizer'} $self->{'tokenizer'}->next_token
Truth table66%2/3
A && B
A= $self->{'tokenizer'}B= $self->{'tokenizer'}->next_token
ABresult
0X0
100
111
41 6018 return $token;
42 }
43  
44 sub parse {
45 430 ▶ my ($self, $toml) = @_;
Subroutine parse: called CC=2 SCAR=6.9
Pod undocumented
46  
47 430 ▶ if ($self->{strict}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"strict"}) 185 245 430
48 185 $toml = decode('UTF-8', "$toml", FB_CROAK);
49 }
50  
51 424 $self->{tokenizer} = TOML::Tiny::Tokenizer->new(source => $toml);
52 424 $self->{keys} = [];
53 424 $self->{root} = {};
54 424 $self->{tables} = {}; # "seen" hash of explicitly defined table names (e.g. [foo])
55 424 $self->{arrays} = {}; # "seen" hash of explicitly defined static arrays (e.g. foo=[])
56 424 $self->{array_tables} = {}; # "seen" hash of explicitly defined arrays of tables (e.g. [[foo]])
57  
58 424 $self->parse_table;
59 245 my $result = $self->{root};
60  
61 245 delete $self->{tokenizer};
62 245 delete $self->{keys};
63 245 delete $self->{root};
64 245 delete $self->{tables};
65 245 delete $self->{arrays};
66 245 delete $self->{array_tables};
67  
68 245 return $result;
69 }
70  
71 sub parse_error {
72 60 ▶ my ($self, $token, $msg) = @_;
Subroutine parse_error: called CC=3 SCAR=13.0
Pod undocumented
73 60 ▶ my $line = $token ? $token->{line} : 'EOF';
Branch: 1 branch
BranchTrueFalseTotal
$token ? : 58 2 60
74 60 ▶ if ($ENV{TOML_TINY_DEBUG}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{'TOML_TINY_DEBUG'}) { } 0 60 60
75 0 my $root = Dumper($self->{root});
76 0 my $tok = Dumper($token);
77 0 my $src = substr $self->{tokenizer}{source}, $self->{tokenizer}{position}, 30;
78  
79 0 confess qq{
80 toml parse error at line $line:
81 $msg
82  
83 Current token:
84 $tok
85  
86 Parse state:
87 $root
88  
89 Source near location of error:
90 ...
91 $src
92 ...
93  
94 };
95 } else {
96 60 die "toml parse error at line $line: $msg\n";
97 }
98 }
99  
100 sub expect_type {
101 3425 ▶ my ($self, $token, $expected) = @_;
Subroutine expect_type: called CC=3 SCAR=11.0
Pod undocumented
102 3425 ▶ my $actual = $token ? $token->{type} : 'EOF';
Branch: 1 branch
BranchTrueFalseTotal
$token ? : 3423 2 3425
103 3425 ▶ $self->parse_error($token, "expected $expected, but found $actual")
Branch: 1 branch
BranchTrueFalseTotal
unless $actual =~ /$expected/u 28 3397 3425
104 unless $actual =~ /$expected/;
105 }
106  
107  
108 sub current_key {
109 862 ▶ my $self = shift;
Subroutine current_key: called CC=1 SCAR=0.0
Pod undocumented
110 862 my @keys = $self->get_keys;
111 862 my $key = join '.', map{ qq{"$_"} } @keys;
112 862 return $key;
113 }
114  
115 sub push_keys {
116 1668 ▶ my ($self, $token) = @_;
Subroutine push_keys: called CC=1 SCAR=0.0
Pod undocumented
117 1668 push @{ $self->{keys} }, $token->{value};
118 }
119  
120 sub pop_keys {
121 1178 ▶ my $self = shift;
Subroutine pop_keys: called CC=1 SCAR=0.0
Pod undocumented
122 1178 pop @{ $self->{keys} };
123 }
124  
125 sub get_keys {
126 2493 ▶ my $self = shift;
Subroutine get_keys: called CC=1 SCAR=0.0
Pod undocumented
127 2493 return map{ @$_ } @{ $self->{keys} };
128 }
129  
130 sub set_key {
131 909 ▶ my ($self, $token) = @_;
Subroutine set_key: called CC=3 SCAR=11.0
Pod undocumented
132 909 my @keys = $self->get_keys;
133 909 my $key = pop @keys;
134 909 my $node = $self->scan_to_key(\@keys);
135  
136 908 ▶ if ($key && exists $node->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key and exists $node->{$key}) 2 906 908
Condition100%3/3
$key and exists $node->{$key}
!ll&&!rl&&r
29042
MC/DC100%2/2
$key and exists $node->{$key}
$key exists $node->{$key}
Truth table100%3/3
A and B
A= $keyB= exists $node->{$key}
ABresult
0X0
100
111
137 2 $self->parse_error($token, 'duplicate key: ' . $self->current_key);
138 }
139  
140 906 $node->{$key} = $self->parse_value($token);
141 }
142  
143 sub declare_key {
144 860 ▶ my ($self, $token) = @_;
Subroutine declare_key: called CC=14 SCAR=26.5
Pod undocumented
145 860 ▶ my $key = $self->current_key || return;
Condition100%2/2
$self->current_key || (return)
l!l
438422
MC/DC100%1/1
$self->current_key || (return)
$self->current_key
Truth table100%2/2
A
A= $self->current_key
Aresult
11
00
146  
147 438 ▶ if ($token->{type} eq 'inline_array') {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->{"type"} eq "inline_array") 148 290 438
148 $self->parse_error($token, "duplicate key: $key")
149 148 ▶ if exists $self->{array_tables}{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"array_tables"}{$key} 0 148 148
150  
151 148 $self->{arrays}{$key} = 1;
152 148 return;
153 }
154  
155 290 ▶ if ($token->{type} eq 'array_table') {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->{"type"} eq "array_table") 66 224 290
156 66 ▶ if (exists $self->{arrays}{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"arrays"}{$key}) 1 65 66
157 1 $self->parse_error($token, "duplicate key: $key");
158 }
159  
160 65 $self->{array_tables}{$key} = 1;
161 65 return;
162 }
163  
164 224 ▶ if ($token->{type} eq 'table') {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->{"type"} eq "table") 224 0 224
165 $self->parse_error($token, "duplicate key: $key")
166 if exists $self->{arrays}{$key}
167 224 ▶ || exists $self->{array_tables}{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"arrays"}{$key} or exists $self->{"array_tables"}{$key} 2 222 224
Condition66%2/3
exists $self->{"arrays"}{$key} or exists $self->{"array_tables"}{$key}
l!l&&r!l&&!r
02222
MC/DC50%1/2
exists $self->{"arrays"}{$key} or exists $self->{"array_tables"}{$key}
exists $self->{"arrays"}{$key} exists $self->{"array_tables"}{$key}
Truth table66%2/3
A or B
A= exists $self->{"arrays"}{$key}B= exists $self->{"array_tables"}{$key}
ABresult
1X1
011
000
168  
169 222 ▶ if (exists $self->{tables}{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"tables"}{$key}) 9 213 222
170 # Tables cannot be redefined, *except* when doing so within a goddamn
171 # table array. Gawd I hate TOML.
172 9 my $in_a_stupid_table_array = 0;
173 9 my $node = $self->{root};
174  
175 9 for my $key ($self->get_keys) {
176 9 ▶ if (exists $node->{$key} && ref($node->{$key}) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $node->{$key} and ref $node->{$key} eq 'ARRAY') { } 8 1 9
Condition66%2/3
exists $node->{$key} and ref $node->{$key} eq "ARRAY"
!ll&&!rl&&r
018
MC/DC50%1/2
exists $node->{$key} and ref $node->{$key} eq "ARRAY"
exists $node->{$key} ref $node->{$key} eq "ARRAY"
Truth table66%2/3
A and B
A= exists $node->{$key}B= ref $node->{$key} eq "ARRAY"
ABresult
0X0
100
111
177 8 $in_a_stupid_table_array = 1;
178 8 last;
179 } else {
180 1 $node = $node->{$key};
181 }
182 }
183  
184 9 ▶ unless ($in_a_stupid_table_array) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($in_a_stupid_table_array) 1 8 9
185 1 $self->parse_error($token, "duplicate key: $key");
186 }
187 8 return;
188 }
189 213 $self->{tables}{$key} = 1;
190 }
191 }
192  
193 sub scan_to_key {
194 1622 ▶ my $self = shift;
Subroutine scan_to_key: called CC=6 SCAR=17.9
Pod undocumented
195 1622 ▶ my $keys = shift // [ $self->get_keys ];
Condition100%2/2
shift() // [$self->get_keys]
l!l
974648
MC/DC100%1/1
shift() // [$self->get_keys]
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
196 1622 my $node = $self->{root};
197  
198 KEY:
199 1622 for my $key (@$keys) {
200 950 ▶ if (exists $node->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $node->{$key}) { } 675 275 950
201 675 my $ref = ref $node->{$key};
202 675 ▶ if ( $ref eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq "HASH") 544 131 675
203 544 $node = $node->{$key};
204 544 next KEY;
205 }
206 131 ▶ if ( $ref eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq "ARRAY") 128 3 131
207 128 $node = $node->{$key}[-1];
208 128 next KEY;
209 }
210 3 my $full_key = join '.', @$keys;
211 3 die "$full_key is already defined\n";
212 }
213 else {
214 275 $node = $node->{$key} = {};
215 }
216 }
217  
218 1619 return $node;
219 }
220  
221 sub parse_table {
222 650 ▶ my $self = shift;
Subroutine parse_table: called CC=9 SCAR=22.0
Pod undocumented
223 650 ▶ my $token = shift // $self->next_token // return; # may be undef on first token in empty document
Condition100%5/5
(shift() // $self->next_token) // (return)
l!l
6482
shift() // $self->next_token
l!l&&r!l&&!r
2264222
MC/DC100%2/2
(shift() // $self->next_token) // (return)
shift() $self->next_token
Truth table100%3/3
A // B
A= shift()B= $self->next_token
ABresult
1X1
011
000
224  
225 648 $self->expect_type($token, 'table');
226 648 $self->push_keys($token);
227 648 $self->scan_to_key;
228  
229 646 $self->declare_key($token);
230  
231 643 ▶ TOKEN: while (my $token = $self->next_token) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $token = $self->next_token) 1436 92 1528
232 1436 my $type = $token->{type};
233 1436 ▶ next TOKEN if $type eq 'EOL';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "EOL" 283 1153 1436
234  
235 1153 ▶ if ( $type eq 'key') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "key") 889 264 1153
236 889 $self->expect_type($self->next_token, 'assign');
237 889 $self->push_keys($token);
238 889 $self->set_key($self->next_token);
239 821 $self->pop_keys;
240  
241 821 ▶ if (my $eol = $self->next_token) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $eol = $self->next_token) { } 628 136 764
242 628 $self->expect_type($eol, 'EOL');
243 } else {
244 136 return;
245 }
246 611 next TOKEN;
247 }
248  
249 264 ▶ if ($type eq 'array_table') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "array_table") 33 231 264
250 33 $self->pop_keys;
251 33 @_ = ($self, $token);
252 33 goto \&parse_array_table;
253 }
254  
255 231 ▶ if ( $type eq 'table') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "table") 211 20 231
256 211 $self->pop_keys;
257 211 @_ = ($self, $token);
258 211 goto \&parse_table;
259 }
260  
261 20 $self->parse_error($token, "expected key-value pair, table, or array of tables but got $type");
262 }
263 }
264  
265 sub parse_array_table {
266 66 ▶ my $self = shift;
Subroutine parse_array_table: called CC=8 SCAR=20.9
Pod undocumented
267 66 ▶ my $token = shift // $self->next_token;
Condition33%1/3
shift() // $self->next_token
l!l&&r!l&&!r
6600
MC/DC0%0/2
shift() // $self->next_token
shift() $self->next_token
Truth table33%1/3
A // B
A= shift()B= $self->next_token
ABresult
1X1
011
000
268 66 $self->expect_type($token, 'array_table');
269 66 $self->push_keys($token);
270  
271 66 $self->declare_key($token);
272  
273 65 my @keys = $self->get_keys;
274 65 my $key = pop @keys;
275 65 my $node = $self->scan_to_key(\@keys);
276 65 ▶ $node->{$key} //= [];
Condition100%2/2
$node->{$key} //= []
l!l
2936
MC/DC100%1/1
$node->{$key} //= []
$node->{$key}
Truth table100%2/2
A
A= $node->{$key}
Aresult
11
00
277 65 push @{ $node->{$key} }, {};
278  
279 TOKEN:
280 63 ▶ while (my $token = $self->next_token) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $token = $self->next_token) 222 15 237
281 222 my $type = $token->{type};
282 222 ▶ next TOKEN if $type eq 'EOL';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "EOL" 109 113 222
283  
284 113 ▶ if ($type eq 'key') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "key") 65 48 113
285 65 $self->expect_type($self->next_token, 'assign');
286 65 $self->push_keys($token);
287 65 $self->set_key($self->next_token);
288 65 $self->pop_keys;
289 65 next TOKEN;
290 }
291  
292 48 ▶ if ($type eq 'array_table') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "array_table") 33 15 48
293 33 $self->pop_keys;
294 33 @_ = ($self, $token);
295 33 goto \&parse_array_table;
296 }
297  
298 15 ▶ if ($type eq 'table') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "table") 15 0 15
299 15 $self->pop_keys;
300 15 @_ = ($self, $token);
301 15 goto \&parse_table;
302 }
303  
304 0 $self->parse_error($token, "expected key-value pair, table, or array of tables but got $type");
305 }
306 }
307  
308 sub parse_key {
309 0 ▶ my $self = shift;
Subroutine parse_key: not called CC=2 SCAR=17.9
Pod undocumented
310 0 ▶ my $token = shift // $self->next_token;
Condition0%0/3
shift() // $self->next_token
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() // $self->next_token
shift() $self->next_token
Truth table0%0/3
A // B
A= shift()B= $self->next_token
ABresult
1X1
011
000
311 0 $self->expect_type($token, 'key');
312 0 return $token->{value};
313 }
314  
315 sub parse_value {
316 1255 ▶ my $self = shift;
Subroutine parse_value: called CC=8 SCAR=20.8
Pod undocumented
317 1255 my $token = shift;
318  
319 1255 my $type = $token->{type};
320 1255 ▶ return $token->{value} if $type eq 'string';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "string" 429 826 1255
321 826 ▶ return $self->inflate_float($token) if $type eq'float';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "float" 99 727 826
322 727 ▶ return $self->inflate_integer($token) if $type eq 'integer';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "integer" 394 333 727
323 333 ▶ return $self->{inflate_boolean}->($token->{value}) if $type eq 'bool';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "bool" 33 300 333
324 300 ▶ return $self->parse_datetime($token) if $type eq 'datetime';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "datetime" 53 247 300
325 247 ▶ return $self->parse_inline_table($token) if $type eq 'inline_table';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "inline_table" 95 152 247
326 152 ▶ return $self->parse_array($token) if $type eq 'inline_array';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "inline_array" 148 4 152
327  
328 4 $self->parse_error($token, "value expected (bool, number, string, datetime, inline array, inline table), but found $type");
329 }
330  
331 #-------------------------------------------------------------------------------
332 # TOML permits a space instead of a T, which RFC3339 does not allow. TOML (at
333 # least, according to BurntSushi/toml-tests) allows z instead of Z, which
334 # RFC3339 also does not permit. We will be flexible and allow them both, but
335 # fix them up. TOML also specifies millisecond precision. If fractional seconds
336 # are specified. Whatever.
337 #-------------------------------------------------------------------------------
338 sub parse_datetime {
339 53 ▶ my $self = shift;
Subroutine parse_datetime: called CC=1 SCAR=0.0
Pod undocumented
340 53 my $token = shift;
341 53 my $value = $token->{value};
342  
343 # Normalize
344 53 $value =~ tr/z/Z/;
345 53 $value =~ tr/ /T/;
346 53 $value =~ s/t/T/;
347 53 $value =~ s/(\.\d+)($TimeOffset)$/sprintf(".%09d%s", $1 * 1000000000, $2)/e;
348  
349 53 return $self->{inflate_datetime}->($value);
350 }
351  
352 sub parse_array {
353 148 ▶ my $self = shift;
Subroutine parse_array: called CC=4 SCAR=13.9
Pod undocumented
354 148 my $token = shift;
355  
356 148 $self->declare_key($token);
357  
358 148 my @array;
359 148 my $expect = 'EOL|inline_array_close|string|float|integer|bool|datetime|inline_table|inline_array';
360  
361 148 TOKEN: while (1) {
362 872 my $token = $self->next_token;
363 869 $self->expect_type($token, $expect);
364  
365 866 ▶ if ( $token->{type} eq 'comma') {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->{"type"} eq "comma") 202 664 866
366 202 $expect = 'EOL|inline_array_close|string|float|integer|bool|datetime|inline_table|inline_array';
367 202 next TOKEN;
368 }
369  
370 664 ▶ next TOKEN if $token->{type} eq 'EOL';
Branch: 1 branch
BranchTrueFalseTotal
if $token->{"type"} eq "EOL" 248 416 664
371 416 ▶ last TOKEN if $token->{type} eq 'inline_array_close';
Branch: 1 branch
BranchTrueFalseTotal
if $token->{"type"} eq "inline_array_close" 140 276 416
372  
373 276 push @array, $self->parse_value($token);
374 274 $expect = 'comma|EOL|inline_array_close';
375 }
376  
377 140 return \@array;
378 }
379  
380 sub parse_inline_table {
381 95 ▶ my $self = shift;
Subroutine parse_inline_table: called CC=8 SCAR=20.9
Pod undocumented
382 95 my $token = shift;
383  
384 95 my $table = {};
385 95 my $expect = 'EOL|inline_table_close|key';
386  
387 95 TOKEN: while (1) {
388 187 my $token = $self->next_token;
389 187 $self->expect_type($token, $expect);
390  
391 179 my $type = $token->{type};
392 179 ▶ if ($type eq 'comma') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "comma") 19 160 179
393 $expect = $self->{strict}
394 19 ▶ ? 'EOL|key'
Branch: 1 branch
BranchTrueFalseTotal
$self->{'strict'} ? : 3 16 19
395 : 'EOL|key|inline_table_close';
396  
397 19 next TOKEN;
398 }
399  
400 160 ▶ if ($type eq 'key') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "key") 73 87 160
401 73 $self->expect_type($self->next_token, 'assign');
402  
403 73 my $node = $table;
404 73 my @keys = @{ $token->{value} };
405 73 my $key = pop @keys;
406  
407 73 for (@keys) {
408 16 ▶ $node->{$_} ||= {};
Condition100%2/2
$node->{$_} ||= {}
l!l
214
MC/DC100%1/1
$node->{$_} ||= {}
$node->{$_}
Truth table100%2/2
A
A= $node->{$_}
Aresult
11
00
409 16 $node = $node->{$_};
410 }
411  
412 73 ▶ if (exists $node->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $node->{$key}) { } 0 73 73
413 0 $self->parse_error($token, 'duplicate key: ' . join('.', map{ qq{"$_"} } @{ $token->{value} }));
414 } else {
415 73 $node->{ $key } = $self->parse_value($self->next_token);
416 }
417  
418 73 $expect = 'comma|inline_table_close';
419 73 next TOKEN;
420 }
421  
422 87 ▶ last TOKEN if $type eq 'inline_table_close';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "inline_table_close" 85 2 87
423  
424 2 $self->parse_error($token, "inline table expected key-value pair, but found $type");
425 }
426  
427 85 return $table;
428 }
429  
430 sub inflate_float {
431 99 ▶ my $self = shift;
Subroutine inflate_float: called CC=8 SCAR=20.8
Pod undocumented
432 99 my $token = shift;
433 99 my $value = $token->{value};
434  
435 # Caller-defined inflation routine
436 99 ▶ if ($self->{inflate_float}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inflate_float"}) 1 98 99
437 1 return $self->{inflate_float}->($value);
438 }
439  
440 98 ▶ return 'NaN' if $value =~ /^[-+]?nan$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /^[-+]?nan$/iu 13 85 98
441 85 ▶ return 'inf' if $value =~ /^\+?inf$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /^\+?inf$/iu 7 78 85
442 78 ▶ return '-inf' if $value =~ /^-inf$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /^-inf$/iu 4 74 78
443  
444 # Not a bignum
445 74 ▶ if (0 + $value eq $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 + $value eq $value) 39 35 74
446 39 return 0 + $value;
447 }
448  
449 #-----------------------------------------------------------------------------
450 # Scientific notation is a hairier situation. In order to determine whether a
451 # value will fit inside a perl svnv, we can't just coerce the value to a
452 # number and then test it against the string, because, for example, this will
453 # always be false:
454 #
455 # 9 eq "3e2"
456 #
457 # Instead, we are forced to test the coerced value against a BigFloat, which
458 # is capable of holding the number.
459 #-----------------------------------------------------------------------------
460 35 ▶ if ($value =~ /[eE]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /[eE]/u) 25 10 35
461 25 ▶ if (Math::BigFloat->new($value)->beq(0 + $value)) {
Branch: 1 branch
BranchTrueFalseTotal
if ("Math::BigFloat"->new($value)->beq(0 + $value)) 25 0 25
462 25 return 0 + $value;
463 }
464 }
465  
466 10 return Math::BigFloat->new($value);
467 }
468  
469 sub inflate_integer {
470 394 ▶ my $self = shift;
Subroutine inflate_integer: called CC=9 SCAR=22.0
Pod undocumented
471 394 my $token = shift;
472 394 my $value = $token->{value};
473  
474 # Caller-defined inflation routine
475 394 ▶ if ($self->{inflate_integer}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inflate_integer"}) 1 393 394
476 1 return $self->{inflate_integer}->($value);
477 }
478  
479 # Hex
480 393 ▶ if ($value =~ /^0x/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^0x/u) 16 377 393
481 287 ▶ no warnings 'portable';
Subroutine BEGIN: called CC=1 SCAR=0.0
482 16 my $hex = hex $value;
483 16 my $big = Math::BigInt->new($value);
484 16 ▶ return $big->beq($hex) ? $hex : $big;
Branch: 1 branch
BranchTrueFalseTotal
$big->beq($hex) ? : 16 0 16
485 }
486  
487 # Octal
488 377 ▶ if ($value =~ /^0o/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^0o/u) 12 365 377
489 287 ▶ no warnings 'portable';
Subroutine BEGIN: called CC=1 SCAR=0.0
490 12 $value =~ s/^0o/0/;
491 12 my $oct = oct $value;
492 12 my $big = Math::BigInt->from_oct($value);
493 12 ▶ return $big->beq($oct) ? $oct : $big;
Branch: 1 branch
BranchTrueFalseTotal
$big->beq($oct) ? : 12 0 12
494 }
495  
496 # Binary
497 365 ▶ if ($value =~ /^0b/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^0b/u) 11 354 365
498 287 ▶ no warnings 'portable';
Subroutine BEGIN: called CC=1 SCAR=0.0
499 11 my $bin = oct $value; # oct handles 0b as binary
500 11 my $big = Math::BigInt->new($value);
501 11 ▶ return $big->beq($bin) ? $bin : $big;
Branch: 1 branch
BranchTrueFalseTotal
$big->beq($bin) ? : 11 0 11
502 }
503  
504 # Not a bignum
505 354 ▶ if (0 + $value eq $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 + $value eq $value) 347 7 354
506 347 return 0 + $value;
507 }
508  
509 7 return Math::BigInt->new($value);
510 }
511  
512 287 1;
513  
514 __END__
515  
516 =pod
517  
518 =encoding UTF-8
519  
520 =head1 NAME
521  
522 TOML::Tiny::Parser - parser used by TOML::Tiny
523  
524 =head1 VERSION
525  
526 version 0.21
527  
528 =head1 AUTHOR
529  
530 Jeff Ober <sysread@fastmail.fm>
531  
532 =head1 COPYRIGHT AND LICENSE
533  
534 This software is copyright (c) 2026 by Jeff Ober.
535  
536 This is free software; you can redistribute it and/or modify it under
537 the same terms as the Perl 5 programming language system itself.
538  
539 =cut
« Tiny/Util.pm ↑ Summary Tiny.pm »