src/Pure/PIDE/command.scala
author wenzelm
Sun Aug 22 18:46:16 2010 +0200 (2010-08-22 ago)
changeset 38577 4e4d3ea3725a
parent 38575 80d962964216
child 38579 ce46a6f55bce
permissions -rw-r--r--
renamed Markup_Tree.Node to Text.Info;
Markup_Tree.select: body may depend on full Text.Info, including range;
tuned;
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@34699
    11
import scala.actors.Actor, Actor._
wenzelm@34497
    12
import scala.collection.mutable
wenzelm@34486
    13
wenzelm@34451
    14
wenzelm@34637
    15
object Command
wenzelm@34637
    16
{
wenzelm@38361
    17
  /** accumulated results from prover **/
wenzelm@38361
    18
wenzelm@38362
    19
  case class State(
wenzelm@38361
    20
    val command: Command,
wenzelm@38480
    21
    val status: List[XML.Tree],
wenzelm@38361
    22
    val reverse_results: List[XML.Tree],
wenzelm@38479
    23
    val markup: Markup_Tree)
wenzelm@38361
    24
  {
wenzelm@38361
    25
    /* content */
wenzelm@38361
    26
wenzelm@38362
    27
    lazy val results = reverse_results.reverse
wenzelm@38361
    28
wenzelm@38362
    29
    def add_result(result: XML.Tree): State = copy(reverse_results = result :: reverse_results)
wenzelm@38361
    30
wenzelm@38577
    31
    def add_markup(info: Text.Info[Any]): State = copy(markup = markup + info)
wenzelm@38479
    32
wenzelm@38577
    33
    def markup_root_info: Text.Info[Any] =
wenzelm@38577
    34
      new Text.Info(command.range, XML.Elem(Markup(Markup.STATUS, Nil), status))
wenzelm@38577
    35
    def markup_root: Markup_Tree = markup + markup_root_info
wenzelm@38361
    36
wenzelm@38361
    37
wenzelm@38361
    38
    /* message dispatch */
wenzelm@38361
    39
wenzelm@38361
    40
    def accumulate(message: XML.Tree): Command.State =
wenzelm@38361
    41
      message match {
wenzelm@38480
    42
        case XML.Elem(Markup(Markup.STATUS, _), body) => copy(status = body ::: status)
wenzelm@38572
    43
        case XML.Elem(Markup(Markup.REPORT, _), msgs) =>
wenzelm@38572
    44
          (this /: msgs)((state, msg) =>
wenzelm@38572
    45
            msg match {
wenzelm@38572
    46
              case XML.Elem(Markup(name, atts), args)
wenzelm@38572
    47
              if Position.get_id(atts) == Some(command.id) && Position.get_range(atts).isDefined =>
wenzelm@38572
    48
                val range = command.decode_range(Position.get_range(atts).get)
wenzelm@38572
    49
                val props = atts.filterNot(p => Markup.POSITION_PROPERTIES(p._1))
wenzelm@38577
    50
                val info = Text.Info[Any](range, XML.Elem(Markup(name, props), args))
wenzelm@38577
    51
                add_markup(info)
wenzelm@38572
    52
              case _ => System.err.println("Ignored report message: " + msg); state
wenzelm@38361
    53
            })
wenzelm@38361
    54
        case _ => add_result(message)
wenzelm@38361
    55
      }
wenzelm@38361
    56
  }
wenzelm@38367
    57
wenzelm@38367
    58
wenzelm@38367
    59
  /* unparsed dummy commands */
wenzelm@38367
    60
wenzelm@38367
    61
  def unparsed(source: String) =
wenzelm@38367
    62
    new Command(Document.NO_ID, List(Token(Token.Kind.UNPARSED, source)))
wenzelm@34318
    63
}
wenzelm@34318
    64
wenzelm@38361
    65
wenzelm@34697
    66
class Command(
wenzelm@38150
    67
    val id: Document.Command_ID,
wenzelm@38373
    68
    val span: List[Token])
wenzelm@34451
    69
{
wenzelm@34859
    70
  /* classification */
wenzelm@34500
    71
wenzelm@36012
    72
  def is_command: Boolean = !span.isEmpty && span.head.is_command
wenzelm@34865
    73
  def is_ignored: Boolean = span.forall(_.is_ignored)
wenzelm@34859
    74
  def is_malformed: Boolean = !is_command && !is_ignored
wenzelm@34859
    75
wenzelm@38367
    76
  def is_unparsed = id == Document.NO_ID
wenzelm@38367
    77
wenzelm@36012
    78
  def name: String = if (is_command) span.head.content else ""
wenzelm@37129
    79
  override def toString =
wenzelm@37373
    80
    id + "/" + (if (is_command) name else if (is_ignored) "IGNORED" else "MALFORMED")
wenzelm@34495
    81
wenzelm@34859
    82
wenzelm@34859
    83
  /* source text */
wenzelm@34451
    84
wenzelm@34859
    85
  val source: String = span.map(_.source).mkString
wenzelm@38426
    86
  def source(range: Text.Range): String = source.substring(range.start, range.stop)
wenzelm@34859
    87
  def length: Int = source.length
wenzelm@38572
    88
wenzelm@38479
    89
  val range: Text.Range = Text.Range(0, length)
wenzelm@34855
    90
wenzelm@34859
    91
  lazy val symbol_index = new Symbol.Index(source)
wenzelm@38572
    92
  def decode_range(r: Text.Range): Text.Range = symbol_index.decode(r)
wenzelm@38370
    93
wenzelm@38370
    94
wenzelm@38370
    95
  /* accumulated results */
wenzelm@38370
    96
wenzelm@38479
    97
  val empty_state: Command.State = Command.State(this, Nil, Nil, Markup_Tree.empty)
immler@34676
    98
}