src/Pure/PIDE/command.scala
author wenzelm
Thu Dec 13 17:29:23 2012 +0100 (2012-12-13 ago)
changeset 50501 6f41f1646617
parent 50500 c94bba7906d2
child 50507 9605b0d93d1e
permissions -rw-r--r--
more careful handling of Dialog_Result, with active area and color feedback;
more formal type Command.Results;
propagate command results to output, which is required to resolve update of dialog state;
clarified Markup.message: retain uninterpreted messages;
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@50501
    20
  type Results = SortedMap[Long, XML.Tree]
wenzelm@50501
    21
  val empty_results: Results = SortedMap.empty
wenzelm@50501
    22
wenzelm@43714
    23
  sealed case class State(
wenzelm@50501
    24
    command: Command,
wenzelm@50501
    25
    status: List[Markup] = Nil,
wenzelm@50501
    26
    results: Results = empty_results,
wenzelm@50501
    27
    markup: Markup_Tree = Markup_Tree.empty)
wenzelm@38361
    28
  {
wenzelm@49645
    29
    def markup_to_XML(filter: XML.Elem => Boolean): XML.Body =
wenzelm@49645
    30
      markup.to_XML(command.range, command.source, filter)
wenzelm@49614
    31
wenzelm@49614
    32
wenzelm@46152
    33
    /* accumulate content */
wenzelm@38361
    34
wenzelm@46152
    35
    private def add_status(st: Markup): State = copy(status = st :: status)
wenzelm@46152
    36
    private def add_markup(m: Text.Markup): State = copy(markup = markup + m)
wenzelm@38361
    37
wenzelm@49527
    38
    def + (alt_id: Document.ID, message: XML.Elem): Command.State =
wenzelm@38361
    39
      message match {
wenzelm@50201
    40
        case XML.Elem(Markup(Markup.STATUS, _), msgs) =>
wenzelm@38714
    41
          (this /: msgs)((state, msg) =>
wenzelm@38714
    42
            msg match {
wenzelm@46152
    43
              case elem @ XML.Elem(markup, Nil) =>
wenzelm@50499
    44
                state.add_status(markup)
wenzelm@50499
    45
                  .add_markup(Text.Info(command.proper_range, elem))  // FIXME cumulation order!?
wenzelm@46152
    46
wenzelm@38714
    47
              case _ => System.err.println("Ignored status message: " + msg); state
wenzelm@38714
    48
            })
wenzelm@38581
    49
wenzelm@50201
    50
        case XML.Elem(Markup(Markup.REPORT, _), msgs) =>
wenzelm@38572
    51
          (this /: msgs)((state, msg) =>
wenzelm@38572
    52
            msg match {
wenzelm@39173
    53
              case XML.Elem(Markup(name, atts @ Position.Id_Range(id, raw_range)), args)
wenzelm@49527
    54
              if (id == command.id || id == alt_id) &&
wenzelm@49527
    55
                  command.range.contains(command.decode(raw_range)) =>
wenzelm@39173
    56
                val range = command.decode(raw_range)
wenzelm@38872
    57
                val props = Position.purge(atts)
wenzelm@45455
    58
                val info: Text.Markup = Text.Info(range, XML.Elem(Markup(name, props), args))
wenzelm@38723
    59
                state.add_markup(info)
wenzelm@49526
    60
              case XML.Elem(Markup(name, atts), args)
wenzelm@50201
    61
              if !atts.exists({ case (a, _) => Markup.POSITION_PROPERTIES(a) }) =>
wenzelm@49037
    62
                val range = command.proper_range
wenzelm@49037
    63
                val props = Position.purge(atts)
wenzelm@49037
    64
                val info: Text.Markup = Text.Info(range, XML.Elem(Markup(name, props), args))
wenzelm@49037
    65
                state.add_markup(info)
wenzelm@40572
    66
              case _ =>
wenzelm@40572
    67
                // FIXME System.err.println("Ignored report message: " + msg)
wenzelm@40572
    68
                state
wenzelm@38361
    69
            })
wenzelm@38872
    70
        case XML.Elem(Markup(name, atts), body) =>
wenzelm@38872
    71
          atts match {
wenzelm@50201
    72
            case Markup.Serial(i) =>
wenzelm@50163
    73
              val props = Position.purge(atts)
wenzelm@50201
    74
              val message1 = XML.Elem(Markup(Markup.message(name), props), body)
wenzelm@50163
    75
              val message2 = XML.Elem(Markup(name, props), body)
wenzelm@50163
    76
wenzelm@50163
    77
              val st0 = copy(results = results + (i -> message1))
wenzelm@39441
    78
              val st1 =
wenzelm@50500
    79
                if (Protocol.is_inlined(message))
wenzelm@45709
    80
                  (st0 /: Protocol.message_positions(command, message))(
wenzelm@50163
    81
                    (st, range) => st.add_markup(Text.Info(range, message2)))
wenzelm@50500
    82
                else st0
wenzelm@49445
    83
wenzelm@49445
    84
              st1
wenzelm@38872
    85
            case _ => System.err.println("Ignored message without serial number: " + message); this
wenzelm@38872
    86
          }
wenzelm@38361
    87
      }
wenzelm@38361
    88
  }
