src/Pure/General/symbol.scala
author wenzelm
Sun, 06 Dec 2009 23:06:53 +0100
changeset 34001 6e5eafb373b3
parent 33998 fc56cfc6906e
child 34098 2b9cdf23c188
permissions -rw-r--r--
elements: more convenient result;
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
31929
ecfc667cac53 is_open: surrogate sequence is High..Low;
wenzelm
parents: 31651
diff changeset
    10
import scala.collection.{jcl, mutable}
31522
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
{
33998
fc56cfc6906e added elements: Interator;
wenzelm
parents: 31929
diff changeset
    16
  /* Symbol regexps */
27901
28083e9f8d1d Basic support for Isabelle symbols.
wenzelm
parents:
diff changeset
    17
31522
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
    18
  private val plain = new Regex("""(?xs)
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
    19
    [^\\ \ud800-\udfff] | [\ud800-\udbff][\udc00-\udfff] """)
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 symbol = new Regex("""(?xs)
31545
5f1f0a20af4d discontinued escaped symbols such as \\<forall> -- only one backslash should be used;
wenzelm
parents: 31523
diff changeset
    22
      \\ < (?:
27924
8dd8b564faf5 tuned comments;
wenzelm
parents: 27923
diff changeset
    23
      \^? [A-Za-z][A-Za-z0-9_']* |
8dd8b564faf5 tuned comments;
wenzelm
parents: 27923
diff changeset
    24
      \^raw: [\x20-\x7e\u0100-\uffff && [^.>]]* ) >""")
8dd8b564faf5 tuned comments;
wenzelm
parents: 27923
diff changeset
    25
31522
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
    26
  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
    27
    """ \\ < (?: (?! \s | [\"`\\] | \(\* | \*\) | \{\* | \*\} ) . )*""")
27924
8dd8b564faf5 tuned comments;
wenzelm
parents: 27923
diff changeset
    28
27939
41b1c0b769bf pattern: proper "." not "[.]"!
wenzelm
parents: 27938
diff changeset
    29
  // total pattern
31522
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
    30
  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
    31
31522
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
    32
  // prefix of another symbol
33998
fc56cfc6906e added elements: Interator;
wenzelm
parents: 31929
diff changeset
    33
  def is_open(s: CharSequence): Boolean =
31522
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
    34
  {
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
    35
    val len = s.length
33998
fc56cfc6906e added elements: Interator;
wenzelm
parents: 31929
diff changeset
    36
    len == 1 && Character.isHighSurrogate(s.charAt(0)) ||
31522
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
    37
    s == "\\" ||
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
    38
    s == "\\<" ||
33998
fc56cfc6906e added elements: Interator;
wenzelm
parents: 31929
diff changeset
    39
    len > 2 && s.charAt(len - 1) != '>'
31522
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
    40
  }
27937
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    41
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
    42
33998
fc56cfc6906e added elements: Interator;
wenzelm
parents: 31929
diff changeset
    43
  /* elements */
fc56cfc6906e added elements: Interator;
wenzelm
parents: 31929
diff changeset
    44
fc56cfc6906e added elements: Interator;
wenzelm
parents: 31929
diff changeset
    45
  private def could_open(c: Char): Boolean =
fc56cfc6906e added elements: Interator;
wenzelm
parents: 31929
diff changeset
    46
    c == '\\' || Character.isHighSurrogate(c)
31929
ecfc667cac53 is_open: surrogate sequence is High..Low;
wenzelm
parents: 31651
diff changeset
    47
34001
6e5eafb373b3 elements: more convenient result;
wenzelm
parents: 33998
diff changeset
    48
  def elements(text: CharSequence) = new Iterator[String] {
33998
fc56cfc6906e added elements: Interator;
wenzelm
parents: 31929
diff changeset
    49
    private val matcher = regex.pattern.matcher(text)
fc56cfc6906e added elements: Interator;
wenzelm
parents: 31929
diff changeset
    50
    private var i = 0
fc56cfc6906e added elements: Interator;
wenzelm
parents: 31929
diff changeset
    51
    def hasNext = i < text.length
fc56cfc6906e added elements: Interator;
wenzelm
parents: 31929
diff changeset
    52
    def next = {
fc56cfc6906e added elements: Interator;
wenzelm
parents: 31929
diff changeset
    53
      val len =
fc56cfc6906e added elements: Interator;
wenzelm
parents: 31929
diff changeset
    54
        if (could_open(text.charAt(i))) {
fc56cfc6906e added elements: Interator;
wenzelm
parents: 31929
diff changeset
    55
          matcher.region(i, text.length).lookingAt
fc56cfc6906e added elements: Interator;
wenzelm
parents: 31929
diff changeset
    56
          matcher.group.length
fc56cfc6906e added elements: Interator;
wenzelm
parents: 31929
diff changeset
    57
        }
fc56cfc6906e added elements: Interator;
wenzelm
parents: 31929
diff changeset
    58
        else 1
fc56cfc6906e added elements: Interator;
wenzelm
parents: 31929
diff changeset
    59
      val s = text.subSequence(i, i + len)
fc56cfc6906e added elements: Interator;
wenzelm
parents: 31929
diff changeset
    60
      i += len
34001
6e5eafb373b3 elements: more convenient result;
wenzelm
parents: 33998
diff changeset
    61
      s.toString
33998
fc56cfc6906e added elements: Interator;
wenzelm
parents: 31929
diff changeset
    62
    }
fc56cfc6906e added elements: Interator;
wenzelm
parents: 31929
diff changeset
    63
  }
