lib/scripts/keywords.pl
author wenzelm
Sat Jun 28 21:21:21 2008 +0200 (2008-06-28)
changeset 27386 d10ec4969b9f
parent 24912 52bc004950c4
child 29145 b1c6f4563df7
permissions -rw-r--r--
allow overlap of minor keywords and commands;
wenzelm@24875
     1
#
wenzelm@24875
     2
# $Id$
wenzelm@24875
     3
# Author: Makarius
wenzelm@24875
     4
#
wenzelm@24875
     5
# keywords.pl - generate outer syntax keyword files from session logs
wenzelm@24875
     6
#
wenzelm@24875
     7
wenzelm@24886
     8
## arguments
wenzelm@24875
     9
wenzelm@24886
    10
my ($keywords_name, $target_tool, $sessions) = @ARGV;
wenzelm@24875
    11
wenzelm@24875
    12
wenzelm@24875
    13
## keywords
wenzelm@24875
    14
wenzelm@24875
    15
my %keywords;
wenzelm@24875
    16
wenzelm@24875
    17
sub set_keyword {
wenzelm@24875
    18
  my ($name, $kind) = @_;
wenzelm@27386
    19
  if (defined $keywords{$name} and $keywords{$name} ne $kind and $keywords{$name} ne "minor") {
wenzelm@27386
    20
    if ($kind ne "minor") {
wenzelm@27386
    21
      print STDERR "### Inconsistent declaration of keyword \"${name}\": $keywords{$name} vs ${kind}\n";
wenzelm@27386
    22
      $keywords{$name} = $kind;
wenzelm@27386
    23
    }
wenzelm@27386
    24
  } else {
wenzelm@27386
    25
    $keywords{$name} = $kind;
wenzelm@24875
    26
  }
wenzelm@24875
    27
}
wenzelm@24875
    28
wenzelm@24875
    29
sub collect_keywords {
wenzelm@24875
    30
  while(<STDIN>) {
wenzelm@24875
    31
    if (m/^Outer syntax keyword:\s*"(.*)"/) {
wenzelm@24875
    32
      my $name = $1;
wenzelm@24875
    33
      &set_keyword($name, "minor");
wenzelm@24875
    34
    }
wenzelm@24875
    35
    elsif (m/^Outer syntax command:\s*"(.*)"\s*\((.*)\)/) {
wenzelm@24875
    36
      my $name = $1;
wenzelm@24875
    37
      my $kind = $2;
wenzelm@24875
    38
      &set_keyword($name, $kind);
wenzelm@24875
    39
    }
wenzelm@24875
    40
  }
wenzelm@24875
    41
}
wenzelm@24875
    42
wenzelm@24875
    43
wenzelm@24875
    44
## Emacs output
wenzelm@24875
    45
wenzelm@24875
    46
