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