| 1 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 2 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | |||||||||||
| 4 | package JSON::Pointer::Marpa; | ||||||||||
| 5 | |||||||||||
| 6 | 2 | $JSON::Pointer::Marpa::VERSION = 'v1.0.3'; | |||||||||
| 7 | |||||||||||
| 8 | 2 | ▶ | use Marpa::R2 (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 2 | ▶ | use URI::Escape qw( uri_unescape ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 2 | ▶ | use JSON::Pointer::Marpa::Semantics (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | 2 | 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 | 2 | my $grammar = Marpa::R2::Scanless::G->new( | |||||||||
| 55 | { | ||||||||||
| 56 | source => \$dsl, | ||||||||||
| 57 | trace_file_handle => *STDERR | ||||||||||
| 58 | } | ||||||||||
| 59 | ); | ||||||||||
| 60 | |||||||||||
| 61 | 48 | ▶ | sub get { | ||||||||
|
Subroutine
get: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 62 | 48 | 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 | ▶ | $json_pointer = uri_unescape( $json_pointer ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | if $json_pointer =~ s/\A#//; ## no critic (RequireExtendedFormatting) | ||||||||||
| 71 | |||||||||||
| 72 | 48 | my $recognizer = Marpa::R2::Scanless::R->new( | |||||||||
| 73 | { | ||||||||||
| 74 | grammar => $grammar | ||||||||||
| 75 | #trace_terminals => 1, | ||||||||||
| 76 | #trace_values => 1, | ||||||||||
| 77 | } | ||||||||||
| 78 | ); | ||||||||||
| 79 | 48 | $recognizer->read( \$json_pointer ); | |||||||||
| 80 | |||||||||||
| 81 | 48 | ${ $recognizer->value( JSON::Pointer::Marpa::Semantics->new( $json_document ) ) | |||||||||
| 82 | } | ||||||||||
| 83 | } | ||||||||||
| 84 | |||||||||||
| 85 | 2 | 1 | |||||||||