| 1 | package DateTime::Format::Builder::Parser; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 24 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 24 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 24 | our $VERSION = '0.83'; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 24 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 24 | ▶ | use Params::Validate qw( | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | validate SCALAR CODEREF UNDEF ARRAYREF | ||||||||||||||||||||||||||||
| 11 | 24 | ); | |||||||||||||||||||||||||||
| 12 | 24 | ▶ | use Scalar::Util qw( weaken ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 4 | ▶ | sub on_fail { | ||||||||||||||||||||||||||
|
Subroutine
on_fail: called CC=3 SCAR=12.7
Pod
documented
| |||||||||||||||||||||||||||||
| 15 | 4 | my ( $self, $input ) = @_; | |||||||||||||||||||||||||||
| 16 | 4 | my $maker = $self->maker; | |||||||||||||||||||||||||||
| 17 | 4 | ▶ | if ( $maker and $maker->can('on_fail') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$maker and $maker->can("on_fail")
MC/DC0%0/2
$maker and $maker->can("on_fail")
$maker
$maker->can("on_fail")
Truth table33%1/3
A and B
A= $makerB= $maker->can("on_fail")
| |||||||||||||||||||||||||||||
| 18 | 4 | $maker->on_fail($input); | |||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||
| 20 | else { | ||||||||||||||||||||||||||||
| 21 | 0 | croak __PACKAGE__ . ": Invalid date format: $input"; | |||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 0 | ▶ | sub no_parser { | ||||||||||||||||||||||||||
|
Subroutine
no_parser: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 26 | 0 | croak "No parser set for this parser object."; | |||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 42 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 30 | 42 | my $class = shift; | |||||||||||||||||||||||||||
| 31 | 42 | ▶ | $class = ref($class) || $class; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table33%1/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 42 | my $self = bless { | |||||||||||||||||||||||||||
| 34 | on_fail => \&on_fail, | ||||||||||||||||||||||||||||
| 35 | parser => \&no_parser, | ||||||||||||||||||||||||||||
| 36 | }, $class; | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 42 | return $self; | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 4 | ▶ | sub maker { $_[0]->{maker} } | ||||||||||||||||||||||||||
|
Subroutine
maker: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 42 | ▶ | sub set_maker { | ||||||||||||||||||||||||||
|
Subroutine
set_maker: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 44 | 42 | my $self = shift; | |||||||||||||||||||||||||||
| 45 | 42 | my $maker = shift; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 42 | $self->{maker} = $maker; | |||||||||||||||||||||||||||
| 48 | weaken $self->{maker} | ||||||||||||||||||||||||||||
| 49 | 42 | ▶ | if ref $self->{maker}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 42 | return $self; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 5 | ▶ | sub fail { | ||||||||||||||||||||||||||
|
Subroutine
fail: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 55 | 5 | my ( $self, $parent, $input ) = @_; | |||||||||||||||||||||||||||
| 56 | 5 | $self->{on_fail}->( $self, $input, $parent ); | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 52 | ▶ | sub parse { | ||||||||||||||||||||||||||
|
Subroutine
parse: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 60 | 52 | my ( $self, $parent, $input, @args ) = @_; | |||||||||||||||||||||||||||
| 61 | 52 | my $r = $self->{parser}->( $parent, $input, @args ); | |||||||||||||||||||||||||||
| 62 | 52 | ▶ | $self->fail( $parent, $input ) unless defined $r; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 49 | $r; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 42 | ▶ | sub set_parser { | ||||||||||||||||||||||||||
|
Subroutine
set_parser: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 67 | 42 | my ( $self, $parser ) = @_; | |||||||||||||||||||||||||||
| 68 | 42 | $self->{parser} = $parser; | |||||||||||||||||||||||||||
| 69 | 42 | $self; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 1 | ▶ | sub set_fail { | ||||||||||||||||||||||||||
|
Subroutine
set_fail: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 73 | 1 | my ( $self, $fail ) = @_; | |||||||||||||||||||||||||||
| 74 | 1 | $self->{on_fail} = $fail; | |||||||||||||||||||||||||||
| 75 | 1 | $self; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 24 | my @callbacks = qw( on_match on_fail postprocess preprocess ); | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | { | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | my %params = ( | ||||||||||||||||||||||||||||
| 83 | common => { | ||||||||||||||||||||||||||||
| 84 | length => { | ||||||||||||||||||||||||||||
| 85 | type => SCALAR | ARRAYREF, | ||||||||||||||||||||||||||||
| 86 | optional => 1, | ||||||||||||||||||||||||||||
| 87 | callbacks => { | ||||||||||||||||||||||||||||
| 88 | 24 | ▶ | 'is an int' => sub { ref $_[0] ? 1 : $_[0] !~ /\D/ }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 24 | ▶ | 'not empty' => sub { ref $_[0] ? @{ $_[0] } >= 1 : 1 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | }, | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | # Stuff used by callbacks | ||||||||||||||||||||||||||||
| 94 | label => { type => SCALAR, optional => 1 }, | ||||||||||||||||||||||||||||
| 95 | ( | ||||||||||||||||||||||||||||
| 96 | 24 | map { $_ => { type => CODEREF | ARRAYREF, optional => 1 } } | |||||||||||||||||||||||||||
| 97 | @callbacks | ||||||||||||||||||||||||||||
| 98 | ), | ||||||||||||||||||||||||||||
| 99 | }, | ||||||||||||||||||||||||||||
| 100 | ); | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 67 | ▶ | sub params { | ||||||||||||||||||||||||||
|
Subroutine
params: called CC=2 SCAR=7.4
Pod
documented
| |||||||||||||||||||||||||||||
| 103 | 67 | my $self = shift; | |||||||||||||||||||||||||||
| 104 | 67 | ▶ | my $caller = ref $self || $self; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $self || $self
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table33%1/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||||||||||
| 105 | 67 | return { map {%$_} @params{ $caller, 'common' } }; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 24 | my $all_params; | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 67 | ▶ | sub params_all { | ||||||||||||||||||||||||||
|
Subroutine
params_all: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 111 | 67 | ▶ | return $all_params if defined $all_params; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 16 | my %all_params = map {%$_} values %params; | |||||||||||||||||||||||||||
| 113 | 16 | $_->{optional} = 1 for values %all_params; | |||||||||||||||||||||||||||
| 114 | 16 | $all_params = \%all_params; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 24 | my %inverse; | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 96 | ▶ | sub valid_params { | ||||||||||||||||||||||||||
|
Subroutine
valid_params: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 120 | 96 | my $self = shift; | |||||||||||||||||||||||||||
| 121 | 96 | my $from = (caller)[0]; | |||||||||||||||||||||||||||
| 122 | 96 | my %args = @_; | |||||||||||||||||||||||||||
| 123 | 96 | $params{$from} = \%args; | |||||||||||||||||||||||||||
| 124 | 96 | for ( keys %args ) { | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | # %inverse contains keys matching all the | ||||||||||||||||||||||||||||
| 127 | # possible params; values are the class if and | ||||||||||||||||||||||||||||
| 128 | # only if that class is the only one that uses | ||||||||||||||||||||||||||||
| 129 | # the given param. | ||||||||||||||||||||||||||||
| 130 | 192 | ▶ | $inverse{$_} = exists $inverse{$_} ? undef : $from; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | 96 | undef $all_params; | |||||||||||||||||||||||||||
| 133 | 96 | 1; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 81 | ▶ | sub whose_params { | ||||||||||||||||||||||||||
|
Subroutine
whose_params: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 137 | 81 | my $param = shift; | |||||||||||||||||||||||||||
| 138 | 81 | return $inverse{$param}; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 24 | ▶ | sub create_single_object { | ||||||||||||||||||||||||||
|
Subroutine
create_single_object: not called CC=1 SCAR=4.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 143 | 0 | my ($self) = shift; | |||||||||||||||||||||||||||
| 144 | 0 | my $obj = $self->new; | |||||||||||||||||||||||||||
| 145 | 0 | my $parser = $self->create_single_parser(@_); | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 0 | $obj->set_parser($parser); | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 67 | ▶ | sub create_single_parser { | ||||||||||||||||||||||||||
|
Subroutine
create_single_parser: called CC=10 SCAR=23.7
Pod
documented
| |||||||||||||||||||||||||||||
| 151 | 67 | my $class = shift; | |||||||||||||||||||||||||||
| 152 | 67 | ▶ | return $_[0] if ref $_[0] eq 'CODE'; # already code | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | 0 | ▶ | @_ = %{ $_[0] } if ref $_[0] eq 'HASH'; # turn hashref into hash | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | # ordinary boring sort | ||||||||||||||||||||||||||||
| 155 | 67 | my %args = validate( @_, params_all() ); | |||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | # Determine variables for ease of reference. | ||||||||||||||||||||||||||||
| 158 | 67 | for (@callbacks) { | |||||||||||||||||||||||||||
| 159 | 268 | ▶ | $args{$_} = $class->merge_callbacks( $args{$_} ) if $args{$_}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | # Determine parser class | ||||||||||||||||||||||||||||
| 163 | 67 | my $from; | |||||||||||||||||||||||||||
| 164 | 67 | for ( keys %args ) { | |||||||||||||||||||||||||||
| 165 | 81 | $from = whose_params($_); | |||||||||||||||||||||||||||
| 166 | 81 | ▶ | next if ( not defined $from ) or ( $from eq 'common' ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $from or $from eq "common"
MC/DC50%1/2
not defined $from or $from eq "common"
not defined $from
$from eq "common"
Truth table66%2/3
not(A) or B
A= not defined $fromB= $from eq "common"
| |||||||||||||||||||||||||||||
| 167 | 67 | last; | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | 67 | ▶ | croak "Could not identify a parsing module to use." unless $from; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | # Find and call parser creation method | ||||||||||||||||||||||||||||
| 172 | 67 | ▶ | my $method = $from->can("create_parser") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 173 | or croak | ||||||||||||||||||||||||||||
| 174 | "Can't create a $_ parser (no appropriate create_parser method)"; | ||||||||||||||||||||||||||||
| 175 | 67 | my @args = %args; | |||||||||||||||||||||||||||
| 176 | 67 | %args = validate( @args, $from->params ); | |||||||||||||||||||||||||||
| 177 | 67 | $from->$method(%args); | |||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 30 | ▶ | sub merge_callbacks { | ||||||||||||||||||||||||||
|
Subroutine
merge_callbacks: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||
| 181 | 30 | my $self = shift; | |||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 30 | ▶ | return unless @_; # No arguments | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 184 | 29 | ▶ | return unless $_[0]; # Irrelevant argument | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 185 | 28 | my @callbacks = @_; | |||||||||||||||||||||||||||
| 186 | 28 | ▶ | if ( @_ == 1 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 187 | 27 | ▶ | return $_[0] if ref $_[0] eq 'CODE'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 188 | 3 | ▶ | @callbacks = @{ $_[0] } if ref $_[0] eq 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | 4 | ▶ | return unless @callbacks; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 3 | for (@callbacks) { | |||||||||||||||||||||||||||
| 193 | 15 | ▶ | croak "All callbacks must be coderefs!" unless ref $_ eq 'CODE'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 2 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.1
| |||||||||||||||||||||||||||||
| 197 | 2 | my $rv; | |||||||||||||||||||||||||||
| 198 | 2 | my %args = @_; | |||||||||||||||||||||||||||
| 199 | 2 | for my $cb (@callbacks) { | |||||||||||||||||||||||||||
| 200 | 14 | $rv = $cb->(%args); | |||||||||||||||||||||||||||
| 201 | 14 | ▶ | return $rv unless $rv; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | # Ugh. Symbiotic. All but postprocessor return the date. | ||||||||||||||||||||||||||||
| 204 | 14 | ▶ | $args{input} = $rv unless $args{parsed}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||
| 206 | 2 | $rv; | |||||||||||||||||||||||||||
| 207 | 2 | }; | |||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | 42 | ▶ | sub create_multiple_parsers { | ||||||||||||||||||||||||||
|
Subroutine
create_multiple_parsers: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 211 | 42 | my $class = shift; | |||||||||||||||||||||||||||
| 212 | 42 | my ( $options, @specs ) = @_; | |||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | 42 | my $obj = $class->new; | |||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | # Organise the specs, and transform them into parsers. | ||||||||||||||||||||||||||||
| 217 | 42 | my ( $lengths, $others ) = $class->sort_parsers( $options, \@specs ); | |||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | # Merge callbacks if any. | ||||||||||||||||||||||||||||
| 220 | 42 | for ('preprocess') { | |||||||||||||||||||||||||||
| 221 | $options->{$_} = $class->merge_callbacks( $options->{$_} ) | ||||||||||||||||||||||||||||
| 222 | 42 | ▶ | if $options->{$_}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | # Custom fail method? | ||||||||||||||||||||||||||||
| 226 | 42 | ▶ | $obj->set_fail( $options->{on_fail} ) if exists $options->{on_fail}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | # Who's our maker? | ||||||||||||||||||||||||||||
| 229 | 42 | ▶ | $obj->set_maker( $options->{maker} ) if exists $options->{maker}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | # We don't want to save the whole options hash as a closure, since | ||||||||||||||||||||||||||||
| 232 | # that can cause a circular reference when $options->{maker} is | ||||||||||||||||||||||||||||
| 233 | # set. | ||||||||||||||||||||||||||||
| 234 | 42 | my $preprocess = $options->{preprocess}; | |||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | # These are the innards of a multi-parser. | ||||||||||||||||||||||||||||
| 237 | 52 | ▶ | my $parser = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||
| 238 | 52 | my ( $self, $date, @args ) = @_; | |||||||||||||||||||||||||||
| 239 | 52 | ▶ | return unless defined $date; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | # Parameters common to the callbacks. Pre-prepared. | ||||||||||||||||||||||||||||
| 242 | 52 | ▶ | my %param = ( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 243 | self => $self, | ||||||||||||||||||||||||||||
| 244 | ( @args ? ( args => \@args ) : () ), | ||||||||||||||||||||||||||||
| 245 | ); | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | 52 | my %p; | |||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | # Preprocess and potentially fill %p | ||||||||||||||||||||||||||||
| 250 | 52 | ▶ | if ($preprocess) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 251 | 7 | $date = $preprocess->( input => $date, parsed => \%p, %param ); | |||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | # Find length parser | ||||||||||||||||||||||||||||
| 255 | 52 | ▶ | if (%$lengths) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 256 | 9 | my $length = length $date; | |||||||||||||||||||||||||||
| 257 | 9 | my $parser = $lengths->{$length}; | |||||||||||||||||||||||||||
| 258 | 9 | ▶ | if ($parser) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | # Found one, call it with _copy_ of %p | ||||||||||||||||||||||||||||
| 261 | 8 | my $dt = $parser->( $self, $date, {%p}, @args ); | |||||||||||||||||||||||||||
| 262 | 8 | ▶ | return $dt if defined $dt; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | # Or calls all others, with _copy_ of %p | ||||||||||||||||||||||||||||
| 267 | 44 | for my $parser (@$others) { | |||||||||||||||||||||||||||
| 268 | 74 | my $dt = $parser->( $self, $date, {%p}, @args ); | |||||||||||||||||||||||||||
| 269 | 74 | ▶ | return $dt if defined $dt; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | # Failed, return undef. | ||||||||||||||||||||||||||||
| 273 | 5 | return; | |||||||||||||||||||||||||||
| 274 | 42 | }; | |||||||||||||||||||||||||||
| 275 | 42 | $obj->set_parser($parser); | |||||||||||||||||||||||||||
| 276 | } | ||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | 42 | ▶ | sub sort_parsers { | ||||||||||||||||||||||||||
|
Subroutine
sort_parsers: called CC=8 SCAR=21.0
Pod
documented
| |||||||||||||||||||||||||||||
| 279 | 42 | my $class = shift; | |||||||||||||||||||||||||||
| 280 | 42 | shift; | |||||||||||||||||||||||||||
| 281 | 42 | my ($specs) = @_; | |||||||||||||||||||||||||||
| 282 | 42 | my ( %lengths, @others ); | |||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | 42 | for my $spec (@$specs) { | |||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||
| 286 | # Put coderefs straight into the 'other' heap. | ||||||||||||||||||||||||||||
| 287 | 67 | ▶ | if ( ref $spec eq 'CODE' ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 288 | 0 | push @others, $spec; | |||||||||||||||||||||||||||
| 289 | } | ||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | # Specifications... | ||||||||||||||||||||||||||||
| 292 | elsif ( ref $spec eq 'HASH' ) { | ||||||||||||||||||||||||||||
| 293 | 67 | ▶ | if ( exists $spec->{length} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 294 | 12 | my $code = $class->create_single_parser(%$spec); | |||||||||||||||||||||||||||
| 295 | my @lengths | ||||||||||||||||||||||||||||
| 296 | = ref $spec->{length} | ||||||||||||||||||||||||||||
| 297 | 1 | ? @{ $spec->{length} } | |||||||||||||||||||||||||||
| 298 | 12 | ▶ | : ( $spec->{length} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 299 | 12 | for my $length (@lengths) { | |||||||||||||||||||||||||||
| 300 | 13 | push @{ $lengths{$length} }, $code; | |||||||||||||||||||||||||||
| 301 | } | ||||||||||||||||||||||||||||
| 302 | } | ||||||||||||||||||||||||||||
| 303 | else { | ||||||||||||||||||||||||||||
| 304 | 55 | push @others, $class->create_single_parser(%$spec); | |||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | # Something else | ||||||||||||||||||||||||||||
| 309 | else { | ||||||||||||||||||||||||||||
| 310 | 0 | croak "Invalid specification in list."; | |||||||||||||||||||||||||||
| 311 | } | ||||||||||||||||||||||||||||
| 312 | } | ||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | 42 | ▶ | while ( my ( $length, $parsers ) = each %lengths ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 315 | 9 | $lengths{$length} = $class->chain_parsers($parsers); | |||||||||||||||||||||||||||
| 316 | } | ||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||
| 318 | 42 | return ( \%lengths, \@others ); | |||||||||||||||||||||||||||
| 319 | } | ||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | 9 | ▶ | sub chain_parsers { | ||||||||||||||||||||||||||
|
Subroutine
chain_parsers: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 322 | 9 | my ( $self, $parsers ) = @_; | |||||||||||||||||||||||||||
| 323 | 9 | ▶ | return $parsers->[0] if @$parsers == 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 324 | 5 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||
| 325 | 5 | my $self = shift; | |||||||||||||||||||||||||||
| 326 | 5 | for my $parser (@$parsers) { | |||||||||||||||||||||||||||
| 327 | 10 | my $rv = $self->$parser(@_); | |||||||||||||||||||||||||||
| 328 | 10 | ▶ | return $rv if defined $rv; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 329 | } | ||||||||||||||||||||||||||||
| 330 | 0 | return undef; | |||||||||||||||||||||||||||
| 331 | 3 | }; | |||||||||||||||||||||||||||
| 332 | } | ||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | 42 | ▶ | sub create_parser { | ||||||||||||||||||||||||||
|
Subroutine
create_parser: called CC=5 SCAR=16.3
Pod
documented
| |||||||||||||||||||||||||||||
| 335 | 42 | my $class = shift; | |||||||||||||||||||||||||||
| 336 | 42 | ▶ | if ( not ref $_[0] ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||
| 338 | # Simple case of single specification as a hash | ||||||||||||||||||||||||||||
| 339 | 0 | return $class->create_single_object(@_); | |||||||||||||||||||||||||||
| 340 | } | ||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||
| 342 | # Let's see if we were given an options block | ||||||||||||||||||||||||||||
| 343 | 42 | my %options; | |||||||||||||||||||||||||||
| 344 | 42 | ▶ | while ( ref $_[0] eq 'ARRAY' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 345 | 46 | my $options = shift; | |||||||||||||||||||||||||||
| 346 | 46 | %options = ( %options, @$options ); | |||||||||||||||||||||||||||
| 347 | } | ||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | # Now, can we create a multi-parser out of the remaining arguments? | ||||||||||||||||||||||||||||
| 350 | 42 | ▶ | if ( ref $_[0] eq 'HASH' or ref $_[0] eq 'CODE' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $_[0] eq "HASH" or ref $_[0] eq "CODE"
MC/DC50%1/2
ref $_[0] eq "HASH" or ref $_[0] eq "CODE"
ref $_[0] eq "HASH"
ref $_[0] eq "CODE"
Truth table66%2/3
A or B
A= ref $_[0] eq "HASH"B= ref $_[0] eq "CODE"
| |||||||||||||||||||||||||||||
| 351 | 22 | return $class->create_multiple_parsers( \%options, @_ ); | |||||||||||||||||||||||||||
| 352 | } | ||||||||||||||||||||||||||||
| 353 | else { | ||||||||||||||||||||||||||||
| 354 | # If it wasn't a HASH or CODE, then it was (ideally) | ||||||||||||||||||||||||||||
| 355 | # a list of pairs describing a single specification. | ||||||||||||||||||||||||||||
| 356 | 20 | return $class->create_multiple_parsers( \%options, {@_} ); | |||||||||||||||||||||||||||
| 357 | } | ||||||||||||||||||||||||||||
| 358 | } | ||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | 24 | require DateTime::Format::Builder::Parser::Dispatch; | |||||||||||||||||||||||||||
| 361 | 24 | require DateTime::Format::Builder::Parser::generic; | |||||||||||||||||||||||||||
| 362 | 24 | require DateTime::Format::Builder::Parser::Quick; | |||||||||||||||||||||||||||
| 363 | 24 | require DateTime::Format::Builder::Parser::Regex; | |||||||||||||||||||||||||||
| 364 | 24 | require DateTime::Format::Builder::Parser::Strptime; | |||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||
| 366 | 24 | 1; | |||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | # ABSTRACT: Parser creation | ||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||
| 370 | __END__ | ||||||||||||||||||||||||||||