src/Pure/library.scala
author wenzelm
Tue, 20 Sep 2016 22:31:50 +0200
changeset 63926 70973a1b4ec0
parent 63867 fb46c031c841
child 64002 08f89f0e8a62
permissions -rw-r--r--
tuned -- fewer warnings;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
34136
3dcb46ae6185 added basic library -- Scala version;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/library.scala
45673
cd41e3903fbf separate compilation of PIDE vs. Pure sources, which enables independent Scala library;
wenzelm
parents: 45667
diff changeset
     2
    Module:     PIDE
34136
3dcb46ae6185 added basic library -- Scala version;
wenzelm
parents:
diff changeset
     3
    Author:     Makarius
3dcb46ae6185 added basic library -- Scala version;
wenzelm
parents:
diff changeset
     4
3dcb46ae6185 added basic library -- Scala version;
wenzelm
parents:
diff changeset
     5
Basic library.
3dcb46ae6185 added basic library -- Scala version;
wenzelm
parents:
diff changeset
     6
*/
3dcb46ae6185 added basic library -- Scala version;
wenzelm
parents:
diff changeset
     7
3dcb46ae6185 added basic library -- Scala version;
wenzelm
parents:
diff changeset
     8
package isabelle
3dcb46ae6185 added basic library -- Scala version;
wenzelm
parents:
diff changeset
     9
38258
dd7dcb9b2637 added Library.thread_actor -- thread as actor;
wenzelm
parents: 38232
diff changeset
    10
63781
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63773
diff changeset
    11
import scala.annotation.tailrec
51981
a8ffd3692f57 more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents: 51616
diff changeset
    12
import scala.collection.mutable
59224
wenzelm
parents: 58592
diff changeset
    13
import scala.util.matching.Regex
51981
a8ffd3692f57 more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents: 51616
diff changeset
    14
37018
39f4cce5a22c added somewhat generic zoom box;
wenzelm
parents: 36791
diff changeset
    15
34136
3dcb46ae6185 added basic library -- Scala version;
wenzelm
parents:
diff changeset
    16
