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
|
#!/usr/bin/perl -w
# Information about the current enumeration
my $flags; # Is enumeration a bitmask
my $seenbitshift; # Have we seen bitshift operators?
my $prefix; # Prefix for this enumeration
my $enumname; # Name for this enumeration
my $firstenum = 1; # Is this the first enumeration in file?
my @entries; # [ $name, $val ] for each entry
sub parse_options {
my $opts = shift;
my @opts;
for $opt (split /\s*,\s*/, $opts) {
my ($key,$val) = $opt =~ /\s*(\w+)(?:=(\S+))?/;
defined $val or $val = 1;
push @opts, $key, $val;
}
@opts;
}
sub parse_entries {
my $file = shift;
while (<$file>) {
# Read lines until we have no open comments
while (m@/\*
([^*]|\*(?!/))*$
@x) {
my $new;
defined ($new = <$file>) || die "Unmatched comment";
$_ .= $new;
}
# Now strip comments
s@/\*(?!<)
([^*]+|\*(?!/))*
\*/@@gx;
s@\n@ @;
next if m@^\s*$@;
# Handle include files
if (/^\#include\s*<([^>]*)>/ ) {
my $file= "../$1";
open NEWFILE, $file or die "Cannot open include file $file: $!\n";
if (parse_entries (\*NEWFILE)) {
return 1;
} else {
next;
}
}
if (/^\s*\}\s*(\w+)/) {
$enumname = $1;
return 1;
}
if (m@^\s*
(\w+)\s* # name
(?:=( # value
(?:[^,/]|/(?!\*))*
))?,?\s*
(?:/\*< # options
(([^*]|\*(?!/))*)
>\*/)?
\s*$
@x) {
my ($name, $value, $options) = ($1,$2,$3);
if (!defined $flags && defined $value && $value =~ /<</) {
$seenbitshift = 1;
}
if (defined $options) {
my %options = parse_options($options);
if (!defined $options{skip}) {
push @entries, [ $name, $options{nick} ];
}
} else {
push @entries, [ $name ];
}
} else {
print STDERR "Can't understand: $_\n";
}
}
return 0;
}
my $gen_arrays = 0;
my $gen_defs = 0;
my $gen_includes = 0;
my $gen_cfile = 0;
# Parse arguments
if (@ARGV) {
if ($ARGV[0] eq "arrays") {
shift @ARGV;
$gen_arrays = 1;
} elsif ($ARGV[0] eq "defs") {
shift @ARGV;
$gen_defs = 1;
} elsif ($ARGV[0] eq "include") {
shift @ARGV;
$gen_includes = 1;
} elsif ($ARGV[0] eq "cfile") {
shift @ARGV;
$gen_cfile = 1;
}
}
if ($gen_defs) {
print ";; generated by makeenums.pl ; -*- scheme -*-\n\n";
} else {
print "/* Generated by makeenums.pl */\n\n";
}
if ($gen_includes) {
print "#ifndef __GDK_ENUM_TYPES_H__\n";
print "#define __GDK_ENUM_TYPES_H__\n";
}
if ($gen_cfile) {
print "#include \"gdk.h\"\n";
}
ENUMERATION:
while (<>) {
if (eof) {
close (ARGV); # reset line numbering
$firstenum = 1; # Flag to print filename at next enum
}
if (m@^\s*typedef\s+enum\s*
({)?\s*
(?:/\*<
(([^*]|\*(?!/))*)
>\*/)?
@x) {
print "\n";
if (defined $2) {
my %options = parse_options($2);
$prefix = $options{prefix};
$flags = $options{flags};
} else {
$prefix = undef;
$flags = undef;
}
# Didn't have trailing '{' look on next lines
if (!defined $1) {
while (<>) {
if (s/^\s*\{//) {
last;
}
}
}
$seenbitshift = 0;
@entries = ();
# Now parse the entries
parse_entries (\*ARGV);
# figure out if this was a flags or enums enumeration
if (!defined $flags) {
$flags = $seenbitshift;
}
# Autogenerate a prefix
if (!defined $prefix) {
for (@entries) {
my $name = $_->[0];
if (defined $prefix) {
my $tmp = ~ ($name ^ $prefix);
($tmp) = $tmp =~ /(^\xff*)/;
$prefix = $prefix & $tmp;
} else {
$prefix = $name;
}
}
# Trim so that it ends in an underscore
$prefix =~ s/_[^_]*$/_/;
}
for $entry (@entries) {
my ($name,$nick) = @{$entry};
if (!defined $nick) {
($nick = $name) =~ s/^$prefix//;
$nick =~ tr/_/-/;
$nick = lc($nick);
@{$entry} = ($name, $nick);
}
}
# Spit out the output
my $valuename = $enumname;
$valuename =~ s/([^A-Z])([A-Z])/$1_$2/g;
$valuename =~ s/([A-Z][A-Z])([A-Z][0-9a-z])/$1_$2/g;
$valuename = lc($valuename);
my $typemacro = $enumname;
$typemacro =~ s/([^A-Z])([A-Z])/$1_$2/g;
$typemacro =~ s/([A-Z][A-Z])([A-Z][0-9a-z])/$1_$2/g;
$typemacro = uc($valuename);
$typemacro =~ s/GDK_/GDK_TYPE_/g;
if ($gen_defs) {
if ($firstenum) {
print qq(\n; enumerations from "$ARGV"\n);
$firstenum = 0;
}
print "\n(define-".($flags ? "flags" : "enum")." $enumname";
for (@entries) {
my ($name,$nick) = @{$_};
print "\n ($nick $name)";
}
print ")\n";
} elsif ($gen_arrays) {
print "static const GtkEnumValue _${valuename}_values[] = {\n";
for (@entries) {
my ($name,$nick) = @{$_};
print qq( { $name, "$name", "$nick" },\n);
}
print " { 0, NULL, NULL }\n";
print "};\n";
} elsif ($gen_includes) {
print "GType ${valuename}_get_type (void);\n";
print "#define ${typemacro} ${valuename}_get_type ()\n";
} elsif ($gen_cfile) {
print (<<EOF);
GType
${valuename}_get_type (void)
{
static GType etype = 0;
if (etype == 0)
{
EOF
if ($flags) {
print " static const GFlagsValue values[] = {\n";
} else {
print " static const GEnumValue values[] = {\n";
}
for (@entries) {
my ($name,$nick) = @{$_};
print qq( { $name, "$name", "$nick" },\n);
}
print " { 0, NULL, NULL }\n";
print " };\n";
if ($flags) {
print " etype = g_flags_register_static (\"$enumname\", values);\n";
} else {
print " etype = g_enum_register_static (\"$enumname\", values);\n";
}
print (<<EOF);
}
return etype;
}
EOF
}
print "\n";
}
}
if ($gen_includes) {
print "#endif /* __GDK_ENUMS_H__ */\n";
}
|