| 1 | package JSON::Decode::Regexp; | ||||||||||||||||||
| 2 | |||||||||||||||||||
| 3 | 2 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |||||||||||||||||
| 4 | 2 | our $DATE = '2021-08-01'; # DATE | |||||||||||||||||
| 5 | 2 | our $DIST = 'JSON-Decode-Regexp'; # DIST | |||||||||||||||||
| 6 | 2 | our $VERSION = '0.102'; # VERSION | |||||||||||||||||
| 7 | |||||||||||||||||||
| 8 | 2 | ▶ | use 5.014004; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 9 | 2 | ▶ | use strict; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 10 | 2 | ▶ | use warnings; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 11 | |||||||||||||||||||
| 12 | #use Data::Dumper; | ||||||||||||||||||
| 13 | |||||||||||||||||||
| 14 | 2 | require Exporter; | |||||||||||||||||
| 15 | 2 | our @ISA = qw(Exporter); | |||||||||||||||||
| 16 | 2 | our @EXPORT_OK = qw(from_json); | |||||||||||||||||
| 17 | |||||||||||||||||||
| 18 | 5 | ▶ | sub _fail { die __PACKAGE__.": $_[0] at offset ".pos()."\n" } | ||||||||||||||||
|
Subroutine
_fail: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 19 | |||||||||||||||||||
| 20 | 2 | my %escape_codes = ( | |||||||||||||||||
| 21 | "\\" => "\\", | ||||||||||||||||||
| 22 | "\"" => "\"", | ||||||||||||||||||
| 23 | "b" => "\b", | ||||||||||||||||||
| 24 | "f" => "\f", | ||||||||||||||||||
| 25 | "n" => "\n", | ||||||||||||||||||
| 26 | "r" => "\r", | ||||||||||||||||||
| 27 | "t" => "\t", | ||||||||||||||||||
| 28 | ); | ||||||||||||||||||
| 29 | |||||||||||||||||||
| 30 | 22 | ▶ | sub _decode_str { | ||||||||||||||||
|
Subroutine
_decode_str: called CC=4 SCAR=13.9
| |||||||||||||||||||
| 31 | 22 | my $str = shift; | |||||||||||||||||
| 32 | 22 | $str =~ s[(\\(?:([0-7]{1,3})|x([0-9A-Fa-f]{1,2})|(.)))] | |||||||||||||||||
| 33 | [defined($2) ? chr(oct $2) : | ||||||||||||||||||
| 34 | defined($3) ? chr(hex $3) : | ||||||||||||||||||
| 35 | 11 | ▶ | $escape_codes{$4} ? $escape_codes{$4} : | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 36 | $1]eg; | ||||||||||||||||||
| 37 | 22 | $str; | |||||||||||||||||
| 38 | } | ||||||||||||||||||
| 39 | |||||||||||||||||||
| 40 | our $FROM_JSON = qr{ | ||||||||||||||||||
| 41 | |||||||||||||||||||
| 42 | (?: | ||||||||||||||||||
| 43 | (?&VALUE) (?{ $_ = $^R->[1] }) | ||||||||||||||||||
| 44 | | | ||||||||||||||||||
| 45 | \z (?{ _fail "Unexpected end of input" }) | ||||||||||||||||||
| 46 | | | ||||||||||||||||||
| 47 | (?{ _fail "Invalid literal" }) | ||||||||||||||||||
| 48 | ) | ||||||||||||||||||
| 49 | |||||||||||||||||||
| 50 | (?(DEFINE) | ||||||||||||||||||
| 51 | |||||||||||||||||||
| 52 | (?<OBJECT> | ||||||||||||||||||
| 53 | \{\s* | ||||||||||||||||||
| 54 | (?{ [$^R, {}] }) | ||||||||||||||||||
| 55 | (?: | ||||||||||||||||||
| 56 | (?&KV) # [[$^R, {}], $k, $v] | ||||||||||||||||||
| 57 | (?{ [$^R->[0][0], {$^R->[1] => $^R->[2]}] }) | ||||||||||||||||||
| 58 | \s* | ||||||||||||||||||
| 59 | (?: | ||||||||||||||||||
| 60 | (?: | ||||||||||||||||||
| 61 | ,\s* (?&KV) # [[$^R, {...}], $k, $v] | ||||||||||||||||||
| 62 | (?{ $^R->[0][1]{ $^R->[1] } = $^R->[2]; $^R->[0] }) | ||||||||||||||||||
| 63 | )* | ||||||||||||||||||
| 64 | | | ||||||||||||||||||
| 65 | (?:[^,\}]|\z) (?{ _fail "Expected ',' or '\x7d'" }) | ||||||||||||||||||
| 66 | )* | ||||||||||||||||||
| 67 | )? | ||||||||||||||||||
| 68 | \s* | ||||||||||||||||||
| 69 | (?: | ||||||||||||||||||
| 70 | \} | ||||||||||||||||||
| 71 | | | ||||||||||||||||||
| 72 | (?:.|\z) (?{ _fail "Expected closing of hash" }) | ||||||||||||||||||
| 73 | ) | ||||||||||||||||||
| 74 | ) | ||||||||||||||||||
| 75 | |||||||||||||||||||
| 76 | (?<KV> | ||||||||||||||||||
| 77 | (?&STRING) # [$^R, "string"] | ||||||||||||||||||
| 78 | \s* | ||||||||||||||||||
| 79 | (?: | ||||||||||||||||||
| 80 | :\s* (?&VALUE) # [[$^R, "string"], $value] | ||||||||||||||||||
| 81 | (?{ [$^R->[0][0], $^R->[0][1], $^R->[1]] }) | ||||||||||||||||||
| 82 | | | ||||||||||||||||||
| 83 | (?:[^:]|\z) (?{ _fail "Expected ':'" }) | ||||||||||||||||||
| 84 | ) | ||||||||||||||||||
| 85 | ) | ||||||||||||||||||
| 86 | |||||||||||||||||||
| 87 | (?<ARRAY> | ||||||||||||||||||
| 88 | \[\s* | ||||||||||||||||||
| 89 | (?{ [$^R, []] }) | ||||||||||||||||||
| 90 | (?: | ||||||||||||||||||
| 91 | (?&VALUE) # [[$^R, []], $val] | ||||||||||||||||||
| 92 | (?{ [$^R->[0][0], [$^R->[1]]] }) | ||||||||||||||||||
| 93 | \s* | ||||||||||||||||||
| 94 | (?: | ||||||||||||||||||
| 95 | (?: | ||||||||||||||||||
| 96 | ,\s* (?&VALUE) | ||||||||||||||||||
| 97 | (?{ push @{$^R->[0][1]}, $^R->[1]; $^R->[0] }) | ||||||||||||||||||
| 98 | )* | ||||||||||||||||||
| 99 | | | ||||||||||||||||||
| 100 | (?: [^,\]]|\z ) (?{ _fail "Expected ',' or '\x5d'" }) | ||||||||||||||||||
| 101 | ) | ||||||||||||||||||
| 102 | )? | ||||||||||||||||||
| 103 | \s* | ||||||||||||||||||
| 104 | (?: | ||||||||||||||||||
| 105 | \] | ||||||||||||||||||
| 106 | | | ||||||||||||||||||
| 107 | (?:.|\z) (?{ _fail "Expected closing of array" }) | ||||||||||||||||||
| 108 | ) | ||||||||||||||||||
| 109 | ) | ||||||||||||||||||
| 110 | |||||||||||||||||||
| 111 | (?<VALUE> | ||||||||||||||||||
| 112 | \s* | ||||||||||||||||||
| 113 | ( | ||||||||||||||||||
| 114 | (?&STRING) | ||||||||||||||||||
| 115 | | | ||||||||||||||||||
| 116 | (?&NUMBER) | ||||||||||||||||||
| 117 | | | ||||||||||||||||||
| 118 | (?&OBJECT) | ||||||||||||||||||
| 119 | | | ||||||||||||||||||
| 120 | (?&ARRAY) | ||||||||||||||||||
| 121 | | | ||||||||||||||||||
| 122 | true (?{ [$^R, 1] }) | ||||||||||||||||||
| 123 | | | ||||||||||||||||||
| 124 | false (?{ [$^R, 0] }) | ||||||||||||||||||
| 125 | | | ||||||||||||||||||
| 126 | null (?{ [$^R, undef] }) | ||||||||||||||||||
| 127 | ) | ||||||||||||||||||
| 128 | \s* | ||||||||||||||||||
| 129 | ) | ||||||||||||||||||
| 130 | |||||||||||||||||||
| 131 | (?<STRING> | ||||||||||||||||||
| 132 | " | ||||||||||||||||||
| 133 | ( | ||||||||||||||||||
| 134 | (?: | ||||||||||||||||||
| 135 | [^\\"]+ | ||||||||||||||||||
| 136 | | | ||||||||||||||||||
| 137 | \\ [0-7]{1,3} | ||||||||||||||||||
| 138 | | | ||||||||||||||||||
| 139 | \\ x [0-9A-Fa-f]{1,2} | ||||||||||||||||||
| 140 | | | ||||||||||||||||||
| 141 | \\ ["\\/bfnrt] | ||||||||||||||||||
| 142 | #| | ||||||||||||||||||
| 143 | # \\ u [0-9a-fA-f]{4} | ||||||||||||||||||
| 144 | | | ||||||||||||||||||
| 145 | \\ (.) (?{ _fail "Invalid string escape character $^N" }) | ||||||||||||||||||
| 146 | )* | ||||||||||||||||||
| 147 | ) | ||||||||||||||||||
| 148 | (?: | ||||||||||||||||||
| 149 | " | ||||||||||||||||||
| 150 | | | ||||||||||||||||||
| 151 | (?:\\|\z) (?{ _fail "Expected closing of string" }) | ||||||||||||||||||
| 152 | ) | ||||||||||||||||||
| 153 | |||||||||||||||||||
| 154 | (?{ [$^R, _decode_str($^N)] }) | ||||||||||||||||||
| 155 | ) | ||||||||||||||||||
| 156 | |||||||||||||||||||
| 157 | (?<NUMBER> | ||||||||||||||||||
| 158 | ( | ||||||||||||||||||
| 159 | -? | ||||||||||||||||||
| 160 | (?: 0 | [1-9][0-9]* ) | ||||||||||||||||||
| 161 | (?: \. [0-9]+ )? | ||||||||||||||||||
| 162 | (?: [eE] [-+]? [0-9]+ )? | ||||||||||||||||||
| 163 | ) | ||||||||||||||||||
| 164 | |||||||||||||||||||
| 165 | (?{ [$^R, 0+$^N] }) | ||||||||||||||||||
| 166 | ) | ||||||||||||||||||
| 167 | |||||||||||||||||||
| 168 | 2 | ) }xms; | |||||||||||||||||
| 169 | |||||||||||||||||||
| 170 | 21 | ▶ | sub from_json { | ||||||||||||||||
|
Subroutine
from_json: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||
| 171 | 21 | state $re = qr{\A$FROM_JSON\z}; | |||||||||||||||||
| 172 | |||||||||||||||||||
| 173 | 21 | local $_ = shift; | |||||||||||||||||
| 174 | 21 | local $^R; | |||||||||||||||||
| 175 | 21 | ▶ | eval { $_ =~ $re } and return $_; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 176 | 5 | ▶ | die $@ if $@; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 177 | 0 | die 'no match'; | |||||||||||||||||
| 178 | } | ||||||||||||||||||
| 179 | |||||||||||||||||||
| 180 | 2 | 1; | |||||||||||||||||
| 181 | # ABSTRACT: JSON parser as a single Perl Regex | ||||||||||||||||||
| 182 | |||||||||||||||||||
| 183 | __END__ | ||||||||||||||||||