src/Pure/PIDE/command.scala
author wenzelm
Sat Mar 23 19:39:31 2013 +0100 (2013-03-23 ago)
changeset 51496 cb677987b7e3
parent 51494 8f3d1a7bee26
child 52507 27925b58d6bd
permissions -rw-r--r--
retain original tooltip range, to avoid repeated window popup when the mouse is moved over the same content;
wenzelm@36676
     1
/*  Title:      Pure/PIDE/command.scala
wenzelm@36676
     2
    Author:     Fabian Immler, TU Munich
wenzelm@36676
     3
    Author:     Makarius
wenzelm@36676
     4
wenzelm@36676
     5
Prover commands with semantic state.
wenzelm@36676
     6
*/
wenzelm@34407
     7
wenzelm@34871
     8
package isabelle
wenzelm@34318
     9
wenzelm@43520
    10
import java.lang.System
wenzelm@34451
    11
wenzelm@45644
    12
import scala.collection.mutable
wenzelm@38872
    13
import scala.collection.immutable.SortedMap
wenzelm@38872
    14
wenzelm@38872
    15
wenzelm@34637
    16
object Command
wenzelm@34637
    17
{
wenzelm@38361
    18
  /** accumulated results from prover **/
wenzelm@38361
    19
wenzelm@50507
    20
  /* results */
wenzelm@50507
    21
wenzelm@50507
    22
  object Results
wenzelm@50507
    23
  {
wenzelm@51496
    24
    type Entry = (Long, XML.Tree)
wenzelm@50507
    25
    val empty = new Results(SortedMap.empty)
wenzelm@51496
    26
    def make(es: Iterable[Results.Entry]): Results = (empty /: es.iterator)(_ + _)
wenzelm@50507
    27
    def merge(rs: Iterable[Results]): Results = (empty /: rs.iterator)(_ ++ _)
wenzelm@50507
    28
  }
wenzelm@50507
    29
wenzelm@51494
    30
  final class Results private(private val rep: SortedMap[Long, XML.Tree])
wenzelm@50507
    31
  {
wenzelm@50507
    32
    def defined(serial: Long): Boolean = rep.isDefinedAt(serial)
wenzelm@50507
    33
    def get(serial: Long): Option[XML.Tree] = rep.get(serial)
wenzelm@51496
    34
    def entries: Iterator[Results.Entry] = rep.iterator
wenzelm@50508
    35
wenzelm@51496
    36
    def + (entry: Results.Entry): Results =
wenzelm@50508
    37
      if (defined(entry._1)) this
wenzelm@50508
    38
      else new Results(rep + entry)
wenzelm@50508
    39
wenzelm@50508
    40
    def ++ (other: Results): Results =
wenzelm@50508
    41
      if (this eq other) this
wenzelm@50508
    42
      else if (rep.isEmpty) other
wenzelm@50508
    43
      else (this /: other.entries)(_ + _)
wenzelm@50540
    44
wenzelm@51494
    45
    override def hashCode: Int = rep.hashCode
wenzelm@51494
    46
    override def equals(that: Any): Boolean =
wenzelm@51494
    47
      that match {
wenzelm@51494
    48
        case other: Results => rep == other.rep
wenzelm@51494
    49
        case _ => false
wenzelm@51494
    50
      }
wenzelm@50540
    51
    override def toString: String = entries.mkString("Results(", ", ", ")")
wenzelm@50507
    52
  }
wenzelm@50507
    53
wenzelm@50507
    54
wenzelm@50507
    55
  /* state */
wenzelm@50501
    56
wenzelm@43714
    57
  sealed case class State(
wenzelm@50501
    58
    command: Command,
wenzelm@50501
    59
    status: List[Markup] = Nil,
wenzelm@50507
    60
    results: Results = Results.empty,
wenzelm@50501
    61
    markup: Markup_Tree = Markup_Tree.empty)
wenzelm@38361
    62
  {
wenzelm@49645
    63
    def markup_to_XML(filter: XML.Elem => Boolean): XML.Body =
wenzelm@49645
    64
      markup.to_XML(command.range, command.source, filter)
wenzelm@49614
    65
wenzelm@49614
    66
wenzelm@51494
    67
    /* content */
wenzelm@51494
    68
wenzelm@51494
    69
    def eq_content(other: State): Boolean =
wenzelm@51494
    70
      command.source == other.command.source &&
wenzelm@51494
    71
      status == other.status &&
wenzelm@51494
    72
      results == other.results &&
wenzelm@51494
    73
      markup == other.markup
wenzelm@38361
    74
wenzelm@46152
    75
    private def add_status(st: Markup): State = copy(status = st :: status)
wenzelm@46152
    76
    private def add_markup(m: Text.Markup): State = copy(markup = markup + m)
wenzelm@38361
    77
wenzelm@49527
    78
    def + (alt_id: Document.ID, message: XML.Elem): Command.State =
wenzelm@38361
    79
      message match {
wenzelm@50201
    80
        case XML.Elem(Markup(Markup.STATUS, _), msgs) =>
wenzelm@38714
    81
          (this /: msgs)((state, msg) =>
wenzelm@38714
    82
            msg match {
wenzelm@46152
    83
              case elem @ XML.Elem(markup, Nil) =>
wenzelm@50499
    84
                state.add_status(markup)
wenzelm@50499
    85
                  .add_markup(Text.Info(command.proper_range, elem))  // FIXME cumulation order!?
wenzelm@46152
    86
wenzelm@38714
    87
              case _ => System.err.println("Ignored status message: " + msg); state
wenzelm@38714
    88
            })
wenzelm@38581
    89
wenzelm@50201
    90
        case XML.Elem(Markup(Markup.REPORT, _), msgs) =>
wenzelm@38572
    91
          (this /: msgs)((state, msg) =>
wenzelm@38572
    92
            msg match {
wenzelm@39173
    93
              case XML.Elem(Markup(name, atts @ Position.Id_Range(id, raw_range)), args)
wenzelm@49527
    94
              if (id == command.id || id == alt_id) &&
wenzelm@49527
    95
                  command.range.contains(command.decode(raw_range)) =>
wenzelm@39173
    96
                val range = command.decode(raw_range)
wenzelm@38872
    97
                val props = Position.purge(atts)
wenzelm@45455
    98
                val info: Text.Markup = Text.Info(range, XML.Elem(Markup(name, props), args))
wenzelm@38723
    99
                state.add_markup(info)
wenzelm@49526
   100
              case XML.Elem(Markup(name, atts), args)
wenzelm@50201
   101
              if !atts.exists({ case (a, _) => Markup.POSITION_PROPERTIES(a) }) =>
wenzelm@49037
   102
                val range = command.proper_range
wenzelm@49037
   103
                val props = Position.purge(atts)
wenzelm@49037
   104
                val info: Text.Markup = Text.Info(range, XML.Elem(Markup(name, props), args))
wenzelm@49037
   105
                state.add_markup(info)
wenzelm@40572
   106
              case _ =>
wenzelm@40572
   107
                // FIXME System.err.println("Ignored report message: " + msg)
wenzelm@40572
   108
                state
wenzelm@38361
   109
            })
wenzelm@38872
   110
        case XML.Elem(Markup(name, atts), body) =>
wenzelm@38872
   111
          atts match {
wenzelm@50201
   112
            case Markup.Serial(i) =>
wenzelm@50163
   113
              val props = Position.purge(atts)
wenzelm@50201
   114
              val message1 = XML.Elem(Markup(Markup.message(name), props), body)
wenzelm@50163
   115
              val message2 = XML.Elem(Markup(name, props), body)
wenzelm@50163
   116
wenzelm@50163
   117
              val st0 = copy(results = results + (i -> message1))
wenzelm@39441
   118
              val st1 =
wenzelm@50500
   119
                if (Protocol.is_inlined(message))
wenzelm@45709
   120
                  (st0 /: Protocol.message_positions(command, message))(
wenzelm@50163
   121
                    (st, range) => st.add_markup(Text.Info(range, message2)))
wenzelm@50500
   122
                else st0
wenzelm@49445
   123
wenzelm@49445
   124
              st1
wenzelm@38872
   125
            case _ => System.err.println("Ignored message without serial number: " + message); this
wenzelm@38872
   126
          }
wenzelm@38361
   127
      }
wenzelm@38361
   128
  }
wenzelm@38367
   129
wenzelm@38367
   130
wenzelm@45644
   131
  /* make commands */
wenzelm@45644
   132
wenzelm@48745
   133
  type Span = List[Token]
wenzelm@48745
   134
wenzelm@50501
   135
  def apply(id: Document.Command_ID, node_name: Document.Node.Name, span: Span,
wenzelm@50507
   136
    results: Results = Results.empty, markup: Markup_Tree = Markup_Tree.empty): Command =
wenzelm@45644
   137
  {
wenzelm@45644
   138
    val source: String =
wenzelm@48745
   139
      span match {
wenzelm@45644
   140
        case List(tok) => tok.source
wenzelm@48745
   141
        case _ => span.map(_.source).mkString
wenzelm@45644
   142
      }
wenzelm@45644
   143
wenzelm@48745
   144
    val span1 = new mutable.ListBuffer[Token]
wenzelm@45644
   145
    var i = 0
wenzelm@48745
   146
    for (Token(kind, s) <- span) {
wenzelm@45644
   147
      val n = s.length
wenzelm@45644
   148
      val s1 = source.substring(i, i + n)
wenzelm@48745
   149
      span1 += Token(kind, s1)
wenzelm@45644
   150
      i += n
wenzelm@45644
   151
    }
wenzelm@45644
   152
wenzelm@50501
   153
    new Command(id, node_name, span1.toList, source, results, markup)
wenzelm@45644
   154
  }
wenzelm@45644
   155
wenzelm@50501
   156
  val empty = Command(Document.no_id, Document.Node.Name.empty, Nil)
wenzelm@49414
   157
wenzelm@50501
   158
  def unparsed(id: Document.Command_ID, source: String, results: Results, markup: Markup_Tree)
wenzelm@50501
   159
      : Command =
wenzelm@50501
   160
    Command(id, Document.Node.Name.empty, List(Token(Token.Kind.UNPARSED, source)), results, markup)
wenzelm@49414
   161
wenzelm@50501
   162
  def unparsed(source: String): Command =
wenzelm@50507
   163
    unparsed(Document.no_id, source, Results.empty, Markup_Tree.empty)
wenzelm@44384
   164
wenzelm@50501
   165
  def rich_text(id: Document.Command_ID, results: Results, body: XML.Body): Command =
wenzelm@49414
   166
  {
wenzelm@49466
   167
    val text = XML.content(body)
wenzelm@49466
   168
    val markup = Markup_Tree.from_XML(body)
wenzelm@50501
   169
    unparsed(id, text, results, markup)
wenzelm@49414
   170
  }
wenzelm@49359
   171
wenzelm@44384
   172
wenzelm@44384
   173
  /* perspective */
wenzelm@44384
   174
wenzelm@44474
   175
  object Perspective
wenzelm@44474
   176
  {
wenzelm@44474
   177
    val empty: Perspective = Perspective(Nil)
wenzelm@44474
   178
  }
wenzelm@44385
   179
wenzelm@44474
   180
  sealed case class Perspective(commands: List[Command])  // visible commands in canonical order
wenzelm@44385
   181
  {
wenzelm@44474
   182
    def same(that: Perspective): Boolean =
wenzelm@44474
   183
    {
wenzelm@44474
   184
      val cmds1 = this.commands
wenzelm@44474
   185
      val cmds2 = that.commands
wenzelm@48754
   186
      require(!cmds1.exists(_.is_undefined))
wenzelm@48754
   187
      require(!cmds2.exists(_.is_undefined))
wenzelm@44474
   188
      cmds1.length == cmds2.length &&
wenzelm@44474
   189
        (cmds1.iterator zip cmds2.iterator).forall({ case (c1, c2) => c1.id == c2.id })
wenzelm@44474
   190
    }
wenzelm@44385
   191
  }
wenzelm@34318
   192
}
wenzelm@34318
   193