fc56cfc6906e added elements: Interator;
wenzelm
parents: 31929
diff changeset
    64
fc56cfc6906e added elements: Interator;
wenzelm
parents: 31929
diff changeset
    65
fc56cfc6906e added elements: Interator;
wenzelm
parents: 31929
diff changeset
    66
  /* decoding offsets */
fc56cfc6906e added elements: Interator;
wenzelm
parents: 31929
diff changeset
    67
fc56cfc6906e added elements: Interator;
wenzelm
parents: 31929
diff changeset
    68
  class Index(text: CharSequence)
31929
ecfc667cac53 is_open: surrogate sequence is High..Low;
wenzelm
parents: 31651
diff changeset
    69
  {
ecfc667cac53 is_open: surrogate sequence is High..Low;
wenzelm
parents: 31651
diff changeset
    70
    case class Entry(chr: Int, sym: Int)
ecfc667cac53 is_open: surrogate sequence is High..Low;
wenzelm
parents: 31651
diff changeset
    71
    val index: Array[Entry] =
ecfc667cac53 is_open: surrogate sequence is High..Low;
wenzelm
parents: 31651
diff changeset
    72
    {
ecfc667cac53 is_open: surrogate sequence is High..Low;
wenzelm
parents: 31651
diff changeset
    73
      val matcher = regex.pattern.matcher(text)
ecfc667cac53 is_open: surrogate sequence is High..Low;
wenzelm
parents: 31651
diff changeset
    74
      val buf = new mutable.ArrayBuffer[Entry]
ecfc667cac53 is_open: surrogate sequence is High..Low;
wenzelm
parents: 31651
diff changeset
    75
      var chr = 0
ecfc667cac53 is_open: surrogate sequence is High..Low;
wenzelm
parents: 31651
diff changeset
    76
      var sym = 0
33998
fc56cfc6906e added elements: Interator;
wenzelm
parents: 31929
diff changeset
    77
      while (chr < text.length) {
31929
ecfc667cac53 is_open: surrogate sequence is High..Low;
wenzelm
parents: 31651
diff changeset
    78
        val len =
33998
fc56cfc6906e added elements: Interator;
wenzelm
parents: 31929
diff changeset
    79
          if (could_open(text.charAt(chr))) {
fc56cfc6906e added elements: Interator;
wenzelm
parents: 31929
diff changeset
    80
            matcher.region(chr, text.length).lookingAt
31929
ecfc667cac53 is_open: surrogate sequence is High..Low;
wenzelm
parents: 31651
diff changeset
    81
            matcher.group.length
33998
fc56cfc6906e added elements: Interator;
wenzelm
parents: 31929
diff changeset
    82
          }
fc56cfc6906e added elements: Interator;
wenzelm
parents: 31929
diff changeset
    83
          else 1
31929
ecfc667cac53 is_open: surrogate sequence is High..Low;
wenzelm
parents: 31651
diff changeset
    84
        chr += len
ecfc667cac53 is_open: surrogate sequence is High..Low;
wenzelm
parents: 31651
diff changeset
    85
        sym += 1
ecfc667cac53 is_open: surrogate sequence is High..Low;
wenzelm
parents: 31651
diff changeset
    86
        if (len > 1) buf += Entry(chr, sym)
ecfc667cac53 is_open: surrogate sequence is High..Low;
wenzelm
parents: 31651
diff changeset
    87
      }
ecfc667cac53 is_open: surrogate sequence is High..Low;
wenzelm
parents: 31651
diff changeset
    88
      buf.toArray
ecfc667cac53 is_open: surrogate sequence is High..Low;
wenzelm
parents: 31651
diff changeset
    89
    }
ecfc667cac53 is_open: surrogate sequence is High..Low;
wenzelm
parents: 31651
diff changeset
    90
    def decode(sym: Int): Int =
ecfc667cac53 is_open: surrogate sequence is High..Low;
wenzelm
parents: 31651
diff changeset
    91
    {
ecfc667cac53 is_open: surrogate sequence is High..Low;
wenzelm
parents: 31651
diff changeset
    92
      val end = index.length
ecfc667cac53 is_open: surrogate sequence is High..Low;
wenzelm
parents: 31651
diff changeset
    93
      def bisect(a: Int, b: Int): Int =
ecfc667cac53 is_open: surrogate sequence is High..Low;
wenzelm
parents: 31651
diff changeset
    94
      {
ecfc667cac53 is_open: surrogate sequence is High..Low;
wenzelm
parents: 31651
diff changeset
    95
        if (a < b) {
ecfc667cac53 is_open: surrogate sequence is High..Low;
wenzelm
parents: 31651
diff changeset
    96
          val c = (a + b) / 2
ecfc667cac53 is_open: surrogate sequence is High..Low;
wenzelm
parents: 31651
diff changeset
    97
          if (sym < index(c).sym) bisect(a, c)
ecfc667cac53 is_open: surrogate sequence is High..Low;
wenzelm
parents: 31651
diff changeset
    98
          else if (c + 1 == end || sym < index(c + 1).sym) c
ecfc667cac53 is_open: surrogate sequence is High..Low;
wenzelm
parents: 31651
diff changeset
    99
          else bisect(c + 1, b)
ecfc667cac53 is_open: surrogate sequence is High..Low;
wenzelm
parents: 31651
diff changeset
   100
        }
ecfc667cac53 is_open: surrogate sequence is High..Low;
wenzelm
parents: 31651
diff changeset
   101
        else -1
ecfc667cac53 is_open: surrogate sequence is High..Low;
wenzelm
parents: 31651
diff changeset
   102
      }
ecfc667cac53 is_open: surrogate sequence is High..Low;
wenzelm
parents: 31651
diff changeset
   103
      val i = bisect(0, end)
ecfc667cac53 is_open: surrogate sequence is High..Low;
wenzelm
parents: 31651
diff changeset
   104
      if (i < 0) sym
ecfc667cac53 is_open: surrogate sequence is High..Low;
wenzelm
parents: 31651
diff changeset
   105
      else index(i).chr + sym - index(i).sym
ecfc667cac53 is_open: surrogate sequence is High..Low;
wenzelm
parents: 31651
diff changeset
   106
    }
ecfc667cac53 is_open: surrogate sequence is High..Low;
wenzelm
parents: 31651
diff changeset
   107
  }
