src/Pure/General/symbol.scala
author wenzelm
Tue, 09 Jun 2009 20:29:23 +0200
changeset 31522 0466cb17064f
parent 29569 f3f529b5d8fb
child 31523 2c0b67a0e5e7
permissions -rw-r--r--
more native Scala style; added is_open; misc tuning;
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
    Author:     Makarius
28083e9f8d1d Basic support for Isabelle symbols.
wenzelm
parents:
diff changeset
     3
27924
8dd8b564faf5 tuned comments;
wenzelm
parents: 27923
diff changeset
     4
Detecting and recoding Isabelle symbols.
27901
28083e9f8d1d Basic support for Isabelle symbols.
wenzelm
parents:
diff changeset
     5
*/
28083e9f8d1d Basic support for Isabelle symbols.
wenzelm
parents:
diff changeset
     6
28083e9f8d1d Basic support for Isabelle symbols.
wenzelm
parents:
diff changeset
     7
package isabelle
28083e9f8d1d Basic support for Isabelle symbols.
wenzelm
parents:
diff changeset
     8
27918
85942d2036a0 reading symbol interpretation tables;
wenzelm
parents: 27905
diff changeset
     9
import java.io.File
31522
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
    10
27918
85942d2036a0 reading symbol interpretation tables;
wenzelm
parents: 27905
diff changeset
    11
import scala.io.Source
31522
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
    12
import scala.collection.jcl
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
    13
import scala.util.matching.Regex
27901
28083e9f8d1d Basic support for Isabelle symbols.
wenzelm
parents:
diff changeset
    14
28083e9f8d1d Basic support for Isabelle symbols.
wenzelm
parents:
diff changeset
    15
31522
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
    16
