src/Pure/PIDE/command.scala
author wenzelm
Tue Apr 08 19:17:28 2014 +0200 (2014-04-08 ago)
changeset 56470 8eda56033203
parent 56469 ffc94a271633
child 56473 5b5c750e9763
permissions -rw-r--r--
accumulate markup reports for "other" command ids, which are later retargeted and merged for rendering (in erratic order);
     1 /*  Title:      Pure/PIDE/command.scala
     2     Author:     Fabian Immler, TU Munich
     3     Author:     Makarius
     4 
     5 Prover commands with accumulated results from execution.
     6 */
     7 
     8 package isabelle
     9 
    10 
    11 import scala.collection.mutable
    12 import scala.collection.immutable.SortedMap
    13 
    14 
    15 object Command
    16 {
    17   type Edit = (Option[Command], Option[Command])
    18   type Blob = Exn.Result[(Document.Node.Name, Option[(SHA1.Digest, Text.Chunk.File)])]
    19 
    20 
    21 
    22   /** accumulated results from prover **/
    23 
    24   /* results */
    25 
    26   object Results
    27   {
    28     type Entry = (Long, XML.Tree)
    29     val empty = new Results(SortedMap.empty)
    30     def make(es: List[Results.Entry]): Results = (empty /: es)(_ + _)
    31     def merge(rs: List[Results]): Results = (empty /: rs)(_ ++ _)
    32   }
    33 
    34   final class Results private(private val rep: SortedMap[Long, XML.Tree])
    35   {
    36     def defined(serial: Long): Boolean = rep.isDefinedAt(serial)
    37     def get(serial: Long): Option[XML.Tree] = rep.get(serial)
    38     def iterator: Iterator[Results.Entry] = rep.iterator
    39 
    40     def + (entry: Results.Entry): Results =
    41       if (defined(entry._1)) this
    42       else new Results(rep + entry)
    43 
    44     def ++ (other: Results): Results =
    45       if (this eq other) this
    46       else if (rep.isEmpty) other
    47       else (this /: other.iterator)(_ + _)
    48 
    49     override def hashCode: Int = rep.hashCode
    50     override def equals(that: Any): Boolean =
    51       that match {
    52         case other: Results => rep == other.rep
    53         case _ => false
    54       }
    55     override def toString: String = iterator.mkString("Results(", ", ", ")")
    56   }
    57 
    58 
    59   /* markup */
    60 
    61   object Markup_Index
    62   {
    63     val markup: Markup_Index = Markup_Index(false, Text.Chunk.Default)
    64   }
    65 
    66   sealed case class Markup_Index(status: Boolean, chunk_name: Text.Chunk.Name)
    67 
    68   object Markups
    69   {
    70     val empty: Markups = new Markups(Map.empty)
    71 
    72     def init(markup: Markup_Tree): Markups =
    73       new Markups(Map(Markup_Index.markup -> markup))
    74   }
    75 
    76   final class Markups private(private val rep: Map[Markup_Index, Markup_Tree])
    77   {
    78     def apply(index: Markup_Index): Markup_Tree =
    79       rep.getOrElse(index, Markup_Tree.empty)
    80 
    81     def add(index: Markup_Index, markup: Text.Markup): Markups =
    82       new Markups(rep + (index -> (this(index) + markup)))
    83 
    84     def other_id_iterator: Iterator[Document_ID.Generic] =
    85       for (Markup_Index(_, Text.Chunk.Id(other_id)) <- rep.keysIterator)
    86         yield other_id
    87 
    88     def retarget(other_id: Document_ID.Generic): Markups =
    89       new Markups(
    90         (for {
    91           (Markup_Index(status, Text.Chunk.Id(id)), markup) <- rep.iterator
    92           if other_id == id
    93         } yield (Markup_Index(status, Text.Chunk.Default), markup)).toMap)
    94 
    95     override def hashCode: Int = rep.hashCode
    96     override def equals(that: Any): Boolean =
    97       that match {
    98         case other: Markups => rep == other.rep
    99         case _ => false
   100       }
   101     override def toString: String = rep.iterator.mkString("Markups(", ", ", ")")
   102   }
   103 
   104 
   105   /* state */
   106 
   107   object State
   108   {
   109     def merge_results(states: List[State]): Command.Results =
   110       Results.merge(states.map(_.results))
   111 
   112     def merge_markup(states: List[State], index: Markup_Index,
   113         range: Text.Range, elements: Document.Elements): Markup_Tree =
   114       Markup_Tree.merge(states.map(_.markup(index)), range, elements)
   115   }
   116 
   117   sealed case class State(
   118     command: Command,
   119     status: List[Markup] = Nil,
   120     results: Results = Results.empty,
   121     markups: Markups = Markups.empty)
   122   {
   123     lazy val protocol_status: Protocol.Status =
   124     {
   125       val warnings =
   126         if (results.iterator.exists(p => Protocol.is_warning(p._2)))
   127           List(Markup(Markup.WARNING, Nil))
   128         else Nil
   129       val errors =
   130         if (results.iterator.exists(p => Protocol.is_error(p._2)))
   131           List(Markup(Markup.ERROR, Nil))
   132         else Nil
   133       Protocol.Status.make((warnings ::: errors ::: status).iterator)
   134     }
   135 
   136     def markup(index: Markup_Index): Markup_Tree = markups(index)
   137 
   138     def retarget(other_command: Command): State =
   139       new State(other_command, Nil, Results.empty, markups.retarget(other_command.id))
   140 
   141 
   142     def eq_content(other: State): Boolean =
   143       command.source == other.command.source &&
   144       status == other.status &&
   145       results == other.results &&
   146       markups == other.markups
   147 
   148     private def add_status(st: Markup): State =
   149       copy(status = st :: status)
   150 
   151     private def add_markup(status: Boolean, chunk_name: Text.Chunk.Name, m: Text.Markup): State =
   152     {
   153       val markups1 =
   154         if (status || Protocol.liberal_status_elements(m.info.name))
   155           markups.add(Markup_Index(true, chunk_name), m)
   156         else markups
   157       copy(markups = markups1.add(Markup_Index(false, chunk_name), m))
   158     }
   159 
   160     def accumulate(
   161         self_id: Document_ID.Generic => Boolean,
   162         other_id: Document_ID.Generic => Option[(Text.Chunk.Id, Text.Chunk)],
   163         message: XML.Elem): State =
   164       message match {
   165         case XML.Elem(Markup(Markup.STATUS, _), msgs) =>
   166           (this /: msgs)((state, msg) =>
   167             msg match {
   168               case elem @ XML.Elem(markup, Nil) =>
   169                 state.
   170                   add_status(markup).
   171                   add_markup(true, Text.Chunk.Default, Text.Info(command.proper_range, elem))
   172               case _ =>
   173                 System.err.println("Ignored status message: " + msg)
   174                 state
   175             })
   176 
   177         case XML.Elem(Markup(Markup.REPORT, _), msgs) =>
   178           (this /: msgs)((state, msg) =>
   179             {
   180               def bad(): Unit = System.err.println("Ignored report message: " + msg)
   181 
   182               msg match {
   183                 case XML.Elem(
   184                     Markup(name, atts @ Position.Reported(id, chunk_name, symbol_range)), args) =>
   185 
   186                   val target =
   187                     if (self_id(id) && command.chunks.isDefinedAt(chunk_name))
   188                       Some((chunk_name, command.chunks(chunk_name)))
   189                     else if (chunk_name == Text.Chunk.Default) other_id(id)
   190                     else None
   191 
   192                   target match {
   193                     case Some((target_name, target_chunk)) =>
   194                       target_chunk.incorporate(symbol_range) match {
   195                         case Some(range) =>
   196                           val props = Position.purge(atts)
   197                           val info = Text.Info(range, XML.Elem(Markup(name, props), args))
   198                           state.add_markup(false, target_name, info)
   199                         case None => bad(); state
   200                       }
   201                     case None => /* FIXME bad(); */ state
   202                   }
   203 
   204                 case XML.Elem(Markup(name, atts), args)
   205                 if !atts.exists({ case (a, _) => Markup.POSITION_PROPERTIES(a) }) =>
   206                   val range = command.proper_range
   207                   val props = Position.purge(atts)
   208                   val info: Text.Markup = Text.Info(range, XML.Elem(Markup(name, props), args))
   209                   state.add_markup(false, Text.Chunk.Default, info)
   210 
   211                 case _ => bad(); state
   212               }
   213             })
   214         case XML.Elem(Markup(name, props), body) =>
   215           props match {
   216             case Markup.Serial(i) =>
   217               val message1 = XML.Elem(Markup(Markup.message(name), props), body)
   218               val message2 = XML.Elem(Markup(name, props), body)
   219 
   220               var st = copy(results = results + (i -> message1))
   221               if (Protocol.is_inlined(message)) {
   222                 for {
   223                   (chunk_name, chunk) <- command.chunks.iterator
   224                   range <- Protocol.message_positions(self_id, chunk_name, chunk, message)
   225                 } st = st.add_markup(false, chunk_name, Text.Info(range, message2))
   226               }
   227               st
   228 
   229             case _ =>
   230               System.err.println("Ignored message without serial number: " + message)
   231               this
   232           }
   233     }
   234   }
   235 
   236 
   237 
   238   /** static content **/
   239 
   240   /* make commands */
   241 
   242   def name(span: List[Token]): String =
   243     span.find(_.is_command) match { case Some(tok) => tok.source case _ => "" }
   244 
   245   private def source_span(span: List[Token]): (String, List[Token]) =
   246   {
   247     val source: String =
   248       span match {
   249         case List(tok) => tok.source
   250         case _ => span.map(_.source).mkString
   251       }
   252 
   253     val span1 = new mutable.ListBuffer[Token]
   254     var i = 0
   255     for (Token(kind, s) <- span) {
   256       val n = s.length
   257       val s1 = source.substring(i, i + n)
   258       span1 += Token(kind, s1)
   259       i += n
   260     }
   261     (source, span1.toList)
   262   }
   263 
   264   def apply(
   265     id: Document_ID.Command,
   266     node_name: Document.Node.Name,
   267     blobs: List[Blob],
   268     span: List[Token]): Command =
   269   {
   270     val (source, span1) = source_span(span)
   271     new Command(id, node_name, blobs, span1, source, Results.empty, Markup_Tree.empty)
   272   }
   273 
   274   val empty: Command = Command(Document_ID.none, Document.Node.Name.empty, Nil, Nil)
   275 
   276   def unparsed(
   277     id: Document_ID.Command,
   278     source: String,
   279     results: Results,
   280     markup: Markup_Tree): Command =
   281   {
   282     val (source1, span) = source_span(List(Token(Token.Kind.UNPARSED, source)))
   283     new Command(id, Document.Node.Name.empty, Nil, span, source1, results, markup)
   284   }
   285 
   286   def unparsed(source: String): Command =
   287     unparsed(Document_ID.none, source, Results.empty, Markup_Tree.empty)
   288 
   289   def rich_text(id: Document_ID.Command, results: Results, body: XML.Body): Command =
   290   {
   291     val text = XML.content(body)
   292     val markup = Markup_Tree.from_XML(body)
   293     unparsed(id, text, results, markup)
   294   }
   295 
   296 
   297   /* perspective */
   298 
   299   object Perspective
   300   {
   301     val empty: Perspective = Perspective(Nil)
   302   }
   303 
   304   sealed case class Perspective(commands: List[Command])  // visible commands in canonical order
   305   {
   306     def same(that: Perspective): Boolean =
   307     {
   308       val cmds1 = this.commands
   309       val cmds2 = that.commands
   310       require(!cmds1.exists(_.is_undefined))
   311       require(!cmds2.exists(_.is_undefined))
   312       cmds1.length == cmds2.length &&
   313         (cmds1.iterator zip cmds2.iterator).forall({ case (c1, c2) => c1.id == c2.id })
   314     }
   315   }
   316 }
   317 
   318 
   319 final class Command private(
   320     val id: Document_ID.Command,
   321     val node_name: Document.Node.Name,
   322     val blobs: List[Command.Blob],
   323     val span: List[Token],
   324     val source: String,
   325     val init_results: Command.Results,
   326     val init_markup: Markup_Tree)
   327 {
   328   /* classification */
   329 
   330   def is_undefined: Boolean = id == Document_ID.none
   331   val is_unparsed: Boolean = span.exists(_.is_unparsed)
   332   val is_unfinished: Boolean = span.exists(_.is_unfinished)
   333 
   334   val is_ignored: Boolean = !span.exists(_.is_proper)
   335   val is_malformed: Boolean = !is_ignored && (!span.head.is_command || span.exists(_.is_error))
   336   def is_command: Boolean = !is_ignored && !is_malformed
   337 
   338   def name: String = Command.name(span)
   339 
   340   override def toString =
   341     id + "/" + (if (is_command) name else if (is_ignored) "IGNORED" else "MALFORMED")
   342 
   343 
   344   /* blobs */
   345 
   346   def blobs_changed(doc_blobs: Document.Blobs): Boolean =
   347     blobs.exists({ case Exn.Res((name, _)) => doc_blobs.changed(name) case _ => false })
   348 
   349   def blobs_names: List[Document.Node.Name] =
   350     for (Exn.Res((name, _)) <- blobs) yield name
   351 
   352   def blobs_digests: List[SHA1.Digest] =
   353     for (Exn.Res((_, Some((digest, _)))) <- blobs) yield digest
   354 
   355 
   356   /* source chunks */
   357 
   358   def length: Int = source.length
   359   val range: Text.Range = Text.Range(0, length)
   360 
   361   val proper_range: Text.Range =
   362     Text.Range(0, (length /: span.reverse.iterator.takeWhile(_.is_improper))(_ - _.source.length))
   363 
   364   def source(range: Text.Range): String = source.substring(range.start, range.stop)
   365 
   366   val chunk: Text.Chunk =
   367     new Text.Chunk {
   368       def range: Text.Range = Command.this.range
   369       lazy val symbol_index = Symbol.Index(source)
   370     }
   371 
   372   val chunks: Map[Text.Chunk.Name, Text.Chunk] =
   373     ((Text.Chunk.Default -> chunk) ::
   374       (for (Exn.Res((name, Some((_, file)))) <- blobs)
   375         yield (Text.Chunk.File_Name(name.node) -> file))).toMap
   376 
   377 
   378   /* accumulated results */
   379 
   380   val init_state: Command.State =
   381     Command.State(this, results = init_results, markups = Command.Markups.init(init_markup))
   382 
   383   val empty_state: Command.State = Command.State(this)
   384 }