-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathgenerate_imgui_bindings.pl
executable file
·479 lines (439 loc) · 15.3 KB
/
generate_imgui_bindings.pl
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
#!/usr/bin/perl
use strict;
use warnings;
use diagnostics;
# This works for IMGUI 1.60 and does not get all functions
#
# to use ./generate_imgui_bindings.pl <../imgui/imgui.h >imgui_iterator.inl
# and define macros properly as in example imgui_lua_bindings.cpp
#
# check imgui_iterator for explanations of why some functions are not supported yet
require "./parse_blocks.pl";
sub generateNamespaceImgui {
my ($imguiCodeBlock) = @_;
my $lineCaptureRegex = qr" *(IMGUI_API) *((const char\*)|([^ ]+)) *([^\(]+)\(([^\;]*)\);";
my $doEndStackOptions = 1;
my $terminator = "} \/\/ namespace ImGui";
my $callPrefix = "";
my $functionSuffix = "";
#define bannedNames with keys of functions to exclude them
# EXAMPLE:
my %bannedNames = (
"NewFrame" => "banned",
"Render" => "banned",
"Shutdown" => "banned" );
#
# This is only useful for ENABLE_IM_LUA_END_STACK
# We hold a list of differnet 'things' that can be pushed to the stack
# i.e. Group for BeginGroup
# It usually works like this BeginBlah EndBlah
# We have to redefine stuff when it doesn't work so cleanly
my %beginN = (
"TreeNode" => "Tree",
"TreePush" => "Tree",
"PushStyleVar" => "StyleVar"
);
my %changeN = (
"Tree" => "TreePop",
"StyleVar"=> "PopStyleVar"
);
my %endN = (
"TreePop" => "Tree",
"PopStyleVar" => "StyleVar"
);
my %endOverride = (
"PopupModal" => "Popup",
"PopupContextItem" => "Popup",
"PopupContextWindow" => "Popup",
"PopupContextVoid" => "Popup" );
generateImguiGeneric(
$lineCaptureRegex,
$doEndStackOptions,
$terminator,
$callPrefix,
$functionSuffix,
\%bannedNames,
\%beginN,
\%changeN,
\%endN,
\%endOverride,
$imguiCodeBlock)
}
sub generateDrawListFunctions {
my ($imguiCodeBlock) = @_;
my $lineCaptureRegex = qr" *(IMGUI_API|inline) *((const char\*)|([^ ]+)) *([^\(]+)\(([^\;]*)\);";
my $doEndStackOptions = 0;
my $terminator = 0;
my $callPrefix = "DRAW_LIST_";
my $functionSuffix = "_DRAW_LIST";
#define bannedNames with keys of functions to exclude them
# EXAMPLE:
my %bannedNames = (
);
#
# This is only useful for ENABLE_IM_LUA_END_STACK
# We hold a list of differnet 'things' that can be pushed to the stack
# i.e. Group for BeginGroup
# It usually works like this BeginBlah EndBlah
# We have to redefine stuff when it doesn't work so cleanly
my %beginN = (
);
my %changeN = (
);
my %endN = (
);
my %endOverride = (
);
generateImguiGeneric(
$lineCaptureRegex,
$doEndStackOptions,
$terminator,
$callPrefix,
$functionSuffix,
\%bannedNames,
\%beginN,
\%changeN,
\%endN,
\%endOverride,
$imguiCodeBlock)
}
sub generateImguiGeneric {
my $lineCaptureRegex = shift;
my $doEndStackOptions = shift;
my $terminator = shift;
my $callPrefix = shift;
my $functionSuffix = shift;
#define bannedNames with keys of functions to exclude them
# EXAMPLE:
my $bannedNamesRef = shift;
my %bannedNames = %{$bannedNamesRef};
#
# This is only useful for ENABLE_IM_LUA_END_STACK
# We hold a list of differnet 'things' that can be pushed to the stack
# i.e. Group for BeginGroup
# It usually works like this BeginBlah EndBlah
# We have to redefine stuff when it doesn't work so cleanly
my $beginNRef = shift;
my %beginN = %{$beginNRef};
my $changeNRef = shift;
my %changeN = %{$changeNRef};
my $endNRef = shift;
my %endN = %{$endNRef};
my $endOverrideRef = shift;
my %endOverride = %{$endOverrideRef};
my ($imguiCodeBlock) = @_;
my $numSupported = 0;
my $numUnsupported = 0;
my $line;
my %funcNames;
my %endTypeToInt;
my @endTypes;
my @functionsAlreadyAdded;
foreach $line (split /\n/, $imguiCodeBlock) {
#replace ImVec2(x, y) with ImVec2 x, y so it's easier for regex (and ImVec4)
$line =~ s/ImVec2\(([^,]*),([^\)]*)\)/ImVec2 $1 $2/g;
$line =~ s/ImVec4\(([^,]*),([^\)]*),([^\)]*),([^\)]*)\)/ImVec4 $1 $2 $3 $4/g;
#delete this so it's eaiser for regexes
$line =~ s/ IM_PRINTFARGS\(.\);/;/g;
if ($line =~ m/$lineCaptureRegex/) {
print "//" . $line . "\n";
# this will be set to 0 if something is not supported yet
my $shouldPrint = 1;
my @args = split(',', $6);
# things to do before calling real c++ function
my @before;
# arguments to real c++ function
my @funcArgs;
# things to do after callign real c++ function
my @after;
# real c++ function name
my $funcName = $5;
#say STDERR "Parsing function: " . $funcName;
if (grep(/^$funcName$/, @functionsAlreadyAdded)) {
#say STDERR $funcName;
}
push @functionsAlreadyAdded, $funcName;
if (defined($bannedNames{$funcName})) {
print "//Not allowed to use this function\n";
$shouldPrint = 0;
}
# c++ type of return value
my $retType = $2;
# macro used for calling function
my $callMacro;
# if it has a return value (yes I know this is not the cleanest code)
my $hasRet = 1;
if ($retType =~ /^void$/) {
$callMacro = "${callPrefix}CALL_FUNCTION_NO_RET";
$hasRet = 0;
} elsif ($retType =~ /^const char\*$/) {
$callMacro = "${callPrefix}CALL_FUNCTION";
push(@funcArgs, "const char*");
push(@after, "PUSH_STRING(ret)");
} elsif ($retType =~ /^bool$/) {
$callMacro = "${callPrefix}CALL_FUNCTION";
push(@funcArgs, "bool");
push(@after, "PUSH_BOOL(ret)");
} elsif ($retType =~ /^float$/) {
$callMacro = "${callPrefix}CALL_FUNCTION";
push(@funcArgs, "float");
push(@after, "PUSH_NUMBER(ret)");
} elsif ($retType =~ /^ImVec2$/) {
$callMacro = "${callPrefix}CALL_FUNCTION";
push(@funcArgs, "ImVec2");
push(@after, "PUSH_NUMBER(ret.x)");
push(@after, "PUSH_NUMBER(ret.y)");
} elsif ($retType =~ /^(unsigned int|ImGuiID|ImU32)$/) {
$callMacro = "${callPrefix}CALL_FUNCTION";
push(@funcArgs, "unsigned int");
push(@after, "PUSH_NUMBER(ret)");
} else {
print "// Unsupported return type $retType\n";
$shouldPrint = 0;
}
for (my $i = 0; $i < @args; $i++) {
# bool * x = NULL or bool * x
if ($args[$i] =~ m/^ *bool *\* *([^ =\[]*)( = NULL|) *$/) {
my $name = $1;
if ($2 =~ m/^ = NULL$/) {
push(@before, "OPTIONAL_BOOL_POINTER_ARG($name)");
} else {
push(@before, "BOOL_POINTER_ARG($name)");
}
push(@funcArgs, $name);
push(@after, "END_BOOL_POINTER($name)");
# float * x
} elsif ($args[$i] =~ m/^ *float *\* *([^ =\[]*)$/) {
my $name = $1;
push(@before, "FLOAT_POINTER_ARG($name)");
push(@funcArgs, $name);
push(@after, "END_FLOAT_POINTER($name)");
#float a or float a = number
} elsif ($args[$i] =~ m/^ *float *([^ =\[]*)( *= *[^ ]*|)$/) {
my $name = $1;
if ($2 =~ m/^ *= *([^ ]*)$/) {
push(@before, "OPTIONAL_NUMBER_ARG($name, $1)");
} else {
push(@before, "NUMBER_ARG($name)");
}
push(@funcArgs, $name);
# const char* a or const char* a = NULL or "blah"
} elsif ($args[$i] =~ m/^ *const char\* *([^ =\[]*)( *= *(NULL|".*")|) *$/) {
my $name = $1;
if ($2 =~ m/^ *= *NULL$/) {
push(@before, "OPTIONAL_LABEL_ARG($name)");
} else {
push(@before, "LABEL_ARG($name)");
}
push(@funcArgs, $name);
#const ImVec2& with default or not
} elsif ($args[$i] =~ m/^ *const ImVec2& ([^ ]*) *(= * ImVec2 [^ ]* [^ ]*|) *$/) {
my $name = $1;
if ($2 =~ m/^= * ImVec2 ([^ ]*) ([^ ]*)$/) {
push(@before, "OPTIONAL_IM_VEC_2_ARG($name, $1, $2)");
} else {
push(@before, "IM_VEC_2_ARG($name)");
}
push(@funcArgs, $name);
# ImVec2
} elsif ($args[$i] =~ m/^ *ImVec2 ([^ ]*) *$/) {
my $name = $1;
push(@before, "IM_VEC_2_ARG($name)");
push(@funcArgs, $name);
#const ImVec4& with default or not
} elsif ($args[$i] =~ m/^ *const ImVec4& ([^ ]*) *(= * ImVec4 [^ ]* [^ ]* [^ ]* [^ ]*|) *$/) {
my $name = $1;
if ($2 =~ m/^= * ImVec4 ([^ ]*) ([^ ]*) ([^ ]*) ([^ ]*)$/) {
push(@before, "OPTIONAL_IM_VEC_4_ARG($name, $1, $2, $3, $4)");
} else {
push(@before, "IM_VEC_4_ARG($name)");
}
push(@funcArgs, $name);
# one of the various enums
# we are handling these as ints
} elsif ($args[$i] =~ m/^ *(ImGuiWindowFlags|ImGuiCol|ImGuiStyleVar|ImGuiKey|ImGuiAlign|ImGuiColorEditMode|ImGuiMouseCursor|ImGuiSetCond|ImGuiInputTextFlags|ImGuiSelectableFlags) ([^ ]*)( = 0|) *$/) {
#These are ints
my $name = $2;
if ($3 =~ m/^ = 0$/) {
push(@before, "OPTIONAL_INT_ARG($name, 0)");
} else {
push(@before, "INT_ARG($name)");
}
push(@funcArgs, $name);
#int with default value or not
} elsif ($args[$i] =~ m/^ *int ([^ =\[]*)( = [^ ]*|) *$/) {
my $name = $1;
if ($2 =~ m/^ = ([^ ]*)$/) {
push(@before, "OPTIONAL_INT_ARG($name, $1)");
} else {
push(@before, "INT_ARG($name)");
}
push(@funcArgs, $name);
#unsigned int with default value or not
} elsif ($args[$i] =~ m/^ *(unsigned +int|ImGuiID|ImU32) ([^ =\[]*)( = [^ ]*|) *$/) {
my $name = $2;
if ($2 =~ m/^ = ([^ ]*)$/) {
push(@before, "OPTIONAL_UINT_ARG($name, $1)");
} else {
push(@before, "UINT_ARG($name)");
}
push(@funcArgs, $name);
#ImTextureID or const ImTextureID&
# const ImTextureID& is the same thing as var
# as lua is concerned
} elsif ($args[$i] =~ m/^ *(ImTextureID|const ImTextureID&) ([^ =\[]*) *$/) {
my $name = $2;
push(@before, "IM_TEXTURE_ID_ARG($name)");
push(@funcArgs, $name);
# bool with default value or not
} elsif ($args[$i] =~ m/^ *bool ([^ =\[]*)( *= *true| *= *false|) *$/) {
my $name = $1;
if ($2 =~ m/^ *= *([^ ]*)$/) {
push(@before, "OPTIONAL_BOOL_ARG($name, $1)");
} else {
push(@before, "BOOL_ARG($name)");
}
push(@funcArgs, $name);
# int * x
} elsif ($args[$i] =~ m/^ *int *\* *([^ =\[]*)$/) {
my $name = $1;
push(@before, "INT_POINTER_ARG($name)");
push(@funcArgs, $name);
push(@after, "END_INT_POINTER($name)");
# unsigned int * x
} elsif ($args[$i] =~ m/^ *unsigned +int *\* *([^ =\[]*)$/) {
my $name = $1;
push(@before, "UINT_POINTER_ARG($name)");
push(@funcArgs, $name);
push(@after, "END_UINT_POINTER($name)");
# we don't support variadic functions yet but we let you use it without extra variables
} elsif ($args[$i] =~ m/^ *\.\.\. *$/) {
print "// Variadic functions aren't suppported but here it is anyway\n";
} else {
print "// Unsupported arg type " . $args[$i] . "\n";
$shouldPrint = 0;
}
}
if ($shouldPrint != 0) {
my $luaFunc = $funcName;
# Stupid way of implementing overriding
while($funcNames{$luaFunc}) {
$luaFunc .= "_" . scalar(@args);
}
$funcNames{$luaFunc} = 1;
print "IMGUI_FUNCTION${functionSuffix}($luaFunc)\n";
for (my $i = 0; $i < @before; $i++) {
print $before[$i] . "\n";
}
print $callMacro . "($funcName";
for (my $i = 0; $i < @funcArgs; $i++) {
print ", " . $funcArgs[$i];
}
print ")\n";
#for begin and end stack stuff
if ($funcName =~ m/^Begin(.*)$/ || defined($beginN{$funcName})) {
my $curEndType;
if (defined($beginN{$funcName})) {
$curEndType = $beginN{$funcName};
} else {
$curEndType = $1;
}
if (defined($endOverride{$curEndType})) {
$curEndType = $endOverride{$curEndType};
}
if (!defined($endTypeToInt{$curEndType})) {
$endTypeToInt{$curEndType} = scalar(@endTypes);
push(@endTypes, $curEndType);
}
my $curEndTypeInt = $endTypeToInt{$curEndType};
if ($hasRet) {
print "IF_RET_ADD_END_STACK($curEndTypeInt)\n";
} else {
print "ADD_END_STACK($curEndTypeInt)\n";
}
} elsif ($funcName =~ m/^End(.*)$/ || defined($endN{$funcName})) {
my $curEndType;
if (defined($endN{$funcName})) {
$curEndType = $endN{$funcName};
} else {
$curEndType = $1;
}
if (defined($endOverride{$curEndType})) {
$curEndType = $endOverride{$curEndType};
}
if (!defined($endTypeToInt{$curEndType})) {
$endTypeToInt{$curEndType} = scalar(@endTypes);
push(@endTypes, $curEndType);
}
my $curEndTypeInt = $endTypeToInt{$curEndType};
print "POP_END_STACK($curEndTypeInt)\n"
}
for (my $i = 0; $i < @after; $i++) {
print $after[$i] . "\n";
}
print "END_IMGUI_FUNC\n";
$numSupported += 1;
} else {
$numUnsupported += 1;
}
} elsif ($terminator) {
if ($line =~ m/^${terminator}$/) {
last;
}
}
}
#for end stack stuff
if ($doEndStackOptions)
{
print "END_STACK_START\n";
for (my $i = 0; $i < @endTypes; $i++) {
my $endFunc;
if (defined($changeN{$endTypes[$i]})) {
$endFunc = $changeN{$endTypes[$i]};
} else {
$endFunc = "End" . $endTypes[$i];
}
print "END_STACK_OPTION($i, " . $endFunc .")\n";
}
print "END_STACK_END\n";
}
#debug info
print STDERR "Supported: $numSupported Unsupported: $numUnsupported\n";
}
sub generateEnums {
my $enumName = shift;
my ($imguiCodeBlock) = @_;
my $lineCaptureRegex = qr"^ *(ImGui)([^, _]+)_([a-zA-Z0-9]+)\b";
print "START_ENUM($enumName)\n";
my $line;
foreach $line (split /\n/, $imguiCodeBlock) {
if ($line =~ m/$lineCaptureRegex/) {
die "Malformed enum at $enumName" unless ($2 eq $enumName);
print "//" . $line . "\n";
print "MAKE_ENUM($1$2_$3,$3)\n";
}
}
print "END_ENUM($enumName)\n";
}
my ($blocksref, $blocknamesref) = parse_blocks();
my @blocks = @$blocksref;
my @blocknames = @$blocknamesref;
# @spaderthomas 3/1/2020: ImGui also puts its deprecated functions in namespace ImGui,
# so we'll end up parsing a couple functions twice and causing compiler errors.
#
# This flag just means that we've parsed the main one, so don't parse the next one. If ImGui
# splits up its header to multiple instances of namespace ImGui, this would break.
my $alreadyParsedMainImguiNamespace = 0;
for (my $i=0; $i < scalar @blocks; $i++) {
print "//" . $blocknames[$i] . "\n";
if (($blocknames[$i] eq "namespace ImGui\n") and not $alreadyParsedMainImguiNamespace) {
$alreadyParsedMainImguiNamespace = 1;
generateNamespaceImgui($blocks[$i]);
}
if ($blocknames[$i] =~ m/enum ImGui(.*)_\n/) {
generateEnums($1, $blocks[$i]);
}
if ($blocknames[$i] eq "struct ImDrawList\n") {
generateDrawListFunctions($blocks[$i]);
}
}