Spiderpup/SFC.pm

filter: Statementstmt 89.5% 282 / 315 Branchbran 72.2% 130 / 180 Conditioncond 55.8% 72 / 129 MC/DCmcdc 34.6% 26 / 75 Subroutinesub 100% 17 / 17 Podpod 0.0% 0 / 1 totaltotal 73.5% 527 / 717 CC 141 SCAR 31.1
CC
141
Cov
78%
_read_balanced
51.1
parse_sfc
44.5
_read_data_value
19.1
CRAP 22.4
« Spiderpup.pm ↑ Summary Spiderpup/Transform.pm »
1 package Yote::Spiderpup::SFC;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 our @EXPORT_OK = qw(parse_sfc);
8  
9 # Keys whose values are function expressions (kept as raw strings)
10 2 my %FUNCTION_KEYS = map { $_ => 1 } qw(methods computed lifecycle watch);
11  
12 # Keys whose values are parsed as Perl data structures
13 2 my %DATA_KEYS = map { $_ => 1 } qw(vars import routes initial_store);
14  
15 # Keys whose values are arrays of strings
16 2 my %ARRAY_KEYS = map { $_ => 1 } ('import-css', 'import-js', 'include-js');
17  
18 #----------------------------------------------------------------------
19 # Public API
20 #----------------------------------------------------------------------
21  
22 28 ▶sub parse_sfc {
Subroutine parse_sfc: called CC=25 SCAR=37.9
Pod undocumented
23 28 my ($content) = @_;
24 28 my $result = {};
25  
26 # Strip HTML comments before parsing to avoid false matches
27 28 $content =~ s/<!--.*?-->//gs;
28  
29 # Extract <style> blocks
30 28 ▶ while ($content =~ /<style([^>]*)>(.*?)<\/style>/gs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ m[<style([^>]*)>(.*?)</style>]gs) 3 28 31
31 3 my ($attrs, $body) = ($1, $2);
32 3 $body =~ s/^\n//;
33 3 $body =~ s/\n$//;
34 3 my $is_less = ($attrs =~ /lang\s*=\s*["']less["']/);
35 3 ▶ my $style_key = $is_less ? 'less' : 'css';
Branch: 1 branch
BranchTrueFalseTotal
$is_less ? : 1 2 3
36 3 ▶ if ($attrs =~ /recipe\s*=\s*["']([^"']+)["']/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attrs =~ /recipe\s*=\s*["']([^"']+)["']/) { } 0 3 3
37 0 my $recipe_name = $1;
38 0 ▶ $result->{recipes} //= {};
Condition0%0/2
$result->{"recipes"} //= {}
l!l
00
MC/DC0%0/1
$result->{"recipes"} //= {}
$result->{"recipes"}
Truth table0%0/2
A
A= $result->{"recipes"}
Aresult
11
00
39 0 ▶ $result->{recipes}{$recipe_name} //= {};
Condition0%0/2
$result->{"recipes"}{$recipe_name} //= {}
l!l
00
MC/DC0%0/1
$result->{"recipes"}{$recipe_name} //= {}
$result->{"recipes"}{$recipe_name}
Truth table0%0/2
A
A= $result->{"recipes"}{$recipe_name}
Aresult
11
00
40 $result->{recipes}{$recipe_name}{$style_key} =
41 0 ▶ ($result->{recipes}{$recipe_name}{$style_key} // '') . $body;
Condition0%0/2
$result->{"recipes"}{$recipe_name}{$style_key} // ""
l!l
00
MC/DC0%0/1
$result->{"recipes"}{$recipe_name}{$style_key} // ""
$result->{"recipes"}{$recipe_name}{$style_key}
Truth table0%0/2
A
A= $result->{"recipes"}{$recipe_name}{$style_key}
Aresult
11
00
42 } else {
43 3 ▶ $result->{$style_key} = ($result->{$style_key} // '') . $body;
Condition50%1/2
$result->{$style_key} // ""
l!l
03
MC/DC0%0/1
$result->{$style_key} // ""
$result->{$style_key}
Truth table50%1/2
A
A= $result->{$style_key}
Aresult
11
00
44 }
45 }
46  
47 # Extract <template> blocks
48 28 ▶ while ($content =~ /<template([^>]*)>(.*?)<\/template>/gs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ m[<template([^>]*)>(.*?)</template>]gs) 6 28 34
49 6 my ($attrs, $body) = ($1, $2);
50 6 $body =~ s/^\n//;
51 6 $body =~ s/\n$//;
52 6 ▶ if ($attrs =~ /recipe\s*=\s*["']([^"']+)["']/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($attrs =~ /recipe\s*=\s*["']([^"']+)["']/) { } 2 4 6
elsif ($attrs =~ /name\s*=\s*["']([^"']+)["']/) { } 1 3 4
53 2 my $recipe_name = $1;
54 2 ▶ $result->{recipes} //= {};
Condition50%1/2
$result->{"recipes"} //= {}
l!l
02
MC/DC0%0/1
$result->{"recipes"} //= {}
$result->{"recipes"}
Truth table50%1/2
A
A= $result->{"recipes"}
Aresult
11
00
55 2 ▶ $result->{recipes}{$recipe_name} //= {};
Condition50%1/2
$result->{"recipes"}{$recipe_name} //= {}
l!l
02
MC/DC0%0/1
$result->{"recipes"}{$recipe_name} //= {}
$result->{"recipes"}{$recipe_name}
Truth table50%1/2
A
A= $result->{"recipes"}{$recipe_name}
Aresult
11
00
56 2 $result->{recipes}{$recipe_name}{html} = $body;
57 } elsif ($attrs =~ /name\s*=\s*["']([^"']+)["']/) {
58 1 my $variant = $1;
59 1 $result->{"html_$variant"} = $body;
60 } else {
61 3 $result->{html} = $body;
62 }
63 }
64  
65 # Extract <script> blocks
66 28 ▶ while ($content =~ /<script([^>]*)>(.*?)<\/script>/gs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ m[<script([^>]*)>(.*?)</script>]gs) 23 28 51
67 23 my ($attrs, $body) = ($1, $2);
68 23 ▶ if ($attrs =~ /recipe\s*=\s*["']([^"']+)["']/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attrs =~ /recipe\s*=\s*["']([^"']+)["']/) { } 1 22 23
69 1 my $recipe_name = $1;
70 1 ▶ $result->{recipes} //= {};
Condition50%1/2
$result->{"recipes"} //= {}
l!l
10
MC/DC0%0/1
$result->{"recipes"} //= {}
$result->{"recipes"}
Truth table50%1/2
A
A= $result->{"recipes"}
Aresult
11
00
71 1 ▶ $result->{recipes}{$recipe_name} //= {};
Condition50%1/2
$result->{"recipes"}{$recipe_name} //= {}
l!l
10
MC/DC0%0/1
$result->{"recipes"}{$recipe_name} //= {}
$result->{"recipes"}{$recipe_name}
Truth table50%1/2
A
A= $result->{"recipes"}{$recipe_name}
Aresult
11
00
72 1 my $parsed = _parse_top_object($body);
73 1 for my $key (keys %$parsed) {
74 2 $result->{recipes}{$recipe_name}{$key} = $parsed->{$key};
75 }
76 } else {
77 22 my $parsed = _parse_top_object($body);
78 22 for my $key (keys %$parsed) {
79 27 ▶ if ($key eq 'recipes' && ref($result->{$key}) eq 'HASH' && ref($parsed->{$key}) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq 'recipes' and ref $result->{$key} eq 'HASH' and ref $parsed->{$key} eq 'HASH') { } 0 27 27
Condition50%3/6
$key eq "recipes" and ref $result->{$key} eq "HASH" and ref $parsed->{$key} eq "HASH"
!ll&&!rl&&r
2700
$key eq "recipes" and ref $result->{$key} eq "HASH"
!ll&&!rl&&r
2610
MC/DC0%0/3
$key eq "recipes" and ref $result->{$key} eq "HASH" and ref $parsed->{$key} eq "HASH"
$key eq "recipes" ref $result->{$key} eq "HASH" ref $parsed->{$key} eq "HASH"
Truth table50%2/4
A and B and C
A= $key eq "recipes"B= ref $result->{$key} eq "HASH"C= ref $parsed->{$key} eq "HASH"
ABCresult
0XX0
10X0
1100
1111
80 # Deep merge: script recipes into existing recipe data (from template blocks)
81 0 for my $rname (keys %{$parsed->{$key}}) {
82 0 ▶ $result->{$key}{$rname} //= {};
Condition0%0/2
$result->{$key}{$rname} //= {}
l!l
00
MC/DC0%0/1
$result->{$key}{$rname} //= {}
$result->{$key}{$rname}
Truth table0%0/2
A
A= $result->{$key}{$rname}
Aresult
11
00
83 0 for my $rkey (keys %{$parsed->{$key}{$rname}}) {
84 0 $result->{$key}{$rname}{$rkey} = $parsed->{$key}{$rname}{$rkey};
85 }
86 }
87 } else {
88 27 $result->{$key} = $parsed->{$key};
89 }
90 }
91 }
92 }
93  
94 28 return $result;
95 }
96  
97 #----------------------------------------------------------------------
98 # Whitespace and comment skipping
99 #----------------------------------------------------------------------
100  
101 393 ▶sub _skip_ws {
Subroutine _skip_ws: called CC=10 SCAR=23.1
102 393 my ($text, $pos) = @_;
103 393 my $len = length($text);
104 393 ▶ while ($$pos < $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$pos < $len) 773 1 774
105 # Skip whitespace
106 773 ▶ if (substr($text, $$pos, 1) =~ /\s/) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($text, $$pos, 1) =~ /\s/) 379 394 773
107 379 $$pos++;
108 379 next;
109 }
110 # Skip // line comments
111 394 ▶ if (substr($text, $$pos, 2) eq '//') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($text, $$pos, 2) eq "//") 1 393 394
112 1 $$pos += 2;
113 1 ▶ while ($$pos < $len && substr($text, $$pos, 1) ne "\n") {
Branch: 1 branch
BranchTrueFalseTotal
if ($$pos < $len and substr($text, $$pos, 1) ne "\n") 16 1 17
Condition66%2/3
$$pos < $len and substr($text, $$pos, 1) ne "\n"
!ll&&!rl&&r
0116
MC/DC50%1/2
$$pos < $len and substr($text, $$pos, 1) ne "\n"
$$pos < $len substr($text, $$pos, 1) ne "\n"
Truth table66%2/3
A and B
A= $$pos < $lenB= substr($text, $$pos, 1) ne "\n"
ABresult
0X0
100
111
114 16 $$pos++;
115 }
116 1 next;
117 }
118 # Skip /* block comments */
119 393 ▶ if (substr($text, $$pos, 2) eq '/*') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($text, $$pos, 2) eq "/*") 1 392 393
120 1 $$pos += 2;
121 1 ▶ while ($$pos < $len - 1 && substr($text, $$pos, 2) ne '*/') {
Branch: 1 branch
BranchTrueFalseTotal
if ($$pos < $len - 1 and substr($text, $$pos, 2) ne "*/") 14 1 15
Condition66%2/3
$$pos < $len - 1 and substr($text, $$pos, 2) ne "*/"
!ll&&!rl&&r
0114
MC/DC50%1/2
$$pos < $len - 1 and substr($text, $$pos, 2) ne "*/"
$$pos < $len - 1 substr($text, $$pos, 2) ne "*/"
Truth table66%2/3
A and B
A= $$pos < $len - 1B= substr($text, $$pos, 2) ne "*/"
ABresult
0X0
100
111
122 14 $$pos++;
123 }
124 1 ▶ $$pos += 2 if $$pos < $len - 1;
Branch: 1 branch
BranchTrueFalseTotal
if $$pos < $len - 1 1 0 1
125 1 next;
126 }
127 392 last;
128 }
129 }
130  
131 #----------------------------------------------------------------------
132 # String reading
133 #----------------------------------------------------------------------
134  
135 20 ▶sub _read_string {
Subroutine _read_string: called CC=5 SCAR=18.7
136 20 my ($text, $pos) = @_;
137 20 my $quote = substr($text, $$pos, 1);
138 20 $$pos++; # skip opening quote
139 20 my $result = '';
140 20 my $len = length($text);
141 20 ▶ while ($$pos < $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$pos < $len) 181 0 181
142 181 my $ch = substr($text, $$pos, 1);
143 181 ▶ if ($ch eq '\\') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\\") 0 181 181
144 # Escape sequence
145 0 $result .= $ch;
146 0 $$pos++;
147 0 ▶ if ($$pos < $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$pos < $len) 0 0 0
148 0 $result .= substr($text, $$pos, 1);
149 0 $$pos++;
150 }
151 0 next;
152 }
153 181 ▶ if ($ch eq $quote) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq $quote) 20 161 181
154 20 $$pos++; # skip closing quote
155 20 return $result;
156 }
157 161 $result .= $ch;
158 161 $$pos++;
159 }
160 0 return $result;
161 }
162  
163 #----------------------------------------------------------------------
164 # Key reading (bare identifier or quoted string)
165 #----------------------------------------------------------------------
166  
167 67 ▶sub _read_key {
Subroutine _read_key: called CC=5 SCAR=16.2
168 67 my ($text, $pos) = @_;
169 67 my $ch = substr($text, $$pos, 1);
170 67 ▶ if ($ch eq '"' || $ch eq "'") {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\"" or $ch eq "'") 3 64 67
Condition66%2/3
$ch eq "\"" or $ch eq "'"
l!l&&r!l&&!r
0364
MC/DC50%1/2
$ch eq "\"" or $ch eq "'"
$ch eq "\"" $ch eq "'"
Truth table66%2/3
A or B
A= $ch eq "\""B= $ch eq "'"
ABresult
1X1
011
000
171 3 return _read_string($text, $pos);
172 }
173 # Bare identifier: letters, digits, underscore, dash
174 64 my $start = $$pos;
175 64 my $len = length($text);
176 64 ▶ while ($$pos < $len && substr($text, $$pos, 1) =~ /[\w\-]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$pos < $len and substr($text, $$pos, 1) =~ /[\w\-]/) 355 64 419
Condition66%2/3
$$pos < $len and substr($text, $$pos, 1) =~ /[\w\-]/
!ll&&!rl&&r
064355
MC/DC50%1/2
$$pos < $len and substr($text, $$pos, 1) =~ /[\w\-]/
$$pos < $len substr($text, $$pos, 1) =~ /[\w\-]/
Truth table66%2/3
A and B
A= $$pos < $lenB= substr($text, $$pos, 1) =~ /[\w\-]/
ABresult
0X0
100
111
177 355 $$pos++;
178 }
179 64 return substr($text, $start, $$pos - $start);
180 }
181  
182 #----------------------------------------------------------------------
183 # Read balanced expression (for function context values)
184 # Scans until we hit , or } at depth 0, respecting strings and brackets
185 #----------------------------------------------------------------------
186  
187 16 ▶sub _read_balanced {
Subroutine _read_balanced: called CC=25 SCAR=39.3
188 16 my ($text, $pos) = @_;
189 16 my $start = $$pos;
190 16 my $depth = 0;
191 16 my $len = length($text);
192  
193 16 ▶ while ($$pos < $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$pos < $len) 598 0 598
194 598 my $ch = substr($text, $$pos, 1);
195  
196 # Handle strings
197 598 ▶ if ($ch eq '"' || $ch eq "'" || $ch eq '`') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\"" or $ch eq "'" or $ch eq "`") 10 588 598
Condition100%6/6
$ch eq "\"" or $ch eq "'" or $ch eq "`"
l!l&&r!l&&!r
82588
$ch eq "\"" or $ch eq "'"
l!l&&r!l&&!r
35590
MC/DC100%3/3
$ch eq "\"" or $ch eq "'" or $ch eq "`"
$ch eq "\"" $ch eq "'" $ch eq "`"
Truth table100%4/4
A or B or C
A= $ch eq "\""B= $ch eq "'"C= $ch eq "`"
ABCresult
1XX1
01X1
0011
0000
198 10 _skip_string($text, $pos);
199 10 next;
200 }
201  
202 # Handle line comments
203 588 ▶ if ($ch eq '/' && $$pos + 1 < $len && substr($text, $$pos + 1, 1) eq '/') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "/" and $$pos + 1 < $len and substr($text, $$pos + 1, 1) eq "/") 0 588 588
Condition33%2/6
$ch eq "/" and $$pos + 1 < $len and substr($text, $$pos + 1, 1) eq "/"
!ll&&!rl&&r
58800
$ch eq "/" and $$pos + 1 < $len
!ll&&!rl&&r
58800
MC/DC0%0/3
$ch eq "/" and $$pos + 1 < $len and substr($text, $$pos + 1, 1) eq "/"
$ch eq "/" $$pos + 1 < $len substr($text, $$pos + 1, 1) eq "/"
Truth table25%1/4
A and B and C
A= $ch eq "/"B= $$pos + 1 < $lenC= substr($text, $$pos + 1, 1) eq "/"
ABCresult
0XX0
10X0
1100
1111
204 0 $$pos += 2;
205 0 ▶ while ($$pos < $len && substr($text, $$pos, 1) ne "\n") {
Branch: 1 branch
BranchTrueFalseTotal
if ($$pos < $len and substr($text, $$pos, 1) ne "\n") 0 0 0
Condition0%0/3
$$pos < $len and substr($text, $$pos, 1) ne "\n"
!ll&&!rl&&r
000
MC/DC0%0/2
$$pos < $len and substr($text, $$pos, 1) ne "\n"
$$pos < $len substr($text, $$pos, 1) ne "\n"
Truth table0%0/3
A and B
A= $$pos < $lenB= substr($text, $$pos, 1) ne "\n"
ABresult
0X0
100
111
206 0 $$pos++;
207 }
208 0 next;
209 }
210  
211 # Handle block comments
212 588 ▶ if ($ch eq '/' && $$pos + 1 < $len && substr($text, $$pos + 1, 1) eq '*') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "/" and $$pos + 1 < $len and substr($text, $$pos + 1, 1) eq "*") 0 588 588
Condition33%2/6
$ch eq "/" and $$pos + 1 < $len and substr($text, $$pos + 1, 1) eq "*"
!ll&&!rl&&r
58800
$ch eq "/" and $$pos + 1 < $len
!ll&&!rl&&r
58800
MC/DC0%0/3
$ch eq "/" and $$pos + 1 < $len and substr($text, $$pos + 1, 1) eq "*"
$ch eq "/" $$pos + 1 < $len substr($text, $$pos + 1, 1) eq "*"
Truth table25%1/4
A and B and C
A= $ch eq "/"B= $$pos + 1 < $lenC= substr($text, $$pos + 1, 1) eq "*"
ABCresult
0XX0
10X0
1100
1111
213 0 $$pos += 2;
214 0 ▶ while ($$pos < $len - 1 && substr($text, $$pos, 2) ne '*/') {
Branch: 1 branch
BranchTrueFalseTotal
if ($$pos < $len - 1 and substr($text, $$pos, 2) ne "*/") 0 0 0
Condition0%0/3
$$pos < $len - 1 and substr($text, $$pos, 2) ne "*/"
!ll&&!rl&&r
000
MC/DC0%0/2
$$pos < $len - 1 and substr($text, $$pos, 2) ne "*/"
$$pos < $len - 1 substr($text, $$pos, 2) ne "*/"
Truth table0%0/3
A and B
A= $$pos < $len - 1B= substr($text, $$pos, 2) ne "*/"
ABresult
0X0
100
111
215 0 $$pos++;
216 }
217 0 ▶ $$pos += 2 if $$pos < $len - 1;
Branch: 1 branch
BranchTrueFalseTotal
if $$pos < $len - 1 0 0 0
218 0 next;
219 }
220  
221 588 ▶ if ($ch eq '{' || $ch eq '[' || $ch eq '(') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "{" or $ch eq "[" or $ch eq "(") 35 553 588
Condition83%5/6
$ch eq "{" or $ch eq "[" or $ch eq "("
l!l&&r!l&&!r
926553
$ch eq "{" or $ch eq "["
l!l&&r!l&&!r
90579
MC/DC66%2/3
$ch eq "{" or $ch eq "[" or $ch eq "("
$ch eq "{" $ch eq "[" $ch eq "("
Truth table75%3/4
A or B or C
A= $ch eq "{"B= $ch eq "["C= $ch eq "("
ABCresult
1XX1
01X1
0011
0000
222 35 $depth++;
223 35 $$pos++;
224 35 next;
225 }
226 553 ▶ if ($ch eq '}' || $ch eq ']' || $ch eq ')') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "}" or $ch eq "]" or $ch eq ")") 47 506 553
Condition83%5/6
$ch eq "}" or $ch eq "]" or $ch eq ")"
l!l&&r!l&&!r
2126506
$ch eq "}" or $ch eq "]"
l!l&&r!l&&!r
210532
MC/DC66%2/3
$ch eq "}" or $ch eq "]" or $ch eq ")"
$ch eq "}" $ch eq "]" $ch eq ")"
Truth table75%3/4
A or B or C
A= $ch eq "}"B= $ch eq "]"C= $ch eq ")"
ABCresult
1XX1
01X1
0011
0000
227 47 ▶ if ($depth == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth == 0) 12 35 47
228 12 last; # end of enclosing object/array
229 }
230 35 $depth--;
231 35 $$pos++;
232 35 next;
233 }
234 506 ▶ if ($ch eq ',' && $depth == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "," and $depth == 0) 4 502 506
Condition100%3/3
$ch eq "," and $depth == 0
!ll&&!rl&&r
49664
MC/DC100%2/2
$ch eq "," and $depth == 0
$ch eq "," $depth == 0
Truth table100%3/3
A and B
A= $ch eq ","B= $depth == 0
ABresult
0X0
100
111
235 4 last;
236 }
237 502 $$pos++;
238 }
239  
240 16 my $val = substr($text, $start, $$pos - $start);
241 16 $val =~ s/^\s+//;
242 16 $val =~ s/\s+$//;
243 16 return $val;
244 }
245  
246 # Skip over a string without returning its content (for balanced scanning)
247 10 ▶sub _skip_string {
Subroutine _skip_string: called CC=4 SCAR=14.2
248 10 my ($text, $pos) = @_;
249 10 my $quote = substr($text, $$pos, 1);
250 10 $$pos++;
251 10 my $len = length($text);
252 10 ▶ while ($$pos < $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$pos < $len) 103 0 103
253 103 my $ch = substr($text, $$pos, 1);
254 103 ▶ if ($ch eq '\\') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\\") 0 103 103
255 0 $$pos += 2;
256 0 next;
257 }
258 103 ▶ if ($ch eq $quote) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq $quote) 10 93 103
259 10 $$pos++;
260 10 return;
261 }
262 93 $$pos++;
263 }
264 }
265  
266 #----------------------------------------------------------------------
267 # Read data value (parse JS literal to Perl scalar/hashref/arrayref)
268 #----------------------------------------------------------------------
269  
270 41 ▶sub _read_data_value {
Subroutine _read_data_value: called CC=17 SCAR=29.5
271 41 my ($text, $pos) = @_;
272 41 _skip_ws($text, $pos);
273 41 my $len = length($text);
274 41 ▶ return undef if $$pos >= $len;
Branch: 1 branch
BranchTrueFalseTotal
if $$pos >= $len 0 41 41
275  
276 41 my $ch = substr($text, $$pos, 1);
277  
278 # String
279 41 ▶ if ($ch eq '"' || $ch eq "'") {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "\"" or $ch eq "'") 17 24 41
Condition100%3/3
$ch eq "\"" or $ch eq "'"
l!l&&r!l&&!r
31424
MC/DC100%2/2
$ch eq "\"" or $ch eq "'"
$ch eq "\"" $ch eq "'"
Truth table100%3/3
A or B
A= $ch eq "\""B= $ch eq "'"
ABresult
1X1
011
000
280 17 return _read_string($text, $pos);
281 }
282  
283 # Backtick template literal - read as string
284 24 ▶ if ($ch eq '`') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "`") 0 24 24
285 0 return _read_string($text, $pos);
286 }
287  
288 # Array
289 24 ▶ if ($ch eq '[') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "[") 2 22 24
290 2 return _read_data_array($text, $pos);
291 }
292  
293 # Object
294 22 ▶ if ($ch eq '{') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch eq "{") 12 10 22
295 12 return _read_data_object($text, $pos);
296 }
297  
298 # Boolean / null / undefined / number / bare value
299 10 my $start = $$pos;
300 10 ▶ while ($$pos < $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$pos < $len) 25 0 25
301 25 my $c = substr($text, $$pos, 1);
302 25 ▶ last if $c eq ',' || $c eq '}' || $c eq ']' || $c =~ /\s/;
Branch: 1 branch
BranchTrueFalseTotal
if $c eq "," or $c eq "}" or $c eq "]" or $c =~ /\s/ 10 15 25
Condition77%7/9
$c eq "," or $c eq "}" or $c eq "]" or $c =~ /\s/
l!l&&r!l&&!r
4615
$c eq "," or $c eq "}" or $c eq "]"
l!l&&r!l&&!r
4021
$c eq "," or $c eq "}"
l!l&&r!l&&!r
4021
MC/DC50%2/4
$c eq "," or $c eq "}" or $c eq "]" or $c =~ /\s/
$c eq "," $c eq "}" $c eq "]" $c =~ /\s/
Truth table60%3/5
A or B or C or D
A= $c eq ","B= $c eq "}"C= $c eq "]"D= $c =~ /\s/
ABCDresult
1XXX1
01XX1
001X1
00011
00000
303 15 $$pos++;
304 }
305 10 my $raw = substr($text, $start, $$pos - $start);
306 # Convert JS types to Perl
307 10 ▶ return 1 if $raw eq 'true';
Branch: 1 branch
BranchTrueFalseTotal
if $raw eq "true" 1 9 10
308 9 ▶ return 0 if $raw eq 'false';
Branch: 1 branch
BranchTrueFalseTotal
if $raw eq "false" 0 9 9
309 9 ▶ return undef if $raw eq 'null' || $raw eq 'undefined';
Branch: 1 branch
BranchTrueFalseTotal
if $raw eq "null" or $raw eq "undefined" 0 9 9
Condition33%1/3
$raw eq "null" or $raw eq "undefined"
l!l&&r!l&&!r
009
MC/DC0%0/2
$raw eq "null" or $raw eq "undefined"
$raw eq "null" $raw eq "undefined"
Truth table33%1/3
A or B
A= $raw eq "null"B= $raw eq "undefined"
ABresult
1X1
011
000
310 # Numbers
311 9 ▶ if ($raw =~ /^-?\d+\.?\d*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($raw =~ /^-?\d+\.?\d*$/) 9 0 9
312 9 return $raw + 0;
313 }
314 0 return $raw;
315 }
316  
317 2 ▶sub _read_data_array {
Subroutine _read_data_array: called CC=7 SCAR=19.7
318 2 my ($text, $pos) = @_;
319 2 $$pos++; # skip [
320 2 my @items;
321 2 my $len = length($text);
322 2 ▶ while ($$pos < $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$pos < $len) 7 0 7
323 7 _skip_ws($text, $pos);
324 7 ▶ last if $$pos >= $len;
Branch: 1 branch
BranchTrueFalseTotal
if $$pos >= $len 0 7 7
325 7 ▶ last if substr($text, $$pos, 1) eq ']';
Branch: 1 branch
BranchTrueFalseTotal
if substr($text, $$pos, 1) eq "]" 2 5 7
326  
327 5 push @items, _read_data_value($text, $pos);
328  
329 5 _skip_ws($text, $pos);
330 5 ▶ if ($$pos < $len && substr($text, $$pos, 1) eq ',') {
Branch: 1 branch
BranchTrueFalseTotal
if ($$pos < $len and substr($text, $$pos, 1) eq ",") 3 2 5
Condition66%2/3
$$pos < $len and substr($text, $$pos, 1) eq ","
!ll&&!rl&&r
023
MC/DC50%1/2
$$pos < $len and substr($text, $$pos, 1) eq ","
$$pos < $len substr($text, $$pos, 1) eq ","
Truth table66%2/3
A and B
A= $$pos < $lenB= substr($text, $$pos, 1) eq ","
ABresult
0X0
100
111
331 3 $$pos++; # skip comma
332 }
333 }
334 2 ▶ $$pos++ if $$pos < $len; # skip ]
Branch: 1 branch
BranchTrueFalseTotal
if $$pos < $len 2 0 2
335 2 return \@items;
336 }
337  
338 12 ▶sub _read_data_object {
Subroutine _read_data_object: called CC=9 SCAR=22.6
339 12 my ($text, $pos) = @_;
340 12 $$pos++; # skip {
341 12 my %obj;
342 12 my $len = length($text);
343 12 ▶ while ($$pos < $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$pos < $len) 31 0 31
344 31 _skip_ws($text, $pos);
345 31 ▶ last if $$pos >= $len;
Branch: 1 branch
BranchTrueFalseTotal
if $$pos >= $len 0 31 31
346 31 ▶ last if substr($text, $$pos, 1) eq '}';
Branch: 1 branch
BranchTrueFalseTotal
if substr($text, $$pos, 1) eq "}" 12 19 31
347  
348 19 my $key = _read_key($text, $pos);
349 19 _skip_ws($text, $pos);
350  
351 # skip colon
352 19 ▶ if ($$pos < $len && substr($text, $$pos, 1) eq ':') {
Branch: 1 branch
BranchTrueFalseTotal
if ($$pos < $len and substr($text, $$pos, 1) eq ":") 19 0 19
Condition33%1/3
$$pos < $len and substr($text, $$pos, 1) eq ":"
!ll&&!rl&&r
0019
MC/DC0%0/2
$$pos < $len and substr($text, $$pos, 1) eq ":"
$$pos < $len substr($text, $$pos, 1) eq ":"
Truth table33%1/3
A and B
A= $$pos < $lenB= substr($text, $$pos, 1) eq ":"
ABresult
0X0
100
111
353 19 $$pos++;
354 }
355  
356 19 _skip_ws($text, $pos);
357 19 $obj{$key} = _read_data_value($text, $pos);
358  
359 19 _skip_ws($text, $pos);
360 19 ▶ if ($$pos < $len && substr($text, $$pos, 1) eq ',') {
Branch: 1 branch
BranchTrueFalseTotal
if ($$pos < $len and substr($text, $$pos, 1) eq ",") 8 11 19
Condition66%2/3
$$pos < $len and substr($text, $$pos, 1) eq ","
!ll&&!rl&&r
0118
MC/DC50%1/2
$$pos < $len and substr($text, $$pos, 1) eq ","
$$pos < $len substr($text, $$pos, 1) eq ","
Truth table66%2/3
A and B
A= $$pos < $lenB= substr($text, $$pos, 1) eq ","
ABresult
0X0
100
111
361 8 $$pos++; # skip comma
362 }
363 }
364 12 ▶ $$pos++ if $$pos < $len; # skip }
Branch: 1 branch
BranchTrueFalseTotal
if $$pos < $len 12 0 12
365 12 return \%obj;
366 }
367  
368 #----------------------------------------------------------------------
369 # Parse function map: { key: funcExpr, ... } keeping values as raw strings
370 #----------------------------------------------------------------------
371  
372 13 ▶sub _parse_function_map {
Subroutine _parse_function_map: called CC=9 SCAR=22.6
373 13 my ($text, $pos) = @_;
374 13 $$pos++; # skip opening {
375 13 my %map;
376 13 my $len = length($text);
377 13 ▶ while ($$pos < $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$pos < $len) 29 0 29
378 29 _skip_ws($text, $pos);
379 29 ▶ last if $$pos >= $len;
Branch: 1 branch
BranchTrueFalseTotal
if $$pos >= $len 0 29 29
380 29 ▶ last if substr($text, $$pos, 1) eq '}';
Branch: 1 branch
BranchTrueFalseTotal
if substr($text, $$pos, 1) eq "}" 13 16 29
381  
382 16 my $key = _read_key($text, $pos);
383 16 _skip_ws($text, $pos);
384  
385 # skip colon
386 16 ▶ if ($$pos < $len && substr($text, $$pos, 1) eq ':') {
Branch: 1 branch
BranchTrueFalseTotal
if ($$pos < $len and substr($text, $$pos, 1) eq ":") 16 0 16
Condition33%1/3
$$pos < $len and substr($text, $$pos, 1) eq ":"
!ll&&!rl&&r
0016
MC/DC0%0/2
$$pos < $len and substr($text, $$pos, 1) eq ":"
$$pos < $len substr($text, $$pos, 1) eq ":"
Truth table33%1/3
A and B
A= $$pos < $lenB= substr($text, $$pos, 1) eq ":"
ABresult
0X0
100
111
387 16 $$pos++;
388 }
389  
390 16 _skip_ws($text, $pos);
391 16 $map{$key} = _read_balanced($text, $pos);
392  
393 16 _skip_ws($text, $pos);
394 16 ▶ if ($$pos < $len && substr($text, $$pos, 1) eq ',') {
Branch: 1 branch
BranchTrueFalseTotal
if ($$pos < $len and substr($text, $$pos, 1) eq ",") 4 12 16
Condition66%2/3
$$pos < $len and substr($text, $$pos, 1) eq ","
!ll&&!rl&&r
0124
MC/DC50%1/2
$$pos < $len and substr($text, $$pos, 1) eq ","
$$pos < $len substr($text, $$pos, 1) eq ","
Truth table66%2/3
A and B
A= $$pos < $lenB= substr($text, $$pos, 1) eq ","
ABresult
0X0
100
111
395 4 $$pos++;
396 }
397 }
398 13 ▶ $$pos++ if $$pos < $len; # skip closing }
Branch: 1 branch
BranchTrueFalseTotal
if $$pos < $len 13 0 13
399 13 return \%map;
400 }
401  
402 #----------------------------------------------------------------------
403 # Parse recipes map: { name: { ... }, ... } where each value is like a
404 # top-level script block but parsed at the object level
405 #----------------------------------------------------------------------
406  
407 1 ▶sub _parse_recipes_map {
Subroutine _parse_recipes_map: called CC=10 SCAR=24.9
408 1 my ($text, $pos) = @_;
409 1 $$pos++; # skip opening {
410 1 my %recipes;
411 1 my $len = length($text);
412 1 ▶ while ($$pos < $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$pos < $len) 2 0 2
413 2 _skip_ws($text, $pos);
414 2 ▶ last if $$pos >= $len;
Branch: 1 branch
BranchTrueFalseTotal
if $$pos >= $len 0 2 2
415 2 ▶ last if substr($text, $$pos, 1) eq '}';
Branch: 1 branch
BranchTrueFalseTotal
if substr($text, $$pos, 1) eq "}" 1 1 2
416  
417 1 my $recipe_name = _read_key($text, $pos);
418 1 _skip_ws($text, $pos);
419  
420 # skip colon
421 1 ▶ if ($$pos < $len && substr($text, $$pos, 1) eq ':') {
Branch: 1 branch
BranchTrueFalseTotal
if ($$pos < $len and substr($text, $$pos, 1) eq ":") 1 0 1
Condition33%1/3
$$pos < $len and substr($text, $$pos, 1) eq ":"
!ll&&!rl&&r
001
MC/DC0%0/2
$$pos < $len and substr($text, $$pos, 1) eq ":"
$$pos < $len substr($text, $$pos, 1) eq ":"
Truth table33%1/3
A and B
A= $$pos < $lenB= substr($text, $$pos, 1) eq ":"
ABresult
0X0
100
111
422 1 $$pos++;
423 }
424  
425 1 _skip_ws($text, $pos);
426  
427 1 ▶ if (substr($text, $$pos, 1) eq '{') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($text, $$pos, 1) eq "{") 1 0 1
428 1 $recipes{$recipe_name} = _parse_recipe_object($text, $pos);
429 }
430  
431 1 _skip_ws($text, $pos);
432 1 ▶ if ($$pos < $len && substr($text, $$pos, 1) eq ',') {
Branch: 1 branch
BranchTrueFalseTotal
if ($$pos < $len and substr($text, $$pos, 1) eq ",") 0 1 1
Condition33%1/3
$$pos < $len and substr($text, $$pos, 1) eq ","
!ll&&!rl&&r
010
MC/DC0%0/2
$$pos < $len and substr($text, $$pos, 1) eq ","
$$pos < $len substr($text, $$pos, 1) eq ","
Truth table33%1/3
A and B
A= $$pos < $lenB= substr($text, $$pos, 1) eq ","
ABresult
0X0
100
111
433 0 $$pos++;
434 }
435 }
436 1 ▶ $$pos++ if $$pos < $len; # skip closing }
Branch: 1 branch
BranchTrueFalseTotal
if $$pos < $len 1 0 1
437 1 return \%recipes;
438 }
439  
440 # Parse a single recipe object (same dispatch as top-level)
441 1 ▶sub _parse_recipe_object {
Subroutine _parse_recipe_object: called CC=9 SCAR=22.6
442 1 my ($text, $pos) = @_;
443 1 $$pos++; # skip opening {
444 1 my %result;
445 1 my $len = length($text);
446 1 ▶ while ($$pos < $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$pos < $len) 3 0 3
447 3 _skip_ws($text, $pos);
448 3 ▶ last if $$pos >= $len;
Branch: 1 branch
BranchTrueFalseTotal
if $$pos >= $len 0 3 3
449 3 ▶ last if substr($text, $$pos, 1) eq '}';
Branch: 1 branch
BranchTrueFalseTotal
if substr($text, $$pos, 1) eq "}" 1 2 3
450  
451 2 my $key = _read_key($text, $pos);
452 2 _skip_ws($text, $pos);
453  
454 # skip colon
455 2 ▶ if ($$pos < $len && substr($text, $$pos, 1) eq ':') {
Branch: 1 branch
BranchTrueFalseTotal
if ($$pos < $len and substr($text, $$pos, 1) eq ":") 2 0 2
Condition33%1/3
$$pos < $len and substr($text, $$pos, 1) eq ":"
!ll&&!rl&&r
002
MC/DC0%0/2
$$pos < $len and substr($text, $$pos, 1) eq ":"
$$pos < $len substr($text, $$pos, 1) eq ":"
Truth table33%1/3
A and B
A= $$pos < $lenB= substr($text, $$pos, 1) eq ":"
ABresult
0X0
100
111
456 2 $$pos++;
457 }
458 2 _skip_ws($text, $pos);
459  
460 2 $result{$key} = _parse_value_for_key($key, $text, $pos);
461  
462 2 _skip_ws($text, $pos);
463 2 ▶ if ($$pos < $len && substr($text, $$pos, 1) eq ',') {
Branch: 1 branch
BranchTrueFalseTotal
if ($$pos < $len and substr($text, $$pos, 1) eq ",") 1 1 2
Condition66%2/3
$$pos < $len and substr($text, $$pos, 1) eq ","
!ll&&!rl&&r
011
MC/DC50%1/2
$$pos < $len and substr($text, $$pos, 1) eq ","
$$pos < $len substr($text, $$pos, 1) eq ","
Truth table66%2/3
A and B
A= $$pos < $lenB= substr($text, $$pos, 1) eq ","
ABresult
0X0
100
111
464 1 $$pos++;
465 }
466 }
467 1 ▶ $$pos++ if $$pos < $len; # skip closing }
Branch: 1 branch
BranchTrueFalseTotal
if $$pos < $len 1 0 1
468 1 return \%result;
469 }
470  
471 #----------------------------------------------------------------------
472 # Value dispatch based on key name
473 #----------------------------------------------------------------------
474  
475 31 ▶sub _parse_value_for_key {
Subroutine _parse_value_for_key: called CC=7 SCAR=19.7
476 31 my ($key, $text, $pos) = @_;
477  
478 31 ▶ if ($FUNCTION_KEYS{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($FUNCTION_KEYS{$key}) 13 18 31
479 13 ▶ if (substr($text, $$pos, 1) eq '{') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($text, $$pos, 1) eq "{") 13 0 13
480 13 return _parse_function_map($text, $pos);
481 }
482 0 return _read_balanced($text, $pos);
483 }
484 18 ▶ if ($DATA_KEYS{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($DATA_KEYS{$key}) 12 6 18
485 12 return _read_data_value($text, $pos);
486 }
487 6 ▶ if ($ARRAY_KEYS{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ARRAY_KEYS{$key}) 1 5 6
488 1 return _read_data_value($text, $pos);
489 }
490 5 ▶ if ($key eq 'recipes') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "recipes") 1 4 5
491 1 ▶ if (substr($text, $$pos, 1) eq '{') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($text, $$pos, 1) eq "{") 1 0 1
492 1 return _parse_recipes_map($text, $pos);
493 }
494 0 return _read_data_value($text, $pos);
495 }
496 # Default: scalar (string, number, etc)
497 4 return _read_data_value($text, $pos);
498 }
499  
500 #----------------------------------------------------------------------
501 # Top-level script parser
502 #----------------------------------------------------------------------
503  
504 23 ▶sub _parse_top_object {
Subroutine _parse_top_object: called CC=12 SCAR=25.1
505 23 my ($text) = @_;
506 23 my $pos = 0;
507 23 my $len = length($text);
508 23 my %result;
509  
510 23 _skip_ws($text, \$pos);
511  
512 # Skip opening { if present
513 23 my $has_braces = 0;
514 23 ▶ if ($pos < $len && substr($text, $pos, 1) eq '{') {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < $len and substr($text, $pos, 1) eq "{") 22 1 23
Condition66%2/3
$pos < $len and substr($text, $pos, 1) eq "{"
!ll&&!rl&&r
0122
MC/DC50%1/2
$pos < $len and substr($text, $pos, 1) eq "{"
$pos < $len substr($text, $pos, 1) eq "{"
Truth table66%2/3
A and B
A= $pos < $lenB= substr($text, $pos, 1) eq "{"
ABresult
0X0
100
111
515 22 $has_braces = 1;
516 22 $pos++;
517 }
518  
519 23 ▶ while ($pos < $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < $len) 51 1 52
520 51 _skip_ws($text, \$pos);
521 51 ▶ last if $pos >= $len;
Branch: 1 branch
BranchTrueFalseTotal
if $pos >= $len 0 51 51
522 51 ▶ last if $has_braces && substr($text, $pos, 1) eq '}';
Branch: 1 branch
BranchTrueFalseTotal
if $has_braces and substr($text, $pos, 1) eq "}" 22 29 51
Condition100%3/3
$has_braces and substr($text, $pos, 1) eq "}"
!ll&&!rl&&r
22722
MC/DC100%2/2
$has_braces and substr($text, $pos, 1) eq "}"
$has_braces substr($text, $pos, 1) eq "}"
Truth table100%3/3
A and B
A= $has_bracesB= substr($text, $pos, 1) eq "}"
ABresult
0X0
100
111
523  
524 29 my $key = _read_key($text, \$pos);
525 29 ▶ last unless length($key);
Branch: 1 branch
BranchTrueFalseTotal
unless length $key 0 29 29
526  
527 29 _skip_ws($text, \$pos);
528  
529 # skip colon
530 29 ▶ if ($pos < $len && substr($text, $pos, 1) eq ':') {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < $len and substr($text, $pos, 1) eq ":") 29 0 29
Condition33%1/3
$pos < $len and substr($text, $pos, 1) eq ":"
!ll&&!rl&&r
0029
MC/DC0%0/2
$pos < $len and substr($text, $pos, 1) eq ":"
$pos < $len substr($text, $pos, 1) eq ":"
Truth table33%1/3
A and B
A= $pos < $lenB= substr($text, $pos, 1) eq ":"
ABresult
0X0
100
111
531 29 $pos++;
532 }
533 29 _skip_ws($text, \$pos);
534  
535 29 $result{$key} = _parse_value_for_key($key, $text, \$pos);
536  
537 29 _skip_ws($text, \$pos);
538 29 ▶ if ($pos < $len && substr($text, $pos, 1) eq ',') {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < $len and substr($text, $pos, 1) eq ",") 7 22 29
Condition100%3/3
$pos < $len and substr($text, $pos, 1) eq ","
!ll&&!rl&&r
1217
MC/DC100%2/2
$pos < $len and substr($text, $pos, 1) eq ","
$pos < $len substr($text, $pos, 1) eq ","
Truth table100%3/3
A and B
A= $pos < $lenB= substr($text, $pos, 1) eq ","
ABresult
0X0
100
111
539 7 $pos++;
540 }
541 }
542  
543 23 return \%result;
544 }
545  
546 2 1;
« Spiderpup.pm ↑ Summary Spiderpup/Transform.pm »