src/Pure/General/symbol.scala
author wenzelm
Thu, 21 Aug 2008 15:20:00 +0200
changeset 27937 fdf77e7be01a
parent 27935 68d40072e9e7
child 27938 3d5b12f23f15
permissions -rw-r--r--
more robust pattern: look at longer matches first, added catch-all case; more private fields; reworked Recoder: more direct char/string operations, avoids inefficiency of large alternatives (java.util.regex does not optimize regexps);
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
27901
28083e9f8d1d Basic support for Isabelle symbols.
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/General/symbol.scala
28083e9f8d1d Basic support for Isabelle symbols.
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
28083e9f8d1d Basic support for Isabelle symbols.
wenzelm
parents:
diff changeset
     3
    Author:     Makarius
28083e9f8d1d Basic support for Isabelle symbols.
wenzelm
parents:
diff changeset
     4
27924
8dd8b564faf5 tuned comments;
wenzelm
parents: 27923
diff changeset
     5
Detecting and recoding Isabelle symbols.
27901
28083e9f8d1d Basic support for Isabelle symbols.
wenzelm
parents:
diff changeset
     6
*/
28083e9f8d1d Basic support for Isabelle symbols.
wenzelm
parents:
diff changeset
     7
28083e9f8d1d Basic support for Isabelle symbols.
wenzelm
parents:
diff changeset
     8
package isabelle
28083e9f8d1d Basic support for Isabelle symbols.
wenzelm
parents:
diff changeset
     9
27937
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    10
import java.util.regex.Pattern
27918
85942d2036a0 reading symbol interpretation tables;
wenzelm
parents: 27905
diff changeset
    11
import java.io.File
85942d2036a0 reading symbol interpretation tables;
wenzelm
parents: 27905
diff changeset
    12
import scala.io.Source
27923
7ebe9d38743a use scala.collection.jcl.HashMap, which seems to be more efficient;
wenzelm
parents: 27918
diff changeset
    13
import scala.collection.jcl.HashMap
27901
28083e9f8d1d Basic support for Isabelle symbols.
wenzelm
parents:
diff changeset
    14
28083e9f8d1d Basic support for Isabelle symbols.
wenzelm
parents:
diff changeset
    15
28083e9f8d1d Basic support for Isabelle symbols.
wenzelm
parents:
diff changeset
    16
