src/Pure/PIDE/command.scala
author wenzelm
Wed Sep 22 22:39:17 2010 +0200 (2010-09-22 ago)
changeset 39622 53365ba766ac
parent 39441 4110cc1b8f9f
child 40454 2516ea25a54b
permissions -rw-r--r--
Command.accumulate: refrain from adding tracing messages to markup tree -- potential scalability problem;
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@34451
    10
wenzelm@38872
    11
import scala.collection.immutable.SortedMap
wenzelm@38872
    12
wenzelm@38872
    13
wenzelm@34637
    14
object Command
wenzelm@34637
    15
{
wenzelm@38361
    16
  /** accumulated results from prover **/
wenzelm@38361
    17
wenzelm@38362
    18
  case class State(
wenzelm@38361
    19
    val command: Command,
wenzelm@38872
    20
    val status: List[Markup] = Nil,
wenzelm@38872
    21
    val results: SortedMap[Long, XML.Tree] = SortedMap.empty,
wenzelm@38872
    22
    val markup: Markup_Tree = Markup_Tree.empty)
wenzelm@38361
    23
  {
wenzelm@38361
    24
    /* content */
wenzelm@38361
    25
wenzelm@38714
    26
    def add_status(st: Markup): State = copy(status = st :: status)
wenzelm@38714
    27
    def add_markup(info: Text.Info[Any]): State = copy(markup = markup + info)
wenzelm@38872
    28
    def add_result(serial: Long, result: XML.Tree): State =
wenzelm@38872
    29
      copy(results = results + (serial -> result))
wenzelm@38361
    30
wenzelm@38658
    31
    def root_info: Text.Info[Any] =
wenzelm@38581
    32
      new Text.Info(command.range,
wenzelm@38658
    33
        XML.Elem(Markup(Markup.STATUS, Nil), status.reverse.map(XML.Elem(_, Nil))))
wenzelm@38658
    34
    def root_markup: Markup_Tree = markup + root_info
wenzelm@38361
    35
wenzelm@38361
    36
wenzelm@38361
    37
    /* message dispatch */
wenzelm@38361
    38
wenzelm@38872
    39
    def accumulate(message: XML.Elem): Command.State =
wenzelm@38361
    40
      message match {
wenzelm@38714
    41
        case XML.Elem(Markup(Markup.STATUS, _), msgs) =>
wenzelm@38714
    42
          (this /: msgs)((state, msg) =>
wenzelm@38714
    43
            msg match {
wenzelm@38714
    44
              case XML.Elem(markup, Nil) => state.add_status(markup)
wenzelm@38714
    45
              case _ => System.err.println("Ignored status message: " + msg); state
wenzelm@38714
    46
            })
wenzelm@38581
    47
wenzelm@38572
    48
        case XML.Elem(Markup(Markup.REPORT, _), msgs) =>
wenzelm@38572
    49
          (this /: msgs)((state, msg) =>
wenzelm@38572
    50
            msg match {
wenzelm@39173
    51
              case XML.Elem(Markup(name, atts @ Position.Id_Range(id, raw_range)), args)
wenzelm@39173
    52
              if id == command.id && command.range.contains(command.decode(raw_range)) =>
wenzelm@39173
    53
                val range = command.decode(raw_range)
wenzelm@38872
    54
                val props = Position.purge(atts)
wenzelm@39173
    55
                val info = Text.Info[Any](range, XML.Elem(Markup(name, props), args))
wenzelm@38723
    56
                state.add_markup(info)
wenzelm@38572
    57
              case _ => System.err.println("Ignored report message: " + msg); state
wenzelm@38361
    58
            })
wenzelm@38872
    59
        case XML.Elem(Markup(name, atts), body) =>
wenzelm@38872
    60
          atts match {
wenzelm@38872
    61
            case Markup.Serial(i) =>
wenzelm@38887
    62
              val result = XML.Elem(Markup(name, Position.purge(atts)), body)
wenzelm@39622
    63
              val st0 = add_result(i, result)
wenzelm@39441
    64
              val st1 =
wenzelm@39622
    65
                if (Isar_Document.is_tracing(message)) st0
wenzelm@39622
    66
                else
wenzelm@39622
    67
                  (st0 /: Isar_Document.message_positions(command, message))(
wenzelm@39622
    68
                    (st, range) => st.add_markup(Text.Info(range, result)))
wenzelm@39622
    69
              val st2 = (st1 /: Isar_Document.message_reports(message))(_ accumulate _)
wenzelm@39622
    70
              st2
wenzelm@38872
    71
            case _ => System.err.println("Ignored message without serial number: " + message); this
wenzelm@38872
    72
          }
wenzelm@38361
    73
      }
wenzelm@38361
    74
  }
wenzelm@38367
    75
wenzelm@38367
    76
wenzelm@38367
    77
  /* unparsed dummy commands */
wenzelm@38367
    78
wenzelm@38367
    79
  def unparsed(source: String) =
wenzelm@38367
    80
    new Command(Document.NO_ID, List(Token(Token.Kind.UNPARSED, source)))
wenzelm@34318
    81
}
wenzelm@34318
    82
wenzelm@38361
    83
wenzelm@34697
    84
class Command(
wenzelm@38150
    85
    val id: Document.Command_ID,
wenzelm@38373
    86
    val span: List[Token])
wenzelm@34451
    87
{
wenzelm@34859
    88
  /* classification */
wenzelm@34500
    89
wenzelm@36012
    90
  def is_command: Boolean = !span.isEmpty && span.head.is_command
wenzelm@34865
    91
  def is_ignored: Boolean = span.forall(_.is_ignored)
wenzelm@34859
    92
  def is_malformed: Boolean = !is_command && !is_ignored
wenzelm@34859
    93
wenzelm@38367
    94
  def is_unparsed = id == Document.NO_ID
wenzelm@38367
    95
wenzelm@36012
    96
  def name: String = if (is_command) span.head.content else ""
wenzelm@37129
    97
  override def toString =
wenzelm@37373
    98
    id + "/" + (if (is_command) name else if (is_ignored) "IGNORED" else "MALFORMED")
wenzelm@34495
    99
wenzelm@34859
   100
wenzelm@34859
   101
  /* source text */
wenzelm@34451
   102
wenzelm@34859
   103
  val source: String = span.map(_.source).mkString
wenzelm@38426
   104
  def source(range: Text.Range): String = source.substring(range.start, range.stop)
wenzelm@34859
   105
  def length: Int = source.length
wenzelm@38572
   106
wenzelm@38877
   107
  val newlines =
wenzelm@38877
   108
    (0 /: Symbol.iterator(source)) {
wenzelm@38877
   109
      case (n, s) => if (Symbol.is_physical_newline(s)) n + 1 else n }
wenzelm@38877
   110
wenzelm@38479
   111
  val range: Text.Range = Text.Range(0, length)
wenzelm@34855
   112
wenzelm@34859
   113
  lazy val symbol_index = new Symbol.Index(source)
wenzelm@38579
   114
  def decode(i: Text.Offset): Text.Offset = symbol_index.decode(i)
wenzelm@38579
   115
  def decode(r: Text.Range): Text.Range = symbol_index.decode(r)
wenzelm@38370
   116
wenzelm@38370
   117
wenzelm@38370
   118
  /* accumulated results */
wenzelm@38370
   119
wenzelm@38872
   120
  val empty_state: Command.State = Command.State(this)
immler@34676
   121
}