package DateTime::Format::Builder::Parser; use strict; use vars qw( $VERSION ); use Carp qw( croak ); use Params::Validate qw( validate SCALAR CODEREF UNDEF ARRAYREF ); use Scalar::Util qw( weaken ); =head1 NAME DateTime::Format::Builder::Parser - Parser creation =head1 SYNOPSIS my $class = 'DateTime::Format::Builder::Parser'; my $parser = $class->create_single_parser( %specs ); =head1 DESCRIPTION This is a utility class for L that handles creation of parsers. It is to here that C delegates most of its responsibilities. =cut $VERSION = '0.77'; =head1 CONSTRUCTORS =cut sub on_fail { my ($self, $input, $parent) = @_; my $maker = $self->maker; if ( $maker and $maker->can( 'on_fail' ) ) { $maker->on_fail( $input ); } else { croak __PACKAGE__.": Invalid date format: $input"; } } sub no_parser { croak "No parser set for this parser object."; } sub new { my $class = shift; $class = ref($class)||$class; my $i = 0; my $self = bless { on_fail => \&on_fail, parser => \&no_parser, }, $class; return $self; } sub maker { $_[0]->{maker} } sub set_maker { my $self = shift; my $maker = shift; $self->{maker} = $maker; weaken $self->{maker} if ref $self->{maker}; return $self; } sub fail { my ($self, $parent, $input) = @_; $self->{on_fail}->( $self, $input, $parent ); } sub parse { my ( $self, $parent, $input, @args ) = @_; my $r = $self->{parser}->( $parent, $input, @args ); $self->fail( $parent, $input ) unless defined $r; $r; } sub set_parser { my ($self, $parser) = @_; $self->{parser} = $parser; $self; } sub set_fail { my ($self, $fail) = @_; $self->{on_fail} = $fail; $self; } =head1 METHODS There are two sorts of methods in this class. Those used by parser implementations and those used by C. It is generally unlikely the user will want to use any of them. They are presented, grouped according to use. =head2 Parameter Handling (implementations) These methods allow implementations to have validation of their arguments in a standard manner and due to C's impelementation, these methods also allow C to determine which implementation to use. =cut my @callbacks = qw( on_match on_fail postprocess preprocess ); { =head3 Common parameters These parameters appear for all parser implementations. These are primarily documented in L. =over 4 =item * B =item * B =item * B =item * B =item * B