object Symbol
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
    17
{
27901
28083e9f8d1d Basic support for Isabelle symbols.
wenzelm
parents:
diff changeset
    18
27924
8dd8b564faf5 tuned comments;
wenzelm
parents: 27923
diff changeset
    19
  /** Symbol regexps **/
27901
28083e9f8d1d Basic support for Isabelle symbols.
wenzelm
parents:
diff changeset
    20
31522
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
    21
  private val plain = new Regex("""(?xs)
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
    22
    [^\\ \ud800-\udfff] | [\ud800-\udbff][\udc00-\udfff] """)
27901
28083e9f8d1d Basic support for Isabelle symbols.
wenzelm
parents:
diff changeset
    23
31522
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
    24
  private val symbol = new Regex("""(?xs)
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
    25
      \\ \\? < (?:
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
31522
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
    29
  private val bad_symbol = new Regex("(?xs) (?!" + symbol + ")" +
27924
8dd8b564faf5 tuned comments;
wenzelm
parents: 27923
diff changeset
    30
    """ \\ \\? < (?: (?! \s | [\"`\\] | \(\* | \*\) | \{\* | \*\} ) . )*""")
8dd8b564faf5 tuned comments;
wenzelm
parents: 27923
diff changeset
    31
27939
41b1c0b769bf pattern: proper "." not "[.]"!
wenzelm
parents: 27938
diff changeset
    32
  // total pattern
31522
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
    33
  val regex = new Regex(plain + "|" + symbol + "|" + bad_symbol + "| .")
27937
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    34
31522
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
    35
  // prefix of another symbol
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
    36
  def is_open(s: String): Boolean =
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
    37
  {
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
    38
    val len = s.length
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
    39
    len == 1 && Character.isHighSurrogate(s(0)) ||
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
    40
    s == "\\" ||
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
    41
    s == "\\<" ||
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
    42
    len > 2 && s(len - 1) != '>'
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
    43
  }
27937
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    44
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    45
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    46
  /** Recoding **/
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    47
31522
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
    48
  private class Recoder(list: List[(String, String)])
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
    49
  {
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
    50
    private val (min, max) =
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
    51
    {
27937
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    52
      var min = '\uffff'
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    53
      var max = '\u0000'
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    54
      for ((x, _) <- list) {
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    55
        val c = x(0)
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    56
        if (c < min) min = c
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    57
        if (c > max) max = c
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    58
      }
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    59
      (min, max)
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    60
    }
31522
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
    61
    private val table =
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
    62
    {
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
    63
      val table = new jcl.HashMap[String, String]   // reasonably efficient?
27937
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    64
      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
    65
      table
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    66
    }
31522
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
    67
    def recode(text: String): String =
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
    68
    {
27937
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    69
      val len = text.length
31522
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
    70
      val matcher = regex.pattern.matcher(text)
27937
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    71
      val result = new StringBuilder(len)
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    72
      var i = 0
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    73
      while (i < len) {
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    74
        val c = text(i)
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    75
        if (min <= c && c <= max) {
27939
41b1c0b769bf pattern: proper "." not "[.]"!
wenzelm
parents: 27938
diff changeset
    76
          matcher.region(i, len)
41b1c0b769bf pattern: proper "." not "[.]"!
wenzelm
parents: 27938
diff changeset
    77
          matcher.lookingAt
27938
3d5b12f23f15 recode: proper result for unmatched symbols;
wenzelm
parents: 27937
diff changeset
    78
          val x = matcher.group
31522
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
    79
          result.append(table.get(x) getOrElse x)
27937
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    80
          i = matcher.end
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    81
        }
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    82
        else { result.append(c); i += 1 }
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    83
      }
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    84
      result.toString
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    85
    }
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    86
  }
27924
8dd8b564faf5 tuned comments;
wenzelm
parents: 27923
diff changeset
    87
27918
85942d2036a0 reading symbol interpretation tables;
wenzelm
parents: 27905
diff changeset
    88
27923
7ebe9d38743a use scala.collection.jcl.HashMap, which seems to be more efficient;
wenzelm
parents: 27918
diff changeset
    89
27927
eb624bb54bc6 tuned Recoder;
wenzelm
parents: 27926
diff changeset
    90
  /** Symbol interpretation **/
eb624bb54bc6 tuned Recoder;
wenzelm
parents: 27926
diff changeset
    91
29569
f3f529b5d8fb more general init of Symbol.Interpretation, independent of IsabelleSystem instance;
wenzelm
parents: 29174
diff changeset
    92
  class Interpretation(symbol_decls: Iterator[String])
f3f529b5d8fb more general init of Symbol.Interpretation, independent of IsabelleSystem instance;
wenzelm
parents: 29174
diff changeset
    93
  {
31522
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
    94
    /* read symbols */
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
    95
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
    96
    private val empty = new Regex("""(?xs) ^\s* (?: \#.* )? $ """)
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
    97
    private val key = new Regex("""(?xs) (.+): """)
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
    98
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
    99
    private def read_decl(decl: String): (String, Map[String, String]) =
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   100
    {
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   101
      def err() = error("Bad symbol declaration: " + decl)
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   102
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   103
      def read_props(props: List[String]): Map[String, String] =
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   104
      {
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   105
        props match {
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   106
          case Nil => Map()
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   107
          case _ :: Nil => err()
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   108
          case key(x) :: y :: rest => read_props(rest) + (x -> y)
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   109
          case _ => err()
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   110
        }
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   111
      }
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   112
      decl.split("\\s+").toList match {
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   113
        case Nil => err()
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   114
        case sym :: props => (sym, read_props(props))
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   115
      }
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   116
    }
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   117
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   118
    private val symbols: List[(String, Map[String, String])] =
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   119
      for (decl <- symbol_decls.toList if !empty.pattern.matcher(decl).matches)
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   120
        yield read_decl(decl)
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   121
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   122
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   123
    /* main recoder methods */
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   124
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   125
    private val (decoder, encoder) =
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   126
    {
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   127
      val mapping =
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   128
        for {
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   129
          (sym, props) <- symbols
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   130
          val code =
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   131
            try { Integer.decode(props("code")).intValue }
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   132
            catch {
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   133
              case _: NoSuchElementException => error("Missing code for symbol " + sym)
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   134
              case _: NumberFormatException => error("Bad code for symbol " + sym)
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   135
            }
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   136
          val ch = new String(Character.toChars(code))
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   137
        } yield (sym, ch)
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   138
      (new Recoder(mapping ++ (for ((x, y) <- mapping) yield ("\\" + x, y))),
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   139
       new Recoder(for ((x, y) <- mapping) yield (y, x)))
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   140
    }
27918
85942d2036a0 reading symbol interpretation tables;
wenzelm
parents: 27905
diff changeset
   141
27924
8dd8b564faf5 tuned comments;
wenzelm
parents: 27923
diff changeset
   142
    def decode(text: String) = decoder.recode(text)
8dd8b564faf5 tuned comments;
wenzelm
parents: 27923
diff changeset
   143
    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
   144
27918
85942d2036a0 reading symbol interpretation tables;
wenzelm
parents: 27905
diff changeset
   145
  }
27901
28083e9f8d1d Basic support for Isabelle symbols.
wenzelm
parents:
diff changeset
   146
}