glib-mkenums.in 13.8 KB
Newer Older
1
#!@PERL_PATH@ -w
Tim Janik's avatar
Tim Janik committed
2 3 4 5

# glib-mkenums.pl 
# Information about the current enumeration
my $flags;			# Is enumeration a bitmask?
6 7
my $option_lowercase_name;			# A lower case name to use as part of the *_get_type() function, instead of the one that we guess.
                        # For instance, when an enum uses abnormal capitalization and we can not guess where to put the underscores.
Tim Janik's avatar
Tim Janik committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
my $seenbitshift;		# Have we seen bitshift operators?
my $enum_prefix;		# Prefix for this enumeration
my $enumname;			# Name for this enumeration
my $enumshort;			# $enumname without prefix
my $enumindex = 0;		# Global enum counter
my $firstenum = 1;		# Is this the first enumeration per file?
my @entries;			# [ $name, $val ] for each entry

sub parse_trigraph {
    my $opts = shift;
    my @opts;

    for $opt (split /\s*,\s*/, $opts) {
	$opt =~ s/^\s*//;
	$opt =~ s/\s*$//;
        my ($key,$val) = $opt =~ /(\w+)(?:=(.+))?/;
	defined $val or $val = 1;
	push @opts, $key, $val;
    }
    @opts;
}
sub parse_entries {
    my $file = shift;
    my $file_name = shift;
32
    my $looking_for_name = 0;
Tim Janik's avatar
Tim Janik committed
33 34
    
    while (<$file>) {
35 36
	# read lines until we have no open comments
	while (m@/\*([^*]|\*(?!/))*$@) {
Tim Janik's avatar
Tim Janik committed
37
	    my $new;
38
	    defined ($new = <$file>) || die "Unmatched comment in $ARGV";
Tim Janik's avatar
Tim Janik committed
39 40 41
	    $_ .= $new;
	}
	# strip comments w/o options
42 43 44 45
	s@/\*(?!<)
	    ([^*]+|\*(?!/))*
	   \*/@@gx;
	
Tim Janik's avatar
Tim Janik committed
46
	# strip newlines
47
	s@\n@ @;
Tim Janik's avatar
Tim Janik committed
48 49 50 51
	
	# skip empty lines
	next if m@^\s*$@;
	
52 53 54 55 56 57
	if ($looking_for_name) {
	    if (/^\s*(\w+)/) {
		$enumname = $1;
		return 1;
	    }
	}
Tim Janik's avatar
Tim Janik committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
	
	# Handle include files
	if (/^\#include\s*<([^>]*)>/ ) {
            my $file= "../$1";
	    open NEWFILE, $file or die "Cannot open include file $file: $!\n";
	    
	    if (parse_entries (\*NEWFILE, $NEWFILE)) {
		return 1;
	    } else {
		next;
	    }
	}
	
	if (/^\s*\}\s*(\w+)/) {
	    $enumname = $1;
	    $enumindex++;
	    return 1;
	}
	
77 78 79 80 81 82
	if (/^\s*\}/) {
	    $enumindex++;
	    $looking_for_name = 1;
	    next;
	}

Tim Janik's avatar
Tim Janik committed
83 84 85
        if (m@^\s*
              (\w+)\s*                   # name
              (?:=(                      # value
86 87 88
		   \s*\w+\s*\(.*\)\s*       # macro with multiple args
		   |                        # OR
                   (?:[^,/]|/(?!\*))*       # anything but a comma or comment
Tim Janik's avatar
Tim Janik committed
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
                  ))?,?\s*
              (?:/\*<                    # options
                (([^*]|\*(?!/))*)
               >\s*\*/)?,?
              \s*$
             @x) {
            my ($name, $value, $options) = ($1,$2,$3);

	    if (!defined $flags && defined $value && $value =~ /<</) {
		$seenbitshift = 1;
	    }

	    if (defined $options) {
		my %options = parse_trigraph($options);
		if (!defined $options{skip}) {
		    push @entries, [ $name, $options{nick} ];
		}
	    } else {
		push @entries, [ $name ];
	    }
	} elsif (m@^\s*\#@) {
	    # ignore preprocessor directives
	} else {
	    print STDERR "$0: $file_name:$.: Failed to parse `$_'\n";
	}
    }

    return 0;
}

