From 606930fb294b6fb636c323d654f7c5c324b6d7dd Mon Sep 17 00:00:00 2001 From: Steve Hancock Date: Wed, 15 Mar 2023 17:27:34 -0700 Subject: [PATCH] output file verbatim for certain lexical sub names Lexical (my) subs which use the builtin names of certain quote-like operators may cause problems in this version of perltidy. For example 'my sub s {...}' may cause parsing errors. If something like this is encountered, then formatting will not be attempted; the file will be output verbatim. --- lib/Perl/Tidy/Tokenizer.pm | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/Perl/Tidy/Tokenizer.pm b/lib/Perl/Tidy/Tokenizer.pm index 0c861a03..656964ec 100644 --- a/lib/Perl/Tidy/Tokenizer.pm +++ b/lib/Perl/Tidy/Tokenizer.pm @@ -163,6 +163,7 @@ BEGIN { _in_end_ => $i++, _in_format_ => $i++, _in_error_ => $i++, + _in_trouble_ => $i++, _in_pod_ => $i++, _in_skipped_ => $i++, _in_attribute_list_ => $i++, @@ -436,6 +437,7 @@ sub new { # _in_pod_ flag set if we are in pod documentation # _in_skipped_ flag set if we are in a skipped section # _in_error_ flag set if we saw severe error (binary in script) + # _in_trouble_ set if we saw a troublesome lexical like 'my sub s' # _in_data_ flag set if we are in __DATA__ section # _in_end_ flag set if we are in __END__ section # _in_format_ flag set if we are in a format description @@ -457,6 +459,7 @@ sub new { $self->[_in_end_] = 0; $self->[_in_format_] = 0; $self->[_in_error_] = 0; + $self->[_in_trouble_] = 0; $self->[_in_pod_] = 0; $self->[_in_skipped_] = 0; $self->[_in_attribute_list_] = 0; @@ -647,7 +650,12 @@ sub report_tokenization_errors { # set severe error flag if tokenizer has encountered file reading problems # (i.e. unexpected binary characters) - my $severe_error = $self->[_in_error_]; + # or code which may not be formatted correctly (such as 'my sub q') + # The difference between _in_error_ and _in_trouble_ is that + # _in_error_ stops the tokenizer immediately whereas + # _in_trouble_ lets the tokenizer finish so that all errors are seen + # Both block formatting and cause the input stream to be output verbatim. + my $severe_error = $self->[_in_error_] || $self->[_in_trouble_]; my $maxle = $self->[_rOpts_maximum_level_errors_]; my $maxue = $self->[_rOpts_maximum_unexpected_errors_]; @@ -8591,6 +8599,10 @@ EOM warning( "'my' sub '$subname' matches a builtin name and may not be handled correctly in this perltidy version.\n" ); + + # This may end badly, it is safest to block formatting + # For an example, see perl527/lexsub.t (issue c203) + $tokenizer_self->[_in_trouble_] = 1; } } else { -- 2.39.5