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