sub version {
120 121 122 123 124 125
    print "glib-mkenums version glib-@GLIB_VERSION@\n";
    print "glib-mkenums comes with ABSOLUTELY NO WARRANTY.\n";
    print "You may redistribute copies of glib-mkenums under the terms of\n";
    print "the GNU General Public License which can be found in the\n";
    print "GLib source package. Sources, examples and contact\n";
    print "information are available at http://www.gtk.org\n";
Tim Janik's avatar
Tim Janik committed
126 127 128
    exit 0;
}
sub usage {
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
    print "Usage: glib-mkenums [options] [files...]\n";
    print "  --fhead <text>             output file header\n";
    print "  --fprod <text>             per input file production\n";
    print "  --ftail <text>             output file trailer\n";
    print "  --eprod <text>             per enum text (produced prior to value itarations)\n";
    print "  --vhead <text>             value header, produced before iterating over enum values\n";
    print "  --vprod <text>             value text, produced for each enum value\n";
    print "  --vtail <text>             value tail, produced after iterating over enum values\n";
    print "  --comments <text>          comment structure\n";
    print "  --template file            template file\n";
    print "  -h, --help                 show this help message\n";
    print "  -v, --version              print version informations\n";
    print "Production text substitutions:\n";
    print "  \@EnumName\@                 PrefixTheXEnum\n";
    print "  \@enum_name\@                prefix_the_xenum\n";
    print "  \@ENUMNAME\@                 PREFIX_THE_XENUM\n";
    print "  \@ENUMSHORT\@                THE_XENUM\n";
    print "  \@VALUENAME\@                PREFIX_THE_XVALUE\n";
    print "  \@valuenick\@                the-xvalue\n";
    print "  \@type\@                     either enum or flags\n";
    print "  \@Type\@                     either Enum or Flags\n";
    print "  \@TYPE\@                     either ENUM or FLAGS\n";
    print "  \@filename\@                 name of current input file\n";
Tim Janik's avatar
Tim Janik committed
152 153 154 155 156 157 158 159 160 161 162 163 164 165
    exit 0;
}

# production variables:
my $fhead = "";   # output file header
my $fprod = "";   # per input file production
my $ftail = "";   # output file trailer
my $eprod = "";   # per enum text (produced prior to value itarations)
my $vhead = "";   # value header, produced before iterating over enum values
my $vprod = "";   # value text, produced for each enum value
my $vtail = "";   # value tail, produced after iterating over enum values
# other options
my $comment_tmpl = "/* \@comment\@ */";

Matthias Clasen's avatar
Matthias Clasen committed
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
sub read_template_file {
  my ($file) = @_;
  my %tmpl = ('file-header', $fhead, 
	      'file-production', $fprod, 
	      'file-tail', $ftail, 
	      'enumeration-production', $eprod,
	      'value-header', $vhead,
	      'value-production', $vprod,
	      'value-tail', $vtail,
	      'comment', $comment_tmpl);
  my $in = 'junk';
  open (FILE, $file) || die "Can't open $file: $!\n";
  while (<FILE>) {
    if (/^\/\*\*\*\s+(BEGIN|END)\s+([\w-]+)\s+\*\*\*\//) {
      if (($in eq 'junk') && ($1 eq 'BEGIN') && (exists($tmpl{$2}))) {
	$in = $2;
	next;
      }
      elsif (($in eq $2) && ($1 eq 'END') && (exists($tmpl{$2}))) {
	$in = 'junk';
	next;
      }
      else {
	  die "Malformed template file $file\n";
      }
    }
    if (!($in eq 'junk')) {
	$tmpl{$in} .= $_;
    }
  }
  close (FILE);
  if (!($in eq 'junk')) {
      die "Malformed template file $file\n";
  }
  $fhead = $tmpl{'file-header'};
  $fprod = $tmpl{'file-production'};
  $ftail = $tmpl{'file-tail'};
  $eprod = $tmpl{'enumeration-production'};
  $vhead = $tmpl{'value-header'};
  $vprod = $tmpl{'value-production'};
  $vtail = $tmpl{'value-tail'};
  $comment_tmpl = $tmpl{'comment'};
}

