File Coverage

blib/lib/JSON/Pointer/Marpa/Semantics.pm
Criterion Covered Total %
statement 44 44 100.0
branch 14 14 100.0
condition n/a
subroutine 13 13 100.0
pod 0 7 0.0
total 71 78 91.0


line stmt bran cond sub pod time code
1 2     2   14 use strict;
  2         4  
  2         87  
2 2     2   9 use warnings;
  2         3  
  2         134  
3              
4             package JSON::Pointer::Marpa::Semantics;
5              
6 2     2   902 use subs qw( _index_exists _member_exists );
  2         488  
  2         12  
7              
8             use constant { ## no critic (ProhibitConstantPragma)
9 2         1464 EMPTY => '',
10             SLASH => '/',
11             TILDE => '~'
12 2     2   107 };
  2         3  
13              
14             # This is a rule evaluation closure of a quantified rule
15             # https://metacpan.org/pod/distribution/Marpa-R2/pod/Semantics.pod#Quantified-rule-nodes
16             sub new {
17 48     48 0 151 my ( $class, $crv ) = @_; # crv == currently referenced value
18              
19 48         372 bless { crv => $crv }, $class
20             }
21              
22             sub concat {
23 57     57 0 173018 shift;
24 57         267 join '', @_
25             }
26              
27             sub array_index_dereferencing {
28 20     20 0 31959 my ( $self, $index ) = @_;
29              
30 20         70 my $crv = $self->get_crv;
31 20         49 my $crt = ref $crv; # crt == currently referenced type
32 20 100       69 if ( $crt eq 'ARRAY' ) {
    100          
33 17         63 $self->set_crv( _index_exists( $crv, $index ) )
34             } elsif ( $crt eq 'HASH' ) {
35 2         9 $self->set_crv( _member_exists( $crv, $index ) )
36             } else {
37 1         6 Marpa::R2::Context::bail(
38             "Currently referenced type '$crt' isn't a JSON structured type (array or object)!"
39             )
40             }
41              
42             undef
43 16         42 }
44              
45             sub next_array_index_dereferencing {
46 2     2 0 93 my ( $self, $next_index ) = @_;
47              
48 2         8 my $crv = $self->get_crv;
49             ref $crv eq 'ARRAY'
50             ? Marpa::R2::Context::bail(
51             "Handling of '$next_index' array index isn't implemented!" )
52 2 100       16 : $self->set_crv( $crv->{ $next_index } );
53              
54             undef
55 1         1 }
56              
57             sub object_name_dereferencing {
58 59     59 0 7793 my ( $self, $member ) = @_;
59 59 100       231 $member = '' if @_ == 1;
60              
61 59         197 my $crv = $self->get_crv;
62 59         151 my $crt = ref $crv; # crt == currently referenced type
63 59 100       189 Marpa::R2::Context::bail(
64             "Currently referenced type '$crt' isn't a JSON object!" )
65             unless $crt eq 'HASH';
66 57         197 $self->set_crv( _member_exists( $crv, $member ) );
67              
68             undef
69 55         144 }
70              
71             sub set_crv {
72 72     72 0 152 my ( $self, $crv ) = @_;
73              
74 72         158 $self->{ crv } = $crv;
75              
76             undef
77 72         115 }
78              
79             sub get_crv {
80 120     120 0 11225 my ( $self ) = @_;
81              
82             $self->{ crv }
83 120         352 }
84              
85             sub _index_exists ( $$ ) {
86 17     17   82 my ( $crv, $index ) = @_;
87              
88 17 100       114 $index < @$crv
89             ? $crv->[ $index ]
90             : Marpa::R2::Context::bail(
91             "JSON array has been accessed with an index $index that is greater than or equal to the size of the array!"
92             )
93             }
94              
95             sub _member_exists ( $$ ) {
96 59     59   132 my ( $crv, $member ) = @_;
97              
98             exists $crv->{ $member }
99 59 100       365 ? $crv->{ $member }
100             : Marpa::R2::Context::bail(
101             "JSON object has been accessed with a member '$member' that does not exist!" )
102             }
103              
104             1