| 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
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
$token eq $JSON::Path::Tokenizer::TOKEN_SCRIPT_OPEN or $token eq $JSON::Path::Tokenizer::TOKEN_FILTER_OPEN
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 55 | 1446 | $filter .= $char; | |||||||||||||||||||||||||||
| 56 | 1446 | ▶ | if ( $char eq $APOSTROPHE || $char eq $QUOTATION_MARK ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$char eq $JSON::Path::Tokenizer::APOSTROPHE or $char eq $JSON::Path::Tokenizer::QUOTATION_MARK
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
| |||||||||||||||||||||||||||||
| 57 | 46 | ▶ | if ( $in_quote && $in_quote eq $char ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$in_quote and $in_quote eq $char
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
| |||||||||||||||||||||||||||||
| 58 | 23 | $in_quote = ''; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | else { | ||||||||||||||||||||||||||||
| 61 | 23 | $in_quote = $char; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 1446 | ▶ | last unless @{$chars}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 1446 | ▶ | last if $chars->[0] eq $RIGHT_PARENTHESIS && !$in_quote; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$chars->[0] eq $JSON::Path::Tokenizer::RIGHT_PARENTHESIS and not $in_quote
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 3700 | ▶ | if ( $char eq $APOSTROPHE || $char eq $QUOTATION_MARK ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$char eq $JSON::Path::Tokenizer::APOSTROPHE or $char eq $JSON::Path::Tokenizer::QUOTATION_MARK
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
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
$in_quote and $in_quote eq $char
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
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
$char eq $ESCAPE_CHAR and not $in_quote
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 3305 | ▶ | if ( $char eq $LEFT_SQUARE_BRACKET ) | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 107 | { # distinguish between '[', '[(', and '[?(' | ||||||||||||||||||||||||||||
| 108 | 163 | ▶ | if ( $chars->[0] eq $LEFT_PARENTHESIS ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 1 | next; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | 162 | ▶ | if ( $chars->[0] eq $QUESTION_MARK ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | # If we've assembled an operator, we're done. | ||||||||||||||||||||||||||||
| 135 | 3252 | ▶ | last if $OPERATORS{$token}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | # Similarly, if the next character is an operator, we're done | ||||||||||||||||||||||||||||
| 138 | 2393 | ▶ | last if $OPERATORS{ $chars->[0] }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||