Tim Janik's avatar
Tim Janik committed
210 211 212 213 214 215
if (!defined $ARGV[0]) {
    usage;
}
while ($_ = $ARGV[0], /^-/) {
    shift;
    last if /^--$/;
Matthias Clasen's avatar
Matthias Clasen committed
216 217
    if (/^--template$/)              { read_template_file (shift); }
    elsif (/^--fhead$/)              { $fhead = $fhead . shift }
Tim Janik's avatar
Tim Janik committed
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
    elsif (/^--fprod$/)              { $fprod = $fprod . shift }
    elsif (/^--ftail$/)              { $ftail = $ftail . shift }
    elsif (/^--eprod$/)              { $eprod = $eprod . shift }
    elsif (/^--vhead$/)              { $vhead = $vhead . shift }
    elsif (/^--vprod$/)              { $vprod = $vprod . shift }
    elsif (/^--vtail$/)              { $vtail = $vtail . shift }
    elsif (/^--comments$/)           { $comment_tmpl = shift }
    elsif (/^--help$/ || /^-h$/)     { usage; }
    elsif (/^--version$/ || /^-v$/)  { version; }
    else { usage; }
}

# put auto-generation comment
{
    my $comment = $comment_tmpl;
    $comment =~ s/\@comment\@/Generated data (by glib-mkenums)/;
    print "\n" . $comment . "\n\n";
}

if (length($fhead)) {
    my $prod = $fhead;

Matthias Clasen's avatar
Matthias Clasen committed
240
    $prod =~ s/\@filename\@/$ARGV[0]/g;
Tim Janik's avatar
Tim Janik committed
241
    $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
242
    $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
243
    chomp ($prod);
Tim Janik's avatar
Tim Janik committed
244 245 246 247 248 249 250 251 252 253
		
    print "$prod\n";
}

