src/Pure/General/symbol.scala
author wenzelm
Wed, 03 Mar 2021 21:58:29 +0100
changeset 73602 ec52a1a6ed31
parent 73587 0af9e7e4476f
child 73606 d8a0e996614b
permissions -rw-r--r--
tuned --- fewer warnings;
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
69502
ce85542368b9 more Haskell operations;
wenzelm
parents: 69458
diff changeset
     4
Isabelle text 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
55618
995162143ef4 tuned imports;
wenzelm
parents: 55497
diff changeset
     9
36011
3ff725ac13a4 adapted to Scala 2.8.0 Beta1 -- with notable changes to scala.collection;
wenzelm
parents: 34316
diff changeset
    10
import scala.collection.mutable
31522
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
    11
import scala.util.matching.Regex
48922
6f3ccfa7818d more precise counting of line/column;
wenzelm
parents: 48775
diff changeset
    12
import scala.annotation.tailrec
27901
28083e9f8d1d Basic support for Isabelle symbols.
wenzelm
parents:
diff changeset
    13
28083e9f8d1d Basic support for Isabelle symbols.
wenzelm
parents:
diff changeset
    14
31522
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
    15
object Symbol
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
    16
{
43696
58bb7ca5c7a2 explicit indication of type Symbol.Symbol;
wenzelm
parents: 43695
diff changeset
    17
  type Symbol = String
58bb7ca5c7a2 explicit indication of type Symbol.Symbol;
wenzelm
parents: 43695
diff changeset
    18
55884
f2c0eaedd579 tuned signature -- emphasize symbol positions (prover) vs. decoded text offsets (editor);
wenzelm
parents: 55618
diff changeset
    19
  // counting Isabelle symbols, starting from 1
f2c0eaedd579 tuned signature -- emphasize symbol positions (prover) vs. decoded text offsets (editor);
wenzelm
parents: 55618
diff changeset
    20
  type Offset = Text.Offset
f2c0eaedd579 tuned signature -- emphasize symbol positions (prover) vs. decoded text offsets (editor);
wenzelm
parents: 55618
diff changeset
    21
  type Range = Text.Range
f2c0eaedd579 tuned signature -- emphasize symbol positions (prover) vs. decoded text offsets (editor);
wenzelm
parents: 55618
diff changeset
    22
43696
58bb7ca5c7a2 explicit indication of type Symbol.Symbol;
wenzelm
parents: 43695
diff changeset
    23
61867
6dcc9e4f1aa6 tuned signature;
wenzelm
parents: 61579
diff changeset
    24
  /* spaces */
6dcc9e4f1aa6 tuned signature;
wenzelm
parents: 61579
diff changeset
    25
71864
2acdbb6ee521 pretty formatting as in Isabelle/ML;
wenzelm
parents: 71816
diff changeset
    26
  val space_char = ' '
61867
6dcc9e4f1aa6 tuned signature;
wenzelm
parents: 61579
diff changeset
    27
  val space = " "
6dcc9e4f1aa6 tuned signature;
wenzelm
parents: 61579
diff changeset
    28
6dcc9e4f1aa6 tuned signature;
wenzelm
parents: 61579
diff changeset
    29
  private val static_spaces = space * 4000
6dcc9e4f1aa6 tuned signature;
wenzelm
parents: 61579
diff changeset
    30
6dcc9e4f1aa6 tuned signature;
wenzelm
parents: 61579
diff changeset
    31
  def spaces(n: Int): String =
6dcc9e4f1aa6 tuned signature;
wenzelm
parents: 61579
diff changeset
    32
  {
73364
c3589f2dff31 more informative errors: simplify diagnosis of spurious failures reported by users;
wenzelm
parents: 73270
diff changeset
    33
    require(n >= 0, "negative spaces")
61867
6dcc9e4f1aa6 tuned signature;
wenzelm
parents: 61579
diff changeset
    34
    if (n < static_spaces.length) static_spaces.substring(0, n)
6dcc9e4f1aa6 tuned signature;
wenzelm
parents: 61579
diff changeset
    35
    else space * n
6dcc9e4f1aa6 tuned signature;
wenzelm
parents: 61579
diff changeset
    36
  }
6dcc9e4f1aa6 tuned signature;
wenzelm
parents: 61579
diff changeset
    37
6dcc9e4f1aa6 tuned signature;
wenzelm
parents: 61579
diff changeset
    38
43418
c69e9fbb81a8 recovered markup for non-alphabetic keywords;
wenzelm
parents: 40529
diff changeset
    39
  /* ASCII characters */
c69e9fbb81a8 recovered markup for non-alphabetic keywords;
wenzelm
parents: 40529
diff changeset
    40
71864
2acdbb6ee521 pretty formatting as in Isabelle/ML;
wenzelm
parents: 71816
diff changeset
    41
  def is_ascii_printable(c: Char): Boolean = space_char <= c && c <= '~'
2acdbb6ee521 pretty formatting as in Isabelle/ML;
wenzelm
parents: 71816
diff changeset
    42
43418
c69e9fbb81a8 recovered markup for non-alphabetic keywords;
wenzelm
parents: 40529
diff changeset
    43
  def is_ascii_letter(c: Char): Boolean = 'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z'
55497
c0f8aebfb43d lexical syntax for SML (in Scala);
wenzelm
parents: 55430
diff changeset
    44
43418
c69e9fbb81a8 recovered markup for non-alphabetic keywords;
wenzelm
parents: 40529
diff changeset
    45
  def is_ascii_digit(c: Char): Boolean = '0' <= c && c <= '9'
55497
c0f8aebfb43d lexical syntax for SML (in Scala);
wenzelm
parents: 55430
diff changeset
    46
c0f8aebfb43d lexical syntax for SML (in Scala);
wenzelm
parents: 55430
diff changeset
    47
  def is_ascii_hex(c: Char): Boolean =
c0f8aebfb43d lexical syntax for SML (in Scala);
wenzelm
parents: 55430
diff changeset
    48
    '0' <= c && c <= '9' || 'A' <= c && c <= 'F' || 'a' <= c && c <= 'f'
c0f8aebfb43d lexical syntax for SML (in Scala);
wenzelm
parents: 55430
diff changeset
    49
43418
c69e9fbb81a8 recovered markup for non-alphabetic keywords;
wenzelm
parents: 40529
diff changeset
    50
  def is_ascii_quasi(c: Char): Boolean = c == '_' || c == '\''
c69e9fbb81a8 recovered markup for non-alphabetic keywords;
wenzelm
parents: 40529
diff changeset
    51
55497
c0f8aebfb43d lexical syntax for SML (in Scala);
wenzelm
parents: 55430
diff changeset
    52
  def is_ascii_blank(c: Char): Boolean = " \t\n\u000b\f\r".contains(c)
c0f8aebfb43d lexical syntax for SML (in Scala);
wenzelm
parents: 55430
diff changeset
    53
69458
51e696887b81 more uniform multi-language operations;
wenzelm
parents: 69328
diff changeset
    54
  def is_ascii_line_terminator(c: Char): Boolean = "\r\n".contains(c)
51e696887b81 more uniform multi-language operations;
wenzelm
parents: 69328
diff changeset
    55
43418
c69e9fbb81a8 recovered markup for non-alphabetic keywords;
wenzelm
parents: 40529
diff changeset
    56
  def is_ascii_letdig(c: Char): Boolean =
c69e9fbb81a8 recovered markup for non-alphabetic keywords;
wenzelm
parents: 40529
diff changeset
    57
    is_ascii_letter(c) || is_ascii_digit(c) || is_ascii_quasi(c)
c69e9fbb81a8 recovered markup for non-alphabetic keywords;
wenzelm
parents: 40529
diff changeset
    58
c69e9fbb81a8 recovered markup for non-alphabetic keywords;
wenzelm
parents: 40529
diff changeset
    59
  def is_ascii_identifier(s: String): Boolean =
50238
98d35a7368bd more uniform Symbol.is_ascii_identifier in ML/Scala;
wenzelm
parents: 50233
diff changeset
    60
    s.length > 0 && is_ascii_letter(s(0)) && s.forall(is_ascii_letdig)
43418
c69e9fbb81a8 recovered markup for non-alphabetic keywords;
wenzelm
parents: 40529
diff changeset
    61
62529
c8c532b22947 clarified ML syntax for strings concerning UTF8;
wenzelm
parents: 62230
diff changeset
    62
  def ascii(c: Char): Symbol =
c8c532b22947 clarified ML syntax for strings concerning UTF8;
wenzelm
parents: 62230
diff changeset
    63
  {
c8c532b22947 clarified ML syntax for strings concerning UTF8;
wenzelm
parents: 62230
diff changeset
    64
    if (c > 127) error("Non-ASCII character: " + quote(c.toString))
c8c532b22947 clarified ML syntax for strings concerning UTF8;
wenzelm
parents: 62230
diff changeset
    65
    else char_symbols(c.toInt)
c8c532b22947 clarified ML syntax for strings concerning UTF8;
wenzelm
parents: 62230
diff changeset
    66
  }
c8c532b22947 clarified ML syntax for strings concerning UTF8;
wenzelm
parents: 62230
diff changeset
    67
66919
1f93e376aeb6 more explicit check;
wenzelm
parents: 66051
diff changeset
    68
  def is_ascii(s: Symbol): Boolean = s.length == 1 && s(0) < 128
1f93e376aeb6 more explicit check;
wenzelm
parents: 66051
diff changeset
    69
43418
c69e9fbb81a8 recovered markup for non-alphabetic keywords;
wenzelm
parents: 40529
diff changeset
    70
48775
92ceb058391f simplified symbol matching;
wenzelm
parents: 48774
diff changeset
    71
  /* symbol matching */
27901
28083e9f8d1d Basic support for Isabelle symbols.
wenzelm
parents:
diff changeset
    72
73602
ec52a1a6ed31 tuned --- fewer warnings;
wenzelm
parents: 73587
diff changeset
    73
  private val symbol_total =
ec52a1a6ed31 tuned --- fewer warnings;
wenzelm
parents: 73587
diff changeset
    74
    new Regex("(?xs) [\ud800-\udbff][\udc00-\udfff] " +
ec52a1a6ed31 tuned --- fewer warnings;
wenzelm
parents: 73587
diff changeset
    75
      """ | \r\n | \\ < \^? ([A-Za-z][A-Za-z0-9_']*)? >? | .""")
27924
8dd8b564faf5 tuned comments;
wenzelm
parents: 27923
diff changeset
    76
48775
92ceb058391f simplified symbol matching;
wenzelm
parents: 48774
diff changeset
    77
  private def is_plain(c: Char): Boolean =
92ceb058391f simplified symbol matching;
wenzelm
parents: 48774
diff changeset
    78
    !(c == '\r' || c == '\\' || Character.isHighSurrogate(c))
48773
0e1bab274672 more liberal scanning of potentially malformed symbols;
wenzelm
parents: 48704
diff changeset
    79
0e1bab274672 more liberal scanning of potentially malformed symbols;
wenzelm
parents: 48704
diff changeset
    80
  def is_malformed(s: Symbol): Boolean =
0e1bab274672 more liberal scanning of potentially malformed symbols;
wenzelm
parents: 48704
diff changeset
    81
    s.length match {
0e1bab274672 more liberal scanning of potentially malformed symbols;
wenzelm
parents: 48704
diff changeset
    82
      case 1 =>
0e1bab274672 more liberal scanning of potentially malformed symbols;
wenzelm
parents: 48704
diff changeset
    83
        val c = s(0)
0e1bab274672 more liberal scanning of potentially malformed symbols;
wenzelm
parents: 48704
diff changeset
    84
        Character.isHighSurrogate(c) || Character.isLowSurrogate(c) || c == '\ufffd'
0e1bab274672 more liberal scanning of potentially malformed symbols;
wenzelm
parents: 48704
diff changeset
    85
      case 2 =>
0e1bab274672 more liberal scanning of potentially malformed symbols;
wenzelm
parents: 48704
diff changeset
    86
        val c1 = s(0)
0e1bab274672 more liberal scanning of potentially malformed symbols;
wenzelm
parents: 48704
diff changeset
    87
        val c2 = s(1)
0e1bab274672 more liberal scanning of potentially malformed symbols;
wenzelm
parents: 48704
diff changeset
    88
        !(c1 == '\r' && c2 == '\n' || Character.isSurrogatePair(c1, c2))
48774
c4bd5bb3ae69 further clarification of malformed symbols;
wenzelm
parents: 48773
diff changeset
    89
      case _ => !s.endsWith(">") || s == "\\<>" || s == "\\<^>"
48773
0e1bab274672 more liberal scanning of potentially malformed symbols;
wenzelm
parents: 48704
diff changeset
    90
    }
34137
6cc9a0cbaf55 refined some Symbol operations/signatures;
wenzelm
parents: 34134
diff changeset
    91
54734
b91afc3aa3e6 clarified Proof General legacy: special treatment of \<^newline> only in TTY mode;
wenzelm
parents: 53400
diff changeset
    92
  def is_newline(s: Symbol): Boolean =
43675
8252d51d70e2 simplified Symbol.iterator: produce strings, which are mostly preallocated;
wenzelm
parents: 43511
diff changeset
    93
    s == "\n" || s == "\r" || s == "\r\n"
38877
682c4932b3cc Command.newlines: account for physical newlines;
wenzelm
parents: 38479
diff changeset
    94
34137
6cc9a0cbaf55 refined some Symbol operations/signatures;
wenzelm
parents: 34134
diff changeset
    95
  class Matcher(text: CharSequence)
6cc9a0cbaf55 refined some Symbol operations/signatures;
wenzelm
parents: 34134
diff changeset
    96
  {
48775
92ceb058391f simplified symbol matching;
wenzelm
parents: 48774
diff changeset
    97
    private val matcher = symbol_total.pattern.matcher(text)
34137
6cc9a0cbaf55 refined some Symbol operations/signatures;
wenzelm
parents: 34134
diff changeset
    98
    def apply(start: Int, end: Int): Int =
6cc9a0cbaf55 refined some Symbol operations/signatures;
wenzelm
parents: 34134
diff changeset
    99
    {
73364
c3589f2dff31 more informative errors: simplify diagnosis of spurious failures reported by users;
wenzelm
parents: 73270
diff changeset
   100
      require(0 <= start && start < end && end <= text.length, "bad matcher range")
34316
f879b649ac4c clarified Symbol.is_plain/is_wellformed -- is_closed was rejecting plain backslashes;
wenzelm
parents: 34193
diff changeset
   101
      if (is_plain(text.charAt(start))) 1
34138
4008c2f5a46e refined some Symbol operations/signatures;
wenzelm
parents: 34137
diff changeset
   102
      else {
34137
6cc9a0cbaf55 refined some Symbol operations/signatures;
wenzelm
parents: 34134
diff changeset
   103
        matcher.region(start, end).lookingAt
6cc9a0cbaf55 refined some Symbol operations/signatures;
wenzelm
parents: 34134
diff changeset
   104
        matcher.group.length
6cc9a0cbaf55 refined some Symbol operations/signatures;
wenzelm
parents: 34134
diff changeset
   105
      }
6cc9a0cbaf55 refined some Symbol operations/signatures;
wenzelm
parents: 34134
diff changeset
   106
    }
31522
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   107
  }
27937
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
   108
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
   109
43695
5130dfe1b7be simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents: 43675
diff changeset
   110
  /* iterator */
33998
fc56cfc6906e added elements: Interator;
wenzelm
parents: 31929
diff changeset
   111
43696
58bb7ca5c7a2 explicit indication of type Symbol.Symbol;
wenzelm
parents: 43695
diff changeset
   112
  private val char_symbols: Array[Symbol] =
43675
8252d51d70e2 simplified Symbol.iterator: produce strings, which are mostly preallocated;
wenzelm
parents: 43511
diff changeset
   113
    (0 until 256).iterator.map(i => new String(Array(i.toChar))).toArray
8252d51d70e2 simplified Symbol.iterator: produce strings, which are mostly preallocated;
wenzelm
parents: 43511
diff changeset
   114
43696
58bb7ca5c7a2 explicit indication of type Symbol.Symbol;
wenzelm
parents: 43695
diff changeset
   115
  def iterator(text: CharSequence): Iterator[Symbol] =
58bb7ca5c7a2 explicit indication of type Symbol.Symbol;
wenzelm
parents: 43695
diff changeset
   116
    new Iterator[Symbol]
40522
wenzelm
parents: 40443
diff changeset
   117
    {
43489
132f99cc0a43 tuned iteration over short symbols;
wenzelm
parents: 43488
diff changeset
   118
      private val matcher = new Matcher(text)
132f99cc0a43 tuned iteration over short symbols;
wenzelm
parents: 43488
diff changeset
   119
      private var i = 0
71816
97ccf48c2f0c misc tuning based on hints by IntelliJ IDEA;
wenzelm
parents: 71592
diff changeset
   120
      def hasNext: Boolean = i < text.length
73587
0af9e7e4476f tuned --- fewer warnings;
wenzelm
parents: 73454
diff changeset
   121
      def next(): Symbol =
43489
132f99cc0a43 tuned iteration over short symbols;
wenzelm
parents: 43488
diff changeset
   122
      {
132f99cc0a43 tuned iteration over short symbols;
wenzelm
parents: 43488
diff changeset
   123
        val n = matcher(i, text.length)
43675
8252d51d70e2 simplified Symbol.iterator: produce strings, which are mostly preallocated;
wenzelm
parents: 43511
diff changeset
   124
        val s =
8252d51d70e2 simplified Symbol.iterator: produce strings, which are mostly preallocated;
wenzelm
parents: 43511
diff changeset
   125
          if (n == 0) ""
8252d51d70e2 simplified Symbol.iterator: produce strings, which are mostly preallocated;
wenzelm
parents: 43511
diff changeset
   126
          else if (n == 1) {
8252d51d70e2 simplified Symbol.iterator: produce strings, which are mostly preallocated;
wenzelm
parents: 43511
diff changeset
   127
            val c = text.charAt(i)
8252d51d70e2 simplified Symbol.iterator: produce strings, which are mostly preallocated;
wenzelm
parents: 43511
diff changeset
   128
            if (c < char_symbols.length) char_symbols(c)
8252d51d70e2 simplified Symbol.iterator: produce strings, which are mostly preallocated;
wenzelm
parents: 43511
diff changeset
   129
            else text.subSequence(i, i + n).toString
8252d51d70e2 simplified Symbol.iterator: produce strings, which are mostly preallocated;
wenzelm
parents: 43511
diff changeset
   130
          }
8252d51d70e2 simplified Symbol.iterator: produce strings, which are mostly preallocated;
wenzelm
parents: 43511
diff changeset
   131
          else text.subSequence(i, i + n).toString
43489
132f99cc0a43 tuned iteration over short symbols;
wenzelm
parents: 43488
diff changeset
   132
        i += n
132f99cc0a43 tuned iteration over short symbols;
wenzelm
parents: 43488
diff changeset
   133
        s
132f99cc0a43 tuned iteration over short symbols;
wenzelm
parents: 43488
diff changeset
   134
      }
33998
fc56cfc6906e added elements: Interator;
wenzelm
parents: 31929
diff changeset
   135
    }
43489
132f99cc0a43 tuned iteration over short symbols;
wenzelm
parents: 43488
diff changeset
   136
44949
b49d7f1066c8 Symbol.explode as in ML;
wenzelm
parents: 44238
diff changeset
   137
  def explode(text: CharSequence): List[Symbol] = iterator(text).toList
b49d7f1066c8 Symbol.explode as in ML;
wenzelm
parents: 44238
diff changeset
   138
64616
fd0d6de380c6 more systematic text length;
wenzelm
parents: 64613
diff changeset
   139
  def length(text: CharSequence): Int = iterator(text).length
64618
01e50039edc9 more systematic text length wrt. encoding;
wenzelm
parents: 64616
diff changeset
   140
67435
f83c1842a559 trim blanks -- more thoroughly than in update_cartouches (for single-line comments);
wenzelm
parents: 67389
diff changeset
   141
  def trim_blanks(text: CharSequence): String =
71816
97ccf48c2f0c misc tuning based on hints by IntelliJ IDEA;
wenzelm
parents: 71592
diff changeset
   142
    Library.trim(is_blank, explode(text)).mkString
67435
f83c1842a559 trim blanks -- more thoroughly than in update_cartouches (for single-line comments);
wenzelm
parents: 67389
diff changeset
   143
69328
f3351bb4390e clarified presentation;
wenzelm
parents: 67449
diff changeset
   144
  def all_blank(str: String): Boolean =
71816
97ccf48c2f0c misc tuning based on hints by IntelliJ IDEA;
wenzelm
parents: 71592
diff changeset
   145
    iterator(str).forall(is_blank)
69328
f3351bb4390e clarified presentation;
wenzelm
parents: 67449
diff changeset
   146
f3351bb4390e clarified presentation;
wenzelm
parents: 67449
diff changeset
   147
  def trim_blank_lines(text: String): String =
f3351bb4390e clarified presentation;
wenzelm
parents: 67449
diff changeset
   148
    cat_lines(split_lines(text).dropWhile(all_blank).reverse.dropWhile(all_blank).reverse)
f3351bb4390e clarified presentation;
wenzelm
parents: 67449
diff changeset
   149
33998
fc56cfc6906e added elements: Interator;
wenzelm
parents: 31929
diff changeset
   150
fc56cfc6906e added elements: Interator;
wenzelm
parents: 31929
diff changeset
   151
  /* decoding offsets */
fc56cfc6906e added elements: Interator;
wenzelm
parents: 31929
diff changeset
   152
52507
27925b58d6bd tuned signature;
wenzelm
parents: 50564
diff changeset
   153
  object Index
27925b58d6bd tuned signature;
wenzelm
parents: 50564
diff changeset
   154
  {
56471
2293a4350716 more frugal Symbol.Index -- no need to waste space on mostly empty arrays;
wenzelm
parents: 56338
diff changeset
   155
    private sealed case class Entry(chr: Int, sym: Int)
52507
27925b58d6bd tuned signature;
wenzelm
parents: 50564
diff changeset
   156
56472
wenzelm
parents: 56471
diff changeset
   157
    val empty: Index = new Index(Nil)
56471
2293a4350716 more frugal Symbol.Index -- no need to waste space on mostly empty arrays;
wenzelm
parents: 56338
diff changeset
   158
2293a4350716 more frugal Symbol.Index -- no need to waste space on mostly empty arrays;
wenzelm
parents: 56338
diff changeset
   159
    def apply(text: CharSequence): Index =
31929
ecfc667cac53 is_open: surrogate sequence is High..Low;
wenzelm
parents: 31651
diff changeset
   160
    {
34137
6cc9a0cbaf55 refined some Symbol operations/signatures;
wenzelm
parents: 34134
diff changeset
   161
      val matcher = new Matcher(text)
56471
2293a4350716 more frugal Symbol.Index -- no need to waste space on mostly empty arrays;
wenzelm
parents: 56338
diff changeset
   162
      val buf = new mutable.ListBuffer[Entry]
31929
ecfc667cac53 is_open: surrogate sequence is High..Low;
wenzelm
parents: 31651
diff changeset
   163
      var chr = 0
ecfc667cac53 is_open: surrogate sequence is High..Low;
wenzelm
parents: 31651
diff changeset
   164
      var sym = 0
33998
fc56cfc6906e added elements: Interator;
wenzelm
parents: 31929
diff changeset
   165
      while (chr < text.length) {
34137
6cc9a0cbaf55 refined some Symbol operations/signatures;
wenzelm
parents: 34134
diff changeset
   166
        val n = matcher(chr, text.length)
6cc9a0cbaf55 refined some Symbol operations/signatures;
wenzelm
parents: 34134
diff changeset
   167
        chr += n
31929
ecfc667cac53 is_open: surrogate sequence is High..Low;
wenzelm
parents: 31651
diff changeset
   168
        sym += 1
34137
6cc9a0cbaf55 refined some Symbol operations/signatures;
wenzelm
parents: 34134
diff changeset
   169
        if (n > 1) buf += Entry(chr, sym)
31929
ecfc667cac53 is_open: surrogate sequence is High..Low;
wenzelm
parents: 31651
diff changeset
   170
      }
56472
wenzelm
parents: 56471
diff changeset
   171
      if (buf.isEmpty) empty else new Index(buf.toList)
31929
ecfc667cac53 is_open: surrogate sequence is High..Low;
wenzelm
parents: 31651
diff changeset
   172
    }
56471
2293a4350716 more frugal Symbol.Index -- no need to waste space on mostly empty arrays;
wenzelm
parents: 56338
diff changeset
   173
  }
55430
8eb6c740ec1a tuned signature;
wenzelm
parents: 55033
diff changeset
   174
56472
wenzelm
parents: 56471
diff changeset
   175
  final class Index private(entries: List[Index.Entry])
56471
2293a4350716 more frugal Symbol.Index -- no need to waste space on mostly empty arrays;
wenzelm
parents: 56338
diff changeset
   176
  {
56472
wenzelm
parents: 56471
diff changeset
   177
    private val hash: Int = entries.hashCode
wenzelm
parents: 56471
diff changeset
   178
    private val index: Array[Index.Entry] = entries.toArray
wenzelm
parents: 56471
diff changeset
   179
55884
f2c0eaedd579 tuned signature -- emphasize symbol positions (prover) vs. decoded text offsets (editor);
wenzelm
parents: 55618
diff changeset
   180
    def decode(symbol_offset: Offset): Text.Offset =
31929
ecfc667cac53 is_open: surrogate sequence is High..Low;
wenzelm
parents: 31651
diff changeset
   181
    {
55884
f2c0eaedd579 tuned signature -- emphasize symbol positions (prover) vs. decoded text offsets (editor);
wenzelm
parents: 55618
diff changeset
   182
      val sym = symbol_offset - 1
31929
ecfc667cac53 is_open: surrogate sequence is High..Low;
wenzelm
parents: 31651
diff changeset
   183
      val end = index.length
48922
6f3ccfa7818d more precise counting of line/column;
wenzelm
parents: 48775
diff changeset
   184
      @tailrec def bisect(a: Int, b: Int): Int =
31929
ecfc667cac53 is_open: surrogate sequence is High..Low;
wenzelm
parents: 31651
diff changeset
   185
      {
ecfc667cac53 is_open: surrogate sequence is High..Low;
wenzelm
parents: 31651
diff changeset
   186
        if (a < b) {
ecfc667cac53 is_open: surrogate sequence is High..Low;
wenzelm
parents: 31651
diff changeset
   187
          val c = (a + b) / 2
ecfc667cac53 is_open: surrogate sequence is High..Low;
wenzelm
parents: 31651
diff changeset
   188
          if (sym < index(c).sym) bisect(a, c)
ecfc667cac53 is_open: surrogate sequence is High..Low;
wenzelm
parents: 31651
diff changeset
   189
          else if (c + 1 == end || sym < index(c + 1).sym) c
ecfc667cac53 is_open: surrogate sequence is High..Low;
wenzelm
parents: 31651
diff changeset
   190
          else bisect(c + 1, b)
ecfc667cac53 is_open: surrogate sequence is High..Low;
wenzelm
parents: 31651
diff changeset
   191
        }
ecfc667cac53 is_open: surrogate sequence is High..Low;
wenzelm
parents: 31651
diff changeset
   192
        else -1
ecfc667cac53 is_open: surrogate sequence is High..Low;
wenzelm
parents: 31651
diff changeset
   193
      }
ecfc667cac53 is_open: surrogate sequence is High..Low;
wenzelm
parents: 31651
diff changeset
   194
      val i = bisect(0, end)
ecfc667cac53 is_open: surrogate sequence is High..Low;
wenzelm
parents: 31651
diff changeset
   195
      if (i < 0) sym
ecfc667cac53 is_open: surrogate sequence is High..Low;
wenzelm
parents: 31651
diff changeset
   196
      else index(i).chr + sym - index(i).sym
ecfc667cac53 is_open: surrogate sequence is High..Low;
wenzelm
parents: 31651
diff changeset
   197
    }
71816
97ccf48c2f0c misc tuning based on hints by IntelliJ IDEA;
wenzelm
parents: 71592
diff changeset
   198
    def decode(symbol_range: Range): Text.Range = symbol_range.map(decode)
56335
8953d4cc060a store blob content within document node: aux. files that were once open are made persistent;
wenzelm
parents: 55884
diff changeset
   199
56338
f968f4e3d520 proper structural hashCode, which is required for Command.File equals (NB: Array has physical object identity);
wenzelm
parents: 56335
diff changeset
   200
    override def hashCode: Int = hash
56335
8953d4cc060a store blob content within document node: aux. files that were once open are made persistent;
wenzelm
parents: 55884
diff changeset
   201
    override def equals(that: Any): Boolean =
8953d4cc060a store blob content within document node: aux. files that were once open are made persistent;
wenzelm
parents: 55884
diff changeset
   202
      that match {
8953d4cc060a store blob content within document node: aux. files that were once open are made persistent;
wenzelm
parents: 55884
diff changeset
   203
        case other: Index => index.sameElements(other.index)
8953d4cc060a store blob content within document node: aux. files that were once open are made persistent;
wenzelm
parents: 55884
diff changeset
   204
        case _ => false
8953d4cc060a store blob content within document node: aux. files that were once open are made persistent;
wenzelm
parents: 55884
diff changeset
   205
      }
31929
ecfc667cac53 is_open: surrogate sequence is High..Low;
wenzelm
parents: 31651
diff changeset
   206
  }
ecfc667cac53 is_open: surrogate sequence is High..Low;
wenzelm
parents: 31651
diff changeset
   207
ecfc667cac53 is_open: surrogate sequence is High..Low;
wenzelm
parents: 31651
diff changeset
   208
64477
8be21ca788ca tuned comment;
wenzelm
parents: 63936
diff changeset
   209
  /* symbolic text chunks -- without actual text */
56746
d37a5d09a277 tuned signature;
wenzelm
parents: 56472
diff changeset
   210
d37a5d09a277 tuned signature;
wenzelm
parents: 56472
diff changeset
   211
  object Text_Chunk
d37a5d09a277 tuned signature;
wenzelm
parents: 56472
diff changeset
   212
  {
d37a5d09a277 tuned signature;
wenzelm
parents: 56472
diff changeset
   213
    sealed abstract class Name
d37a5d09a277 tuned signature;
wenzelm
parents: 56472
diff changeset
   214
    case object Default extends Name
d37a5d09a277 tuned signature;
wenzelm
parents: 56472
diff changeset
   215
    case class Id(id: Document_ID.Generic) extends Name
d37a5d09a277 tuned signature;
wenzelm
parents: 56472
diff changeset
   216
    case class File(name: String) extends Name
d37a5d09a277 tuned signature;
wenzelm
parents: 56472
diff changeset
   217
d37a5d09a277 tuned signature;
wenzelm
parents: 56472
diff changeset
   218
    def apply(text: CharSequence): Text_Chunk =
d37a5d09a277 tuned signature;
wenzelm
parents: 56472
diff changeset
   219
      new Text_Chunk(Text.Range(0, text.length), Index(text))
d37a5d09a277 tuned signature;
wenzelm
parents: 56472
diff changeset
   220
  }
d37a5d09a277 tuned signature;
wenzelm
parents: 56472
diff changeset
   221
d37a5d09a277 tuned signature;
wenzelm
parents: 56472
diff changeset
   222
  final class Text_Chunk private(val range: Text.Range, private val index: Index)
d37a5d09a277 tuned signature;
wenzelm
parents: 56472
diff changeset
   223
  {
d37a5d09a277 tuned signature;
wenzelm
parents: 56472
diff changeset
   224
    override def hashCode: Int = (range, index).hashCode
d37a5d09a277 tuned signature;
wenzelm
parents: 56472
diff changeset
   225
    override def equals(that: Any): Boolean =
d37a5d09a277 tuned signature;
wenzelm
parents: 56472
diff changeset
   226
      that match {
d37a5d09a277 tuned signature;
wenzelm
parents: 56472
diff changeset
   227
        case other: Text_Chunk =>
d37a5d09a277 tuned signature;
wenzelm
parents: 56472
diff changeset
   228
          range == other.range &&
d37a5d09a277 tuned signature;
wenzelm
parents: 56472
diff changeset
   229
          index == other.index
d37a5d09a277 tuned signature;
wenzelm
parents: 56472
diff changeset
   230
        case _ => false
d37a5d09a277 tuned signature;
wenzelm
parents: 56472
diff changeset
   231
      }
d37a5d09a277 tuned signature;
wenzelm
parents: 56472
diff changeset
   232
57840
074cb68b40a8 tuned output;
wenzelm
parents: 56746
diff changeset
   233
    override def toString: String = "Text_Chunk" + range.toString
074cb68b40a8 tuned output;
wenzelm
parents: 56746
diff changeset
   234
56746
d37a5d09a277 tuned signature;
wenzelm
parents: 56472
diff changeset
   235
    def decode(symbol_offset: Offset): Text.Offset = index.decode(symbol_offset)
d37a5d09a277 tuned signature;
wenzelm
parents: 56472
diff changeset
   236
    def decode(symbol_range: Range): Text.Range = index.decode(symbol_range)
d37a5d09a277 tuned signature;
wenzelm
parents: 56472
diff changeset
   237
    def incorporate(symbol_range: Range): Option[Text.Range] =
d37a5d09a277 tuned signature;
wenzelm
parents: 56472
diff changeset
   238
    {
d37a5d09a277 tuned signature;
wenzelm
parents: 56472
diff changeset
   239
      def in(r: Range): Option[Text.Range] =
d37a5d09a277 tuned signature;
wenzelm
parents: 56472
diff changeset
   240
        range.try_restrict(decode(r)) match {
d37a5d09a277 tuned signature;
wenzelm
parents: 56472
diff changeset
   241
          case Some(r1) if !r1.is_singularity => Some(r1)
d37a5d09a277 tuned signature;
wenzelm
parents: 56472
diff changeset
   242
          case _ => None
d37a5d09a277 tuned signature;
wenzelm
parents: 56472
diff changeset
   243
        }
d37a5d09a277 tuned signature;
wenzelm
parents: 56472
diff changeset
   244
     in(symbol_range) orElse in(symbol_range - 1)
d37a5d09a277 tuned signature;
wenzelm
parents: 56472
diff changeset
   245
    }
d37a5d09a277 tuned signature;
wenzelm
parents: 56472
diff changeset
   246
  }
d37a5d09a277 tuned signature;
wenzelm
parents: 56472
diff changeset
   247
d37a5d09a277 tuned signature;
wenzelm
parents: 56472
diff changeset
   248
33998
fc56cfc6906e added elements: Interator;
wenzelm
parents: 31929
diff changeset
   249
  /* recoding text */
27937
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
   250
31522
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   251
  private class Recoder(list: List[(String, String)])
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   252
  {
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   253
    private val (min, max) =
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   254
    {
27937
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
   255
      var min = '\uffff'
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
   256
      var max = '\u0000'
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
   257
      for ((x, _) <- list) {
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
   258
        val c = x(0)
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
   259
        if (c < min) min = c
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
   260
        if (c > max) max = c
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
   261
      }
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
   262
      (min, max)
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
   263
    }
40443
41c32616298c explicitly check uniqueness of symbol recoding;
wenzelm
parents: 38877
diff changeset
   264
    private val table =
41c32616298c explicitly check uniqueness of symbol recoding;
wenzelm
parents: 38877
diff changeset
   265
    {
41c32616298c explicitly check uniqueness of symbol recoding;
wenzelm
parents: 38877
diff changeset
   266
      var tab = Map[String, String]()
41c32616298c explicitly check uniqueness of symbol recoding;
wenzelm
parents: 38877
diff changeset
   267
      for ((x, y) <- list) {
41c32616298c explicitly check uniqueness of symbol recoding;
wenzelm
parents: 38877
diff changeset
   268
        tab.get(x) match {
41c32616298c explicitly check uniqueness of symbol recoding;
wenzelm
parents: 38877
diff changeset
   269
          case None => tab += (x -> y)
41c32616298c explicitly check uniqueness of symbol recoding;
wenzelm
parents: 38877
diff changeset
   270
          case Some(z) =>
62230
949d2c9f6ff7 tuned message;
wenzelm
parents: 62104
diff changeset
   271
            error("Duplicate symbol mapping of " + quote(x) + " to " + quote(y) + " vs. " + quote(z))
40443
41c32616298c explicitly check uniqueness of symbol recoding;
wenzelm
parents: 38877
diff changeset
   272
        }
41c32616298c explicitly check uniqueness of symbol recoding;
wenzelm
parents: 38877
diff changeset
   273
      }
41c32616298c explicitly check uniqueness of symbol recoding;
wenzelm
parents: 38877
diff changeset
   274
      tab
41c32616298c explicitly check uniqueness of symbol recoding;
wenzelm
parents: 38877
diff changeset
   275
    }
31522
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   276
    def recode(text: String): String =
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   277
    {
27937
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
   278
      val len = text.length
48775
92ceb058391f simplified symbol matching;
wenzelm
parents: 48774
diff changeset
   279
      val matcher = symbol_total.pattern.matcher(text)
27937
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
   280
      val result = new StringBuilder(len)
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
   281
      var i = 0
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
   282
      while (i < len) {
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
   283
        val c = text(i)
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
   284
        if (min <= c && c <= max) {
31929
ecfc667cac53 is_open: surrogate sequence is High..Low;
wenzelm
parents: 31651
diff changeset
   285
          matcher.region(i, len).lookingAt
27938
3d5b12f23f15 recode: proper result for unmatched symbols;
wenzelm
parents: 27937
diff changeset
   286
          val x = matcher.group
52888
wenzelm
parents: 52616
diff changeset
   287
          result.append(table.getOrElse(x, x))
27937
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
   288
          i = matcher.end
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
   289
        }
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
   290
        else { result.append(c); i += 1 }
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
   291
      }
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
   292
      result.toString
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
   293
    }
fdf77e7be01a more robust pattern: look at longer matches first, added catch-all case;
wenzelm
parents: 27935
diff changeset
   294
  }
27924
8dd8b564faf5 tuned comments;
wenzelm
parents: 27923
diff changeset
   295
27918
85942d2036a0 reading symbol interpretation tables;
wenzelm
parents: 27905
diff changeset
   296
27923
7ebe9d38743a use scala.collection.jcl.HashMap, which seems to be more efficient;
wenzelm
parents: 27918
diff changeset
   297
43695
5130dfe1b7be simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents: 43675
diff changeset
   298
  /** symbol interpretation **/
27927
eb624bb54bc6 tuned Recoder;
wenzelm
parents: 27926
diff changeset
   299
67311
3869b2400e22 more completion templates;
wenzelm
parents: 67304
diff changeset
   300
  val ARGUMENT_CARTOUCHE = "cartouche"
3869b2400e22 more completion templates;
wenzelm
parents: 67304
diff changeset
   301
  val ARGUMENT_SPACE_CARTOUCHE = "space_cartouche"
3869b2400e22 more completion templates;
wenzelm
parents: 67304
diff changeset
   302
43695
5130dfe1b7be simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents: 43675
diff changeset
   303
  private lazy val symbols =
61959
wenzelm
parents: 61867
diff changeset
   304
  {
wenzelm
parents: 61867
diff changeset
   305
    val contents =
wenzelm
parents: 61867
diff changeset
   306
      for (path <- Path.split(Isabelle_System.getenv("ISABELLE_SYMBOLS")) if path.is_file)
wenzelm
parents: 61867
diff changeset
   307
        yield (File.read(path))
wenzelm
parents: 61867
diff changeset
   308
    new Interpretation(cat_lines(contents))
wenzelm
parents: 61867
diff changeset
   309
  }
43695
5130dfe1b7be simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents: 43675
diff changeset
   310
5130dfe1b7be simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents: 43675
diff changeset
   311
  private class Interpretation(symbols_spec: String)
29569
f3f529b5d8fb more general init of Symbol.Interpretation, independent of IsabelleSystem instance;
wenzelm
parents: 29174
diff changeset
   312
  {
31522
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   313
    /* read symbols */
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   314
50136
a96bd08258a2 support for symbol groups, retaining original order of declarations;
wenzelm
parents: 48922
diff changeset
   315
    private val No_Decl = new Regex("""(?xs) ^\s* (?: \#.* )? $ """)
a96bd08258a2 support for symbol groups, retaining original order of declarations;
wenzelm
parents: 48922
diff changeset
   316
    private val Key = new Regex("""(?xs) (.+): """)
31522
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   317
53316
c3e549e0d3c7 allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents: 53021
diff changeset
   318
    private def read_decl(decl: String): (Symbol, Properties.T) =
31522
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   319
    {
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   320
      def err() = error("Bad symbol declaration: " + decl)
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   321
53316
c3e549e0d3c7 allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents: 53021
diff changeset
   322
      def read_props(props: List[String]): Properties.T =
31522
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   323
      {
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   324
        props match {
53316
c3e549e0d3c7 allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents: 53021
diff changeset
   325
          case Nil => Nil
31522
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   326
          case _ :: Nil => err()
61174
74eddfef841e replacement character for spaces;
wenzelm
parents: 60215
diff changeset
   327
          case Key(x) :: y :: rest => (x -> y.replace('\u2423', ' ')) :: read_props(rest)
31522
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   328
          case _ => err()
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   329
        }
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   330
      }
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   331
      decl.split("\\s+").toList match {
53316
c3e549e0d3c7 allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents: 53021
diff changeset
   332
        case sym :: props if sym.length > 1 && !is_malformed(sym) =>
c3e549e0d3c7 allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents: 53021
diff changeset
   333
          (sym, read_props(props))
34193
d3358b909c40 some sanity checks for symbol interpretation;
wenzelm
parents: 34138
diff changeset
   334
        case _ => err()
31522
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   335
      }
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   336
    }
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   337
53316
c3e549e0d3c7 allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents: 53021
diff changeset
   338
    private val symbols: List[(Symbol, Properties.T)] =
c3e549e0d3c7 allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents: 53021
diff changeset
   339
      (((List.empty[(Symbol, Properties.T)], Set.empty[Symbol]) /:
50136
a96bd08258a2 support for symbol groups, retaining original order of declarations;
wenzelm
parents: 48922
diff changeset
   340
          split_lines(symbols_spec).reverse)
a96bd08258a2 support for symbol groups, retaining original order of declarations;
wenzelm
parents: 48922
diff changeset
   341
        { case (res, No_Decl()) => res
a96bd08258a2 support for symbol groups, retaining original order of declarations;
wenzelm
parents: 48922
diff changeset
   342
          case ((list, known), decl) =>
a96bd08258a2 support for symbol groups, retaining original order of declarations;
wenzelm
parents: 48922
diff changeset
   343
            val (sym, props) = read_decl(decl)
a96bd08258a2 support for symbol groups, retaining original order of declarations;
wenzelm
parents: 48922
diff changeset
   344
            if (known(sym)) (list, known)
a96bd08258a2 support for symbol groups, retaining original order of declarations;
wenzelm
parents: 48922
diff changeset
   345
            else ((sym, props) :: list, known + sym)
a96bd08258a2 support for symbol groups, retaining original order of declarations;
wenzelm
parents: 48922
diff changeset
   346
        })._1
31522
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   347
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   348
53400
673eb869e6ee expose basic Symbol.properties (uninterpreted);
wenzelm
parents: 53337
diff changeset
   349
    /* basic properties */
673eb869e6ee expose basic Symbol.properties (uninterpreted);
wenzelm
parents: 53337
diff changeset
   350
673eb869e6ee expose basic Symbol.properties (uninterpreted);
wenzelm
parents: 53337
diff changeset
   351
    val properties: Map[Symbol, Properties.T] = Map(symbols: _*)
31651
7d6a518b5a2b added names, abbrevs;
wenzelm
parents: 31548
diff changeset
   352
67311
3869b2400e22 more completion templates;
wenzelm
parents: 67304
diff changeset
   353
    val names: Map[Symbol, (String, String)] =
34134
d8d9df8407f6 added symbol classification;
wenzelm
parents: 34098
diff changeset
   354
    {
67311
3869b2400e22 more completion templates;
wenzelm
parents: 67304
diff changeset
   355
      val Name = new Regex("""\\<\^?([A-Za-z][A-Za-z0-9_']*)>""")
3869b2400e22 more completion templates;
wenzelm
parents: 67304
diff changeset
   356
      val Argument = new Properties.String("argument")
3869b2400e22 more completion templates;
wenzelm
parents: 67304
diff changeset
   357
      def argument(sym: Symbol, props: Properties.T): String =
3869b2400e22 more completion templates;
wenzelm
parents: 67304
diff changeset
   358
        props match {
3869b2400e22 more completion templates;
wenzelm
parents: 67304
diff changeset
   359
          case Argument(arg) =>
3869b2400e22 more completion templates;
wenzelm
parents: 67304
diff changeset
   360
            if (arg == ARGUMENT_CARTOUCHE || arg == ARGUMENT_SPACE_CARTOUCHE) arg
3869b2400e22 more completion templates;
wenzelm
parents: 67304
diff changeset
   361
            else error("Bad argument: " + quote(arg) + " for symbol " + quote(sym))
3869b2400e22 more completion templates;
wenzelm
parents: 67304
diff changeset
   362
          case _ => ""
3869b2400e22 more completion templates;
wenzelm
parents: 67304
diff changeset
   363
        }
3869b2400e22 more completion templates;
wenzelm
parents: 67304
diff changeset
   364
      Map((for ((sym @ Name(a), props) <- symbols) yield sym -> (a, argument(sym, props))): _*)
31651
7d6a518b5a2b added names, abbrevs;
wenzelm
parents: 31548
diff changeset
   365
    }
7d6a518b5a2b added names, abbrevs;
wenzelm
parents: 31548
diff changeset
   366
50136
a96bd08258a2 support for symbol groups, retaining original order of declarations;
wenzelm
parents: 48922
diff changeset
   367
    val groups: List[(String, List[Symbol])] =
71592
8313dca6dee9 misc tuning, following hint by IntelliJ;
wenzelm
parents: 70072
diff changeset
   368
      symbols.flatMap({ case (sym, props) =>
53316
c3e549e0d3c7 allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents: 53021
diff changeset
   369
        val gs = for (("group", g) <- props) yield g
c3e549e0d3c7 allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents: 53021
diff changeset
   370
        if (gs.isEmpty) List(sym -> "unsorted") else gs.map(sym -> _)
71592
8313dca6dee9 misc tuning, following hint by IntelliJ;
wenzelm
parents: 70072
diff changeset
   371
      }).groupBy(_._2).toList.map({ case (group, list) => (group, list.map(_._1)) })
50136
a96bd08258a2 support for symbol groups, retaining original order of declarations;
wenzelm
parents: 48922
diff changeset
   372
        .sortBy(_._1)
a96bd08258a2 support for symbol groups, retaining original order of declarations;
wenzelm
parents: 48922
diff changeset
   373
53316
c3e549e0d3c7 allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents: 53021
diff changeset
   374
    val abbrevs: Multi_Map[Symbol, String] =
c3e549e0d3c7 allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents: 53021
diff changeset
   375
      Multi_Map((
c3e549e0d3c7 allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents: 53021
diff changeset
   376
        for {
c3e549e0d3c7 allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents: 53021
diff changeset
   377
          (sym, props) <- symbols
c3e549e0d3c7 allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents: 53021
diff changeset
   378
          ("abbrev", a) <- props.reverse
60215
5fb4990dfc73 misc tuning, based on warnings by IntelliJ IDEA;
wenzelm
parents: 59107
diff changeset
   379
        } yield sym -> a): _*)
43488
39035276927c Symbol.is_ctrl: handle decoded version as well;
wenzelm
parents: 43487
diff changeset
   380
66051
70d3d0818d42 tuned signature;
wenzelm
parents: 66006
diff changeset
   381
    val codes: List[(Symbol, Int)] =
61376
93224745477f output HTML text according to Isabelle/Scala Symbol.Interpretation;
wenzelm
parents: 61174
diff changeset
   382
    {
93224745477f output HTML text according to Isabelle/Scala Symbol.Interpretation;
wenzelm
parents: 61174
diff changeset
   383
      val Code = new Properties.String("code")
93224745477f output HTML text according to Isabelle/Scala Symbol.Interpretation;
wenzelm
parents: 61174
diff changeset
   384
      for {
93224745477f output HTML text according to Isabelle/Scala Symbol.Interpretation;
wenzelm
parents: 61174
diff changeset
   385
        (sym, props) <- symbols
67311
3869b2400e22 more completion templates;
wenzelm
parents: 67304
diff changeset
   386
        code <-
61376
93224745477f output HTML text according to Isabelle/Scala Symbol.Interpretation;
wenzelm
parents: 61174
diff changeset
   387
          props match {
93224745477f output HTML text according to Isabelle/Scala Symbol.Interpretation;
wenzelm
parents: 61174
diff changeset
   388
            case Code(s) =>
67311
3869b2400e22 more completion templates;
wenzelm
parents: 67304
diff changeset
   389
              try { Some(Integer.decode(s).intValue) }
61376
93224745477f output HTML text according to Isabelle/Scala Symbol.Interpretation;
wenzelm
parents: 61174
diff changeset
   390
              catch { case _: NumberFormatException => error("Bad code for symbol " + sym) }
67311
3869b2400e22 more completion templates;
wenzelm
parents: 67304
diff changeset
   391
            case _ => None
61376
93224745477f output HTML text according to Isabelle/Scala Symbol.Interpretation;
wenzelm
parents: 61174
diff changeset
   392
          }
93224745477f output HTML text according to Isabelle/Scala Symbol.Interpretation;
wenzelm
parents: 61174
diff changeset
   393
      } yield {
93224745477f output HTML text according to Isabelle/Scala Symbol.Interpretation;
wenzelm
parents: 61174
diff changeset
   394
        if (code < 128) error("Illegal ASCII code for symbol " + sym)
93224745477f output HTML text according to Isabelle/Scala Symbol.Interpretation;
wenzelm
parents: 61174
diff changeset
   395
        else (sym, code)
93224745477f output HTML text according to Isabelle/Scala Symbol.Interpretation;
wenzelm
parents: 61174
diff changeset
   396
      }
93224745477f output HTML text according to Isabelle/Scala Symbol.Interpretation;
wenzelm
parents: 61174
diff changeset
   397
    }
93224745477f output HTML text according to Isabelle/Scala Symbol.Interpretation;
wenzelm
parents: 61174
diff changeset
   398
43488
39035276927c Symbol.is_ctrl: handle decoded version as well;
wenzelm
parents: 43487
diff changeset
   399
43490
5e6f76cacb93 more uniform treatment of recode_set/recode_map;
wenzelm
parents: 43489
diff changeset
   400
    /* recoding */
31522
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   401
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   402
    private val (decoder, encoder) =
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   403
    {
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   404
      val mapping =
61376
93224745477f output HTML text according to Isabelle/Scala Symbol.Interpretation;
wenzelm
parents: 61174
diff changeset
   405
        for ((sym, code) <- codes) yield (sym, new String(Character.toChars(code)))
93224745477f output HTML text according to Isabelle/Scala Symbol.Interpretation;
wenzelm
parents: 61174
diff changeset
   406
      (new Recoder(mapping), new Recoder(for ((x, y) <- mapping) yield (y, x)))
31522
0466cb17064f more native Scala style;
wenzelm
parents: 29569
diff changeset
   407
    }
27918
85942d2036a0 reading symbol interpretation tables;
wenzelm
parents: 27905
diff changeset
   408
34098
2b9cdf23c188 tuned signature;
wenzelm
parents: 34001
diff changeset
   409
    def decode(text: String): String = decoder.recode(text)
2b9cdf23c188 tuned signature;
wenzelm
parents: 34001
diff changeset
   410
    def encode(text: String): String = encoder.recode(text)
34134
d8d9df8407f6 added symbol classification;
wenzelm
parents: 34098
diff changeset
   411
43490
5e6f76cacb93 more uniform treatment of recode_set/recode_map;
wenzelm
parents: 43489
diff changeset
   412
    private def recode_set(elems: String*): Set[String] =
5e6f76cacb93 more uniform treatment of recode_set/recode_map;
wenzelm
parents: 43489
diff changeset
   413
    {
5e6f76cacb93 more uniform treatment of recode_set/recode_map;
wenzelm
parents: 43489
diff changeset
   414
      val content = elems.toList
5e6f76cacb93 more uniform treatment of recode_set/recode_map;
wenzelm
parents: 43489
diff changeset
   415
      Set((content ::: content.map(decode)): _*)
5e6f76cacb93 more uniform treatment of recode_set/recode_map;
wenzelm
parents: 43489
diff changeset
   416
    }
5e6f76cacb93 more uniform treatment of recode_set/recode_map;
wenzelm
parents: 43489
diff changeset
   417
5e6f76cacb93 more uniform treatment of recode_set/recode_map;
wenzelm
parents: 43489
diff changeset
   418
    private def recode_map[A](elems: (String, A)*): Map[String, A] =
5e6f76cacb93 more uniform treatment of recode_set/recode_map;
wenzelm
parents: 43489
diff changeset
   419
    {
5e6f76cacb93 more uniform treatment of recode_set/recode_map;
wenzelm
parents: 43489
diff changeset
   420
      val content = elems.toList
5e6f76cacb93 more uniform treatment of recode_set/recode_map;
wenzelm
parents: 43489
diff changeset
   421
      Map((content ::: content.map({ case (sym, a) => (decode(sym), a) })): _*)
5e6f76cacb93 more uniform treatment of recode_set/recode_map;
wenzelm
parents: 43489
diff changeset
   422
    }
5e6f76cacb93 more uniform treatment of recode_set/recode_map;
wenzelm
parents: 43489
diff changeset
   423
5e6f76cacb93 more uniform treatment of recode_set/recode_map;
wenzelm
parents: 43489
diff changeset
   424
5e6f76cacb93 more uniform treatment of recode_set/recode_map;
wenzelm
parents: 43489
diff changeset
   425
    /* user fonts */
5e6f76cacb93 more uniform treatment of recode_set/recode_map;
wenzelm
parents: 43489
diff changeset
   426
53316
c3e549e0d3c7 allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents: 53021
diff changeset
   427
    private val Font = new Properties.String("font")
43696
58bb7ca5c7a2 explicit indication of type Symbol.Symbol;
wenzelm
parents: 43695
diff changeset
   428
    val fonts: Map[Symbol, String] =
60215
5fb4990dfc73 misc tuning, based on warnings by IntelliJ IDEA;
wenzelm
parents: 59107
diff changeset
   429
      recode_map((for ((sym, Font(font)) <- symbols) yield sym -> font): _*)
43490
5e6f76cacb93 more uniform treatment of recode_set/recode_map;
wenzelm
parents: 43489
diff changeset
   430
5e6f76cacb93 more uniform treatment of recode_set/recode_map;
wenzelm
parents: 43489
diff changeset
   431
    val font_names: List[String] = Set(fonts.toList.map(_._2): _*).toList
71592
8313dca6dee9 misc tuning, following hint by IntelliJ;
wenzelm
parents: 70072
diff changeset
   432
    val font_index: Map[String, Int] = Map((font_names zip font_names.indices.toList): _*)
43490
5e6f76cacb93 more uniform treatment of recode_set/recode_map;
wenzelm
parents: 43489
diff changeset
   433
34134
d8d9df8407f6 added symbol classification;
wenzelm
parents: 34098
diff changeset
   434
d8d9df8407f6 added symbol classification;
wenzelm
parents: 34098
diff changeset
   435
    /* classification */
d8d9df8407f6 added symbol classification;
wenzelm
parents: 34098
diff changeset
   436
71816
97ccf48c2f0c misc tuning based on hints by IntelliJ IDEA;
wenzelm
parents: 71592
diff changeset
   437
    val letters: Set[String] = recode_set(
34134
d8d9df8407f6 added symbol classification;
wenzelm
parents: 34098
diff changeset
   438
      "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
d8d9df8407f6 added symbol classification;
wenzelm
parents: 34098
diff changeset
   439
      "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
d8d9df8407f6 added symbol classification;
wenzelm
parents: 34098
diff changeset
   440
      "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
d8d9df8407f6 added symbol classification;
wenzelm
parents: 34098
diff changeset
   441
      "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
d8d9df8407f6 added symbol classification;
wenzelm
parents: 34098
diff changeset
   442
d8d9df8407f6 added symbol classification;
wenzelm
parents: 34098
diff changeset
   443
      "\\<A>", "\\<B>", "\\<C>", "\\<D>", "\\<E>", "\\<F>", "\\<G>",
d8d9df8407f6 added symbol classification;
wenzelm
parents: 34098
diff changeset
   444
      "\\<H>", "\\<I>", "\\<J>", "\\<K>", "\\<L>", "\\<M>", "\\<N>",
d8d9df8407f6 added symbol classification;
wenzelm
parents: 34098
diff changeset
   445
      "\\<O>", "\\<P>", "\\<Q>", "\\<R>", "\\<S>", "\\<T>", "\\<U>",
d8d9df8407f6 added symbol classification;
wenzelm
parents: 34098
diff changeset
   446
      "\\<V>", "\\<W>", "\\<X>", "\\<Y>", "\\<Z>", "\\<a>", "\\<b>",
d8d9df8407f6 added symbol classification;
wenzelm
parents: 34098
diff changeset
   447
      "\\<c>", "\\<d>", "\\<e>", "\\<f>", "\\<g>", "\\<h>", "\\<i>",
d8d9df8407f6 added symbol classification;
wenzelm
parents: 34098
diff changeset
   448
      "\\<j>", "\\<k>", "\\<l>", "\\<m>", "\\<n>", "\\<o>", "\\<p>",
d8d9df8407f6 added symbol classification;
wenzelm
parents: 34098
diff changeset
   449
      "\\<q>", "\\<r>", "\\<s>", "\\<t>", "\\<u>", "\\<v>", "\\<w>",
d8d9df8407f6 added symbol classification;
wenzelm
parents: 34098
diff changeset
   450
      "\\<x>", "\\<y>", "\\<z>",
d8d9df8407f6 added symbol classification;
wenzelm
parents: 34098
diff changeset
   451
d8d9df8407f6 added symbol classification;
wenzelm
parents: 34098
diff changeset
   452
      "\\<AA>", "\\<BB>", "\\<CC>", "\\<DD>", "\\<EE>", "\\<FF>",
d8d9df8407f6 added symbol classification;
wenzelm
parents: 34098
diff changeset
   453
      "\\<GG>", "\\<HH>", "\\<II>", "\\<JJ>", "\\<KK>", "\\<LL>",
d8d9df8407f6 added symbol classification;
wenzelm
parents: 34098
diff changeset
   454
      "\\<MM>", "\\<NN>", "\\<OO>", "\\<PP>", "\\<QQ>", "\\<RR>",
d8d9df8407f6 added symbol classification;
wenzelm
parents: 34098
diff changeset
   455
      "\\<SS>", "\\<TT>", "\\<UU>", "\\<VV>", "\\<WW>", "\\<XX>",
d8d9df8407f6 added symbol classification;
wenzelm
parents: 34098
diff changeset
   456
      "\\<YY>", "\\<ZZ>", "\\<aa>", "\\<bb>", "\\<cc>", "\\<dd>",
d8d9df8407f6 added symbol classification;
wenzelm
parents: 34098
diff changeset
   457
      "\\<ee>", "\\<ff>", "\\<gg>", "\\<hh>", "\\<ii>", "\\<jj>",
d8d9df8407f6 added symbol classification;
wenzelm
parents: 34098
diff changeset
   458
      "\\<kk>", "\\<ll>", "\\<mm>", "\\<nn>", "\\<oo>", "\\<pp>",
d8d9df8407f6 added symbol classification;
wenzelm
parents: 34098
diff changeset
   459
      "\\<qq>", "\\<rr>", "\\<ss>", "\\<tt>", "\\<uu>", "\\<vv>",
d8d9df8407f6 added symbol classification;
wenzelm
parents: 34098
diff changeset
   460
      "\\<ww>", "\\<xx>", "\\<yy>", "\\<zz>",
d8d9df8407f6 added symbol classification;
wenzelm
parents: 34098
diff changeset
   461
d8d9df8407f6 added symbol classification;
wenzelm
parents: 34098
diff changeset
   462
      "\\<alpha>", "\\<beta>", "\\<gamma>", "\\<delta>", "\\<epsilon>",
d8d9df8407f6 added symbol classification;
wenzelm
parents: 34098
diff changeset
   463
      "\\<zeta>", "\\<eta>", "\\<theta>", "\\<iota>", "\\<kappa>",
d8d9df8407f6 added symbol classification;
wenzelm
parents: 34098
diff changeset
   464
      "\\<mu>", "\\<nu>", "\\<xi>", "\\<pi>", "\\<rho>", "\\<sigma>",
d8d9df8407f6 added symbol classification;
wenzelm
parents: 34098
diff changeset
   465
      "\\<tau>", "\\<upsilon>", "\\<phi>", "\\<chi>", "\\<psi>",
d8d9df8407f6 added symbol classification;
wenzelm
parents: 34098
diff changeset
   466
      "\\<omega>", "\\<Gamma>", "\\<Delta>", "\\<Theta>", "\\<Lambda>",
d8d9df8407f6 added symbol classification;
wenzelm
parents: 34098
diff changeset
   467
      "\\<Xi>", "\\<Pi>", "\\<Sigma>", "\\<Upsilon>", "\\<Phi>",
52616
3ac2878764f9 more robust identifier syntax: sub/superscript counts as modifier of LETDIG part instead of LETTER, both isub/isup and sub/sup are allowed;
wenzelm
parents: 52507
diff changeset
   468
      "\\<Psi>", "\\<Omega>")
34134
d8d9df8407f6 added symbol classification;
wenzelm
parents: 34098
diff changeset
   469
71816
97ccf48c2f0c misc tuning based on hints by IntelliJ IDEA;
wenzelm
parents: 71592
diff changeset
   470
    val blanks: Set[String] = recode_set(space, "\t", "\n", "\u000B", "\f", "\r", "\r\n")
34138
4008c2f5a46e refined some Symbol operations/signatures;
wenzelm
parents: 34137
diff changeset
   471
43695
5130dfe1b7be simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents: 43675
diff changeset
   472
    val sym_chars =
34138
4008c2f5a46e refined some Symbol operations/signatures;
wenzelm
parents: 34137
diff changeset
   473
      Set("!", "#", "$", "%", "&", "*", "+", "-", "/", "<", "=", ">", "?", "@", "^", "_", "|", "~")
34134
d8d9df8407f6 added symbol classification;
wenzelm
parents: 34098
diff changeset
   474
71816
97ccf48c2f0c misc tuning based on hints by IntelliJ IDEA;
wenzelm
parents: 71592
diff changeset
   475
    val symbolic: Set[String] = recode_set((for {(sym, _) <- symbols; if raw_symbolic(sym)} yield sym): _*)
44992
aa34d2d049ce refined Symbol.is_symbolic -- cover recoded versions as well;
wenzelm
parents: 44949
diff changeset
   476
43455
4b4b93672f15 some unicode chars for special control symbols;
wenzelm
parents: 43447
diff changeset
   477
63529
0f39f59317c1 completion templates for commands involving "begin ... end" blocks;
wenzelm
parents: 62529
diff changeset
   478
    /* misc symbols */
61579
634cd44bb1d3 symbolic syntax "\<comment> text";
wenzelm
parents: 61483
diff changeset
   479
63529
0f39f59317c1 completion templates for commands involving "begin ... end" blocks;
wenzelm
parents: 62529
diff changeset
   480
    val newline_decoded = decode(newline)
61579
634cd44bb1d3 symbolic syntax "\<comment> text";
wenzelm
parents: 61483
diff changeset
   481
    val comment_decoded = decode(comment)
67438
fdb7b995974d clarified modules;
wenzelm
parents: 67435
diff changeset
   482
    val cancel_decoded = decode(cancel)
fdb7b995974d clarified modules;
wenzelm
parents: 67435
diff changeset
   483
    val latex_decoded = decode(latex)
70072
def3ec9cdb7e document markers are formal comments, and may thus occur anywhere in the command-span;
wenzelm
parents: 70068
diff changeset
   484
    val marker_decoded = decode(marker)
55033
8e8243975860 support for nested text cartouches;
wenzelm
parents: 54734
diff changeset
   485
    val open_decoded = decode(open)
8e8243975860 support for nested text cartouches;
wenzelm
parents: 54734
diff changeset
   486
    val close_decoded = decode(close)
8e8243975860 support for nested text cartouches;
wenzelm
parents: 54734
diff changeset
   487
8e8243975860 support for nested text cartouches;
wenzelm
parents: 54734
diff changeset
   488
43488
39035276927c Symbol.is_ctrl: handle decoded version as well;
wenzelm
parents: 43487
diff changeset
   489
    /* control symbols */
39035276927c Symbol.is_ctrl: handle decoded version as well;
wenzelm
parents: 43487
diff changeset
   490
59107
48429ad6d0c8 tuned signature;
wenzelm
parents: 57840
diff changeset
   491
    val control_decoded: Set[Symbol] =
43488
39035276927c Symbol.is_ctrl: handle decoded version as well;
wenzelm
parents: 43487
diff changeset
   492
      Set((for ((sym, _) <- symbols if sym.startsWith("\\<^")) yield decode(sym)): _*)
39035276927c Symbol.is_ctrl: handle decoded version as well;
wenzelm
parents: 43487
diff changeset
   493
62103
wenzelm
parents: 61959
diff changeset
   494
    val sub_decoded = decode(sub)
wenzelm
parents: 61959
diff changeset
   495
    val sup_decoded = decode(sup)
wenzelm
parents: 61959
diff changeset
   496
    val bold_decoded = decode(bold)
62104
fb73c0d7bb37 clarified symbol insertion, depending on buffer encoding;
wenzelm
parents: 62103
diff changeset
   497
    val emph_decoded = decode(emph)
65999
e3dc9ea67a62 output control symbols like ML version, with optionally hidden source;
wenzelm
parents: 65521
diff changeset
   498
    val bsub_decoded = decode(bsub)
e3dc9ea67a62 output control symbols like ML version, with optionally hidden source;
wenzelm
parents: 65521
diff changeset
   499
    val esub_decoded = decode(esub)
e3dc9ea67a62 output control symbols like ML version, with optionally hidden source;
wenzelm
parents: 65521
diff changeset
   500
    val bsup_decoded = decode(bsup)
e3dc9ea67a62 output control symbols like ML version, with optionally hidden source;
wenzelm
parents: 65521
diff changeset
   501
    val esup_decoded = decode(esup)
27918
85942d2036a0 reading symbol interpretation tables;
wenzelm
parents: 27905
diff changeset
   502
  }
43695
5130dfe1b7be simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents: 43675
diff changeset
   503
5130dfe1b7be simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents: 43675
diff changeset
   504
5130dfe1b7be simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents: 43675
diff changeset
   505
  /* tables */
5130dfe1b7be simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents: 43675
diff changeset
   506
53400
673eb869e6ee expose basic Symbol.properties (uninterpreted);
wenzelm
parents: 53337
diff changeset
   507
  def properties: Map[Symbol, Properties.T] = symbols.properties
67311
3869b2400e22 more completion templates;
wenzelm
parents: 67304
diff changeset
   508
  def names: Map[Symbol, (String, String)] = symbols.names
50136
a96bd08258a2 support for symbol groups, retaining original order of declarations;
wenzelm
parents: 48922
diff changeset
   509
  def groups: List[(String, List[Symbol])] = symbols.groups
53316
c3e549e0d3c7 allow multiple symbol properties, notably groups and abbrevs;
wenzelm
parents: 53021
diff changeset
   510
  def abbrevs: Multi_Map[Symbol, String] = symbols.abbrevs
66051
70d3d0818d42 tuned signature;
wenzelm
parents: 66006
diff changeset
   511
  def codes: List[(Symbol, Int)] = symbols.codes
67389
7e21d19e7ad7 show only symbols with code;
wenzelm
parents: 67311
diff changeset
   512
  def groups_code: List[(String, List[Symbol])] =
7e21d19e7ad7 show only symbols with code;
wenzelm
parents: 67311
diff changeset
   513
  {
7e21d19e7ad7 show only symbols with code;
wenzelm
parents: 67311
diff changeset
   514
    val has_code = codes.iterator.map(_._1).toSet
7e21d19e7ad7 show only symbols with code;
wenzelm
parents: 67311
diff changeset
   515
    groups.flatMap({ case (group, symbols) =>
7e21d19e7ad7 show only symbols with code;
wenzelm
parents: 67311
diff changeset
   516
      val symbols1 = symbols.filter(has_code)
7e21d19e7ad7 show only symbols with code;
wenzelm
parents: 67311
diff changeset
   517
      if (symbols1.isEmpty) None else Some((group, symbols1))
7e21d19e7ad7 show only symbols with code;
wenzelm
parents: 67311
diff changeset
   518
    })
7e21d19e7ad7 show only symbols with code;
wenzelm
parents: 67311
diff changeset
   519
  }
43695
5130dfe1b7be simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents: 43675
diff changeset
   520
67304
3cf05d7cf174 more robust treatment of conflicts with existing Unicode text;
wenzelm
parents: 67256
diff changeset
   521
  lazy val is_code: Int => Boolean = codes.map(_._2).toSet
43695
5130dfe1b7be simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents: 43675
diff changeset
   522
  def decode(text: String): String = symbols.decode(text)
5130dfe1b7be simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents: 43675
diff changeset
   523
  def encode(text: String): String = symbols.encode(text)
5130dfe1b7be simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents: 43675
diff changeset
   524
73270
72a8fdfa185d support more direct hash-consing via XML.Cache;
wenzelm
parents: 73103
diff changeset
   525
  def decode_yxml(text: String, cache: XML.Cache = XML.Cache.none): XML.Body =
72a8fdfa185d support more direct hash-consing via XML.Cache;
wenzelm
parents: 73103
diff changeset
   526
    YXML.parse_body(decode(text), cache = cache)
72a8fdfa185d support more direct hash-consing via XML.Cache;
wenzelm
parents: 73103
diff changeset
   527
72a8fdfa185d support more direct hash-consing via XML.Cache;
wenzelm
parents: 73103
diff changeset
   528
  def decode_yxml_failsafe(text: String, cache: XML.Cache = XML.Cache.none): XML.Body =
72a8fdfa185d support more direct hash-consing via XML.Cache;
wenzelm
parents: 73103
diff changeset
   529
    YXML.parse_body_failsafe(decode(text), cache = cache)
72a8fdfa185d support more direct hash-consing via XML.Cache;
wenzelm
parents: 73103
diff changeset
   530
65344
b99283eed13c clarified YXML vs. symbol encoding: operate on whole message;
wenzelm
parents: 65335
diff changeset
   531
  def encode_yxml(body: XML.Body): String = encode(YXML.string_of_body(body))
53337
b3817a0e3211 sort items according to persistent history of frequency of use;
wenzelm
parents: 53316
diff changeset
   532
50291
674893679352 prefer Symbol.decode_strict in batch mode, to avoid files with spurious Unicode symbols that clash with Isabelle symbol interpretation;
wenzelm
parents: 50238
diff changeset
   533
  def decode_strict(text: String): String =
674893679352 prefer Symbol.decode_strict in batch mode, to avoid files with spurious Unicode symbols that clash with Isabelle symbol interpretation;
wenzelm
parents: 50238
diff changeset
   534
  {
674893679352 prefer Symbol.decode_strict in batch mode, to avoid files with spurious Unicode symbols that clash with Isabelle symbol interpretation;
wenzelm
parents: 50238
diff changeset
   535
    val decoded = decode(text)
674893679352 prefer Symbol.decode_strict in batch mode, to avoid files with spurious Unicode symbols that clash with Isabelle symbol interpretation;
wenzelm
parents: 50238
diff changeset
   536
    if (encode(decoded) == text) decoded
674893679352 prefer Symbol.decode_strict in batch mode, to avoid files with spurious Unicode symbols that clash with Isabelle symbol interpretation;
wenzelm
parents: 50238
diff changeset
   537
    else {
674893679352 prefer Symbol.decode_strict in batch mode, to avoid files with spurious Unicode symbols that clash with Isabelle symbol interpretation;
wenzelm
parents: 50238
diff changeset
   538
      val bad = new mutable.ListBuffer[Symbol]
674893679352 prefer Symbol.decode_strict in batch mode, to avoid files with spurious Unicode symbols that clash with Isabelle symbol interpretation;
wenzelm
parents: 50238
diff changeset
   539
      for (s <- iterator(text) if encode(decode(s)) != s && !bad.contains(s))
674893679352 prefer Symbol.decode_strict in batch mode, to avoid files with spurious Unicode symbols that clash with Isabelle symbol interpretation;
wenzelm
parents: 50238
diff changeset
   540
        bad += s
674893679352 prefer Symbol.decode_strict in batch mode, to avoid files with spurious Unicode symbols that clash with Isabelle symbol interpretation;
wenzelm
parents: 50238
diff changeset
   541
      error("Bad Unicode symbols in text: " + commas_quote(bad))
674893679352 prefer Symbol.decode_strict in batch mode, to avoid files with spurious Unicode symbols that clash with Isabelle symbol interpretation;
wenzelm
parents: 50238
diff changeset
   542
    }
674893679352 prefer Symbol.decode_strict in batch mode, to avoid files with spurious Unicode symbols that clash with Isabelle symbol interpretation;
wenzelm
parents: 50238
diff changeset
   543
  }
674893679352 prefer Symbol.decode_strict in batch mode, to avoid files with spurious Unicode symbols that clash with Isabelle symbol interpretation;
wenzelm
parents: 50238
diff changeset
   544
73103
1d21b4c8023d tuned signature;
wenzelm
parents: 72982
diff changeset
   545
  def output(unicode_symbols: Boolean, text: String): String =
1d21b4c8023d tuned signature;
wenzelm
parents: 72982
diff changeset
   546
    if (unicode_symbols) Symbol.decode(text) else Symbol.encode(text)
1d21b4c8023d tuned signature;
wenzelm
parents: 72982
diff changeset
   547
43696
58bb7ca5c7a2 explicit indication of type Symbol.Symbol;
wenzelm
parents: 43695
diff changeset
   548
  def fonts: Map[Symbol, String] = symbols.fonts
43695
5130dfe1b7be simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents: 43675
diff changeset
   549
  def font_names: List[String] = symbols.font_names
5130dfe1b7be simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents: 43675
diff changeset
   550
  def font_index: Map[String, Int] = symbols.font_index
43696
58bb7ca5c7a2 explicit indication of type Symbol.Symbol;
wenzelm
parents: 43695
diff changeset
   551
  def lookup_font(sym: Symbol): Option[Int] = symbols.fonts.get(sym).map(font_index(_))
43695
5130dfe1b7be simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents: 43675
diff changeset
   552
5130dfe1b7be simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents: 43675
diff changeset
   553
5130dfe1b7be simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents: 43675
diff changeset
   554
  /* classification */
5130dfe1b7be simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents: 43675
diff changeset
   555
43696
58bb7ca5c7a2 explicit indication of type Symbol.Symbol;
wenzelm
parents: 43695
diff changeset
   556
  def is_letter(sym: Symbol): Boolean = symbols.letters.contains(sym)
58bb7ca5c7a2 explicit indication of type Symbol.Symbol;
wenzelm
parents: 43695
diff changeset
   557
  def is_digit(sym: Symbol): Boolean = sym.length == 1 && '0' <= sym(0) && sym(0) <= '9'
58bb7ca5c7a2 explicit indication of type Symbol.Symbol;
wenzelm
parents: 43695
diff changeset
   558
  def is_quasi(sym: Symbol): Boolean = sym == "_" || sym == "'"
58bb7ca5c7a2 explicit indication of type Symbol.Symbol;
wenzelm
parents: 43695
diff changeset
   559
  def is_letdig(sym: Symbol): Boolean = is_letter(sym) || is_digit(sym) || is_quasi(sym)
58bb7ca5c7a2 explicit indication of type Symbol.Symbol;
wenzelm
parents: 43695
diff changeset
   560
  def is_blank(sym: Symbol): Boolean = symbols.blanks.contains(sym)
44992
aa34d2d049ce refined Symbol.is_symbolic -- cover recoded versions as well;
wenzelm
parents: 44949
diff changeset
   561
55033
8e8243975860 support for nested text cartouches;
wenzelm
parents: 54734
diff changeset
   562
67438
fdb7b995974d clarified modules;
wenzelm
parents: 67435
diff changeset
   563
  /* symbolic newline */
63529
0f39f59317c1 completion templates for commands involving "begin ... end" blocks;
wenzelm
parents: 62529
diff changeset
   564
0f39f59317c1 completion templates for commands involving "begin ... end" blocks;
wenzelm
parents: 62529
diff changeset
   565
  val newline: Symbol = "\\<newline>"
0f39f59317c1 completion templates for commands involving "begin ... end" blocks;
wenzelm
parents: 62529
diff changeset
   566
  def newline_decoded: Symbol = symbols.newline_decoded
0f39f59317c1 completion templates for commands involving "begin ... end" blocks;
wenzelm
parents: 62529
diff changeset
   567
0f39f59317c1 completion templates for commands involving "begin ... end" blocks;
wenzelm
parents: 62529
diff changeset
   568
  def print_newlines(str: String): String =
0f39f59317c1 completion templates for commands involving "begin ... end" blocks;
wenzelm
parents: 62529
diff changeset
   569
    if (str.contains('\n'))
0f39f59317c1 completion templates for commands involving "begin ... end" blocks;
wenzelm
parents: 62529
diff changeset
   570
      (for (s <- iterator(str)) yield { if (s == "\n") newline_decoded else s }).mkString
0f39f59317c1 completion templates for commands involving "begin ... end" blocks;
wenzelm
parents: 62529
diff changeset
   571
    else str
61579
634cd44bb1d3 symbolic syntax "\<comment> text";
wenzelm
parents: 61483
diff changeset
   572
67438
fdb7b995974d clarified modules;
wenzelm
parents: 67435
diff changeset
   573
fdb7b995974d clarified modules;
wenzelm
parents: 67435
diff changeset
   574
  /* formal comments */
fdb7b995974d clarified modules;
wenzelm
parents: 67435
diff changeset
   575
61579
634cd44bb1d3 symbolic syntax "\<comment> text";
wenzelm
parents: 61483
diff changeset
   576
  val comment: Symbol = "\\<comment>"
67449
1caeb087d957 tuned signature;
wenzelm
parents: 67438
diff changeset
   577
  val cancel: Symbol = "\\<^cancel>"
1caeb087d957 tuned signature;
wenzelm
parents: 67438
diff changeset
   578
  val latex: Symbol = "\\<^latex>"
70072
def3ec9cdb7e document markers are formal comments, and may thus occur anywhere in the command-span;
wenzelm
parents: 70068
diff changeset
   579
  val marker: Symbol = "\\<^marker>"
67449
1caeb087d957 tuned signature;
wenzelm
parents: 67438
diff changeset
   580
61579
634cd44bb1d3 symbolic syntax "\<comment> text";
wenzelm
parents: 61483
diff changeset
   581
  def comment_decoded: Symbol = symbols.comment_decoded
67438
fdb7b995974d clarified modules;
wenzelm
parents: 67435
diff changeset
   582
  def cancel_decoded: Symbol = symbols.cancel_decoded
fdb7b995974d clarified modules;
wenzelm
parents: 67435
diff changeset
   583
  def latex_decoded: Symbol = symbols.latex_decoded
70072
def3ec9cdb7e document markers are formal comments, and may thus occur anywhere in the command-span;
wenzelm
parents: 70068
diff changeset
   584
  def marker_decoded: Symbol = symbols.marker_decoded
61579
634cd44bb1d3 symbolic syntax "\<comment> text";
wenzelm
parents: 61483
diff changeset
   585
634cd44bb1d3 symbolic syntax "\<comment> text";
wenzelm
parents: 61483
diff changeset
   586
55033
8e8243975860 support for nested text cartouches;
wenzelm
parents: 54734
diff changeset
   587
  /* cartouches */
8e8243975860 support for nested text cartouches;
wenzelm
parents: 54734
diff changeset
   588
61579
634cd44bb1d3 symbolic syntax "\<comment> text";
wenzelm
parents: 61483
diff changeset
   589
  val open: Symbol = "\\<open>"
634cd44bb1d3 symbolic syntax "\<comment> text";
wenzelm
parents: 61483
diff changeset
   590
  val close: Symbol = "\\<close>"
55033
8e8243975860 support for nested text cartouches;
wenzelm
parents: 54734
diff changeset
   591
8e8243975860 support for nested text cartouches;
wenzelm
parents: 54734
diff changeset
   592
  def open_decoded: Symbol = symbols.open_decoded
8e8243975860 support for nested text cartouches;
wenzelm
parents: 54734
diff changeset
   593
  def close_decoded: Symbol = symbols.close_decoded
8e8243975860 support for nested text cartouches;
wenzelm
parents: 54734
diff changeset
   594
8e8243975860 support for nested text cartouches;
wenzelm
parents: 54734
diff changeset
   595
  def is_open(sym: Symbol): Boolean = sym == open_decoded || sym == open
8e8243975860 support for nested text cartouches;
wenzelm
parents: 54734
diff changeset
   596
  def is_close(sym: Symbol): Boolean = sym == close_decoded || sym == close
8e8243975860 support for nested text cartouches;
wenzelm
parents: 54734
diff changeset
   597
67131
85d10959c2e4 tuned signature;
wenzelm
parents: 67127
diff changeset
   598
  def cartouche(s: String): String = open + s + close
85d10959c2e4 tuned signature;
wenzelm
parents: 67127
diff changeset
   599
  def cartouche_decoded(s: String): String = open_decoded + s + close_decoded
85d10959c2e4 tuned signature;
wenzelm
parents: 67127
diff changeset
   600
55033
8e8243975860 support for nested text cartouches;
wenzelm
parents: 54734
diff changeset
   601
8e8243975860 support for nested text cartouches;
wenzelm
parents: 54734
diff changeset
   602
  /* symbols for symbolic identifiers */
44992
aa34d2d049ce refined Symbol.is_symbolic -- cover recoded versions as well;
wenzelm
parents: 44949
diff changeset
   603
aa34d2d049ce refined Symbol.is_symbolic -- cover recoded versions as well;
wenzelm
parents: 44949
diff changeset
   604
  private def raw_symbolic(sym: Symbol): Boolean =
43695
5130dfe1b7be simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents: 43675
diff changeset
   605
    sym.startsWith("\\<") && sym.endsWith(">") && !sym.startsWith("\\<^")
5130dfe1b7be simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents: 43675
diff changeset
   606
55033
8e8243975860 support for nested text cartouches;
wenzelm
parents: 54734
diff changeset
   607
  def is_symbolic(sym: Symbol): Boolean =
8e8243975860 support for nested text cartouches;
wenzelm
parents: 54734
diff changeset
   608
    !is_open(sym) && !is_close(sym) && (raw_symbolic(sym) || symbols.symbolic.contains(sym))
8e8243975860 support for nested text cartouches;
wenzelm
parents: 54734
diff changeset
   609
8e8243975860 support for nested text cartouches;
wenzelm
parents: 54734
diff changeset
   610
  def is_symbolic_char(sym: Symbol): Boolean = symbols.sym_chars.contains(sym)
8e8243975860 support for nested text cartouches;
wenzelm
parents: 54734
diff changeset
   611
43695
5130dfe1b7be simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents: 43675
diff changeset
   612
5130dfe1b7be simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents: 43675
diff changeset
   613
  /* control symbols */
5130dfe1b7be simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents: 43675
diff changeset
   614
67127
cf111622c9f8 font style for literal control symbols, notably for antiquotations;
wenzelm
parents: 66919
diff changeset
   615
  val control_prefix = "\\<^"
cf111622c9f8 font style for literal control symbols, notably for antiquotations;
wenzelm
parents: 66919
diff changeset
   616
  val control_suffix = ">"
cf111622c9f8 font style for literal control symbols, notably for antiquotations;
wenzelm
parents: 66919
diff changeset
   617
67256
f1f983484878 HTML rendering of \<^control> as in Isabelle/jEdit;
wenzelm
parents: 67131
diff changeset
   618
  def control_name(sym: Symbol): Option[String] =
f1f983484878 HTML rendering of \<^control> as in Isabelle/jEdit;
wenzelm
parents: 67131
diff changeset
   619
    if (is_control_encoded(sym))
f1f983484878 HTML rendering of \<^control> as in Isabelle/jEdit;
wenzelm
parents: 67131
diff changeset
   620
      Some(sym.substring(control_prefix.length, sym.length - control_suffix.length))
f1f983484878 HTML rendering of \<^control> as in Isabelle/jEdit;
wenzelm
parents: 67131
diff changeset
   621
    else None
f1f983484878 HTML rendering of \<^control> as in Isabelle/jEdit;
wenzelm
parents: 67131
diff changeset
   622
67127
cf111622c9f8 font style for literal control symbols, notably for antiquotations;
wenzelm
parents: 66919
diff changeset
   623
  def is_control_encoded(sym: Symbol): Boolean =
cf111622c9f8 font style for literal control symbols, notably for antiquotations;
wenzelm
parents: 66919
diff changeset
   624
    sym.startsWith(control_prefix) && sym.endsWith(control_suffix)
cf111622c9f8 font style for literal control symbols, notably for antiquotations;
wenzelm
parents: 66919
diff changeset
   625
59107
48429ad6d0c8 tuned signature;
wenzelm
parents: 57840
diff changeset
   626
  def is_control(sym: Symbol): Boolean =
67127
cf111622c9f8 font style for literal control symbols, notably for antiquotations;
wenzelm
parents: 66919
diff changeset
   627
    is_control_encoded(sym) || symbols.control_decoded.contains(sym)
43695
5130dfe1b7be simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents: 43675
diff changeset
   628
43696
58bb7ca5c7a2 explicit indication of type Symbol.Symbol;
wenzelm
parents: 43695
diff changeset
   629
  def is_controllable(sym: Symbol): Boolean =
66006
cec184536dfd uniform notion of Symbol.is_controllable (see also 265d9300d523);
wenzelm
parents: 65999
diff changeset
   630
    !is_blank(sym) && !is_control(sym) && !is_open(sym) && !is_close(sym) &&
cec184536dfd uniform notion of Symbol.is_controllable (see also 265d9300d523);
wenzelm
parents: 65999
diff changeset
   631
    !is_malformed(sym) && sym != "\""
43695
5130dfe1b7be simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents: 43675
diff changeset
   632
73454
e53f4c5927a1 tuned signature: more types;
wenzelm
parents: 73364
diff changeset
   633
  val sub: Symbol = "\\<^sub>"
e53f4c5927a1 tuned signature: more types;
wenzelm
parents: 73364
diff changeset
   634
  val sup: Symbol = "\\<^sup>"
e53f4c5927a1 tuned signature: more types;
wenzelm
parents: 73364
diff changeset
   635
  val bold: Symbol = "\\<^bold>"
e53f4c5927a1 tuned signature: more types;
wenzelm
parents: 73364
diff changeset
   636
  val emph: Symbol = "\\<^emph>"
e53f4c5927a1 tuned signature: more types;
wenzelm
parents: 73364
diff changeset
   637
  val bsub: Symbol = "\\<^bsub>"
e53f4c5927a1 tuned signature: more types;
wenzelm
parents: 73364
diff changeset
   638
  val esub: Symbol = "\\<^esub>"
e53f4c5927a1 tuned signature: more types;
wenzelm
parents: 73364
diff changeset
   639
  val bsup: Symbol = "\\<^bsup>"
e53f4c5927a1 tuned signature: more types;
wenzelm
parents: 73364
diff changeset
   640
  val esup: Symbol = "\\<^esup>"
62103
wenzelm
parents: 61959
diff changeset
   641
44238
36120feb70ed some convenience actions/shortcuts for control symbols;
wenzelm
parents: 44181
diff changeset
   642
  def sub_decoded: Symbol = symbols.sub_decoded
36120feb70ed some convenience actions/shortcuts for control symbols;
wenzelm
parents: 44181
diff changeset
   643
  def sup_decoded: Symbol = symbols.sup_decoded
62103
wenzelm
parents: 61959
diff changeset
   644
  def bold_decoded: Symbol = symbols.bold_decoded
wenzelm
parents: 61959
diff changeset
   645
  def emph_decoded: Symbol = symbols.emph_decoded
44238
36120feb70ed some convenience actions/shortcuts for control symbols;
wenzelm
parents: 44181
diff changeset
   646
  def bsub_decoded: Symbol = symbols.bsub_decoded
36120feb70ed some convenience actions/shortcuts for control symbols;
wenzelm
parents: 44181
diff changeset
   647
  def esub_decoded: Symbol = symbols.esub_decoded
36120feb70ed some convenience actions/shortcuts for control symbols;
wenzelm
parents: 44181
diff changeset
   648
  def bsup_decoded: Symbol = symbols.bsup_decoded
36120feb70ed some convenience actions/shortcuts for control symbols;
wenzelm
parents: 44181
diff changeset
   649
  def esup_decoded: Symbol = symbols.esup_decoded
71864
2acdbb6ee521 pretty formatting as in Isabelle/ML;
wenzelm
parents: 71816
diff changeset
   650
2acdbb6ee521 pretty formatting as in Isabelle/ML;
wenzelm
parents: 71816
diff changeset
   651
2acdbb6ee521 pretty formatting as in Isabelle/ML;
wenzelm
parents: 71816
diff changeset
   652
  /* metric */
2acdbb6ee521 pretty formatting as in Isabelle/ML;
wenzelm
parents: 71816
diff changeset
   653
2acdbb6ee521 pretty formatting as in Isabelle/ML;
wenzelm
parents: 71816
diff changeset
   654
  def is_printable(sym: Symbol): Boolean =
2acdbb6ee521 pretty formatting as in Isabelle/ML;
wenzelm
parents: 71816
diff changeset
   655
    if (is_ascii(sym)) is_ascii_printable(sym(0))
2acdbb6ee521 pretty formatting as in Isabelle/ML;
wenzelm
parents: 71816
diff changeset
   656
    else !is_control(sym)
2acdbb6ee521 pretty formatting as in Isabelle/ML;
wenzelm
parents: 71816
diff changeset
   657
2acdbb6ee521 pretty formatting as in Isabelle/ML;
wenzelm
parents: 71816
diff changeset
   658
  object Metric extends Pretty.Metric
2acdbb6ee521 pretty formatting as in Isabelle/ML;
wenzelm
parents: 71816
diff changeset
   659
  {
2acdbb6ee521 pretty formatting as in Isabelle/ML;
wenzelm
parents: 71816
diff changeset
   660
    val unit = 1.0
2acdbb6ee521 pretty formatting as in Isabelle/ML;
wenzelm
parents: 71816
diff changeset
   661
    def apply(str: String): Double =
2acdbb6ee521 pretty formatting as in Isabelle/ML;
wenzelm
parents: 71816
diff changeset
   662
      (for (s <- iterator(str)) yield {
2acdbb6ee521 pretty formatting as in Isabelle/ML;
wenzelm
parents: 71816
diff changeset
   663
        val sym = encode(s)
2acdbb6ee521 pretty formatting as in Isabelle/ML;
wenzelm
parents: 71816
diff changeset
   664
        if (sym.startsWith("\\<long") || sym.startsWith("\\<Long")) 2
2acdbb6ee521 pretty formatting as in Isabelle/ML;
wenzelm
parents: 71816
diff changeset
   665
        else if (is_printable(sym)) 1
2acdbb6ee521 pretty formatting as in Isabelle/ML;
wenzelm
parents: 71816
diff changeset
   666
        else 0
2acdbb6ee521 pretty formatting as in Isabelle/ML;
wenzelm
parents: 71816
diff changeset
   667
      }).sum
2acdbb6ee521 pretty formatting as in Isabelle/ML;
wenzelm
parents: 71816
diff changeset
   668
  }
27901
28083e9f8d1d Basic support for Isabelle symbols.
wenzelm
parents:
diff changeset
   669
}