wenzelm@38367
    89
wenzelm@38367
    90
wenzelm@45644
    91
  /* make commands */
wenzelm@45644
    92
wenzelm@48745
    93
  type Span = List[Token]
wenzelm@48745
    94
wenzelm@50501
    95
  def apply(id: Document.Command_ID, node_name: Document.Node.Name, span: Span,
wenzelm@50501
    96
    results: Results = empty_results, markup: Markup_Tree = Markup_Tree.empty): Command =
wenzelm@45644
    97
  {
wenzelm@45644
    98
    val source: String =
wenzelm@48745
    99
      span match {
wenzelm@45644
   100
        case List(tok) => tok.source
wenzelm@48745
   101
        case _ => span.map(_.source).mkString
wenzelm@45644
   102
      }
wenzelm@45644
   103
wenzelm@48745
   104
    val span1 = new mutable.ListBuffer[Token]
wenzelm@45644
   105
    var i = 0
wenzelm@48745
   106
    for (Token(kind, s) <- span) {
wenzelm@45644
   107
      val n = s.length
wenzelm@45644
   108
      val s1 = source.substring(i, i + n)
wenzelm@48745
   109
      span1 += Token(kind, s1)
wenzelm@45644
   110
      i += n
wenzelm@45644
   111
    }
wenzelm@45644
   112
wenzelm@50501
   113
    new Command(id, node_name, span1.toList, source, results, markup)
wenzelm@45644
   114
  }
wenzelm@45644
   115
wenzelm@50501
   116
  val empty = Command(Document.no_id, Document.Node.Name.empty, Nil)
wenzelm@49414
   117
wenzelm@50501
   118
  def unparsed(id: Document.Command_ID, source: String, results: Results, markup: Markup_Tree)
wenzelm@50501
   119
      : Command =
wenzelm@50501
   120
    Command(id, Document.Node.Name.empty, List(Token(Token.Kind.UNPARSED, source)), results, markup)
wenzelm@49414
   121
wenzelm@50501
   122
  def unparsed(source: String): Command =
wenzelm@50501
   123
    unparsed(Document.no_id, source, empty_results, Markup_Tree.empty)
wenzelm@44384
   124
wenzelm@50501
   125
  def rich_text(id: Document.Command_ID, results: Results, body: XML.Body): Command =
wenzelm@49414
   126
  {
wenzelm@49466
   127
    val text = XML.content(body)
wenzelm@49466
   128
    val markup = Markup_Tree.from_XML(body)
wenzelm@50501
   129
    unparsed(id, text, results, markup)
wenzelm@49414
   130
  }
wenzelm@49359
   131
wenzelm@44384
   132
wenzelm@44384
   133
  /* perspective */
wenzelm@44384
   134
wenzelm@44474
   135
  object Perspective
