-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprop-gen.pl6
executable file
·358 lines (296 loc) · 8.76 KB
/
prop-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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#!/usr/bin/env perl6
use v6.c;
use LWP::Simple;
#use Mojo::DOM:from<Perl5>;
use DOM::Tiny;
use lib <scripts .>;
use GTKScripts;
my @really-strings = <
char gchar
uchar guchar
chararray gchararray
unichar
string gstring
>;
my @gtypes = <
boolean
int uint
long ulong
int64 uint64
double
float
string
pointer
>;
sub get-vtype-rw ($gtype) {
my ($vtype-r, $vtype-w);
my $i = ' ' x 6;
if $gtype ne '-type-' {
$_ = $*types;
my $u = S/^ 'g'//;
if $u eq @really-strings.any {
$u = 'string';
}
$vtype-r = $i ~ '$gv.' ~ $u ~ ';';
$vtype-w = '$gv.' ~ $u ~ ' = $val;';
} else {
$vtype-r = $i ~ '#$gv.TYPE';
$vtype-w = '#$gv.TYPE = $val;';
}
($vtype-r, $vtype-w);
}
sub getType {
do given $*types {
when 'gboolean' | 'boolean' { $*co = 'Int()'; 'G_TYPE_BOOLEAN' }
when 'gint' | 'int' { $*co = 'Int()'; 'G_TYPE_INT' }
when 'gint64' | 'int64' { $*co = 'Int()'; 'G_TYPE_INT64' }
when 'guint64' | 'uint64' { $*co = 'Int()'; 'G_TYPE_UINT64' }
when 'guint' | 'uint' { $*co = 'Int()'; 'G_TYPE_UINT' }
when 'glong' | 'long' { $*co = 'Int()'; 'G_TYPE_LONG' }
when 'gulong' | 'ulong' { $*co = 'Int()'; 'G_TYPE_ULONG' }
when 'gdouble' | 'double' { $*co = 'Num()'; 'G_TYPE_DOUBLE' }
when 'gfloat' | 'float' { $*co = 'Num()'; 'G_TYPE_FLOAT' }
when 'gpointer' | 'pointer' { 'G_TYPE_POINTER' }
when @really-strings.any { $*co = 'Str()'; 'G_TYPE_STRING' }
default {
'-type-'
}
}
}
my %methods;
sub genSub ($mn, $rw, $gtype, $dep, $vtype-r, $vtype-w) {
my %c;
# say "MN: $mn, RW: { $rw.map( "'" ~ * ~ "'" ).join(', ') }";
with $rw {
%c<read> =
#"\t \$gv = GLib::Value.new(\n" ~
' ' x 6 ~ "self.prop_get('{ $mn }', \$gv);\n" ~
#' ' x 6 ~ ");\n" ~
$vtype-r
if $rw.any eq 'read';
# say "R: { so($rw.any eq 'read') }";
%c<write> =
"{ $vtype-w }\n" ~
' ' x 6 ~ "self.prop_set(\'{ $mn }\', \$gv);"
if $rw.any eq 'write';
# say "W: { so($rw.any eq 'write') }";
%c<write> = "warn '{ $mn } is a construct-only attribute'"
if $rw.any eq 'construct';
}
%c<write> //= "warn '{ $mn } does not allow writing'";
# Remember to emit a returned value, or the STORE will not work.
# Read warnings should appear behind a DEBUG sentinel.
%c<read> //= qq:to/READ/;
warn '{ $mn } does not allow reading' if \$DEBUG;
{ ' ' x 6 }{ $gtype eq 'G_TYPE_STRING' ?? "''" !! '0' };
READ
my $deprecated = '';
if $dep {
$deprecated = ' is DEPRECATED';
$deprecated ~= "({$dep})" unless $dep ~~ Bool;
};
%methods{$mn} = qq:to/METH/;
# Type: { $*types }
method $mn is rw { $deprecated } is g-property \{
my \$gv = GLib::Value.new( { $gtype } );
Proxy.new(
FETCH => sub (\$) \{
{ %c<read>.trim }
\},
STORE => -> \$, { $*co // '' } \$val is copy \{
{ %c<write> }
\}
);
\}
METH
%methods{$mn}.say;
%methods{$mn}
}
sub generateFromDOM ($dom, $control, $var) {
my $v = "\$\!$var";
#my $sig-div = $dom.find('div.refsect1 a').to_array.List.grep(
# *.attr('name') eq "{ $control }.signal-details"
#)[0].parent;
for '.property-details', '.style-properties', '#properties' -> $pd {
my $found = False;
quietly {
for $dom.find('div.refsect1 a') -> $e {
#say "Searching for: { $control }{ $pd }...";
if $e && $e.attr('name') eq "{ $control }{ $pd }" {
$found = $e.parent;
last;
}
}
}
unless $found {
say "Could not find { $pd } section for { $control }";
next;
}
if $found && $pd eq '.style-properties' {
say 'Retrieval of style properties NYI';
next;
}
for $found.find('div h3 code') -> $e {
(my $mn = $e.text) ~~ s:g/<[“”"]>//;
my $pre = $e.parent.parent.find('pre').tail;
my @t = $pre.find('span.type');
my @i = $pre.parent.find('p');
my @w = $pre.parent.find('div.warning');
my ($dep, $rw);
for @i {
if .text ~~ /
'Flags'? ': '
('Read' | 'Write' | 'Construct')+ % ' / '
/ {
$rw = $/[0].map( *.lc ).Array;
}
}
# Due to the variety of types, this isn't the only place to look...
unless $rw.defined {
if $pre.text ~~ /
'Flags'? ': '
('Read' | 'Write' | 'Construct')+ % ' / '
/ {
$rw = $/[0].map( *.lc ).Array;
}
}
for @w {
$dep = so .all_text ~~ /'deprecated'/;
if $dep {
.all_text ~~ /'Use' (.+?) 'instead'/;
with $/ {
$dep = $/[0] with $/[0];
}
}
}
my (%c, $*co);
my $*types = @t.map(*.text.trim).join(', ');
my $gtype = getType;
genSub( $mn, $rw, $gtype, $dep, |get-vtype-rw($gtype) )
}
}
}
sub generateFromFile (
$type-prefix,
$control is copy,
$var,
:$control-name
) {
say "Loading: { $control }";
$control = $control.subst('file://', '');
my $control-type = do if $control-name.not {
# First part must come from header (.h) file
my $contents = $control.ends-with('.h') ??
$control.IO.slurp !!
$control.IO.extension('h').IO.slurp;
my token start-decls {
'G_DECLARE_FINAL_TYPE' | 'G_DECLARE_DERIVABLE_TYPE'
'G_DEFINE_TYPE_WITH_CODE'
}
my $search = $contents ~~ /
'G_TYPE_CHECK_INSTANCE_CAST' .+? $<t>=[ <{ $type-prefix }> \w+ ] ')' $$ |
^^ <start-decls> \s* '(' $<t>=[ <{ $type-prefix }> \w+ ]
/;
$/<t> ?? $/<t>.Str !! 'Dzl'
} else {
$control-name
}
# Final part must come from implementation (.c) file
my $contents = $control.ends-with('.c') ??
$control.IO.slurp !!
$control.IO.extension('c').IO.slurp;
my $search = $contents ~~ m:g/
'g_param_spec_' (\w+) <?{ $0.Str eq <ref unref>.none }> <.ws>? '(' <.ws>?
$<params>=[
[ $<p>=<-[,]>+? ]+ % ','
]
<.ws>? ');'
/;
my %properties;
for $search.sort( *.<p>[0].Str ) {
#.gist.say;
my $prop-name = .<p>[0].Str;
my $rw = (
do gather for .<p>.tail
.lc
.split(/ <.ws> '|' <.ws> /)
-> $_ is copy
{
my @perms;
# say "RW-P: $_";
s:g/\W//;
s/ 'writ'» /write/;
@perms.push: 'read' if .ends-with('_read' | '_readable');
@perms.push: 'write' if .ends-with('_write' | '_writable');
@perms.append: |<read write> if .ends-with('readwrite');
if +@perms {
take $_ for @perms;
}
}
).cache;
#say "RW: { $rw.gist }";
my $*co;
my $type = my $*types = .[0].Str;
$type = do if $type eq @gtypes.any {
$*types = $type;
getType
} else {
my $pre = .<p>[3] // '';
$*types = $type-prefix ~ $pre.split('_')
.skip(2)
.map( *.lc.tc )
.join;
}
my $dep = False;
genSub(
$prop-name.subst('"', '', :g),
$rw,
$type,
$dep,
|get-vtype-rw($type)
)
}
}
sub MAIN (
$control is copy,
:$var is copy = 'w',
:$prefix is copy = "https://developer.gnome.org/gtk3/stable/",
:$type-prefix = %config<struct-prefix> // %config<struct_prefix> //
%config<type-prefix> // %config<type_prefix> // %config<prefix>,
:$control-name
) {
# If it's a URL, then try to pick it apart
my $ext = '';
$control ~~ / ^ $<con>=[ 'http's? | 'file' ] '://' /;
given $/<con> {
when 'file' | $control.ends-with('.c') {
die 'Must specify --type-prefix!' unless $type-prefix;
$control = %config<include-directory>.IO.add($control).absolute
unless $control.starts-with('/');
generateFromFile($type-prefix, $control, $var, :$control-name);
}
when 'http' | 'https' | $control.ends-with('.html') {
my $new_prefix = $/.Str;
my $new_control = $new_prefix.split('/')[* - 1];
$new_control ~~ s/ '.' (.+?) $//;
$ext = ".{ $/[0] }";
$new_prefix ~~ s| '/' <-[/]>+? $|/|;
($prefix, $control) = ($new_prefix.trim, $type-prefix // $new_control.trim);
my $uri = "{ $prefix }{ $control }{ $ext }";
say "Retrieving: $uri";
my $dom = DOM::Tiny.parse(
do {
when $prefix.starts-with('http' | 'https') {
LWP::Simple.new.get($uri)
}
when $prefix.starts-with('file') {
$uri.subst('file://', '').IO.slurp
}
}
);
generateFromDOM($dom, $control, $var);
}
say "Attempting with prefix = { $prefix } control = { $control }";
}
#.value.say for %methods.pairs.sort( *.key );
}