src/Pure/PIDE/command.scala
author wenzelm
Tue Sep 07 16:51:28 2010 +0200 (2010-09-07)
changeset 39173 ed3946086358
parent 39172 31b95e0da7b7
child 39441 4110cc1b8f9f
permissions -rw-r--r--
Command.State.accumulate: check actual source range;
     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.collection.immutable.SortedMap
    12 
    13 
    14 object Command
    15 {
    16   /** accumulated results from prover **/
    17 
    18   case class State(
    19     val command: Command,
    20     val status: List[Markup] = Nil,
    21     val results: SortedMap[Long, XML.Tree] = SortedMap.empty,
    22     val markup: Markup_Tree = Markup_Tree.empty)
    23   {
    24     /* content */
    25 
    26     def add_status(st: Markup): State = copy(status = st :: status)
    27     def add_markup(info: Text.Info[Any]): State = copy(markup = markup + info)
    28     def add_result(serial: Long, result: XML.Tree): State =
    29       copy(results = results + (serial -> result))
    30 
    31     def root_info: Text.Info[Any] =
    32       new Text.Info(command.range,
    33         XML.Elem(Markup(Markup.STATUS, Nil), status.reverse.map(XML.Elem(_, Nil))))
    34     def root_markup: Markup_Tree = markup + root_info
    35 
    36 
    37     /* message dispatch */
    38 
    39     def accumulate(message: XML.Elem): Command.State =
    40       message match {
    41         case XML.Elem(Markup(Markup.STATUS, _), msgs) =>
    42           (this /: msgs)((state, msg) =>
    43             msg match {
    44               case XML.Elem(markup, Nil) => state.add_status(markup)
    45               case _ => System.err.println("Ignored status message: " + msg); state
    46             })
    47 
    48         case XML.Elem(Markup(Markup.REPORT, _), msgs) =>
    49           (this /: msgs)((state, msg) =>
    50             msg match {
    51               case XML.Elem(Markup(name, atts @ Position.Id_Range(id, raw_range)), args)
    52               if id == command.id && command.range.contains(command.decode(raw_range)) =>
    53                 val range = command.decode(raw_range)
    54                 val props = Position.purge(atts)
    55                 val info = Text.Info[Any](range, XML.Elem(Markup(name, props), args))
    56                 state.add_markup(info)
    57               case _ => System.err.println("Ignored report message: " + msg); state
    58             })
    59         case XML.Elem(Markup(name, atts), body) =>
    60           atts match {
    61             case Markup.Serial(i) =>
    62               val result = XML.Elem(Markup(name, Position.purge(atts)), body)
    63               (add_result(i, result) /: Isar_Document.reported_positions(command, message))(
    64                 (st, range) => st.add_markup(Text.Info(range, result)))
    65             case _ => System.err.println("Ignored message without serial number: " + message); this
    66           }
    67       }
    68   }
    69 
    70 
    71   /* unparsed dummy commands */
    72 
    73   def unparsed(source: String) =
    74     new Command(Document.NO_ID, List(Token(Token.Kind.UNPARSED, source)))
    75 }
    76 
    77 
    78 class Command(
    79     val id: Document.Command_ID,
    80     val span: List[Token])
    81 {
    82   /* classification */
    83 
    84   def is_command: Boolean = !span.isEmpty && span.head.is_command
    85   def is_ignored: Boolean = span.forall(_.is_ignored)
    86   def is_malformed: Boolean = !is_command && !is_ignored
    87 
    88   def is_unparsed = id == Document.NO_ID
    89 
    90   def name: String = if (is_command) span.head.content else ""
    91   override def toString =
    92     id + "/" + (if (is_command) name else if (is_ignored) "IGNORED" else "MALFORMED")
    93 
    94 
    95   /* source text */
    96 
    97   val source: String = span.map(_.source).mkString
    98   def source(range: Text.Range): String = source.substring(range.start, range.stop)
    99   def length: Int = source.length
   100 
   101   val newlines =
   102     (0 /: Symbol.iterator(source)) {
   103       case (n, s) => if (Symbol.is_physical_newline(s)) n + 1 else n }
   104 
   105   val range: Text.Range = Text.Range(0, length)
   106 
   107   lazy val symbol_index = new Symbol.Index(source)
   108   def decode(i: Text.Offset): Text.Offset = symbol_index.decode(i)
   109   def decode(r: Text.Range): Text.Range = symbol_index.decode(r)
   110 
   111 
   112   /* accumulated results */
   113 
   114   val empty_state: Command.State = Command.State(this)
   115 }