src/Pure/PIDE/command.scala
author wenzelm
Tue Mar 13 21:17:37 2012 +0100 (2012-03-13 ago)
changeset 46910 3e068ef04b42
parent 46813 bb7280848c99
child 47012 0e246130486b
permissions -rw-r--r--
clarified command state -- markup within proper_range, excluding trailing whitespace;
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@40572
    51
              case _ =>
wenzelm@40572
    52
                // FIXME System.err.println("Ignored report message: " + msg)
wenzelm@40572
    53
                state
wenzelm@38361
    54
            })
wenzelm@38872
    55
        case XML.Elem(Markup(name, atts), body) =>
wenzelm@38872
    56
          atts match {
wenzelm@45666
    57
            case Isabelle_Markup.Serial(i) =>
wenzelm@38887
    58
              val result = XML.Elem(Markup(name, Position.purge(atts)), body)
wenzelm@46152
    59
              val st0 = copy(results = results + (i -> result))
wenzelm@39441
    60
              val st1 =
wenzelm@45709
    61
                if (Protocol.is_tracing(message)) st0
wenzelm@39622
    62
                else
wenzelm@45709
    63
                  (st0 /: Protocol.message_positions(command, message))(
wenzelm@39622
    64
                    (st, range) => st.add_markup(Text.Info(range, result)))
wenzelm@46152
    65
              val st2 = (st1 /: Protocol.message_reports(message))(_ + _)
wenzelm@39622
    66
              st2
wenzelm@38872
    67
            case _ => System.err.println("Ignored message without serial number: " + message); this
wenzelm@38872
    68
          }
wenzelm@38361
    69
      }
wenzelm@38361
    70
  }
wenzelm@38367
    71
wenzelm@38367
    72
wenzelm@45644
    73
  /* make commands */
wenzelm@45644
    74
wenzelm@45644
    75
  def apply(id: Document.Command_ID, node_name: Document.Node.Name, toks: List[Token]): Command =
wenzelm@45644
    76
  {
wenzelm@45644
    77
    val source: String =
wenzelm@45644
    78
      toks match {
wenzelm@45644
    79
        case List(tok) => tok.source
wenzelm@45644
    80
        case _ => toks.map(_.source).mkString
wenzelm@45644
    81
      }
wenzelm@45644
    82
wenzelm@45644
    83
    val span = new mutable.ListBuffer[Token]
wenzelm@45644
    84
    var i = 0
wenzelm@45644
    85
    for (Token(kind, s) <- toks) {
wenzelm@45644
    86
      val n = s.length
wenzelm@45644
    87
      val s1 = source.substring(i, i + n)
wenzelm@45644
    88
      span += Token(kind, s1)
wenzelm@45644
    89
      i += n
wenzelm@45644
    90
    }
wenzelm@45644
    91
wenzelm@45644
    92
    new Command(id, node_name, span.toList, source)
wenzelm@45644
    93
  }
wenzelm@45644
    94
wenzelm@45644
    95
  def apply(node_name: Document.Node.Name, toks: List[Token]): Command =
wenzelm@45644
    96
    Command(Document.no_id, node_name, toks)
wenzelm@38367
    97
wenzelm@40454
    98
  def unparsed(source: String): Command =
wenzelm@45644
    99
    Command(Document.no_id, Document.Node.Name.empty, List(Token(Token.Kind.UNPARSED, source)))
wenzelm@44384
   100
wenzelm@44384
   101
wenzelm@44384
   102
  /* perspective */
wenzelm@44384
   103
wenzelm@44474
   104
  object Perspective
wenzelm@44474
   105
  {
wenzelm@44474
   106
    val empty: Perspective = Perspective(Nil)
wenzelm@44474
   107
  }
wenzelm@44385
   108
wenzelm@44474
   109
  sealed case class Perspective(commands: List[Command])  // visible commands in canonical order
wenzelm@44385
   110
  {
wenzelm@44474
   111
    def same(that: Perspective): Boolean =
wenzelm@44474
   112
    {
wenzelm@44474
   113
      val cmds1 = this.commands
wenzelm@44474
   114
      val cmds2 = that.commands
wenzelm@44474
   115
      require(cmds1.forall(_.is_defined))
wenzelm@44474
   116
      require(cmds2.forall(_.is_defined))
wenzelm@44474
   117
      cmds1.length == cmds2.length &&
wenzelm@44474
   118
        (cmds1.iterator zip cmds2.iterator).forall({ case (c1, c2) => c1.id == c2.id })
wenzelm@44474
   119
    }
wenzelm@44385
   120
  }
wenzelm@34318
   121
}
wenzelm@34318
   122
wenzelm@38361
   123
wenzelm@46712
   124
final class Command private(
wenzelm@38150
   125
    val id: Document.Command_ID,
wenzelm@44615
   126
    val node_name: Document.Node.Name,
wenzelm@45644
   127
    val span: List[Token],
wenzelm@45644
   128
    val source: String)
wenzelm@34451
   129
{
wenzelm@34859
   130
  /* classification */
wenzelm@34500
   131
wenzelm@44385
   132
  def is_defined: Boolean = id != Document.no_id
wenzelm@44385
   133
wenzelm@36012
   134
  def is_command: Boolean = !span.isEmpty && span.head.is_command
wenzelm@34865
   135
  def is_ignored: Boolean = span.forall(_.is_ignored)
wenzelm@34859
   136
  def is_malformed: Boolean = !is_command && !is_ignored
wenzelm@34859
   137
wenzelm@36012
   138
  def name: String = if (is_command) span.head.content else ""
wenzelm@37129
   139
  override def toString =
wenzelm@37373
   140
    id + "/" + (if (is_command) name else if (is_ignored) "IGNORED" else "MALFORMED")
wenzelm@34495
   141
wenzelm@34859
   142
wenzelm@34859
   143
  /* source text */
wenzelm@34451
   144
wenzelm@46813
   145
  def length: Int = source.length
wenzelm@46813
   146
  val range: Text.Range = Text.Range(0, length)
wenzelm@46813
   147
wenzelm@46813
   148
  val proper_range: Text.Range =
wenzelm@46813
   149
    Text.Range(0, (length /: span.reverse.iterator.takeWhile(_.is_ignored))(_ - _.source.length))
wenzelm@46813
   150
wenzelm@38426
   151
  def source(range: Text.Range): String = source.substring(range.start, range.stop)
wenzelm@38572
   152
wenzelm@38877
   153
  val newlines =
wenzelm@38877
   154
    (0 /: Symbol.iterator(source)) {
wenzelm@38877
   155
      case (n, s) => if (Symbol.is_physical_newline(s)) n + 1 else n }
wenzelm@38877
   156
wenzelm@34859
   157
  lazy val symbol_index = new Symbol.Index(source)
wenzelm@38579
   158
  def decode(i: Text.Offset): Text.Offset = symbol_index.decode(i)
wenzelm@38579
   159
  def decode(r: Text.Range): Text.Range = symbol_index.decode(r)
wenzelm@38370
   160
wenzelm@38370
   161
wenzelm@38370
   162
  /* accumulated results */
wenzelm@38370
   163
wenzelm@38872
   164
  val empty_state: Command.State = Command.State(this)
immler@34676
   165
}