src/Pure/PIDE/command.scala
author wenzelm
Fri Aug 31 14:35:04 2012 +0200 (2012-08-31 ago)
changeset 49037 d7a1973b063c
parent 48922 6f3ccfa7818d
child 49359 c1262d7389fb
permissions -rw-r--r--
more markup for failed goal forks, reusing "bad";
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@46152
    26
    /* accumulate content */
wenzelm@38361
    27
wenzelm@46152
    28
    private def add_status(st: Markup): State = copy(status = st :: status)
wenzelm@46152
    29
    private def add_markup(m: Text.Markup): State = copy(markup = markup + m)
wenzelm@38361
    30
wenzelm@46152
    31
    def + (message: XML.Elem): Command.State =
wenzelm@38361
    32
      message match {
wenzelm@45666
    33
        case XML.Elem(Markup(Isabelle_Markup.STATUS, _), msgs) =>
wenzelm@38714
    34
          (this /: msgs)((state, msg) =>
wenzelm@38714
    35
            msg match {
wenzelm@46152
    36
              case elem @ XML.Elem(markup, Nil) =>
wenzelm@46910
    37
                state.add_status(markup).add_markup(Text.Info(command.proper_range, elem))
wenzelm@46152
    38
wenzelm@38714
    39
              case _ => System.err.println("Ignored status message: " + msg); state
wenzelm@38714
    40
            })
wenzelm@38581
    41
wenzelm@45666
    42
        case XML.Elem(Markup(Isabelle_Markup.REPORT, _), msgs) =>
wenzelm@38572
    43
          (this /: msgs)((state, msg) =>
wenzelm@38572
    44
            msg match {
wenzelm@39173
    45
              case XML.Elem(Markup(name, atts @ Position.Id_Range(id, raw_range)), args)
wenzelm@39173
    46
              if id == command.id && command.range.contains(command.decode(raw_range)) =>
wenzelm@39173
    47
                val range = command.decode(raw_range)
wenzelm@38872
    48
                val props = Position.purge(atts)
wenzelm@45455
    49
                val info: Text.Markup = Text.Info(range, XML.Elem(Markup(name, props), args))
wenzelm@38723
    50
                state.add_markup(info)
wenzelm@49037
    51
              case XML.Elem(Markup(name, atts), args) =>
wenzelm@49037
    52
                val range = command.proper_range
wenzelm@49037
    53
                val props = Position.purge(atts)
wenzelm@49037
    54
                val info: Text.Markup = Text.Info(range, XML.Elem(Markup(name, props), args))
wenzelm@49037
    55
                state.add_markup(info)
wenzelm@40572
    56
              case _ =>
wenzelm@40572
    57
                // FIXME System.err.println("Ignored report message: " + msg)
wenzelm@40572
    58
                state
wenzelm@38361
    59
            })
wenzelm@38872
    60
        case XML.Elem(Markup(name, atts), body) =>
wenzelm@38872
    61
          atts match {
wenzelm@45666
    62
            case Isabelle_Markup.Serial(i) =>
wenzelm@38887
    63
              val result = XML.Elem(Markup(name, Position.purge(atts)), body)
wenzelm@46152
    64
              val st0 = copy(results = results + (i -> result))
wenzelm@39441
    65
              val st1 =
wenzelm@45709
    66
                if (Protocol.is_tracing(message)) st0
wenzelm@39622
    67
                else
wenzelm@45709
    68
                  (st0 /: Protocol.message_positions(command, message))(
wenzelm@39622
    69
                    (st, range) => st.add_markup(Text.Info(range, result)))
wenzelm@46152
    70
              val st2 = (st1 /: Protocol.message_reports(message))(_ + _)
wenzelm@39622
    71
              st2
wenzelm@38872
    72
            case _ => System.err.println("Ignored message without serial number: " + message); this
wenzelm@38872
    73
          }
wenzelm@38361
    74
      }
wenzelm@38361
    75
  }
wenzelm@38367
    76
wenzelm@38367
    77
wenzelm@45644
    78
  /* make commands */
wenzelm@45644
    79
wenzelm@48745
    80
  type Span = List[Token]
wenzelm@48745
    81
wenzelm@48745
    82
  def apply(id: Document.Command_ID, node_name: Document.Node.Name, span: Span): Command =
