src/Pure/PIDE/command.scala
author wenzelm
Tue Aug 12 00:08:32 2014 +0200 (2014-08-12 ago)
changeset 57905 c0c5652e796e
parent 57904 922273b7bf8a
child 57910 a50837b637dc
permissions -rw-r--r--
separate module Command_Span: mostly syntactic representation;
potentially prover-specific Output_Syntax.parse_spans;
     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, Symbol.Text_Chunk)])]
    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, Symbol.Text_Chunk.Default)
    64   }
    65 
    66   sealed case class Markup_Index(status: Boolean, chunk_name: Symbol.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 is_empty: Boolean = rep.isEmpty
    79 
    80     def apply(index: Markup_Index): Markup_Tree =
    81       rep.getOrElse(index, Markup_Tree.empty)
    82 
    83     def add(index: Markup_Index, markup: Text.Markup): Markups =
    84       new Markups(rep + (index -> (this(index) + markup)))
    85 
    86     def redirection_iterator: Iterator[Document_ID.Generic] =
    87       for (Markup_Index(_, Symbol.Text_Chunk.Id(id)) <- rep.keysIterator)
    88         yield id
    89 
    90     def redirect(other_id: Document_ID.Generic): Markups =
    91     {
    92       val rep1 =
    93         (for {
    94           (Markup_Index(status, Symbol.Text_Chunk.Id(id)), markup) <- rep.iterator
    95           if other_id == id
    96         } yield (Markup_Index(status, Symbol.Text_Chunk.Default), markup)).toMap
    97       if (rep1.isEmpty) Markups.empty else new Markups(rep1)
    98     }
    99 
   100     override def hashCode: Int = rep.hashCode
   101     override def equals(that: Any): Boolean =
   102       that match {
   103         case other: Markups => rep == other.rep
   104         case _ => false
   105       }
   106     override def toString: String = rep.iterator.mkString("Markups(", ", ", ")")
   107   }
   108 
   109 
   110   /* state */
   111 
   112   object State
   113   {
   114     def merge_results(states: List[State]): Command.Results =
   115       Results.merge(states.map(_.results))
   116 
   117     def merge_markup(states: List[State], index: Markup_Index,
   118         range: Text.Range, elements: Markup.Elements): Markup_Tree =
   119       Markup_Tree.merge(states.map(_.markup(index)), range, elements)
   120   }
   121 
   122   sealed case class State(
   123     command: Command,
   124     status: List[Markup] = Nil,
   125     results: Results = Results.empty,
   126     markups: Markups = Markups.empty)
   127   {
   128     lazy val protocol_status: Protocol.Status =
   129     {
   130       val warnings =
   131         if (results.iterator.exists(p => Protocol.is_warning(p._2)))
   132           List(Markup(Markup.WARNING, Nil))
   133         else Nil
   134       val errors =
   135         if (results.iterator.exists(p => Protocol.is_error(p._2)))
   136           List(Markup(Markup.ERROR, Nil))
   137         else Nil
   138       Protocol.Status.make((warnings ::: errors ::: status).iterator)
   139     }
   140 
   141     def markup(index: Markup_Index): Markup_Tree = markups(index)
   142 
   143     def redirect(other_command: Command): Option[State] =
   144     {
   145       val markups1 = markups.redirect(other_command.id)
   146       if (markups1.is_empty) None
   147       else Some(new State(other_command, Nil, Results.empty, markups1))
   148     }
   149 
   150     def eq_content(other: State): Boolean =
   151       command.source == other.command.source &&
   152       status == other.status &&
   153       results == other.results &&
   154       markups == other.markups
   155 
   156     private def add_status(st: Markup): State =
   157       copy(status = st :: status)
   158 
   159     private def add_markup(
   160       status: Boolean, chunk_name: Symbol.Text_Chunk.Name, m: Text.Markup): State =
   161     {
   162       val markups1 =
   163         if (status || Protocol.liberal_status_elements(m.info.name))
   164           markups.add(Markup_Index(true, chunk_name), m)
   165         else markups
   166       copy(markups = markups1.add(Markup_Index(false, chunk_name), m))
   167     }
   168 
   169     def accumulate(
   170         self_id: Document_ID.Generic => Boolean,
   171         other_id: Document_ID.Generic => Option[(Symbol.Text_Chunk.Id, Symbol.Text_Chunk)],
   172         message: XML.Elem): State =
   173       message match {
   174         case XML.Elem(Markup(Markup.STATUS, _), msgs) =>
   175           (this /: msgs)((state, msg) =>
   176             msg match {
   177               case elem @ XML.Elem(markup, Nil) =>
   178                 state.
   179                   add_status(markup).
   180                   add_markup(true, Symbol.Text_Chunk.Default, Text.Info(command.proper_range, elem))
   181               case _ =>
   182                 Output.warning("Ignored status message: " + msg)
   183                 state
   184             })
   185 
   186         case XML.Elem(Markup(Markup.REPORT, _), msgs) =>
   187           (this /: msgs)((state, msg) =>
   188             {
   189               def bad(): Unit = Output.warning("Ignored report message: " + msg)
   190 
   191               msg match {
   192                 case XML.Elem(
   193                     Markup(name, atts @ Position.Reported(id, chunk_name, symbol_range)), args) =>
   194 
   195                   val target =
   196                     if (self_id(id) && command.chunks.isDefinedAt(chunk_name))
   197                       Some((chunk_name, command.chunks(chunk_name)))
   198                     else if (chunk_name == Symbol.Text_Chunk.Default) other_id(id)
   199                     else None
   200 
   201                   target match {
   202                     case Some((target_name, target_chunk)) =>
   203                       target_chunk.incorporate(symbol_range) match {
   204                         case Some(range) =>
   205                           val props = Position.purge(atts)
   206                           val info = Text.Info(range, XML.Elem(Markup(name, props), args))
   207                           state.add_markup(false, target_name, info)
   208                         case None => bad(); state
   209                       }
   210                     case None =>
   211                       // silently ignore excessive reports
   212                       state
   213                   }
   214 
   215                 case XML.Elem(Markup(name, atts), args)
   216                 if !atts.exists({ case (a, _) => Markup.POSITION_PROPERTIES(a) }) =>
   217                   val range = command.proper_range
   218                   val props = Position.purge(atts)
   219                   val info: Text.Markup = Text.Info(range, XML.Elem(Markup(name, props), args))
   220                   state.add_markup(false, Symbol.Text_Chunk.Default, info)
   221 
   222                 case _ => bad(); state
   223               }
   224             })
   225         case XML.Elem(Markup(name, props), body) =>
   226           props match {
   227             case Markup.Serial(i) =>
   228               val message1 = XML.Elem(Markup(Markup.message(name), props), body)
   229               val message2 = XML.Elem(Markup(name, props), body)
   230 
   231               var st = copy(results = results + (i -> message1))
   232               if (Protocol.is_inlined(message)) {
   233                 for {
   234                   (chunk_name, chunk) <- command.chunks.iterator
   235                   range <- Protocol.message_positions(self_id, chunk_name, chunk, message)
   236                 } st = st.add_markup(false, chunk_name, Text.Info(range, message2))
   237               }
   238               st
   239 
   240             case _ =>
   241               Output.warning("Ignored message without serial number: " + message)
   242               this
   243           }
   244     }
   245   }
   246 
   247 
   248 
   249   /** static content **/
   250 
   251   /* make commands */
   252 
   253   def apply(
   254     id: Document_ID.Command,
   255     node_name: Document.Node.Name,
   256     blobs: List[Blob],
   257     span: Command_Span.Span): Command =
   258   {
   259     val (source, span1) = span.compact_source
   260     new Command(id, node_name, blobs, span1, source, Results.empty, Markup_Tree.empty)
   261   }
   262 
   263   val empty: Command =
   264     Command(Document_ID.none, Document.Node.Name.empty, Nil, Command_Span.empty)
   265 
   266   def unparsed(
   267     id: Document_ID.Command,
   268     source: String,
   269     results: Results,
   270     markup: Markup_Tree): Command =
   271   {
   272     val (source1, span1) = Command_Span.unparsed(source).compact_source
   273     new Command(id, Document.Node.Name.empty, Nil, span1, source1, results, markup)
   274   }
   275 
   276   def unparsed(source: String): Command =
   277     unparsed(Document_ID.none, source, Results.empty, Markup_Tree.empty)
   278 
   279   def rich_text(id: Document_ID.Command, results: Results, body: XML.Body): Command =
   280   {
   281     val text = XML.content(body)
   282     val markup = Markup_Tree.from_XML(body)
   283     unparsed(id, text, results, markup)
   284   }
   285 
   286 
   287   /* perspective */
   288 
   289   object Perspective
   290   {
   291     val empty: Perspective = Perspective(Nil)
   292   }
   293 
   294   sealed case class Perspective(commands: List[Command])  // visible commands in canonical order
   295   {
   296     def is_empty: Boolean = commands.isEmpty
   297 
   298     def same(that: Perspective): Boolean =
   299     {
   300       val cmds1 = this.commands
   301       val cmds2 = that.commands
   302       require(!cmds1.exists(_.is_undefined))
   303       require(!cmds2.exists(_.is_undefined))
   304       cmds1.length == cmds2.length &&
   305         (cmds1.iterator zip cmds2.iterator).forall({ case (c1, c2) => c1.id == c2.id })
   306     }
   307   }
   308 }
   309 
   310 
   311 final class Command private(
   312     val id: Document_ID.Command,
   313     val node_name: Document.Node.Name,
   314     val blobs: List[Command.Blob],
   315     val span: Command_Span.Span,
   316     val source: String,
   317     val init_results: Command.Results,
   318     val init_markup: Markup_Tree)
   319 {
   320   /* name and classification */
   321 
   322   def name: String =
   323     span.kind match { case Command_Span.Command_Span(name) => name case _ => "" }
   324 
   325   override def toString = id + "/" + span.kind.toString
   326 
   327   def is_proper: Boolean = span.kind.isInstanceOf[Command_Span.Command_Span]
   328   def is_ignored: Boolean = span.kind == Command_Span.Ignored_Span
   329 
   330   def is_undefined: Boolean = id == Document_ID.none
   331   val is_unparsed: Boolean = span.content.exists(_.is_unparsed)
   332   val is_unfinished: Boolean = span.content.exists(_.is_unfinished)
   333 
   334 
   335   /* blobs */
   336 
   337   def blobs_names: List[Document.Node.Name] =
   338     for (Exn.Res((name, _)) <- blobs) yield name
   339 
   340   def blobs_defined: List[(Document.Node.Name, SHA1.Digest)] =
   341     for (Exn.Res((name, Some((digest, _)))) <- blobs) yield (name, digest)
   342 
   343   def blobs_changed(doc_blobs: Document.Blobs): Boolean =
   344     blobs.exists({ case Exn.Res((name, _)) => doc_blobs.changed(name) case _ => false })
   345 
   346 
   347   /* source chunks */
   348 
   349   val chunk: Symbol.Text_Chunk = Symbol.Text_Chunk(source)
   350 
   351   val chunks: Map[Symbol.Text_Chunk.Name, Symbol.Text_Chunk] =
   352     ((Symbol.Text_Chunk.Default -> chunk) ::
   353       (for (Exn.Res((name, Some((_, file)))) <- blobs)
   354         yield (Symbol.Text_Chunk.File(name.node) -> file))).toMap
   355 
   356   def length: Int = source.length
   357   def range: Text.Range = chunk.range
   358 
   359   val proper_range: Text.Range =
   360     Text.Range(0,
   361       (length /: span.content.reverse.iterator.takeWhile(_.is_improper))(_ - _.source.length))
   362 
   363   def source(range: Text.Range): String = source.substring(range.start, range.stop)
   364 
   365 
   366   /* accumulated results */
   367 
   368   val init_state: Command.State =
   369     Command.State(this, results = init_results, markups = Command.Markups.init(init_markup))
   370 
   371   val empty_state: Command.State = Command.State(this)
   372 }