-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsipify.pl
executable file
·1403 lines (1308 loc) · 53.1 KB
/
sipify.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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env perl
use strict;
use warnings;
use File::Basename;
use File::Spec;
use Getopt::Long;
use YAML::Tiny;
no if $] >= 5.018000, warnings => 'experimental::smartmatch';
use constant PRIVATE => 0;
use constant PROTECTED => 1;
use constant PUBLIC => 2;
use constant STRICT => 10;
use constant UNSTRICT => 11;
use constant MULTILINE_NO => 20;
use constant MULTILINE_METHOD => 21;
use constant MULTILINE_CONDITIONAL_STATEMENT => 22;
use constant CODE_SNIPPET => 30;
use constant CODE_SNIPPET_CPP => 31;
# read arguments
my $debug = 0;
my $sip_output = '';
my $python_output = '';
#my $SUPPORT_TEMPLATE_DOCSTRING = 0;
#die("usage: $0 [-debug] [-template-doc] headerfile\n") unless GetOptions ("debug" => \$debug, "template-doc" => \$SUPPORT_TEMPLATE_DOCSTRING) && @ARGV == 1;
die("usage: $0 [-debug] [-sip_output FILE] [-python_output FILE] headerfile\n")
unless GetOptions ("debug" => \$debug, "sip_output=s" => \$sip_output, "python_output=s" => \$python_output) && @ARGV == 1;
my $headerfile = $ARGV[0];
# read file
open(my $handle, "<", $headerfile) || die "Couldn't open '".$headerfile."' for reading because: ".$!;
chomp(my @INPUT_LINES = <$handle>);
close $handle;
# config
my $cfg_file = File::Spec->catfile( dirname(__FILE__), 'sipify.yaml' );
my $yaml = YAML::Tiny->read( $cfg_file );
my $SIP_CONFIG = $yaml->[0];
# contexts
my $SIP_RUN = 0;
my $HEADER_CODE = 0;
my @ACCESS = (PUBLIC);
my @CLASSNAME = ();
my @DECLARED_CLASSES = ();
my @EXPORTED = (0);
my $MULTILINE_DEFINITION = MULTILINE_NO;
my $ACTUAL_CLASS = '';
my $PYTHON_SIGNATURE = '';
my $INDENT = '';
my $PREV_INDENT = '';
my $COMMENT = '';
my $COMMENT_PARAM_LIST = 0;
my $COMMENT_LAST_LINE_NOTE_WARNING = 0;
my $COMMENT_CODE_SNIPPET = 0;
my $COMMENT_TEMPLATE_DOCSTRING = 0;
my @SKIPPED_PARAMS_OUT = ();
my @SKIPPED_PARAMS_REMOVE = ();
my $GLOB_IFDEF_NESTING_IDX = 0;
my @GLOB_BRACKET_NESTING_IDX = (0);
my $PRIVATE_SECTION_LINE = '';
my $RETURN_TYPE = '';
my $IS_OVERRIDE = 0;
my $IF_FEATURE_CONDITION = '';
my $FOUND_SINCE = 0;
my %QFLAG_HASH;
my $LINE_COUNT = @INPUT_LINES;
my $LINE_IDX = 0;
my $LINE;
my @OUTPUT = ();
my @OUTPUT_PYTHON = ();
sub read_line {
my $new_line = $INPUT_LINES[$LINE_IDX];
$LINE_IDX++;
$debug == 0 or print sprintf('LIN:%d DEPTH:%d ACC:%d BRCK:%d SIP:%d MLT:%d OVR: %d CLSS: %s/%d',
$LINE_IDX,
$#ACCESS,
$ACCESS[$#ACCESS],
$GLOB_BRACKET_NESTING_IDX[$#GLOB_BRACKET_NESTING_IDX],
$SIP_RUN,
$MULTILINE_DEFINITION,
$IS_OVERRIDE,
$ACTUAL_CLASS,
$#CLASSNAME)." :: ".$new_line."\n";
$new_line = replace_macros($new_line);
return $new_line;
}
sub write_output {
my ($dbg_code, $out) = @_;
if ($debug == 1){
$dbg_code = sprintf("%d %-4s :: ", $LINE_IDX, $dbg_code);
}
else{
$dbg_code = '';
}
push @OUTPUT, "%If ($IF_FEATURE_CONDITION)\n" if $IF_FEATURE_CONDITION ne '';
push @OUTPUT, $dbg_code.$out;
push @OUTPUT, "%End\n" if $IF_FEATURE_CONDITION ne '';
$IF_FEATURE_CONDITION = '';
}
sub dbg_info {
my $info = $_[0];
if ($debug == 1){
push @OUTPUT, $info."\n";
print $LINE_IDX." ".@ACCESS." ".$SIP_RUN." ".$MULTILINE_DEFINITION." ".$info."\n";
}
}
sub exit_with_error {
die "! Sipify error in $headerfile at line :: $LINE_IDX\n! $_[0]\n";
}
sub sip_header_footer {
my @header_footer = ();
# small hack to turn files src/core/3d/X.h to src/core/./3d/X.h
# otherwise "sip up to date" test fails. This is because the test uses %Include entries
# and over there we have to use ./3d/X.h entries because SIP parser does not allow a number
# as the first letter of a relative path
my $headerfile_x = $headerfile;
$headerfile_x =~ s/src\/core\/3d/src\/core\/.\/3d/;
push @header_footer, "/************************************************************************\n";
push @header_footer, " * This file has been generated automatically from *\n";
push @header_footer, " * *\n";
push @header_footer, sprintf " * %-*s *\n", 68, $headerfile_x;
push @header_footer, " * *\n";
push @header_footer, " * Do not edit manually ! Edit header and run scripts/sipify.pl again *\n";
push @header_footer, " ************************************************************************/\n";
return @header_footer;
}
sub python_header {
my @header = ();
my $headerfile_x = $headerfile;
$headerfile_x =~ s/src\/core\/3d/src\/core\/.\/3d/;
push @header, "# The following has been generated automatically from ";
push @header, sprintf "%s\n", $headerfile_x;
return @header;
}
sub processDoxygenLine {
my $line = $_[0];
# detect code snippet
if ( $line =~ m/\\code(\{\.(\w+)\})?/ ) {
my $codelang = "";
$codelang = " $2" if (defined $2);
$COMMENT_CODE_SNIPPET = CODE_SNIPPET;
$COMMENT_CODE_SNIPPET = CODE_SNIPPET_CPP if ($codelang =~ m/cpp/ );
$codelang =~ s/py/python/;
return "\n" if ( $COMMENT_CODE_SNIPPET == CODE_SNIPPET_CPP );
return ".. code-block::$codelang\n\n";
}
if ( $line =~ m/\\endcode/ ) {
$COMMENT_CODE_SNIPPET = 0;
return "\n";
}
if ($COMMENT_CODE_SNIPPET != 0){
if ( $COMMENT_CODE_SNIPPET == CODE_SNIPPET_CPP ){
return "";
} else {
if ( $line ne ''){
return " $line\n";
} else {
return "\n";
}
}
}
# remove prepending spaces
$line =~ s/^\s+//g;
# remove \a formatting
$line =~ s/\\a (.+?)\b/``$1``/g;
# replace :: with . (changes c++ style namespace/class directives to Python style)
$line =~ s/::/./g;
# replace nullptr with None (nullptr means nothing to Python devs)
$line =~ s/\bnullptr\b/None/g;
if ( $line =~ m/^\\(?<SUB>sub)?section/) {
my $sep = "-";
$sep = "~" if defined $+{SUB};
$line =~ s/^\\(sub)?section \w+ //;
my $sep_line = $line =~ s/[\w ()]/$sep/gr;
$line .= "\n".$sep_line;
}
# convert ### style headings
if ( $line =~ m/^###\s+(.*)$/) {
$line = "$1\n".('-' x length($1));
}
if ( $line =~ m/^##\s+(.*)$/) {
$line = "$1\n".('=' x length($1));
}
if ( $line eq '*' ) {
$line = '';
}
# handle multi-line parameters/returns/lists
if ($line ne '') {
if ( $line =~ m/^\s*[\-#]/ ){
# start of a list item, ensure following lines are correctly indented
$line = "$PREV_INDENT$line";
$INDENT = $PREV_INDENT." ";
}
elsif ( $line !~ m/^\s*[\\:]+(param|note|since|return|deprecated|warning|throws)/ ) {
# if inside multi-line parameter, ensure additional lines are indented
$line = "$INDENT$line";
}
}
else
{
$PREV_INDENT = $INDENT;
$INDENT = '';
}
# replace \returns with :return:
if ( $line =~ m/\\return(s)?/ ){
$line =~ s/\s*\\return(s)?\s*/\n:return: /;
# remove any trailing spaces, will be present now for empty 'returns' tags
$line =~ s/\s*$//g;
$INDENT = ' 'x( index($line,':',4) + 1);
}
# params
if ( $line =~ m/\\param / ){
$line =~ s/\s*\\param\s+(\w+)\b\s*/:param $1: /g;
# remove any trailing spaces, will be present now for empty 'param' tags
$line =~ s/\s*$//g;
$INDENT = ' 'x( index($line,':',2) + 2);
if ( $line =~ m/^:param/ ){
if ( $COMMENT_PARAM_LIST == 0 )
{
$line = "\n$line";
}
$COMMENT_PARAM_LIST = 1;
$COMMENT_LAST_LINE_NOTE_WARNING = 0;
}
}
if ( $line =~ m/^\s*[\\@]brief/){
$line =~ s/[\\@]brief\s*//;
if ( $FOUND_SINCE eq 1 ) {
exit_with_error("$headerfile\:\:$LINE_IDX Since annotation must come after brief")
}
$FOUND_SINCE = 0;
if ( $line =~ m/^\s*$/ ){
return "";
}
}
if ( $line =~ m/[\\@](ingroup|class)/ ) {
$PREV_INDENT = $INDENT;
$INDENT = '';
return "";
}
if ( $line =~ m/\\since .*?([\d\.]+)/i ) {
$PREV_INDENT = $INDENT;
$INDENT = '';
$FOUND_SINCE = 1;
return "\n.. versionadded:: $1\n";
}
if ( $line =~ m/\\deprecated(?:\s+since\s+(?:QGIS\s+)(?<DEPR_VERSION>[0-9.]+)(,\s*)?)?(?<DEPR_MESSAGE>.*)?/i ) {
$PREV_INDENT = $INDENT;
$INDENT = '';
my $depr_line = "\n.. deprecated::";
$depr_line .= " QGIS $+{DEPR_VERSION}" if (defined $+{DEPR_VERSION});
$depr_line .= "\n $+{DEPR_MESSAGE}\n" if (defined $+{DEPR_MESSAGE});
return $depr_line;
}
# create links in see also
if ( $line =~ m/\\see +(\w+(\.\w+)*)(\([^()]*\))?/ ) {
my $seealso = $1;
my $seeline = '';
dbg_info("see also: $seealso");
if ( $seealso =~ m/^Qgs[A-Z]\w+(\([^()]*\))?$/ ) {
dbg_info("\\see py:class");
$seeline = ":py:class:`$seealso`";
}
elsif ( $seealso =~ m/^(Qgs[A-Z]\w+)\.(\w+)(\([^()]*\))?$/ ) {
dbg_info("\\see py:func with param");
$seeline = ":py:func:`$1.$2`";
}
elsif ( $seealso =~ m/^[a-z]\w+(\([^()]*\))?$/ ) {
dbg_info("\\see py:func");
$seeline = ":py:func:`$seealso`";
}
if ( $line =~ m/^\s*\\see/ ){
if ( $seeline ne ''){
return "\n.. seealso:: $seeline\n";
} else {
return "\n.. seealso:: $seealso\n";
}
} else {
if ( $seeline ne ''){
$line =~ s/\\see +(\w+(\.\w+)*(\(\))?)/$seeline/;
} else {
$line =~s/\\see/see/;
}
}
}
else
{
# create links in plain text too (less performant)
if ( $line =~ m/\b(Qgs[A-Z]\w+)\b(\.?$|[^\w]{2})/) {
if ( defined $ACTUAL_CLASS && $1 !~ $ACTUAL_CLASS ) {
$line =~ s/\b(Qgs[A-Z]\w+)\b(\.?$|[^\w]{2})/:py:class:`$1`$2/g;
}
}
$line =~ s/\b(Qgs[A-Z]\w+\.[a-z]\w+\(\))(?!\w)/:py:func:`$1`/g;
if ( defined $ACTUAL_CLASS && $ACTUAL_CLASS) {
$line =~ s/(?<!\.)\b(?:([a-z]\w+)\(\))(?!\w)/:py:func:`~$ACTUAL_CLASS.$1`/g;
}
else {
$line =~ s/(?<!\.)\b(?:([a-z]\w+)\(\))(?!\w)/:py:func:`~$1`/g;
}
}
if ( $line =~ m/[\\@]note (.*)/ ) {
$COMMENT_LAST_LINE_NOTE_WARNING = 1;
$PREV_INDENT = $INDENT;
$INDENT = '';
return "\n.. note::\n\n $1\n";
}
if ( $line =~ m/[\\@]warning (.*)/ ) {
$PREV_INDENT = $INDENT;
$INDENT = '';
$COMMENT_LAST_LINE_NOTE_WARNING = 1;
return "\n.. warning::\n\n $1\n";
}
if ( $line =~ m/[\\@]throws (.+?)\b\s*(.*)/ ) {
$PREV_INDENT = $INDENT;
$INDENT = '';
$COMMENT_LAST_LINE_NOTE_WARNING = 1;
return "\n:raises $1: $2\n";
}
if ( $line !~ m/^\s*$/ ){
if ( $COMMENT_LAST_LINE_NOTE_WARNING == 1 ){
dbg_info("prepend spaces for multiline warning/note xx$line");
$line = " $line";
}
} else {
$COMMENT_LAST_LINE_NOTE_WARNING = 0;
}
return "$line\n";
}
sub detect_and_remove_following_body_or_initializerlist {
# https://regex101.com/r/ZaP3tC/8
my $python_signature = '';
do {no warnings 'uninitialized';
if ( $LINE =~ m/^(\s*)?((?:(?:explicit|static|const|unsigned|virtual)\s+)*)(([\w:]+(<.*?>)?\s+[*&]?)?(~?\w+|(\w+::)?operator.{1,2})\s*\(([\w=()\/ ,&*<>."-]|::)*\)( +(?:const|SIP_[\w_]+?))*)\s*((\s*[:,]\s+\w+\(.*\))*\s*\{.*\}\s*(?:SIP_[\w_]+)?;?|(?!;))(\s*\/\/.*)?$/
|| $LINE =~ m/SIP_SKIP\s*(?!;)\s*(\/\/.*)?$/
|| $LINE =~ m/^\s*class.*SIP_SKIP/ ){
dbg_info("remove constructor definition, function bodies, member initializing list");
my $newline = "$1$2$3;";
$python_signature = remove_following_body_or_initializerlist() unless $LINE =~ m/{.*}(\s*SIP_\w+)*\s*(\/\/.*)?$/;
$LINE = $newline;
}
};
return $python_signature;
}
sub remove_following_body_or_initializerlist {
my $python_signature = '';
do {no warnings 'uninitialized';
dbg_info("remove constructor definition, function bodies, member initializing list");
my $line = read_line();
# python signature
if ($line =~ m/^\s*\[\s*(\w+\s*)?\(/){
dbg_info("python signature detected");
my $nesting_index = 0;
while ($LINE_IDX < $LINE_COUNT){
$nesting_index += $line =~ tr/\[//;
$nesting_index -= $line =~ tr/\]//;
if ($nesting_index == 0){
if ($line =~ m/^(.*);\s*(\/\/.*)?$/){
$line = $1; # remove semicolon (added later)
$python_signature .= "\n$line";
return $python_signature;
}
last;
}
$python_signature .= "\n$line";
$line = read_line();
}
}
# member initializing list
while ( $line =~ m/^\s*[:,]\s+([\w<>]|::)+\(.*?\)/){
dbg_info("member initializing list");
$line = read_line();
}
# body
if ( $line =~ m/^\s*\{/ ){
my $nesting_index = 0;
while ($LINE_IDX < $LINE_COUNT){
dbg_info(" remove body");
$nesting_index += $line =~ tr/\{//;
$nesting_index -= $line =~ tr/\}//;
if ($nesting_index == 0){
last;
}
$line = read_line();
}
}
};
return $python_signature;
}
sub fix_annotations {
my $line = $_[0];
# get removed params to be able to drop them out of the API doc
if ( $line =~ m/(\w+)\s+SIP_PYARGREMOVE/ ){
push @SKIPPED_PARAMS_REMOVE, $1;
dbg_info("caught removed param: $SKIPPED_PARAMS_REMOVE[$#SKIPPED_PARAMS_REMOVE]");
}
if ( $line =~ m/(\w+)\s+SIP_OUT/ ){
push @SKIPPED_PARAMS_OUT, $1;
dbg_info("caught removed param: $SKIPPED_PARAMS_OUT[$#SKIPPED_PARAMS_OUT]");
}
# printed annotations
$line =~ s/\/\/\s*SIP_ABSTRACT\b/\/Abstract\//;
$line =~ s/\bSIP_ABSTRACT\b/\/Abstract\//;
$line =~ s/\bSIP_ALLOWNONE\b/\/AllowNone\//;
$line =~ s/\bSIP_ARRAY\b/\/Array\//g;
$line =~ s/\bSIP_ARRAYSIZE\b/\/ArraySize\//g;
$line =~ s/\bSIP_DEPRECATED\b/\/Deprecated\//g;
$line =~ s/\bSIP_CONSTRAINED\b/\/Constrained\//g;
$line =~ s/\bSIP_EXTERNAL\b/\/External\//g;
$line =~ s/\bSIP_FACTORY\b/\/Factory\//;
$line =~ s/\bSIP_IN\b/\/In\//g;
$line =~ s/\bSIP_INOUT\b/\/In,Out\//g;
$line =~ s/\bSIP_KEEPREFERENCE\b/\/KeepReference\//;
$line =~ s/\bSIP_NODEFAULTCTORS\b/\/NoDefaultCtors\//;
$line =~ s/\bSIP_OUT\b/\/Out\//g;
$line =~ s/\bSIP_RELEASEGIL\b/\/ReleaseGIL\//;
$line =~ s/\bSIP_TRANSFER\b/\/Transfer\//g;
$line =~ s/\bSIP_TRANSFERBACK\b/\/TransferBack\//;
$line =~ s/\bSIP_TRANSFERTHIS\b/\/TransferThis\//;
$line =~ s/\bSIP_GETWRAPPER\b/\/GetWrapper\//;
$line =~ s/SIP_PYNAME\(\s*(\w+)\s*\)/\/PyName=$1\//;
$line =~ s/SIP_TYPEHINT\(\s*(\w+)\s*\)/\/TypeHint="$1"\//;
$line =~ s/SIP_VIRTUALERRORHANDLER\(\s*(\w+)\s*\)/\/VirtualErrorHandler=$1\//;
$line =~ s/SIP_THROW\(\s*(\w+)\s*\)/throw\( $1 \)/;
# combine multiple annotations
# https://regex101.com/r/uvCt4M/4
do {no warnings 'uninitialized';
$line =~ s/\/([\w,]+(=\w+)?)\/\s*\/([\w,]+(=\w+)?)\//\/$1,$3\//;
(! $3) or dbg_info("combine multiple annotations -- works only for 2");
};
# unprinted annotations
$line =~ s/(\w+)(\<(?>[^<>]|(?2))*\>)?\s+SIP_PYALTERNATIVETYPE\(\s*\'?([^()']+)(\(\s*(?:[^()]++|(?2))*\s*\))?\'?\s*\)/$3/g;
$line =~ s/=\s+[^=]*?\s+SIP_PYARGDEFAULT\(\s*\'?([^()']+)(\(\s*(?:[^()]++|(?2))*\s*\))?\'?\s*\)/= $1/g;
# remove argument
if ($line =~ m/SIP_PYARGREMOVE/){
dbg_info("remove arg");
if ( $MULTILINE_DEFINITION != MULTILINE_NO ){
my $prev_line = pop(@OUTPUT) =~ s/\n$//r;
# update multi line status
my $parenthesis_balance = 0;
$parenthesis_balance += $prev_line =~ tr/\(//;
$parenthesis_balance -= $prev_line =~ tr/\)//;
if ($parenthesis_balance == 1){
$MULTILINE_DEFINITION = MULTILINE_NO;
}
# concat with above line to bring previous commas
$line =~ s/^\s+//;
$line = "$prev_line $line\n";
}
# see https://regex101.com/r/5iNptO/4
$line =~ s/(?<coma>, +)?(const )?(\w+)(\<(?>[^<>]|(?4))*\>)?\s+[\w&*]+\s+SIP_PYARGREMOVE( = [^()]*(\(\s*(?:[^()]++|(?6))*\s*\))?)?(?(<coma>)|,?)//g;
$line =~ s/\(\s+\)/()/;
}
$line =~ s/SIP_FORCE//;
$line =~ s/SIP_DOC_TEMPLATE//;
$line =~ s/\s+;$/;/;
return $line;
}
sub fix_constants {
my $line = $_[0];
$line =~ s/\bstd::numeric_limits<double>::max\(\)/DBL_MAX/g;
$line =~ s/\bstd::numeric_limits<double>::lowest\(\)/-DBL_MAX/g;
$line =~ s/\bstd::numeric_limits<double>::epsilon\(\)/DBL_EPSILON/g;
$line =~ s/\bstd::numeric_limits<int>::max\(\)/INT_MAX/g;
$line =~ s/\bstd::numeric_limits<int>::min\(\)/INT_MIN/g;
return $line;
}
sub replace_macros {
my $line = $_[0];
$line =~ s/\bTRUE\b/``True``/g;
$line =~ s/\bFALSE\b/``False``/g;
$line =~ s/\bNULLPTR\b/``None``/g;
return $line;
}
# detect a comment block, return 1 if found
# if STRICT comment block shall begin at beginning of line (no code in front)
sub detect_comment_block{
my %args = ( strict_mode => STRICT, @_ );
# dbg_info("detect comment strict:" . $args{strict_mode} );
$COMMENT_PARAM_LIST = 0;
$INDENT = '';
$PREV_INDENT = '';
$COMMENT_CODE_SNIPPET = 0;
$COMMENT_LAST_LINE_NOTE_WARNING = 0;
$FOUND_SINCE = 0;
@SKIPPED_PARAMS_OUT = ();
@SKIPPED_PARAMS_REMOVE = ();
if ( $LINE =~ m/^\s*\/\*/ || $args{strict_mode} == UNSTRICT && $LINE =~ m/\/\*/ ){
dbg_info("found comment block");
do {no warnings 'uninitialized';
$COMMENT = processDoxygenLine( $LINE =~ s/^\s*\/\*(\*)?(.*?)\n?$/$2/r );
};
$COMMENT =~ s/^\s*$//;
while ($LINE !~ m/\*\/\s*(\/\/.*?)?$/){
$LINE = read_line();
$COMMENT .= processDoxygenLine( $LINE =~ s/\s*\*?(.*?)(\/)?\n?$/$1/r );
}
$COMMENT =~ s/\n\s+\n/\n\n/;
$COMMENT =~ s/\n{3,}/\n\n/;
$COMMENT =~ s/\n+$//;
return 1;
}
return 0;
}
# Detect if line is a non method member declaration
# https://regex101.com/r/gUBZUk/14
sub detect_non_method_member{
return 1 if $LINE =~ m/^\s*(?:template\s*<\w+>\s+)?(?:(const|mutable|static|friend|unsigned)\s+)*\w+(::\w+)?(<([\w<> *&,()]|::)+>)?(,?\s+\*?\w+( = (-?\d+(\.\d+)?|((QMap|QList)<[^()]+>\(\))|(\w+::)*\w+(\([^()]?\))?)|\[\d+\])?)+;/;
return 0;
}
# write some code in front of line to know where the output comes from
$debug == 0 or push @OUTPUT, "CODE SIP_RUN MultiLine\n";
# main loop
while ($LINE_IDX < $LINE_COUNT){
$PYTHON_SIGNATURE = '';
$ACTUAL_CLASS = $CLASSNAME[$#CLASSNAME] unless $#CLASSNAME < 0;
$LINE = read_line();
if ( $LINE =~ m/^\s*(#define )?+SIP_IF_MODULE\(.*\)$/ ){
dbg_info('skipping SIP include condition macro');
next;
}
if ($LINE =~ m/^\s*SIP_FEATURE\( (\w+) \)(.*)$/){
write_output("SF1", "%Feature $1$2\n");
next;
}
if ($LINE =~ m/^\s*SIP_PROPERTY\((.*)\)$/){
write_output("SF1", "%Property($1)\n");
next;
}
if ($LINE =~ m/^\s*SIP_IF_FEATURE\( (\!?\w+) \)(.*)$/){
write_output("SF2", "%If ($1)$2\n");
next;
}
if ($LINE =~ m/^\s*SIP_CONVERT_TO_SUBCLASS_CODE(.*)$/){
$LINE = "%ConvertToSubClassCode$1";
# do not go next, let run the "do not process SIP code"
}
if ($LINE =~ m/^\s*SIP_VIRTUAL_CATCHER_CODE(.*)$/){
$LINE = "%VirtualCatcherCode$1";
# do not go next, let run the "do not process SIP code"
}
if ($LINE =~ m/^\s*SIP_END(.*)$/){
write_output("SEN", "%End$1\n");
next;
}
if ( $LINE =~ s/SIP_WHEN_FEATURE\(\s*(.*?)\s*\)// ){
dbg_info('found SIP_WHEN_FEATURE');
$IF_FEATURE_CONDITION = $1;
}
# do not process SIP code %XXXCode
if ( $SIP_RUN == 1 && $LINE =~ m/^ *% *(VirtualErrorHandler|MappedType|Type(?:Header)?Code|Module(?:Header)?Code|Convert(?:From|To)(?:Type|SubClass)Code|MethodCode|Docstring)(.*)?$/ ){
$LINE = "%$1$2";
$COMMENT = '';
dbg_info("do not process SIP code");
while ( $LINE !~ m/^ *% *End/ ){
write_output("COD", $LINE."\n");
$LINE = read_line();
$LINE =~ s/^ *% *(VirtualErrorHandler|MappedType|Type(?:Header)?Code|Module(?:Header)?Code|Convert(?:From|To)(?:Type|SubClass)Code|MethodCode|Docstring)(.*)?$/%$1$2/;
$LINE =~ s/^\s*SIP_END(.*)$/%End$1/;
}
$LINE =~ s/^\s*% End/%End/;
write_output("COD", $LINE."\n");
next;
}
# do not process SIP code %Property
if ( $SIP_RUN == 1 && $LINE =~ m/^ *% *(Property)(.*)?$/ ){
$LINE = "%$1$2";
$COMMENT = '';
write_output("COD", $LINE."\n");
next;
}
# Skip preprocessor stuff
if ($LINE =~ m/^\s*#/){
# skip #if 0 blocks
if ( $LINE =~ m/^\s*#if (0|defined\(Q_OS_WIN\))/){
dbg_info("skipping #if $1 block");
my $nesting_index = 0;
while ($LINE_IDX < $LINE_COUNT){
$LINE = read_line();
if ( $LINE =~ m/^\s*#if(def)?\s+/ ){
$nesting_index++;
}
elsif ( $nesting_index == 0 && $LINE =~ m/^\s*#(endif|else)/ ){
$COMMENT = '';
last;
}
elsif ( $nesting_index != 0 && $LINE =~ m/^\s*#(endif)/ ){
$nesting_index--;
}
}
next;
}
if ( $LINE =~ m/^\s*#ifdef SIP_RUN/){
$SIP_RUN = 1;
if ($ACCESS[$#ACCESS] == PRIVATE){
dbg_info("writing private content");
write_output("PRV1", $PRIVATE_SECTION_LINE."\n") if $PRIVATE_SECTION_LINE ne '';
$PRIVATE_SECTION_LINE = '';
}
next;
}
if ( $SIP_RUN == 1 ){
if ( $LINE =~ m/^\s*#endif/ ){
if ( $GLOB_IFDEF_NESTING_IDX == 0 ){
$SIP_RUN = 0;
next;
}
else {
$GLOB_IFDEF_NESTING_IDX--;
}
}
if ( $LINE =~ m/^\s*#if(def)?\s+/ ){
$GLOB_IFDEF_NESTING_IDX++;
}
# if there is an else at this level, code will be ignored i.e. not SIP_RUN
if ( $LINE =~ m/^\s*#else/ && $GLOB_IFDEF_NESTING_IDX == 0){
while ($LINE_IDX < $LINE_COUNT){
$LINE = read_line();
if ( $LINE =~ m/^\s*#if(def)?\s+/ ){
$GLOB_IFDEF_NESTING_IDX++;
}
elsif ( $LINE =~ m/^\s*#endif/ ){
if ( $GLOB_IFDEF_NESTING_IDX == 0 ){
$COMMENT = '';
$SIP_RUN = 0;
last;
}
else {
$GLOB_IFDEF_NESTING_IDX--;
}
}
}
next;
}
}
elsif ( $LINE =~ m/^\s*#ifndef SIP_RUN/){
# code is ignored here
while ($LINE_IDX < $LINE_COUNT){
$LINE = read_line();
if ( $LINE =~ m/^\s*#if(def)?\s+/ ){
$GLOB_IFDEF_NESTING_IDX++;
}
elsif ( $LINE =~ m/^\s*#else/ && $GLOB_IFDEF_NESTING_IDX == 0 ){
# code here will be printed out
if ($ACCESS[$#ACCESS] == PRIVATE){
dbg_info("writing private content");
write_output("PRV2", $PRIVATE_SECTION_LINE."\n") if $PRIVATE_SECTION_LINE ne '';
$PRIVATE_SECTION_LINE = '';
}
$SIP_RUN = 1;
last;
}
elsif ( $LINE =~ m/^\s*#endif/ ){
if ( $GLOB_IFDEF_NESTING_IDX == 0 ){
$COMMENT = '';
$SIP_RUN = 0;
last;
}
else {
$GLOB_IFDEF_NESTING_IDX--;
}
}
}
next;
}
else {
next;
}
}
# TYPE HEADER CODE
if ( $HEADER_CODE && $SIP_RUN == 0 ){
$HEADER_CODE = 0;
write_output("HCE", "%End\n");
}
# Skip forward declarations
if ($LINE =~ m/^\s*(enum\s+)?(class|struct) \w+(?<external> *SIP_EXTERNAL)?;\s*(\/\/.*)?$/){
if ($+{external}){
dbg_info('do not skip external forward declaration');
$COMMENT = '';
}
else
{
dbg_info('skipping forward declaration');
next;
}
}
# skip friends
if ( $LINE =~ m/^\s*friend class \w+/ ){
next;
}
# insert metaoject for Q_GADGET
if ($LINE =~ m/^\s*Q_GADGET\b.*?$/){
if ($LINE !~ m/SIP_SKIP/){
dbg_info('Q_GADGET');
write_output("HCE", " public:\n");
write_output("HCE", " static const QMetaObject staticMetaObject;\n\n");
}
next;
}
# insert in python output (python/module/__init__.py)
if ($LINE =~ m/Q_(ENUM|FLAG)\(\s*(\w+)\s*\)/ ){
if ($LINE !~ m/SIP_SKIP/){
my $is_flag = $1 eq 'FLAG' ? 1 : 0;
my $enum_helper = "$ACTUAL_CLASS.$2.baseClass = $ACTUAL_CLASS";
dbg_info("Q_ENUM/Q_FLAG $enum_helper");
if ($python_output ne ''){
if ($enum_helper ne ''){
push @OUTPUT_PYTHON, "$enum_helper\n";
if ($is_flag == 1){
# SIP seems to introduce the flags in the module rather than in the class itself
# as a dirty hack, inject directly in module, hopefully we don't have flags with the same name....
push @OUTPUT_PYTHON, "$2 = $ACTUAL_CLASS # dirty hack since SIP seems to introduce the flags in module\n";
}
}
}
}
next;
}
# Skip Q_OBJECT, Q_PROPERTY, Q_ENUM etc.
if ($LINE =~ m/^\s*Q_(OBJECT|ENUMS|ENUM|FLAG|PROPERTY|DECLARE_METATYPE|DECLARE_TYPEINFO|NOWARN_DEPRECATED_(PUSH|POP))\b.*?$/){
next;
}
# SIP_SKIP
if ( $LINE =~ m/SIP_SKIP|SIP_PYTHON_SPECIAL_/ ){
dbg_info('SIP SKIP!');
# if multiline definition, remove previous lines
if ( $MULTILINE_DEFINITION != MULTILINE_NO){
dbg_info('SIP_SKIP with MultiLine');
my $opening_line = '';
while ( $opening_line !~ m/^[^()]*\(([^()]*\([^()]*\)[^()]*)*[^()]*$/){
$opening_line = pop(@OUTPUT);
$#OUTPUT >= 0 or exit_with_error('could not reach opening definition');
}
dbg_info("removed multiline definition of SIP_SKIP method");
$MULTILINE_DEFINITION = MULTILINE_NO;
}
# also skip method body if there is one
detect_and_remove_following_body_or_initializerlist();
# line skipped, go to next iteration
if ($LINE =~ m/SIP_PYTHON_SPECIAL_(\w+)\(\s*(".*"|\w+)\s*\)/ ){
my $method_or_code = $2;
dbg_info("PYTHON SPECIAL method or code: $method_or_code");
my $pyop = "${ACTUAL_CLASS}.__" . lc($1) . "__ = lambda self: ";
if ( $method_or_code =~ m/^"(.*)"$/ ){
$pyop .= $1;
}
else
{
$pyop .= "self.${method_or_code}()";
}
dbg_info("PYTHON SPECIAL $pyop");
if ($python_output ne ''){
push @OUTPUT_PYTHON, "$pyop\n";
}
}
$COMMENT = '';
next;
}
# Detect comment block
if (detect_comment_block()){
next;
}
if ( $LINE =~ m/^\s*struct(\s+\w+_EXPORT)?\s+\w+$/ ) {
dbg_info(" going to struct => public");
push @CLASSNAME, $CLASSNAME[$#CLASSNAME]; # fake new class since struct has considered similarly
push @ACCESS, PUBLIC;
push @EXPORTED, $EXPORTED[-1];
push @GLOB_BRACKET_NESTING_IDX, 0;
}
# class declaration started
# https://regex101.com/r/6FWntP/16
if ( $LINE =~ m/^(\s*(class))(\s+Q_DECL_DEPRECATED)?\s+([A-Z0-9_]+_EXPORT\s+)?(?<classname>\w+)(?<domain>\s*\:\s*(public|protected|private)\s+\w+(< *(\w|::)+ *>)?(::\w+(<\w+>)?)*(,\s*(public|protected|private)\s+\w+(< *(\w|::)+ *>)?(::\w+(<\w+>)?)*)*)?(?<annot>\s*\/?\/?\s*SIP_\w+)?\s*?(\/\/.*|(?!;))$/ ){
dbg_info("class definition started");
push @ACCESS, PUBLIC;
push @EXPORTED, 0;
push @GLOB_BRACKET_NESTING_IDX, 0;
my @template_inheritance_template = ();
my @template_inheritance_class = ();
do {no warnings 'uninitialized';
push @CLASSNAME, $+{classname};
if ($#CLASSNAME == 0){
# might be worth to add in-class classes later on
# in case of a tamplate based class declaration
# based on an in-class and in the same file
push @DECLARED_CLASSES, $CLASSNAME[$#CLASSNAME];
}
dbg_info("class: ".$CLASSNAME[$#CLASSNAME]);
if ($LINE =~ m/\b[A-Z0-9_]+_EXPORT\b/ || $#CLASSNAME != 0 || $INPUT_LINES[$LINE_IDX-2] =~ m/^\s*template</){
# class should be exported except those not at top level or template classes
# if class is not exported, then its methods should be (checked whenever leaving out the class)
$EXPORTED[-1]++;
}
};
$LINE = "$1 $+{classname}";
# Inheritance
if (defined $+{domain}){
my $m = $+{domain};
$m =~ s/public +(\w+, *)*(Ui::\w+,? *)+//g; # remove Ui::xxx public inheritance as the namespace is causing troubles
$m =~ s/public +//g;
$m =~ s/[,:]?\s*private +\w+(::\w+)?//g;
# detect template based inheritance
while ($m =~ /[,:]\s+((?!QList)\w+)< *((\w|::)+) *>/g){
dbg_info("template class");
push @template_inheritance_template, $1;
push @template_inheritance_class, $2;
}
$m =~ s/(\b(?!QList)\w+)< *((?:\w|::)+) *>/$1${2}Base/g; # use the typeded as template inheritance
$m =~ s/(\w+)< *((?:\w|::)+) *>//g; # remove remaining templates
$m =~ s/([:,])\s*,/$1/g;
$m =~ s/(\s*[:,])?\s*$//;
$LINE .= $m;
}
if (defined $+{annot})
{
$LINE .= "$+{annot}";
$LINE = fix_annotations($LINE);
}
$LINE .= "\n{\n";
if ( $COMMENT !~ m/^\s*$/ ){
$LINE .= "%Docstring\n$COMMENT\n%End\n";
}
$LINE .= "\n%TypeHeaderCode\n#include \"" . basename($headerfile) . "\"";
# for template based inheritance, add a typedef to define the base type
# add it to the class and to the TypeHeaderCode
# also include the template header
# see https://www.riverbankcomputing.com/pipermail/pyqt/2015-May/035893.html
while ( @template_inheritance_template ) {
my $tpl = pop @template_inheritance_template;
my $cls = pop @template_inheritance_class;
$LINE = "\ntypedef $tpl<$cls> ${tpl}${cls}Base;\n\n$LINE";
if ( not $tpl ~~ @DECLARED_CLASSES ){
my $tpl_header = lc $tpl . ".h";
if ( exists $SIP_CONFIG->{class_headerfile}->{$tpl} ){
$tpl_header = $SIP_CONFIG->{class_headerfile}->{$tpl};
}
$LINE .= "\n#include \"" . $tpl_header . "\"";
}
$LINE .= "\ntypedef $tpl<$cls> ${tpl}${cls}Base;";
}
if ( PRIVATE ~~ @ACCESS && $#ACCESS != 0){
# do not write anything in PRIVATE context and not top level
dbg_info("skipping class in private context");
next;
}
$ACCESS[$#ACCESS] = PRIVATE; # private by default
write_output("CLS", "$LINE\n");
# Skip opening curly bracket, incrementing hereunder
my $skip = read_line();
$skip =~ m/^\s*{\s*$/ or exit_with_error("expecting { after class definition");
$GLOB_BRACKET_NESTING_IDX[$#GLOB_BRACKET_NESTING_IDX]++;
$COMMENT = '';
$HEADER_CODE = 1;
$ACCESS[$#ACCESS] = PRIVATE;
next;
}
# bracket balance in class/struct tree
if ($SIP_RUN == 0){
my $bracket_balance = 0;
$bracket_balance += $LINE =~ tr/\{//;
$bracket_balance -= $LINE =~ tr/\}//;
if ($bracket_balance != 0){
$GLOB_BRACKET_NESTING_IDX[$#GLOB_BRACKET_NESTING_IDX] += $bracket_balance;
if ($GLOB_BRACKET_NESTING_IDX[$#GLOB_BRACKET_NESTING_IDX] == 0){
dbg_info(" going up in class/struct tree");
if ($#ACCESS > 0){
pop(@GLOB_BRACKET_NESTING_IDX);
pop(@ACCESS);
exit_with_error("Class $CLASSNAME[$#CLASSNAME] should be exported with appropriate [LIB]_EXPORT macro. If this should not be available in python, wrap it in a `#ifndef SIP_RUN` block.")
if $EXPORTED[-1] == 0 and not $CLASSNAME[$#CLASSNAME] ~~ $SIP_CONFIG->{no_export_macro};
pop @EXPORTED;
}
pop(@CLASSNAME);
if ($#ACCESS == 0){
dbg_info("reached top level");
# top level should stasy public
$ACCESS[$#ACCESS] = PUBLIC;
}
$COMMENT = '';
$RETURN_TYPE = '';
$PRIVATE_SECTION_LINE = '';
}
dbg_info("new bracket balance: @GLOB_BRACKET_NESTING_IDX");
}
}
# Private members (exclude SIP_RUN)
if ( $LINE =~ m/^\s*private( slots)?:/ ){
$ACCESS[$#ACCESS] = PRIVATE;
$PRIVATE_SECTION_LINE = $LINE;
$COMMENT = '';
dbg_info("going private");
next;
}
elsif ( $LINE =~ m/^\s*(public( slots)?|signals):.*$/ ){
dbg_info("going public");
$ACCESS[$#ACCESS] = PUBLIC;
$COMMENT = '';
}
elsif ( $LINE =~ m/^\s*(protected)( slots)?:.*$/ ){
dbg_info("going protected");
$ACCESS[$#ACCESS] = PROTECTED;
$COMMENT = '';
}
elsif ( $ACCESS[$#ACCESS] == PRIVATE && $LINE =~ m/SIP_FORCE/){
dbg_info("private with SIP_FORCE");
write_output("PRV3", $PRIVATE_SECTION_LINE."\n") if $PRIVATE_SECTION_LINE ne '';
$PRIVATE_SECTION_LINE = '';
}
elsif ( PRIVATE ~~ @ACCESS && $SIP_RUN == 0 ) {
$COMMENT = '';
next;
}
# Skip operators
if ( $ACCESS[$#ACCESS] != PRIVATE && $LINE =~ m/operator(=|<<|>>|->)\s*\(/ ){
dbg_info("skip operator");
detect_and_remove_following_body_or_initializerlist();
next;
}
# save comments and do not print them, except in SIP_RUN
if ( $SIP_RUN == 0 ){
if ( $LINE =~ m/^\s*\/\// ){
if ($LINE =~ m/^\s*\/\/\!\s*(.*?)\n?$/){
$COMMENT_PARAM_LIST = 0;
$PREV_INDENT = $INDENT;
$INDENT = '';
$COMMENT_LAST_LINE_NOTE_WARNING = 0;
$COMMENT = processDoxygenLine( $1 );
$COMMENT =~ s/\n+$//;
}
elsif ($INPUT_LINES[$LINE_IDX-1] !~ m/\*\/.*/) {
$COMMENT = '';
}
next;
}