ecfc667cac53 is_open: surrogate sequence is High..Low;
wenzelm
parents: 31651
diff changeset
   108
ecfc667cac53 is_open: surrogate sequence is High..Low;
wenzelm
parents: 31651
diff changeset
   109
33998
fc56cfc6906e added elements: Interator;
wenzelm
parents: 31929
diff changeset
   110
  /* recoding text */
27937
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
   111
31522
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   112
  private class Recoder(list: List[(String, String)])
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   113
  {
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   114
    private val (min, max) =
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   115
    {
27937
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
   116
      var min = '\uffff'
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
   117
      var max = '\u0000'
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
   118
      for ((x, _) <- list) {
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
   119
        val c = x(0)
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
   120
        if (c < min) min = c
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
   121
        if (c > max) max = c
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
   122
      }
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
   123
      (min, max)
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
   124
    }
31522
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   125
    private val table =
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   126
    {
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   127
      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
   128
      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
   129
      table
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
   130
    }
31522
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   131
    def recode(text: String): String =
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   132
    {
27937
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
   133
      val len = text.length
31522
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   134
      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
   135
      val result = new StringBuilder(len)
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
   136
      var i = 0
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
   137
      while (i < len) {
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
   138
        val c = text(i)
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
   139
        if (min <= c && c <= max) {
31929
ecfc667cac53 is_open: surrogate sequence is High..Low;
wenzelm
parents: 31651
diff changeset
   140
          matcher.region(i, len).lookingAt
27938
3d5b12f23f15 recode: proper result for unmatched symbols;
wenzelm
parents: 27937
diff changeset
   141
          val x = matcher.group
31522
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   142
          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
   143
          i = matcher.end
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
   144
        }
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
   145
        else { result.append(c); i += 1 }
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
   146
      }
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
   147
      result.toString
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
   148
    }
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
   149
  }
27924
8dd8b564faf5 tuned comments;
wenzelm
parents: 27923
diff changeset
   150
27918
85942d2036a0 reading symbol interpretation tables;
wenzelm
parents: 27905
diff changeset
   151
27923
7ebe9d38743a use scala.collection.jcl.HashMap, which seems to be more efficient;
wenzelm
parents: 27918
diff changeset
   152
27927
eb624bb54bc6 tuned Recoder;
wenzelm
parents: 27926
diff changeset
   153
  /** Symbol interpretation **/
eb624bb54bc6 tuned Recoder;
wenzelm
parents: 27926
diff changeset
   154
