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