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