Alt-CWB-ambs
view release on metacpan or search on metacpan
lib/CWB/CEQL/Parser.pm view on Meta::CPAN
=item I<$result> = I<$self>->B<Try>(I<$rule>, I<$input>);
Tentatively apply rule I<$rule> to the input string. If I<$input> is parsed
successfully, B<Try> returns the translated version I<$result> (or an
arbitrary data structure such as a parse tree for I<$input>) just as B<Call>
would. If parsing fails, B<Try> does not abort but simply returns B<undef>,
ignoring any error messages generated during the attempt. In addition, the
call stack is restored and all parameters are reset to their previous values,
so that parsing can continue as if nothing had happened (note, however, that
this is based on flat backup copies, so complex data structures may have been
altered destructively).
=cut
sub Try {
confess 'Usage: $result = $self->Try($rule, $input);'
unless @_ == 3;
my ($self, $rule, $input) = @_;
confess "Sorry, we're not parsing yet"
unless defined $self->{INPUT};
## make flat backup copies of important data structures and ensure they are restored upon return
## (this is not completely safe, but should undo most changes that a failed parse may have made)
my $back_param = [ @{$self->{PARAM}} ];
my $back_callstack = [ @{$self->{CALLSTACK}} ];
my ($back_groups, $back_current_group, $back_groupstack) = (undef, undef, undef);
if (defined $self->{GROUPS}) {
$back_groups = [ @{$self->{GROUPS}} ];
$back_current_group = [ @{$back_groups->[0]} ]
if @$back_groups > 0;
}
if (defined $self->{GROUPSTACK}) {
$back_groupstack = [ @{$self->{GROUPSTACK}} ];
}
my $result = eval { $self->Call($rule, $input) };
## if parsing failed, restore internal data structures from backup copies
if (not defined $result) {
$self->{PARAM} = $back_param;
$self->{CALLSTACK} = $back_callstack;
$self->{GROUPS} = $back_groups;
if (defined $back_groups and defined $back_current_group) {
$self->{GROUPS}->[0] = $back_current_group;
}
$self->{GROUPSTACK} = $back_groupstack;
}
script/cwb-regedit view on Meta::CPAN
}
}
else {
die "Command $COMMAND is not valid at this point. Aborted.\n";
}
}
## write back if any changes have been made
if ($CHANGES > 0) {
my $regfile = $reg->filename;
# make a backup first
system "cp", "-p", $regfile, "$regfile~"; # ignore errors (e.g. if we don't have permissions to make a copy)
$reg->write;
print "Changes saved to ", $reg->filename, "\n";
}
##
## subroutines
##
( run in 0.639 second using v1.01-cache-2.11-cpan-49f99fa48dc )