]> git.donarmstrong.com Git - perltidy.git/commitdiff
change method for parsing sub signatures
authorSteve Hancock <perltidy@users.sourceforge.net>
Sun, 18 Oct 2020 23:59:49 +0000 (16:59 -0700)
committerSteve Hancock <perltidy@users.sourceforge.net>
Sun, 18 Oct 2020 23:59:49 +0000 (16:59 -0700)
lib/Perl/Tidy/Tokenizer.pm
local-docs/BugLog.pod
t/filter_example.t
t/snippets/switch1.in
t/snippets11.t

index 4c4b0032f3896ed4c414bf8f8bef16fc11c40e7b..46c79f26085c4b3733fc9a99c25597fa4e93b3d4 100644 (file)
@@ -2250,8 +2250,8 @@ sub prepare_for_a_new_file {
             }
 
             # ATTRS: check for a ':' which introduces an attribute list
-            # (this might eventually get its own token type)
-            elsif ( $statement_type =~ /^sub\b/ ) {
+            # either after a 'sub' keyword or within a paren list
+            elsif ($statement_type =~ /^sub\b/ ) {
                 $type              = 'A';
                 $in_attribute_list = 1;
             }
@@ -2281,6 +2281,16 @@ sub prepare_for_a_new_file {
                 $type = 'J';
             }
 
+            # Withing a signature.  For example,
+            # from 't/filter_example.t':
+            #    method foo4 ( $class: $bar ) { $class->bar($bar) }
+            elsif ( !is_balanced_closing_container(QUESTION_COLON)
+                && $paren_type[$paren_depth] =~ /^sub\b/ )
+            {
+                $type              = 'A';
+                $in_attribute_list = 1;
+            }
+
             # otherwise, it should be part of a ?/: operator
             else {
                 ( $type_sequence, $indent_flag ) =
@@ -6690,8 +6700,8 @@ sub scan_identifier_do {
         # particular, we stop if we see any nested parens, braces, or commas.
         # Also note, a valid prototype cannot contain any alphabetic character
         #  -- see https://perldoc.perl.org/perlsub
-        # But it appears that an underscore may be valid now, so this is
-        # using [A-Za-z] instead of \w 
+        # old PROTO:
+        # $input_line =~ m/\G(\s*\([^\)\(\}\{\,#]*\))?  # PROTO
         my $saw_opening_paren = $input_line =~ /\G\s*\(/;
         if (
             $input_line =~ m/\G(\s*\([^\)\(\}\{\,#A-Za-z]*\))?  # PROTO
index 0d56a83314b59508be0a70a319a23b1b0c80f844..7ce22c09ccf1ac026b1ec407869e61ff84099d16 100644 (file)
@@ -2,19 +2,33 @@
 
 =item b<sub signatures no longer parsed with prototypes>
 
-Simple signatures were being parsed with the code for prototypes, which
-prevented them from being formatted with the usual formatting rules.  All
-signatures are now formatted separately from prototypes with the normal
-formatting rules. For example:
+Simple signatures (those without commas) were being parsed with code originally
+written for prototypes. This prevented them from being formatted with the
+usual formatting rules.  This was changed so that all signatures are now
+formatted with the normal formatting rules. For example:
 
- # Old, intput and after formatting:
+ # Old, input and after formatting:
  sub t123 ($list=wantarray) { $list ? "list" : "scalar" }
 
  # New, after formatting
  sub t123 ( $list = wantarray ) { $list ? "list" : "scalar" }
 
 Notice that some spaces have been introduced within the signature. Previously
-the contents of the parens was left unchanged.
+the contents of the parens not changed unless the parens contained a list.
+
+This change introduced a problem parsing extended syntax within
+signatures which has been fixed. In the following snippet, the ':' caused
+a parsing error which was fixed.
+
+  # perltidy -sal='method'
+  method foo4 ( $class : $bar, $bubba ) { $class->bar($bar) }
+
+The ':' here is given a type of 'A'. This may be used to change
+the spacing around it. For example:
+
+  # perltidy -sal='method' -nwls='A'
+  method foo4 ( $class: $bar, $bubba ) { $class->bar($bar) }
+
 
 =item b<fix parsing problem with $#>
 
index 825c2c148459663b9bd4ab00084cadf8b472f404..bdecf449f5270d18148ee596c3be2e07e518d698 100755 (executable)
@@ -24,7 +24,7 @@ use Method::Signatures::Simple;
 
  # change invocant name
     method 
-foo4 ($class: $bar) { $class->bar($bar) }
+foo4 ( $class : $bar ) { $class->bar($bar) }
 ENDS
 
 my $expect = <<'ENDE';
@@ -42,7 +42,7 @@ method foo3 : lvalue {
 }
 
 # change invocant name
-method foo4 ($class: $bar) { $class->bar($bar) }
+method foo4 ( $class : $bar ) { $class->bar($bar) }
 ENDE
 
 my $output;
index 908b9761dee7eab9112c5551031db3b2bc791f71..ce31bde90b4c6ce2a06ef677fcfee238bf29b487 100644 (file)
@@ -1,4 +1,4 @@
-sub classify_digit($digit)
+sub classify_digit ($digit)
   { switch($digit)
     { case 0 { return 'zero' } case [ 2, 4, 6, 8 ]{ return 'even' }
         case [ 1, 3, 4, 7, 9 ]{ return 'odd' } case /[A-F]/i { return 'hex' } }
index 2e869f867898ebda2ebb65050ba063ad7e5b82aa..b417965e5cede555bb2a930625183b157e5970c2 100644 (file)
@@ -95,7 +95,7 @@ $a->();
 ----------
 
         'switch1' => <<'----------',
-sub classify_digit($digit)
+sub classify_digit ($digit)
   { switch($digit)
     { case 0 { return 'zero' } case [ 2, 4, 6, 8 ]{ return 'even' }
         case [ 1, 3, 4, 7, 9 ]{ return 'odd' } case /[A-F]/i { return 'hex' } }