sub emacs_output {
wenzelm@24886
    47
  my @kinds = (
wenzelm@24886
    48
    "major",
wenzelm@24886
    49
    "minor",
wenzelm@24886
    50
    "control",
wenzelm@24886
    51
    "diag",
wenzelm@24886
    52
    "theory-begin",
wenzelm@24886
    53
    "theory-switch",
wenzelm@24886
    54
    "theory-end",
wenzelm@24886
    55
    "theory-heading",
wenzelm@24886
    56
    "theory-decl",
wenzelm@24886
    57
    "theory-script",
wenzelm@24886
    58
    "theory-goal",
wenzelm@24886
    59
    "qed",
wenzelm@24886
    60
    "qed-block",
wenzelm@24886
    61
    "qed-global",
wenzelm@24886
    62
    "proof-heading",
wenzelm@24886
    63
    "proof-goal",
wenzelm@24886
    64
    "proof-block",
wenzelm@24886
    65
    "proof-open",
wenzelm@24886
    66
    "proof-close",
wenzelm@24886
    67
    "proof-chain",
wenzelm@24886
    68
    "proof-decl",
wenzelm@24886
    69
    "proof-asm",
wenzelm@24886
    70
    "proof-asm-goal",
wenzelm@24886
    71
    "proof-script"
wenzelm@24886
    72
  );
wenzelm@24875
    73
  my $file = $keywords_name eq "" ? "isar-keywords.el" : "isar-keywords-${keywords_name}.el";
wenzelm@24875
    74
  open (OUTPUT, "> ${file}") || die "$!";
wenzelm@24875
    75
  select OUTPUT;
wenzelm@24875
    76
wenzelm@24875
    77
  print ";;\n";
wenzelm@24875
    78
  print ";; Keyword classification tables for Isabelle/Isar.\n";
wenzelm@24912
    79
  print ";; Generated from ${sessions}.\n";
wenzelm@24905
    80
  print ";; *** DO NOT EDIT *** DO NOT EDIT *** DO NOT EDIT ***\n";
wenzelm@24875
    81
  print ";;\n";
wenzelm@24875
    82
  print ";; \$", "Id\$\n";
wenzelm@24875
    83
  print ";;\n";
wenzelm@24875
    84
wenzelm@24875
    85
  for my $kind (@kinds) {
wenzelm@24875
    86
    my @names;
wenzelm@24875
    87
    for my $name (keys(%keywords)) {
wenzelm@24875
    88
      if ($kind eq "major" ? $keywords{$name} ne "minor" : $keywords{$name} eq $kind) {
wenzelm@24875
    89
        if ($kind ne "minor" or $name =~ m/^[A-Za-z0-9_]+$/) {
wenzelm@24875
    90
          push @names, $name;
wenzelm@24875
    91
        }
wenzelm@24875
    92
      }
wenzelm@24875
    93
    }
wenzelm@24875
    94
    @names = sort(@names);
wenzelm@24875
    95
wenzelm@24875
    96
    print "\n(defconst isar-keywords-${kind}";
wenzelm@24875
    97
    print "\n  '(";
wenzelm@24875
    98
    my $first = 1;
wenzelm@24875
    99
    for my $name (@names) {
wenzelm@24875
   100
      $name =~ s/([\.\*\+\?\[\]\^\$])/\\\\$1/g;
wenzelm@24875
   101
      if ($first) {
wenzelm@24875
   102
        print "\"${name}\"";
wenzelm@24875
   103
        $first = 0;
wenzelm@24875
   104
      }
wenzelm@24875
   105
      else {
wenzelm@24875
   106
        print "\n    \"${name}\"";
wenzelm@24875
   107
      }
wenzelm@24875
   108
    }
wenzelm@24875
   109
    print "))\n";
wenzelm@24875
   110
  }
wenzelm@24875
   111
  print "\n(provide 'isar-keywords)\n";
wenzelm@24875
   112
wenzelm@24875
   113
  close OUTPUT;
wenzelm@24875
   114
  select;
wenzelm@24886
   115
  print STDERR "${target_tool}: ${file}\n";
wenzelm@24886
   116
}
wenzelm@24886
   117
wenzelm@24886
   118
wenzelm@24886
   119
## jEdit output
wenzelm@24886
   120
wenzelm@24886
   121