object Library
3dcb46ae6185 added basic library -- Scala version;
wenzelm
parents:
diff changeset
    17
{
63789
af28929ff219 support resource management;
wenzelm
parents: 63781
diff changeset
    18
  /* resource management */
af28929ff219 support resource management;
wenzelm
parents: 63781
diff changeset
    19
af28929ff219 support resource management;
wenzelm
parents: 63781
diff changeset
    20
  def using[A <: { def close() }, B](x: A)(f: A => B): B =
af28929ff219 support resource management;
wenzelm
parents: 63781
diff changeset
    21
  {
63926
70973a1b4ec0 tuned -- fewer warnings;
wenzelm
parents: 63867
diff changeset
    22
    import scala.language.reflectiveCalls
70973a1b4ec0 tuned -- fewer warnings;
wenzelm
parents: 63867
diff changeset
    23
63789
af28929ff219 support resource management;
wenzelm
parents: 63781
diff changeset
    24
    try { f(x) }
af28929ff219 support resource management;
wenzelm
parents: 63781
diff changeset
    25
    finally { if (x != null) x.close() }
af28929ff219 support resource management;
wenzelm
parents: 63781
diff changeset
    26
  }
af28929ff219 support resource management;
wenzelm
parents: 63781
diff changeset
    27
af28929ff219 support resource management;
wenzelm
parents: 63781
diff changeset
    28
57909
0fb331032f02 more compact representation of special string values;
wenzelm
parents: 57831
diff changeset
    29
  /* integers */
0fb331032f02 more compact representation of special string values;
wenzelm
parents: 57831
diff changeset
    30
0fb331032f02 more compact representation of special string values;
wenzelm
parents: 57831
diff changeset
    31
  private val small_int = 10000
0fb331032f02 more compact representation of special string values;
wenzelm
parents: 57831
diff changeset
    32
  private lazy val small_int_table =
0fb331032f02 more compact representation of special string values;
wenzelm
parents: 57831
diff changeset
    33
  {
0fb331032f02 more compact representation of special string values;
wenzelm
parents: 57831
diff changeset
    34
    val array = new Array[String](small_int)
0fb331032f02 more compact representation of special string values;
wenzelm
parents: 57831
diff changeset
    35
    for (i <- 0 until small_int) array(i) = i.toString
0fb331032f02 more compact representation of special string values;
wenzelm
parents: 57831
diff changeset
    36
    array
0fb331032f02 more compact representation of special string values;
wenzelm
parents: 57831
diff changeset
    37
  }
0fb331032f02 more compact representation of special string values;
wenzelm
parents: 57831
diff changeset
    38
0fb331032f02 more compact representation of special string values;
wenzelm
parents: 57831
diff changeset
    39
  def is_small_int(s: String): Boolean =
0fb331032f02 more compact representation of special string values;
wenzelm
parents: 57831
diff changeset
    40
  {
0fb331032f02 more compact representation of special string values;
wenzelm
parents: 57831
diff changeset
    41
    val len = s.length
0fb331032f02 more compact representation of special string values;
wenzelm
parents: 57831
diff changeset
    42
    1 <= len && len <= 4 &&
0fb331032f02 more compact representation of special string values;
wenzelm
parents: 57831
diff changeset
    43
    s.forall(c => '0' <= c && c <= '9') &&
0fb331032f02 more compact representation of special string values;
wenzelm
parents: 57831
diff changeset
    44
    (len == 1 || s(0) != '0')
0fb331032f02 more compact representation of special string values;
wenzelm
parents: 57831
diff changeset
    45
  }
0fb331032f02 more compact representation of special string values;
wenzelm
parents: 57831
diff changeset
    46
0fb331032f02 more compact representation of special string values;
wenzelm
parents: 57831
diff changeset
    47
  def signed_string_of_long(i: Long): String =
0fb331032f02 more compact representation of special string values;
wenzelm
parents: 57831
diff changeset
    48
    if (0 <= i && i < small_int) small_int_table(i.toInt)
0fb331032f02 more compact representation of special string values;
wenzelm
parents: 57831
diff changeset
    49
    else i.toString
0fb331032f02 more compact representation of special string values;
wenzelm
parents: 57831
diff changeset
    50
0fb331032f02 more compact representation of special string values;
wenzelm
parents: 57831
diff changeset
    51
  def signed_string_of_int(i: Int): String =
0fb331032f02 more compact representation of special string values;
wenzelm
parents: 57831
diff changeset
    52
    if (0 <= i && i < small_int) small_int_table(i)
0fb331032f02 more compact representation of special string values;
wenzelm
parents: 57831
diff changeset
    53
    else i.toString
0fb331032f02 more compact representation of special string values;
wenzelm
parents: 57831
diff changeset
    54
0fb331032f02 more compact representation of special string values;
wenzelm
parents: 57831
diff changeset
    55
48996
a8bad1369ada clarified separated_chunks vs. space_explode;
wenzelm
parents: 48479
diff changeset
    56
  /* separated chunks */
36688
321d392ab12e added separate;
wenzelm
parents: 36685
diff changeset
    57
321d392ab12e added separate;
wenzelm
parents: 36685
diff changeset
    58
  def separate[A](s: A, list: List[A]): List[A] =
51981
a8ffd3692f57 more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents: 51616
diff changeset
    59
  {
a8ffd3692f57 more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents: 51616
diff changeset
    60
    val result = new mutable.ListBuffer[A]
a8ffd3692f57 more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents: 51616
diff changeset
    61
    var first = true
a8ffd3692f57 more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents: 51616
diff changeset
    62
    for (x <- list) {
a8ffd3692f57 more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents: 51616
diff changeset
    63
      if (first) {
a8ffd3692f57 more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents: 51616
diff changeset
    64
        first = false
a8ffd3692f57 more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents: 51616
diff changeset
    65
        result += x
a8ffd3692f57 more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents: 51616
diff changeset
    66
      }
a8ffd3692f57 more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents: 51616
diff changeset
    67
      else {
a8ffd3692f57 more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents: 51616
diff changeset
    68
        result += s
a8ffd3692f57 more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents: 51616
diff changeset
    69
        result += x
a8ffd3692f57 more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents: 51616
diff changeset
    70
      }
36688
321d392ab12e added separate;
wenzelm
parents: 36685
diff changeset
    71
    }
51981
a8ffd3692f57 more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents: 51616
diff changeset
    72
    result.toList
a8ffd3692f57 more scalable Library.separate -- NB: JVM has tiny fixed-size stack;
wenzelm
parents: 51616
diff changeset
    73
  }
36688
321d392ab12e added separate;
wenzelm
parents: 36685
diff changeset
    74
56600
628e039cc34d more specific support for sequence of words;
wenzelm
parents: 56599
diff changeset
    75
  def separated_chunks(sep: Char => Boolean, source: CharSequence): Iterator[CharSequence] =
48996
a8bad1369ada clarified separated_chunks vs. space_explode;
wenzelm
parents: 48479
diff changeset
    76
    new Iterator[CharSequence] {
a8bad1369ada clarified separated_chunks vs. space_explode;
wenzelm
parents: 48479
diff changeset
    77
      private val end = source.length
a8bad1369ada clarified separated_chunks vs. space_explode;
wenzelm
parents: 48479
diff changeset
    78
      private def next_chunk(i: Int): Option[(CharSequence, Int)] =
a8bad1369ada clarified separated_chunks vs. space_explode;
wenzelm
parents: 48479
diff changeset
    79
      {
a8bad1369ada clarified separated_chunks vs. space_explode;
wenzelm
parents: 48479
diff changeset
    80
        if (i < end) {
56600
628e039cc34d more specific support for sequence of words;
wenzelm
parents: 56599
diff changeset
    81
          var j = i; do j += 1 while (j < end && !sep(source.charAt(j)))
48996
a8bad1369ada clarified separated_chunks vs. space_explode;
wenzelm
parents: 48479
diff changeset
    82
          Some((source.subSequence(i + 1, j), j))
a8bad1369ada clarified separated_chunks vs. space_explode;
wenzelm
parents: 48479
diff changeset
    83
        }
a8bad1369ada clarified separated_chunks vs. space_explode;
wenzelm
parents: 48479
diff changeset
    84
        else None
43598
826ddd91ae2b basic operations on lists and strings;
wenzelm
parents: 43442
diff changeset
    85
      }
48996
a8bad1369ada clarified separated_chunks vs. space_explode;
wenzelm
parents: 48479
diff changeset
    86
      private var state: Option[(CharSequence, Int)] = if (end == 0) None else next_chunk(-1)
a8bad1369ada clarified separated_chunks vs. space_explode;
wenzelm
parents: 48479
diff changeset
    87
a8bad1369ada clarified separated_chunks vs. space_explode;
wenzelm
parents: 48479
diff changeset
    88
      def hasNext(): Boolean = state.isDefined
a8bad1369ada clarified separated_chunks vs. space_explode;
wenzelm
parents: 48479
diff changeset
    89
      def next(): CharSequence =
a8bad1369ada clarified separated_chunks vs. space_explode;
wenzelm
parents: 48479
diff changeset
    90
        state match {
60215
5fb4990dfc73 misc tuning, based on warnings by IntelliJ IDEA;
wenzelm
parents: 59697
diff changeset
    91
          case Some((s, i)) => state = next_chunk(i); s
48996
a8bad1369ada clarified separated_chunks vs. space_explode;
wenzelm
parents: 48479
diff changeset
    92
          case None => Iterator.empty.next()
a8bad1369ada clarified separated_chunks vs. space_explode;
wenzelm
parents: 48479
diff changeset
    93
        }
43598
826ddd91ae2b basic operations on lists and strings;
wenzelm
parents: 43442
diff changeset
    94
    }
826ddd91ae2b basic operations on lists and strings;
wenzelm
parents: 43442
diff changeset
    95
48996
a8bad1369ada clarified separated_chunks vs. space_explode;
wenzelm
parents: 48479
diff changeset
    96
  def space_explode(sep: Char, str: String): List[String] =
56600
628e039cc34d more specific support for sequence of words;
wenzelm
parents: 56599
diff changeset
    97
    separated_chunks(_ == sep, str).map(_.toString).toList
48996
a8bad1369ada clarified separated_chunks vs. space_explode;
wenzelm
parents: 48479
diff changeset
    98
a8bad1369ada clarified separated_chunks vs. space_explode;
wenzelm
parents: 48479
diff changeset
    99
a8bad1369ada clarified separated_chunks vs. space_explode;
wenzelm
parents: 48479
diff changeset
   100
  /* lines */
a8bad1369ada clarified separated_chunks vs. space_explode;
wenzelm
parents: 48479
diff changeset
   101
51983
32692ce4c61a more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents: 51981
diff changeset
   102
  def terminate_lines(lines: Iterable[CharSequence]): Iterable[CharSequence] =
32692ce4c61a more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents: 51981
diff changeset
   103
    new Iterable[CharSequence] {
32692ce4c61a more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents: 51981
diff changeset
   104
      def iterator: Iterator[CharSequence] =
32692ce4c61a more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents: 51981
diff changeset
   105
        lines.iterator.map(line => new Line_Termination(line))
32692ce4c61a more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents: 51981
diff changeset
   106
    }
32692ce4c61a more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents: 51981
diff changeset
   107
48996
a8bad1369ada clarified separated_chunks vs. space_explode;
wenzelm
parents: 48479
diff changeset
   108
  def cat_lines(lines: TraversableOnce[String]): String = lines.mkString("\n")
a8bad1369ada clarified separated_chunks vs. space_explode;
wenzelm
parents: 48479
diff changeset
   109
43670
7f933761764b prefer space_explode/split_lines as in Isabelle/ML;
wenzelm
parents: 43652
diff changeset
   110
  def split_lines(str: String): List[String] = space_explode('\n', str)
7f933761764b prefer space_explode/split_lines as in Isabelle/ML;
wenzelm
parents: 43652
diff changeset
   111
62590
0c837beeb5e7 upgrade "isabelle build" to Isabelle/Scala;
wenzelm
parents: 62492
diff changeset
   112
  def prefix_lines(prfx: String, str: String): String =
0c837beeb5e7 upgrade "isabelle build" to Isabelle/Scala;
wenzelm
parents: 62492
diff changeset
   113
    if (str == "") str
0c837beeb5e7 upgrade "isabelle build" to Isabelle/Scala;
wenzelm
parents: 62492
diff changeset
   114
    else cat_lines(split_lines(str).map(s => prfx + s))
0c837beeb5e7 upgrade "isabelle build" to Isabelle/Scala;
wenzelm
parents: 62492
diff changeset
   115
48996
a8bad1369ada clarified separated_chunks vs. space_explode;
wenzelm
parents: 48479
diff changeset
   116
  def first_line(source: CharSequence): String =
a8bad1369ada clarified separated_chunks vs. space_explode;
wenzelm
parents: 48479
diff changeset
   117
  {
56600
628e039cc34d more specific support for sequence of words;
wenzelm
parents: 56599
diff changeset
   118
    val lines = separated_chunks(_ == '\n', source)
48996
a8bad1369ada clarified separated_chunks vs. space_explode;
wenzelm
parents: 48479
diff changeset
   119
    if (lines.hasNext) lines.next.toString
a8bad1369ada clarified separated_chunks vs. space_explode;
wenzelm
parents: 48479
diff changeset
   120
    else ""
a8bad1369ada clarified separated_chunks vs. space_explode;
wenzelm
parents: 48479
diff changeset
   121
  }
a8bad1369ada clarified separated_chunks vs. space_explode;
wenzelm
parents: 48479
diff changeset
   122
50847
78c40f1cc9b3 tuned signature;
wenzelm
parents: 50845
diff changeset
   123
78c40f1cc9b3 tuned signature;
wenzelm
parents: 50845
diff changeset
   124
  /* strings */
78c40f1cc9b3 tuned signature;
wenzelm
parents: 50845
diff changeset
   125
78c40f1cc9b3 tuned signature;
wenzelm
parents: 50845
diff changeset
   126
  def try_unprefix(prfx: String, s: String): Option[String] =
78c40f1cc9b3 tuned signature;
wenzelm
parents: 50845
diff changeset
   127
    if (s.startsWith(prfx)) Some(s.substring(prfx.length)) else None
78c40f1cc9b3 tuned signature;
wenzelm
parents: 50845
diff changeset
   128
55033
8e8243975860 support for nested text cartouches;
wenzelm
parents: 54548
diff changeset
   129
  def try_unsuffix(sffx: String, s: String): Option[String] =
8e8243975860 support for nested text cartouches;
wenzelm
parents: 54548
diff changeset
   130
    if (s.endsWith(sffx)) Some(s.substring(0, s.length - sffx.length)) else None
8e8243975860 support for nested text cartouches;
wenzelm
parents: 54548
diff changeset
   131
52444
2cfe6656d6d6 slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents: 51983
diff changeset
   132
  def trim_line(s: String): String =
2cfe6656d6d6 slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents: 51983
diff changeset
   133
    if (s.endsWith("\r\n")) s.substring(0, s.length - 2)
2cfe6656d6d6 slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents: 51983
diff changeset
   134
    else if (s.endsWith("\r") || s.endsWith("\n")) s.substring(0, s.length - 1)
2cfe6656d6d6 slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents: 51983
diff changeset
   135
    else s
2cfe6656d6d6 slightly improved "isabelle doc" based on Isabelle/Scala;
wenzelm
parents: 51983
diff changeset
   136
43598
826ddd91ae2b basic operations on lists and strings;
wenzelm
parents: 43442
diff changeset
   137
48996
a8bad1369ada clarified separated_chunks vs. space_explode;
wenzelm
parents: 48479
diff changeset
   138
  /* quote */
46196
805de058722b added cat_lines convenience;
wenzelm
parents: 45900
diff changeset
   139
43598
826ddd91ae2b basic operations on lists and strings;
wenzelm
parents: 43442
diff changeset
   140
  def quote(s: String): String = "\"" + s + "\""
56843
b2bfcd8cda80 support for path completion based on file-system content;
wenzelm
parents: 56730
diff changeset
   141
b2bfcd8cda80 support for path completion based on file-system content;
wenzelm
parents: 56730
diff changeset
   142
  def try_unquote(s: String): Option[String] =
b2bfcd8cda80 support for path completion based on file-system content;
wenzelm
parents: 56730
diff changeset
   143
    if (s.startsWith("\"") && s.endsWith("\"")) Some(s.substring(1, s.length - 1))
b2bfcd8cda80 support for path completion based on file-system content;
wenzelm
parents: 56730
diff changeset
   144
    else None
b2bfcd8cda80 support for path completion based on file-system content;
wenzelm
parents: 56730
diff changeset
   145
58592
b0fff34d3247 completion for bibtex entries;
wenzelm
parents: 57909
diff changeset
   146
  def perhaps_unquote(s: String): String = try_unquote(s) getOrElse s
b0fff34d3247 completion for bibtex entries;
wenzelm
parents: 57909
diff changeset
   147
43598
826ddd91ae2b basic operations on lists and strings;
wenzelm
parents: 43442
diff changeset
   148
  def commas(ss: Iterable[String]): String = ss.iterator.mkString(", ")
48362
c3192ccb0ff4 proper commas_quote;
wenzelm
parents: 48345
diff changeset
   149
  def commas_quote(ss: Iterable[String]): String = ss.iterator.map(quote).mkString(", ")
43598
826ddd91ae2b basic operations on lists and strings;
wenzelm
parents: 43442
diff changeset
   150
36688
321d392ab12e added separate;
wenzelm
parents: 36685
diff changeset
   151
51983
32692ce4c61a more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents: 51981
diff changeset
   152
  /* CharSequence */
34141
297b2149077d simiplified result of keyword parser (again);
wenzelm
parents: 34136
diff changeset
   153
297b2149077d simiplified result of keyword parser (again);
wenzelm
parents: 34136
diff changeset
   154
  class Reverse(text: CharSequence, start: Int, end: Int) extends CharSequence
297b2149077d simiplified result of keyword parser (again);
wenzelm
parents: 34136
diff changeset
   155
  {
297b2149077d simiplified result of keyword parser (again);
wenzelm
parents: 34136
diff changeset
   156
    require(0 <= start && start <= end && end <= text.length)
297b2149077d simiplified result of keyword parser (again);
wenzelm
parents: 34136
diff changeset
   157
297b2149077d simiplified result of keyword parser (again);
wenzelm
parents: 34136
diff changeset
   158
    def this(text: CharSequence) = this(text, 0, text.length)
297b2149077d simiplified result of keyword parser (again);
wenzelm
parents: 34136
diff changeset
   159
297b2149077d simiplified result of keyword parser (again);
wenzelm
parents: 34136
diff changeset
   160
    def length: Int = end - start
297b2149077d simiplified result of keyword parser (again);
wenzelm
parents: 34136
diff changeset
   161
    def charAt(i: Int): Char = text.charAt(end - i - 1)
297b2149077d simiplified result of keyword parser (again);
wenzelm
parents: 34136
diff changeset
   162
297b2149077d simiplified result of keyword parser (again);
wenzelm
parents: 34136
diff changeset
   163
    def subSequence(i: Int, j: Int): CharSequence =
297b2149077d simiplified result of keyword parser (again);
wenzelm
parents: 34136
diff changeset
   164
      if (0 <= i && i <= j && j <= length) new Reverse(text, end - j, end - i)
297b2149077d simiplified result of keyword parser (again);
wenzelm
parents: 34136
diff changeset
   165
      else throw new IndexOutOfBoundsException
297b2149077d simiplified result of keyword parser (again);
wenzelm
parents: 34136
diff changeset
   166
297b2149077d simiplified result of keyword parser (again);
wenzelm
parents: 34136
diff changeset
   167
    override def toString: String =
297b2149077d simiplified result of keyword parser (again);
wenzelm
parents: 34136
diff changeset
   168
    {
297b2149077d simiplified result of keyword parser (again);
wenzelm
parents: 34136
diff changeset
   169
      val buf = new StringBuilder(length)
297b2149077d simiplified result of keyword parser (again);
wenzelm
parents: 34136
diff changeset
   170
      for (i <- 0 until length)
297b2149077d simiplified result of keyword parser (again);
wenzelm
parents: 34136
diff changeset
   171
        buf.append(charAt(i))
297b2149077d simiplified result of keyword parser (again);
wenzelm
parents: 34136
diff changeset
   172
      buf.toString
297b2149077d simiplified result of keyword parser (again);
wenzelm
parents: 34136
diff changeset
   173
    }
297b2149077d simiplified result of keyword parser (again);
wenzelm
parents: 34136
diff changeset
   174
  }
297b2149077d simiplified result of keyword parser (again);
wenzelm
parents: 34136
diff changeset
   175
51983
32692ce4c61a more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents: 51981
diff changeset
   176
  class Line_Termination(text: CharSequence) extends CharSequence
32692ce4c61a more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents: 51981
diff changeset
   177
  {
32692ce4c61a more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents: 51981
diff changeset
   178
    def length: Int = text.length + 1
32692ce4c61a more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents: 51981
diff changeset
   179
    def charAt(i: Int): Char = if (i == text.length) '\n' else text.charAt(i)
32692ce4c61a more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents: 51981
diff changeset
   180
    def subSequence(i: Int, j: Int): CharSequence =
32692ce4c61a more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents: 51981
diff changeset
   181
      if (j == text.length + 1) new Line_Termination(text.subSequence(i, j - 1))
32692ce4c61a more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents: 51981
diff changeset
   182
      else text.subSequence(i, j)
32692ce4c61a more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents: 51981
diff changeset
   183
    override def toString: String = text.toString + "\n"
32692ce4c61a more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents: 51981
diff changeset
   184
  }
32692ce4c61a more frugal line termination, to cope with huge log files (see also 016cb7d8f297);
wenzelm
parents: 51981
diff changeset
   185
34141
297b2149077d simiplified result of keyword parser (again);
wenzelm
parents: 34136
diff changeset
   186
59224
wenzelm
parents: 58592
diff changeset
   187
  /* regular expressions */
wenzelm
parents: 58592
diff changeset
   188
wenzelm
parents: 58592
diff changeset
   189
  def make_regex(s: String): Option[Regex] =
wenzelm
parents: 58592
diff changeset
   190
    try { Some(new Regex(s)) } catch { case ERROR(_) => None }
wenzelm
parents: 58592
diff changeset
   191
wenzelm
parents: 58592
diff changeset
   192
61883
c0f34fe6aa61 clarified length of block with pre-existant forced breaks;
wenzelm
parents: 60215
diff changeset
   193
  /* lists */
c0f34fe6aa61 clarified length of block with pre-existant forced breaks;
wenzelm
parents: 60215
diff changeset
   194
c0f34fe6aa61 clarified length of block with pre-existant forced breaks;
wenzelm
parents: 60215
diff changeset
   195
  def take_prefix[A](pred: A => Boolean, xs: List[A]): (List[A], List[A]) =
c0f34fe6aa61 clarified length of block with pre-existant forced breaks;
wenzelm
parents: 60215
diff changeset
   196
    (xs.takeWhile(pred), xs.dropWhile(pred))
56686
2386d1a3ca8f canonical list operations, as in ML;
wenzelm
parents: 56672
diff changeset
   197
60215
5fb4990dfc73 misc tuning, based on warnings by IntelliJ IDEA;
wenzelm
parents: 59697
diff changeset
   198
  def member[A, B](xs: List[A])(x: B): Boolean = xs.contains(x)
56688
f3932166a33d more canonical list operations;
wenzelm
parents: 56686
diff changeset
   199
  def insert[A](x: A)(xs: List[A]): List[A] = if (xs.contains(x)) xs else x :: xs
f3932166a33d more canonical list operations;
wenzelm
parents: 56686
diff changeset
   200
  def remove[A, B](x: B)(xs: List[A]): List[A] = if (member(xs)(x)) xs.filterNot(_ == x) else xs
f3932166a33d more canonical list operations;
wenzelm
parents: 56686
diff changeset
   201
  def update[A](x: A)(xs: List[A]): List[A] = x :: remove(x)(xs)
63734
133e3e84e6fb some support for merge of Isabelle/jEdit shortcuts wrt. jEdit keymap;
wenzelm
parents: 62590
diff changeset
   202
63867
fb46c031c841 maintain abbrevs in canonical reverse order;
wenzelm
parents: 63789
diff changeset
   203
  def merge[A](xs: List[A], ys: List[A]): List[A] =
fb46c031c841 maintain abbrevs in canonical reverse order;
wenzelm
parents: 63789
diff changeset
   204
    if (xs.eq(ys)) xs
fb46c031c841 maintain abbrevs in canonical reverse order;
wenzelm
parents: 63789
diff changeset
   205
    else if (xs.isEmpty) ys
fb46c031c841 maintain abbrevs in canonical reverse order;
wenzelm
parents: 63789
diff changeset
   206
    else ys.foldRight(xs)(Library.insert(_)(_))
fb46c031c841 maintain abbrevs in canonical reverse order;
wenzelm
parents: 63789
diff changeset
   207
63773
d1a5d10affc0 simplified;
wenzelm
parents: 63734
diff changeset
   208
  def distinct[A](xs: List[A]): List[A] =
63734
133e3e84e6fb some support for merge of Isabelle/jEdit shortcuts wrt. jEdit keymap;
wenzelm
parents: 62590
diff changeset
   209
  {
133e3e84e6fb some support for merge of Isabelle/jEdit shortcuts wrt. jEdit keymap;
wenzelm
parents: 62590
diff changeset
   210
    val result = new mutable.ListBuffer[A]
63773
d1a5d10affc0 simplified;
wenzelm
parents: 63734
diff changeset
   211
    xs.foreach(x => if (!result.contains(x)) result += x)
63734
133e3e84e6fb some support for merge of Isabelle/jEdit shortcuts wrt. jEdit keymap;
wenzelm
parents: 62590
diff changeset
   212
    result.toList
133e3e84e6fb some support for merge of Isabelle/jEdit shortcuts wrt. jEdit keymap;
wenzelm
parents: 62590
diff changeset
   213
  }
63781
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63773
diff changeset
   214
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63773
diff changeset
   215
  def duplicates[A](lst: List[A]): List[A] =
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63773
diff changeset
   216
  {
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63773
diff changeset
   217
    val result = new mutable.ListBuffer[A]
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63773
diff changeset
   218
    @tailrec def dups(rest: List[A]): Unit =
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63773
diff changeset
   219
      rest match {
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63773
diff changeset
   220
        case Nil =>
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63773
diff changeset
   221
        case x :: xs =>
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63773
diff changeset
   222
          if (!result.contains(x) && xs.contains(x)) result += x
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63773
diff changeset
   223
          dups(xs)
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63773
diff changeset
   224
      }
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63773
diff changeset
   225
    dups(lst)
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63773
diff changeset
   226
    result.toList
af9fe0b6b78e support for (single) primary key;
wenzelm
parents: 63773
diff changeset
   227
  }
34136
3dcb46ae6185 added basic library -- Scala version;
wenzelm
parents:
diff changeset
   228
}