| 1 | package Data::TOON::Decoder; | ||||||||||||||||||||||||||||
| 2 | 10 | ▶ | use 5.014; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 10 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 10 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 47 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=12.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 7 | 47 | my ($class, %opts) = @_; | |||||||||||||||||||||||||||
| 8 | return bless { | ||||||||||||||||||||||||||||
| 9 | strict => $opts{strict} // 1, | ||||||||||||||||||||||||||||
| 10 | lines => [], | ||||||||||||||||||||||||||||
| 11 | pos => 0, | ||||||||||||||||||||||||||||
| 12 | 47 | ▶ | max_depth => $opts{max_depth} // 100, # Prevent DoS from deep nesting | ||||||||||||||||||||||||||
|
Condition50%2/4
$opts{"strict"} // 1
$opts{"max_depth"} // 100
MC/DC0%0/1
$opts{"strict"} // 1
$opts{"strict"}
MC/DC0%0/1
$opts{"max_depth"} // 100
$opts{"max_depth"}
Truth table50%1/2
A
A= $opts{"strict"}
Truth table50%1/2
A
A= $opts{"max_depth"}
| |||||||||||||||||||||||||||||
| 13 | current_depth => 0, | ||||||||||||||||||||||||||||
| 14 | }, $class; | ||||||||||||||||||||||||||||
| 15 | } | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 47 | ▶ | sub decode { | ||||||||||||||||||||||||||
|
Subroutine
decode: called CC=3 SCAR=11.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 18 | 47 | my ($self, $toon_text) = @_; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | # Split into lines and remove trailing newlines | ||||||||||||||||||||||||||||
| 21 | 47 | my @lines = split /\r?\n/, $toon_text; | |||||||||||||||||||||||||||
| 22 | 47 | ▶ | pop @lines if @lines && $lines[-1] eq ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@lines and $lines[-1] eq ""
MC/DC0%0/2
@lines and $lines[-1] eq ""
@lines
$lines[-1] eq ""
Truth table33%1/3
A and B
A= @linesB= $lines[-1] eq ""
| |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 47 | $self->{lines} = \@lines; | |||||||||||||||||||||||||||
| 25 | 47 | $self->{pos} = 0; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | # Determine root form and decode | ||||||||||||||||||||||||||||
| 28 | 47 | return $self->_decode_root(); | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 47 | ▶ | sub _decode_root { | ||||||||||||||||||||||||||
|
Subroutine
_decode_root: called CC=20 SCAR=44.0
| |||||||||||||||||||||||||||||
| 32 | 47 | my ($self) = @_; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | # Initialize position | ||||||||||||||||||||||||||||
| 35 | 47 | $self->{pos} = 0; | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | # Find first non-empty line at depth 0 | ||||||||||||||||||||||||||||
| 38 | 47 | my @non_empty = (); | |||||||||||||||||||||||||||
| 39 | 47 | foreach my $line (@{$self->{lines}}) { | |||||||||||||||||||||||||||
| 40 | 234 | ▶ | next if !defined $line || $line =~ /^\s*$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $line or $line =~ /^\s*$/u
MC/DC0%0/2
not defined $line or $line =~ /^\s*$/u
not defined $line
$line =~ /^\s*$/u
Truth table33%1/3
not(A) or B
A= not defined $lineB= $line =~ /^\s*$/u
| |||||||||||||||||||||||||||||
| 41 | 234 | ▶ | next if $self->_get_depth($line) != 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 53 | push @non_empty, $line; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | # Empty document → empty object | ||||||||||||||||||||||||||||
| 46 | 47 | ▶ | return {} if !@non_empty; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 47 | my $first = $non_empty[0]; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | # Check if first line is a root array header: [N], [N|], [N\t] | ||||||||||||||||||||||||||||
| 51 | 47 | ▶ | if ($first =~ /^\s*\[(\d+)([\t|]?)?\]\s*:\s*(.*)$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | # Root array header | ||||||||||||||||||||||||||||
| 53 | 3 | my $count = $1; | |||||||||||||||||||||||||||
| 54 | 3 | ▶ | my $delimiter = $2 ? $2 : ','; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 3 | my $rest = $3; | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | # Inline values | ||||||||||||||||||||||||||||
| 58 | 3 | ▶ | if ($rest) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 3 | my @values = split /\Q$delimiter\E/, $rest; | |||||||||||||||||||||||||||
| 60 | 3 | return [map { $self->_parse_primitive($_) } @values]; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | # Or read list items below | ||||||||||||||||||||||||||||
| 64 | 0 | $self->{pos} = 0; | |||||||||||||||||||||||||||
| 65 | 0 | my @items; | |||||||||||||||||||||||||||
| 66 | 0 | ▶ | while ($self->{pos} < @{$self->{lines}}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 0 | my $line = $self->{lines}->[$self->{pos}]; | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 0 | ▶ | if (!$line || $line =~ /^\s*$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $line or $line =~ /^\s*$/u
MC/DC0%0/2
not $line or $line =~ /^\s*$/u
not $line
$line =~ /^\s*$/u
Truth table0%0/3
not(A) or B
A= not $lineB= $line =~ /^\s*$/u
| |||||||||||||||||||||||||||||
| 70 | 0 | $self->{pos}++; | |||||||||||||||||||||||||||
| 71 | 0 | next; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 0 | my $depth = $self->_get_depth($line); | |||||||||||||||||||||||||||
| 75 | 0 | ▶ | if ($depth == 0) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 76 | # Still at root - check if it's the header | ||||||||||||||||||||||||||||
| 77 | 0 | ▶ | if ($line =~ /^\[/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 0 | $self->{pos}++; | |||||||||||||||||||||||||||
| 79 | 0 | next; # Skip header | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | } elsif ($depth > 0) { | ||||||||||||||||||||||||||||
| 82 | 0 | my $trimmed = $line; | |||||||||||||||||||||||||||
| 83 | 0 | $trimmed =~ s/^\s+//; | |||||||||||||||||||||||||||
| 84 | 0 | ▶ | if ($trimmed =~ /^-/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | # List item | ||||||||||||||||||||||||||||
| 86 | 0 | $self->{pos}++; | |||||||||||||||||||||||||||
| 87 | 0 | $trimmed =~ s/^-\s+//; | |||||||||||||||||||||||||||
| 88 | 0 | push @items, $self->_parse_primitive($trimmed); | |||||||||||||||||||||||||||
| 89 | 0 | next; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | } else { | ||||||||||||||||||||||||||||
| 92 | 0 | last; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | 0 | $self->{pos}++; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | 0 | ▶ | return \@items if @items; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 0 | return []; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | # Check if first non-empty line is an array header with key (has [N]{...}: pattern) | ||||||||||||||||||||||||||||
| 101 | 44 | ▶ | if ($first =~ /^\w+\[/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | # Object with array field - use object mode | ||||||||||||||||||||||||||||
| 103 | 14 | return $self->_decode_object(0); | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | # Single line and it's a primitive (no colon = not key-value) | ||||||||||||||||||||||||||||
| 107 | 30 | ▶ | if (@non_empty == 1 && $first !~ /:/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@non_empty == 1 and not $first =~ /:/u
MC/DC100%2/2
@non_empty == 1 and not $first =~ /:/u
@non_empty == 1
not $first =~ /:/u
Truth table100%3/3
A and not(B)
A= @non_empty == 1B= not $first =~ /:/u
| |||||||||||||||||||||||||||||
| 108 | 10 | return $self->_parse_primitive($first); | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | # Otherwise, decode as object with depth 0 | ||||||||||||||||||||||||||||
| 112 | 20 | return $self->_decode_object(0); | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 140 | ▶ | sub _decode_object { | ||||||||||||||||||||||||||
|
Subroutine
_decode_object: called CC=13 SCAR=27.6
| |||||||||||||||||||||||||||||
| 116 | 140 | my ($self, $target_depth) = @_; | |||||||||||||||||||||||||||
| 117 | 140 | ▶ | $target_depth //= 0; | ||||||||||||||||||||||||||
|
Condition50%1/2
$target_depth //= 0
MC/DC0%0/1
$target_depth //= 0
$target_depth
Truth table50%1/2
A
A= $target_depth
| |||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | # Check max depth to prevent DoS | ||||||||||||||||||||||||||||
| 120 | 140 | ▶ | if ($target_depth > $self->{max_depth}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 1 | die "Maximum nesting depth exceeded (max: $self->{max_depth})\n"; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 139 | my $obj = {}; | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 139 | ▶ | while ($self->{pos} < @{$self->{lines}}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | 149 | my $line = $self->{lines}->[$self->{pos}]; | |||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | # Skip empty lines | ||||||||||||||||||||||||||||
| 130 | 149 | ▶ | if (!$line || $line =~ /^\s*$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $line or $line =~ /^\s*$/u
MC/DC0%0/2
not $line or $line =~ /^\s*$/u
not $line
$line =~ /^\s*$/u
Truth table33%1/3
not(A) or B
A= not $lineB= $line =~ /^\s*$/u
| |||||||||||||||||||||||||||||
| 131 | 0 | $self->{pos}++; | |||||||||||||||||||||||||||
| 132 | 0 | next; | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 149 | my $depth = $self->_get_depth($line); | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | # If depth is less than or equal to target, we're done with this object | ||||||||||||||||||||||||||||
| 138 | 149 | ▶ | if ($depth < $target_depth) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | 0 | last; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | # If depth is greater than target (but not target+1), skip (shouldn't happen in well-formed TOON) | ||||||||||||||||||||||||||||
| 143 | 149 | ▶ | if ($depth > $target_depth + 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | 0 | $self->{pos}++; | |||||||||||||||||||||||||||
| 145 | 0 | next; | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | # If depth is greater than target, it's a child of a nested key | ||||||||||||||||||||||||||||
| 149 | 149 | ▶ | if ($depth > $target_depth) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | 0 | last; | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 149 | $self->{pos}++; | |||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | # Parse key-value line | ||||||||||||||||||||||||||||
| 156 | 149 | my $trimmed = $line; | |||||||||||||||||||||||||||
| 157 | 149 | $trimmed =~ s/^\s+//; | |||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | # Match patterns like: | ||||||||||||||||||||||||||||
| 160 | # key: value | ||||||||||||||||||||||||||||
| 161 | # key[N]: ... | ||||||||||||||||||||||||||||
| 162 | # key[N]{fields}: ... | ||||||||||||||||||||||||||||
| 163 | # key: (empty - nested object) | ||||||||||||||||||||||||||||
| 164 | 149 | ▶ | if ($trimmed =~ /^(\w+)(\[[^\]]*\])?(\{[^}]*\})?\s*:\s*(.*)$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 165 | 147 | my ($key, $bracket, $fields, $rest) = ($1, $2, $3, $4); | |||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | # If there's a bracket segment, it's an array | ||||||||||||||||||||||||||||
| 168 | 147 | ▶ | if ($bracket) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 169 | 13 | $obj->{$key} = $self->_decode_array_value($bracket, $fields, $rest); | |||||||||||||||||||||||||||
| 170 | ▶ | } elsif (!$rest || $rest =~ /^\s*$/) { | |||||||||||||||||||||||||||
|
Condition66%2/3
not $rest or $rest =~ /^\s*$/u
MC/DC50%1/2
not $rest or $rest =~ /^\s*$/u
not $rest
$rest =~ /^\s*$/u
Truth table66%2/3
not(A) or B
A= not $restB= $rest =~ /^\s*$/u
| |||||||||||||||||||||||||||||
| 171 | # Empty value after colon = nested object | ||||||||||||||||||||||||||||
| 172 | 106 | $obj->{$key} = $self->_decode_object($target_depth + 1); | |||||||||||||||||||||||||||
| 173 | } else { | ||||||||||||||||||||||||||||
| 174 | # Primitive value | ||||||||||||||||||||||||||||
| 175 | 28 | $obj->{$key} = $self->_parse_primitive($rest); | |||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 38 | return $obj; | |||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 13 | ▶ | sub _decode_array_value { | ||||||||||||||||||||||||||
|
Subroutine
_decode_array_value: called CC=31 SCAR=37.9
| |||||||||||||||||||||||||||||
| 184 | 13 | my ($self, $bracket_part, $fields_part, $rest) = @_; | |||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | # Parse bracket part: [N] or [N\t] or [N|] | ||||||||||||||||||||||||||||
| 187 | 13 | my $delimiter = ','; # default | |||||||||||||||||||||||||||
| 188 | 13 | ▶ | if ($bracket_part =~ /^\[(\d+)([\t|])?\]/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 189 | 12 | my $count = $1; | |||||||||||||||||||||||||||
| 190 | 12 | ▶ | if (defined $2) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 191 | 7 | $delimiter = $2; | |||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 12 | ▶ | if ($fields_part) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 195 | # Tabular format: extract field names and parse rows | ||||||||||||||||||||||||||||
| 196 | 7 | my $fields_str = $fields_part; | |||||||||||||||||||||||||||
| 197 | 7 | $fields_str =~ s/^{|}$//g; | |||||||||||||||||||||||||||
| 198 | 7 | my @fields = split /\Q$delimiter\E/, $fields_str; | |||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 7 | my @rows; | |||||||||||||||||||||||||||
| 201 | 7 | ▶ | while ($self->{pos} < @{$self->{lines}}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 202 | 14 | my $line = $self->{lines}->[$self->{pos}]; | |||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 14 | ▶ | if (!$line || $line =~ /^\s*$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $line or $line =~ /^\s*$/u
MC/DC0%0/2
not $line or $line =~ /^\s*$/u
not $line
$line =~ /^\s*$/u
Truth table33%1/3
not(A) or B
A= not $lineB= $line =~ /^\s*$/u
| |||||||||||||||||||||||||||||
| 205 | 0 | $self->{pos}++; | |||||||||||||||||||||||||||
| 206 | 0 | next; | |||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | 14 | my $depth = $self->_get_depth($line); | |||||||||||||||||||||||||||
| 210 | 14 | ▶ | if ($depth <= 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 211 | 0 | last; | |||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | # Check for list items (- prefix) | ||||||||||||||||||||||||||||
| 215 | 14 | my $trimmed = $line; | |||||||||||||||||||||||||||
| 216 | 14 | $trimmed =~ s/^\s+//; | |||||||||||||||||||||||||||
| 217 | 14 | ▶ | if ($trimmed =~ /^-\s/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 218 | 0 | last; # List format starts here | |||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | 14 | $self->{pos}++; | |||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | 14 | my @values = split /\Q$delimiter\E/, $trimmed; | |||||||||||||||||||||||||||
| 224 | 14 | my $obj = {}; | |||||||||||||||||||||||||||
| 225 | 14 | ▶ | for (my $i = 0; $i < @fields && $i < @values; $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$i < @fields and $i < @values
MC/DC100%2/2
$i < @fields and $i < @values
$i < @fields
$i < @values
Truth table100%3/3
A and B
A= $i < @fieldsB= $i < @values
| |||||||||||||||||||||||||||||
| 226 | 28 | $obj->{$fields[$i]} = $self->_parse_primitive($values[$i]); | |||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||
| 228 | 14 | push @rows, $obj; | |||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||
| 230 | 7 | return \@rows; | |||||||||||||||||||||||||||
| 231 | } else { | ||||||||||||||||||||||||||||
| 232 | # Check for list format (items starting with -) | ||||||||||||||||||||||||||||
| 233 | # Peek ahead to see if next line (at depth+1) starts with "-" | ||||||||||||||||||||||||||||
| 234 | 5 | my $has_list_format = 0; | |||||||||||||||||||||||||||
| 235 | 5 | my $peek_pos = $self->{pos}; | |||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | 5 | ▶ | while ($peek_pos < @{$self->{lines}}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 238 | 3 | my $peek_line = $self->{lines}->[$peek_pos]; | |||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | 3 | ▶ | if (!$peek_line || $peek_line =~ /^\s*$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $peek_line or $peek_line =~ /^\s*$/u
MC/DC0%0/2
not $peek_line or $peek_line =~ /^\s*$/u
not $peek_line
$peek_line =~ /^\s*$/u
Truth table33%1/3
not(A) or B
A= not $peek_lineB= $peek_line =~ /^\s*$/u
| |||||||||||||||||||||||||||||
| 241 | 0 | $peek_pos++; | |||||||||||||||||||||||||||
| 242 | 0 | next; | |||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | 3 | my $peek_depth = $self->_get_depth($peek_line); | |||||||||||||||||||||||||||
| 246 | 3 | ▶ | if ($peek_depth <= 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 247 | 0 | last; | |||||||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | 3 | my $peek_trimmed = $peek_line; | |||||||||||||||||||||||||||
| 251 | 3 | $peek_trimmed =~ s/^\s+//; | |||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | 3 | ▶ | if ($peek_trimmed =~ /^-/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 254 | 3 | $has_list_format = 1; | |||||||||||||||||||||||||||
| 255 | 3 | last; | |||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | # If it's not empty and doesn't start with -, it's inline or not list format | ||||||||||||||||||||||||||||
| 259 | 0 | last; | |||||||||||||||||||||||||||
| 260 | } | ||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | 5 | ▶ | if ($has_list_format) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 263 | # Parse list format items (rest of the implementation) | ||||||||||||||||||||||||||||
| 264 | 3 | my @items; | |||||||||||||||||||||||||||
| 265 | 3 | ▶ | while ($self->{pos} < @{$self->{lines}}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 266 | 7 | my $line = $self->{lines}->[$self->{pos}]; | |||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | 7 | ▶ | if (!$line || $line =~ /^\s*$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $line or $line =~ /^\s*$/u
MC/DC0%0/2
not $line or $line =~ /^\s*$/u
not $line
$line =~ /^\s*$/u
Truth table33%1/3
not(A) or B
A= not $lineB= $line =~ /^\s*$/u
| |||||||||||||||||||||||||||||
| 269 | 0 | $self->{pos}++; | |||||||||||||||||||||||||||
| 270 | 0 | next; | |||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | 7 | my $depth = $self->_get_depth($line); | |||||||||||||||||||||||||||
| 274 | 7 | ▶ | if ($depth <= 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 275 | 0 | last; | |||||||||||||||||||||||||||
| 276 | } | ||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | 7 | my $trimmed = $line; | |||||||||||||||||||||||||||
| 279 | 7 | $trimmed =~ s/^\s+//; | |||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | 7 | ▶ | if ($trimmed =~ /^-\s(.*)$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 282 | 7 | $self->{pos}++; | |||||||||||||||||||||||||||
| 283 | 7 | my $item_content = $1; | |||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | # Parse first field or value | ||||||||||||||||||||||||||||
| 286 | 7 | ▶ | if ($item_content =~ /^(\w+):\s*(.*)$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 287 | # Object item: first field on hyphen line | ||||||||||||||||||||||||||||
| 288 | 4 | my ($first_key, $first_value) = ($1, $2); | |||||||||||||||||||||||||||
| 289 | 4 | my $item = {}; | |||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | 4 | ▶ | if ($first_value =~ /^\s*$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 292 | # Nested object - parse remaining fields at depth+2 | ||||||||||||||||||||||||||||
| 293 | 0 | $item->{$first_key} = $self->_decode_object($depth + 2); | |||||||||||||||||||||||||||
| 294 | } else { | ||||||||||||||||||||||||||||
| 295 | # Primitive value | ||||||||||||||||||||||||||||
| 296 | 4 | $item->{$first_key} = $self->_parse_primitive($first_value); | |||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | # Parse remaining fields at depth+1 | ||||||||||||||||||||||||||||
| 299 | 4 | ▶ | while ($self->{pos} < @{$self->{lines}}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 300 | 4 | my $next_line = $self->{lines}->[$self->{pos}]; | |||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | 4 | ▶ | if (!$next_line || $next_line =~ /^\s*$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $next_line or $next_line =~ /^\s*$/u
MC/DC0%0/2
not $next_line or $next_line =~ /^\s*$/u
not $next_line
$next_line =~ /^\s*$/u
Truth table33%1/3
not(A) or B
A= not $next_lineB= $next_line =~ /^\s*$/u
| |||||||||||||||||||||||||||||
| 303 | 0 | $self->{pos}++; | |||||||||||||||||||||||||||
| 304 | 0 | next; | |||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | 4 | my $next_depth = $self->_get_depth($next_line); | |||||||||||||||||||||||||||
| 308 | 4 | ▶ | if ($next_depth < $depth + 1 || $next_depth > $depth + 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$next_depth < $depth + 1 or $next_depth > $depth + 1
MC/DC50%1/2
$next_depth < $depth + 1 or $next_depth > $depth + 1
$next_depth < $depth + 1
$next_depth > $depth + 1
Truth table66%2/3
A or B
A= $next_depth < $depth + 1B= $next_depth > $depth + 1
| |||||||||||||||||||||||||||||
| 309 | 2 | last; | |||||||||||||||||||||||||||
| 310 | } | ||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||
| 312 | 2 | my $next_trimmed = $next_line; | |||||||||||||||||||||||||||
| 313 | 2 | $next_trimmed =~ s/^\s+//; | |||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | 2 | ▶ | if ($next_trimmed =~ /^-/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 316 | # Next list item | ||||||||||||||||||||||||||||
| 317 | 0 | last; | |||||||||||||||||||||||||||
| 318 | } | ||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||
| 320 | 2 | ▶ | if ($next_trimmed =~ /^(\w+):\s*(.*)$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 321 | 2 | $self->{pos}++; | |||||||||||||||||||||||||||
| 322 | 2 | $item->{$1} = $self->_parse_primitive($2); | |||||||||||||||||||||||||||
| 323 | } else { | ||||||||||||||||||||||||||||
| 324 | 0 | last; | |||||||||||||||||||||||||||
| 325 | } | ||||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||
| 327 | } | ||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||
| 329 | 4 | push @items, $item; | |||||||||||||||||||||||||||
| 330 | } else { | ||||||||||||||||||||||||||||
| 331 | # Primitive item | ||||||||||||||||||||||||||||
| 332 | 3 | push @items, $self->_parse_primitive($item_content); | |||||||||||||||||||||||||||
| 333 | } | ||||||||||||||||||||||||||||
| 334 | } else { | ||||||||||||||||||||||||||||
| 335 | 0 | last; | |||||||||||||||||||||||||||
| 336 | } | ||||||||||||||||||||||||||||
| 337 | } | ||||||||||||||||||||||||||||
| 338 | 3 | return \@items; | |||||||||||||||||||||||||||
| 339 | } else { | ||||||||||||||||||||||||||||
| 340 | # Inline primitive array format: parse inline values with correct delimiter | ||||||||||||||||||||||||||||
| 341 | 2 | my @values = split /\Q$delimiter\E/, $rest; | |||||||||||||||||||||||||||
| 342 | 2 | my @result = map { $self->_parse_primitive($_) } @values; | |||||||||||||||||||||||||||
| 343 | 2 | return \@result; | |||||||||||||||||||||||||||
| 344 | } | ||||||||||||||||||||||||||||
| 345 | } | ||||||||||||||||||||||||||||
| 346 | } | ||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||
| 348 | 1 | return []; | |||||||||||||||||||||||||||
| 349 | } | ||||||||||||||||||||||||||||
| 350 | 90 | ▶ | sub _parse_primitive { | ||||||||||||||||||||||||||
|
Subroutine
_parse_primitive: called CC=11 SCAR=24.0
| |||||||||||||||||||||||||||||
| 351 | 90 | my ($self, $value) = @_; | |||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||
| 353 | 90 | ▶ | return undef unless defined $value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||
| 355 | 90 | $value =~ s/^\s+|\s+$//g; | |||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||
| 357 | # Handle quoted strings | ||||||||||||||||||||||||||||
| 358 | 90 | ▶ | if ($value =~ /^"(.*)"$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 359 | 13 | my $str = $1; | |||||||||||||||||||||||||||
| 360 | # Unescape | ||||||||||||||||||||||||||||
| 361 | 13 | $str =~ s/\\"/"/g; | |||||||||||||||||||||||||||
| 362 | 13 | $str =~ s/\\\\/\\/g; | |||||||||||||||||||||||||||
| 363 | 13 | $str =~ s/\\n/\n/g; | |||||||||||||||||||||||||||
| 364 | 13 | $str =~ s/\\r/\r/g; | |||||||||||||||||||||||||||
| 365 | 13 | $str =~ s/\\t/\t/g; | |||||||||||||||||||||||||||
| 366 | 13 | return $str; | |||||||||||||||||||||||||||
| 367 | } | ||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||
| 369 | 77 | ▶ | return undef if $value eq 'null'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 370 | 76 | ▶ | return 1 if $value eq 'true'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 371 | 74 | ▶ | return 0 if $value eq 'false'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||
| 373 | # Number parsing with canonical form | ||||||||||||||||||||||||||||
| 374 | 73 | ▶ | if ($value =~ /^-?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 375 | # Reject leading zeros (except 0 itself and 0.x) | ||||||||||||||||||||||||||||
| 376 | 30 | ▶ | if ($value =~ /^[+-]?0\d/ && $value !~ /^[+-]?0\./) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$value =~ /^[+-]?0\d/u and not $value =~ /^[+-]?0\./u
MC/DC50%1/2
$value =~ /^[+-]?0\d/u and not $value =~ /^[+-]?0\./u
$value =~ /^[+-]?0\d/u
not $value =~ /^[+-]?0\./u
Truth table66%2/3
A and not(B)
A= $value =~ /^[+-]?0\d/uB= not $value =~ /^[+-]?0\./u
| |||||||||||||||||||||||||||||
| 377 | # Leading zero - treat as string | ||||||||||||||||||||||||||||
| 378 | 1 | return $value; | |||||||||||||||||||||||||||
| 379 | } | ||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||
| 381 | # Parse and normalize | ||||||||||||||||||||||||||||
| 382 | 29 | my $num = 0 + $value; | |||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||
| 384 | # Normalize: -0 → 0, remove trailing zeros | ||||||||||||||||||||||||||||
| 385 | 29 | ▶ | if ($num == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 386 | 2 | return 0; | |||||||||||||||||||||||||||
| 387 | } | ||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||
| 389 | # Return normalized number | ||||||||||||||||||||||||||||
| 390 | 27 | ▶ | if ($num != int($num)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 391 | # Float - remove trailing zeros | ||||||||||||||||||||||||||||
| 392 | 2 | my $str = sprintf("%.15g", $num); | |||||||||||||||||||||||||||
| 393 | 2 | return 0 + $str; | |||||||||||||||||||||||||||
| 394 | } | ||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||
| 396 | 25 | return $num; | |||||||||||||||||||||||||||
| 397 | } | ||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||
| 399 | 43 | return $value; | |||||||||||||||||||||||||||
| 400 | } | ||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||
| 402 | 411 | ▶ | sub _get_depth { | ||||||||||||||||||||||||||
|
Subroutine
_get_depth: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 403 | 411 | my ($self, $line) = @_; | |||||||||||||||||||||||||||
| 404 | 411 | ▶ | return 0 unless $line; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 405 | 410 | my $spaces = length($line) - length($line =~ s/^ +//r); | |||||||||||||||||||||||||||
| 406 | 410 | return int($spaces / 2); | |||||||||||||||||||||||||||
| 407 | } | ||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||
| 409 | 10 | 1; | |||||||||||||||||||||||||||