src/Pure/PIDE/command.scala
author wenzelm
Tue Apr 29 13:32:13 2014 +0200 (2014-04-29 ago)
changeset 56782 433cf57550fa
parent 56746 d37a5d09a277
child 57615 df1b3452d71c
permissions -rw-r--r--
more systematic Isabelle output, like in classic Isabelle/ML (without markup);
     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 name(span: List[Token]): String =
   254     span.find(_.is_command) match { case Some(tok) => tok.source case _ => "" }
   255 
   256   private def source_span(span: List[Token]): (String, List[Token]) =
   257   {
   258     val source: String =
   259       span match {
   260         case List(tok) => tok.source
   261         case _ => span.map(_.source).mkString
   262       }
   263 
   264     val span1 = new mutable.ListBuffer[Token]
   265     var i = 0
   266     for (Token(kind, s) <- span) {
   267       val n = s.length
   268       val s1 = source.substring(i, i + n)
   269       span1 += Token(kind, s1)
   270       i += n
   271     }
   272     (source, span1.toList)
   273   }
   274 
   275   def apply(
   276     id: Document_ID.Command,
   277     node_name: Document.Node.Name,
   278     blobs: List[Blob],
   279     span: List[Token]): Command =
   280   {
   281     val (source, span1) = source_span(span)
   282     new Command(id, node_name, blobs, span1, source, Results.empty, Markup_Tree.empty)
   283   }
   284 
   285   val empty: Command = Command(Document_ID.none, Document.Node.Name.empty, Nil, Nil)
   286 
   287   def unparsed(
   288     id: Document_ID.Command,
   289     source: String,
   290     results: Results,
   291     markup: Markup_Tree): Command =
   292   {
   293     val (source1, span) = source_span(List(Token(Token.Kind.UNPARSED, source)))
   294     new Command(id, Document.Node.Name.empty, Nil, span, source1, results, markup)
   295   }
   296 
   297   def unparsed(source: String): Command =
   298     unparsed(Document_ID.none, source, Results.empty, Markup_Tree.empty)
   299 
   300   def rich_text(id: Document_ID.Command, results: Results, body: XML.Body): Command =
   301   {
   302     val text = XML.content(body)
   303     val markup = Markup_Tree.from_XML(body)
   304     unparsed(id, text, results, markup)
   305   }
   306 
   307 
   308   /* perspective */
   309 
   310   object Perspective
   311   {
   312     val empty: Perspective = Perspective(Nil)
   313   }
   314 
   315   sealed case class Perspective(commands: List[Command])  // visible commands in canonical order
   316   {
   317     def same(that: Perspective): Boolean =
   318     {
   319       val cmds1 = this.commands
   320       val cmds2 = that.commands
   321       require(!cmds1.exists(_.is_undefined))
   322       require(!cmds2.exists(_.is_undefined))
   323       cmds1.length == cmds2.length &&
   324         (cmds1.iterator zip cmds2.iterator).forall({ case (c1, c2) => c1.id == c2.id })
   325     }
   326   }
   327 }
   328 
   329 
   330 final class Command private(
   331     val id: Document_ID.Command,
   332     val node_name: Document.Node.Name,
   333     val blobs: List[Command.Blob],
   334     val span: List[Token],
   335     val source: String,
   336     val init_results: Command.Results,
   337     val init_markup: Markup_Tree)
   338 {
   339   /* classification */
   340 
   341   def is_undefined: Boolean = id == Document_ID.none
   342   val is_unparsed: Boolean = span.exists(_.is_unparsed)
   343   val is_unfinished: Boolean = span.exists(_.is_unfinished)
   344 
   345   val is_ignored: Boolean = !span.exists(_.is_proper)
   346   val is_malformed: Boolean = !is_ignored && (!span.head.is_command || span.exists(_.is_error))
   347   def is_command: Boolean = !is_ignored && !is_malformed
   348 
   349   def name: String = Command.name(span)
   350 
   351   override def toString =
   352     id + "/" + (if (is_command) name else if (is_ignored) "IGNORED" else "MALFORMED")
   353 
   354 
   355   /* blobs */
   356 
   357   def blobs_changed(doc_blobs: Document.Blobs): Boolean =
   358     blobs.exists({ case Exn.Res((name, _)) => doc_blobs.changed(name) case _ => false })
   359 
   360   def blobs_names: List[Document.Node.Name] =
   361     for (Exn.Res((name, _)) <- blobs) yield name
   362 
   363   def blobs_digests: List[SHA1.Digest] =
   364     for (Exn.Res((_, Some((digest, _)))) <- blobs) yield digest
   365 
   366 
   367   /* source chunks */
   368 
   369   val chunk: Symbol.Text_Chunk = Symbol.Text_Chunk(source)
   370 
   371   val chunks: Map[Symbol.Text_Chunk.Name, Symbol.Text_Chunk] =
   372     ((Symbol.Text_Chunk.Default -> chunk) ::
   373       (for (Exn.Res((name, Some((_, file)))) <- blobs)
   374         yield (Symbol.Text_Chunk.File(name.node) -> file))).toMap
   375 
   376   def length: Int = source.length
   377   def range: Text.Range = chunk.range
   378 
   379   val proper_range: Text.Range =
   380     Text.Range(0, (length /: span.reverse.iterator.takeWhile(_.is_improper))(_ - _.source.length))
   381 
   382   def source(range: Text.Range): String = source.substring(range.start, range.stop)
   383 
   384 
   385   /* accumulated results */
   386 
   387   val init_state: Command.State =
   388     Command.State(this, results = init_results, markups = Command.Markups.init(init_markup))
   389 
   390   val empty_state: Command.State = Command.State(this)
   391 }