Path/Tokenizer.pm

filter: Statementstmt 100% 73 / 73 Branchbran 94.7% 36 / 38 Conditioncond 75.0% 9 / 12 MC/DCmcdc 62.5% 5 / 8 Subroutinesub 100% 10 / 10 Podpod 0.0% 0 / 1 totaltotal 93.6% 133 / 142 CC 24 SCAR 24.0
CC
24
Cov
96%
_read_to_next_token
18.0
_read_to_filter_script_close
4.0
tokenize
4.0
CRAP 11.0
« Path/Evaluator.pm ↑ Summary Path.pm »
1 package JSON::Path::Tokenizer;
2 18 $JSON::Path::Tokenizer::VERSION = '0.51';
3 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 18 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 18 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 18 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 18 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 18 ▶use JSON::Path::Constants qw(:symbols :operators);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 18 ▶use Exporter::Shiny 'tokenize';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 18 Readonly my $ESCAPE_CHAR => qq{\\};
13 18 Readonly my %OPERATORS => (
14 $TOKEN_ROOT => 1, # $
15 $TOKEN_RECURSIVE => 1, # ..
16 $TOKEN_CHILD => 1, # .
17 $TOKEN_FILTER_OPEN => 1, # [?(
18 $TOKEN_FILTER_SCRIPT_CLOSE => 1, # )]
19 $TOKEN_SCRIPT_OPEN => 1, # [(
20 $TOKEN_SUBSCRIPT_OPEN => 1, # [
21 $TOKEN_SUBSCRIPT_CLOSE => 1, # ]
22 );
23  
24 # ABSTRACT: Helper class for JSON::Path::Evaluator. Do not call directly.
25  
26 # Take an expression and break it up into tokens
27 sub tokenize {
28 220 ▶ my $expression = shift;
Subroutine tokenize: called CC=4 SCAR=13.9
Pod undocumented
29  
30 220 my $chars = [ split //, $expression ];
31  
32 220 my @tokens;
33 220 ▶ while ( defined( my $token = _read_to_next_token($chars) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $token = _read_to_next_token($chars))) 1181 220 1401
34 1181 push @tokens, $token;
35 1181 ▶ if ( $token eq $TOKEN_SCRIPT_OPEN || $token eq $TOKEN_FILTER_OPEN ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token eq $JSON::Path::Tokenizer::TOKEN_SCRIPT_OPEN or $token eq $JSON::Path::Tokenizer::TOKEN_FILTER_OPEN) 40 1141 1181
Condition100%3/3
$token eq $JSON::Path::Tokenizer::TOKEN_SCRIPT_OPEN or $token eq $JSON::Path::Tokenizer::TOKEN_FILTER_OPEN
l!l&&r!l&&!r
1391141
MC/DC100%2/2
$token eq $JSON::Path::Tokenizer::TOKEN_SCRIPT_OPEN or $token eq $JSON::Path::Tokenizer::TOKEN_FILTER_OPEN
$token eq $JSON::Path::Tokenizer::TOKEN_SCRIPT_OPEN $token eq $JSON::Path::Tokenizer::TOKEN_FILTER_OPEN
Truth table100%3/3
A or B
A= $token eq $JSON::Path::Tokenizer::TOKEN_SCRIPT_OPENB= $token eq $JSON::Path::Tokenizer::TOKEN_FILTER_OPEN
ABresult
1X1
011
000
36 40 push @tokens, _read_to_filter_script_close($chars);
37 }
38 }
39 220 return @tokens;
40 }
41  
42 sub _read_to_filter_script_close {
43 40 ▶ my $chars = shift;
Subroutine _read_to_filter_script_close: called CC=4 SCAR=14.0
44  
45 40 my $filter;
46 40 ▶ while ( defined( my $char = shift @{$chars} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $char = shift @{$chars;})) 1025 0 1025
47 1025 $filter .= $char;
48  
49 1025 ▶ last unless @{$chars};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$chars;} 0 1025 1025
50 1025 ▶ last if $chars->[0] eq $RIGHT_PARENTHESIS;
Branch: 1 branch
BranchTrueFalseTotal
if $chars->[0] eq $JSON::Path::Tokenizer::RIGHT_PARENTHESIS 40 985 1025
51 }
52 40 return $filter;
53 }
54  
55 sub _read_to_next_token {
56 1401 ▶ my $chars = shift;
Subroutine _read_to_next_token: called CC=18 SCAR=28.9
57  
58 1401 my $in_quote;
59 my $token;
60 1401 ▶ while ( defined( my $char = shift @{$chars} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $char = shift @{$chars;})) 2855 220 3075
61 2855 ▶ if ( $char eq $APOSTROPHE || $char eq $QUOTATION_MARK ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($char eq $JSON::Path::Tokenizer::APOSTROPHE or $char eq $JSON::Path::Tokenizer::QUOTATION_MARK) 48 2807 2855
Condition66%2/3
$char eq $JSON::Path::Tokenizer::APOSTROPHE or $char eq $JSON::Path::Tokenizer::QUOTATION_MARK
l!l&&r!l&&!r
4802807
MC/DC50%1/2
$char eq $JSON::Path::Tokenizer::APOSTROPHE or $char eq $JSON::Path::Tokenizer::QUOTATION_MARK
$char eq $JSON::Path::Tokenizer::APOSTROPHE $char eq $JSON::Path::Tokenizer::QUOTATION_MARK
Truth table66%2/3
A or B
A= $char eq $JSON::Path::Tokenizer::APOSTROPHEB= $char eq $JSON::Path::Tokenizer::QUOTATION_MARK
ABresult
1X1
011
000
62 48 ▶ if ( $in_quote && $in_quote eq $char ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_quote and $in_quote eq $char) 24 24 48
Condition66%2/3
$in_quote and $in_quote eq $char
!ll&&!rl&&r
24024
MC/DC50%1/2
$in_quote and $in_quote eq $char
$in_quote $in_quote eq $char
Truth table66%2/3
A and B
A= $in_quoteB= $in_quote eq $char
ABresult
0X0
100
111
63 24 $in_quote = '';
64 24 last;
65 }
66 24 $in_quote = $char;
67 24 next;
68 }
69  
70 2807 ▶ if ( $char eq $ESCAPE_CHAR && !$in_quote ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($char eq $ESCAPE_CHAR and not $in_quote) 2 2805 2807
Condition66%2/3
$char eq $ESCAPE_CHAR and not $in_quote
!ll&&!rl&&r
280502
MC/DC50%1/2
$char eq $ESCAPE_CHAR and not $in_quote
$char eq $ESCAPE_CHAR not $in_quote
Truth table66%2/3
A and not(B)
A= $char eq $ESCAPE_CHARB= not $in_quote
ABresult
0X0
100
111
71 2 $token .= shift @{$chars};
72 2 next;
73 }
74  
75 2805 $token .= $char;
76  
77 2805 ▶ next if $in_quote;
Branch: 1 branch
BranchTrueFalseTotal
if $in_quote 101 2704 2805
78  
79 # Break out of the loop if the current character is the last one in the stream.
80 2704 ▶ last unless @{$chars};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$chars;} 205 2499 2704
81  
82 2499 ▶ if ( $char eq $LEFT_SQUARE_BRACKET ) { # distinguish between '[', '[(', and '[?('
Branch: 3 branches
BranchTrueFalseTotal
if ($char eq $JSON::Path::Tokenizer::LEFT_SQUARE_BRACKET) { } 116 2383 2499
elsif ($char eq $JSON::Path::Tokenizer::RIGHT_PARENTHESIS) { } 40 2343 2383
elsif ($char eq $JSON::Path::Tokenizer::FULL_STOP) { } 330 2013 2343
83 116 ▶ if ( $chars->[0] eq $LEFT_PARENTHESIS ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chars->[0] eq $JSON::Path::Tokenizer::LEFT_PARENTHESIS) 1 115 116
84 1 next;
85 }
86 115 ▶ if ( $chars->[0] eq $QUESTION_MARK ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chars->[0] eq $JSON::Path::Tokenizer::QUESTION_MARK) 39 76 115
87  
88 # The below appends the '?'. The '(' will be appended in the next iteration of the loop
89 39 $token .= shift @{$chars};
90 39 next;
91 }
92 }
93 elsif ( $char eq $RIGHT_PARENTHESIS ) {
94  
95 # A right parenthesis should be followed by a right square bracket, which itself is a token.
96 # Append the next character and proceed.
97 40 $token .= shift @{$chars};
98 }
99 elsif ( $char eq $FULL_STOP ) {
100  
101 # A full stop (i.e. a period, '.') may be the child operator '.' or the recursive operator '..'
102 330 ▶ $token .= shift @{$chars} if $chars->[0] eq $FULL_STOP;
Branch: 1 branch
BranchTrueFalseTotal
if $chars->[0] eq $JSON::Path::Tokenizer::FULL_STOP 17 313 330
103 }
104  
105 # If we've assembled an operator, we're done.
106 2459 ▶ last if $OPERATORS{$token};
Branch: 1 branch
BranchTrueFalseTotal
if $OPERATORS{$token} 664 1795 2459
107  
108 # Similarly, if the next character is an operator, we're done
109 1795 ▶ last if $OPERATORS{ $chars->[0] };
Branch: 1 branch
BranchTrueFalseTotal
if $OPERATORS{$chars->[0]} 288 1507 1795
110 }
111 1401 return $token;
112 }
113  
114 18 1;
115  
116 __END__
« Path/Evaluator.pm ↑ Summary Path.pm »