blib/lib/TOML/Parser.pm

filter: Statementstmt 87.6% 113 / 129 Branchbran 75.6% 59 / 78 Conditioncond 53.8% 7 / 13 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 90.9% 20 / 22 Podpod 100% 6 / 6 totaltotal 80.8% 207 / 256 SCAR 41.2
CC
47
Cov
81%
_parse_value_token
24.6
_parse_array_of_table
11.6
_parse_table
6.2
CRAP 61.3
↑ Summary blib/lib/TOML/Parser/Tokenizer.pm »
1 package TOML::Parser;
2 20 ▶use 5.010000;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 20 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 20 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 20 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 20 our $VERSION = "0.90_01";
8  
9 20 ▶use TOML::Parser::Tokenizer qw/:constant/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 20 ▶use TOML::Parser::Tokenizer::Strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 20 ▶use TOML::Parser::Util qw/unescape_str/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 20 ▶use Types::Serialiser;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 sub new {
15 56 ▶ my $class = shift;
Subroutine new: called CC=3 SCAR=12.0
Pod documented
16 56 ▶ my $args = (@_ == 1 and ref $_[0] eq 'HASH') ? +shift : +{ @_ };
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 && ref $_[0] eq 'HASH' ? : 0 56 56
Condition33%1/3
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
5600
MC/DC0%0/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
Truth table33%1/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
17 return bless +{
18 2 ▶ inflate_datetime => sub { $_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
19 4 ▶ inflate_boolean => sub { $_[0] eq 'true' ? Types::Serialiser::true : Types::Serialiser::false },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[0] eq 'true' ? : 4 0 4
20 56 strict_mode => 0,
21 %$args,
22 } => $class;
23 }
24  
25 sub parse_file {
26 0 ▶ my ($self, $file) = @_;
Subroutine parse_file: not called CC=2 SCAR=17.9
Pod documented
27 0 ▶ open my $fh, '<:encoding(utf-8)', $file or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:encoding(utf-8)", $file or die $! 0 0 0
28 0 return $self->parse_fh($fh);
29 }
30  
31 sub parse_fh {
32 0 ▶ my ($self, $fh) = @_;
Subroutine parse_fh: not called CC=1 SCAR=6.9
Pod documented
33 0 my $src = do { local $/; <$fh> };
34 0 return $self->parse($src);
35 }
36  
37 sub _tokenizer_class {
38 56 ▶ my $self = shift;
Subroutine _tokenizer_class: called CC=2 SCAR=6.9
39 56 ▶ return $self->{strict_mode} ? 'TOML::Parser::Tokenizer::Strict' : 'TOML::Parser::Tokenizer';
Branch: 1 branch
BranchTrueFalseTotal
$self->{'strict_mode'} ? : 15 41 56
40 }
41  
42 20 our @TOKENS;
43 20 our $ROOT;
44 20 our $CONTEXT;
45 sub parse {
46 56 ▶ my ($self, $src) = @_;
Subroutine parse: called CC=2 SCAR=6.9
Pod documented
47  
48 56 local $ROOT = {};
49 56 local $CONTEXT = $ROOT;
50 56 local @TOKENS = $self->_tokenizer_class->tokenize($src);
51 36 ▶ while (my $token = shift @TOKENS) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $token = shift @TOML::Parser::TOKENS) 332 33 365
52 332 $self->_parse_token($token);
53 }
54 33 return $ROOT;
55 }
56  
57 sub _parse_token {
58 332 ▶ my ($self, $token) = @_;
Subroutine _parse_token: called CC=6 SCAR=18.0
59  
60 332 my ($type, $val) = @$token;
61 332 ▶ if ($type eq TOKEN_TABLE) {
Branch: 4 branches
BranchTrueFalseTotal
if ($type eq 'TABLE') { } 40 292 332
elsif ($type eq 'ARRAY_OF_TABLE') { } 17 275 292
elsif (() = $self->_parse_key_and_value($token)) { } 213 62 275
elsif ($type eq 'COMMENT') { } 62 0 62
62 40 $self->_parse_table($val);
63 }
64 elsif ($type eq TOKEN_ARRAY_OF_TABLE) {
65 17 $self->_parse_array_of_table($val);
66 }
67 elsif (my ($key, $value) = $self->_parse_key_and_value($token)) {
68 213 ▶ die "Duplicate key. key:$key" if exists $CONTEXT->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $TOML::Parser::CONTEXT->{$key} 3 210 213
69 210 $CONTEXT->{$key} = $value;
70 }
71 elsif ($type eq TOKEN_COMMENT) {
72 # pass through
73 }
74 else {
75 0 die "Unknown case. type:$type";
76 }
77 }
78  
79 sub _parse_key_and_value {
80 319 ▶ my ($self, $token) = @_;
Subroutine _parse_key_and_value: called CC=2 SCAR=6.9
81  
82 319 my ($type, $val) = @$token;
83 319 ▶ if ($type eq TOKEN_KEY) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "KEY") 257 62 319
84 257 my $token = shift @TOKENS;
85  
86 257 my $key = $val;
87 257 my $value = $self->_parse_value_token($token);
88 257 return ($key, $value);
89 }
90  
91 62 return;
92 }
93  
94 sub _parse_table {
95 40 ▶ my ($self, $keys) = @_;
Subroutine _parse_table: called CC=6 SCAR=18.3
96 40 my @keys = @$keys;
97  
98 40 $CONTEXT = $ROOT;
99 40 for my $k (@keys) {
100 67 ▶ if (exists $CONTEXT->{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $TOML::Parser::CONTEXT->{$k}) { } 9 58 67
101 $CONTEXT = ref $CONTEXT->{$k} eq 'ARRAY' ? $CONTEXT->{$k}->[-1] :
102 9 ▶ ref $CONTEXT->{$k} eq 'HASH' ? $CONTEXT->{$k} :
Branch: 2 branches
BranchTrueFalseTotal
ref $TOML::Parser::CONTEXT->{$k} eq 'ARRAY' ? : 2 7 9
ref $TOML::Parser::CONTEXT->{$k} eq 'HASH' ? : 7 0 7
103 0 die "invalid structure. @{[ join '.', @keys ]} cannot be `Table`";
104 }
105 else {
106 58 ▶ $CONTEXT = $CONTEXT->{$k} ||= +{};
Condition50%1/2
$TOML::Parser::CONTEXT->{$k} ||= {}
l!l
058
MC/DC0%0/1
$TOML::Parser::CONTEXT->{$k} ||= {}
$TOML::Parser::CONTEXT->{$k}
Truth table50%1/2
A
A= $TOML::Parser::CONTEXT->{$k}
Aresult
11
00
107 }
108 }
109 }
110  
111 sub _parse_array_of_table {
112 17 ▶ my ($self, $keys) = @_;
Subroutine _parse_array_of_table: called CC=8 SCAR=24.5
113 17 my @keys = @$keys;
114 17 my $last_key = pop @keys;
115  
116 17 $CONTEXT = $ROOT;
117 17 for my $k (@keys) {
118 6 ▶ if (exists $CONTEXT->{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $TOML::Parser::CONTEXT->{$k}) { } 6 0 6
119 $CONTEXT = ref $CONTEXT->{$k} eq 'ARRAY' ? $CONTEXT->{$k}->[-1] :
120 6 ▶ ref $CONTEXT->{$k} eq 'HASH' ? $CONTEXT->{$k} :
Branch: 2 branches
BranchTrueFalseTotal
ref $TOML::Parser::CONTEXT->{$k} eq 'ARRAY' ? : 6 0 6
ref $TOML::Parser::CONTEXT->{$k} eq 'HASH' ? : 0 0 0
121 0 die "invalid structure. @{[ join '.', @keys ]} cannot be `Array of table`.";
122 }
123 else {
124 0 ▶ $CONTEXT = $CONTEXT->{$k} ||= +{};
Condition0%0/2
$TOML::Parser::CONTEXT->{$k} ||= {}
l!l
00
MC/DC0%0/1
$TOML::Parser::CONTEXT->{$k} ||= {}
$TOML::Parser::CONTEXT->{$k}
Truth table0%0/2
A
A= $TOML::Parser::CONTEXT->{$k}
Aresult
11
00
125 }
126 }
127  
128 17 ▶ $CONTEXT->{$last_key} = [] unless exists $CONTEXT->{$last_key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $TOML::Parser::CONTEXT->{$last_key} 10 7 17
129 17 ▶ die "invalid structure. @{[ join '.', @keys ]} cannot be `Array of table`" unless ref $CONTEXT->{$last_key} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $TOML::Parser::CONTEXT->{$last_key} eq "ARRAY" 0 17 17
130 17 push @{ $CONTEXT->{$last_key} } => $CONTEXT = {};
131 }
132  
133 sub _parse_value_token {
134 326 ▶ my $self = shift;
Subroutine _parse_value_token: called CC=23 SCAR=32.0
135 326 my $token = shift;
136  
137 326 my ($type, $val, @args) = @$token;
138 326 ▶ if ($type eq TOKEN_COMMENT) {
Branch: 8 branches
BranchTrueFalseTotal
if ($type eq 'COMMENT') { } 0 326 326
elsif ($type eq 'INTEGER' or $type eq 'FLOAT') { } 98 228 326
elsif ($type eq 'BOOLEAN') { } 4 224 228
elsif ($type eq 'DATETIME') { } 2 222 224
elsif ($type eq 'STRING') { } 157 65 222
elsif ($type eq 'MULTI_LINE_STRING_BEGIN') { } 20 45 65
elsif ($type eq 'INLINE_TABLE_BEGIN') { } 16 29 45
elsif ($type eq 'ARRAY_BEGIN') { } 29 0 29
139 0 return; # pass through
140 }
141 ▶ elsif ($type eq TOKEN_INTEGER || $type eq TOKEN_FLOAT) {
Condition100%3/3
$type eq "INTEGER" or $type eq "FLOAT"
l!l&&r!l&&!r
7820228
MC/DC100%2/2
$type eq "INTEGER" or $type eq "FLOAT"
$type eq "INTEGER" $type eq "FLOAT"
Truth table100%3/3
A or B
A= $type eq "INTEGER"B= $type eq "FLOAT"
ABresult
1X1
011
000
142 98 $val =~ tr/_//d;
143 98 return 0+$val;
144 }
145 elsif ($type eq TOKEN_BOOLEAN) {
146 4 return $self->inflate_boolean($val);
147 }
148 elsif ($type eq TOKEN_DATETIME) {
149 2 return $self->inflate_datetime($val);
150 }
151 elsif ($type eq TOKEN_STRING) {
152 157 my ($is_raw) = @args;
153 157 ▶ return $is_raw ? $val : unescape_str($val);
Branch: 1 branch
BranchTrueFalseTotal
$is_raw ? : 20 137 157
154 }
155 elsif ($type eq TOKEN_MULTI_LINE_STRING_BEGIN) {
156 20 my ($is_raw) = @args;
157 20 my $value = $self->_parse_value_token(shift @TOKENS);
158 20 $value =~ s/\A(?:\r\n|[\r\n])//msg;
159 20 $value =~ s/\\\s+//msg;
160 20 ▶ if (my $token = shift @TOKENS) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $token = shift @TOML::Parser::TOKENS) 20 0 20
161 20 my ($type) = @$token;
162 20 ▶ return $value if $type eq TOKEN_MULTI_LINE_STRING_END;
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "MULTI_LINE_STRING_END" 20 0 20
163 0 die "Unexpected token: $type";
164 }
165 }
166 elsif ($type eq TOKEN_INLINE_TABLE_BEGIN) {
167 16 my %data;
168 16 ▶ while (my $token = shift @TOKENS) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $token = shift @TOML::Parser::TOKENS) 62 0 62
169 62 ▶ last if $token->[0] eq TOKEN_INLINE_TABLE_END;
Branch: 1 branch
BranchTrueFalseTotal
if $token->[0] eq "INLINE_TABLE_END" 16 46 62
170 46 ▶ next if $token->[0] eq TOKEN_COMMENT;
Branch: 1 branch
BranchTrueFalseTotal
if $token->[0] eq "COMMENT" 2 44 46
171 44 my ($key, $value) = $self->_parse_key_and_value($token);
172 44 ▶ die "Duplicate key. key:$key" if exists $data{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $data{$key} 0 44 44
173 44 $data{$key} = $value;
174 }
175 16 return \%data;
176 }
177 elsif ($type eq TOKEN_ARRAY_BEGIN) {
178 29 my @data;
179  
180 my $last_token;
181 29 ▶ while (my $token = shift @TOKENS) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $token = shift @TOML::Parser::TOKENS) 85 0 85
182 85 ▶ last if $token->[0] eq TOKEN_ARRAY_END;
Branch: 1 branch
BranchTrueFalseTotal
if $token->[0] eq "ARRAY_END" 29 56 85
183 56 ▶ next if $token->[0] eq TOKEN_COMMENT;
Branch: 1 branch
BranchTrueFalseTotal
if $token->[0] eq "COMMENT" 7 49 56
184 49 ▶ if ($self->{strict_mode}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"strict_mode"}) 22 27 49
185 22 ▶ die "Unexpected token: $token->[0]" if defined $last_token && $token->[0] ne $last_token->[0];
Branch: 1 branch
BranchTrueFalseTotal
if defined $last_token and $token->[0] ne $last_token->[0] 0 22 22
Condition66%2/3
defined $last_token and $token->[0] ne $last_token->[0]
!ll&&!rl&&r
11110
MC/DC0%0/2
defined $last_token and $token->[0] ne $last_token->[0]
defined $last_token $token->[0] ne $last_token->[0]
Truth table66%2/3
A and B
A= defined $last_tokenB= $token->[0] ne $last_token->[0]
ABresult
0X0
100
111
186 }
187 49 push @data => $self->_parse_value_token($token);
188 49 $last_token = $token;
189 }
190 29 return \@data;
191 }
192  
193 0 die "Unexpected token: $type";
194 }
195  
196 sub inflate_datetime {
197 2 ▶ my $self = shift;
Subroutine inflate_datetime: called CC=1 SCAR=0.0
Pod documented
198 2 return $self->{inflate_datetime}->(@_);
199 }
200  
201 sub inflate_boolean {
202 4 ▶ my $self = shift;
Subroutine inflate_boolean: called CC=1 SCAR=0.0
Pod documented
203 4 return $self->{inflate_boolean}->(@_);
204 }
205  
206 20 1;
207 __END__
↑ Summary blib/lib/TOML/Parser/Tokenizer.pm »