wenzelm@38361
   194
wenzelm@46712
   195
final class Command private(
wenzelm@38150
   196
    val id: Document.Command_ID,
wenzelm@44615
   197
    val node_name: Document.Node.Name,
wenzelm@48745
   198
    val span: Command.Span,
wenzelm@49414
   199
    val source: String,
wenzelm@50501
   200
    val init_results: Command.Results,
wenzelm@49414
   201
    val init_markup: Markup_Tree)
wenzelm@34451
   202
{
wenzelm@34859
   203
  /* classification */
wenzelm@34500
   204
wenzelm@48754
   205
  def is_undefined: Boolean = id == Document.no_id
wenzelm@48754
   206
  val is_unparsed: Boolean = span.exists(_.is_unparsed)
wenzelm@48754
   207
  val is_unfinished: Boolean = span.exists(_.is_unfinished)
wenzelm@44385
   208
wenzelm@48599
   209
  val is_ignored: Boolean = !span.exists(_.is_proper)
wenzelm@48754
   210
  val is_malformed: Boolean = !is_ignored && (!span.head.is_command || span.exists(_.is_error))
wenzelm@47012
   211
  def is_command: Boolean = !is_ignored && !is_malformed
wenzelm@34859
   212
wenzelm@47012
   213
  def name: String =
wenzelm@48718
   214
    span.find(_.is_command) match { case Some(tok) => tok.source case _ => "" }
wenzelm@47012
   215
wenzelm@37129
   216
  override def toString =
wenzelm@37373
   217
    id + "/" + (if (is_command) name else if (is_ignored) "IGNORED" else "MALFORMED")
wenzelm@34495
   218
wenzelm@34859
   219
wenzelm@34859
   220
  /* source text */
wenzelm@34451
   221
wenzelm@46813
   222
  def length: Int = source.length
wenzelm@46813
   223
  val range: Text.Range = Text.Range(0, length)
wenzelm@46813
   224
wenzelm@46813
   225
  val proper_range: Text.Range =
wenzelm@51048
   226
    Text.Range(0, (length /: span.reverse.iterator.takeWhile(_.is_improper))(_ - _.source.length))
wenzelm@46813
   227
wenzelm@38426
   228
  def source(range: Text.Range): String = source.substring(range.start, range.stop)
wenzelm@38572
   229
wenzelm@34859
   230
  lazy val symbol_index = new Symbol.Index(source)
wenzelm@38579
   231
  def decode(i: Text.Offset): Text.Offset = symbol_index.decode(i)
wenzelm@38579
   232
  def decode(r: Text.Range): Text.Range = symbol_index.decode(r)
wenzelm@38370
   233
wenzelm@38370
   234
wenzelm@38370
   235
  /* accumulated results */
wenzelm@38370
   236
wenzelm@50501
   237
  val init_state: Command.State =
wenzelm@50501
   238
    Command.State(this, results = init_results, markup = init_markup)
immler@34676
   239
}