Path/Tokenizer.pm

filter: Statementstmt 100% 98 / 98 Branchbran 96.5% 56 / 58 Conditioncond 76.1% 16 / 21 MC/DCmcdc 61.5% 8 / 13 Subroutinesub 100% 11 / 11 Podpod 0.0% 0 / 1 totaltotal 93.5% 189 / 202 CC 37 SCAR 26.1
CC
37
Cov
96%
_read_to_next_token
18.0
_read_to_filter_script_close
17.1
tokenize
4.0
CRAP 13.6
« Path/Evaluator.pm ↑ Summary Path.pm »
1 package JSON::Path::Tokenizer;
2 21 $JSON::Path::Tokenizer::VERSION = '1.0.6';
3 21 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 21 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 21 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 21 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 21 ▶use JSON::Path::Constants qw(:symbols :operators);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 21 ▶use Exporter::Shiny 'tokenize';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 21 Readonly my $ESCAPE_CHAR => qq{\\};
12 21 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 304 ▶sub tokenize {
Subroutine tokenize: called CC=4 SCAR=13.9
Pod undocumented
30 304 my $expression = shift;
31 #print "Tokenize \"$expression\"\n";
32 304 my $chars = [ split //, $expression ];
33  
34 304 my @tokens;
35 304 ▶ while ( defined( my $token = _read_to_next_token($chars) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $token = _read_to_next_token($chars))) 1686 304 1990
36  
37 # print "$invocation: Got token: $token\n";
38 1686 push @tokens, $token;
39 1686 ▶ 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) 72 1614 1686
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
1711614
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 72 push @tokens, _read_to_filter_script_close($chars);
43 }
44 }
45 304 return @tokens;
46 }
47  
48 72 ▶sub _read_to_filter_script_close {
Subroutine _read_to_filter_script_close: called CC=17 SCAR=28.4
49 72 my $chars = shift;
50  
51 72 my %escaped_chars = (
52 "b" => "\x{0008}",
53 "f" => "\x{000C}",
54 "n" => "\x{000A}",
55 "r" => "\x{000D}",
56 "t" => "\x{0009}",
57 "v" => "\x{000B}",
58 "0" => "\x{0000}",
59 "'" => "\x{0027}",
60 '"' => "\x{0022}",
61 "\\" => "\x{005C}",
62 );
63  
64 #print "$invocation: read to filter script close: " . join( '', @{$chars} ) . "\n";
65 72 my $filter;
66 my $in_regex;
67 72 my $in_quote;
68 72 my $escape = 0;
69  
70 72 my @quote_chars = ($APOSTROPHE, $QUOTATION_MARK);
71 72 my @regex_chars = "/";
72  
73 72 ▶ while ( defined( my $char = shift @{$chars} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $char = shift @{$chars;})) 1934 0 1934
74 1934 ▶ if ( $in_quote ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($in_quote) { } 265 1669 1934
elsif ($in_regex) { } 225 1444 1669
elsif (grep {$_ eq $char;} @quote_chars) { } 24 1420 1444
elsif (grep {$_ eq $char;} @regex_chars) { } 19 1401 1420
75 265 ▶ if ( $escape ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($escape) { } 3 262 265
elsif ($char eq '\\') { } 3 259 262
elsif ($char eq $in_quote) { } 24 235 259
76 ## Replace \t by tab, \\ by \, etc
77 3 ▶ $char = $escaped_chars{$char} || $char;
Condition33%1/3
$escaped_chars{$char} || $char
l!l&&r!l&&!r
300
MC/DC0%0/2
$escaped_chars{$char} || $char
$escaped_chars{$char} $char
Truth table33%1/3
A || B
A= $escaped_chars{$char}B= $char
ABresult
1X1
011
000
78 3 $escape = 0;
79 }
80 elsif ( $char eq "\\" ) {
81 ## Don't include \ and flag so next char in sequence
82 ## is replaced correctly.
83 3 $escape = 1;
84 3 next;
85 }
86 elsif ( $char eq $in_quote ) {
87 24 $in_quote = '';
88 }
89 }
90 elsif ( $in_regex ) {
91 225 ▶ if ( $escape ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($escape) { } 25 200 225
elsif ($char eq '\\') { } 25 175 200
elsif ($char eq $in_regex) { } 19 156 175
92 25 $escape = 0;
93 }
94 elsif ( $char eq "\\" ) {
95 25 $escape = 1;
96 }
97 elsif ( $char eq $in_regex ) {
98 19 $in_regex = '';
99 }
100 }
101 2888 elsif (grep { $_ eq $char } @quote_chars) {
102 24 $in_quote = $char;
103 }
104 1420 elsif (grep { $_ eq $char } @regex_chars) {
105 19 $in_regex = $char;
106 }
107  
108 1931 $filter .= $char;
109  
110 1931 ▶ last unless @{$chars};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$chars;} 0 1931 1931
111 1931 ▶ last if $chars->[0] eq $RIGHT_PARENTHESIS && !$in_quote && !$in_regex;
Branch: 1 branch
BranchTrueFalseTotal
if $chars->[0] eq $JSON::Path::Tokenizer::RIGHT_PARENTHESIS and not $in_quote and not $in_regex 72 1859 1931
Condition100%6/6
$chars->[0] eq $JSON::Path::Tokenizer::RIGHT_PARENTHESIS and not $in_quote and not $in_regex
!ll&&!rl&&r
1855472
$chars->[0] eq $JSON::Path::Tokenizer::RIGHT_PARENTHESIS and not $in_quote
!ll&&!rl&&r
1853276
MC/DC100%3/3
$chars->[0] eq $JSON::Path::Tokenizer::RIGHT_PARENTHESIS and not $in_quote and not $in_regex
$chars->[0] eq $JSON::Path::Tokenizer::RIGHT_PARENTHESIS not $in_quote not $in_regex
Truth table100%4/4
A and not(B) and not(C)
A= $chars->[0] eq $JSON::Path::Tokenizer::RIGHT_PARENTHESISB= not $in_quoteC= not $in_regex
ABCresult
0XX0
10X0
1100
1111
112 }
113 72 return $filter;
114 }
115  
116 1990 ▶sub _read_to_next_token {
Subroutine _read_to_next_token: called CC=18 SCAR=28.9
117 #$invocation++;
118 1990 my $chars = shift;
119  
120 #print "$invocation: Get next token: " . join( '', @{$chars} ) . "\n";
121 1990 my $in_quote = '';
122 1990 my $token;
123 1990 ▶ while ( defined( my $char = shift @{$chars} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $char = shift @{$chars;})) 4109 304 4413
124  
125 4109 ▶ 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 4061 4109
Condition66%2/3
$char eq $JSON::Path::Tokenizer::APOSTROPHE or $char eq $JSON::Path::Tokenizer::QUOTATION_MARK
l!l&&r!l&&!r
4804061
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
126 #print "$invocation: Char is $APOSTROPHE or $QUOTATION_MARK. Char: $char, in_quote: $in_quote\n";
127 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
128 24 $in_quote = '';
129 24 last;
130 }
131 24 $in_quote = $char;
132  
133 #print "$invocation: Set \$in_quote to $in_quote\n";
134 24 next;
135 }
136  
137 4061 ▶ if ( $char eq $ESCAPE_CHAR && !$in_quote ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($char eq $ESCAPE_CHAR and not $in_quote) 2 4059 4061
Condition66%2/3
$char eq $ESCAPE_CHAR and not $in_quote
!ll&&!rl&&r
405902
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
138  
139 #print "$invocation: Got escape char: $char\n";
140 2 $token .= shift @{$chars};
141 2 next;
142 }
143  
144 #print "Append '$char' to token '$token'\n";
145 4059 $token .= $char;
146 4059 ▶ next if $in_quote;
Branch: 1 branch
BranchTrueFalseTotal
if $in_quote 101 3958 4059
147  
148 # Break out of the loop if the current character is the last one in the stream.
149 3958 ▶ last unless @{$chars};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$chars;} 265 3693 3958
150  
151 3693 ▶ if ( $char eq $LEFT_SQUARE_BRACKET )
Branch: 3 branches
BranchTrueFalseTotal
if ($char eq $JSON::Path::Tokenizer::LEFT_SQUARE_BRACKET) { } 182 3511 3693
elsif ($char eq $JSON::Path::Tokenizer::RIGHT_PARENTHESIS) { } 72 3439 3511
elsif ($char eq $JSON::Path::Tokenizer::FULL_STOP) { } 458 2981 3439
152 { # distinguish between '[', '[(', and '[?('
153 182 ▶ if ( $chars->[0] eq $LEFT_PARENTHESIS ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chars->[0] eq $JSON::Path::Tokenizer::LEFT_PARENTHESIS) 1 181 182
154 1 next;
155 }
156 181 ▶ if ( $chars->[0] eq $QUESTION_MARK ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chars->[0] eq $JSON::Path::Tokenizer::QUESTION_MARK) 71 110 181
157  
158 # The below appends the '?'. The '(' will be appended in the next iteration of the loop
159 71 $token .= shift @{$chars};
160 71 next;
161 }
162 }
163 elsif ( $char eq $RIGHT_PARENTHESIS ) {
164  
165 #print "$invocation: Found right parenthesis\n";
166  
167 # A right parenthesis should be followed by a right square bracket, which itself is a token.
168 # Append the next character and proceed.
169 72 $token .= shift @{$chars};
170  
171 #print "$invocation: Token is now: $token\n";
172 }
173 elsif ( $char eq $FULL_STOP ) {
174  
175 # A full stop (i.e. a period, '.') may be the child operator '.' or the recursive operator '..'
176 458 ▶ $token .= shift @{$chars} if $chars->[0] eq $FULL_STOP;
Branch: 1 branch
BranchTrueFalseTotal
if $chars->[0] eq $JSON::Path::Tokenizer::FULL_STOP 17 441 458
177 }
178  
179 # If we've assembled an operator, we're done.
180 3621 ▶ last if $OPERATORS{$token};
Branch: 1 branch
BranchTrueFalseTotal
if $OPERATORS{$token} 956 2665 3621
181  
182 # Similarly, if the next character is an operator, we're done
183 2665 ▶ last if $OPERATORS{ $chars->[0] };
Branch: 1 branch
BranchTrueFalseTotal
if $OPERATORS{$chars->[0]} 441 2224 2665
184 }
185 21 ▶ no warnings qw/uninitialized/;
Subroutine BEGIN: called CC=1 SCAR=0.0
186  
187 #print "$invocation: Token: $token\n";
188 21 ▶ use warnings qw/uninitialized/;
Subroutine BEGIN: called CC=1 SCAR=0.0
189 1990 return $token;
190 }
191  
192 21 1;
193  
194 __END__
« Path/Evaluator.pm ↑ Summary Path.pm »