object Symbol {
28083e9f8d1d Basic support for Isabelle symbols.
wenzelm
parents:
diff changeset
    17
27924
8dd8b564faf5 tuned comments;
wenzelm
parents: 27923
diff changeset
    18
  /** Symbol regexps **/
27901
28083e9f8d1d Basic support for Isabelle symbols.
wenzelm
parents:
diff changeset
    19
28083e9f8d1d Basic support for Isabelle symbols.
wenzelm
parents:
diff changeset
    20
  private def compile(s: String) =
28083e9f8d1d Basic support for Isabelle symbols.
wenzelm
parents:
diff changeset
    21
    Pattern.compile(s, Pattern.COMMENTS | Pattern.DOTALL)
28083e9f8d1d Basic support for Isabelle symbols.
wenzelm
parents:
diff changeset
    22
27937
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    23
  private val plain_pattern = compile(""" [^\\ \ud800-\udfff] | [\ud800-\udbff][\udc00-\udfff] """)
27924
8dd8b564faf5 tuned comments;
wenzelm
parents: 27923
diff changeset
    24
27937
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    25
  private val symbol_pattern = compile(""" \\ \\? < (?:
27924
8dd8b564faf5 tuned comments;
wenzelm
parents: 27923
diff changeset
    26
      \^? [A-Za-z][A-Za-z0-9_']* |
8dd8b564faf5 tuned comments;
wenzelm
parents: 27923
diff changeset
    27
      \^raw: [\x20-\x7e\u0100-\uffff && [^.>]]* ) >""")
8dd8b564faf5 tuned comments;
wenzelm
parents: 27923
diff changeset
    28
27937
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    29
  private val bad_symbol_pattern = compile("(?!" + symbol_pattern + ")" +
27924
8dd8b564faf5 tuned comments;
wenzelm
parents: 27923
diff changeset
    30
    """ \\ \\? < (?: (?! \s | [\"`\\] | \(\* | \*\) | \{\* | \*\} ) . )*""")
8dd8b564faf5 tuned comments;
wenzelm
parents: 27923
diff changeset
    31
27937
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    32
  val pattern = compile(plain_pattern + "|" + symbol_pattern + "|" + bad_symbol_pattern + "| [.]")
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    33
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    34
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    35
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    36
  /** Recoding **/
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    37
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    38
  private class Recoder(list: List[(String, String)]) {
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    39
    private val (min, max) = {
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    40
      var min = '\uffff'
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    41
      var max = '\u0000'
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    42
      for ((x, _) <- list) {
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    43
        val c = x(0)
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    44
        if (c < min) min = c
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    45
        if (c > max) max = c
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    46
      }
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    47
      (min, max)
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    48
    }
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    49
    private val table = {
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    50
      val table = new HashMap[String, String]
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    51
      for ((x, y) <- list) table + (x -> y)
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    52
      table
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    53
    }
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    54
    def recode(text: String) = {
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    55
      val len = text.length
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    56
      val matcher = pattern.matcher(text)
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    57
      val result = new StringBuilder(len)
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    58
      var i = 0
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    59
      while (i < len) {
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    60
        val c = text(i)
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    61
        if (min <= c && c <= max) {
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    62
          matcher.region(i, len).lookingAt
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    63
          table.get(matcher.group) match {
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    64
            case Some(y) => result.append(y)
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    65
            case None => result.append(c)
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    66
          }
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    67
          i = matcher.end
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    68
        }
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    69
        else { result.append(c); i += 1 }
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    70
      }
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    71
      result.toString
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    72
    }
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    73
  }
27924
8dd8b564faf5 tuned comments;
wenzelm
parents: 27923
diff changeset
    74
27918
85942d2036a0 reading symbol interpretation tables;
wenzelm
parents: 27905
diff changeset
    75
27923
7ebe9d38743a use scala.collection.jcl.HashMap, which seems to be more efficient;
wenzelm
parents: 27918
diff changeset
    76
27927
eb624bb54bc6 tuned Recoder;
wenzelm
parents: 27926
diff changeset
    77
  /** Symbol interpretation **/
eb624bb54bc6 tuned Recoder;
wenzelm
parents: 27926
diff changeset
    78
27923
7ebe9d38743a use scala.collection.jcl.HashMap, which seems to be more efficient;
wenzelm
parents: 27918
diff changeset
    79
  class Interpretation {
7ebe9d38743a use scala.collection.jcl.HashMap, which seems to be more efficient;
wenzelm
parents: 27918
diff changeset
    80
7ebe9d38743a use scala.collection.jcl.HashMap, which seems to be more efficient;
wenzelm
parents: 27918
diff changeset
    81
    class BadSymbol(val msg: String) extends Exception
7ebe9d38743a use scala.collection.jcl.HashMap, which seems to be more efficient;
wenzelm
parents: 27918
diff changeset
    82
27924
8dd8b564faf5 tuned comments;
wenzelm
parents: 27923
diff changeset
    83
    private var symbols = new HashMap[String, HashMap[String, String]]
27926
308be7332e25 more private fields;
wenzelm
parents: 27924
diff changeset
    84
    private var decoder: Recoder = null
308be7332e25 more private fields;
wenzelm
parents: 27924
diff changeset
    85
    private var encoder: Recoder = null
27918
85942d2036a0 reading symbol interpretation tables;
wenzelm
parents: 27905
diff changeset
    86
27924
8dd8b564faf5 tuned comments;
wenzelm
parents: 27923
diff changeset
    87
    def decode(text: String) = decoder.recode(text)
8dd8b564faf5 tuned comments;
wenzelm
parents: 27923
diff changeset
    88
    def encode(text: String) = encoder.recode(text)
27923
7ebe9d38743a use scala.collection.jcl.HashMap, which seems to be more efficient;
wenzelm
parents: 27918
diff changeset
    89
27918
85942d2036a0 reading symbol interpretation tables;
wenzelm
parents: 27905
diff changeset
    90
27923
7ebe9d38743a use scala.collection.jcl.HashMap, which seems to be more efficient;
wenzelm
parents: 27918
diff changeset
    91
    /* read symbols */
7ebe9d38743a use scala.collection.jcl.HashMap, which seems to be more efficient;
wenzelm
parents: 27918
diff changeset
    92
7ebe9d38743a use scala.collection.jcl.HashMap, which seems to be more efficient;
wenzelm
parents: 27918
diff changeset
    93
    private val empty_pattern = compile(""" ^\s* (?: \#.* )? $ """)
7ebe9d38743a use scala.collection.jcl.HashMap, which seems to be more efficient;
wenzelm
parents: 27918
diff changeset
    94
    private val blank_pattern = compile(""" \s+ """)
7ebe9d38743a use scala.collection.jcl.HashMap, which seems to be more efficient;
wenzelm
parents: 27918
diff changeset
    95
    private val key_pattern = compile(""" (.+): """)
27918
85942d2036a0 reading symbol interpretation tables;
wenzelm
parents: 27905
diff changeset
    96
27923
7ebe9d38743a use scala.collection.jcl.HashMap, which seems to be more efficient;
wenzelm
parents: 27918
diff changeset
    97
    private def read_line(line: String) = {
7ebe9d38743a use scala.collection.jcl.HashMap, which seems to be more efficient;
wenzelm
parents: 27918
diff changeset
    98
      def err() = throw new BadSymbol(line)
27918
85942d2036a0 reading symbol interpretation tables;
wenzelm
parents: 27905
diff changeset
    99
27923
7ebe9d38743a use scala.collection.jcl.HashMap, which seems to be more efficient;
wenzelm
parents: 27918
diff changeset
   100
      def read_props(props: List[String], tab: HashMap[String, String]): Unit = {
27918
85942d2036a0 reading symbol interpretation tables;
wenzelm
parents: 27905
diff changeset
   101
        props match {
27923
7ebe9d38743a use scala.collection.jcl.HashMap, which seems to be more efficient;
wenzelm
parents: 27918
diff changeset
   102
          case Nil => ()
27918
85942d2036a0 reading symbol interpretation tables;
wenzelm
parents: 27905
diff changeset
   103
          case _ :: Nil => err()
85942d2036a0 reading symbol interpretation tables;
wenzelm
parents: 27905
diff changeset
   104
          case key :: value :: rest => {
85942d2036a0 reading symbol interpretation tables;
wenzelm
parents: 27905
diff changeset
   105
            val key_matcher = key_pattern.matcher(key)
27923
7ebe9d38743a use scala.collection.jcl.HashMap, which seems to be more efficient;
wenzelm
parents: 27918
diff changeset
   106
            if (key_matcher.matches) {
7ebe9d38743a use scala.collection.jcl.HashMap, which seems to be more efficient;
wenzelm
parents: 27918
diff changeset
   107
              tab + (key_matcher.group(1) -> value)
7ebe9d38743a use scala.collection.jcl.HashMap, which seems to be more efficient;
wenzelm
parents: 27918
diff changeset
   108
              read_props(rest, tab)
7ebe9d38743a use scala.collection.jcl.HashMap, which seems to be more efficient;
wenzelm
parents: 27918
diff changeset
   109
            }
27918
85942d2036a0 reading symbol interpretation tables;
wenzelm
parents: 27905
diff changeset
   110
            else err ()
85942d2036a0 reading symbol interpretation tables;
wenzelm
parents: 27905
diff changeset
   111
          }
85942d2036a0 reading symbol interpretation tables;
wenzelm
parents: 27905
diff changeset
   112
        }
85942d2036a0 reading symbol interpretation tables;
wenzelm
parents: 27905
diff changeset
   113
      }
85942d2036a0 reading symbol interpretation tables;
wenzelm
parents: 27905
diff changeset
   114
85942d2036a0 reading symbol interpretation tables;
wenzelm
parents: 27905
diff changeset
   115
      if (!empty_pattern.matcher(line).matches) {
85942d2036a0 reading symbol interpretation tables;
wenzelm
parents: 27905
diff changeset
   116
        blank_pattern.split(line).toList match {
85942d2036a0 reading symbol interpretation tables;
wenzelm
parents: 27905
diff changeset
   117
          case Nil => err()
27923
7ebe9d38743a use scala.collection.jcl.HashMap, which seems to be more efficient;
wenzelm
parents: 27918
diff changeset
   118
          case symbol :: props => {
7ebe9d38743a use scala.collection.jcl.HashMap, which seems to be more efficient;
wenzelm
parents: 27918
diff changeset
   119
            val tab = new HashMap[String, String]
7ebe9d38743a use scala.collection.jcl.HashMap, which seems to be more efficient;
wenzelm
parents: 27918
diff changeset
   120
            read_props(props, tab)
7ebe9d38743a use scala.collection.jcl.HashMap, which seems to be more efficient;
wenzelm
parents: 27918
diff changeset
   121
            symbols + (symbol -> tab)
7ebe9d38743a use scala.collection.jcl.HashMap, which seems to be more efficient;
wenzelm
parents: 27918
diff changeset
   122
          }
27918
85942d2036a0 reading symbol interpretation tables;
wenzelm
parents: 27905
diff changeset
   123
        }
85942d2036a0 reading symbol interpretation tables;
wenzelm
parents: 27905
diff changeset
   124
      }
85942d2036a0 reading symbol interpretation tables;
wenzelm
parents: 27905
diff changeset
   125
    }
85942d2036a0 reading symbol interpretation tables;
wenzelm
parents: 27905
diff changeset
   126
27935
68d40072e9e7 read_symbols: proper IsabelleSystem.platform_path;
wenzelm
parents: 27928
diff changeset
   127
    private def read_symbols(path: String) = {
68d40072e9e7 read_symbols: proper IsabelleSystem.platform_path;
wenzelm
parents: 27928
diff changeset
   128
      val file = new File(IsabelleSystem.platform_path(path))
27918
85942d2036a0 reading symbol interpretation tables;
wenzelm
parents: 27905
diff changeset
   129
      if (file.canRead) {
85942d2036a0 reading symbol interpretation tables;
wenzelm
parents: 27905
diff changeset
   130
        for (line <- Source.fromFile(file).getLines) read_line(line)
85942d2036a0 reading symbol interpretation tables;
wenzelm
parents: 27905
diff changeset
   131
      }
85942d2036a0 reading symbol interpretation tables;
wenzelm
parents: 27905
diff changeset
   132
    }
27923
7ebe9d38743a use scala.collection.jcl.HashMap, which seems to be more efficient;
wenzelm
parents: 27918
diff changeset
   133
7ebe9d38743a use scala.collection.jcl.HashMap, which seems to be more efficient;
wenzelm
parents: 27918
diff changeset
   134
7ebe9d38743a use scala.collection.jcl.HashMap, which seems to be more efficient;
wenzelm
parents: 27918
diff changeset
   135
    /* init tables */
7ebe9d38743a use scala.collection.jcl.HashMap, which seems to be more efficient;
wenzelm
parents: 27918
diff changeset
   136
27924
8dd8b564faf5 tuned comments;
wenzelm
parents: 27923
diff changeset
   137
    private def get_code(entry: (String, HashMap[String, String])) = {
8dd8b564faf5 tuned comments;
wenzelm
parents: 27923
diff changeset
   138
      val (symbol, props) = entry
8dd8b564faf5 tuned comments;
wenzelm
parents: 27923
diff changeset
   139
      val code =
8dd8b564faf5 tuned comments;
wenzelm
parents: 27923
diff changeset
   140
        try { Integer.decode(props("code")).intValue }
8dd8b564faf5 tuned comments;
wenzelm
parents: 27923
diff changeset
   141
        catch {
8dd8b564faf5 tuned comments;
wenzelm
parents: 27923
diff changeset
   142
          case e: NoSuchElementException => throw new BadSymbol(symbol)
8dd8b564faf5 tuned comments;
wenzelm
parents: 27923
diff changeset
   143
          case e: NumberFormatException => throw new BadSymbol(symbol)
27923
7ebe9d38743a use scala.collection.jcl.HashMap, which seems to be more efficient;
wenzelm
parents: 27918
diff changeset
   144
        }
27924
8dd8b564faf5 tuned comments;
wenzelm
parents: 27923
diff changeset
   145
      (symbol, new String(Character.toChars(code)))
8dd8b564faf5 tuned comments;
wenzelm
parents: 27923
diff changeset
   146
    }
27923
7ebe9d38743a use scala.collection.jcl.HashMap, which seems to be more efficient;
wenzelm
parents: 27918
diff changeset
   147
27924
8dd8b564faf5 tuned comments;
wenzelm
parents: 27923
diff changeset
   148
    private def init_recoders() = {
8dd8b564faf5 tuned comments;
wenzelm
parents: 27923
diff changeset
   149
      val list = symbols.elements.toList.map(get_code)
27928
b1d0d1351ed9 decode escaped symbols as well;
wenzelm
parents: 27927
diff changeset
   150
      decoder = new Recoder(list ::: (for ((x, y) <- list) yield ("\\" + x, y)))
b1d0d1351ed9 decode escaped symbols as well;
wenzelm
parents: 27927
diff changeset
   151
      encoder = new Recoder(for ((x, y) <- list) yield (y, x))
27923
7ebe9d38743a use scala.collection.jcl.HashMap, which seems to be more efficient;
wenzelm
parents: 27918
diff changeset
   152
    }
7ebe9d38743a use scala.collection.jcl.HashMap, which seems to be more efficient;
wenzelm
parents: 27918
diff changeset
   153
7ebe9d38743a use scala.collection.jcl.HashMap, which seems to be more efficient;
wenzelm
parents: 27918
diff changeset
   154
7ebe9d38743a use scala.collection.jcl.HashMap, which seems to be more efficient;
wenzelm
parents: 27918
diff changeset
   155
    /* constructor */
7ebe9d38743a use scala.collection.jcl.HashMap, which seems to be more efficient;
wenzelm
parents: 27918
diff changeset
   156
27935
68d40072e9e7 read_symbols: proper IsabelleSystem.platform_path;
wenzelm
parents: 27928
diff changeset
   157
    read_symbols("$ISABELLE_HOME/etc/symbols")
68d40072e9e7 read_symbols: proper IsabelleSystem.platform_path;
wenzelm
parents: 27928
diff changeset
   158
    read_symbols("$ISABELLE_HOME_USER/etc/symbols")
27924
8dd8b564faf5 tuned comments;
wenzelm
parents: 27923
diff changeset
   159
    init_recoders()
27918
85942d2036a0 reading symbol interpretation tables;
wenzelm
parents: 27905
diff changeset
   160
  }
85942d2036a0 reading symbol interpretation tables;
wenzelm
parents: 27905
diff changeset
   161
27901
28083e9f8d1d Basic support for Isabelle symbols.
wenzelm
parents:
diff changeset
   162
}