]> git.donarmstrong.com Git - perltidy.git/commitdiff
Fix bad guess of divide vs pattern
authorSteve Hancock <perltidy@users.sourceforge.net>
Wed, 13 Jan 2021 16:34:48 +0000 (08:34 -0800)
committerSteve Hancock <perltidy@users.sourceforge.net>
Wed, 13 Jan 2021 16:34:48 +0000 (08:34 -0800)
lib/Perl/Tidy/Tokenizer.pm
local-docs/BugLog.pod

index 30b2c30f0ceb5fb9b3515fd657ecbc51eec1d920..63ca4710760e452fe4a4772dc5c8c473ae2c21cc 100644 (file)
@@ -5797,6 +5797,20 @@ sub guess_if_pattern_or_conditional {
     return ( $is_pattern, $msg );
 }
 
+my %is_known_constant;
+my %is_known_function;
+
+BEGIN {
+
+    # Constants like 'pi' in Trig.pm are common
+    my @q = qw(pi pi2 pi4 pip2 pip4);
+    @{is_known_constant}{@q} = (1) x scalar(@q);
+
+    # parenless calls of 'ok' are common
+    @q = qw( ok );
+    @{is_known_function}{@q} = (1) x scalar(@q);
+}
+
 sub guess_if_pattern_or_division {
 
     # this routine is called when we have encountered a / following an
@@ -5881,15 +5895,20 @@ sub guess_if_pattern_or_division {
 
                     # Both pattern and divide can work here...
 
-                    # A very common bare word in math expressions is 'pi'
-                    if ( $last_nonblank_token eq 'pi' ) {
-                        $msg .= "division (pattern works too but saw 'pi')\n";
+                    # Increase weight of divide if a pure number follows
+                    $divide_expected += $next_token =~ /^\d+$/;
+
+                    # Check for known constants in the numerator, like 'pi'
+                    if ( $is_known_constant{$last_nonblank_token} ) {
+                        $msg .=
+"division (pattern works too but saw known constant '$last_nonblank_token')\n";
                         $is_pattern = 0;
                     }
 
                     # A very common bare word in pattern expressions is 'ok'
-                    elsif ( $last_nonblank_token eq 'ok' ) {
-                        $msg .= "pattern (division works too but saw 'ok')\n";
+                    elsif ( $is_known_function{$last_nonblank_token} ) {
+                        $msg .=
+"pattern (division works too but saw '$last_nonblank_token')\n";
                         $is_pattern = 1;
                     }
 
index f653ce967b0f9b107a8e6501b2e39d5e72f3e739..3fa7aca5257166b42793f17fec3ace427770324d 100644 (file)
@@ -1,7 +1,30 @@
-=head1 Issues fixed after release 20201207
+=head1 Issues fixed after release 20210111
 
 =over 4
 
+=item B<Fixed incorrect guess of division vs pattern>
+
+A syntax error was produced in random testing when perltidy was fed
+the following line:
+
+    sub _DR () { pi2 /360 } sub _RD () { 360 /pi2 }
+
+The bareword 'pi2' was not recognized and the text between the two slashes
+was a taken as a possible pattern argument in a parenless call to pi2.  Two
+fixes were made to fix this. Perltidy looks for 'pi' but not 'pi2', so the
+first fix was to expand its table to include all variations of 'pi' in Trig.pm.
+Second, the fact that the first slash was followed by a number should have
+tipped the guess to favor division, so this was fixed.
+As it was, a backup spacing rule was used, which favored a pattern.
+
+The formatted result is now
+
+    sub _DR () { pi2 / 360 }
+    sub _RD () { 360 / pi2 }
+
+This update was made 13 Jan 2021.
+
+
 =item B<Correct formula for estimating line length with -wn option>
 
 A formula used to estimating maximum line length when the -wn option is set was
@@ -28,6 +51,11 @@ caused an oscillation between two states.  An unusual feature which contributed
 to the problem is the very large ci value. This is fixed in a patch made 12 Jan
 2021.
 
+=back
+
+=head1 Issues fixed after release 20201207
+
+=over 4
 
 =item B<Improve indentation of multiline qw quotes when -xci flag is set>