From: Steve Hancock Date: Thu, 18 Aug 2022 00:29:59 +0000 (-0700) Subject: copy input to output verbatim on detection of programming error X-Git-Tag: 20220613.05~43 X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=e7d791fd666cc62a40f90676748a66145d74d983;p=perltidy.git copy input to output verbatim on detection of programming error --- diff --git a/lib/Perl/Tidy/Formatter.pm b/lib/Perl/Tidy/Formatter.pm index befd406e..eb1bd539 100644 --- a/lib/Perl/Tidy/Formatter.pm +++ b/lib/Perl/Tidy/Formatter.pm @@ -125,6 +125,31 @@ EOM return; } ## end sub Fault +sub Fault_Warn { + my ($msg) = @_; + + # This is the same as Fault except that it calls Warn instead of Die + # and returns. + my ( $package0, $filename0, $line0, $subroutine0 ) = caller(0); + my ( $package1, $filename1, $line1, $subroutine1 ) = caller(1); + my ( $package2, $filename2, $line2, $subroutine2 ) = caller(2); + my $input_stream_name = get_input_stream_name(); + + Warn(<respace_tokens(); + $severe_error = $self->respace_tokens(); + if ($severe_error) { + $self->dump_verbatim(); + $self->wrapup(); + return; + } $self->set_excluded_lp_containers(); @@ -6276,6 +6306,8 @@ sub respace_tokens { # This routine is called once per file to do as much formatting as possible # before new line breaks are set. + # Returns 1 on a severe error which requires processing to terminate + # This routine makes all necessary and possible changes to the tokenization # after the initial tokenization of the file. This is a tedious routine, # but basically it consists of inserting and deleting whitespace between @@ -6327,13 +6359,12 @@ sub respace_tokens { # of K values. Or a line of CODE may have been mis-marked as # something else. There is no good way to continue after such an # error. - # FIXME: Calling Fault will produce zero output; it would be best to - # find a way to dump the input file. if ( defined($last_K_out) ) { if ( $Kfirst != $last_K_out + 1 ) { - Fault( + Fault_Warn( "Program Bug: last K out was $last_K_out but Kfirst=$Kfirst" ); + return 1; } } else { @@ -6493,9 +6524,9 @@ sub respace_tokens { DEVEL_MODE && $self->check_token_array(); # update the token limits of each line - $self->resync_lines_and_tokens(); + my $severe_error = $self->resync_lines_and_tokens(); - return; + return $severe_error; } ## end sub respace_tokens sub respace_tokens_inner_loop { @@ -7815,6 +7846,8 @@ sub resync_lines_and_tokens { # since they have probably changed due to inserting and deleting blanks # and a few other tokens. + # Returns 1 on a severe error which requires processing to terminate + # This is the next token and its line index: my $Knext = 0; my $Kmax = defined($Klimit) ? $Klimit : -1; @@ -7939,11 +7972,10 @@ EOM # relinking lines and tokens after the tokens have been respaced. A fault # here indicates some kind of bug has been introduced into the above loops. # There is not good way to keep going; we better stop here. - # FIXME: Calling Fault will produce zero output. it would be best to find - # a way to dump the input file. if ( $Knext <= $Kmax ) { - - Fault("unexpected tokens at end of file when reconstructing lines"); + Fault_Warn( + "unexpected tokens at end of file when reconstructing lines"); + return 1; } $self->[_rKrange_code_without_comments_] = \@Krange_code_without_comments;