| 1 | package JQ::Lite::Expression; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 182 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 182 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 182 | ▶ | use JSON::PP (); # lightweight decoder for string literals | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 182 | ▶ | use Scalar::Util qw(looks_like_number); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | # Internal constant used to signal that parsing failed and callers should | ||||||||||||||||||||||||||||
| 10 | # silently fall back to other heuristics. | ||||||||||||||||||||||||||||
| 11 | 182 | my $PARSE_ERROR = "__JQ_LITE_EXPR_PARSE_ERROR__"; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 66 | ▶ | sub evaluate { | ||||||||||||||||||||||||||
|
Subroutine
evaluate: called CC=11 SCAR=24.9
| |||||||||||||||||||||||||||||
| 14 | 66 | my (%opts) = @_; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 66 | my $expr = $opts{expr}; | |||||||||||||||||||||||||||
| 17 | 66 | my $context = $opts{context}; | |||||||||||||||||||||||||||
| 18 | 0 | ▶ | my $resolve_path = $opts{resolve_path} || sub { return undef }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=2.0
Condition33%1/3
$opts{'resolve_path'} || sub {
return undef;
}
MC/DC0%0/2
$opts{'resolve_path'} || sub {
return undef;
}
$opts{'resolve_path'}
sub {
return undef;
}
Truth table33%1/3
A || B
A= $opts{'resolve_path'}B= sub {
return undef;
}
| |||||||||||||||||||||||||||||
| 19 | 66 | ▶ | my $coerce_number = $opts{coerce_number} || \&_default_coerce_number; | ||||||||||||||||||||||||||
|
Condition50%1/2
$opts{'coerce_number'} || \&JQ::Lite::Expression::_default_coerce_number
MC/DC0%0/1
$opts{'coerce_number'} || \&JQ::Lite::Expression::_default_coerce_number
$opts{'coerce_number'}
Truth table50%1/2
A
A= $opts{'coerce_number'}
| |||||||||||||||||||||||||||||
| 20 | 66 | ▶ | my $builtins = $opts{builtins} || {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$opts{'builtins'} || {}
MC/DC0%0/1
$opts{'builtins'} || {}
$opts{'builtins'}
Truth table50%1/2
A
A= $opts{'builtins'}
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 66 | ▶ | return (0, undef) unless defined $expr; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 66 | ▶ | my $tokens = _tokenize($expr) or return (0, undef); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | 41 | my $state = { | |||||||||||||||||||||||||||
| 26 | tokens => $tokens, | ||||||||||||||||||||||||||||
| 27 | pos => 0, | ||||||||||||||||||||||||||||
| 28 | }; | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 41 | my $ast = eval { _parse_expression($state, 0) }; | |||||||||||||||||||||||||||
| 31 | 41 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 7 | ▶ | return (0, undef) if $@ =~ /\Q$PARSE_ERROR\E/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | 0 | die $@; | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 34 | my $next = _peek($state); | |||||||||||||||||||||||||||
| 37 | 34 | ▶ | return (0, undef) unless $next->{type} eq 'EOF'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 34 | my $value = eval { | |||||||||||||||||||||||||||
| 40 | 34 | _eval_node( | |||||||||||||||||||||||||||
| 41 | $ast, | ||||||||||||||||||||||||||||
| 42 | { | ||||||||||||||||||||||||||||
| 43 | context => $context, | ||||||||||||||||||||||||||||
| 44 | resolve_path => $resolve_path, | ||||||||||||||||||||||||||||
| 45 | coerce_number => $coerce_number, | ||||||||||||||||||||||||||||
| 46 | builtins => $builtins, | ||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | ); | ||||||||||||||||||||||||||||
| 49 | }; | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 34 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 25 | ▶ | return (0, undef) if $@ =~ /\Q$PARSE_ERROR\E/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 8 | die $@; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 9 | return (1, $value); | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 66 | ▶ | sub _tokenize { | ||||||||||||||||||||||||||
|
Subroutine
_tokenize: called CC=17 SCAR=29.0
| |||||||||||||||||||||||||||||
| 60 | 66 | my ($expr) = @_; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 66 | my @tokens; | |||||||||||||||||||||||||||
| 63 | 66 | my $len = length $expr; | |||||||||||||||||||||||||||
| 64 | 66 | my $i = 0; | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 66 | ▶ | while ($i < $len) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 240 | my $char = substr($expr, $i, 1); | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 240 | ▶ | if ($char =~ /\s/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 69 | $i++; | |||||||||||||||||||||||||||
| 71 | 69 | next; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 171 | ▶ | if ($char =~ /[+\-*\/%]/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 37 | push @tokens, { type => 'OP', value => $char }; | |||||||||||||||||||||||||||
| 76 | 37 | $i++; | |||||||||||||||||||||||||||
| 77 | 37 | next; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 134 | ▶ | if ($char eq '(') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 5 | push @tokens, { type => 'LPAREN', value => '(' }; | |||||||||||||||||||||||||||
| 82 | 5 | $i++; | |||||||||||||||||||||||||||
| 83 | 5 | next; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 129 | ▶ | if ($char eq ')') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 2 | push @tokens, { type => 'RPAREN', value => ')' }; | |||||||||||||||||||||||||||
| 88 | 2 | $i++; | |||||||||||||||||||||||||||
| 89 | 2 | next; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 127 | ▶ | if ($char eq ',') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 0 | push @tokens, { type => 'COMMA', value => ',' }; | |||||||||||||||||||||||||||
| 94 | 0 | $i++; | |||||||||||||||||||||||||||
| 95 | 0 | next; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 127 | ▶ | if ($char eq '.') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 53 | my ($consumed, $path) = _consume_path($expr, $i); | |||||||||||||||||||||||||||
| 100 | 53 | ▶ | return unless defined $consumed; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 53 | $i += $consumed; | |||||||||||||||||||||||||||
| 102 | 53 | ▶ | if (!defined $path || $path eq '') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $path or $path eq ""
MC/DC50%1/2
not defined $path or $path eq ""
not defined $path
$path eq ""
Truth table66%2/3
not(A) or B
A= not defined $pathB= $path eq ""
| |||||||||||||||||||||||||||||
| 103 | 5 | push @tokens, { type => 'CURRENT' }; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | else { | ||||||||||||||||||||||||||||
| 106 | 48 | push @tokens, { type => 'PATH', value => $path }; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | 53 | next; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 74 | ▶ | if ($char eq '"') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 2 | my ($consumed, $value) = _consume_json_string($expr, $i); | |||||||||||||||||||||||||||
| 113 | 2 | ▶ | return unless defined $consumed; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | 2 | $i += $consumed; | |||||||||||||||||||||||||||
| 115 | 2 | push @tokens, { type => 'STRING', value => $value }; | |||||||||||||||||||||||||||
| 116 | 2 | next; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 72 | ▶ | if ($char eq "'") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 0 | my ($consumed, $value) = _consume_single_string($expr, $i); | |||||||||||||||||||||||||||
| 121 | 0 | ▶ | return unless defined $consumed; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 0 | $i += $consumed; | |||||||||||||||||||||||||||
| 123 | 0 | push @tokens, { type => 'STRING', value => $value }; | |||||||||||||||||||||||||||
| 124 | 0 | next; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 72 | ▶ | if (substr($expr, $i) =~ /\G(-?\d+(?:\.\d+)?)/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | 18 | my $match = $1; | |||||||||||||||||||||||||||
| 129 | 18 | my $len_match = length $match; | |||||||||||||||||||||||||||
| 130 | 18 | push @tokens, { type => 'NUMBER', value => 0 + $match }; | |||||||||||||||||||||||||||
| 131 | 18 | $i += $len_match; | |||||||||||||||||||||||||||
| 132 | 18 | next; | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 54 | ▶ | if (substr($expr, $i) =~ /\G([A-Za-z_][A-Za-z0-9_]*)/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | 29 | my $ident = $1; | |||||||||||||||||||||||||||
| 137 | 29 | push @tokens, { type => 'IDENT', value => $ident }; | |||||||||||||||||||||||||||
| 138 | 29 | $i += length $ident; | |||||||||||||||||||||||||||
| 139 | 29 | next; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 25 | return; | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 41 | push @tokens, { type => 'EOF', value => undef }; | |||||||||||||||||||||||||||
| 146 | 41 | return \@tokens; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 53 | ▶ | sub _consume_path { | ||||||||||||||||||||||||||
|
Subroutine
_consume_path: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||
| 150 | 53 | my ($expr, $start) = @_; | |||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 53 | my $len = length $expr; | |||||||||||||||||||||||||||
| 153 | 53 | my $i = $start + 1; # skip the leading '.' | |||||||||||||||||||||||||||
| 154 | 53 | my $depth = 0; | |||||||||||||||||||||||||||
| 155 | 53 | my $path = ''; | |||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 53 | ▶ | while ($i < $len) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | 225 | my $char = substr($expr, $i, 1); | |||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 225 | ▶ | last if $depth == 0 && $char =~ /[+\-*\/%(),\s]/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$depth == 0 and $char =~ m[[+\-*/%(),\s]]
MC/DC100%2/2
$depth == 0 and $char =~ m[[+\-*/%(),\s]]
$depth == 0
$char =~ m[[+\-*/%(),\s]]
Truth table100%3/3
A and B
A= $depth == 0B= $char =~ m[[+\-*/%(),\s]]
| |||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 183 | ▶ | if ($char eq '[') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 163 | 1 | $depth++; | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | elsif ($char eq ']') { | ||||||||||||||||||||||||||||
| 166 | 1 | ▶ | $depth-- if $depth; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 183 | $path .= $char; | |||||||||||||||||||||||||||
| 170 | 183 | $i++; | |||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 53 | $path =~ s/\s+$//; | |||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 53 | return ($i - $start, $path); | |||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 2 | ▶ | sub _consume_json_string { | ||||||||||||||||||||||||||
|
Subroutine
_consume_json_string: called CC=7 SCAR=22.3
| |||||||||||||||||||||||||||||
| 179 | 2 | my ($expr, $start) = @_; | |||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 2 | my $i = $start; | |||||||||||||||||||||||||||
| 182 | 2 | my $len = length $expr; | |||||||||||||||||||||||||||
| 183 | 2 | my $end = $i + 1; | |||||||||||||||||||||||||||
| 184 | 2 | my $escaped = 0; | |||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 2 | ▶ | while ($end < $len) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 187 | 22 | my $char = substr($expr, $end, 1); | |||||||||||||||||||||||||||
| 188 | 22 | ▶ | if ($escaped) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 189 | 0 | $escaped = 0; | |||||||||||||||||||||||||||
| 190 | 0 | $end++; | |||||||||||||||||||||||||||
| 191 | 0 | next; | |||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 22 | ▶ | if ($char eq '\\') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 195 | 0 | $escaped = 1; | |||||||||||||||||||||||||||
| 196 | 0 | $end++; | |||||||||||||||||||||||||||
| 197 | 0 | next; | |||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 22 | ▶ | if ($char eq '"') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 201 | 2 | my $text = substr($expr, $i, $end - $i + 1); | |||||||||||||||||||||||||||
| 202 | 2 | my $decoded = eval { JSON::PP::decode_json($text) }; | |||||||||||||||||||||||||||
| 203 | 2 | ▶ | return unless defined $decoded && !$@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $decoded and not $@
MC/DC0%0/2
defined $decoded and not $@
defined $decoded
not $@
Truth table33%1/3
A and not(B)
A= defined $decodedB= not $@
| |||||||||||||||||||||||||||||
| 204 | 2 | return ($end - $i + 1, $decoded); | |||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | 20 | $end++; | |||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | 0 | return; | |||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | 0 | ▶ | sub _consume_single_string { | ||||||||||||||||||||||||||
|
Subroutine
_consume_single_string: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 214 | 0 | my ($expr, $start) = @_; | |||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | 0 | my $i = $start + 1; | |||||||||||||||||||||||||||
| 217 | 0 | my $len = length $expr; | |||||||||||||||||||||||||||
| 218 | 0 | my $value = ''; | |||||||||||||||||||||||||||
| 219 | 0 | my $escaped = 0; | |||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | 0 | ▶ | while ($i < $len) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 222 | 0 | my $char = substr($expr, $i, 1); | |||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | 0 | ▶ | if ($escaped) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 225 | 0 | $value .= $char; | |||||||||||||||||||||||||||
| 226 | 0 | $escaped = 0; | |||||||||||||||||||||||||||
| 227 | 0 | $i++; | |||||||||||||||||||||||||||
| 228 | 0 | next; | |||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | 0 | ▶ | if ($char eq '\\') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 232 | 0 | $escaped = 1; | |||||||||||||||||||||||||||
| 233 | 0 | $i++; | |||||||||||||||||||||||||||
| 234 | 0 | next; | |||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | 0 | ▶ | if ($char eq "'") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 238 | 0 | return ($i - $start + 1, $value); | |||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | 0 | $value .= $char; | |||||||||||||||||||||||||||
| 242 | 0 | $i++; | |||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | 0 | return; | |||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | 182 | my %LBP = ( | |||||||||||||||||||||||||||
| 249 | '+' => 10, | ||||||||||||||||||||||||||||
| 250 | '-' => 10, | ||||||||||||||||||||||||||||
| 251 | '*' => 20, | ||||||||||||||||||||||||||||
| 252 | '/' => 20, | ||||||||||||||||||||||||||||
| 253 | '%' => 20, | ||||||||||||||||||||||||||||
| 254 | ); | ||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | 67 | ▶ | sub _parse_expression { | ||||||||||||||||||||||||||
|
Subroutine
_parse_expression: called CC=11 SCAR=25.0
| |||||||||||||||||||||||||||||
| 257 | 67 | my ($state, $min_bp) = @_; | |||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | 67 | ▶ | my $token = _next($state) or _parse_error(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 260 | 67 | my $lhs = _nud($state, $token); | |||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | 60 | while (1) { | |||||||||||||||||||||||||||
| 263 | 78 | my $next = _peek($state); | |||||||||||||||||||||||||||
| 264 | 78 | ▶ | last unless $next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | 78 | ▶ | if ($next->{type} eq 'LPAREN' && _is_callable($lhs)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$next->{"type"} eq "LPAREN" and _is_callable($lhs)
MC/DC50%1/2
$next->{"type"} eq "LPAREN" and _is_callable($lhs)
$next->{"type"} eq "LPAREN"
_is_callable($lhs)
Truth table66%2/3
A and B
A= $next->{"type"} eq "LPAREN"B= _is_callable($lhs)
| |||||||||||||||||||||||||||||
| 267 | 1 | _next($state); # consume '(' | |||||||||||||||||||||||||||
| 268 | 1 | my @args; | |||||||||||||||||||||||||||
| 269 | 1 | ▶ | if (_peek($state)->{type} ne 'RPAREN') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 270 | 1 | push @args, _parse_expression($state, 0); | |||||||||||||||||||||||||||
| 271 | 1 | ▶ | while (_peek($state)->{type} eq 'COMMA') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 272 | 0 | _next($state); # consume ',' | |||||||||||||||||||||||||||
| 273 | 0 | push @args, _parse_expression($state, 0); | |||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||
| 275 | } | ||||||||||||||||||||||||||||
| 276 | 1 | my $closing = _next($state); | |||||||||||||||||||||||||||
| 277 | 1 | ▶ | _parse_error() unless $closing->{type} eq 'RPAREN'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 278 | $lhs = { | ||||||||||||||||||||||||||||
| 279 | type => 'CALL', | ||||||||||||||||||||||||||||
| 280 | name => $lhs->{name}, | ||||||||||||||||||||||||||||
| 281 | 1 | args => \@args, | |||||||||||||||||||||||||||
| 282 | }; | ||||||||||||||||||||||||||||
| 283 | 1 | next; | |||||||||||||||||||||||||||
| 284 | } | ||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||
| 286 | 77 | ▶ | last unless $next->{type} eq 'OP'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 287 | 24 | my $op = $next->{value}; | |||||||||||||||||||||||||||
| 288 | 24 | ▶ | my $lbp = $LBP{$op} || 0; | ||||||||||||||||||||||||||
|
Condition50%1/2
$LBP{$op} || 0
MC/DC0%0/1
$LBP{$op} || 0
$LBP{$op}
Truth table50%1/2
A
A= $LBP{$op}
| |||||||||||||||||||||||||||||
| 289 | 24 | ▶ | last if $lbp < $min_bp; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | 24 | _next($state); # consume operator | |||||||||||||||||||||||||||
| 292 | 24 | my $rhs = _parse_expression($state, $lbp + 1); | |||||||||||||||||||||||||||
| 293 | 17 | $lhs = { | |||||||||||||||||||||||||||
| 294 | type => 'BINARY', | ||||||||||||||||||||||||||||
| 295 | op => $op, | ||||||||||||||||||||||||||||
| 296 | left => $lhs, | ||||||||||||||||||||||||||||
| 297 | right => $rhs, | ||||||||||||||||||||||||||||
| 298 | }; | ||||||||||||||||||||||||||||
| 299 | } | ||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||
| 301 | 53 | return $lhs; | |||||||||||||||||||||||||||
| 302 | } | ||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | 67 | ▶ | sub _nud { | ||||||||||||||||||||||||||
|
Subroutine
_nud: called CC=13 SCAR=26.9
| |||||||||||||||||||||||||||||
| 305 | 67 | my ($state, $token) = @_; | |||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | 67 | ▶ | if ($token->{type} eq 'NUMBER') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 308 | 14 | return { type => 'NUMBER', value => $token->{value} }; | |||||||||||||||||||||||||||
| 309 | } | ||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | 53 | ▶ | if ($token->{type} eq 'STRING') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 312 | 1 | return { type => 'STRING', value => $token->{value} }; | |||||||||||||||||||||||||||
| 313 | } | ||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | 52 | ▶ | if ($token->{type} eq 'CURRENT') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 316 | 2 | return { type => 'CURRENT' }; | |||||||||||||||||||||||||||
| 317 | } | ||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||
| 319 | 50 | ▶ | if ($token->{type} eq 'PATH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 320 | 24 | return { type => 'PATH', value => $token->{value} }; | |||||||||||||||||||||||||||
| 321 | } | ||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | 26 | ▶ | if ($token->{type} eq 'IDENT') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 324 | 18 | ▶ | if ($token->{value} eq 'true') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 325 | 0 | return { type => 'BOOLEAN', value => JSON::PP::true }; | |||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||
| 327 | 18 | ▶ | if ($token->{value} eq 'false') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 328 | 0 | return { type => 'BOOLEAN', value => JSON::PP::false }; | |||||||||||||||||||||||||||
| 329 | } | ||||||||||||||||||||||||||||
| 330 | 18 | ▶ | if ($token->{value} eq 'null') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 331 | 0 | return { type => 'NULL', value => undef }; | |||||||||||||||||||||||||||
| 332 | } | ||||||||||||||||||||||||||||
| 333 | 18 | return { type => 'IDENT', name => $token->{value} }; | |||||||||||||||||||||||||||
| 334 | } | ||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||
| 336 | 8 | ▶ | if ($token->{type} eq 'OP' && $token->{value} eq '-') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$token->{"type"} eq "OP" and $token->{"value"} eq "-"
MC/DC0%0/2
$token->{"type"} eq "OP" and $token->{"value"} eq "-"
$token->{"type"} eq "OP"
$token->{"value"} eq "-"
Truth table66%2/3
A and B
A= $token->{"type"} eq "OP"B= $token->{"value"} eq "-"
| |||||||||||||||||||||||||||||
| 337 | 0 | my $rhs = _parse_expression($state, $LBP{'-'} + 1); | |||||||||||||||||||||||||||
| 338 | 0 | return { type => 'UNARY', op => '-', expr => $rhs }; | |||||||||||||||||||||||||||
| 339 | } | ||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | 8 | ▶ | if ($token->{type} eq 'LPAREN') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 342 | 1 | my $expr = _parse_expression($state, 0); | |||||||||||||||||||||||||||
| 343 | 1 | my $closing = _next($state); | |||||||||||||||||||||||||||
| 344 | 1 | ▶ | _parse_error() unless $closing->{type} eq 'RPAREN'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 345 | 1 | return $expr; | |||||||||||||||||||||||||||
| 346 | } | ||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||
| 348 | 7 | _parse_error(); | |||||||||||||||||||||||||||
| 349 | } | ||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | 1 | ▶ | sub _is_callable { | ||||||||||||||||||||||||||
|
Subroutine
_is_callable: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 352 | 1 | my ($node) = @_; | |||||||||||||||||||||||||||
| 353 | 1 | return $node->{type} eq 'IDENT'; | |||||||||||||||||||||||||||
| 354 | } | ||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | 114 | ▶ | sub _peek { | ||||||||||||||||||||||||||
|
Subroutine
_peek: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||
| 357 | 114 | my ($state) = @_; | |||||||||||||||||||||||||||
| 358 | 114 | ▶ | return $state->{tokens}[ $state->{pos} ] // { type => 'EOF', value => undef }; | ||||||||||||||||||||||||||
|
Condition50%1/2
$state->{"tokens"}[$state->{"pos"}] // {"type", "EOF", "value", undef}
MC/DC0%0/1
$state->{"tokens"}[$state->{"pos"}] // {"type", "EOF", "value", undef}
$state->{"tokens"}[$state->{"pos"}]
Truth table50%1/2
A
A= $state->{"tokens"}[$state->{"pos"}]
| |||||||||||||||||||||||||||||
| 359 | } | ||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||
| 361 | 94 | ▶ | sub _next { | ||||||||||||||||||||||||||
|
Subroutine
_next: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 362 | 94 | my ($state) = @_; | |||||||||||||||||||||||||||
| 363 | 94 | return $state->{tokens}[ $state->{pos}++ ]; | |||||||||||||||||||||||||||
| 364 | } | ||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||
| 366 | 24 | ▶ | sub _parse_error { | ||||||||||||||||||||||||||
|
Subroutine
_parse_error: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 367 | 24 | die $PARSE_ERROR; | |||||||||||||||||||||||||||
| 368 | } | ||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||
| 370 | 69 | ▶ | sub _eval_node { | ||||||||||||||||||||||||||
|
Subroutine
_eval_node: called CC=18 SCAR=31.3
| |||||||||||||||||||||||||||||
| 371 | 69 | my ($node, $opts) = @_; | |||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||
| 373 | 69 | ▶ | if ($node->{type} eq 'NUMBER') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 374 | 14 | return $node->{value}; | |||||||||||||||||||||||||||
| 375 | } | ||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||
| 377 | 55 | ▶ | if ($node->{type} eq 'STRING') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 378 | 1 | return $node->{value}; | |||||||||||||||||||||||||||
| 379 | } | ||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||
| 381 | 54 | ▶ | if ($node->{type} eq 'BOOLEAN') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 382 | 0 | return $node->{value}; | |||||||||||||||||||||||||||
| 383 | } | ||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||
| 385 | 54 | ▶ | if ($node->{type} eq 'NULL') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 386 | 0 | return undef; | |||||||||||||||||||||||||||
| 387 | } | ||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||
| 389 | 54 | ▶ | if ($node->{type} eq 'CURRENT') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 390 | 1 | return $opts->{context}; | |||||||||||||||||||||||||||
| 391 | } | ||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||
| 393 | 53 | ▶ | if ($node->{type} eq 'PATH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 394 | 18 | return $opts->{resolve_path}->($opts->{context}, $node->{value}); | |||||||||||||||||||||||||||
| 395 | } | ||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||
| 397 | 35 | ▶ | if ($node->{type} eq 'UNARY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 398 | 0 | my $value = _eval_node($node->{expr}, $opts); | |||||||||||||||||||||||||||
| 399 | 0 | my $num = $opts->{coerce_number}->($value, 'unary - operand'); | |||||||||||||||||||||||||||
| 400 | 0 | return -$num; | |||||||||||||||||||||||||||
| 401 | } | ||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||
| 403 | 35 | ▶ | if ($node->{type} eq 'BINARY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 404 | 17 | my $left_value = _eval_node($node->{left}, $opts); | |||||||||||||||||||||||||||
| 405 | 17 | my $right_value = _eval_node($node->{right}, $opts); | |||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||
| 407 | 17 | my $left_num = $opts->{coerce_number}->($left_value, 'left operand'); | |||||||||||||||||||||||||||
| 408 | 17 | my $right_num = $opts->{coerce_number}->($right_value, 'right operand'); | |||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||
| 410 | 17 | ▶ | if ($node->{op} eq '+') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 411 | 3 | return $left_num + $right_num; | |||||||||||||||||||||||||||
| 412 | } | ||||||||||||||||||||||||||||
| 413 | 14 | ▶ | if ($node->{op} eq '-') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 414 | 0 | return $left_num - $right_num; | |||||||||||||||||||||||||||
| 415 | } | ||||||||||||||||||||||||||||
| 416 | 14 | ▶ | if ($node->{op} eq '*') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 417 | 3 | return $left_num * $right_num; | |||||||||||||||||||||||||||
| 418 | } | ||||||||||||||||||||||||||||
| 419 | 11 | ▶ | if ($node->{op} eq '/') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 420 | 11 | ▶ | die 'Division by zero' if $right_num == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 421 | 3 | return $left_num / $right_num; | |||||||||||||||||||||||||||
| 422 | } | ||||||||||||||||||||||||||||
| 423 | 0 | ▶ | if ($node->{op} eq '%') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 424 | 0 | ▶ | die 'Modulo by zero' if $right_num == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 425 | 0 | return $left_num % $right_num; | |||||||||||||||||||||||||||
| 426 | } | ||||||||||||||||||||||||||||
| 427 | } | ||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||
| 429 | 18 | ▶ | if ($node->{type} eq 'CALL') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 430 | 1 | my $name = $node->{name}; | |||||||||||||||||||||||||||
| 431 | 1 | my $func = $opts->{builtins}{$name}; | |||||||||||||||||||||||||||
| 432 | 1 | ▶ | _parse_error() unless $func; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 433 | 1 | my @args = map { _eval_node($_, $opts) } @{ $node->{args} }; | |||||||||||||||||||||||||||
| 434 | 1 | return $func->(@args); | |||||||||||||||||||||||||||
| 435 | } | ||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||
| 437 | 17 | _parse_error(); | |||||||||||||||||||||||||||
| 438 | } | ||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||
| 440 | 0 | ▶ | sub _default_coerce_number { | ||||||||||||||||||||||||||
|
Subroutine
_default_coerce_number: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||
| 441 | 0 | my ($value, $label) = @_; | |||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||
| 443 | 0 | ▶ | $label ||= 'value'; | ||||||||||||||||||||||||||
|
Condition0%0/2
$label ||= "value"
MC/DC0%0/1
$label ||= "value"
$label
Truth table0%0/2
A
A= $label
| |||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||
| 445 | 0 | ▶ | die "$label must be a number" unless defined $value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||
| 447 | 0 | ▶ | if (ref($value) eq 'JSON::PP::Boolean') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 448 | 0 | ▶ | return $value ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 449 | } | ||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||
| 451 | 0 | ▶ | die "$label must be a number" if ref $value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 452 | 0 | ▶ | die "$label must be a number" unless looks_like_number($value); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||
| 454 | 0 | return 0 + $value; | |||||||||||||||||||||||||||
| 455 | } | ||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||
| 457 | 182 | 1; | |||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||