src/Pure/PIDE/command.scala
author wenzelm
Sun Aug 22 20:11:17 2010 +0200 (2010-08-22)
changeset 38581 d503a0912e14
parent 38579 ce46a6f55bce
child 38637 03b27bd0505e
permissions -rw-r--r--
simplified Command.status again, reverting most of e5eed57913d0 (note that more complex information can be represented with full markup reports);
     1 /*  Title:      Pure/PIDE/command.scala
     2     Author:     Fabian Immler, TU Munich
     3     Author:     Makarius
     4 
     5 Prover commands with semantic state.
     6 */
     7 
     8 package isabelle
     9 
    10 
    11 import scala.actors.Actor, Actor._
    12 import scala.collection.mutable
    13 
    14 
    15 object Command
    16 {
    17   /** accumulated results from prover **/
    18 
    19   case class State(
    20     val command: Command,
    21     val status: List[Markup],
    22     val reverse_results: List[XML.Tree],
    23     val markup: Markup_Tree)
    24   {
    25     /* content */
    26 
    27     lazy val results = reverse_results.reverse
    28 
    29     def add_result(result: XML.Tree): State = copy(reverse_results = result :: reverse_results)
    30 
    31     def add_markup(info: Text.Info[Any]): State = copy(markup = markup + info)
    32 
    33     def markup_root_info: Text.Info[Any] =
    34       new Text.Info(command.range,
    35         XML.Elem(Markup(Markup.STATUS, Nil), status.map(XML.Elem(_, Nil))))
    36     def markup_root: Markup_Tree = markup + markup_root_info
    37 
    38 
    39     /* message dispatch */
    40 
    41     def accumulate(message: XML.Tree): Command.State =
    42       message match {
    43         case XML.Elem(Markup(Markup.STATUS, _), body) =>  // FIXME explicit checks!?
    44           copy(status = (for (XML.Elem(markup, _) <- body) yield markup) ::: status)
    45 
    46         case XML.Elem(Markup(Markup.REPORT, _), msgs) =>
    47           (this /: msgs)((state, msg) =>
    48             msg match {
    49               case XML.Elem(Markup(name, atts), args)
    50               if Position.get_id(atts) == Some(command.id) && Position.get_range(atts).isDefined =>
    51                 val range = command.decode(Position.get_range(atts).get)
    52                 val props = atts.filterNot(p => Markup.POSITION_PROPERTIES(p._1))
    53                 val info = Text.Info[Any](range, XML.Elem(Markup(name, props), args))
    54                 add_markup(info)
    55               case _ => System.err.println("Ignored report message: " + msg); state
    56             })
    57         case _ => add_result(message)
    58       }
    59   }
    60 
    61 
    62   /* unparsed dummy commands */
    63 
    64   def unparsed(source: String) =
    65     new Command(Document.NO_ID, List(Token(Token.Kind.UNPARSED, source)))
    66 }
    67 
    68 
    69 class Command(
    70     val id: Document.Command_ID,
    71     val span: List[Token])
    72 {
    73   /* classification */
    74 
    75   def is_command: Boolean = !span.isEmpty && span.head.is_command
    76   def is_ignored: Boolean = span.forall(_.is_ignored)
    77   def is_malformed: Boolean = !is_command && !is_ignored
    78 
    79   def is_unparsed = id == Document.NO_ID
    80 
    81   def name: String = if (is_command) span.head.content else ""
    82   override def toString =
    83     id + "/" + (if (is_command) name else if (is_ignored) "IGNORED" else "MALFORMED")
    84 
    85 
    86   /* source text */
    87 
    88   val source: String = span.map(_.source).mkString
    89   def source(range: Text.Range): String = source.substring(range.start, range.stop)
    90   def length: Int = source.length
    91 
    92   val range: Text.Range = Text.Range(0, length)
    93 
    94   lazy val symbol_index = new Symbol.Index(source)
    95   def decode(i: Text.Offset): Text.Offset = symbol_index.decode(i)
    96   def decode(r: Text.Range): Text.Range = symbol_index.decode(r)
    97 
    98 
    99   /* accumulated results */
   100 
   101   val empty_state: Command.State = Command.State(this, Nil, Nil, Markup_Tree.empty)
   102 }