src/Pure/PIDE/command.scala
author wenzelm
Tue Aug 24 20:36:48 2010 +0200 (2010-08-24)
changeset 38658 20d82e98bcd7
parent 38637 03b27bd0505e
child 38714 31da698fc4e5
permissions -rw-r--r--
tuned root markup;
     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 object Command
    12 {
    13   /** accumulated results from prover **/
    14 
    15   case class State(
    16     val command: Command,
    17     val status: List[Markup],
    18     val reverse_results: List[XML.Tree],
    19     val markup: Markup_Tree)
    20   {
    21     /* content */
    22 
    23     lazy val results = reverse_results.reverse
    24 
    25     def add_result(result: XML.Tree): State = copy(reverse_results = result :: reverse_results)
    26 
    27     def add_markup(info: Text.Info[Any]): State = copy(markup = markup + info)
    28 
    29     def root_info: Text.Info[Any] =
    30       new Text.Info(command.range,
    31         XML.Elem(Markup(Markup.STATUS, Nil), status.reverse.map(XML.Elem(_, Nil))))
    32     def root_markup: Markup_Tree = markup + root_info
    33 
    34 
    35     /* message dispatch */
    36 
    37     def accumulate(message: XML.Tree): Command.State =
    38       message match {
    39         case XML.Elem(Markup(Markup.STATUS, _), body) =>  // FIXME explicit body check!?
    40           copy(status = (for (XML.Elem(markup, _) <- body) yield markup) ::: status)
    41 
    42         case XML.Elem(Markup(Markup.REPORT, _), msgs) =>
    43           (this /: msgs)((state, msg) =>
    44             msg match {
    45               case XML.Elem(Markup(name, atts), args)
    46               if Position.get_id(atts) == Some(command.id) && Position.get_range(atts).isDefined =>
    47                 val range = command.decode(Position.get_range(atts).get)
    48                 val props = atts.filterNot(p => Markup.POSITION_PROPERTIES(p._1))
    49                 val info = Text.Info[Any](range, XML.Elem(Markup(name, props), args))
    50                 add_markup(info)
    51               case _ => System.err.println("Ignored report message: " + msg); state
    52             })
    53         case _ => add_result(message)
    54       }
    55   }
    56 
    57 
    58   /* unparsed dummy commands */
    59 
    60   def unparsed(source: String) =
    61     new Command(Document.NO_ID, List(Token(Token.Kind.UNPARSED, source)))
    62 }
    63 
    64 
    65 class Command(
    66     val id: Document.Command_ID,
    67     val span: List[Token])
    68 {
    69   /* classification */
    70 
    71   def is_command: Boolean = !span.isEmpty && span.head.is_command
    72   def is_ignored: Boolean = span.forall(_.is_ignored)
    73   def is_malformed: Boolean = !is_command && !is_ignored
    74 
    75   def is_unparsed = id == Document.NO_ID
    76 
    77   def name: String = if (is_command) span.head.content else ""
    78   override def toString =
    79     id + "/" + (if (is_command) name else if (is_ignored) "IGNORED" else "MALFORMED")
    80 
    81 
    82   /* source text */
    83 
    84   val source: String = span.map(_.source).mkString
    85   def source(range: Text.Range): String = source.substring(range.start, range.stop)
    86   def length: Int = source.length
    87 
    88   val range: Text.Range = Text.Range(0, length)
    89 
    90   lazy val symbol_index = new Symbol.Index(source)
    91   def decode(i: Text.Offset): Text.Offset = symbol_index.decode(i)
    92   def decode(r: Text.Range): Text.Range = symbol_index.decode(r)
    93 
    94 
    95   /* accumulated results */
    96 
    97   val empty_state: Command.State = Command.State(this, Nil, Nil, Markup_Tree.empty)
    98 }