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