Path/Tokenizer.pm

filter: Statementstmt 100% 82 / 82 Branchbran 95.2% 40 / 42 Conditioncond 80.9% 17 / 21 MC/DCmcdc 71.4% 10 / 14 Subroutinesub 100% 11 / 11 Podpod 0.0% 0 / 1 totaltotal 93.5% 160 / 171 CC 29 SCAR 24.0
CC
29
Cov
96%
_read_to_next_token
18.0
_read_to_filter_script_close
9.1
tokenize
4.0
CRAP 11.0
« Path/Evaluator.pm ↑ Summary Path.pm »
1 package JSON::Path::Tokenizer;
2 20 $JSON::Path::Tokenizer::VERSION = '1.0.2';
3 20 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 20 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 20 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 20 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 20 ▶use JSON::Path::Constants qw(:symbols :operators);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 20 ▶use Exporter::Shiny 'tokenize';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 20 Readonly my $ESCAPE_CHAR => qq{\\};
12 20 Readonly my %OPERATORS => (
13 $TOKEN_ROOT => 1, # $
14 $TOKEN_RECURSIVE => 1, # ..
15 $TOKEN_CHILD => 1, # .
16 $TOKEN_FILTER_OPEN => 1, # [?(
17 $TOKEN_FILTER_SCRIPT_CLOSE => 1, # )]
18 $TOKEN_SCRIPT_OPEN => 1, # [(
19 $TOKEN_SUBSCRIPT_OPEN => 1, # [
20 $TOKEN_SUBSCRIPT_CLOSE => 1, # ]
21 $TOKEN_QUOTE => 1, # "
22 );
23  
24 # my $invocation = 0;
25  
26 # ABSTRACT: Helper class for JSON::Path::Evaluator. Do not call directly.
27  
28 # Take an expression and break it up into tokens
29 sub tokenize {
30 264 ▶ my $expression = shift;
Subroutine tokenize: called CC=4 SCAR=13.9
Pod undocumented
31 #print "Tokenize \"$expression\"\n";
32 264 my $chars = [ split //, $expression ];
33  
34 264 my @tokens;
35 264 ▶ while ( defined( my $token = _read_to_next_token($chars) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $token = _read_to_next_token($chars))) 1528 264 1792
36  
37 # print "$invocation: Got token: $token\n";
38 1528 push @tokens, $token;
39 1528 ▶ 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) 53 1475 1528
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
1521475
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
40  
41 # print "$invocation: script/filter open: $token\n";
42 53 push @tokens, _read_to_filter_script_close($chars);
43 }
44 }
45 264 return @tokens;
46 }
47  
48 sub _read_to_filter_script_close {
49 53 ▶ my $chars = shift;
Subroutine _read_to_filter_script_close: called CC=9 SCAR=22.0
50  
51 #print "$invocation: read to filter script close: " . join( '', @{$chars} ) . "\n";
52 53 my $filter;
53 my $in_quote;
54 53 ▶ while ( defined( my $char = shift @{$chars} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $char = shift @{$chars;})) 1446 0 1446
55 1446 $filter .= $char;
56 1446 ▶ 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) 46 1400 1446
Condition100%3/3
$char eq $JSON::Path::Tokenizer::APOSTROPHE or $char eq $JSON::Path::Tokenizer::QUOTATION_MARK
l!l&&r!l&&!r
2441400
MC/DC100%2/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 table100%3/3
A or B
A= $char eq $JSON::Path::Tokenizer::APOSTROPHEB= $char eq $JSON::Path::Tokenizer::QUOTATION_MARK
ABresult
1X1
011
000
57 46 ▶ if ( $in_quote && $in_quote eq $char ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_quote and $in_quote eq $char) { } 23 23 46
Condition66%2/3
$in_quote and $in_quote eq $char
!ll&&!rl&&r
23023
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
58 23 $in_quote = '';
59 }
60 else {
61 23 $in_quote = $char;
62 }
63 }
64  
65 1446 ▶ last unless @{$chars};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$chars;} 0 1446 1446
66 1446 ▶ last if $chars->[0] eq $RIGHT_PARENTHESIS && !$in_quote;
Branch: 1 branch
BranchTrueFalseTotal
if $chars->[0] eq $JSON::Path::Tokenizer::RIGHT_PARENTHESIS and not $in_quote 53 1393 1446
Condition100%3/3
$chars->[0] eq $JSON::Path::Tokenizer::RIGHT_PARENTHESIS and not $in_quote
!ll&&!rl&&r
1392153
MC/DC100%2/2
$chars->[0] eq $JSON::Path::Tokenizer::RIGHT_PARENTHESIS and not $in_quote
$chars->[0] eq $JSON::Path::Tokenizer::RIGHT_PARENTHESIS not $in_quote
Truth table100%3/3
A and not(B)
A= $chars->[0] eq $JSON::Path::Tokenizer::RIGHT_PARENTHESISB= not $in_quote
ABresult
0X0
100
111
67 }
68 53 return $filter;
69 }
70  
71 sub _read_to_next_token {
72 #$invocation++;
73 1792 ▶ my $chars = shift;
Subroutine _read_to_next_token: called CC=18 SCAR=28.9
74  
75 #print "$invocation: Get next token: " . join( '', @{$chars} ) . "\n";
76 1792 my $in_quote = '';
77 1792 my $token;
78 1792 ▶ while ( defined( my $char = shift @{$chars} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $char = shift @{$chars;})) 3700 264 3964
79  
80 3700 ▶ 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 3652 3700
Condition66%2/3
$char eq $JSON::Path::Tokenizer::APOSTROPHE or $char eq $JSON::Path::Tokenizer::QUOTATION_MARK
l!l&&r!l&&!r
4803652
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
81 #print "$invocation: Char is $APOSTROPHE or $QUOTATION_MARK. Char: $char, in_quote: $in_quote\n";
82 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
83 24 $in_quote = '';
84 24 last;
85 }
86 24 $in_quote = $char;
87  
88 #print "$invocation: Set \$in_quote to $in_quote\n";
89 24 next;
90 }
91  
92 3652 ▶ if ( $char eq $ESCAPE_CHAR && !$in_quote ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($char eq $ESCAPE_CHAR and not $in_quote) 2 3650 3652
Condition66%2/3
$char eq $ESCAPE_CHAR and not $in_quote
!ll&&!rl&&r
365002
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
93  
94 #print "$invocation: Got escape char: $char\n";
95 2 $token .= shift @{$chars};
96 2 next;
97 }
98  
99 #print "Append '$char' to token '$token'\n";
100 3650 $token .= $char;
101 3650 ▶ next if $in_quote;
Branch: 1 branch
BranchTrueFalseTotal
if $in_quote 101 3549 3650
102  
103 # Break out of the loop if the current character is the last one in the stream.
104 3549 ▶ last unless @{$chars};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$chars;} 244 3305 3549
105  
106 3305 ▶ if ( $char eq $LEFT_SQUARE_BRACKET )
Branch: 3 branches
BranchTrueFalseTotal
if ($char eq $JSON::Path::Tokenizer::LEFT_SQUARE_BRACKET) { } 163 3142 3305
elsif ($char eq $JSON::Path::Tokenizer::RIGHT_PARENTHESIS) { } 53 3089 3142
elsif ($char eq $JSON::Path::Tokenizer::FULL_STOP) { } 418 2671 3089
107 { # distinguish between '[', '[(', and '[?('
108 163 ▶ if ( $chars->[0] eq $LEFT_PARENTHESIS ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chars->[0] eq $JSON::Path::Tokenizer::LEFT_PARENTHESIS) 1 162 163
109 1 next;
110 }
111 162 ▶ if ( $chars->[0] eq $QUESTION_MARK ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chars->[0] eq $JSON::Path::Tokenizer::QUESTION_MARK) 52 110 162
112  
113 # The below appends the '?'. The '(' will be appended in the next iteration of the loop
114 52 $token .= shift @{$chars};
115 52 next;
116 }
117 }
118 elsif ( $char eq $RIGHT_PARENTHESIS ) {
119  
120 #print "$invocation: Found right parenthesis\n";
121  
122 # A right parenthesis should be followed by a right square bracket, which itself is a token.
123 # Append the next character and proceed.
124 53 $token .= shift @{$chars};
125  
126 #print "$invocation: Token is now: $token\n";
127 }
128 elsif ( $char eq $FULL_STOP ) {
129  
130 # A full stop (i.e. a period, '.') may be the child operator '.' or the recursive operator '..'
131 418 ▶ $token .= shift @{$chars} if $chars->[0] eq $FULL_STOP;
Branch: 1 branch
BranchTrueFalseTotal
if $chars->[0] eq $JSON::Path::Tokenizer::FULL_STOP 17 401 418
132 }
133  
134 # If we've assembled an operator, we're done.
135 3252 ▶ last if $OPERATORS{$token};
Branch: 1 branch
BranchTrueFalseTotal
if $OPERATORS{$token} 859 2393 3252
136  
137 # Similarly, if the next character is an operator, we're done
138 2393 ▶ last if $OPERATORS{ $chars->[0] };
Branch: 1 branch
BranchTrueFalseTotal
if $OPERATORS{$chars->[0]} 401 1992 2393
139 }
140 20 ▶ no warnings qw/uninitialized/;
Subroutine BEGIN: called CC=1 SCAR=0.0
141  
142 #print "$invocation: Token: $token\n";
143 20 ▶ use warnings qw/uninitialized/;
Subroutine BEGIN: called CC=1 SCAR=0.0
144 1792 return $token;
145 }
146  
147 20 1;
148  
149 __END__
« Path/Evaluator.pm ↑ Summary Path.pm »