wenzelm@44474
   136
  {
wenzelm@44474
   137
    val empty: Perspective = Perspective(Nil)
wenzelm@44474
   138
  }
wenzelm@44385
   139
wenzelm@44474
   140
  sealed case class Perspective(commands: List[Command])  // visible commands in canonical order
wenzelm@44385
   141
  {
wenzelm@44474
   142
    def same(that: Perspective): Boolean =
wenzelm@44474
   143
    {
wenzelm@44474
   144
      val cmds1 = this.commands
wenzelm@44474
   145
      val cmds2 = that.commands
wenzelm@48754
   146
      require(!cmds1.exists(_.is_undefined))
wenzelm@48754
   147
      require(!cmds2.exists(_.is_undefined))
wenzelm@44474
   148
      cmds1.length == cmds2.length &&
wenzelm@44474
   149
        (cmds1.iterator zip cmds2.iterator).forall({ case (c1, c2) => c1.id == c2.id })
wenzelm@44474
   150
    }
wenzelm@44385
   151
  }
wenzelm@34318
   152
}
wenzelm@34318
   153
wenzelm@38361
   154
wenzelm@46712
   155
final class Command private(
wenzelm@38150
   156
    val id: Document.Command_ID,
wenzelm@44615
   157
    val node_name: Document.Node.Name,
wenzelm@48745
   158
    val span: Command.Span,
wenzelm@49414
   159
    val source: String,
wenzelm@50501
   160
    val init_results: Command.Results,
wenzelm@49414
   161
    val init_markup: Markup_Tree)
wenzelm@34451
   162
{
wenzelm@34859
   163
  /* classification */
wenzelm@34500
   164
wenzelm@48754
   165
  def is_undefined: Boolean = id == Document.no_id
wenzelm@48754
   166
  val is_unparsed: Boolean = span.exists(_.is_unparsed)
wenzelm@48754
   167
  val is_unfinished: Boolean = span.exists(_.is_unfinished)
wenzelm@44385
   168
wenzelm@48599
   169
  val is_ignored: Boolean = !span.exists(_.is_proper)
wenzelm@48754
   170
  val is_malformed: Boolean = !is_ignored && (!span.head.is_command || span.exists(_.is_error))
wenzelm@47012
   171
  def is_command: Boolean = !is_ignored && !is_malformed
wenzelm@34859
   172
wenzelm@47012
   173
  def name: String =
wenzelm@48718
   174
    span.find(_.is_command) match { case Some(tok) => tok.source case _ => "" }
wenzelm@47012
   175
wenzelm@37129
   176
  override def toString =
wenzelm@37373
   177
    id + "/" + (if (is_command) name else if (is_ignored) "IGNORED" else "MALFORMED")
wenzelm@34495
   178
wenzelm@34859
   179
wenzelm@34859
   180
  /* source text */
wenzelm@34451
   181
wenzelm@46813
   182
  def length: Int = source.length
wenzelm@46813
   183
  val range: Text.Range = Text.Range(0, length)
wenzelm@46813
   184
wenzelm@46813
   185
  val proper_range: Text.Range =
wenzelm@47459
   186
    Text.Range(0, (length /: span.reverse.iterator.takeWhile(_.is_space))(_ - _.source.length))
wenzelm@46813
   187
wenzelm@38426
   188
  def source(range: Text.Range): String = source.substring(range.start, range.stop)
wenzelm@38572
   189
wenzelm@34859
   190
  lazy val symbol_index = new Symbol.Index(source)
wenzelm@38579
   191
  def decode(i: Text.Offset): Text.Offset = symbol_index.decode(i)
wenzelm@38579
   192
  def decode(r: Text.Range): Text.Range = symbol_index.decode(r)
wenzelm@38370
   193
wenzelm@38370
   194
wenzelm@38370
   195
  /* accumulated results */
wenzelm@38370
   196
wenzelm@50501
   197
  val init_state: Command.State =
wenzelm@50501
   198
    Command.State(this, results = init_results, markup = init_markup)
immler@34676
   199
}