File Coverage

blib/lib/JSON/Pointer/Marpa.pm
Criterion Covered Total %
statement 21 21 100.0
branch 2 2 100.0
condition n/a
subroutine 6 6 100.0
pod 0 1 0.0
total 29 30 96.6


line stmt bran cond sub pod time code
1 2     2   271627 use strict;
  2         5  
  2         67  
2 2     2   8 use warnings;
  2         6  
  2         159  
3              
4             package JSON::Pointer::Marpa;
5              
6             $JSON::Pointer::Marpa::VERSION = 'v1.0.3';
7              
8 2     2   1038 use Marpa::R2 ();
  2         326136  
  2         81  
9 2     2   834 use URI::Escape qw( uri_unescape );
  2         1960  
  2         171  
10              
11 2     2   1690 use JSON::Pointer::Marpa::Semantics ();
  2         6  
  2         324  
12              
13             my $dsl = <<'END_OF_DSL';
14             lexeme default = latm => 1
15              
16             # Pseudo-rules:
17             :start ::= pointer
18             # Increasing the priority of the array_index lexeme from 0 (the default) to 1
19             # avoids parse ambiguity errors of the "ambiguous symch" type
20             :lexeme ~ array_index priority => 1
21             # The next array index refers to the (nonexistent) array element after the last
22             # array element.
23             :lexeme ~ next_array_index priority => 2
24             :lexeme ~ unescaped
25             :lexeme ~ escaped_slash
26             :lexeme ~ escaped_tilde
27              
28             # Structural (G1) rules:
29             pointer ::= pointer_segment* action => get_crv
30             pointer_segment ::= '/' reference_token
31             reference_token ::= next_array_index action => next_array_index_dereferencing
32             | array_index action => array_index_dereferencing
33             | object_name action => object_name_dereferencing
34             reference_token ::= action => object_name_dereferencing
35             object_name ::= object_name_part+ action => concat
36             object_name_part ::= unescaped action => ::first
37             | escaped_slash action => SLASH
38             | escaped_tilde action => TILDE
39              
40             # Lexical (L0) rules:
41             escaped_tilde ~ '~0'
42             escaped_slash ~ '~1'
43             # Leading zeros in the hexadecimal number representation of the Unicode code
44             # point between the curly braces are omitted.
45             unescaped ~ [\x{00}-\x{2E}\x{30}-\x{7D}\x{7F}-\x{10FFFF}]+
46              
47             array_index ~ zero | positive digits
48             next_array_index ~ '-'
49             digits ~ [\d]*
50             positive ~ [1-9]
51             zero ~ [0]
52             END_OF_DSL
53              
54             my $grammar = Marpa::R2::Scanless::G->new(
55             {
56             source => \$dsl,
57             trace_file_handle => *STDERR
58             }
59             );
60              
61             sub get {
62 48     48 0 601903 my ( undef, $json_document, $json_pointer ) = @_;
63              
64             # FIXME: properly differentiate between the 2 different representations
65             # (RFC6901 section 5 and section 6) of a JSON pointer. uri_unescape() has
66             # to be called only(!) for the URI fragment identifier representation type
67             # (section 6). Backslash unescaping has to be done for the JSON string
68             # representation (section 5) type.
69 48 100       459 $json_pointer = uri_unescape( $json_pointer )
70             if $json_pointer =~ s/\A#//; ## no critic (RequireExtendedFormatting)
71              
72 48         725 my $recognizer = Marpa::R2::Scanless::R->new(
73             {
74             grammar => $grammar
75             #trace_terminals => 1,
76             #trace_values => 1,
77             }
78             );
79 48         17810 $recognizer->read( \$json_pointer );
80              
81 48         13709 ${ $recognizer->value( JSON::Pointer::Marpa::Semantics->new( $json_document ) )
  48         314  
82             }
83             }
84              
85             1