extract-upper-case.pl 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #!/usr/bin/env perl
  2. use strict;
  3. use warnings;
  4. my @variables;
  5. my @commands;
  6. my @properties;
  7. my @modules;
  8. my %keywords; # command => keyword-list
  9. # unwanted upper-cases
  10. my %unwanted = map { $_ => 1 } qw(VS CXX IDE NOTFOUND NO_ DFOO DBAR);
  11. # cannot remove ALL - exists for add_custom_command
  12. # control-statements
  13. my %conditional = map { $_ => 1 } qw(if else elseif endif);
  14. my %loop = map { $_ => 1 } qw(foreach while endforeach endwhile);
  15. # decrecated
  16. my %deprecated = map { $_ => 1 } qw(build_name exec_program export_library_dependencies install_files install_programs install_targets link_libraries make_directory output_required_files remove subdir_depends subdirs use_mangled_mesa utility_source variable_requires write_file);
  17. # add some (popular) modules
  18. push @modules, "ExternalProject";
  19. # variables
  20. open(CMAKE, "cmake --help-variable-list|") or die "could not run cmake";
  21. while (<CMAKE>) {
  22. chomp;
  23. push @variables, $_;
  24. }
  25. close(CMAKE);
  26. # transform all variables in a hash - to be able to use exists later on
  27. my %variables = map { $_ => 1 } @variables;
  28. # commands
  29. open(CMAKE, "cmake --help-command-list|");
  30. while (my $cmd = <CMAKE>) {
  31. chomp $cmd;
  32. push @commands, $cmd;
  33. }
  34. close(CMAKE);
  35. # now generate a keyword-list per command
  36. foreach my $cmd (@commands) {
  37. my @word = extract_upper("cmake --help-command $cmd|");
  38. next if scalar @word == 0;
  39. $keywords{$cmd} = [ sort keys %{ { map { $_ => 1 } @word } } ];
  40. }
  41. # and now for modules
  42. foreach my $mod (@modules) {
  43. my @word = extract_upper("cmake --help-module $mod|");
  44. next if scalar @word == 0;
  45. $keywords{$mod} = [ sort keys %{ { map { $_ => 1 } @word } } ];
  46. }
  47. # and now for generator-expressions
  48. my @generator_expr = extract_upper("cmake --help-manual cmake-generator-expressions |");
  49. # properties
  50. open(CMAKE, "cmake --help-property-list|");
  51. while (<CMAKE>) {
  52. chomp;
  53. push @properties, $_;
  54. }
  55. close(CMAKE);
  56. # generate cmake.vim
  57. open(IN, "<cmake.vim.in") or die "could not read cmake.vim.in";
  58. open(OUT, ">syntax/cmake.vim") or die "could not write to syntax/cmake.vim";
  59. my @keyword_hi;
  60. while(<IN>)
  61. {
  62. if (m/\@([A-Z0-9_]+)\@/) { # match for @SOMETHING@
  63. if ($1 eq "COMMAND_LIST") {
  64. # do not include "special" commands in this list
  65. my @tmp = grep { ! exists $conditional{$_} and
  66. ! exists $loop{$_} and
  67. ! exists $deprecated{$_} } @commands;
  68. print OUT " " x 12 , "\\ ", join(" ", @tmp), "\n";
  69. } elsif ($1 eq "VARIABLE_LIST") {
  70. print OUT " " x 12 , "\\ ", join(" ", @variables), "\n";
  71. } elsif ($1 eq "MODULES") {
  72. print OUT " " x 12 , "\\ ", join("\n", @modules), "\n";
  73. } elsif ($1 eq "GENERATOR_EXPRESSIONS") {
  74. print OUT " " x 12 , "\\ ", join(" ", @generator_expr), "\n";
  75. } elsif ($1 eq "CONDITIONALS") {
  76. print OUT " " x 12 , "\\ ", join(" ", sort keys %conditional), "\n";
  77. } elsif ($1 eq "LOOPS") {
  78. print OUT " " x 12 , "\\ ", join(" ", sort keys %loop), "\n";
  79. } elsif ($1 eq "DEPRECATED") {
  80. print OUT " " x 12 , "\\ ", join(" ", sort keys %deprecated), "\n";
  81. } elsif ($1 eq "KEYWORDS") {
  82. foreach my $k (sort keys %keywords) {
  83. print OUT "syn keyword cmakeKW$k\n";
  84. print OUT " " x 12, "\\ ", join(" ", @{$keywords{$k}}), "\n";
  85. print OUT " " x 12, "\\ contained\n";
  86. print OUT "\n";
  87. push @keyword_hi, "hi def link cmakeKW$k ModeMsg";
  88. }
  89. } elsif ($1 eq "KEYWORDS_HIGHLIGHT") {
  90. print OUT join("\n", @keyword_hi), "\n";
  91. } else {
  92. print "ERROR do not know how to replace $1\n";
  93. }
  94. } else {
  95. print OUT $_;
  96. }
  97. }
  98. close(IN);
  99. close(OUT);
  100. sub extract_upper
  101. {
  102. my $input = shift;
  103. my @word;
  104. open(KW, $input);
  105. while (<KW>) {
  106. foreach my $w (m/\b([A-Z_]{2,})\b/g) {
  107. next
  108. if exists $variables{$w} or # skip if it is a variable
  109. exists $unwanted{$w}; # skip if not wanted
  110. push @word, $w;
  111. }
  112. }
  113. close(KW);
  114. return @word;
  115. }