wenzelm@45644
    83
  {
wenzelm@45644
    84
    val source: String =
wenzelm@48745
    85
      span match {
wenzelm@45644
    86
        case List(tok) => tok.source
wenzelm@48745
    87
        case _ => span.map(_.source).mkString
wenzelm@45644
    88
      }
wenzelm@45644
    89
wenzelm@48745
    90
    val span1 = new mutable.ListBuffer[Token]
wenzelm@45644
    91
    var i = 0
wenzelm@48745
    92
    for (Token(kind, s) <- span) {
wenzelm@45644
    93
      val n = s.length
wenzelm@45644
    94
      val s1 = source.substring(i, i + n)
wenzelm@48745
    95
      span1 += Token(kind, s1)
wenzelm@45644
    96
      i += n
wenzelm@45644
    97
    }
wenzelm@45644
    98
wenzelm@48745
    99
    new Command(id, node_name, span1.toList, source)
wenzelm@45644
   100
  }
wenzelm@45644
   101
wenzelm@40454
   102
  def unparsed(source: String): Command =
wenzelm@45644
   103
    Command(Document.no_id, Document.Node.Name.empty, List(Token(Token.Kind.UNPARSED, source)))
wenzelm@44384
   104
wenzelm@44384
   105
wenzelm@44384
   106
  /* perspective */
wenzelm@44384
   107
wenzelm@44474
   108
  object Perspective
wenzelm@44474
   109
  {
wenzelm@44474
   110
    val empty: Perspective = Perspective(Nil)
wenzelm@44474
   111
  }
wenzelm@44385
   112
wenzelm@44474
   113
  sealed case class Perspective(commands: List[Command])  // visible commands in canonical order
wenzelm@44385
   114
  {
wenzelm@44474
   115
    def same(that: Perspective): Boolean =
wenzelm@44474
   116
    {
wenzelm@44474
   117
      val cmds1 = this.commands
wenzelm@44474
   118
      val cmds2 = that.commands
wenzelm@48754
   119
      require(!cmds1.exists(_.is_undefined))
wenzelm@48754
   120
      require(!cmds2.exists(_.is_undefined))
wenzelm@44474
   121
      cmds1.length == cmds2.length &&
wenzelm@44474
   122
        (cmds1.iterator zip cmds2.iterator).forall({ case (c1, c2) => c1.id == c2.id })
wenzelm@44474
   123
    }
wenzelm@44385
   124
  }
wenzelm@34318
   125
}
wenzelm@34318
   126
wenzelm@38361
   127
wenzelm@46712
   128
final class Command private(
wenzelm@38150
   129
    val id: Document.Command_ID,
wenzelm@44615
   130
    val node_name: Document.Node.Name,
wenzelm@48745
   131
    val span: Command.Span,
wenzelm@45644
   132
    val source: String)
wenzelm@34451
   133
{
wenzelm@34859
   134
  /* classification */
wenzelm@34500
   135
wenzelm@48754
   136
  def is_undefined: Boolean = id == Document.no_id
wenzelm@48754
   137
  val is_unparsed: Boolean = span.exists(_.is_unparsed)
wenzelm@48754
   138
  val is_unfinished: Boolean = span.exists(_.is_unfinished)
wenzelm@44385
   139
wenzelm@48599
   140
  val is_ignored: Boolean = !span.exists(_.is_proper)
wenzelm@48754
   141
  val is_malformed: Boolean = !is_ignored && (!span.head.is_command || span.exists(_.is_error))
wenzelm@47012
   142
  def is_command: Boolean = !is_ignored && !is_malformed
wenzelm@34859
   143
wenzelm@47012
   144
  def name: String =
wenzelm@48718
   145
    span.find(_.is_command) match { case Some(tok) => tok.source case _ => "" }
wenzelm@47012
   146
wenzelm@37129
   147
  override def toString =
wenzelm@37373
   148
    id + "/" + (if (is_command) name else if (is_ignored) "IGNORED" else "MALFORMED")
wenzelm@34495
   149
wenzelm@34859
   150
wenzelm@34859
   151
  /* source text */
wenzelm@34451
   152
wenzelm@46813
   153
  def length: Int = source.length
wenzelm@46813
   154
  val range: Text.Range = Text.Range(0, length)
wenzelm@46813
   155
wenzelm@46813
   156
  val proper_range: Text.Range =
wenzelm@47459
   157
    Text.Range(0, (length /: span.reverse.iterator.takeWhile(_.is_space))(_ - _.source.length))
wenzelm@46813
   158
wenzelm@38426
   159
  def source(range: Text.Range): String = source.substring(range.start, range.stop)
wenzelm@38572
   160
wenzelm@34859
   161
  lazy val symbol_index = new Symbol.Index(source)
wenzelm@38579
   162
  def decode(i: Text.Offset): Text.Offset = symbol_index.decode(i)
wenzelm@38579
   163
  def decode(r: Text.Range): Text.Range = symbol_index.decode(r)
wenzelm@38370
   164
wenzelm@38370
   165
wenzelm@38370
   166
  /* accumulated results */
wenzelm@38370
   167
wenzelm@38872
   168
  val empty_state: Command.State = Command.State(this)
immler@34676
   169
}