From: Steve Hancock Date: Wed, 7 Jul 2021 00:42:48 +0000 (-0700) Subject: Fix error parsing '%#' and similar combinations X-Git-Tag: 20210625.02~6 X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=e233d41f57738259e8820a65a5787081ce2b5aec;p=perltidy.git Fix error parsing '%#' and similar combinations --- diff --git a/lib/Perl/Tidy/Tokenizer.pm b/lib/Perl/Tidy/Tokenizer.pm index 99b0a919..0c224083 100644 --- a/lib/Perl/Tidy/Tokenizer.pm +++ b/lib/Perl/Tidy/Tokenizer.pm @@ -2580,7 +2580,10 @@ EOM '*' => sub { # typeglob, or multiply? if ( $expecting == UNKNOWN && $last_nonblank_type eq 'Z' ) { - if ( $next_type ne 'b' && $next_type ne '(' ) { + if ( $next_type ne 'b' + && $next_type ne '(' + && $next_type ne '#' ) # Fix c036 + { $expecting = TERM; } } @@ -6818,30 +6821,39 @@ sub scan_identifier_do { # howdy::123::bubba(); # } + elsif ( $tok eq '#' ) { - # $# and POSTDEFREF ->$# - elsif ( - ( $tok eq '#' ) - && ( $identifier =~ /\$$/ ) + # side comment or identifier? + if ( - # a # inside a prototype or signature can only start a comment - && !$in_prototype_or_signature - ) - { - # A '#' starts a comment if it follows a space. For example, - # the following is equivalent to $ans=40. - # my $ # - # ans = 40; - if ($last_tok_is_blank) { - $type = 'i'; - if ( $id_scan_state eq '$' ) { $type = 't' } + # A '#' starts a comment if it follows a space. For example, + # the following is equivalent to $ans=40. + # my $ # + # ans = 40; + !$last_tok_is_blank + + # a # inside a prototype or signature can only start a + # comment + && !$in_prototype_or_signature + + # these are valid punctuation vars: *# %# @# $# + # May also be '$#array' or POSTDEFREF ->$# + && ( $identifier =~ /^[\%\@\$\*]$/ || $identifier =~ /\$$/ ) + + ) + { + $identifier .= $tok; # keep same state, a $ could follow + } + else { + + # otherwise it is a side comment + if ( $identifier eq '->' ) { } + elsif ( $id_scan_state eq '$' ) { $type = 't' } + else { $type = 'i' } $i = $i_save; $id_scan_state = ''; last; } - - # May be '$#' or '$#array' - $identifier .= $tok; # keep same state, a $ could follow } elsif ( $tok eq '{' ) { diff --git a/local-docs/BugLog.pod b/local-docs/BugLog.pod index 4703e7fe..c34d60a2 100644 --- a/local-docs/BugLog.pod +++ b/local-docs/BugLog.pod @@ -2,6 +2,35 @@ =over 4 +=item B + +Perltidy was correctly distinguishing between '$#' and '$ #' but not between +'@#' and '@ #' and '%#' and '% #'. The coding for parsing these types of +expressions has been corrected. Some simple examples: + + # this is a valid program, '%#' is a punctuation variable + %# = ( foo => 'bar', baz => 'buz' ); + print keys(%#), "\n"; + + # but this is a syntax error (space before # makes a side comment) + # (perltidy was ignoring the space and forming '%#' here) + % # = ( foo => 'bar', baz => 'buz' ); + print keys(%#), "\n"; + + # this is a valid program, '@#' is a punctuation variable + @# = ( foo , 'bar', baz , 'buz' ); + print @#, "\n"; + + # this is a valid program, the space makes the '#' a side comment + # perltidy formed %# here, causing an error + % # + var = ( foo => 'bar', baz => 'buz' ); + print keys(%var), "\n"; + +This fixes case c036. + +6 Jul 2021. + =item B The following test script caused an error when perltidy did not correctly parse