while (<>) {
    if (eof) {
	close (ARGV);		# reset line numbering
	$firstenum = 1;		# Flag to print filename at next enum
    }

254 255
    # read lines until we have no open comments
    while (m@/\*([^*]|\*(?!/))*$@) {
Tim Janik's avatar
Tim Janik committed
256 257 258 259 260
	my $new;
	defined ($new = <>) || die "Unmatched comment in $ARGV";
	$_ .= $new;
    }
    # strip comments w/o options
261 262 263 264
    s@/\*(?!<)
       ([^*]+|\*(?!/))*
       \*/@@gx;
	
Tim Janik's avatar
Tim Janik committed
265 266 267 268 269
    if (m@^\s*typedef\s+enum\s*
           ({)?\s*
           (?:/\*<
             (([^*]|\*(?!/))*)
            >\s*\*/)?
Matthias Clasen's avatar
Matthias Clasen committed
270
           \s*({)?
Tim Janik's avatar
Tim Janik committed
271 272 273 274 275 276
         @x) {
	if (defined $2) {
	    my %options = parse_trigraph ($2);
	    next if defined $options{skip};
	    $enum_prefix = $options{prefix};
	    $flags = $options{flags};
277
      $option_lowercase_name = $options{lowercase_name};
Tim Janik's avatar
Tim Janik committed
278 279 280
	} else {
	    $enum_prefix = undef;
	    $flags = undef;
281
      $option_lowercase_name = undef;
Tim Janik's avatar
Tim Janik committed
282 283
	}
	# Didn't have trailing '{' look on next lines
Matthias Clasen's avatar
Matthias Clasen committed
284
	if (!defined $1 && !defined $4) {
Tim Janik's avatar
Tim Janik committed
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
	    while (<>) {
		if (s/^\s*\{//) {
		    last;
		}
	    }
	}

	$seenbitshift = 0;
	@entries = ();

	# Now parse the entries
	parse_entries (\*ARGV, $ARGV);

	# figure out if this was a flags or enums enumeration
	if (!defined $flags) {
	    $flags = $seenbitshift;
	}

	# Autogenerate a prefix
	if (!defined $enum_prefix) {
	    for (@entries) {
		my $nick = $_->[1];
		if (!defined $nick) {
		    my $name = $_->[0];
		    if (defined $enum_prefix) {
			my $tmp = ~ ($name ^ $enum_prefix);
			($tmp) = $tmp =~ /(^\xff*)/;
			$enum_prefix = $enum_prefix & $tmp;
		    } else {
			$enum_prefix = $name;
		    }
		}
	    }
	    if (!defined $enum_prefix) {
		$enum_prefix = "";
	    } else {
		# Trim so that it ends in an underscore
		$enum_prefix =~ s/_[^_]*$/_/;
	    }
	} else {
	    # canonicalize user defined prefixes
	    $enum_prefix = uc($enum_prefix);
	    $enum_prefix =~ s/-/_/g;
	    $enum_prefix =~ s/(.*)([^_])$/$1$2_/;
	}
	
	for $entry (@entries) {
	    my ($name,$nick) = @{$entry};
            if (!defined $nick) {
 	        ($nick = $name) =~ s/^$enum_prefix//;
	        $nick =~ tr/_/-/;
	        $nick = lc($nick);
	        @{$entry} = ($name, $nick);
            }
	}
	

	# Spit out the output
	
	# enumname is e.g. GMatchType
	$enspace = $enumname;
	$enspace =~ s/^([A-Z][a-z]*).*$/$1/;
	
	$enumshort = $enumname;
	$enumshort =~ s/^[A-Z][a-z]*//;
	$enumshort =~ s/([^A-Z])([A-Z])/$1_$2/g;
	$enumshort =~ s/([A-Z][A-Z])([A-Z][0-9a-z])/$1_$2/g;
	$enumshort = uc($enumshort);

	$enumlong = uc($enspace) . "_" . $enumshort;
	$enumsym = lc($enspace) . "_" . lc($enumshort);

357 358 359 360 361
  #The options might override the lower case name if it could not be generated correctly:
  if (defined($option_lowercase_name)) {
      $enumsym = $option_lowercase_name;
  }

Tim Janik's avatar
Tim Janik committed
362 363 364 365 366 367 368 369
	if ($firstenum) {
	    $firstenum = 0;
	    
	    if (length($fprod)) {
		my $prod = $fprod;

		$prod =~ s/\@filename\@/$ARGV/g;
		$prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
370
		$prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
371
	        chomp ($prod);
Tim Janik's avatar
Tim Janik committed
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
		
		print "$prod\n";
	    }
	}
	
	if (length($eprod)) {
	    my $prod = $eprod;

	    $prod =~ s/\@enum_name\@/$enumsym/g;
	    $prod =~ s/\@EnumName\@/$enumname/g;
	    $prod =~ s/\@ENUMSHORT\@/$enumshort/g;
	    $prod =~ s/\@ENUMNAME\@/$enumlong/g;
	    if ($flags) { $prod =~ s/\@type\@/flags/g; } else { $prod =~ s/\@type\@/enum/g; }
	    if ($flags) { $prod =~ s/\@Type\@/Flags/g; } else { $prod =~ s/\@Type\@/Enum/g; }
	    if ($flags) { $prod =~ s/\@TYPE\@/FLAGS/g; } else { $prod =~ s/\@TYPE\@/ENUM/g; }
	    $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
388
	    $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
389
            chomp ($prod);
Tim Janik's avatar
Tim Janik committed
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404

	    print "$prod\n";
	}

	if (length($vhead)) {
	    my $prod = $vhead;

	    $prod =~ s/\@enum_name\@/$enumsym/g;
            $prod =~ s/\@EnumName\@/$enumname/g;
            $prod =~ s/\@ENUMSHORT\@/$enumshort/g;
            $prod =~ s/\@ENUMNAME\@/$enumlong/g;
	    if ($flags) { $prod =~ s/\@type\@/flags/g; } else { $prod =~ s/\@type\@/enum/g; }
	    if ($flags) { $prod =~ s/\@Type\@/Flags/g; } else { $prod =~ s/\@Type\@/Enum/g; }
	    if ($flags) { $prod =~ s/\@TYPE\@/FLAGS/g; } else { $prod =~ s/\@TYPE\@/ENUM/g; }
            $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
405
            $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
406
            chomp ($prod);
Tim Janik's avatar
Tim Janik committed
407 408 409 410 411 412 413 414
	    
            print "$prod\n";
	}

	if (length($vprod)) {
	    my $prod = $vprod;
	    
	    $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
415
	    $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
Tim Janik's avatar
Tim Janik committed
416 417
	    for (@entries) {
		my ($name,$nick) = @{$_};
418
		my $tmp_prod = $prod;
Tim Janik's avatar
Tim Janik committed
419 420 421 422 423 424

		$tmp_prod =~ s/\@VALUENAME\@/$name/g;
		$tmp_prod =~ s/\@valuenick\@/$nick/g;
		if ($flags) { $tmp_prod =~ s/\@type\@/flags/g; } else { $tmp_prod =~ s/\@type\@/enum/g; }
		if ($flags) { $tmp_prod =~ s/\@Type\@/Flags/g; } else { $tmp_prod =~ s/\@Type\@/Enum/g; }
		if ($flags) { $tmp_prod =~ s/\@TYPE\@/FLAGS/g; } else { $tmp_prod =~ s/\@TYPE\@/ENUM/g; }
425
		chomp ($tmp_prod);
Tim Janik's avatar
Tim Janik committed
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441

		print "$tmp_prod\n";
	    }
	}

	if (length($vtail)) {
	    my $prod = $vtail;

	    $prod =~ s/\@enum_name\@/$enumsym/g;
            $prod =~ s/\@EnumName\@/$enumname/g;
            $prod =~ s/\@ENUMSHORT\@/$enumshort/g;
            $prod =~ s/\@ENUMNAME\@/$enumlong/g;
	    if ($flags) { $prod =~ s/\@type\@/flags/g; } else { $prod =~ s/\@type\@/enum/g; }
	    if ($flags) { $prod =~ s/\@Type\@/Flags/g; } else { $prod =~ s/\@Type\@/Enum/g; }
	    if ($flags) { $prod =~ s/\@TYPE\@/FLAGS/g; } else { $prod =~ s/\@TYPE\@/ENUM/g; }
            $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
442
            $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
443
            chomp ($prod);
Tim Janik's avatar
Tim Janik committed
444 445 446 447 448 449 450 451 452 453 454
	    
            print "$prod\n";
	}
    }
}

if (length($ftail)) {
    my $prod = $ftail;

    $prod =~ s/\@filename\@/$ARGV/g;
    $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
455
    $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
456
    chomp ($prod);
Tim Janik's avatar
Tim Janik committed
457 458 459 460 461 462 463 464 465 466
		
    print "$prod\n";
}

# put auto-generation comment
{
    my $comment = $comment_tmpl;
    $comment =~ s/\@comment\@/Generated data ends here/;
    print "\n" . $comment . "\n\n";
}