-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsig-gen.pl6
executable file
·275 lines (235 loc) · 7.25 KB
/
sig-gen.pl6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/usr/bin/env perl6
use v6.c;
use LWP::Simple;
#use Mojo::DOM:from<Perl5>;
use DOM::Tiny;
my @defined-sigs = (
[ 'gpointer', 'void', 'connect' ],
[ 'guint, gpointer', 'void', 'connect-uint' ],
);
sub MAIN (
$control is copy,
:$var is copy = 'w',
:$lib is copy = 'gobject',
:$prefix is copy = "https://developer.gnome.org/gtk3/stable/",
:$user-data = False,
) {
# If it's a URL, then try to pick it apart
my ($ext, $v) = ('', "\$\!{ $var }");
my %signals;
sub trimIt ($_) {
.Str.subst("\n", ' ', :g).subst(/\s+/, ' ', :g)
}
if $control ~~ / ^ 'http's?'://' / {
$control ~~ / 'http' s? '://' <-[\#]>+ /;
my $new_prefix = $/.Str;
my $new_control = $new_prefix.split('/')[* - 1];
$new_control ~~ s/ '.' (.+?) $//;
$ext = ".{ $/[0] }";
$new_prefix ~~ s| '/' <-[/]>+? $|/|;
($prefix, $control) = ($new_prefix.trim, $new_control.trim);
say "Attempting with prefix = { $prefix } control = { $control }";
my $uri = "{ $prefix }{ $control }{ $ext }";
say "Retrieving: $uri";
my $dom = DOM::Tiny.parse(
LWP::Simple.new.get($uri)
);
#my $sig-div = $dom.find('div.refsect1 a').to_array.List.grep(
# *.attr('name') eq "{ $control }.signal-details"
#)[0].parent;
my $found = False;
quietly {
for $dom.find('div.refsect1 a') -> $e {
if $e && $e.attr('name') eq "{ $control }.signal-details" {
$found = $e.parent;
last;
}
}
}
die "Could not find signals section for { $control }" unless $found;
for $found.find('div h3 code') -> $e {
(my $mn = $e.text) ~~ s:g/<[“”"]>//;
my $pre = $e.parent.parent.find('pre').tail;
my $rts = $pre.find('span.returnvalue').tail;
my $rt = $rts.text.&trimIt;
my @t = $pre.find('span.type');
# Existing check to go here.
my $s-sig = @t.map(*.text.trim).join(', ');
my $udm = $s-sig.elems == 2 && $s-sig.tail.contains('void');
# my $emn = check_defined($s-sig, $rt);
%signals{$mn} = ( :$udm, :$mn, :$v, :$s-sig, :$rt ).Hash;
}
} elsif (my $control-io = $control.IO).r {
say 'Reading from file...';
# If it's a readable file, we have to do things the (James) Hardway
# 1) Read in the .c file for the signal names
my @signals;
my @matches = $control-io.slurp ~~ m:g/
"g_signal_new" \s* "("
#\s* <["]> (<[\-\_\w]>+)
(<-[\,]>+)
/;
for @matches {
my $sn = .[0].Str;
$sn.gist.say;
$sn ~~ s/<[A..Z]>+ '_("' (<[\-\_\w]>+) '")' /$0/;
@signals.push: $sn;
}
@signals.gist.say;
# 2) Read in the .h file for the signal signatures
my $signature-matches;
SIGNATURES: loop {
my $header = $control-io.absolute.subst('.c', '') ~ '.h';
die "Could not find header '{ $header }' file for signatures!"
unless $header.IO.r;
@signals.map({ .subst('-', '_', :g) }).say;
$signature-matches = $header.IO.slurp ~~ m:g/
$<rt>=[(\w+) \s* '*'?] '(*' \s*
$<signal>=<{ @signals.map({ .subst('-', '_', :g) }) }> ')'
\s*
'(' $<sig>=( <-[\)]>+ ) ')'
/;
last if $signature-matches;
$header = $control-io.absolute.subst('.c', '') ~ '-private.h';
die "Could not find header '{ $header }' file for signatures!"
unless $header.IO.r;
my $file-prefix = $control-io.extension('').basename.subst('-', '_', :g);
$signature-matches = $header.IO.slurp ~~ m:g/
$<rt>=[(\w+) \s* '*'?] \s*
'_'<{ $file-prefix }>'_'
$<signal>=<{ @signals.map({ .subst('-', '_', :g) }) }>
\s*
'(' $<sig>=( <-[\)]>+ ) ')'
/;
last
}
$signature-matches.gist.say;
for $signature-matches[] {
.gist.say;
my $s-sig = .<sig>.&trimIt.split(',').map({
.subst('const ', '').subst('gchar', 'Str', :g)
});
my $udm = $s-sig.elems == 2 && $s-sig.tail.contains('void');
my $signal-name = .<signal>.Str;
%signals{ $signal-name } = my %sig-data = (
:$udm,
# cw: This back and forth is hard to avoid.
mn => $signal-name.subst('_', '-', :g),
:$v,
:$s-sig,
rt => .<rt>.&trimIt,
).Hash;
}
}
# Output in-class handlers
for %signals.pairs {
given .value {
say qq:to/METH/;
# Is originally:
# { .<s-sig>.join(', ') } --> { .<rt> }
method { .<mn> } \{
self.{ .<udm> ?? "connect({ .<v> }, '{ .<mn> }')"
!! "connect-{ .<mn> }({ .<v> })" };
\}
METH
}
}
# Output role code
# Handle return type!!
for %signals.pairs {
my %vars;
my @p = .value<s-sig>.skip(1);
my $cpp = False;
# If this is processed lazily, the post-process step will not work.
my $pp = @p.map({
/ (<[A..Za..z]>)+ %% [ <[a..z]>+ ] /;
my $v = '$' ~ $/[0].map( *.Str.lc )[^(* - 1)].join('');
# Collision handling.
if %vars{$v}:exists {
$cpp = True;
if $v ~~ /^ (\w+)(\d+) $/ {
$v = $0 ~ { $1.Int + 1};
} else {
$v ~= '2'
}
}
%vars{$v} = 1;
$v
}).eager;
# Collision handing post-process.
if $cpp {
for $pp.kv -> $k, $v {
#say "K: $k / V: $v";
if $v.ends-with('2') {
$pp[$k - 1] ~= '1';
}
}
}
$pp .= join(', ');
my $rt = .value<rt> ne 'void' ?? " --> { .value<rt> }" !! '';
my $name = $v.substr(2);
my $emission;
my $params = "self{ $pp ?? ', ' !! ''}{ $pp }";
$params ~= ", \$ud" if $user-data;
if $rt.defined && $rt.trim && $rt.trim ne 'void' {
$emission = qq:to/NONVOID/.chomp;
my \$r = ReturnedValue.new;
𝒮.emit( [{ $params }, \$r] );
\$r.r;
NONVOID
} else {
$emission = qq:to/VOID/.chomp;
𝒮.emit( [self, { $params }] );
VOID
}
# We use 𝒮 as opposed to 's' to prevent a naming collision.
# between the Supplier and the parameters in the pointy block.
my $ud = $user-data ?? ', \$ud' !! '';
say qq:to/METH/;
# { @p.join(', ') }{ $rt }
method connect-{ .value<mn> } (
\$obj,
\$signal = '{ .value<mn> }',
\&handler?
) \{
my \$hid;
\%!signals-{ $name }\{\$signal\} //= do \{
my \\𝒮 = Supplier.new;
\$hid = g-connect-{ .value<mn> }(\$obj, \$signal,
-> \${ $pp ?? ', ' !! ''}{ $pp }{ $ud } \{
CATCH \{
default \{ 𝒮.note(\$_) \}
\}
{ $emission }
\},
Pointer, 0
);
[ 𝒮.Supply, \$obj, \$hid ];
\};
\%!signals-{ $name }\{\$signal\}[0].tap(\&handler) with \&handler;
\%!signals-{ $name }\{\$signal\}[0];
\}
METH
}
# Emit non-default nativecall defs
for %signals.pairs {
my $rt = .value<rt>.trim ne 'void' ?? " --> { .value<rt>.trim }" !! '';
my @s-sig = .value<s-sig>.skip(1);
my $c = +@s-sig ?? ', ' !! ' ';
say qq:to/NC/;
# { .value<s-sig>.join(', ') }{ $rt }
sub g-connect-{ .value<mn> } (
Pointer \$app,
Str \$name,
\&handler (Pointer{ $c }{ @s-sig.map({ ' ' ~ .split(' ')[1] }).join(',') }{
$user-data ?? ($c ~ 'Pointer') !! '' }{ $rt }),
Pointer \$data,
uint32 \$flags
)
returns uint64
is native({ $lib })
is symbol('g_signal_connect_object')
\{ \* \}
NC
}
}