src/Pure/PIDE/command.scala
author wenzelm
Sat Nov 26 17:10:03 2011 +0100 (2011-11-26 ago)
changeset 45644 8634b4e61b88
parent 45455 4f974c0c5c2f
child 45666 d83797ef0d2d
permissions -rw-r--r--
sharing of token source with span source;
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@38361
    26
    /* content */
wenzelm@38361
    27
wenzelm@38714
    28
    def add_status(st: Markup): State = copy(status = st :: status)
wenzelm@45455
    29
    def add_markup(m: Text.Markup): State = copy(markup = markup + m)
wenzelm@38872
    30
    def add_result(serial: Long, result: XML.Tree): State =
wenzelm@38872
    31
      copy(results = results + (serial -> result))
wenzelm@38361
    32
wenzelm@45455
    33
    def root_info: Text.Markup =
wenzelm@45455
    34
      Text.Info(command.range,
wenzelm@38658
    35
        XML.Elem(Markup(Markup.STATUS, Nil), status.reverse.map(XML.Elem(_, Nil))))
wenzelm@38658
    36
    def root_markup: Markup_Tree = markup + root_info
wenzelm@38361
    37
wenzelm@38361
    38
wenzelm@38361
    39
    /* message dispatch */
wenzelm@38361
    40
wenzelm@38872
    41
    def accumulate(message: XML.Elem): Command.State =
