Jmespath/Parser.pm

filter: Statementstmt 94.8% 334 / 352 Branchbran 94.3% 83 / 88 Conditioncond 88.8% 8 / 9 MC/DCmcdc 83.3% 5 / 6 Subroutinesub 97.1% 67 / 69 Podpod 0.0% 0 / 3 totaltotal 94.3% 497 / 527 CC 48 SCAR 9.2
CC
48
Cov
95%
_parse_slice_expression
6.0
_parse_projection_rhs
5.0
_expression
5.0
CRAP 2.5
« Jmespath.pm ↑ Summary Jmespath/GraphvizVisitor.pm »
1 package Jmespath::Parser;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Jmespath;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Jmespath::Lexer;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Jmespath::Ast;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Jmespath::Visitor;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Jmespath::ParsedResult;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Jmespath::IncompleteExpressionException;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use List::Util qw(any);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 my $BINDING_POWER = { 'eof' => 0,
14 'unquoted_identifier' => 0,
15 'quoted_identifier' => 0,
16 'literal' => 0,
17 'rbracket' => 0,
18 'rparen' => 0,
19 'comma' => 0,
20 'rbrace' => 0,
21 'number' => 0,
22 'current' => 0,
23 'expref' => 0,
24 'colon' => 0,
25 'pipe' => 1,
26 'or' => 2,
27 'and' => 3,
28 'eq' => 5,
29 'gt' => 5,
30 'lt' => 5,
31 'gte' => 5,
32 'lte' => 5,
33 'ne' => 5,
34 'flatten' => 9,
35 # Everything above stops a projection.
36 'star' => 20,
37 'filter' => 21,
38 'dot' => 40,
39 'not' => 45,
40 'lbrace' => 50,
41 'lbracket' => 55,
42 'lparen' => 60 };
43  
44 # The maximum binding power for a token that can stop
45 # a projection.
46 2 my $PROJECTION_STOP = 10;
47 # The _MAX_SIZE most recent expressions are cached in
48 # _CACHE hash.
49 2 my $CACHE = {};
50 2 my $MAX_SIZE = 128;
51  
52 862 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod undocumented
53 862 my ( $class, $lookahead ) = @_;
54 862 my $self = bless {}, $class;
55 862 ▶ $lookahead = 2 if not defined $lookahead;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $lookahead 862 0 862
56 862 $self->{ tokenizer } = undef;
57 862 $self->{ tokens } = [ undef ] * $lookahead;
58 862 $self->{ buffer_size } = $lookahead;
59 862 $self->{ index } = 0;
60 862 return $self;
61 }
62  
63 861 ▶sub parse {
Subroutine parse: called CC=3 SCAR=11.3
Pod undocumented
64 861 my ($self, $expression) = @_;
65  
66 861 my $cached = $self->{_CACHE}->{$expression};
67 861 ▶ return $cached if defined $cached;
Branch: 1 branch
BranchTrueFalseTotal
if defined $cached 0 861 861
68  
69 861 my $parsed_result = $self->_do_parse($expression);
70  
71 760 $self->{_CACHE}->{expression} = $parsed_result;
72 760 ▶ if (scalar keys %{$self->{_CACHE}} > $MAX_SIZE) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %{$$self{"_CACHE"};} > $MAX_SIZE) 0 760 760
73 0 $self->_free_cache_entries;
74 }
75 760 return $parsed_result;
76 }
77  
78 861 ▶sub _do_parse {
Subroutine _do_parse: called CC=1 SCAR=0.0
79 861 my ( $self, $expression ) = @_;
80 861 my $parsed;
81 861 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
82 861 $parsed = $self->_parse($expression);
83 101 ▶ } catch {
Subroutine __ANON__: called CC=4 SCAR=14.0
84 101 ▶ if ($_->isa('Jmespath::LexerException')) {
Branch: 3 branches
BranchTrueFalseTotal
if ($_->isa('Jmespath::LexerException')) { } 10 91 101
elsif ($_->isa('Jmespath::IncompleteExpressionException')) { } 11 80 91
elsif ($_->isa('Jmespath::ParseException')) { } 80 0 80
85 10 $_->expression($self->{_expression});
86 10 $_->throw;
87 }
88 elsif ($_->isa('Jmespath::IncompleteExpressionException')) {
89 11 $_->expression($self->{_expression});
90 11 $_->throw;
91 }
92 elsif ($_->isa('Jmespath::ParseException')) {
93 80 $_->expression($self->{_expression});
94 80 $_->throw;
95 }
96 else {
97 0 $_->throw;
98 }
99 861 };
100 760 return $parsed;
101 }
102  
103 861 ▶sub _parse {
Subroutine _parse: called CC=2 SCAR=6.9
104 861 my ( $self, $expression ) = @_;
105 861 $self->{_expression} = $expression;
106 861 $self->{_index} = 0;
107 861 $self->{_tokens} = Jmespath::Lexer->new->tokenize($expression);
108  
109 851 my $parsed = $self->_expression(0); #binding_power = 0
110 764 ▶ if ($self->_current_token_type ne 'eof') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_current_token_type ne "eof") 4 760 764
111 4 my $t = $self->_lookahead_token(0);
112 return Jmespath::ParseException->new(lex_position => $t->{start},
113 token_value => $t->{value},
114 token_type => $t->{type},
115 4 message => "Unexpected token: " . $t->{value})->throw;
116 }
117  
118 760 return Jmespath::ParsedResult->new($expression, $parsed);
119 }
120  
121  
122 1899 ▶sub _expression {
Subroutine _expression: called CC=5 SCAR=16.1
123 1899 my ( $self, $binding_power ) = @_;
124 1899 ▶ $binding_power = defined $binding_power ? $binding_power : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $binding_power ? : 1565 334 1899
125  
126 # Get the current token under evaluation
127 1899 my $left_token = $self->_lookahead_token(0);
128  
129 # Advance the token index
130 1899 $self->_advance;
131  
132 1899 my $nud_function = \&{'_token_nud_' . $left_token->{type}};
133 1899 ▶ if ( not defined &$nud_function ) { $self->_error_nud_token($left_token); }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined &$nud_function) 27 1872 1899
134  
135 1872 my $left_ast = &$nud_function($self, $left_token);
136 1852 my $current_token = $self->_current_token_type;
137  
138 1852 ▶ while ( $binding_power < $BINDING_POWER->{$current_token} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($binding_power < $BINDING_POWER->{$current_token}) 991 1779 2770
139 991 my $led = \&{'_token_led_' . $current_token};
140  
141 991 ▶ if (not defined &$led) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined &$led) { } 10 981 991
142 10 $self->_error_led_token($self->_lookahead_token(0));
143 }
144 else {
145 981 $self->_advance;
146 981 $left_ast = &$led($self, $left_ast);
147 918 $current_token = $self->_current_token_type;
148 }
149 }
150 1779 return $left_ast;
151 }
152  
153 226 ▶sub _token_nud_literal {
Subroutine _token_nud_literal: called CC=1 SCAR=0.0
154 226 my ($self, $token) = @_;
155 226 return Jmespath::Ast->literal($token->{value});
156 }
157  
158 1356 ▶sub _token_nud_unquoted_identifier {
Subroutine _token_nud_unquoted_identifier: called CC=1 SCAR=0.0
159 1356 my ($self, $token) = @_;
160 1356 return Jmespath::Ast->field($token->{value});
161 }
162  
163 97 ▶sub _token_nud_quoted_identifier {
Subroutine _token_nud_quoted_identifier: called CC=2 SCAR=6.9
164 97 my ($self, $token) = @_;
165  
166 97 my $field = Jmespath::Ast->field($token->{value});
167  
168 # You can't have a quoted identifier as a function name.
169 97 ▶ if ( $self->_current_token_type eq 'lparen' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_current_token_type eq "lparen") 2 95 97
170 2 my $t = $self->_lookahead_token(0);
171 Jmespath::ParseException
172 ->new( lex_position => 0,
173 token_value => $t->{value},
174 token_type => $t->{type},
175 2 message => 'Quoted identifier not allowed for function names.')
176 ->throw;
177 }
178 95 return $field;
179 }
180  
181 36 ▶sub _token_nud_star {
Subroutine _token_nud_star: called CC=2 SCAR=6.9
182 36 my ($self, $token) = @_;
183 36 my $left = Jmespath::Ast->identity;
184 36 my $right;
185 36 ▶ if ( $self->_current_token_type eq 'rbracket' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_current_token_type eq 'rbracket') { } 3 33 36
186 3 $right = Jmespath::Ast->identity;
187 }
188 else {
189 33 $right = $self->_parse_projection_rhs( $BINDING_POWER->{ star } );
190 }
191 35 return Jmespath::Ast->value_projection($left, $right);
192 }
193  
194 3 ▶sub _token_nud_filter {
Subroutine _token_nud_filter: called CC=1 SCAR=0.0
195 3 my ($self, $token) = @_;
196 3 return $self->_token_led_filter(Jmespath::Ast->identity);
197 }
198  
199 12 ▶sub _token_nud_lbrace {
Subroutine _token_nud_lbrace: called CC=1 SCAR=0.0
200 12 my ($self, $token) = @_;
201 12 return $self->_parse_multi_select_hash;
202 }
203  
204 16 ▶sub _token_nud_lparen {
Subroutine _token_nud_lparen: called CC=1 SCAR=0.0
205 16 my ($self, $token) = @_;
206 16 my $expression = $self->_expression;
207 12 $self->_match('rparen');
208 11 return $expression;
209 }
210  
211 2 ▶sub _token_nud_flatten {
Subroutine _token_nud_flatten: called CC=1 SCAR=0.0
212 2 my ($self, $token) = @_;
213 2 my $left = Jmespath::Ast->flatten(Jmespath::Ast->identity);
214 2 my $right = $self->_parse_projection_rhs( $BINDING_POWER->{ flatten } );
215 2 return Jmespath::Ast->projection($left, $right);
216 }
217  
218 18 ▶sub _token_nud_not {
Subroutine _token_nud_not: called CC=1 SCAR=0.0
219 18 my ($self, $token) = @_;
220 18 my $expr = $self->_expression( $BINDING_POWER->{ not } );
221 14 return Jmespath::Ast->not_expression($expr);
222 }
223  
224 57 ▶sub _token_nud_lbracket {
Subroutine _token_nud_lbracket: called CC=4 SCAR=13.9
225 57 my ($self, $token) = @_;
226 57 ▶ if (any { $_ eq $self->_current_token_type } qw(number colon)) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 2 branches
BranchTrueFalseTotal
if (&any(sub { $_ eq $self->_current_token_type; } , 'number', 'colon')) { } 42 15 57
elsif ($self->_current_token_type eq 'star' and $self->_lookahead(1) eq 'rbracket') { } 10 5 15
227 42 my $right = $self->_parse_index_expression;
228 38 return $self->_project_if_slice(Jmespath::Ast->identity, $right);
229 }
230 ▶ elsif ($self->_current_token_type eq 'star' and
Condition100%3/3
$self->_current_token_type eq "star" and $self->_lookahead(1) eq "rbracket"
!ll&&!rl&&r
4110
MC/DC100%2/2
$self->_current_token_type eq "star" and $self->_lookahead(1) eq "rbracket"
$self->_current_token_type eq "star" $self->_lookahead(1) eq "rbracket"
Truth table100%3/3
A and B
A= $self->_current_token_type eq "star"B= $self->_lookahead(1) eq "rbracket"
ABresult
0X0
100
111
231 $self->_lookahead(1) eq 'rbracket') {
232 10 $self->_advance;
233 10 $self->_advance;
234 10 my $right = $self->_parse_projection_rhs( $BINDING_POWER->{ star } );
235 10 return Jmespath::Ast->projection(Jmespath::Ast->identity, $right);
236 }
237 else {
238 5 return $self->_parse_multi_select_list;
239 }
240 }
241  
242 158 ▶sub _parse_index_expression {
Subroutine _parse_index_expression: called CC=3 SCAR=11.0
243 158 my ($self) = @_;
244 # We're here:
245 # [<current>
246 # ^
247 # | current token
248 158 ▶ if ($self->_lookahead(0) eq 'colon' or
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_lookahead(0) eq 'colon' or $self->_lookahead(1) eq 'colon') { } 45 113 158
Condition100%3/3
$self->_lookahead(0) eq "colon" or $self->_lookahead(1) eq "colon"
l!l&&r!l&&!r
2223113
MC/DC100%2/2
$self->_lookahead(0) eq "colon" or $self->_lookahead(1) eq "colon"
$self->_lookahead(0) eq "colon" $self->_lookahead(1) eq "colon"
Truth table100%3/3
A or B
A= $self->_lookahead(0) eq "colon"B= $self->_lookahead(1) eq "colon"
ABresult
1X1
011
000
249 $self->_lookahead(1) eq 'colon') {
250 45 return $self->_parse_slice_expression;
251 }
252 else {
253 #parse the syntax [number]
254 113 my $node = Jmespath::Ast->index_of($self->_lookahead_token(0)->{value});
255 113 $self->_advance;
256 113 $self->_match('rbracket');
257 109 return $node;
258 }
259 }
260  
261 45 ▶sub _parse_slice_expression {
Subroutine _parse_slice_expression: called CC=6 SCAR=17.9
262 45 my ($self) = @_;
263 # [start:end:step]
264 # Where start, end, and step are optional.
265 # The last colon is optional as well.
266 45 my @parts = (undef, undef, undef);
267 45 my $index = 0;
268 45 my $current_token = $self->_current_token_type;
269 45 ▶ while ($current_token ne 'rbracket' and $index < 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_token ne "rbracket" and $index < 3) 143 38 181
Condition66%2/3
$current_token ne "rbracket" and $index < 3
!ll&&!rl&&r
380143
MC/DC50%1/2
$current_token ne "rbracket" and $index < 3
$current_token ne "rbracket" $index < 3
Truth table66%2/3
A and B
A= $current_token ne "rbracket"B= $index < 3
ABresult
0X0
100
111
270 143 ▶ if ($current_token eq 'colon') {
Branch: 2 branches
BranchTrueFalseTotal
if ($current_token eq 'colon') { } 70 73 143
elsif ($current_token eq 'number') { } 68 5 73
271 70 $index += 1;
272 70 ▶ if ( $index == 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index == 3) 2 68 70
273 2 $self->_raise_parse_error_for_token($self->_lookahead_token(0),
274 'syntax error');
275 }
276 68 $self->_advance;
277 }
278 elsif ($current_token eq 'number') {
279 68 $parts[$index] = $self->_lookahead_token(0)->{value};
280 68 $self->_advance;
281 }
282 else {
283 5 $self->_raise_parse_error_for_token( $self->_lookahead_token(0),
284 'syntax error');
285 0 $current_token = $self->_current_token_type;
286 }
287 136 $current_token = $self->_current_token_type;
288 }
289 38 $self->_match('rbracket');
290 38 return Jmespath::Ast->slice(@parts);
291 }
292  
293 20 ▶sub _token_nud_current {
Subroutine _token_nud_current: called CC=1 SCAR=0.0
294 20 my ($self, $token) = @_;
295 20 return Jmespath::Ast->current_node;
296 }
297  
298 29 ▶sub _token_nud_expref {
Subroutine _token_nud_expref: called CC=1 SCAR=0.0
299 29 my ($self, $token) = @_;
300 29 my $expression = $self->_expression( $BINDING_POWER->{ expref } );
301 28 return Jmespath::Ast->expref($expression);
302 }
303  
304 221 ▶sub _token_led_dot {
Subroutine _token_led_dot: called CC=3 SCAR=11.0
305 221 my ($self, $left) = @_;
306  
307 221 ▶ if ($self->_current_token_type ne 'star') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_current_token_type ne "star") 201 20 221
308  
309 # Begin the evaluation of the subexpression.
310 201 my $right = $self->_parse_dot_rhs( $BINDING_POWER->{ dot } );
311  
312 174 ▶ if ($left->{type} eq 'subexpression') {
Branch: 1 branch
BranchTrueFalseTotal
if ($left->{"type"} eq "subexpression") 21 153 174
313 21 push @{$left->{children}}, $right;
314 21 return $left;
315 }
316  
317 # We have identified a subexpression, but the current AST is not a
318 # subexpression. Convert to a subexpression here.
319 153 return Jmespath::Ast->subexpression([$left, $right]);
320 }
321 20 $self->_advance;
322 20 my $right = $self->_parse_projection_rhs( $BINDING_POWER->{ dot } );
323 20 return Jmespath::Ast->value_projection($left, $right);
324 }
325  
326 22 ▶sub _token_led_pipe {
Subroutine _token_led_pipe: called CC=1 SCAR=0.0
327 22 my ($self, $left) = @_;
328 22 my $right = $self->_expression( $BINDING_POWER->{ pipe } );
329 22 return Jmespath::Ast->pipe_oper($left, $right);
330 }
331  
332 51 ▶sub _token_led_or {
Subroutine _token_led_or: called CC=1 SCAR=0.0
333 51 my ($self, $left) = @_;
334 51 my $right = $self->_expression( $BINDING_POWER->{ or } );
335 48 return Jmespath::Ast->or_expression($left, $right);
336 }
337  
338 30 ▶sub _token_led_and {
Subroutine _token_led_and: called CC=1 SCAR=0.0
339 30 my ($self, $left) = @_;
340 30 my $right = $self->_expression( $BINDING_POWER->{ and } );
341 30 return Jmespath::Ast->and_expression($left, $right);
342 }
343  
344 178 ▶sub _token_led_lparen {
Subroutine _token_led_lparen: called CC=4 SCAR=13.9
345 178 my ($self, $left) = @_;
346 178 ▶ if ( $left->{type} ne 'field' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($left->{"type"} ne "field") 1 177 178
347 # 0 - first func arg or closing paren.
348 # -1 - '(' token
349 # -2 - invalid function "name".
350 1 my $prev_t = $self->_lookahead_token(-2);
351 1 my $message = "Invalid function name '" . $prev_t->{value} ."'";
352 Jmespath::ParseException
353 ->new( lex_position => $prev_t->{start},
354 token_value => $prev_t->{value},
355 token_type => $prev_t->{type},
356 1 message => $message)
357 ->throw;
358 }
359 177 my $name = $left->{value};
360 177 my $args = [];
361 177 ▶ while (not $self->_current_token_type eq 'rparen') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_current_token_type eq "rparen") 252 177 429
362 252 my $expression = $self->_expression;
363 252 ▶ if ( $self->_current_token_type eq 'comma') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_current_token_type eq "comma") 77 175 252
364 77 $self->_match('comma');
365 }
366 252 push @$args, $expression;
367 }
368 177 $self->_match('rparen');
369 177 return Jmespath::Ast->function_expression($name, $args);
370 }
371  
372 86 ▶sub _token_led_filter {
Subroutine _token_led_filter: called CC=2 SCAR=6.9
373 86 my ($self, $left) = @_;
374 86 my $right;
375 86 my $condition = $self->_expression(0);
376 82 $self->_match('rbracket');
377 81 ▶ if ( $self->_current_token_type eq 'flatten' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_current_token_type eq 'flatten') { } 1 80 81
378 1 $right = Jmespath::Ast->identity;
379 }
380 else {
381 80 $right = $self->_parse_projection_rhs( $BINDING_POWER->{ filter } );
382 }
383  
384 81 return Jmespath::Ast->filter_projection($left, $right, $condition);
385 }
386  
387 82 ▶sub _token_led_eq {
Subroutine _token_led_eq: called CC=1 SCAR=0.0
388 82 my ($self, $left) = @_;
389 82 return $self->_parse_comparator($left, 'eq');
390 }
391  
392 16 ▶sub _token_led_ne {
Subroutine _token_led_ne: called CC=1 SCAR=0.0
393 16 my ($self, $left) = @_;
394 16 return $self->_parse_comparator($left, 'ne');
395 }
396  
397 8 ▶sub _token_led_gt {
Subroutine _token_led_gt: called CC=1 SCAR=0.0
398 8 my ($self, $left) = @_;
399 8 return $self->_parse_comparator($left, 'gt');
400 }
401  
402 2 ▶sub _token_led_gte {
Subroutine _token_led_gte: called CC=1 SCAR=0.0
403 2 my ($self, $left) = @_;
404 2 return $self->_parse_comparator($left, 'gte');
405 }
406  
407 11 ▶sub _token_led_lt {
Subroutine _token_led_lt: called CC=1 SCAR=0.0
408 11 my ($self, $left) = @_;
409 11 return $self->_parse_comparator($left, 'lt');
410 }
411  
412 2 ▶sub _token_led_lte {
Subroutine _token_led_lte: called CC=1 SCAR=0.0
413 2 my ($self, $left) = @_;
414 2 return $self->_parse_comparator($left, 'lte');
415 }
416  
417 94 ▶sub _token_led_flatten {
Subroutine _token_led_flatten: called CC=1 SCAR=0.0
418 94 my ($self, $left) = @_;
419 94 $left = Jmespath::Ast->flatten($left);
420 94 my $right = $self->_parse_projection_rhs( $BINDING_POWER->{ flatten } );
421 90 return Jmespath::Ast->projection($left, $right);
422 }
423  
424 181 ▶sub _token_led_lbracket {
Subroutine _token_led_lbracket: called CC=3 SCAR=11.0
425 181 my ($self, $left) = @_;
426 181 my $token = $self->_lookahead_token(0);
427 181 ▶ if ( any { $_ eq $token->{type}} qw(number colon) ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { $_ eq $token->{'type'}; } , 'number', 'colon')) { } 116 65 181
428 116 my $right = $self->_parse_index_expression();
429 109 ▶ if ($left->{type} eq 'index_expression') {
Branch: 1 branch
BranchTrueFalseTotal
if ($left->{"type"} eq "index_expression") 17 92 109
430 17 push @{$left->{children}}, $right;
431 17 return $left;
432 }
433 92 return $self->_project_if_slice($left, $right);
434 }
435 else {
436 65 $self->_match('star');
437 59 $self->_match('rbracket');
438 58 my $right = $self->_parse_projection_rhs( $BINDING_POWER->{ star } );
439 51 return Jmespath::Ast->projection($left, $right);
440 }
441 }
442  
443 130 ▶sub _project_if_slice {
Subroutine _project_if_slice: called CC=2 SCAR=6.9
444 130 my ($self, $left, $right) = @_;
445 130 my $index_expr = Jmespath::Ast->index_expression([$left, $right]);
446 130 ▶ if ( $right->{type} eq 'slice' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($right->{"type"} eq "slice") 38 92 130
447 return Jmespath::Ast->projection( $index_expr,
448 38 $self->_parse_projection_rhs($BINDING_POWER->{star}));
449 }
450  
451 92 return $index_expr;
452 }
453  
454 121 ▶sub _parse_comparator {
Subroutine _parse_comparator: called CC=1 SCAR=0.0
455 121 my ($self, $left, $comparator) = @_;
456 121 my $right = $self->_expression( $BINDING_POWER->{ $comparator } );
457 119 return Jmespath::Ast->comparator($comparator, $left, $right);
458 }
459  
460 40 ▶sub _parse_multi_select_list {
Subroutine _parse_multi_select_list: called CC=1 SCAR=0.0
461 40 my ($self) = @_;
462 40 my $expressions = [];
463 40 my $result;
464 40 ▶ try {
Subroutine __ANON__: called CC=2 SCAR=6.9
465 40 while (1) {
466 66 my $expression = $self->_expression;
467 58 push @$expressions, $expression;
468 58 ▶ last if ($self->_current_token_type eq 'rbracket');
Branch: 1 branch
BranchTrueFalseTotal
if $self->_current_token_type eq "rbracket" 31 27 58
469 27 $self->_match('comma');
470 }
471 31 $self->_match('rbracket');
472 31 $result = Jmespath::Ast->multi_select_list($expressions);
473 9 ▶ } catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
474 9 $_->throw;
475 40 };
476 31 return $result;
477 }
478  
479  
480 42 ▶sub _parse_multi_select_hash {
Subroutine _parse_multi_select_hash: called CC=3 SCAR=11.0
481 42 my ($self) = @_;
482 42 my @pairs;
483 42 while (1) {
484 72 my $key_token = $self->_lookahead_token(0);
485 # Before getting the token value, verify it's
486 # an identifier.
487 72 $self->_match_multiple_tokens( [ 'quoted_identifier', 'unquoted_identifier' ]);
488 67 my $key_name = $key_token->{ value };
489 67 $self->_match('colon');
490 65 my $value = $self->_expression(0);
491 64 my $node = Jmespath::Ast->key_val_pair( $key_name,
492 $value );
493 64 push @pairs, $node;
494 64 ▶ if ( $self->_current_token_type eq 'comma' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_current_token_type eq 'comma') { } 29 35 64
elsif ($self->_current_token_type eq 'rbrace') { } 34 1 35
495 29 $self->_match('comma');
496 }
497 elsif ( $self->_current_token_type eq 'rbrace' ) {
498 34 $self->_match('rbrace');
499 34 last;
500 }
501 }
502 34 return Jmespath::Ast->multi_select_hash(\@pairs);
503 }
504  
505 335 ▶sub _parse_projection_rhs {
Subroutine _parse_projection_rhs: called CC=5 SCAR=16.1
506 335 my ($self, $binding_power) = @_;
507 335 ▶ if ( $BINDING_POWER->{ $self->_current_token_type } < $PROJECTION_STOP) {
Branch: 4 branches
BranchTrueFalseTotal
if ($$BINDING_POWER{$self->_current_token_type} < $PROJECTION_STOP) { } 160 175 335
elsif ($self->_current_token_type eq 'lbracket') { } 18 157 175
elsif ($self->_current_token_type eq 'filter') { } 1 156 157
elsif ($self->_current_token_type eq 'dot') { } 155 1 156
508 160 return Jmespath::Ast->identity();
509 }
510 elsif ($self->_current_token_type eq 'lbracket') {
511 18 return $self->_expression( $binding_power );
512 }
513 elsif ($self->_current_token_type eq 'filter') {
514 1 return $self->_expression( $binding_power );
515 }
516 elsif ($self->_current_token_type eq 'dot') {
517 155 $self->_match('dot');
518 155 return $self->_parse_dot_rhs($binding_power);
519 }
520  
521 1 $self->_raise_parse_error_for_token($self->_lookahead_token(0),
522 'syntax error');
523 0 return;
524 }
525  
526 356 ▶sub _parse_dot_rhs {
Subroutine _parse_dot_rhs: called CC=4 SCAR=13.9
527 356 my ($self, $binding_power) = @_;
528 # From the grammar:
529 # expression '.' ( identifier /
530 # multi-select-list /
531 # multi-select-hash /
532 # function-expression /
533 # *
534 # In terms of tokens that means that after a '.',
535 # you can have:
536 # my $lookahead = $self->_current_token_type;
537  
538 # What token do we have next in the index
539 356 my $lookahead = $self->_current_token_type;
540  
541 # Common case "foo.bar", so first check for an identifier.
542 356 ▶ if ( any { $_ eq $lookahead } qw(quoted_identifier unquoted_identifier star) ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 3 branches
BranchTrueFalseTotal
if (&any(sub { $_ eq $lookahead; } , 'quoted_identifier', 'unquoted_identifier', 'star')) { } 273 83 356
elsif ($lookahead eq 'lbracket') { } 35 48 83
elsif ($lookahead eq 'lbrace') { } 30 18 48
543 273 return $self->_expression( $binding_power );
544 }
545 elsif ( $lookahead eq 'lbracket' ) {
546 35 $self->_match('lbracket');
547 35 return $self->_parse_multi_select_list;
548 }
549 elsif ( $lookahead eq 'lbrace' ) {
550 30 $self->_match('lbrace');
551 30 return $self->_parse_multi_select_hash;
552 }
553  
554 18 my $t = $self->_lookahead_token(0);
555 18 my @allowed = qw(quoted_identifier unquoted_identified lbracket lbrace);
556 18 my $msg = 'Expecting: ' . join(' ', @allowed) . ', got: ' . $t->{ type };
557 18 $self->_raise_parse_error_for_token( $t, $msg )->throw;
558 0 return;
559 }
560  
561 27 ▶sub _error_nud_token {
Subroutine _error_nud_token: called CC=2 SCAR=7.0
562 27 my ($self, $token) = @_;
563 27 ▶ if ( $token->{type} eq 'eof' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->{"type"} eq "eof") 5 22 27
564 Jmespath::IncompleteExpressionException->new( lex_expression => $token->{ start },
565 token_value => $token->{ value },
566 5 token_type => $token->{ type } )->throw;
567 }
568 22 $self->_raise_parse_error_for_token($token, 'invalid token');
569 0 return;
570 }
571  
572 10 ▶sub _error_led_token {
Subroutine _error_led_token: called CC=1 SCAR=0.4
573 10 my ($self, $token) = @_;
574 10 $self->_raise_parse_error_for_token($token, 'invalid token');
575 0 return;
576 }
577  
578 1031 ▶sub _match {
Subroutine _match: called CC=2 SCAR=6.9
579 1031 my ($self, $token_type) = @_;
580 1031 ▶ if ($self->_current_token_type eq $token_type ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_current_token_type eq $token_type) { } 1015 16 1031
581 1015 $self->_advance();
582 }
583 else {
584 16 $self->_raise_parse_error_maybe_eof( $token_type,
585 $self->_lookahead_token(0) )->throw;
586 }
587 1015 return;
588 }
589  
590 72 ▶sub _match_multiple_tokens {
Subroutine _match_multiple_tokens: called CC=2 SCAR=6.9
591 72 my ( $self, $token_types ) = @_;
592  
593 72 ▶ if ( not any { $_ eq $self->_current_token_type } @$token_types ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
unless (&any(sub { $_ eq $self->_current_token_type; } , @$token_types)) 5 67 72
594 5 $self->_raise_parse_error_maybe_eof( $token_types,
595 $self->_lookahead_token(0) );
596 }
597  
598 67 $self->_advance();
599 67 return;
600 }
601  
602 4251 ▶sub _advance {
Subroutine _advance: called CC=1 SCAR=0.0
603 4251 my ($self) = @_;
604 4251 $self->{ _index } += 1;
605 4251 return;
606 }
607  
608 7422 ▶sub _current_token_type {
Subroutine _current_token_type: called CC=1 SCAR=0.0
609 7422 my ($self) = @_;
610 7422 return @{ $self->{ _tokens } }[ $self->{_index} ]->{type};
611 }
612  
613 # _lookahead
614 #
615 # retrieve the type of the token at position current + n.
616 305 ▶sub _lookahead {
Subroutine _lookahead: called CC=2 SCAR=7.0
617 305 my ($self, $number) = @_;
618 305 ▶ $number = defined $number ? $number : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $number ? : 305 0 305
619 305 return @{ $self->{ _tokens } }[ $self->{_index} + $number ]->{type};
620 }
621  
622 2397 ▶sub _lookahead_token {
Subroutine _lookahead_token: called CC=1 SCAR=0.0
623 2397 my ($self, $number) = @_;
624  
625 2397 my $lookahead = @{$self->{ _tokens }}[ $self->{_index} + $number ];
626  
627 2397 return $lookahead;
628 }
629  
630 58 ▶sub _raise_parse_error_for_token {
Subroutine _raise_parse_error_for_token: called CC=1 SCAR=0.0
631 58 my ($self, $token, $reason) = @_;
632 58 my $lex_position = $token->{ start };
633 58 my $actual_value = $token->{ value };
634 58 my $actual_type = $token->{ type };
635  
636 58 Jmespath::ParseException->new( lex_position => $lex_position,
637 token_value => $actual_value,
638 token_type => $actual_type,
639 message => $reason )->throw;
640 0 return;
641 }
642  
643 21 ▶sub _raise_parse_error_maybe_eof {
Subroutine _raise_parse_error_maybe_eof: called CC=2 SCAR=6.9
644 21 my ($self, $expected_type, $token) = @_;
645 21 my $lex_position = $token->{ start };
646 21 my $actual_value = $token->{ value };
647 21 my $actual_type = $token->{ type };
648  
649 21 ▶ if ( $actual_type eq 'eof' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($actual_type eq "eof") 6 15 21
650 6 Jmespath::IncompleteExpressionException
651 ->new( lex_position => $lex_position,
652 token_value => $actual_value,
653 token_type => $actual_type )
654 ->throw;
655 }
656  
657 15 my $message = "Expecting: $expected_type, got: $actual_type";
658  
659 15 Jmespath::ParseException
660 ->new( lex_position => $lex_position,
661 token_value => $actual_value,
662 token_type => $actual_type,
663 message => $message )
664 ->throw;
665 0 return;
666 }
667  
668 0 ▶sub _free_cache_entries {
Subroutine _free_cache_entries: not called CC=1 SCAR=6.9
669 0 my ($self) = @_;
670 0 my $key = $self->{_CACHE}{(keys %{$self->{_CACHE}})[rand keys %{$self->{_CACHE}}]};
671 0 delete $self->{ _CACHE }->{ $key };
672 0 return;
673 }
674  
675 0 ▶sub purge {
Subroutine purge: not called CC=1 SCAR=6.9
Pod undocumented
676 0 my ($self, $cls) = @_;
677 0 $cls->_CACHE->clear();
678 0 return;
679 }
680  
681 2 1;
« Jmespath.pm ↑ Summary Jmespath/GraphvizVisitor.pm »