glib-mkenums: Fix support for comment templates
[platform/upstream/glib.git] / gobject / glib-mkenums.in
1 #!@PERL_PATH@ -w
2
3 use File::Basename;
4
5 # glib-mkenums.pl 
6 # Information about the current enumeration
7 my $flags;                      # Is enumeration a bitmask?
8 my $option_underscore_name;     # Overriden underscore variant of the enum name
9                                 # for example to fix the cases we don't get the
10                                 # mixed-case -> underscorized transform right.
11 my $option_lowercase_name;      # DEPRECATED.  A lower case name to use as part
12                                 # of the *_get_type() function, instead of the
13                                 # one that we guess. For instance, when an enum
14                                 # uses abnormal capitalization and we can not
15                                 # guess where to put the underscores.
16 my $seenbitshift;               # Have we seen bitshift operators?
17 my $enum_prefix;                # Prefix for this enumeration
18 my $enumname;                   # Name for this enumeration
19 my $enumshort;                  # $enumname without prefix
20 my $enumname_prefix;            # prefix of $enumname
21 my $enumindex = 0;              # Global enum counter
22 my $firstenum = 1;              # Is this the first enumeration per file?
23 my @entries;                    # [ $name, $val ] for each entry
24
25 sub parse_trigraph {
26     my $opts = shift;
27     my @opts;
28
29     for $opt (split /\s*,\s*/, $opts) {
30         $opt =~ s/^\s*//;
31         $opt =~ s/\s*$//;
32         my ($key,$val) = $opt =~ /(\w+)(?:=(.+))?/;
33         defined $val or $val = 1;
34         push @opts, $key, $val;
35     }
36     @opts;
37 }
38 sub parse_entries {
39     my $file = shift;
40     my $file_name = shift;
41     my $looking_for_name = 0;
42     
43     while (<$file>) {
44         # read lines until we have no open comments
45         while (m@/\*([^*]|\*(?!/))*$@) {
46             my $new;
47             defined ($new = <$file>) || die "Unmatched comment in $ARGV";
48             $_ .= $new;
49         }
50         # strip comments w/o options
51         s@/\*(?!<)
52             ([^*]+|\*(?!/))*
53            \*/@@gx;
54         
55         # strip newlines
56         s@\n@ @;
57         
58         # skip empty lines
59         next if m@^\s*$@;
60         
61         if ($looking_for_name) {
62             if (/^\s*(\w+)/) {
63                 $enumname = $1;
64                 return 1;
65             }
66         }
67         
68         # Handle include files
69         if (/^\#include\s*<([^>]*)>/ ) {
70             my $file= "../$1";
71             open NEWFILE, $file or die "Cannot open include file $file: $!\n";
72             
73             if (parse_entries (\*NEWFILE, $NEWFILE)) {
74                 return 1;
75             } else {
76                 next;
77             }
78         }
79         
80         if (/^\s*\}\s*(\w+)/) {
81             $enumname = $1;
82             $enumindex++;
83             return 1;
84         }
85         
86         if (/^\s*\}/) {
87             $enumindex++;
88             $looking_for_name = 1;
89             next;
90         }
91
92         if (m@^\s*
93               (\w+)\s*                   # name
94               (?:=(                      # value
95                    \s*\w+\s*\(.*\)\s*       # macro with multiple args
96                    |                        # OR
97                    (?:[^,/]|/(?!\*))*       # anything but a comma or comment
98                   ))?,?\s*
99               (?:/\*<                    # options
100                 (([^*]|\*(?!/))*)
101                >\s*\*/)?,?
102               \s*$
103              @x) {
104             my ($name, $value, $options) = ($1,$2,$3);
105
106             if (!defined $flags && defined $value && $value =~ /<</) {
107                 $seenbitshift = 1;
108             }
109
110             if (defined $options) {
111                 my %options = parse_trigraph($options);
112                 if (!defined $options{skip}) {
113                     push @entries, [ $name, $options{nick} ];
114                 }
115             } else {
116                 push @entries, [ $name ];
117             }
118         } elsif (m@^\s*\#@) {
119             # ignore preprocessor directives
120         } else {
121             print STDERR "$0: $file_name:$.: Failed to parse `$_'\n";
122         }
123     }
124
125     return 0;
126 }
127
128 sub version {
129     print "glib-mkenums version glib-@GLIB_VERSION@\n";
130     print "glib-mkenums comes with ABSOLUTELY NO WARRANTY.\n";
131     print "You may redistribute copies of glib-mkenums under the terms of\n";
132     print "the GNU General Public License which can be found in the\n";
133     print "GLib source package. Sources, examples and contact\n";
134     print "information are available at http://www.gtk.org\n";
135     exit 0;
136 }
137 sub usage {
138     print "Usage:\n";
139     print "  glib-mkenums [OPTION...] [FILES...]\n\n";
140     print "Help Options:\n";
141     print "  -h, --help            Show this help message\n\n";
142     print "Utility Options:\n";
143     print "  --fhead <text>        Output file header\n";
144     print "  --fprod <text>        Per input file production\n";
145     print "  --ftail <text>        Output file trailer\n";
146     print "  --eprod <text>        Per enum text (produced prior to value itarations)\n";
147     print "  --vhead <text>        Value header, produced before iterating over enum values\n";
148     print "  --vprod <text>        Value text, produced for each enum value\n";
149     print "  --vtail <text>        Value tail, produced after iterating over enum values\n";
150     print "  --comments <text>     Comment structure\n";
151     print "  --template file       Template file\n";
152     print "  -v, --version         Print version informations\n\n";
153     print "Production text substitutions:\n";
154     print "  \@EnumName\@            PrefixTheXEnum\n";
155     print "  \@enum_name\@           prefix_the_xenum\n";
156     print "  \@ENUMNAME\@            PREFIX_THE_XENUM\n";
157     print "  \@ENUMSHORT\@           THE_XENUM\n";
158     print "  \@ENUMPREFIX\@          PREFIX\n";
159     print "  \@VALUENAME\@           PREFIX_THE_XVALUE\n";
160     print "  \@valuenick\@           the-xvalue\n";
161     print "  \@type\@                either enum or flags\n";
162     print "  \@Type\@                either Enum or Flags\n";
163     print "  \@TYPE\@                either ENUM or FLAGS\n";
164     print "  \@filename\@            name of current input file\n";
165     print "  \@basename\@            base name of the current input file (Since: 2.22)\n";
166     exit 0;
167 }
168
169 # production variables:
170 my $fhead = "";   # output file header
171 my $fprod = "";   # per input file production
172 my $ftail = "";   # output file trailer
173 my $eprod = "";   # per enum text (produced prior to value itarations)
174 my $vhead = "";   # value header, produced before iterating over enum values
175 my $vprod = "";   # value text, produced for each enum value
176 my $vtail = "";   # value tail, produced after iterating over enum values
177 my $comment_tmpl = "";   # comment template
178
179 sub read_template_file {
180   my ($file) = @_;
181   my %tmpl = ('file-header', $fhead, 
182               'file-production', $fprod, 
183               'file-tail', $ftail, 
184               'enumeration-production', $eprod,
185               'value-header', $vhead,
186               'value-production', $vprod,
187               'value-tail', $vtail,
188               'comment', $comment_tmpl);
189   my $in = 'junk';
190   open (FILE, $file) || die "Can't open $file: $!\n";
191   while (<FILE>) {
192     if (/^\/\*\*\*\s+(BEGIN|END)\s+([\w-]+)\s+\*\*\*\//) {
193       if (($in eq 'junk') && ($1 eq 'BEGIN') && (exists($tmpl{$2}))) {
194         $in = $2;
195         next;
196       }
197       elsif (($in eq $2) && ($1 eq 'END') && (exists($tmpl{$2}))) {
198         $in = 'junk';
199         next;
200       } else {
201           die "Malformed template file $file\n";
202       }
203     }
204     if (!($in eq 'junk')) {
205         $tmpl{$in} .= $_;
206     }
207   }
208   close (FILE);
209   if (!($in eq 'junk')) {
210       die "Malformed template file $file\n";
211   }
212   $fhead = $tmpl{'file-header'};
213   $fprod = $tmpl{'file-production'};
214   $ftail = $tmpl{'file-tail'};
215   $eprod = $tmpl{'enumeration-production'};
216   $vhead = $tmpl{'value-header'};
217   $vprod = $tmpl{'value-production'};
218   $vtail = $tmpl{'value-tail'};
219   $comment_tmpl = $tmpl{'comment'};
220
221   # default to C-style comments
222   $comment_tmpl = "/* \@comment\@ */" if $comment_tmpl eq "";
223 }
224
225 if (!defined $ARGV[0]) {
226     usage;
227 }
228 while ($_=$ARGV[0],/^-/) {
229     shift;
230     last if /^--$/;
231     if (/^--template$/)                      { read_template_file (shift); }
232     elsif (/^--fhead$/)                      { $fhead = $fhead . shift }
233     elsif (/^--fprod$/)                      { $fprod = $fprod . shift }
234     elsif (/^--ftail$/)                      { $ftail = $ftail . shift }
235     elsif (/^--eprod$/)                      { $eprod = $eprod . shift }
236     elsif (/^--vhead$/)                      { $vhead = $vhead . shift }
237     elsif (/^--vprod$/)                      { $vprod = $vprod . shift }
238     elsif (/^--vtail$/)                      { $vtail = $vtail . shift }
239     elsif (/^--comments$/)                   { $comment_tmpl = shift }
240     elsif (/^--help$/ || /^-h$/ || /^-\?$/)  { usage; }
241     elsif (/^--version$/ || /^-v$/)          { version; }
242     else { usage; }
243     last if not defined($ARGV[0]);
244 }
245
246 # put auto-generation comment
247 {
248     my $comment = $comment_tmpl;
249     $comment =~ s/\@comment\@/Generated data (by glib-mkenums)/;
250     print "\n" . $comment . "\n\n";
251 }
252
253 if (length($fhead)) {
254     my $prod = $fhead;
255     my $base = basename ($ARGV[0]);
256
257     $prod =~ s/\@filename\@/$ARGV[0]/g;
258     $prod =~ s/\@basename\@/$base/g;
259     $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
260     $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
261     chomp ($prod);
262                 
263     print "$prod\n";
264 }
265
266 while (<>) {
267     if (eof) {
268         close (ARGV);           # reset line numbering
269         $firstenum = 1;         # Flag to print filename at next enum
270     }
271
272     # read lines until we have no open comments
273     while (m@/\*([^*]|\*(?!/))*$@) {
274         my $new;
275         defined ($new = <>) || die "Unmatched comment in $ARGV";
276         $_ .= $new;
277     }
278     # strip comments w/o options
279     s@/\*(?!<)
280        ([^*]+|\*(?!/))*
281        \*/@@gx;
282         
283     if (m@^\s*typedef\s+enum\s*
284            ({)?\s*
285            (?:/\*<
286              (([^*]|\*(?!/))*)
287             >\s*\*/)?
288            \s*({)?
289          @x) {
290         if (defined $2) {
291             my %options = parse_trigraph ($2);
292             next if defined $options{skip};
293             $enum_prefix = $options{prefix};
294             $flags = $options{flags};
295             $option_lowercase_name = $options{lowercase_name};
296             $option_underscore_name = $options{underscore_name};
297         } else {
298             $enum_prefix = undef;
299             $flags = undef;
300             $option_lowercase_name = undef;
301             $option_underscore_name = undef;
302         }
303         if (defined $option_lowercase_name) {
304             if (defined $option_underscore_name) {
305                 print STDERR "$0: $ARGV:$.: lowercase_name overriden with underscore_name\n";
306                 $option_lowercase_name = undef;
307             } else {
308                 print STDERR "$0: $ARGV:$.: lowercase_name is deprecated, use underscore_name\n";
309             }
310         }
311         # Didn't have trailing '{' look on next lines
312         if (!defined $1 && !defined $4) {
313             while (<>) {
314                 if (s/^\s*\{//) {
315                     last;
316                 }
317             }
318         }
319
320         $seenbitshift = 0;
321         @entries = ();
322
323         # Now parse the entries
324         parse_entries (\*ARGV, $ARGV);
325
326         # figure out if this was a flags or enums enumeration
327         if (!defined $flags) {
328             $flags = $seenbitshift;
329         }
330
331         # Autogenerate a prefix
332         if (!defined $enum_prefix) {
333             for (@entries) {
334                 my $nick = $_->[1];
335                 if (!defined $nick) {
336                     my $name = $_->[0];
337                     if (defined $enum_prefix) {
338                         my $tmp = ~ ($name ^ $enum_prefix);
339                         ($tmp) = $tmp =~ /(^\xff*)/;
340                         $enum_prefix = $enum_prefix & $tmp;
341                     } else {
342                         $enum_prefix = $name;
343                     }
344                 }
345             }
346             if (!defined $enum_prefix) {
347                 $enum_prefix = "";
348             } else {
349                 # Trim so that it ends in an underscore
350                 $enum_prefix =~ s/_[^_]*$/_/;
351             }
352         } else {
353             # canonicalize user defined prefixes
354             $enum_prefix = uc($enum_prefix);
355             $enum_prefix =~ s/-/_/g;
356             $enum_prefix =~ s/(.*)([^_])$/$1$2_/;
357         }
358         
359         for $entry (@entries) {
360             my ($name,$nick) = @{$entry};
361             if (!defined $nick) {
362                 ($nick = $name) =~ s/^$enum_prefix//;
363                 $nick =~ tr/_/-/;
364                 $nick = lc($nick);
365                 @{$entry} = ($name, $nick);
366             }
367         }
368         
369
370         # Spit out the output
371         if (defined $option_underscore_name) {
372             $enumlong = uc $option_underscore_name;
373             $enumsym = lc $option_underscore_name;
374             $enumshort = $enumlong;
375             $enumshort =~ s/^[A-Z][A-Z0-9]*_//;
376
377             $enumname_prefix = $enumlong;
378             $enumname_prefix =~ s/$enumshort$//;
379         } else {
380             # enumname is e.g. GMatchType
381             $enspace = $enumname;
382             $enspace =~ s/^([A-Z][a-z]*).*$/$1/;
383
384             $enumshort = $enumname;
385             $enumshort =~ s/^[A-Z][a-z]*//;
386             $enumshort =~ s/([^A-Z])([A-Z])/$1_$2/g;
387             $enumshort =~ s/([A-Z][A-Z])([A-Z][0-9a-z])/$1_$2/g;
388             $enumshort = uc($enumshort);
389
390             $enumname_prefix = $enumname;
391             $enumname_prefix =~ s/^([A-Z][a-z]*).*$/$1/;
392             $enumname_prefix = uc($enumname_prefix);
393
394             $enumlong = uc($enspace) . "_" . $enumshort;
395             $enumsym = lc($enspace) . "_" . lc($enumshort);
396
397             if (defined($option_lowercase_name)) {
398                 $enumsym = $option_lowercase_name;
399             }
400         }
401
402         if ($firstenum) {
403             $firstenum = 0;
404             
405             if (length($fprod)) {
406                 my $prod = $fprod;
407                 my $base = basename ($ARGV);
408
409                 $prod =~ s/\@filename\@/$ARGV/g;
410                 $prod =~ s/\@basename\@/$base/g;
411                 $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
412                 $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
413                 chomp ($prod);
414                 
415                 print "$prod\n";
416             }
417         }
418         
419         if (length($eprod)) {
420             my $prod = $eprod;
421
422             $prod =~ s/\@enum_name\@/$enumsym/g;
423             $prod =~ s/\@EnumName\@/$enumname/g;
424             $prod =~ s/\@ENUMSHORT\@/$enumshort/g;
425             $prod =~ s/\@ENUMNAME\@/$enumlong/g;
426             $prod =~ s/\@ENUMPREFIX\@/$enumname_prefix/g;
427             if ($flags) { $prod =~ s/\@type\@/flags/g; } else { $prod =~ s/\@type\@/enum/g; }
428             if ($flags) { $prod =~ s/\@Type\@/Flags/g; } else { $prod =~ s/\@Type\@/Enum/g; }
429             if ($flags) { $prod =~ s/\@TYPE\@/FLAGS/g; } else { $prod =~ s/\@TYPE\@/ENUM/g; }
430             $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
431             $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
432             chomp ($prod);
433
434             print "$prod\n";
435         }
436
437         if (length($vhead)) {
438             my $prod = $vhead;
439
440             $prod =~ s/\@enum_name\@/$enumsym/g;
441             $prod =~ s/\@EnumName\@/$enumname/g;
442             $prod =~ s/\@ENUMSHORT\@/$enumshort/g;
443             $prod =~ s/\@ENUMNAME\@/$enumlong/g;
444             $prod =~ s/\@ENUMPREFIX\@/$enumname_prefix/g;
445             if ($flags) { $prod =~ s/\@type\@/flags/g; } else { $prod =~ s/\@type\@/enum/g; }
446             if ($flags) { $prod =~ s/\@Type\@/Flags/g; } else { $prod =~ s/\@Type\@/Enum/g; }
447             if ($flags) { $prod =~ s/\@TYPE\@/FLAGS/g; } else { $prod =~ s/\@TYPE\@/ENUM/g; }
448             $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
449             $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
450             chomp ($prod);
451             
452             print "$prod\n";
453         }
454
455         if (length($vprod)) {
456             my $prod = $vprod;
457             
458             $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
459             $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
460             for (@entries) {
461                 my ($name,$nick) = @{$_};
462                 my $tmp_prod = $prod;
463
464                 $tmp_prod =~ s/\@VALUENAME\@/$name/g;
465                 $tmp_prod =~ s/\@valuenick\@/$nick/g;
466                 if ($flags) { $tmp_prod =~ s/\@type\@/flags/g; } else { $tmp_prod =~ s/\@type\@/enum/g; }
467                 if ($flags) { $tmp_prod =~ s/\@Type\@/Flags/g; } else { $tmp_prod =~ s/\@Type\@/Enum/g; }
468                 if ($flags) { $tmp_prod =~ s/\@TYPE\@/FLAGS/g; } else { $tmp_prod =~ s/\@TYPE\@/ENUM/g; }
469                 chomp ($tmp_prod);
470
471                 print "$tmp_prod\n";
472             }
473         }
474
475         if (length($vtail)) {
476             my $prod = $vtail;
477
478             $prod =~ s/\@enum_name\@/$enumsym/g;
479             $prod =~ s/\@EnumName\@/$enumname/g;
480             $prod =~ s/\@ENUMSHORT\@/$enumshort/g;
481             $prod =~ s/\@ENUMNAME\@/$enumlong/g;
482             $prod =~ s/\@ENUMPREFIX\@/$enumname_prefix/g;
483             if ($flags) { $prod =~ s/\@type\@/flags/g; } else { $prod =~ s/\@type\@/enum/g; }
484             if ($flags) { $prod =~ s/\@Type\@/Flags/g; } else { $prod =~ s/\@Type\@/Enum/g; }
485             if ($flags) { $prod =~ s/\@TYPE\@/FLAGS/g; } else { $prod =~ s/\@TYPE\@/ENUM/g; }
486             $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
487             $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
488             chomp ($prod);
489             
490             print "$prod\n";
491         }
492     }
493 }
494
495 if (length($ftail)) {
496     my $prod = $ftail;
497     my $base = basename ($ARGV);
498
499     $prod =~ s/\@filename\@/$ARGV/g;
500     $prod =~ s/\@basename\@/$base/g;
501     $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
502     $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
503     chomp ($prod);
504                 
505     print "$prod\n";
506 }
507
508 # put auto-generation comment
509 {
510     my $comment = $comment_tmpl;
511     $comment =~ s/\@comment\@/Generated data ends here/;
512     print "\n" . $comment . "\n\n";
513 }