Skip to content

Commit a5eb070

Browse files
committed
Pure-perl verions of the script to expand method sigs
Signed-off-by: Pedro Melo <[email protected]>
1 parent 0f7e7e3 commit a5eb070

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed

bin/x-perl-expand-signature-pp

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#!/usr/bin/env perl
2+
3+
4+
#### RE's
5+
6+
my $decl_re = qr/
7+
^
8+
\s*
9+
(method|func)
10+
\s*
11+
([\w_]+)
12+
\s*
13+
[(]
14+
(.+?)
15+
[)]
16+
\s*
17+
[{]
18+
$
19+
/x;
20+
21+
my $match_self_name = qr/
22+
\s*
23+
\$
24+
([\w_]+)
25+
:
26+
\s*
27+
,?
28+
\s*
29+
/x;
30+
31+
my $match_pos_protos = qr/
32+
\s*
33+
\$
34+
([\w_]+)
35+
\s*
36+
(\?)?
37+
\s*
38+
,?
39+
\s*
40+
/x;
41+
42+
my $match_named_protos = qr/
43+
\s*
44+
:
45+
\$
46+
([\w_]+)
47+
\s*
48+
(?:
49+
=
50+
\s*
51+
(
52+
(?:
53+
["']
54+
.*?
55+
['"]
56+
)
57+
|
58+
(?:
59+
[{\]]
60+
.*?
61+
[}\]]
62+
)
63+
|
64+
(?:
65+
[^,]*
66+
)
67+
)
68+
\s*
69+
)?
70+
\s*
71+
,?
72+
\s*
73+
/x;
74+
75+
76+
#### Code
77+
78+
my $decl = do { local $\; <> };
79+
my ($type, $name, $proto) = $decl =~ m/$decl_re/;
80+
my @perl = ("sub $name {");
81+
82+
if ($type eq 'method') {
83+
if ($proto =~ s/^$match_self_name//) {
84+
push @perl, " my \$$1 = shift;";
85+
}
86+
else {
87+
push @perl, ' my $self = shift;';
88+
}
89+
}
90+
91+
## collect fixed pos args
92+
my @fixed;
93+
while ($proto =~ s/^$match_pos_protos//) {
94+
95+
}
96+
97+
## collect named args
98+
my @named;
99+
while ($proto =~ s/^$match_named_protos//) {
100+
push @named, { name => $1, default => $2};
101+
}
102+
103+
print "# $decl\n", join("\n", @perl), "\n";
104+
use Data::Dump qw(pp); print "proto '$proto' = ", pp(\@named), "\n";

0 commit comments

Comments
 (0)