src/Pure/PIDE/command.scala
author wenzelm
Wed Sep 22 22:39:17 2010 +0200 (2010-09-22)
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;
     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               val st0 = add_result(i, result)
    64               val st1 =
    65                 if (Isar_Document.is_tracing(message)) st0
    66                 else
    67                   (st0 /: Isar_Document.message_positions(command, message))(
    68                     (st, range) => st.add_markup(Text.Info(range, result)))
    69               val st2 = (st1 /: Isar_Document.message_reports(message))(_ accumulate _)
    70               st2
    71             case _ => System.err.println("Ignored message without serial number: " + message); this
    72           }
    73       }
    74   }
    75 
    76 
    77   /* unparsed dummy commands */
    78 
    79   def unparsed(source: String) =
    80     new Command(Document.NO_ID, List(Token(Token.Kind.UNPARSED, source)))
    81 }
    82 
    83 
    84 class Command(
    85     val id: Document.Command_ID,
    86     val span: List[Token])
    87 {
    88   /* classification */
    89 
    90   def is_command: Boolean = !span.isEmpty && span.head.is_command
    91   def is_ignored: Boolean = span.forall(_.is_ignored)
    92   def is_malformed: Boolean = !is_command && !is_ignored
    93 
    94   def is_unparsed = id == Document.NO_ID
    95 
    96   def name: String = if (is_command) span.head.content else ""
    97   override def toString =
    98     id + "/" + (if (is_command) name else if (is_ignored) "IGNORED" else "MALFORMED")
    99 
   100 
   101   /* source text */
   102 
   103   val source: String = span.map(_.source).mkString
   104   def source(range: Text.Range): String = source.substring(range.start, range.stop)
   105   def length: Int = source.length
   106 
   107   val newlines =
   108     (0 /: Symbol.iterator(source)) {
   109       case (n, s) => if (Symbol.is_physical_newline(s)) n + 1 else n }
   110 
   111   val range: Text.Range = Text.Range(0, length)
   112 
   113   lazy val symbol_index = new Symbol.Index(source)
   114   def decode(i: Text.Offset): Text.Offset = symbol_index.decode(i)
   115   def decode(r: Text.Range): Text.Range = symbol_index.decode(r)
   116 
   117 
   118   /* accumulated results */
   119 
   120   val empty_state: Command.State = Command.State(this)
   121 }