src/Pure/PIDE/command.scala
author wenzelm
Tue Aug 23 12:20:12 2011 +0200 (2011-08-23 ago)
changeset 44385 e7fdb008aa7d
parent 44384 8f6054a63f96
child 44474 681447a9ffe5
permissions -rw-r--r--
propagate editor perspective through document model;
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@38872
    12
import scala.collection.immutable.SortedMap
wenzelm@38872
    13
wenzelm@38872
    14
wenzelm@34637
    15
object Command
wenzelm@34637
    16
{
wenzelm@38361
    17
  /** accumulated results from prover **/
wenzelm@38361
    18
wenzelm@43714
    19
  sealed case class State(
wenzelm@38361
    20
    val command: Command,
wenzelm@38872
    21
    val status: List[Markup] = Nil,
wenzelm@38872
    22
    val results: SortedMap[Long, XML.Tree] = SortedMap.empty,
wenzelm@38872
    23
    val markup: Markup_Tree = Markup_Tree.empty)
wenzelm@38361
    24
  {
wenzelm@38361
    25
    /* content */
wenzelm@38361
    26
wenzelm@38714
    27
    def add_status(st: Markup): State = copy(status = st :: status)
wenzelm@38714
    28
    def add_markup(info: Text.Info[Any]): State = copy(markup = markup + info)
wenzelm@38872
    29
    def add_result(serial: Long, result: XML.Tree): State =
wenzelm@38872
    30
      copy(results = results + (serial -> result))
wenzelm@38361
    31
wenzelm@38658
    32
    def root_info: Text.Info[Any] =
wenzelm@38581
    33
      new Text.Info(command.range,
wenzelm@38658
    34
        XML.Elem(Markup(Markup.STATUS, Nil), status.reverse.map(XML.Elem(_, Nil))))
wenzelm@38658
    35
    def root_markup: Markup_Tree = markup + root_info
wenzelm@38361
    36
wenzelm@38361
    37
wenzelm@38361
    38
    /* message dispatch */
wenzelm@38361
    39
wenzelm@38872
    40
    def accumulate(message: XML.Elem): Command.State =
wenzelm@38361
    41
      message match {
wenzelm@38714
    42
        case XML.Elem(Markup(Markup.STATUS, _), msgs) =>
wenzelm@38714
    43
          (this /: msgs)((state, msg) =>
wenzelm@38714
    44
            msg match {
wenzelm@38714
    45
              case XML.Elem(markup, Nil) => state.add_status(markup)
wenzelm@38714
    46
              case _ => System.err.println("Ignored status message: " + msg); state
wenzelm@38714
    47
            })
wenzelm@38581
    48
wenzelm@38572
    49
        case XML.Elem(Markup(Markup.REPORT, _), msgs) =>
wenzelm@38572
    50
          (this /: msgs)((state, msg) =>
wenzelm@38572
    51
            msg match {
wenzelm@39173
    52
              case XML.Elem(Markup(name, atts @ Position.Id_Range(id, raw_range)), args)
wenzelm@39173
    53
              if id == command.id && command.range.contains(command.decode(raw_range)) =>
wenzelm@39173
    54
                val range = command.decode(raw_range)
wenzelm@38872
    55
                val props = Position.purge(atts)
wenzelm@39173
    56
                val info = Text.Info[Any](range, XML.Elem(Markup(name, props), args))
wenzelm@38723
    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@38872
    64
            case Markup.Serial(i) =>
wenzelm@38887
    65
              val result = XML.Elem(Markup(name, Position.purge(atts)), body)
wenzelm@39622
    66
              val st0 = add_result(i, result)
wenzelm@39441
    67
              val st1 =
wenzelm@39622
    68
                if (Isar_Document.is_tracing(message)) st0
wenzelm@39622
    69
                else
wenzelm@39622
    70
                  (st0 /: Isar_Document.message_positions(command, message))(
wenzelm@39622
    71
                    (st, range) => st.add_markup(Text.Info(range, result)))
wenzelm@39622
    72
              val st2 = (st1 /: Isar_Document.message_reports(message))(_ accumulate _)
wenzelm@39622
    73
              st2
wenzelm@38872
    74
            case _ => System.err.println("Ignored message without serial number: " + message); this
wenzelm@38872
    75
          }
wenzelm@38361
    76
      }
wenzelm@38361
    77
  }
wenzelm@38367
    78
wenzelm@38367
    79
wenzelm@40454
    80
  /* dummy commands */
wenzelm@38367
    81
wenzelm@40454
    82
  def unparsed(source: String): Command =
wenzelm@43662
    83
    new Command(Document.no_id, List(Token(Token.Kind.UNPARSED, source)))
wenzelm@40454
    84
wenzelm@40454
    85
  def span(toks: List[Token]): Command =
wenzelm@43662
    86
    new Command(Document.no_id, toks)
wenzelm@44384
    87
wenzelm@44384
    88
wenzelm@44384
    89
  /* perspective */
wenzelm@44384
    90
wenzelm@44384
    91
  type Perspective = List[Command]  // visible commands in canonical order
wenzelm@44385
    92
wenzelm@44385
    93
  def equal_perspective(p1: Perspective, p2: Perspective): Boolean =
wenzelm@44385
    94
  {
wenzelm@44385
    95
    require(p1.forall(_.is_defined))
wenzelm@44385
    96
    require(p2.forall(_.is_defined))
wenzelm@44385
    97
    p1.length == p2.length &&
wenzelm@44385
    98
      (p1.iterator zip p2.iterator).forall({ case (c1, c2) => c1.id == c2.id })
wenzelm@44385
    99
  }
wenzelm@34318
   100
}
wenzelm@34318
   101
wenzelm@38361
   102
wenzelm@34697
   103
class Command(
wenzelm@38150
   104
    val id: Document.Command_ID,
wenzelm@38373
   105
    val span: List[Token])
wenzelm@34451
   106
{
wenzelm@34859
   107
  /* classification */
wenzelm@34500
   108
wenzelm@44385
   109
  def is_defined: Boolean = id != Document.no_id
wenzelm@44385
   110
wenzelm@36012
   111
  def is_command: Boolean = !span.isEmpty && span.head.is_command
wenzelm@34865
   112
  def is_ignored: Boolean = span.forall(_.is_ignored)
wenzelm@34859
   113
  def is_malformed: Boolean = !is_command && !is_ignored
wenzelm@34859
   114
wenzelm@36012
   115
  def name: String = if (is_command) span.head.content else ""
wenzelm@37129
   116
  override def toString =
wenzelm@37373
   117
    id + "/" + (if (is_command) name else if (is_ignored) "IGNORED" else "MALFORMED")
wenzelm@34495
   118
wenzelm@34859
   119
wenzelm@34859
   120
  /* source text */
wenzelm@34451
   121
wenzelm@34859
   122
  val source: String = span.map(_.source).mkString
wenzelm@38426
   123
  def source(range: Text.Range): String = source.substring(range.start, range.stop)
wenzelm@34859
   124
  def length: Int = source.length
wenzelm@38572
   125
wenzelm@38877
   126
  val newlines =
wenzelm@38877
   127
    (0 /: Symbol.iterator(source)) {
wenzelm@38877
   128
      case (n, s) => if (Symbol.is_physical_newline(s)) n + 1 else n }
wenzelm@38877
   129
wenzelm@38479
   130
  val range: Text.Range = Text.Range(0, length)
wenzelm@34855
   131
wenzelm@34859
   132
  lazy val symbol_index = new Symbol.Index(source)
wenzelm@38579
   133
  def decode(i: Text.Offset): Text.Offset = symbol_index.decode(i)
wenzelm@38579
   134
  def decode(r: Text.Range): Text.Range = symbol_index.decode(r)
wenzelm@38370
   135
wenzelm@38370
   136
wenzelm@38370
   137
  /* accumulated results */
wenzelm@38370
   138
wenzelm@38872
   139
  val empty_state: Command.State = Command.State(this)
immler@34676
   140
}