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