From 15a4ef28e498c31ebc67dce7703e323088504230 Mon Sep 17 00:00:00 2001 From: Steve Hancock Date: Fri, 7 Jul 2023 20:04:52 -0700 Subject: [PATCH] fix c231 --- dev-bin/run_tokenizer_tests.pl.data | 3 ++ lib/Perl/Tidy/Tokenizer.pm | 45 ++++++++++++++++++++--------- 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/dev-bin/run_tokenizer_tests.pl.data b/dev-bin/run_tokenizer_tests.pl.data index 0c6cb7b1..69bf15bd 100644 --- a/dev-bin/run_tokenizer_tests.pl.data +++ b/dev-bin/run_tokenizer_tests.pl.data @@ -162,6 +162,9 @@ print "str='$str'\n"; ==> c208.in <== $1x$2 +==> c231.in <== +print {LABEL: print "Hello "; STDOUT} "World\n"; + ==> git82.in <== say $ diff --git a/lib/Perl/Tidy/Tokenizer.pm b/lib/Perl/Tidy/Tokenizer.pm index 4ebab03c..64b288fc 100644 --- a/lib/Perl/Tidy/Tokenizer.pm +++ b/lib/Perl/Tidy/Tokenizer.pm @@ -4512,7 +4512,7 @@ EOM # like 'sub : lvalue' ? && !$self->sub_attribute_ok_here( $tok_kw, $next_nonblank_token, $i_next ) - && label_ok() + && new_statement_ok() ) { if ( $tok !~ /[A-Z]/ ) { @@ -6656,27 +6656,44 @@ sub operator_expected { sub new_statement_ok { - # return true if the current token can start a new statement - # USES GLOBAL VARIABLES: $last_nonblank_type + # Returns: + # true if a new statement can begin here + # false otherwise - return label_ok() # a label would be ok here + # USES GLOBAL VARIABLES: $last_nonblank_token, $last_nonblank_type, + # $brace_depth, $rbrace_type - || $last_nonblank_type eq 'J'; # or we follow a label + # Uses: + # - See if a 'class' statement can occur here + # - See if a keyword begins at a new statement; i.e. is an 'if' a + # block if or a trailing if? Also see if 'format' starts a statement. + # - Decide if a ':' is part of a statement label (not a ternary) -} ## end sub new_statement_ok + # Curly braces are tricky because some small blocks do not get marked as + # blocks.. -sub label_ok { + # if it follows an opening curly brace.. + if ( $last_nonblank_token eq '{' ) { - # Decide if a bare word followed by a colon here is a label - # USES GLOBAL VARIABLES: $last_nonblank_token, $last_nonblank_type, - # $brace_depth, $rbrace_type + # The safe thing is to return true in all cases because: + # - a ternary ':' cannot occur here + # - an 'if' here, for example, cannot be a trailing if + # See test case c231 for an example. + # This works but could be improved, if necessary, by returning + # 'false' at obvious non-blocks. + return 1; + } - # if it follows an opening or closing code block curly brace.. - if ( ( $last_nonblank_token eq '{' || $last_nonblank_token eq '}' ) + # if it follows a closing code block curly brace.. + elsif ($last_nonblank_token eq '}' && $last_nonblank_type eq $last_nonblank_token ) { - # it is a label if and only if the curly encloses a code block + # a new statement can follow certain closing block braces ... + # FIXME: The following has worked well but returns true in some cases + # where it really should not. We could fix this by either excluding + # certain blocks, like sort/map/grep/eval/asub or by just including + # certain blocks. return $rbrace_type->[$brace_depth]; } @@ -6685,7 +6702,7 @@ sub label_ok { else { return ( $last_nonblank_type eq ';' || $last_nonblank_type eq 'J' ); } -} ## end sub label_ok +} ## end sub new_statement_ok sub code_block_type { -- 2.39.5