Jmespath/Lexer.pm

filter: Statementstmt 98.3% 181 / 184 Branchbran 96.7% 60 / 62 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 96.8% 31 / 32 Podpod 0.0% 0 / 3 totaltotal 96.8% 277 / 286 CC 33 SCAR 23.1
CC
33
Cov
98%
tokenize
24.0
_consume_until
4.0
_has_identifier
3.0
CRAP 10.1
« Jmespath/Functions.pm ↑ Summary Jmespath/TreeInterpreter.pm »
1 package Jmespath::Lexer;
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Jmespath::LexerException;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Jmespath::EmptyExpressionException;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use String::Util qw(trim);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use List::Util qw(any);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use feature 'unicode_strings';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 our $START_IDENTIFIER = ['A'..'Z','a'..'z','_'];
14 3 our $VALID_IDENTIFIER = ['A'..'Z','a'..'z',0..9,'_'];
15 3 our $VALID_NUMBER = [0..9];
16 3 our $WHITESPACE = [' ', "\t", "\n", "\r"];
17 3 our $SIMPLE_TOKENS = { '.' => 'dot',
18 '*' => "star",
19 ']' => 'rbracket',
20 ',' => 'comma',
21 ':' => 'colon',
22 '@' => 'current',
23 '(' => 'lparen',
24 ')' => 'rparen',
25 '{' => 'lbrace',
26 '}' => 'rbrace', };
27 3 our $BACKSLASH = "\\";
28  
29 862 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
30 862 my ( $class ) = @_;
31 862 my $self = bless {}, $class;
32 862 $self->{STACK} = [];
33 862 return $self;
34 }
35  
36 0 ▶sub stack {
Subroutine stack: not called CC=1 SCAR=6.9
Pod undocumented
37 0 my $self = shift;
38 0 return $self->{STACK};
39 }
40  
41 880 ▶sub tokenize {
Subroutine tokenize: called CC=24 SCAR=31.8
Pod undocumented
42 880 my ( $self, $expression ) = @_;
43 880 ▶ Jmespath::EmptyExpressionError->new->throw
Branch: 1 branch
BranchTrueFalseTotal
unless defined $expression 0 880 880
44 if not defined $expression;
45 880 $self->{STACK} = [];
46 880 $self->{_position} = 0;
47 880 $self->{_expression} = $expression;
48 880 @{$self->{_chars}} = split //, $expression;
49 880 $self->{_current} = @{$self->{_chars}}[$self->{_position}];
50 880 $self->{_length} = length $expression;
51  
52 880 ▶ while (defined $self->{_current}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"_current"}) 5058 866 5924
53 5058 ▶ if ( any { $_ eq $self->{_current} } keys %$SIMPLE_TOKENS ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 15 branches
BranchTrueFalseTotal
if (&any(sub { $_ eq $self->{'_current'}; } , keys %$Jmespath::Lexer::SIMPLE_TOKENS)) { } 1678 3380 5058
elsif (&any(sub { $_ eq $self->{'_current'}; } , @$Jmespath::Lexer::START_IDENTIFIER)) { } 1465 1915 3380
elsif (&any(sub { $_ eq $self->{'_current'}; } , @$Jmespath::Lexer::WHITESPACE)) { } 597 1318 1915
elsif ($self->{'_current'} eq '[') { } 465 853 1318
elsif ($self->{'_current'} eq q[']) { } 61 792 853
elsif ($self->{'_current'} eq '|') { } 78 714 792
elsif ($self->{'_current'} eq '&') { } 60 654 714
elsif ($self->{'_current'} eq '`') { } 186 468 654
elsif (&any(sub { $_ eq $self->{'_current'}; } , @$Jmespath::Lexer::VALID_NUMBER)) { } 177 291 468
elsif ($self->{'_current'} eq '-') { } 24 267 291
elsif ($self->{'_current'} eq '"') { } 116 151 267
elsif ($self->{'_current'} eq '<') { } 13 138 151
elsif ($self->{'_current'} eq '>') { } 11 127 138
elsif ($self->{'_current'} eq '!') { } 34 93 127
elsif ($self->{'_current'} eq '=') { } 87 6 93
54 1678 push @{$self->{STACK}},
55 { type => $SIMPLE_TOKENS->{ $self->{_current} },
56 value => $self->{_current},
57 start => $self->{_position},
58 1678 end => $self->{_position} + 1 };
59 1678 $self->_next;
60 }
61  
62 149859 ▶ elsif ( any { $_ eq $self->{_current} } @$START_IDENTIFIER ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
63 1465 my $start = $self->{_position};
64 1465 my $buff = $self->{_current};
65 1465 my $next;
66 1465 ▶ while ( $self->_has_identifier( $self->_next, $VALID_IDENTIFIER )) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_has_identifier($self->_next, $Jmespath::Lexer::VALID_IDENTIFIER)) 4487 1465 5952
67 4487 $buff .= $self->{_current};
68 }
69 1465 push @{$self->{STACK}},
70 { type => 'unquoted_identifier',
71 value => $buff,
72 start => $start,
73 end => $start + length $buff };
74 }
75  
76 5875 ▶ elsif ( any { $_ eq $self->{_current} } @$WHITESPACE ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
77 597 $self->_next;
78 }
79  
80 elsif ( $self->{_current} eq q/[/ ) {
81 465 my $start = $self->{_position};
82 465 my $next_char = $self->_next;
83 # use Data::Dumper;
84 # print Dumper $next_char;
85 # if not defined $next_char;
86 465 ▶ if (not defined $next_char) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $next_char) { } 3 462 465
elsif ($next_char eq ']') { } 96 366 462
elsif ($next_char eq '?') { } 89 277 366
87 Jmespath::LexerException->new( lexer_position => $start,
88 lexer_value => $self->{_current},
89 3 message => "Unexpected end of expression" )->throw;
90 }
91 elsif ( $next_char eq q/]/ ) {
92 96 $self->_next;
93 96 push @{$self->{STACK}},
94 { type => 'flatten',
95 value => '[]',
96 start => $start,
97 end => $start + 2 };
98 }
99 elsif ( $next_char eq q/?/ ) {
100 89 $self->_next;
101 89 push @{$self->{STACK}},
102 { type => 'filter',
103 value => '[?',
104 start => $start,
105 end => $start + 2 };
106 }
107  
108 else {
109 277 push @{$self->{STACK}},
110 { type => 'lbracket',
111 value => '[',
112 start => $start,
113 end => $start + 1 };
114 }
115 }
116  
117 elsif ( $self->{_current} eq q/'/ ) {
118 61 push @{$self->{STACK}},
119 $self->_consume_raw_string_literal;
120 }
121  
122 elsif ( $self->{_current} eq q/|/ ) {
123 78 push @{$self->{STACK}},
124 $self->_match_or_else('|', 'or', 'pipe');
125 }
126  
127 elsif ( $self->{_current} eq q/&/ ) {
128 60 push @{$self->{STACK}},
129 $self->_match_or_else('&', 'and', 'expref');
130 }
131  
132 elsif ( $self->{_current} eq q/`/ ) {
133 186 push @{$self->{STACK}},
134 $self->_consume_literal;
135 }
136  
137 3282 ▶ elsif ( any {$_ eq $self->{_current} } @$VALID_NUMBER ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
138 177 my $start = $self->{_position};
139 177 my $buff = $self->_consume_number;
140 177 push @{$self->{STACK}},
141 { type => 'number',
142 value => $buff,
143 start => $start,
144 end => $start + length $buff };
145 }
146  
147 # negative number
148 elsif ( $self->{_current} eq q/-/ ) {
149 24 my $start = $self->{_position};
150 24 my $buff = $self->_consume_number;
151 24 ▶ if (length $buff > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $buff > 1) { } 23 1 24
152 23 push @{$self->{STACK}},
153 { type => 'number',
154 value => $buff,
155 start => $start,
156 end => $start + length $buff };
157 }
158 else {
159 1 Jmespath::LexerException->new( lexer_position => $start,
160 lexer_value => $buff,
161 message => "Unknown token '$buff'" )->throw;
162 }
163 }
164  
165 elsif ( $self->{_current} eq q/"/ ) {
166 116 push @{$self->{STACK}}, $self->_consume_quoted_identifier;
167 }
168  
169 elsif ( $self->{_current} eq q/</ ) {
170 13 push @{$self->{STACK}}, $self->_match_or_else('=', 'lte', 'lt');
171 }
172  
173 elsif ( $self->{_current} eq q/>/ ) {
174 11 push @{$self->{STACK}}, $self->_match_or_else('=', 'gte', 'gt');
175 }
176  
177 elsif ( $self->{_current} eq q/!/ ) {
178 34 push @{$self->{STACK}}, $self->_match_or_else('=', 'ne', 'not');
179 }
180  
181 elsif ( $self->{_current} eq q/=/ ) {
182 87 ▶ if ($self->_next eq '=') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_next eq '=') { } 87 0 87
183 87 push @{$self->{STACK}},
184 { type => 'eq',
185 value => '==',
186 start => $self->{_position} - 1,
187 87 end => $self->{_position} };
188 87 $self->_next;
189 }
190 else {
191 0 Jmespath::LexerException->new( lexer_position => $self->{_position} - 1,
192 lexer_value => '=',
193 message => 'Unknown token =' )->throw;
194 }
195 }
196 else {
197 Jmespath::LexerException->new( lexer_position => $self->{_position},
198 lexer_value => $self->{_current},
199 6 message => 'Unknown token ' . $self->{_current})->throw;
200 }
201 }
202 866 push @{$self->{STACK}},
203 { type => 'eof',
204 value => '',
205 start => $self->{_length},
206 866 end => $self->{_length} };
207 866 return $self->{STACK};
208 }
209  
210 201 ▶sub _consume_number {
Subroutine _consume_number: called CC=2 SCAR=6.9
211 201 my ($self) = @_;
212 201 my $start = $self->{_position};
213 201 my $buff = $self->{_current};
214 201 ▶ while ( $self->_has_identifier( $self->_next, $VALID_NUMBER)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_has_identifier($self->_next, $Jmespath::Lexer::VALID_NUMBER)) 42 201 243
215 42 $buff .= $self->{_current};
216 }
217 201 return $buff;
218 }
219  
220 6195 ▶sub _has_identifier {
Subroutine _has_identifier: called CC=3 SCAR=11.0
221 6195 my ($self, $value, $identifier) = @_;
222 6195 ▶ return 0 if not defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 213 5982 6195
223 5982 ▶ return 1 if any { $_ eq $value } @$identifier;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if &any(sub { $_ eq $value; } , @$identifier) 4529 1453 5982
224 1453 return 0;
225 }
226  
227 11770 ▶sub _next {
Subroutine _next: called CC=2 SCAR=6.9
228 11770 my ($self) = @_;
229 11770 ▶ if ( $self->{_position} == $self->{_length} - 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_position'} == $self->{'_length'} - 1) { } 872 10898 11770
230 872 $self->{_current} = undef;
231 }
232 else {
233 10898 $self->{_position} += 1;
234 10898 $self->{_current} = @{$self->{_chars}}[$self->{_position}];
235 }
236 11770 return $self->{_current};
237 }
238  
239 363 ▶sub _consume_until {
Subroutine _consume_until: called CC=4 SCAR=13.9
240 363 my ($self, $delimiter) = @_;
241 363 my $start = $self->{_position};
242 363 my $buff = '';
243 363 $self->_next;
244 363 ▶ while ($self->{_current} ne $delimiter ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_current"} ne $delimiter) 1340 362 1702
245 1340 ▶ if ($self->{_current} eq $BACKSLASH) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_current"} eq $Jmespath::Lexer::BACKSLASH) 110 1230 1340
246 110 $buff .= $BACKSLASH;
247 # Advance to escaped character.
248 110 $self->_next;
249 }
250 1340 $buff .= $self->{_current};
251 1340 $self->_next;
252 1340 ▶ if (not defined $self->{_current}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"_current"}) 1 1339 1340
253 Jmespath::LexerException
254 ->new( lexer_position => $start,
255 lexer_value => $self->{_expression},
256 1 message => "Unclosed delimiter $delimiter" )
257 ->throw;
258 }
259 }
260 362 $self->_next;
261 362 return $buff;
262 }
263  
264 186 ▶sub _consume_literal {
Subroutine _consume_literal: called CC=1 SCAR=0.0
265 186 my ($self) = @_;
266 186 my $start = $self->{_position};
267 186 my $lexeme = $self->_consume_until('`');
268 186 $lexeme =~ s/\\`/`/;
269 186 my $parsed_json;
270 186 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
271 186 $parsed_json = JSON->new->allow_nonref->decode($lexeme);
272 5 ▶ } catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
273 5 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
274 5 $parsed_json = JSON->new->allow_nonref->decode('"' . trim($lexeme) . '"');
275 2 ▶ } catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
276 Jmespath::LexerException->new( lexer_position => $start,
277 lexer_value => $self->{_expression},
278 2 message => "Bad token $lexeme" )->throw;
279 5 };
280 186 };
281  
282 184 my $token_len = $self->{_position} - $start;
283 184 return { type => 'literal',
284 value => $parsed_json,
285 start => $start,
286 end => $token_len, };
287 }
288  
289 116 ▶sub _consume_quoted_identifier {
Subroutine _consume_quoted_identifier: called CC=1 SCAR=0.0
290 116 my ( $self ) = @_;
291 116 my $start = $self->{_position};
292 116 my $lexeme = '"' . $self->_consume_until('"') . '"';
293 115 my $error = "error consuming quoted identifier";
294 115 my $decoded_lexeme;
295  
296 115 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
297 115 $decoded_lexeme = JSON->new->allow_nonref->decode($lexeme);
298 1 ▶ } catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
299 1 Jmespath::LexerException->new( lexer_position => $start,
300 expression => $lexeme,
301 message => $error )->throw;
302 115 };
303 return { type => 'quoted_identifier',
304 value => $decoded_lexeme,
305 start => $start,
306 114 end => $self->{ _position } - $start,
307 };
308 }
309  
310 61 ▶sub _consume_raw_string_literal {
Subroutine _consume_raw_string_literal: called CC=1 SCAR=0.0
311 61 my ( $self ) = @_;
312 61 my $start = $self->{ _position };
313 61 my $lexeme = $self->_consume_until("'"); $lexeme =~ s/\\\'/\'/g;
314 61 my $token_len = $self->{_position} - $start;
315 61 return { 'type' => 'literal',
316 'value' => $lexeme,
317 'start' => $start,
318 'end' => $token_len,
319 };
320 }
321  
322  
323 196 ▶sub _match_or_else {
Subroutine _match_or_else: called CC=3 SCAR=11.0
324 196 my ( $self, $expected, $match_type, $else_type) = @_;
325 196 my $start = $self->{_position};
326 196 my $current = $self->{_current};
327 196 my $next_char = $self->_next;
328 196 ▶ if ( not defined $next_char or
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $next_char or $next_char ne $expected) 91 105 196
Condition100%3/3
not defined $next_char or $next_char ne $expected
l!l&&r!l&&!r
388105
MC/DC100%2/2
not defined $next_char or $next_char ne $expected
not defined $next_char $next_char ne $expected
Truth table100%3/3
not(A) or B
A= not defined $next_charB= $next_char ne $expected
ABresult
1X1
011
000
329 $next_char ne $expected ) {
330 91 return { 'type' => $else_type,
331 'value' => $current,
332 'start' => $start,
333 'end' => $start,
334 };
335 }
336  
337 105 $self->_next();
338 105 return { 'type' => $match_type,
339 'value' => $current . $next_char,
340 'start' => $start,
341 'end' => $start + 1,
342 };
343 }
344  
345  
346 3 1;
« Jmespath/Functions.pm ↑ Summary Jmespath/TreeInterpreter.pm »