gimp/tools/pdbgen/enumgen.pl

254 lines
5.9 KiB
Perl
Raw Normal View History

1999-03-20 07:04:16 +08:00
#!/usr/bin/perl -w
# The GIMP -- an image manipulation program
# Copyright (C) 1999 Manish Singh <yosh@gimp.org>
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUTFILE ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
BEGIN {
$srcdir = $ENV{srcdir} || '.';
$destdir = $ENV{srcdir} || '.';
1999-03-20 07:04:16 +08:00
}
1999-03-21 10:14:08 +08:00
use lib $srcdir;
1999-03-20 07:04:16 +08:00
use Text::Wrap qw(wrap $columns);
1999-04-04 13:59:08 +08:00
$columns = 77;
1999-03-20 07:04:16 +08:00
1999-04-04 13:59:08 +08:00
BEGIN { require 'util.pl' }
1999-03-21 10:14:08 +08:00
*write_file = \&Gimp::CodeGen::util::write_file;
*FILE_EXT = \$Gimp::CodeGen::util::FILE_EXT;
1999-03-20 07:04:16 +08:00
my $header = <<'HEADER';
:# The GIMP -- an image manipulation program
:# Copyright (C) 1999 Manish Singh <yosh@gimp.org>
:
:# This program is free software; you can redistribute it and/or modify
:# it under the terms of the GNU General Public License as published by
:# the Free Software Foundation; either version 2 of the License, or
:# (at your option) any later version.
:
:# This program is distributed in the hope that it will be useful,
:# but WITHOUTFILE ANY WARRANTY; without even the implied warranty of
:# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
:# GNU General Public License for more details.
:
:# You should have received a copy of the GNU General Public License
:# along with this program; if not, write to the Free Software
:# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
:
:# autogenerated by enumgen.pl
:
:package Gimp::CodeGen::enums;
:
:%enums = (
HEADER
my $footer = <<'FOOTER';
:);
:
:foreach $e (values %enums) {
: $e->{info} = "";
1999-04-04 13:59:08 +08:00
: foreach (@{$e->{symbols}}) {
: my $nick = exists $e->{nicks}->{$_} ? $e->{nicks}->{$_} : $_;
: $e->{info} .= "$nick ($e->{mapping}->{$_}), "
: }
1999-03-20 07:04:16 +08:00
: $e->{info} =~ s/, $//;
:}
:
:1;
FOOTER
1999-04-04 13:59:08 +08:00
my ($enumname, $contig, $symbols, @nicks, @mapping, $before, $chop);
1999-03-20 07:04:16 +08:00
# Most of this enum parsing stuff was swiped from makeenums.pl in GTK+
1999-04-04 13:59:08 +08:00
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;
}
1999-03-20 07:04:16 +08:00
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) {
1999-04-04 13:59:08 +08:00
my ($name, $value, $options) = ($1, $2, $3);
if (defined $options) {
my %options = parse_options($options);
next if defined $options{skip};
if (defined $options{nick}) {
push @nicks, $name, $options{nick};
}
}
elsif (defined $chop) {
my $nick = $name;
$nick =~ s/$chop//;
push @nicks, $name, $nick;
}
1999-03-20 07:04:16 +08:00
$symbols .= $name . ' ';
# Figure out a default value (not really foolproof)
$value = $before + 1 if !defined $value;
$value =~ s/\s*$//s;
$value =~ s/^\s*//s;
push @mapping, $name, $value;
my $test = $before + 1;
# Warnings in our eval should be fatal so they set $@
local $SIG{__WARN__} = sub { die $_[0] };
# Try to get a numeric value
eval "\$test = $value * 1;";
# Assume noncontiguous if it's not a number
$contig = 0 if $contig && ($@ || $test - 1 != $before);
$before = $test;
} else {
print STDERR "Can't understand: $_\n";
}
}
return 0;
}
my $code = "";
while (<>) {
if (eof) {
close (ARGV); # reset line numbering
}
1999-04-04 13:59:08 +08:00
if (m@^\s*typedef\s+enum\s*
({)?\s*
(?:/\*<
(([^*]|\*(?!/))*)
>\*/)?
@x) {
if (defined $2) {
my %options = parse_options($2);
$chop = $options{"chop"};
} else {
$chop = undef;
}
1999-03-20 07:04:16 +08:00
# Didn't have trailing '{' look on next lines
if (!defined $1) {
while (<>) {
if (s/^\s*\{//) {
last;
}
}
}
1999-04-04 13:59:08 +08:00
$symbols = ""; $contig = 1; $before = -1; @mapping = (); @nicks = ();
1999-03-20 07:04:16 +08:00
# Now parse the entries
&parse_entries (\*ARGV);
$symbols =~ s/\s*$//s;
$symbols = wrap("\t\t\t ", "\t\t\t " , $symbols);
$symbols =~ s/^\s*//s;
my $mapping = ""; $pos = 1;
foreach (@mapping) {
$mapping .= $pos++ % 2 ? "$_ => " : "'$_',\n\t\t ";
}
$mapping =~ s/,\n\s*$//s;
1999-04-04 13:59:08 +08:00
my $nicks = ""; $pos = 1;
foreach (@nicks) {
$nicks .= $pos++ % 2 ? "$_ => " : "'$_',\n\t\t ";
}
if ($nicks) {
$nicks =~ s/,\n\s*$//s;
$nicks = ",\n\t nicks => { " . $nicks . " }";
}
1999-04-23 14:55:37 +08:00
$ARGV =~ m@([^/]*)$@;
1999-03-20 07:04:16 +08:00
$code .= <<ENTRY;
1999-04-10 12:52:07 +08:00
: $enumname =>
: { contig => $contig,
1999-04-23 14:55:37 +08:00
: header => '$1',
1999-04-10 12:52:07 +08:00
: symbols => [ qw($symbols) ],
: mapping => { $mapping }$nicks
: },
1999-03-20 07:04:16 +08:00
ENTRY
}
}
$code =~ s/,\n$/\n/s;
1999-04-10 12:52:07 +08:00
foreach ($header, $code, $footer) { s/^://mg }
1999-03-21 10:14:08 +08:00
$outfile = "$destdir/enums.pl$FILE_EXT";
open OUTFILE, "> $outfile";
1999-03-20 07:04:16 +08:00
print OUTFILE $header, $code, $footer;
close OUTFILE;
1999-03-21 10:14:08 +08:00
&write_file($outfile);