src/Pure/PIDE/command.scala
author wenzelm
Wed Sep 19 17:07:25 2012 +0200 (2012-09-19 ago)
changeset 49445 638cefe3ee99
parent 49418 c451856129cd
child 49466 99ed1f422635
permissions -rw-r--r--
earlier treatment of embedded report/no_report messages (see also 4110cc1b8f9f);
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@49418
    63
              val props = Position.purge(atts)
wenzelm@49418
    64
              val result = XML.Elem(Markup(name, props), body)
wenzelm@49418
    65
              val result_message = XML.Elem(Markup(Isabelle_Markup.message(name), props), body)
wenzelm@49418
    66
wenzelm@49418
    67
              val st0 = copy(results = results + (i -> result_message))
wenzelm@39441
    68
              val st1 =
wenzelm@49418
    69
                if (Protocol.is_tracing(result)) st0
wenzelm@39622
    70
                else
wenzelm@45709
    71
                  (st0 /: Protocol.message_positions(command, message))(
wenzelm@39622
    72
                    (st, range) => st.add_markup(Text.Info(range, result)))
wenzelm@49445
    73
wenzelm@49445
    74
              st1
wenzelm@38872
    75
            case _ => System.err.println("Ignored message without serial number: " + message); this
wenzelm@38872
    76
          }
wenzelm@38361
    77
      }
wenzelm@38361
    78
  }
wenzelm@38367
    79
wenzelm@38367
    80
wenzelm@45644
    81
  /* make commands */
wenzelm@45644
    82
wenzelm@48745
    83
  type Span = List[Token]
wenzelm@48745
    84
wenzelm@49414
    85
  def apply(id: Document.Command_ID, node_name: Document.Node.Name,
wenzelm@49414
    86
    span: Span, markup: Markup_Tree): Command =
wenzelm@45644
    87
  {
wenzelm@45644
    88
    val source: String =
wenzelm@48745
    89
      span match {
wenzelm@45644
    90
        case List(tok) => tok.source
wenzelm@48745
    91
        case _ => span.map(_.source).mkString
wenzelm@45644
    92
      }
wenzelm@45644
    93
wenzelm@48745
    94
    val span1 = new mutable.ListBuffer[Token]
wenzelm@45644
    95
    var i = 0
wenzelm@48745
    96
    for (Token(kind, s) <- span) {
wenzelm@45644
    97
      val n = s.length
wenzelm@45644
    98
      val s1 = source.substring(i, i + n)
wenzelm@48745
    99
      span1 += Token(kind, s1)
wenzelm@45644
   100
      i += n
wenzelm@45644
   101
    }
wenzelm@45644
   102
wenzelm@49414
   103
    new Command(id, node_name, span1.toList, source, markup)
wenzelm@45644
   104
  }
wenzelm@45644
   105
wenzelm@49414
   106
  val empty = Command(Document.no_id, Document.Node.Name.empty, Nil, Markup_Tree.empty)
wenzelm@49414
   107
wenzelm@49414
   108
  def unparsed(id: Document.Command_ID, source: String, markup: Markup_Tree): Command =
wenzelm@49414
   109
    Command(id, Document.Node.Name.empty, List(Token(Token.Kind.UNPARSED, source)), markup)
wenzelm@49414
   110
wenzelm@49414
   111
  def unparsed(source: String): Command = unparsed(Document.no_id, source, Markup_Tree.empty)
wenzelm@44384
   112
wenzelm@49414
   113
  def rich_text(id: Document.Command_ID, body: XML.Body): Command =
wenzelm@49414
   114
  {
wenzelm@49416
   115
    val (text, markup) = XML.content_markup(body)
wenzelm@49414
   116
    unparsed(id, text, markup)
wenzelm@49414
   117
  }
wenzelm@49359
   118
wenzelm@44384
   119
wenzelm@44384
   120
  /* perspective */
wenzelm@44384
   121
wenzelm@44474
   122
  object Perspective
