~ubuntu-branches/ubuntu/hoary/gimp/hoary

« back to all changes in this revision

Viewing changes to tools/pdbgen/enumgen.pl

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2005-04-04 14:51:23 UTC
  • Revision ID: james.westby@ubuntu.com-20050404145123-9py049eeelfymur8
Tags: upstream-2.2.2
ImportĀ upstreamĀ versionĀ 2.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/perl -w
 
2
 
 
3
# The GIMP -- an image manipulation program
 
4
# Copyright (C) 1999-2003 Manish Singh <yosh@gimp.org>
 
5
 
 
6
# This program is free software; you can redistribute it and/or modify
 
7
# it under the terms of the GNU General Public License as published by
 
8
# the Free Software Foundation; either version 2 of the License, or
 
9
# (at your option) any later version.
 
10
 
 
11
# This program is distributed in the hope that it will be useful,
 
12
# but WITHOUTFILE ANY WARRANTY; without even the implied warranty of
 
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
# GNU General Public License for more details.
 
15
 
 
16
# You should have received a copy of the GNU General Public License
 
17
# along with this program; if not, write to the Free Software
 
18
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
19
 
 
20
BEGIN {
 
21
    $srcdir  = $ENV{srcdir} || '.';
 
22
    $destdir = $ENV{srcdir} || '.';
 
23
}
 
24
 
 
25
use lib $srcdir;
 
26
 
 
27
use Text::Wrap qw(wrap $columns);
 
28
$columns = 77;
 
29
 
 
30
#BEGIN { require 'util.pl' }
 
31
require 'util.pl';
 
32
 
 
33
*write_file = \&Gimp::CodeGen::util::write_file;
 
34
*FILE_EXT   = \$Gimp::CodeGen::util::FILE_EXT;
 
35
 
 
36
my $header = <<'HEADER';
 
37
:# The GIMP -- an image manipulation program
 
38
:# Copyright (C) 1999-2003 Manish Singh <yosh@gimp.org>
 
39
:
 
40
:# This program is free software; you can redistribute it and/or modify
 
41
:# it under the terms of the GNU General Public License as published by
 
42
:# the Free Software Foundation; either version 2 of the License, or
 
43
:# (at your option) any later version.
 
44
:
 
45
:# This program is distributed in the hope that it will be useful,
 
46
:# but WITHOUTFILE ANY WARRANTY; without even the implied warranty of
 
47
:# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
48
:# GNU General Public License for more details.
 
49
:
 
50
:# You should have received a copy of the GNU General Public License
 
51
:# along with this program; if not, write to the Free Software
 
52
:# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
53
:
 
54
:# autogenerated by enumgen.pl
 
55
:
 
56
:package Gimp::CodeGen::enums;
 
57
:
 
58
:%enums = (
 
59
HEADER
 
60
 
 
61
my $footer = <<'FOOTER';
 
62
:);
 
63
:
 
64
:foreach $e (values %enums) {
 
65
:    $e->{info} = "";
 
66
:    foreach (@{$e->{symbols}}) {
 
67
:       $e->{info} .= "$_ ($e->{mapping}->{$_}), "
 
68
:    }
 
69
:    $e->{info} =~ s/, $//;
 
70
:}
 
71
:
 
72
:1;
 
73
FOOTER
 
74
 
 
75
my ($enumname, $contig, $symbols, @mapping, $before);
 
76
 
 
77
# Most of this enum parsing stuff was swiped from makeenums.pl in GTK+
 
78
sub parse_options {
 
79
    my $opts = shift;
 
80
    my @opts;
 
81
 
 
82
    for $opt (split /\s*,\s*/, $opts) {
 
83
        $opt =~ s/^\s*//;
 
84
        $opt =~ s/\s*$//;
 
85
        my ($key,$val) = $opt =~ /([-\w]+)(?:=(.+))?/;
 
86
        defined $val or $val = 1;
 
87
        push @opts, $key, $val;
 
88
    }
 
89
    @opts;
 
90
}
 
91
 
 
92
sub parse_entries {
 
93
    my $file = shift;
 
94
    my $file_name = shift;
 
95
    my $looking_for_name = 0;
 
96
 
 
97
    while (<$file>) {
 
98
        # Read lines until we have no open comments
 
99
        while (m@/\*([^*]|\*(?!/))*$@) {
 
100
            my $new;
 
101
            defined ($new = <$file>) || die "Unmatched comment in $ARGV";
 
102
            $_ .= $new;
 
103
        }
 
104
        # strip comments w/o options
 
105
        s@/\*(?!<)
 
106
            ([^*]+|\*(?!/))*
 
107
           \*/@@gx;
 
108
        
 
109
        s@\n@ @;
 
110
        
 
111
        next if m@^\s*$@;
 
112
 
 
113
        if ($looking_for_name) {
 
114
            if (/^\s*(\w+)/) {
 
115
                $enumname = $1;
 
116
                return 1;
 
117
            }
 
118
        }
 
119
 
 
120
        # Handle include files
 
121
        if (/^\#include\s*<([^>]*)>/ ) {
 
122
            my $file= "../$1";
 
123
            open NEWFILE, $file or die "Cannot open include file $file: $!\n";
 
124
 
 
125
            if (&parse_entries (\*NEWFILE, $NEWFILE)) {
 
126
                return 1;
 
127
            } else {
 
128
                next;
 
129
            }
 
130
        }
 
131
 
 
132
        if (/^\s*\}\s*(\w+)/) {
 
133
            $enumname = $1;
 
134
            return 1;
 
135
        }
 
136
 
 
137
        if (/^\s*\}/) {
 
138
            $looking_for_name = 1;
 
139
            next;
 
140
        }
 
