forked from fletcher/MultiMarkdown-4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
enumsToPerl.pl
executable file
·167 lines (140 loc) · 4.54 KB
/
enumsToPerl.pl
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
#!/usr/bin/perl
# enumsToPerl:
# This Perl script creates a Perl version of the enum's that are in
# the C/C++ header file supplied as the first command-line argument.
# The second command-line argument specifies the filepath to the
# module file that is to be created.
# Sample usage:
# perl enumsToPerl MyEnums.h MyEnums.pm
#
# Cameron Hayne ([email protected]) May 2009
use strict;
use warnings;
use File::Basename;
# getEnumsFromString:
sub getEnumsFromString($)
{
my ($str) = @_;
# strip C & C++ style comments
# (regex from: http://perldoc.perl.org/perlfaq6.html)
$str =~ s#/\*[^*]*\*+([^/*][^*]*\*+)*/
|//[^\n]*
|q("(\\.|[^"\\])*" | '(\\.|[^'\\])*' | .[^/"'\\]*)
#defined $2 ? $2 : ""#gsex;
my @enums = ();
while ($str =~ /(enum\s+(\w+)\s*{([^}]*)}\s*;)/g)
{
my $enumDecl = $1;
my $enumName = $2;
my $enumBody = $3;
my $enum = {};
$enum->{name} = $enumName;
$enum->{pairs} = [];
push(@enums, $enum);
my $prevValue = -1;
while ($enumBody =~ /(\w+)\s*(?:=\s*(.+))?,?/g)
{
my $name = $1;
my $value = $2;
if (defined($value))
{
$value = eval($value);
}
else
{
$value = $prevValue + 1;
}
$prevValue = $value;
push(@{$enum->{pairs}}, [$name, $value]);
}
}
return @enums;
}
# writePerlCodeForEnums:
sub writePerlCodeForEnums($$@)
{
my ($filehandle, $useHex, @enums) = @_;
print $filehandle "# Enums:\n";
print $filehandle "# -----------------------------------------\n";
my $format = $useHex ? "\t'%s' => 0x%02x,\n"
: "\t'%s' => %d,\n";
foreach my $enum (@enums)
{
my $enumName = $enum->{name};
print $filehandle "our %$enumName = (\n";
foreach my $pair (@{$enum->{pairs}})
{
my $name = $pair->[0];
my $value = $pair->[1];
printf $filehandle ($format, $name, $value);
}
print $filehandle ");\n";
print $filehandle "\n";
}
}
# writePerlCodeForReverseEnums:
sub writePerlCodeForReverseEnums($$@)
{
my ($filehandle, $useHex, @enums) = @_;
print $filehandle "# Reverse Enums:\n";
print $filehandle "# -----------------------------------------\n";
my $format = $useHex ? "\t0x%02x => '%s',\n"
: "\t%d => '%s',\n";
foreach my $enum (@enums)
{
my $enumName = $enum->{name} . "REVERSE";
print $filehandle "our %$enumName = (\n";
foreach my $pair (@{$enum->{pairs}})
{
my $name = $pair->[0];
my $value = $pair->[1];
printf $filehandle ($format, $value, $name);
}
print $filehandle ");\n";
print $filehandle "\n";
}
}
sub writePerlCodeForModule($$$@)
{
my ($filehandle, $headerFile, $moduleName, @enums) = @_;
my $time = localtime();
my $moduleStart = << "EOT";
# This Perl module was generated: $time
# by using the 'enumsToPerl' script on the following header file:
# $headerFile
package $moduleName;
EOT
my $moduleCode = <<'EOT';
use Exporter;
our @ISA = qw(Exporter);
our @EXPORT = do {
no strict 'refs';
map { '%' . $_ } keys %{ __PACKAGE__ . '::' };
};
EOT
print $filehandle "$moduleStart\n";
print $filehandle "$moduleCode\n";
}
# MAIN
{
die "Usage: enumsToPerl headerFile moduleFile\n" unless scalar(@ARGV) == 2;
my $headerFile = $ARGV[0];
my $moduleFile = $ARGV[1];
die "Can't read header file '$headerFile'" unless -r $headerFile;
my ($basename, $dirname, $ext) = fileparse($moduleFile, qr/\.[^.]*/);
die "Can't create module file '$moduleFile'" unless -w $dirname;
my $moduleName = $basename;
undef $/; # slurp mode
open(HEADERFILE, "< $headerFile")
or die "Can't open header file '$headerFile' : $!\n";
my $contents = <HEADERFILE>;
close(HEADERFILE) or die "Failed to close header file: $!\n";
open(MODULEFILE, "> $moduleFile")
or die "Can't open module file '$moduleFile' for writing: $!\n";
my @enums = getEnumsFromString($contents);
my $useHex = 0;
writePerlCodeForModule(*MODULEFILE, $headerFile, $moduleName, @enums);
writePerlCodeForEnums(*MODULEFILE, $useHex, @enums);
writePerlCodeForReverseEnums(*MODULEFILE, $useHex, @enums);
close(MODULEFILE) or die "Failed to close module file: $!\n";
}