sub jedit_output {
wenzelm@24886
   122
  my %keyword_types = (
wenzelm@24886
   123
    "minor"           => "KEYWORD4",
wenzelm@24886
   124
    "control"         => "INVALID",
wenzelm@24886
   125
    "diag"            => "LABEL",
wenzelm@24886
   126
    "theory-begin"    => "KEYWORD3",
wenzelm@24886
   127
    "theory-switch"   => "KEYWORD3",
wenzelm@24886
   128
    "theory-end"      => "KEYWORD3",
wenzelm@24886
   129
    "theory-heading"  => "OPERATOR",
wenzelm@24886
   130
    "theory-decl"     => "OPERATOR",
wenzelm@24886
   131
    "theory-script"   => "KEYWORD1",
wenzelm@24886
   132
    "theory-goal"     => "OPERATOR",
wenzelm@24886
   133
    "qed"             => "OPERATOR",
wenzelm@24886
   134
    "qed-block"       => "OPERATOR",
wenzelm@24886
   135
    "qed-global"      => "OPERATOR",
wenzelm@24886
   136
    "proof-heading"   => "OPERATOR",
wenzelm@24886
   137
    "proof-goal"      => "OPERATOR",
wenzelm@24886
   138
    "proof-block"     => "OPERATOR",
wenzelm@24886
   139
    "proof-open"      => "OPERATOR",
wenzelm@24886
   140
    "proof-close"     => "OPERATOR",
wenzelm@24886
   141
    "proof-chain"     => "OPERATOR",
wenzelm@24886
   142
    "proof-decl"      => "OPERATOR",
wenzelm@24886
   143
    "proof-asm"       => "KEYWORD2",
wenzelm@24886
   144
    "proof-asm-goal"  => "KEYWORD2",
wenzelm@24886
   145
    "proof-script"    => "KEYWORD1"
wenzelm@24886
   146
  );
wenzelm@24886
   147
  my $file = "isabelle.xml";
wenzelm@24886
   148
  open (OUTPUT, "> ${file}") || die "$!";
wenzelm@24886
   149
  select OUTPUT;
wenzelm@24886
   150
wenzelm@24886
   151
  print <<'EOF';
wenzelm@24886
   152
<?xml version="1.0"?>
wenzelm@24886
   153
<!DOCTYPE MODE SYSTEM "xmode.dtd">
wenzelm@24890
   154
EOF
wenzelm@24912
   155
  print "<!-- Generated from ${sessions}. -->\n";
wenzelm@24905
   156
  print "<!-- *** DO NOT EDIT *** DO NOT EDIT *** DO NOT EDIT *** -->\n";
wenzelm@24912
   157
  print "<!-- \$", "Id\$ -->\n";
wenzelm@24890
   158
  print <<'EOF';
wenzelm@24886
   159
<MODE>
wenzelm@24886
   160
  <PROPS>
wenzelm@24886
   161
    <PROPERTY NAME="commentStart" VALUE="(*"/>
wenzelm@24886
   162
    <PROPERTY NAME="commentEnd" VALUE="*)"/>
wenzelm@24886
   163
    <PROPERTY NAME="noWordSep" VALUE="_'.?"/>
wenzelm@24886
   164
    <PROPERTY NAME="indentOpenBrackets" VALUE="{"/>
wenzelm@24886
   165
    <PROPERTY NAME="indentCloseBrackets" VALUE="}"/>
wenzelm@24886
   166
    <PROPERTY NAME="unalignedOpenBrackets" VALUE="(" />
wenzelm@24886
   167
    <PROPERTY NAME="unalignedCloseBrackets" VALUE=")" />
wenzelm@24886
   168
    <PROPERTY NAME="tabSize" VALUE="2" />
wenzelm@24886
   169
    <PROPERTY NAME="indentSize" VALUE="2" />
wenzelm@24886
   170
  </PROPS>
wenzelm@24886
   171
  <RULES IGNORE_CASE="FALSE" HIGHLIGHT_DIGITS="FALSE" ESCAPE="\">
wenzelm@24886
   172
    <SPAN TYPE="COMMENT1" NO_ESCAPE="TRUE">
wenzelm@24886
   173
      <BEGIN>(*</BEGIN>
wenzelm@24886
   174
      <END>*)</END>
wenzelm@24886
   175
    </SPAN>
wenzelm@24886
   176
    <SPAN TYPE="COMMENT3" NO_ESCAPE="TRUE">
wenzelm@24886
   177
      <BEGIN>{*</BEGIN>
wenzelm@24886
   178
      <END>*}</END>
wenzelm@24886
   179
    </SPAN>
wenzelm@24886
   180
    <SPAN TYPE="LITERAL1">
wenzelm@24886
   181
      <BEGIN>`</BEGIN>
wenzelm@24886
   182
      <END>`</END>
wenzelm@24886
   183
    </SPAN>
wenzelm@24886
   184
    <SPAN TYPE="LITERAL3">
wenzelm@24886
   185
      <BEGIN>"</BEGIN>
wenzelm@24886
   186
      <END>"</END>
wenzelm@24886
   187
    </SPAN>
wenzelm@24886
   188
    <KEYWORDS>
wenzelm@24886
   189
EOF
wenzelm@24886
   190
wenzelm@24886
   191
  for my $name (sort(keys(%keywords))) {
wenzelm@24886
   192
    my $kind = $keywords{$name};
wenzelm@24886
   193
    my $type = $keyword_types{$kind};
wenzelm@24886
   194
    if ($kind ne "minor" or $name =~ m/^[A-Za-z0-9_]+$/) {
wenzelm@24886
   195
      $name =~ s/&/&amp;/g;
wenzelm@24886
   196
      $name =~ s/</&lt;/g;
wenzelm@24886
   197
      $name =~ s/>/&lt;/g;
wenzelm@24886
   198
      print "      <${type}>${name}</${type}>\n";
wenzelm@24886
   199
    }
wenzelm@24886
   200
  }
wenzelm@24886
   201
wenzelm@24886
   202
  print <<'EOF';
wenzelm@24886
   203
    </KEYWORDS>
wenzelm@24886
   204
  </RULES>
wenzelm@24886
   205
</MODE>
wenzelm@24886
   206
EOF
wenzelm@24886
   207
wenzelm@24886
   208
  close OUTPUT;
wenzelm@24886
   209
  select;
wenzelm@24886
   210
  print STDERR "${target_tool}: ${file}\n";
wenzelm@24875
   211
}
wenzelm@24875
   212
wenzelm@24875
   213
wenzelm@24875
   214
## main
wenzelm@24875
   215
wenzelm@24875
   216
&collect_keywords();
wenzelm@24886
   217
eval "${target_tool}_output()";
wenzelm@24875
   218