Debian Codegraph @

lib/Perl/Tidy/Sweetened/Keyword/Block.pm

raw symbols SCIP
1 package Perl::Tidy::Sweetened::Keyword::Block;
2
3 # ABSTRACT: Perl::Tidy::Sweetened filter plugin to define new subroutine and class keywords
4
5 use 5.010; # Needed for balanced parens matching with qr/(?-1)/
6 use strict;
7 use warnings;
8 use Carp;
9 $|++;
10
11 our $VERSION = '1.20';
12
13 # Regex to match balanced params. Reproduced from Regexp::Common to avoid
14 # adding a non-core dependency.
15 # $RE{balanced}{-parens=>'()'};
16 # The (?-1) construct requires 5.010
17 our $Paren = '(?:((?:\((?:(?>[^\(\)]+)|(?-1))*\))))';
18
19 sub new {
20 my ( $class, %args ) = @_;
21 croak 'keyword not specified' if not exists $args{keyword};
22 croak 'marker not specified' if not exists $args{marker};
23 croak 'replacement not specified' if not exists $args{replacement};
24 $args{clauses} = [] unless exists $args{clauses};
25 return bless {%args}, $class;
26 }
27
28 sub keyword { return $_[0]->{keyword} }
29 sub marker { return $_[0]->{marker} }
30 sub replacement { return $_[0]->{replacement} }
31
32 sub emit_placeholder {
33 my ( $self, $subname, $brace, $clauses ) = @_;
34
35 # Store the signature and returns() for later use
36 my $id = sprintf "%03d", $self->{counter}++;
37 $self->{store_clause}->{$id} = $clauses;
38 $self->{store_sub}->{$id} = $subname;
39
40 # Turns 'my_method_name' into 'SUB004hod_name'
41 my $marker = $self->marker . $id;
42 substr( $subname, 0, length($marker), $marker );
43
44 return sprintf '%s %s %s', $self->replacement, $marker, $brace;
45 }
46
47 sub emit_keyword {
48 my ( $self, $brace, $id ) = @_;
49
50 # Get the signature and returns() from store
51 my $clauses = $self->{store_clause}->{$id};
52
53 my $subname = $self->{store_sub}->{$id};
54
55 # Combine clauses (parameter list, returns(), etc) into a string separate
56 # each with a space and lead with a space if there are any
57 my $clause = join ' ', grep { length $_ } @$clauses;
58
59 # FIXME: This forces space between sub/func/method name and clauses list
60 # (ignores perltidy settings)
61 $clause = ' ' . $clause if length $clause;
62
63 return sprintf '%s %s%s%s', $self->keyword, $subname, $clause, $brace;
64 }
65
66 sub emit_csc {
67 my ( $self, $id, $cscp ) = @_;
68 return sprintf "%s %s %s", $cscp, $self->keyword, $self->{store_sub}->{$_};
69 }
70
71 sub clauses {
72 my $self = shift;
73
74 # Create a regex (as a string) for all the clauses (ie, parameter list,
75 # returns(), etc).
76 my $clause_re = '';
77 my $i = 0;
78 for my $clause ( @{ $self->{clauses} } ) {
79 $clause =~ s{PAREN}{$Paren}g;
80
81 $clause_re .= "(?<clause_$i> $clause ) \\s* \n";
82 $i++;
83 }
84
85 return $clause_re;
86 }
87
88 sub identifier { # method or package identifier
89 my $self = shift;
90
91 return '[$@%]? \w+ (?: ::\w+ )*'; # words, possibly with twigil and/or separated by ::
92 }
93
94 sub prefilter {
95 my ( $self, $code ) = @_;
96 my $keyword = $self->keyword;
97 my $subname = $self->identifier;
98
99 $code =~ s{
100 ^\s*\K # okay to have leading whitespace (preserve)
101 $keyword \s+ # the "func/method" keyword
102 (?<subname> $subname) # the function name or class name (needs ::)
103 (?!\w|\s*=>) \s* # check to make sure this isn't a sub call with params
104 @{[ $self->clauses ]} # any clauses defined (ie, a parameter list)
105 (?<brace> .*?) # anything else (ie, comments) including brace
106 $
107 }{
108 my $i = 0;
109 my $clauses = [];
110 while( exists $+{"clause_$i"} ){
111 ## warn "# clause_$i: " . $+{"clause_$i"} . "\n";
112 push @$clauses, $+{"clause_$i"};
113 $i++;
114 }
115 $self->emit_placeholder( $+{subname}, $+{brace}, $clauses )
116 }egmx;
117
118 return $code;
119 }
120
121 sub postfilter {
122 my ( $self, $code, $args ) = @_;
123 my $marker = $self->marker;
124 my $replacement = $self->replacement;
125 my $subname = $self->identifier;
126 my @ids;
127
128 # Convert back to method
129 $code =~ s{
130 ^\s*\K # preserve leading whitespace
131 $replacement \s+ # keyword was converted to sub/package
132 $marker #
133 (?<id> \d\d\d) # the identifier
134 [\w:]* \b # the rest of the orignal sub/package name
135 (?<newline> \n? \s* ) # possible newline and indentation
136 (?<brace> .*? ) [ ]* # opening brace on followed orig comments
137 [ ]* # trailing spaces (not all whitespace)
138 }{
139 push @ids, $+{id};
140 $self->emit_keyword( $+{newline} . $+{brace}, $+{id} );
141 }egmx;
142
143 # Restore the orig sub name when inserted via the -csc flag
144 my $cscp = $args->{'-cscp'} || '## end';
145 $code =~ s{
146 \Q${cscp}\E \s sub \s ${marker} $_
147 }{
148 $self->emit_csc( $_, $cscp );
149 }egx for @ids;
150
151 return $code;
152 }
153
154 1;
155
156 __END__
157
158 =pod
159
160 =head1 NAME
161
162 Perl::Tidy::Sweetened::Keyword::Block - Perl::Tidy::Sweetened filter plugin to define new subroutine and class keywords
163
164 =head1 VERSION
165
166 version 1.20
167
168 =head1 SYNOPSIS
169
170 our $plugins = Perl::Tidy::Sweetened::Pluggable->new();
171
172 $plugins->add_filter(
173 Perl::Tidy::Sweetened::Keyword::Block->new(
174 keyword => 'method',
175 marker => 'METHOD',
176 replacement => 'sub',
177 clauses => [ 'PAREN?', '(returns \s* PAREN)?' ],
178 ) );
179
180 =head1 DESCRIPTION
181
182 This is a Perl::Tidy::Sweetened filter which enables the definition of
183 arbitrary keywords for subroutines with any number of potential signature
184 definitions. New accepts:
185
186 =over 4
187
188 =item keyword
189
190 keyword => 'method'
191
192 Declares a new keyword (in this example the "method" keyword).
193
194 =item marker
195
196 marker => 'METHOD'
197
198 Provides a text marker to be used to flag the new keywords during
199 C<prefilter>. The source code will be filtered prior to formatting by
200 Perl::Tidy such that:
201
202 method foo {
203 }
204
205 is turned into:
206
207 sub foo { # __METHOD 1
208 }
209
210 =item replacement
211
212 replacement => 'sub'
213
214 Will convert the keyword to a C<sub> as shown above.
215
216 =item clauses
217
218 clauses => [ 'PAREN?' ]
219
220 Provides a list of strings which will be turned into a regex to capture
221 additional clauses. The regex will include the 'xm' flags (so be sure to escape
222 spaces). The clause can be marked optional with '?'. The special text "PAREN"
223 can be used to capture a balanced parenthetical.
224
225 This example will capture a parameter list enclosed by parenthesis, ie:
226
227 method foo (Int $i) {
228 }
229
230 No formatting is done on the clauses at this time. The order of declaration
231 is significant.
232
233 =back
234
235 =head1 AUTHOR
236
237 Mark Grimes <[email protected]>
238
239 =head1 SOURCE
240
241 Source repository is at L<https://github.com/mvgrimes/Perl-Tidy-Sweetened>.
242
243 =head1 BUGS
244
245 Please report any bugs or feature requests on the bugtracker website L<https://github.com/mvgrimes/Perl-Tidy-Sweetened/issues>
246
247 When submitting a bug or request, please include a test-file or a
248 patch to an existing test-file that illustrates the bug or desired
249 feature.
250
251 =head1 COPYRIGHT AND LICENSE
252
253 This software is copyright (c) 2023 by Mark Grimes <[email protected]>.
254
255 This is free software; you can redistribute it and/or modify it under
256 the same terms as the Perl 5 programming language system itself.
257
258 =cut