| 1 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 2 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | package YAML::PP::Schema::YAML1_1; | ||||||||||
| 4 | |||||||||||
| 5 | 3 | our $VERSION = 'v0.41.1'; # TRIAL VERSION | |||||||||
| 6 | |||||||||||
| 7 | 3 | ▶ | use YAML::PP::Schema::JSON qw/ | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | represent_int represent_float represent_literal represent_bool | ||||||||||
| 9 | represent_undef | ||||||||||
| 10 | 3 | /; | |||||||||
| 11 | |||||||||||
| 12 | 3 | ▶ | use YAML::PP::Common qw/ YAML_PLAIN_SCALAR_STYLE /; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | #https://yaml.org/type/bool.html | ||||||||||
| 15 | # y|Y|yes|Yes|YES|n|N|no|No|NO | ||||||||||
| 16 | # |true|True|TRUE|false|False|FALSE | ||||||||||
| 17 | # |on|On|ON|off|Off|OFF | ||||||||||
| 18 | |||||||||||
| 19 | # https://yaml.org/type/float.html | ||||||||||
| 20 | # [-+]?([0-9][0-9_]*)?\.[0-9.]*([eE][-+][0-9]+)? (base 10) | ||||||||||
| 21 | # |[-+]?[0-9][0-9_]*(:[0-5]?[0-9])+\.[0-9_]* (base 60) | ||||||||||
| 22 | # |[-+]?\.(inf|Inf|INF) # (infinity) | ||||||||||
| 23 | # |\.(nan|NaN|NAN) # (not a number) | ||||||||||
| 24 | |||||||||||
| 25 | # https://yaml.org/type/int.html | ||||||||||
| 26 | # [-+]?0b[0-1_]+ # (base 2) | ||||||||||
| 27 | # |[-+]?0[0-7_]+ # (base 8) | ||||||||||
| 28 | # |[-+]?(0|[1-9][0-9_]*) # (base 10) | ||||||||||
| 29 | # |[-+]?0x[0-9a-fA-F_]+ # (base 16) | ||||||||||
| 30 | # |[-+]?[1-9][0-9_]*(:[0-5]?[0-9])+ # (base 60) | ||||||||||
| 31 | |||||||||||
| 32 | # https://yaml.org/type/null.html | ||||||||||
| 33 | # ~ # (canonical) | ||||||||||
| 34 | # |null|Null|NULL # (English) | ||||||||||
| 35 | # | # (Empty) | ||||||||||
| 36 | |||||||||||
| 37 | 3 | my $RE_INT_1_1 = qr{^([+-]?(?:0|[1-9][0-9_]*))$}; | |||||||||
| 38 | #my $RE_FLOAT_1_1 = qr{^([+-]?([0-9][0-9_]*)?\.[0-9.]*([eE][+-][0-9]+)?)$}; | ||||||||||
| 39 | # https://yaml.org/type/float.html has a bug. The regex says \.[0-9.], but | ||||||||||
| 40 | # probably means \.[0-9_] | ||||||||||
| 41 | 3 | my $RE_FLOAT_1_1 = qr{^([+-]?(?:[0-9][0-9_]*)?\.[0-9_]*(?:[eE][+-][0-9]+)?)$}; | |||||||||
| 42 | 3 | my $RE_SEXAGESIMAL = qr{^([+-]?[0-9][0-9_]*(:[0-5]?[0-9])+\.[0-9_]*)$}; | |||||||||
| 43 | 3 | my $RE_SEXAGESIMAL_INT = qr{^([-+]?[1-9][0-9_]*(:[0-5]?[0-9])+)$}; | |||||||||
| 44 | 3 | my $RE_INT_OCTAL_1_1 = qr{^([+-]?)0([0-7_]+)$}; | |||||||||
| 45 | 3 | my $RE_INT_HEX_1_1 = qr{^([+-]?)(0x[0-9a-fA-F_]+)$}; | |||||||||
| 46 | 3 | my $RE_INT_BIN_1_1 = qr{^([-+]?)(0b[0-1_]+)$}; | |||||||||
| 47 | |||||||||||
| 48 | 22 | ▶ | sub _from_oct { | ||||||||
|
Subroutine
_from_oct: called CC=2 SCAR=6.9
| |||||||||||
| 49 | 22 | my ($constructor, $event, $matches) = @_; | |||||||||
| 50 | 22 | my ($sign, $oct) = @$matches; | |||||||||
| 51 | 22 | $oct =~ tr/_//d; | |||||||||
| 52 | 22 | my $result = oct $oct; | |||||||||
| 53 | 22 | ▶ | $result = -$result if $sign eq '-'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 54 | 22 | return $result; | |||||||||
| 55 | } | ||||||||||
| 56 | 12 | ▶ | sub _from_hex { | ||||||||
|
Subroutine
_from_hex: called CC=2 SCAR=6.9
| |||||||||||
| 57 | 12 | my ($constructor, $event, $matches) = @_; | |||||||||
| 58 | 12 | my ($sign, $hex) = @$matches; | |||||||||
| 59 | 12 | my $result = hex $hex; | |||||||||
| 60 | 12 | ▶ | $result = -$result if $sign eq '-'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | 12 | return $result; | |||||||||
| 62 | } | ||||||||||
| 63 | 8 | ▶ | sub _sexa_to_float { | ||||||||
|
Subroutine
_sexa_to_float: called CC=3 SCAR=11.0
| |||||||||||
| 64 | 8 | my ($constructor, $event, $matches) = @_; | |||||||||
| 65 | 8 | my ($float) = @$matches; | |||||||||
| 66 | 8 | my $result = 0; | |||||||||
| 67 | 8 | my $i = 0; | |||||||||
| 68 | 8 | my $sign = 1; | |||||||||
| 69 | 8 | ▶ | $float =~ s/^-// and $sign = -1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | 8 | for my $part (reverse split m/:/, $float) { | |||||||||
| 71 | 24 | $result += $part * ( 60 ** $i ); | |||||||||
| 72 | 24 | $i++; | |||||||||
| 73 | } | ||||||||||
| 74 | 8 | $result = unpack F => pack F => $result; | |||||||||
| 75 | 8 | return $result * $sign; | |||||||||
| 76 | } | ||||||||||
| 77 | 24 | ▶ | sub _to_float { | ||||||||
|
Subroutine
_to_float: called CC=1 SCAR=0.0
| |||||||||||
| 78 | 24 | my ($constructor, $event, $matches) = @_; | |||||||||
| 79 | 24 | my ($float) = @$matches; | |||||||||
| 80 | 24 | $float =~ tr/_//d; | |||||||||
| 81 | 24 | $float = unpack F => pack F => $float; | |||||||||
| 82 | 24 | return $float; | |||||||||
| 83 | } | ||||||||||
| 84 | 14 | ▶ | sub _to_int { | ||||||||
|
Subroutine
_to_int: called CC=1 SCAR=0.0
| |||||||||||
| 85 | 14 | my ($constructor, $event, $matches) = @_; | |||||||||
| 86 | 14 | my ($int) = @$matches; | |||||||||
| 87 | 14 | $int =~ tr/_//d; | |||||||||
| 88 | 14 | 0 + $int; | |||||||||
| 89 | } | ||||||||||
| 90 | |||||||||||
| 91 | 5 | ▶ | sub register { | ||||||||
|
Subroutine
register: called CC=11 SCAR=24.0
Pod
documented
| |||||||||||
| 92 | 5 | my ($self, %args) = @_; | |||||||||
| 93 | 5 | my $schema = $args{schema}; | |||||||||
| 94 | |||||||||||
| 95 | $schema->add_resolver( | ||||||||||
| 96 | tag => 'tag:yaml.org,2002:null', | ||||||||||
| 97 | match => [ equals => $_ => undef ], | ||||||||||
| 98 | 5 | ) for (qw/ null NULL Null ~ /, ''); | |||||||||
| 99 | $schema->add_resolver( | ||||||||||
| 100 | tag => 'tag:yaml.org,2002:bool', | ||||||||||
| 101 | match => [ equals => $_ => $schema->true ], | ||||||||||
| 102 | 5 | ) for (qw/ true TRUE True y Y yes Yes YES on On ON /); | |||||||||
| 103 | $schema->add_resolver( | ||||||||||
| 104 | tag => 'tag:yaml.org,2002:bool', | ||||||||||
| 105 | match => [ equals => $_ => $schema->false ], | ||||||||||
| 106 | 5 | ) for (qw/ false FALSE False n N no No NO off Off OFF /); | |||||||||
| 107 | 5 | $schema->add_resolver( | |||||||||
| 108 | tag => 'tag:yaml.org,2002:int', | ||||||||||
| 109 | match => [ regex => $RE_INT_OCTAL_1_1 => \&_from_oct ], | ||||||||||
| 110 | ); | ||||||||||
| 111 | 5 | $schema->add_resolver( | |||||||||
| 112 | tag => 'tag:yaml.org,2002:int', | ||||||||||
| 113 | match => [ regex => $RE_INT_1_1 => \&_to_int ], | ||||||||||
| 114 | ); | ||||||||||
| 115 | 5 | $schema->add_resolver( | |||||||||
| 116 | tag => 'tag:yaml.org,2002:int', | ||||||||||
| 117 | match => [ regex => $RE_INT_HEX_1_1 => \&_from_hex ], | ||||||||||
| 118 | ); | ||||||||||
| 119 | 5 | $schema->add_resolver( | |||||||||
| 120 | tag => 'tag:yaml.org,2002:float', | ||||||||||
| 121 | match => [ regex => $RE_FLOAT_1_1 => \&_to_float ], | ||||||||||
| 122 | ); | ||||||||||
| 123 | 5 | $schema->add_resolver( | |||||||||
| 124 | tag => 'tag:yaml.org,2002:int', | ||||||||||
| 125 | match => [ regex => $RE_INT_BIN_1_1 => \&_from_oct ], | ||||||||||
| 126 | ); | ||||||||||
| 127 | 5 | $schema->add_resolver( | |||||||||
| 128 | tag => 'tag:yaml.org,2002:int', | ||||||||||
| 129 | match => [ regex => $RE_SEXAGESIMAL_INT => \&_sexa_to_float ], | ||||||||||
| 130 | ); | ||||||||||
| 131 | 5 | $schema->add_resolver( | |||||||||
| 132 | tag => 'tag:yaml.org,2002:float', | ||||||||||
| 133 | match => [ regex => $RE_SEXAGESIMAL => \&_sexa_to_float ], | ||||||||||
| 134 | ); | ||||||||||
| 135 | $schema->add_resolver( | ||||||||||
| 136 | tag => 'tag:yaml.org,2002:float', | ||||||||||
| 137 | match => [ equals => $_ => 0 + "inf" ], | ||||||||||
| 138 | 5 | ) for (qw/ .inf .Inf .INF +.inf +.Inf +.INF /); | |||||||||
| 139 | $schema->add_resolver( | ||||||||||
| 140 | tag => 'tag:yaml.org,2002:float', | ||||||||||
| 141 | match => [ equals => $_ => 0 - "inf" ], | ||||||||||
| 142 | 5 | ) for (qw/ -.inf -.Inf -.INF /); | |||||||||
| 143 | $schema->add_resolver( | ||||||||||
| 144 | tag => 'tag:yaml.org,2002:float', | ||||||||||
| 145 | match => [ equals => $_ => 0 + "nan" ], | ||||||||||
| 146 | 5 | ) for (qw/ .nan .NaN .NAN /); | |||||||||
| 147 | $schema->add_resolver( | ||||||||||
| 148 | tag => 'tag:yaml.org,2002:str', | ||||||||||
| 149 | 5 | ▶ | match => [ all => sub { $_[1]->{value} } ], | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 150 | implicit => 0, | ||||||||||
| 151 | ); | ||||||||||
| 152 | |||||||||||
| 153 | 5 | my $int_flags = B::SVp_IOK; | |||||||||
| 154 | 5 | my $float_flags = B::SVp_NOK; | |||||||||
| 155 | 5 | $schema->add_representer( | |||||||||
| 156 | flags => $int_flags, | ||||||||||
| 157 | code => \&represent_int, | ||||||||||
| 158 | ); | ||||||||||
| 159 | 5 | $schema->add_representer( | |||||||||
| 160 | flags => $float_flags, | ||||||||||
| 161 | code => \&represent_float, | ||||||||||
| 162 | ); | ||||||||||
| 163 | 5 | $schema->add_representer( | |||||||||
| 164 | undefined => \&represent_undef, | ||||||||||
| 165 | ); | ||||||||||
| 166 | $schema->add_representer( | ||||||||||
| 167 | equals => $_, | ||||||||||
| 168 | code => \&represent_literal, | ||||||||||
| 169 | 5 | ) for ("", qw/ | |||||||||
| 170 | true TRUE True y Y yes Yes YES on On ON | ||||||||||
| 171 | false FALSE False n N n no No NO off Off OFF | ||||||||||
| 172 | null NULL Null ~ | ||||||||||
| 173 | .inf .Inf .INF -.inf -.Inf -.INF +.inf +.Inf +.INF .nan .NaN .NAN | ||||||||||
| 174 | /); | ||||||||||
| 175 | 5 | $schema->add_representer( | |||||||||
| 176 | regex => qr{$RE_INT_1_1|$RE_FLOAT_1_1|$RE_INT_OCTAL_1_1|$RE_INT_HEX_1_1|$RE_INT_BIN_1_1|$RE_SEXAGESIMAL_INT|$RE_SEXAGESIMAL}, | ||||||||||
| 177 | code => \&represent_literal, | ||||||||||
| 178 | ); | ||||||||||
| 179 | |||||||||||
| 180 | 5 | ▶ | if ($schema->bool_class) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 181 | 2 | for my $class (@{ $schema->bool_class }) { | |||||||||
| 182 | 2 | ▶ | if ($class eq 'perl') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 183 | 1 | $schema->add_representer( | |||||||||
| 184 | bool => 1, | ||||||||||
| 185 | code => \&represent_bool, | ||||||||||
| 186 | ); | ||||||||||
| 187 | 1 | next; | |||||||||
| 188 | } | ||||||||||
| 189 | $schema->add_representer( | ||||||||||
| 190 | 1 | class_equals => $class, | |||||||||
| 191 | code => \&represent_bool, | ||||||||||
| 192 | ); | ||||||||||
| 193 | } | ||||||||||
| 194 | } | ||||||||||
| 195 | |||||||||||
| 196 | 5 | return; | |||||||||
| 197 | } | ||||||||||
| 198 | |||||||||||
| 199 | |||||||||||
| 200 | 3 | 1; | |||||||||
| 201 | |||||||||||
| 202 | __END__ | ||||||||||