]> git.donarmstrong.com Git - perltidy.git/commitdiff
prevent syntax error by breaking dashed barewords
authorSteve Hancock <perltidy@users.sourceforge.net>
Mon, 26 Oct 2020 13:51:53 +0000 (06:51 -0700)
committerSteve Hancock <perltidy@users.sourceforge.net>
Mon, 26 Oct 2020 13:51:53 +0000 (06:51 -0700)
lib/Perl/Tidy/Formatter.pm
local-docs/BugLog.pod

index 083962bedd5c670acad875769ebe15f89546990f..1192a4b0d424991870d4375bcbf6c5ac6804eb9f 100644 (file)
@@ -2860,6 +2860,13 @@ EOM
 
         $binary_bond_strength{'w'}{'R}'} = NO_BREAK;
 
+        # The following two rules prevent a syntax error caused by breaking up
+        # a construction like '{-y}'.  The '-' quotes the 'y' and prevents
+        # it from being taken as a transliteration. We have to keep
+        # token types 'L m w' together to prevent this error.
+        $binary_bond_strength{'L{'}{'m'} = NO_BREAK;
+        $binary_bond_strength{'m'}{'w'} = NO_BREAK;
+
         # use strict requires that bare word and => not be separated
         $binary_bond_strength{'w'}{'=>'} = NO_BREAK;
 
@@ -4511,6 +4518,15 @@ sub respace_tokens {
         # This will be the index of this item in the new array
         my $KK_new = @{$rLL_new};
 
+        my $type     = $item->[_TYPE_];
+        my $is_blank = $type eq 'b';
+
+        # Do not output consecutive blanks. This should not happen, but
+        # is worth checking because later routines make this assumption.
+        if ( $is_blank && $KK_new && $rLL_new->[-1]->[_TYPE_] eq 'b' ) {
+            return;
+        }
+
         # check for a sequenced item (i.e., container or ?/:)
         my $type_sequence = $item->[_TYPE_SEQUENCE_];
         if ($type_sequence) {
@@ -4554,7 +4570,6 @@ sub respace_tokens {
         my $token_length = $length_function->( $item->[_TOKEN_] );
 
         # handle comments
-        my $type       = $item->[_TYPE_];
         my $is_comment = $type eq '#';
         if ($is_comment) {
 
@@ -4579,7 +4594,7 @@ sub respace_tokens {
         # Save the length sum to just AFTER this token
         $item->[_CUMULATIVE_LENGTH_] = $cumulative_length;
 
-        if ( $type && $type ne 'b' && !$is_comment ) {
+        if ( !$is_blank && !$is_comment ) {
             $last_nonblank_type       = $type;
             $last_nonblank_token      = $item->[_TOKEN_];
             $last_nonblank_block_type = $item->[_BLOCK_TYPE_];
@@ -7939,6 +7954,8 @@ sub prepare_for_next_batch {
             $rtoken_vars = $rLL->[$Ktoken_vars];
         }
 
+        my $type = $rtoken_vars->[_TYPE_];
+
         # Check for emergency flush...
         # The K indexes in the batch must always be a continuous sequence of
         # the global token array.  The batch process programming assumes this.
@@ -7954,17 +7971,24 @@ sub prepare_for_next_batch {
             if ( $Ktoken_vars != $Klast + 1 ) {
                 $self->flush_batch_of_CODE();
             }
+
+           # Do not output consecutive blank tokens ... this should not
+           # happen, but it is worth checking.  Later code can then make the
+           # simplifying assumption that blank tokens are not consecutive.
+            elsif ( $type eq 'b' && $types_to_go[$max_index_to_go] eq 'b' ) {
+                return;
+            }
         }
 
         ++$max_index_to_go;
-        $K_to_go[$max_index_to_go] = $Ktoken_vars;
+        $K_to_go[$max_index_to_go]     = $Ktoken_vars;
+        $types_to_go[$max_index_to_go] = $rtoken_vars->[_TYPE_];
 
         $old_breakpoint_to_go[$max_index_to_go]    = 0;
         $forced_breakpoint_to_go[$max_index_to_go] = 0;
         $mate_index_to_go[$max_index_to_go]        = -1;
 
         my $token = $tokens_to_go[$max_index_to_go] = $rtoken_vars->[_TOKEN_];
-        my $type  = $types_to_go[$max_index_to_go]  = $rtoken_vars->[_TYPE_];
         my $ci_level = $ci_levels_to_go[$max_index_to_go] =
           $rtoken_vars->[_CI_LEVEL_];
 
index 880394238ee84e57ad5bd5402a774acde249ca5f..1551c05ca01797f5e3e54b56da92f1c11466d796 100644 (file)
@@ -2,6 +2,67 @@
 
 =over 4
 
+
+=item b<Prevent syntax error by breaking dashed barewords>
+
+In random testing with the following test snippet with the -extrude option
+
+  my %var;
+  {
+      $var{-y}  = 1;
+      $var{-q}  = 1;
+      $var{-qq} = 1;
+      $var{-m}  = 1;
+      $var{y}   = 1;
+      $var{q}   = 1;
+      $var{qq}  = 1;
+      $var{m}   = 1;
+  }
+
+a syntax error was created in the resulting file when newlines were placed
+before or after the dashes.  It is necessary to always keep a dash on the same
+line with its surrounding tokens.  A rule was added to do this.  The new
+'extruded' result for the above snippet is:
+
+  my%var
+  ;
+  {
+  $var{-y}
+  =
+  1
+  ;
+  $var{-q}
+  =
+  1
+  ;
+  $var{-qq}
+  =
+  1
+  ;
+  $var{-m}
+  =
+  1
+  ;
+  $var{y}
+  =
+  1
+  ;
+  $var{q}
+  =
+  1
+  ;
+  $var{qq}
+  =
+  1
+  ;
+  $var{m}
+  =
+  1
+  ;
+  }
+
+This update was added 25 Oct 2020.
+
 =item b<more types of severe errors will prevent formatting>
 
 Files for which 'severe errors' are found have always been output verbatim
@@ -28,7 +89,7 @@ along with an error message. But now it is just output verbatim as
 {{{{
 }}
 
-along with an error message.
+along with an error message.  This update was added 25 Oct 2020.
 
 =item b<added 'state' as keyword>