wenzelm@44474
   123
  {
wenzelm@44474
   124
    val empty: Perspective = Perspective(Nil)
wenzelm@44474
   125
  }
wenzelm@44385
   126
wenzelm@44474
   127
  sealed case class Perspective(commands: List[Command])  // visible commands in canonical order
wenzelm@44385
   128
  {
wenzelm@44474
   129
    def same(that: Perspective): Boolean =
wenzelm@44474
   130
    {
wenzelm@44474
   131
      val cmds1 = this.commands
wenzelm@44474
   132
      val cmds2 = that.commands
wenzelm@48754
   133
      require(!cmds1.exists(_.is_undefined))
wenzelm@48754
   134
      require(!cmds2.exists(_.is_undefined))
wenzelm@44474
   135
      cmds1.length == cmds2.length &&
wenzelm@44474
   136
        (cmds1.iterator zip cmds2.iterator).forall({ case (c1, c2) => c1.id == c2.id })
wenzelm@44474
   137
    }
wenzelm@44385
   138
  }
wenzelm@34318
   139
}
wenzelm@34318
   140
wenzelm@38361
   141
wenzelm@46712
   142
final class Command private(
wenzelm@38150
   143
    val id: Document.Command_ID,
wenzelm@44615
   144
    val node_name: Document.Node.Name,
wenzelm@48745
   145
    val span: Command.Span,
wenzelm@49414
   146
    val source: String,
wenzelm@49414
   147
    val init_markup: Markup_Tree)
wenzelm@34451
   148
{
wenzelm@34859
   149
  /* classification */
wenzelm@34500
   150
wenzelm@48754
   151
  def is_undefined: Boolean = id == Document.no_id
wenzelm@48754
   152
  val is_unparsed: Boolean = span.exists(_.is_unparsed)
wenzelm@48754
   153
  val is_unfinished: Boolean = span.exists(_.is_unfinished)
wenzelm@44385
   154
wenzelm@48599
   155
  val is_ignored: Boolean = !span.exists(_.is_proper)
wenzelm@48754
   156
  val is_malformed: Boolean = !is_ignored && (!span.head.is_command || span.exists(_.is_error))
wenzelm@47012
   157
  def is_command: Boolean = !is_ignored && !is_malformed
wenzelm@34859
   158
wenzelm@47012
   159
  def name: String =
wenzelm@48718
   160
    span.find(_.is_command) match { case Some(tok) => tok.source case _ => "" }
wenzelm@47012
   161
wenzelm@37129
   162
  override def toString =
wenzelm@37373
   163
    id + "/" + (if (is_command) name else if (is_ignored) "IGNORED" else "MALFORMED")
wenzelm@34495
   164
wenzelm@34859
   165
wenzelm@34859
   166
  /* source text */
wenzelm@34451
   167
wenzelm@46813
   168
  def length: Int = source.length
wenzelm@46813
   169
  val range: Text.Range = Text.Range(0, length)
wenzelm@46813
   170
wenzelm@46813
   171
  val proper_range: Text.Range =
wenzelm@47459
   172
    Text.Range(0, (length /: span.reverse.iterator.takeWhile(_.is_space))(_ - _.source.length))
wenzelm@46813
   173
wenzelm@38426
   174
  def source(range: Text.Range): String = source.substring(range.start, range.stop)
wenzelm@38572
   175
wenzelm@34859
   176
  lazy val symbol_index = new Symbol.Index(source)
wenzelm@38579
   177
  def decode(i: Text.Offset): Text.Offset = symbol_index.decode(i)
wenzelm@38579
   178
  def decode(r: Text.Range): Text.Range = symbol_index.decode(r)
wenzelm@38370
   179
wenzelm@38370
   180
wenzelm@38370
   181
  /* accumulated results */
wenzelm@38370
   182
wenzelm@49414
   183
  val init_state: Command.State = Command.State(this, markup = init_markup)
immler@34676
   184
}