wenzelm@38361
    42
      message match {
wenzelm@38714
    43
        case XML.Elem(Markup(Markup.STATUS, _), msgs) =>
wenzelm@38714
    44
          (this /: msgs)((state, msg) =>
wenzelm@38714
    45
            msg match {
wenzelm@38714
    46
              case XML.Elem(markup, Nil) => state.add_status(markup)
wenzelm@38714
    47
              case _ => System.err.println("Ignored status message: " + msg); state
wenzelm@38714
    48
            })
wenzelm@38581
    49
wenzelm@38572
    50
        case XML.Elem(Markup(Markup.REPORT, _), msgs) =>
wenzelm@38572
    51
          (this /: msgs)((state, msg) =>
wenzelm@38572
    52
            msg match {
wenzelm@39173
    53
              case XML.Elem(Markup(name, atts @ Position.Id_Range(id, raw_range)), args)
wenzelm@39173
    54
              if id == command.id && command.range.contains(command.decode(raw_range)) =>
wenzelm@39173
    55
                val range = command.decode(raw_range)
wenzelm@38872
    56
                val props = Position.purge(atts)
wenzelm@45455
    57
                val info: Text.Markup = Text.Info(range, XML.Elem(Markup(name, props), args))
wenzelm@38723
    58
                state.add_markup(info)
wenzelm@40572
    59
              case _ =>
wenzelm@40572
    60
                // FIXME System.err.println("Ignored report message: " + msg)
wenzelm@40572
    61
                state
wenzelm@38361
    62
            })
wenzelm@38872
    63
        case XML.Elem(Markup(name, atts), body) =>
wenzelm@38872
    64
          atts match {
wenzelm@38872
    65
            case Markup.Serial(i) =>
wenzelm@38887
    66
              val result = XML.Elem(Markup(name, Position.purge(atts)), body)
wenzelm@39622
    67
              val st0 = add_result(i, result)
wenzelm@39441
    68
              val st1 =
wenzelm@39622
    69
                if (Isar_Document.is_tracing(message)) st0
wenzelm@39622
    70
                else
wenzelm@39622
    71
                  (st0 /: Isar_Document.message_positions(command, message))(
wenzelm@39622
    72
                    (st, range) => st.add_markup(Text.Info(range, result)))
wenzelm@39622
    73
              val st2 = (st1 /: Isar_Document.message_reports(message))(_ accumulate _)
wenzelm@39622
    74
              st2
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@45644
    83
  def apply(id: Document.Command_ID, node_name: Document.Node.Name, toks: List[Token]): Command =
wenzelm@45644
    84
  {
wenzelm@45644
    85
    val source: String =
wenzelm@45644
    86
      toks match {
wenzelm@45644
    87
        case List(tok) => tok.source
wenzelm@45644
    88
        case _ => toks.map(_.source).mkString
wenzelm@45644
    89
      }
wenzelm@45644
    90
wenzelm@45644
    91
    val span = new mutable.ListBuffer[Token]
wenzelm@45644
    92
    var i = 0
wenzelm@45644
    93
    for (Token(kind, s) <- toks) {
wenzelm@45644
    94
      val n = s.length
wenzelm@45644
    95
      val s1 = source.substring(i, i + n)
wenzelm@45644
    96
      span += Token(kind, s1)
wenzelm@45644
    97
      i += n
wenzelm@45644
    98
    }
wenzelm@45644
    99
wenzelm@45644
   100
    new Command(id, node_name, span.toList, source)
wenzelm@45644
   101
  }
wenzelm@45644
   102
wenzelm@45644
   103
  def apply(node_name: Document.Node.Name, toks: List[Token]): Command =
wenzelm@45644
   104
    Command(Document.no_id, node_name, toks)
wenzelm@38367
   105
wenzelm@40454
   106
  def unparsed(source: String): Command =
wenzelm@45644
   107
    Command(Document.no_id, Document.Node.Name.empty, List(Token(Token.Kind.UNPARSED, source)))
wenzelm@44384
   108
wenzelm@44384
   109
wenzelm@44384
   110
  /* perspective */
wenzelm@44384
   111
wenzelm@44474
   112
  object Perspective
wenzelm@44474
   113
  {
wenzelm@44474
   114
    val empty: Perspective = Perspective(Nil)
wenzelm@44474
   115
  }
wenzelm@44385
   116
wenzelm@44474
   117
  sealed case class Perspective(commands: List[Command])  // visible commands in canonical order
wenzelm@44385
   118
  {
wenzelm@44474
   119
    def same(that: Perspective): Boolean =
wenzelm@44474
   120
    {
wenzelm@44474
   121
      val cmds1 = this.commands
wenzelm@44474
   122
      val cmds2 = that.commands
wenzelm@44474
   123
      require(cmds1.forall(_.is_defined))
wenzelm@44474
   124
      require(cmds2.forall(_.is_defined))
wenzelm@44474
   125
      cmds1.length == cmds2.length &&
wenzelm@44474
   126
        (cmds1.iterator zip cmds2.iterator).forall({ case (c1, c2) => c1.id == c2.id })
wenzelm@44474
   127
    }
wenzelm@44385
   128
  }
wenzelm@34318
   129
}
wenzelm@34318
   130
wenzelm@38361
   131
wenzelm@45644
   132
class Command private(
wenzelm@38150
   133
    val id: Document.Command_ID,
wenzelm@44615
   134
    val node_name: Document.Node.Name,
wenzelm@45644
   135
    val span: List[Token],
wenzelm@45644
   136
    val source: String)
wenzelm@34451
   137
{
wenzelm@34859
   138
  /* classification */
wenzelm@34500
   139
wenzelm@44385
   140
  def is_defined: Boolean = id != Document.no_id
wenzelm@44385
   141
wenzelm@36012
   142
  def is_command: Boolean = !span.isEmpty && span.head.is_command
wenzelm@34865
   143
  def is_ignored: Boolean = span.forall(_.is_ignored)
wenzelm@34859
   144
  def is_malformed: Boolean = !is_command && !is_ignored
wenzelm@34859
   145
wenzelm@36012
   146
  def name: String = if (is_command) span.head.content else ""
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@38426
   153
  def source(range: Text.Range): String = source.substring(range.start, range.stop)
wenzelm@34859
   154
  def length: Int = source.length
wenzelm@38572
   155
wenzelm@38877
   156
  val newlines =
wenzelm@38877
   157
    (0 /: Symbol.iterator(source)) {
wenzelm@38877
   158
      case (n, s) => if (Symbol.is_physical_newline(s)) n + 1 else n }
wenzelm@38877
   159
wenzelm@38479
   160
  val range: Text.Range = Text.Range(0, length)
wenzelm@34855
   161
wenzelm@34859
   162
  lazy val symbol_index = new Symbol.Index(source)
wenzelm@38579
   163
  def decode(i: Text.Offset): Text.Offset = symbol_index.decode(i)
wenzelm@38579
   164
  def decode(r: Text.Range): Text.Range = symbol_index.decode(r)
wenzelm@38370
   165
wenzelm@38370
   166
wenzelm@38370
   167
  /* accumulated results */
wenzelm@38370
   168
wenzelm@38872
   169
  val empty_state: Command.State = Command.State(this)
immler@34676
   170
}