| 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
| |||||||||||||||||||||||||||||
| 34 | 1181 | push @tokens, $token; | |||||||||||||||||||||||||||
| 35 | 1181 | ▶ | 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 47 | 1025 | $filter .= $char; | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 1025 | ▶ | last unless @{$chars}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 1025 | ▶ | last if $chars->[0] eq $RIGHT_PARENTHESIS; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 61 | 2855 | ▶ | 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
| |||||||||||||||||||||||||||||
| 62 | 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
| |||||||||||||||||||||||||||||
| 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
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
| |||||||||||||||||||||||||||||
| 71 | 2 | $token .= shift @{$chars}; | |||||||||||||||||||||||||||
| 72 | 2 | next; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 2805 | $token .= $char; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 2805 | ▶ | next if $in_quote; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 2499 | ▶ | if ( $char eq $LEFT_SQUARE_BRACKET ) { # distinguish between '[', '[(', and '[?(' | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 83 | 116 | ▶ | if ( $chars->[0] eq $LEFT_PARENTHESIS ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 1 | next; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | 115 | ▶ | if ( $chars->[0] eq $QUESTION_MARK ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | # If we've assembled an operator, we're done. | ||||||||||||||||||||||||||||
| 106 | 2459 | ▶ | last if $OPERATORS{$token}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | # Similarly, if the next character is an operator, we're done | ||||||||||||||||||||||||||||
| 109 | 1795 | ▶ | last if $OPERATORS{ $chars->[0] }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | 1401 | return $token; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 18 | 1; | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | __END__ | ||||||||||||||||||||||||||||