141
 
 
142
        if (m@^\s*
 
143
              (\w+)\s*                   # name
 
144
              (?:=(                      # value
 
145
                   (?:[^,/]|/(?!\*))*
 
146
                  ))?,?\s*
 
147
              (?:/\*<                    # options 
 
148
                (([^*]|\*(?!/))*)
 
149
               >\s*\*/)?,?
 
150
              \s*$
 
151
             @x) {
 
152
            my ($name, $value, $options) = ($1, $2, $3);
 
153
 
 
154
            if (defined $options) {
 
155
                my %options = parse_options($options);
 
156
                next if defined $options{"pdb-skip"};
 
157
            }
 
158
 
 
159
            $symbols .= $name . ' ';
 
160
 
 
161
            # Figure out a default value (not really foolproof)
 
162
            $value = $before + 1 if !defined $value;
 
163
            $value =~ s/\s*$//s;
 
164
            $value =~ s/^\s*//s;
 
165
 
 
166
            push @mapping, $name, $value;
 
167
 
 
168
            my $test = $before + 1;
 
169
 
 
170
            # Warnings in our eval should be fatal so they set $@
 
171
            local $SIG{__WARN__} = sub { die $_[0] };
 
172
 
 
173
            # Try to get a numeric value
 
174
            eval "\$test = $value * 1;";
 
175
 
 
176
            # Assume noncontiguous if it's not a number
 
177
            $contig = 0 if $contig && ($@ || $test - 1 != $before);
 
178
 
 
179
            $before = $test;
 
180
        } elsif (m@^\s*\#@) {
 
181
            # ignore preprocessor directives
 
182
        } else {
 
183
            print STDERR "$0: $file_name:$.: Failed to parse `$_'\n";
 
184
        }
 
185
    }
 
186
    return 0;
 
187
}
 
188
 
 
189
my $code = "";
 
190
while (<>) {
 
191
    if (eof) {
 
192
        close (ARGV);           # reset line numbering
 
193
    }
 
194
 
 
195
    # read lines until we have no open comments
 
196
    while (m@/\*([^*]|\*(?!/))*$@) {
 
197
        my $new;
 
198
        defined ($new = <>) || die "Unmatched comment in $ARGV";
 
199
        $_ .= $new;
 
200
    }
 
201
    # strip comments w/o options
 
202
    s@/\*(?!<)
 
203
       ([^*]+|\*(?!/))*
 
204
       \*/@@gx;
 
205
 
 
206
    if (m@^\s*typedef\s+enum\s*
 
207
           ({)?\s*
 
208
           (?:/\*<
 
209
             (([^*]|\*(?!/))*)
 
210
            >\s*\*/)?
 
211
         @x) {
 
212
        if (defined $2) {
 
213
            my %options = parse_options($2);
 
214
            next if defined $options{"pdb-skip"};
 
215
        }           
 
216
        # Didn't have trailing '{' look on next lines
 
217
        if (!defined $1) {
 
218
            while (<>) {
 
219
                if (s/^\s*\{//) {
 
220
                    last;
 
221
                }
 
222
            }
 
223
        }
 
224
 
 
225
        $symbols = ""; $contig = 1; $before = -1; @mapping = ();
 
226
 
 
227
        # Now parse the entries
 
228
        &parse_entries (\*ARGV, $ARGV);
 
229
 
 
230
        $symbols =~ s/\s*$//s;
 
231
        $symbols = wrap("\t\t\t  ", "\t\t\t  " , $symbols);
 
232
        $symbols =~ s/^\s*//s;
 
233
 
 
234
        my $mapping = ""; $pos = 1;
 
235
        foreach (@mapping) {
 
236
            $mapping .= $pos++ % 2 ? "$_ => " : "'$_',\n\t\t       ";
 
237
        }
 
238
        $mapping =~ s/,\n\s*$//s;
 
239
 
 
240
        $ARGV =~ s@(?:(?:..|app)/)*@@;
 
241
 
 
242
        $code .= <<ENTRY;
 
243
:    $enumname =>
 
244
:       { contig => $contig,
 
245
:         header => '$ARGV',
 
246
:         symbols => [ qw($symbols) ],
 
247
:         mapping => { $mapping }
 
248
:       },
 
249
ENTRY
 
250
    }
 
251
}
 
252
 
 
253
$code =~ s/,\n$/\n/s;
 
254
 
 
255
foreach ($header, $code, $footer) { s/^://mg }
 
256
 
 
257
$outfile = "$destdir/enums.pl$FILE_EXT";
 
258
open OUTFILE, "> $outfile";
 
259
print OUTFILE $header, $code, $footer;
 
260
close OUTFILE;
 
261
&write_file($outfile);