29569
f3f529b5d8fb more general init of Symbol.Interpretation, independent of IsabelleSystem instance;
wenzelm
parents: 29174
diff changeset
   155
  class Interpretation(symbol_decls: Iterator[String])
f3f529b5d8fb more general init of Symbol.Interpretation, independent of IsabelleSystem instance;
wenzelm
parents: 29174
diff changeset
   156
  {
31522
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   157
    /* read symbols */
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   158
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   159
    private val empty = new Regex("""(?xs) ^\s* (?: \#.* )? $ """)
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   160
    private val key = new Regex("""(?xs) (.+): """)
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   161
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   162
    private def read_decl(decl: String): (String, Map[String, String]) =
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   163
    {
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   164
      def err() = error("Bad symbol declaration: " + decl)
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   165
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   166
      def read_props(props: List[String]): Map[String, String] =
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   167
      {
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   168
        props match {
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   169
          case Nil => Map()
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   170
          case _ :: Nil => err()
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   171
          case key(x) :: y :: rest => read_props(rest) + (x -> y)
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   172
          case _ => err()
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   173
        }
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   174
      }
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   175
      decl.split("\\s+").toList match {
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   176
        case Nil => err()
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   177
        case sym :: props => (sym, read_props(props))
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   178
      }
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   179
    }
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   180
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   181
    private val symbols: List[(String, Map[String, String])] =
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   182
      for (decl <- symbol_decls.toList if !empty.pattern.matcher(decl).matches)
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   183
        yield read_decl(decl)
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   184
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   185
31651
7d6a518b5a2b added names, abbrevs;
wenzelm
parents: 31548
diff changeset
   186
    /* misc properties */
7d6a518b5a2b added names, abbrevs;
wenzelm
parents: 31548
diff changeset
   187
7d6a518b5a2b added names, abbrevs;
wenzelm
parents: 31548
diff changeset
   188
    val names: Map[String, String] = {
7d6a518b5a2b added names, abbrevs;
wenzelm
parents: 31548
diff changeset
   189
      val name = new Regex("""\\<([A-Za-z][A-Za-z0-9_']*)>""")
7d6a518b5a2b added names, abbrevs;
wenzelm
parents: 31548
diff changeset
   190
      Map((for ((sym @ name(a), _) <- symbols) yield (sym -> a)): _*)
7d6a518b5a2b added names, abbrevs;
wenzelm
parents: 31548
diff changeset
   191
    }
7d6a518b5a2b added names, abbrevs;
wenzelm
parents: 31548
diff changeset
   192
7d6a518b5a2b added names, abbrevs;
wenzelm
parents: 31548
diff changeset
   193
    val abbrevs: Map[String, String] = Map((
7d6a518b5a2b added names, abbrevs;
wenzelm
parents: 31548
diff changeset
   194
      for ((sym, props) <- symbols if props.isDefinedAt("abbrev"))
7d6a518b5a2b added names, abbrevs;
wenzelm
parents: 31548
diff changeset
   195
        yield (sym -> props("abbrev"))): _*)
7d6a518b5a2b added names, abbrevs;
wenzelm
parents: 31548
diff changeset
   196
7d6a518b5a2b added names, abbrevs;
wenzelm
parents: 31548
diff changeset
   197
31522
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   198
    /* main recoder methods */
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   199
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   200
    private val (decoder, encoder) =
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   201
    {
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   202
      val mapping =
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   203
        for {
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   204
          (sym, props) <- symbols
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   205
          val code =
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   206
            try { Integer.decode(props("code")).intValue }
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   207
            catch {
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   208
              case _: NoSuchElementException => error("Missing code for symbol " + sym)
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   209
              case _: NumberFormatException => error("Bad code for symbol " + sym)
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   210
            }
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   211
          val ch = new String(Character.toChars(code))
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   212
        } yield (sym, ch)
31545
5f1f0a20af4d discontinued escaped symbols such as \\<forall> -- only one backslash should be used;
wenzelm
parents: 31523
diff changeset
   213
      (new Recoder(mapping),
31548
wenzelm
parents: 31545
diff changeset
   214
       new Recoder(mapping map { case (x, y) => (y, x) }))
31522
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   215
    }
27918
85942d2036a0 reading symbol interpretation tables;
wenzelm
parents: 27905
diff changeset
   216
27924
8dd8b564faf5 tuned comments;
wenzelm
parents: 27923
diff changeset
   217
    def decode(text: String) = decoder.recode(text)
8dd8b564faf5 tuned comments;
wenzelm
parents: 27923
diff changeset
   218
    def encode(text: String) = encoder.recode(text)
27918
85942d2036a0 reading symbol interpretation tables;
wenzelm
parents: 27905
diff changeset
   219
  }
27901
28083e9f8d1d Basic support for Isabelle symbols.
wenzelm
parents:
diff changeset
   220
}