You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

glib-mkenums 16 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. #!/bin/perl -w
  2. use File::Basename;
  3. use Safe;
  4. # glib-mkenums.pl
  5. # Information about the current enumeration
  6. my $flags; # Is enumeration a bitmask?
  7. my $option_underscore_name; # Overriden underscore variant of the enum name
  8. # for example to fix the cases we don't get the
  9. # mixed-case -> underscorized transform right.
  10. my $option_lowercase_name; # DEPRECATED. A lower case name to use as part
  11. # of the *_get_type() function, instead of the
  12. # one that we guess. For instance, when an enum
  13. # uses abnormal capitalization and we can not
  14. # guess where to put the underscores.
  15. my $seenbitshift; # Have we seen bitshift operators?
  16. my $enum_prefix; # Prefix for this enumeration
  17. my $enumname; # Name for this enumeration
  18. my $enumshort; # $enumname without prefix
  19. my $enumname_prefix; # prefix of $enumname
  20. my $enumindex = 0; # Global enum counter
  21. my $firstenum = 1; # Is this the first enumeration per file?
  22. my @entries; # [ $name, $val ] for each entry
  23. my $sandbox = Safe->new; # sandbox for safe evaluation of expressions
  24. sub parse_trigraph {
  25. my $opts = shift;
  26. my @opts;
  27. for $opt (split /\s*,\s*/, $opts) {
  28. $opt =~ s/^\s*//;
  29. $opt =~ s/\s*$//;
  30. my ($key,$val) = $opt =~ /(\w+)(?:=(.+))?/;
  31. defined $val or $val = 1;
  32. push @opts, $key, $val;
  33. }
  34. @opts;
  35. }
  36. sub parse_entries {
  37. my $file = shift;
  38. my $file_name = shift;
  39. my $looking_for_name = 0;
  40. while (<$file>) {
  41. # read lines until we have no open comments
  42. while (m@/\*([^*]|\*(?!/))*$@) {
  43. my $new;
  44. defined ($new = <$file>) || die "Unmatched comment in $ARGV";
  45. $_ .= $new;
  46. }
  47. # strip comments w/o options
  48. s@/\*(?!<)
  49. ([^*]+|\*(?!/))*
  50. \*/@@gx;
  51. # strip newlines
  52. s@\n@ @;
  53. # skip empty lines
  54. next if m@^\s*$@;
  55. if ($looking_for_name) {
  56. if (/^\s*(\w+)/) {
  57. $enumname = $1;
  58. return 1;
  59. }
  60. }
  61. # Handle include files
  62. if (/^\#include\s*<([^>]*)>/ ) {
  63. my $file= "../$1";
  64. open NEWFILE, $file or die "Cannot open include file $file: $!\n";
  65. if (parse_entries (\*NEWFILE, $NEWFILE)) {
  66. return 1;
  67. } else {
  68. next;
  69. }
  70. }
  71. if (/^\s*\}\s*(\w+)/) {
  72. $enumname = $1;
  73. $enumindex++;
  74. return 1;
  75. }
  76. if (/^\s*\}/) {
  77. $enumindex++;
  78. $looking_for_name = 1;
  79. next;
  80. }
  81. if (m@^\s*
  82. (\w+)\s* # name
  83. (?:=( # value
  84. \s*\w+\s*\(.*\)\s* # macro with multiple args
  85. | # OR
  86. (?:[^,/]|/(?!\*))* # anything but a comma or comment
  87. ))?,?\s*
  88. (?:/\*< # options
  89. (([^*]|\*(?!/))*)
  90. >\s*\*/)?,?
  91. \s*$
  92. @x) {
  93. my ($name, $value, $options) = ($1,$2,$3);
  94. if (!defined $flags && defined $value && $value =~ /<</) {
  95. $seenbitshift = 1;
  96. }
  97. if (defined $options) {
  98. my %options = parse_trigraph($options);
  99. if (!defined $options{skip}) {
  100. push @entries, [ $name, $value, $options{nick} ];
  101. }
  102. } else {
  103. push @entries, [ $name, $value ];
  104. }
  105. } elsif (m@^\s*\#@) {
  106. # ignore preprocessor directives
  107. } else {
  108. print STDERR "$0: $file_name:$.: Failed to parse `$_'\n";
  109. }
  110. }
  111. return 0;
  112. }
  113. sub version {
  114. print "glib-mkenums version glib-2.26.1\n";
  115. print "glib-mkenums comes with ABSOLUTELY NO WARRANTY.\n";
  116. print "You may redistribute copies of glib-mkenums under the terms of\n";
  117. print "the GNU General Public License which can be found in the\n";
  118. print "GLib source package. Sources, examples and contact\n";
  119. print "information are available at http://www.gtk.org\n";
  120. exit 0;
  121. }
  122. sub usage {
  123. print "Usage:\n";
  124. print " glib-mkenums [OPTION...] [FILES...]\n\n";
  125. print "Help Options:\n";
  126. print " -h, --help Show this help message\n\n";
  127. print "Utility Options:\n";
  128. print " --fhead <text> Output file header\n";
  129. print " --fprod <text> Per input file production\n";
  130. print " --ftail <text> Output file trailer\n";
  131. print " --eprod <text> Per enum text (produced prior to value itarations)\n";
  132. print " --vhead <text> Value header, produced before iterating over enum values\n";
  133. print " --vprod <text> Value text, produced for each enum value\n";
  134. print " --vtail <text> Value tail, produced after iterating over enum values\n";
  135. print " --comments <text> Comment structure\n";
  136. print " --template file Template file\n";
  137. print " -v, --version Print version informations\n\n";
  138. print "Production text substitutions:\n";
  139. print " \@EnumName\@ PrefixTheXEnum\n";
  140. print " \@enum_name\@ prefix_the_xenum\n";
  141. print " \@ENUMNAME\@ PREFIX_THE_XENUM\n";
  142. print " \@ENUMSHORT\@ THE_XENUM\n";
  143. print " \@ENUMPREFIX\@ PREFIX\n";
  144. print " \@VALUENAME\@ PREFIX_THE_XVALUE\n";
  145. print " \@valuenick\@ the-xvalue\n";
  146. print " \@valuenum\@ the integer value (limited support, Since: 2.26)\n";
  147. print " \@type\@ either enum or flags\n";
  148. print " \@Type\@ either Enum or Flags\n";
  149. print " \@TYPE\@ either ENUM or FLAGS\n";
  150. print " \@filename\@ name of current input file\n";
  151. print " \@basename\@ base name of the current input file (Since: 2.22)\n";
  152. exit 0;
  153. }
  154. # production variables:
  155. my $fhead = ""; # output file header
  156. my $fprod = ""; # per input file production
  157. my $ftail = ""; # output file trailer
  158. my $eprod = ""; # per enum text (produced prior to value itarations)
  159. my $vhead = ""; # value header, produced before iterating over enum values
  160. my $vprod = ""; # value text, produced for each enum value
  161. my $vtail = ""; # value tail, produced after iterating over enum values
  162. my $comment_tmpl = ""; # comment template
  163. sub read_template_file {
  164. my ($file) = @_;
  165. my %tmpl = ('file-header', $fhead,
  166. 'file-production', $fprod,
  167. 'file-tail', $ftail,
  168. 'enumeration-production', $eprod,
  169. 'value-header', $vhead,
  170. 'value-production', $vprod,
  171. 'value-tail', $vtail,
  172. 'comment', $comment_tmpl);
  173. my $in = 'junk';
  174. open (FILE, $file) || die "Can't open $file: $!\n";
  175. while (<FILE>) {
  176. if (/^\/\*\*\*\s+(BEGIN|END)\s+([\w-]+)\s+\*\*\*\//) {
  177. if (($in eq 'junk') && ($1 eq 'BEGIN') && (exists($tmpl{$2}))) {
  178. $in = $2;
  179. next;
  180. }
  181. elsif (($in eq $2) && ($1 eq 'END') && (exists($tmpl{$2}))) {
  182. $in = 'junk';
  183. next;
  184. } else {
  185. die "Malformed template file $file\n";
  186. }
  187. }
  188. if (!($in eq 'junk')) {
  189. $tmpl{$in} .= $_;
  190. }
  191. }
  192. close (FILE);
  193. if (!($in eq 'junk')) {
  194. die "Malformed template file $file\n";
  195. }
  196. $fhead = $tmpl{'file-header'};
  197. $fprod = $tmpl{'file-production'};
  198. $ftail = $tmpl{'file-tail'};
  199. $eprod = $tmpl{'enumeration-production'};
  200. $vhead = $tmpl{'value-header'};
  201. $vprod = $tmpl{'value-production'};
  202. $vtail = $tmpl{'value-tail'};
  203. $comment_tmpl = $tmpl{'comment'};
  204. # default to C-style comments
  205. $comment_tmpl = "/* \@comment\@ */" if $comment_tmpl eq "";
  206. }
  207. if (!defined $ARGV[0]) {
  208. usage;
  209. }
  210. while ($_=$ARGV[0],/^-/) {
  211. shift;
  212. last if /^--$/;
  213. if (/^--template$/) { read_template_file (shift); }
  214. elsif (/^--fhead$/) { $fhead = $fhead . shift }
  215. elsif (/^--fprod$/) { $fprod = $fprod . shift }
  216. elsif (/^--ftail$/) { $ftail = $ftail . shift }
  217. elsif (/^--eprod$/) { $eprod = $eprod . shift }
  218. elsif (/^--vhead$/) { $vhead = $vhead . shift }
  219. elsif (/^--vprod$/) { $vprod = $vprod . shift }
  220. elsif (/^--vtail$/) { $vtail = $vtail . shift }
  221. elsif (/^--comments$/) { $comment_tmpl = shift }
  222. elsif (/^--help$/ || /^-h$/ || /^-\?$/) { usage; }
  223. elsif (/^--version$/ || /^-v$/) { version; }
  224. else { usage; }
  225. last if not defined($ARGV[0]);
  226. }
  227. # put auto-generation comment
  228. {
  229. my $comment = $comment_tmpl;
  230. $comment =~ s/\@comment\@/Generated data (by glib-mkenums)/;
  231. print "\n" . $comment . "\n\n";
  232. }
  233. if (length($fhead)) {
  234. my $prod = $fhead;
  235. my $base = basename ($ARGV[0]);
  236. $prod =~ s/\@filename\@/$ARGV[0]/g;
  237. $prod =~ s/\@basename\@/$base/g;
  238. $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
  239. $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
  240. chomp ($prod);
  241. print "$prod\n";
  242. }
  243. while (<>) {
  244. if (eof) {
  245. close (ARGV); # reset line numbering
  246. $firstenum = 1; # Flag to print filename at next enum
  247. }
  248. # read lines until we have no open comments
  249. while (m@/\*([^*]|\*(?!/))*$@) {
  250. my $new;
  251. defined ($new = <>) || die "Unmatched comment in $ARGV";
  252. $_ .= $new;
  253. }
  254. # strip comments w/o options
  255. s@/\*(?!<)
  256. ([^*]+|\*(?!/))*
  257. \*/@@gx;
  258. if (m@^\s*typedef\s+enum\s*
  259. ({)?\s*
  260. (?:/\*<
  261. (([^*]|\*(?!/))*)
  262. >\s*\*/)?
  263. \s*({)?
  264. @x) {
  265. if (defined $2) {
  266. my %options = parse_trigraph ($2);
  267. next if defined $options{skip};
  268. $enum_prefix = $options{prefix};
  269. $flags = $options{flags};
  270. $option_lowercase_name = $options{lowercase_name};
  271. $option_underscore_name = $options{underscore_name};
  272. } else {
  273. $enum_prefix = undef;
  274. $flags = undef;
  275. $option_lowercase_name = undef;
  276. $option_underscore_name = undef;
  277. }
  278. if (defined $option_lowercase_name) {
  279. if (defined $option_underscore_name) {
  280. print STDERR "$0: $ARGV:$.: lowercase_name overriden with underscore_name\n";
  281. $option_lowercase_name = undef;
  282. } else {
  283. print STDERR "$0: $ARGV:$.: lowercase_name is deprecated, use underscore_name\n";
  284. }
  285. }
  286. # Didn't have trailing '{' look on next lines
  287. if (!defined $1 && !defined $4) {
  288. while (<>) {
  289. if (s/^\s*\{//) {
  290. last;
  291. }
  292. }
  293. }
  294. $seenbitshift = 0;
  295. @entries = ();
  296. # Now parse the entries
  297. parse_entries (\*ARGV, $ARGV);
  298. # figure out if this was a flags or enums enumeration
  299. if (!defined $flags) {
  300. $flags = $seenbitshift;
  301. }
  302. # Autogenerate a prefix
  303. if (!defined $enum_prefix) {
  304. for (@entries) {
  305. my $nick = $_->[2];
  306. if (!defined $nick) {
  307. my $name = $_->[0];
  308. if (defined $enum_prefix) {
  309. my $tmp = ~ ($name ^ $enum_prefix);
  310. ($tmp) = $tmp =~ /(^\xff*)/;
  311. $enum_prefix = $enum_prefix & $tmp;
  312. } else {
  313. $enum_prefix = $name;
  314. }
  315. }
  316. }
  317. if (!defined $enum_prefix) {
  318. $enum_prefix = "";
  319. } else {
  320. # Trim so that it ends in an underscore
  321. $enum_prefix =~ s/_[^_]*$/_/;
  322. }
  323. } else {
  324. # canonicalize user defined prefixes
  325. $enum_prefix = uc($enum_prefix);
  326. $enum_prefix =~ s/-/_/g;
  327. $enum_prefix =~ s/(.*)([^_])$/$1$2_/;
  328. }
  329. for $entry (@entries) {
  330. my ($name,$num,$nick) = @{$entry};
  331. if (!defined $nick) {
  332. ($nick = $name) =~ s/^$enum_prefix//;
  333. $nick =~ tr/_/-/;
  334. $nick = lc($nick);
  335. @{$entry} = ($name, $num, $nick);
  336. }
  337. }
  338. # Spit out the output
  339. if (defined $option_underscore_name) {
  340. $enumlong = uc $option_underscore_name;
  341. $enumsym = lc $option_underscore_name;
  342. $enumshort = $enumlong;
  343. $enumshort =~ s/^[A-Z][A-Z0-9]*_//;
  344. $enumname_prefix = $enumlong;
  345. $enumname_prefix =~ s/$enumshort$//;
  346. } else {
  347. # enumname is e.g. GMatchType
  348. $enspace = $enumname;
  349. $enspace =~ s/^([A-Z][a-z]*).*$/$1/;
  350. $enumshort = $enumname;
  351. $enumshort =~ s/^[A-Z][a-z]*//;
  352. $enumshort =~ s/([^A-Z])([A-Z])/$1_$2/g;
  353. $enumshort =~ s/([A-Z][A-Z])([A-Z][0-9a-z])/$1_$2/g;
  354. $enumshort = uc($enumshort);
  355. $enumname_prefix = $enumname;
  356. $enumname_prefix =~ s/^([A-Z][a-z]*).*$/$1/;
  357. $enumname_prefix = uc($enumname_prefix);
  358. $enumlong = uc($enspace) . "_" . $enumshort;
  359. $enumsym = lc($enspace) . "_" . lc($enumshort);
  360. if (defined($option_lowercase_name)) {
  361. $enumsym = $option_lowercase_name;
  362. }
  363. }
  364. if ($firstenum) {
  365. $firstenum = 0;
  366. if (length($fprod)) {
  367. my $prod = $fprod;
  368. my $base = basename ($ARGV);
  369. $prod =~ s/\@filename\@/$ARGV/g;
  370. $prod =~ s/\@basename\@/$base/g;
  371. $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
  372. $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
  373. chomp ($prod);
  374. print "$prod\n";
  375. }
  376. }
  377. if (length($eprod)) {
  378. my $prod = $eprod;
  379. $prod =~ s/\@enum_name\@/$enumsym/g;
  380. $prod =~ s/\@EnumName\@/$enumname/g;
  381. $prod =~ s/\@ENUMSHORT\@/$enumshort/g;
  382. $prod =~ s/\@ENUMNAME\@/$enumlong/g;
  383. $prod =~ s/\@ENUMPREFIX\@/$enumname_prefix/g;
  384. if ($flags) { $prod =~ s/\@type\@/flags/g; } else { $prod =~ s/\@type\@/enum/g; }
  385. if ($flags) { $prod =~ s/\@Type\@/Flags/g; } else { $prod =~ s/\@Type\@/Enum/g; }
  386. if ($flags) { $prod =~ s/\@TYPE\@/FLAGS/g; } else { $prod =~ s/\@TYPE\@/ENUM/g; }
  387. $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);
  390. print "$prod\n";
  391. }
  392. if (length($vhead)) {
  393. my $prod = $vhead;
  394. $prod =~ s/\@enum_name\@/$enumsym/g;
  395. $prod =~ s/\@EnumName\@/$enumname/g;
  396. $prod =~ s/\@ENUMSHORT\@/$enumshort/g;
  397. $prod =~ s/\@ENUMNAME\@/$enumlong/g;
  398. $prod =~ s/\@ENUMPREFIX\@/$enumname_prefix/g;
  399. if ($flags) { $prod =~ s/\@type\@/flags/g; } else { $prod =~ s/\@type\@/enum/g; }
  400. if ($flags) { $prod =~ s/\@Type\@/Flags/g; } else { $prod =~ s/\@Type\@/Enum/g; }
  401. if ($flags) { $prod =~ s/\@TYPE\@/FLAGS/g; } else { $prod =~ s/\@TYPE\@/ENUM/g; }
  402. $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
  403. $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
  404. chomp ($prod);
  405. print "$prod\n";
  406. }
  407. if (length($vprod)) {
  408. my $prod = $vprod;
  409. my $next_num = 0;
  410. $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
  411. $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
  412. for (@entries) {
  413. my ($name,$num,$nick) = @{$_};
  414. my $tmp_prod = $prod;
  415. if ($prod =~ /\@valuenum\@/) {
  416. # only attempt to eval the value if it is requested
  417. # this prevents us from throwing errors otherwise
  418. if (defined $num) {
  419. # use sandboxed perl evaluation as a reasonable
  420. # approximation to C constant folding
  421. $num = $sandbox->reval ($num);
  422. # make sure it parsed to an integer
  423. if (!defined $num or $num !~ /^-?\d+$/) {
  424. die "Unable to parse enum value '$num'";
  425. }
  426. } else {
  427. $num = $next_num;
  428. }
  429. $tmp_prod =~ s/\@valuenum\@/$num/g;
  430. $next_num = $num + 1;
  431. }
  432. $tmp_prod =~ s/\@VALUENAME\@/$name/g;
  433. $tmp_prod =~ s/\@valuenick\@/$nick/g;
  434. if ($flags) { $tmp_prod =~ s/\@type\@/flags/g; } else { $tmp_prod =~ s/\@type\@/enum/g; }
  435. if ($flags) { $tmp_prod =~ s/\@Type\@/Flags/g; } else { $tmp_prod =~ s/\@Type\@/Enum/g; }
  436. if ($flags) { $tmp_prod =~ s/\@TYPE\@/FLAGS/g; } else { $tmp_prod =~ s/\@TYPE\@/ENUM/g; }
  437. chomp ($tmp_prod);
  438. print "$tmp_prod\n";
  439. }
  440. }
  441. if (length($vtail)) {
  442. my $prod = $vtail;
  443. $prod =~ s/\@enum_name\@/$enumsym/g;
  444. $prod =~ s/\@EnumName\@/$enumname/g;
  445. $prod =~ s/\@ENUMSHORT\@/$enumshort/g;
  446. $prod =~ s/\@ENUMNAME\@/$enumlong/g;
  447. $prod =~ s/\@ENUMPREFIX\@/$enumname_prefix/g;
  448. if ($flags) { $prod =~ s/\@type\@/flags/g; } else { $prod =~ s/\@type\@/enum/g; }
  449. if ($flags) { $prod =~ s/\@Type\@/Flags/g; } else { $prod =~ s/\@Type\@/Enum/g; }
  450. if ($flags) { $prod =~ s/\@TYPE\@/FLAGS/g; } else { $prod =~ s/\@TYPE\@/ENUM/g; }
  451. $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
  452. $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
  453. chomp ($prod);
  454. print "$prod\n";
  455. }
  456. }
  457. }
  458. if (length($ftail)) {
  459. my $prod = $ftail;
  460. my $base = basename ($ARGV);
  461. $prod =~ s/\@filename\@/$ARGV/g;
  462. $prod =~ s/\@basename\@/$base/g;
  463. $prod =~ s/\\a/\a/g; $prod =~ s/\\b/\b/g; $prod =~ s/\\t/\t/g; $prod =~ s/\\n/\n/g;
  464. $prod =~ s/\\f/\f/g; $prod =~ s/\\r/\r/g;
  465. chomp ($prod);
  466. print "$prod\n";
  467. }
  468. # put auto-generation comment
  469. {
  470. my $comment = $comment_tmpl;
  471. $comment =~ s/\@comment\@/Generated data ends here/;
  472. print "\n" . $comment . "\n\n";
  473. }