src/Pure/PIDE/command.scala
author wenzelm
Wed Apr 02 20:22:12 2014 +0200 (2014-04-02 ago)
changeset 56372 fadb0fef09d7
parent 56359 bca016a9a18d
child 56395 0546e036d1c0
permissions -rw-r--r--
more explicit iterator terminology, in accordance to Scala 2.8 library;
clarified Graph.keys_iterator vs. Graph.keys, with subtle change of semantics;
tuned output;
     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, 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, "")
    64   }
    65 
    66   sealed case class Markup_Index(status: Boolean, file_name: String)
    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     override def hashCode: Int = rep.hashCode
    85     override def equals(that: Any): Boolean =
    86       that match {
    87         case other: Markups => rep == other.rep
    88         case _ => false
    89       }
    90     override def toString: String = rep.iterator.mkString("Markups(", ", ", ")")
    91   }
    92 
    93 
    94   /* state */
    95 
    96   object State
    97   {
    98     def merge_results(states: List[State]): Command.Results =
    99       Results.merge(states.map(_.results))
   100 
   101     def merge_markup(states: List[State], index: Markup_Index,
   102         range: Text.Range, elements: Document.Elements): Markup_Tree =
   103       Markup_Tree.merge(states.map(_.markup(index)), range, elements)
   104   }
   105 
   106   sealed case class State(
   107     command: Command,
   108     status: List[Markup] = Nil,
   109     results: Results = Results.empty,
   110     markups: Markups = Markups.empty)
   111   {
   112     lazy val protocol_status: Protocol.Status = Protocol.Status.make(status.iterator)
   113 
   114     def markup(index: Markup_Index): Markup_Tree = markups(index)
   115 
   116 
   117     def eq_content(other: State): Boolean =
   118       command.source == other.command.source &&
   119       status == other.status &&
   120       results == other.results &&
   121       markups == other.markups
   122 
   123     private def add_status(st: Markup): State =
   124       copy(status = st :: status)
   125 
   126     private def add_markup(status: Boolean, file_name: String, m: Text.Markup): State =
   127     {
   128       val markups1 =
   129         if (status || Protocol.status_elements(m.info.name))
   130           markups.add(Markup_Index(true, file_name), m)
   131         else markups
   132       copy(markups = markups1.add(Markup_Index(false, file_name), m))
   133     }
   134 
   135     def + (valid_id: Document_ID.Generic => Boolean, message: XML.Elem): State =
   136       message match {
   137         case XML.Elem(Markup(Markup.STATUS, _), msgs) =>
   138           (this /: msgs)((state, msg) =>
   139             msg match {
   140               case elem @ XML.Elem(markup, Nil) =>
   141                 state.
   142                   add_status(markup).
   143                   add_markup(true, "", Text.Info(command.proper_range, elem))
   144               case _ =>
   145                 System.err.println("Ignored status message: " + msg)
   146                 state
   147             })
   148 
   149         case XML.Elem(Markup(Markup.REPORT, _), msgs) =>
   150           (this /: msgs)((state, msg) =>
   151             {
   152               def bad(): Unit = System.err.println("Ignored report message: " + msg)
   153 
   154               msg match {
   155                 case XML.Elem(Markup(name,
   156                   atts @ Position.Reported(id, file_name, symbol_range)), args)
   157                 if valid_id(id) =>
   158                   command.chunks.get(file_name) match {
   159                     case Some(chunk) =>
   160                       chunk.incorporate(symbol_range) match {
   161                         case Some(range) =>
   162                           val props = Position.purge(atts)
   163                           val info = Text.Info(range, XML.Elem(Markup(name, props), args))
   164                           state.add_markup(false, file_name, info)
   165                         case None => bad(); state
   166                       }
   167                     case None => bad(); state
   168                   }
   169 
   170                 case XML.Elem(Markup(name, atts), args)
   171                 if !atts.exists({ case (a, _) => Markup.POSITION_PROPERTIES(a) }) =>
   172                   val range = command.proper_range
   173                   val props = Position.purge(atts)
   174                   val info: Text.Markup = Text.Info(range, XML.Elem(Markup(name, props), args))
   175                   state.add_markup(false, "", info)
   176 
   177                 case _ => /* FIXME bad(); */ state
   178               }
   179             })
   180         case XML.Elem(Markup(name, props), body) =>
   181           props match {
   182             case Markup.Serial(i) =>
   183               val message1 = XML.Elem(Markup(Markup.message(name), props), body)
   184               val message2 = XML.Elem(Markup(name, props), body)
   185 
   186               var st = copy(results = results + (i -> message1))
   187               if (Protocol.is_inlined(message)) {
   188                 for {
   189                   (file_name, chunk) <- command.chunks
   190                   range <- Protocol.message_positions(valid_id, chunk, message)
   191                 } st = st.add_markup(false, file_name, Text.Info(range, message2))
   192               }
   193               st
   194 
   195             case _ =>
   196               System.err.println("Ignored message without serial number: " + message)
   197               this
   198           }
   199     }
   200   }
   201 
   202 
   203 
   204   /** static content **/
   205 
   206   /* text chunks */
   207 
   208   abstract class Chunk
   209   {
   210     def file_name: String
   211     def length: Int
   212     def range: Text.Range
   213     def decode(symbol_range: Symbol.Range): Text.Range
   214 
   215     def incorporate(symbol_range: Symbol.Range): Option[Text.Range] =
   216     {
   217       def inc(r: Symbol.Range): Option[Text.Range] =
   218         range.try_restrict(decode(r)) match {
   219           case Some(r1) if !r1.is_singularity => Some(r1)
   220           case _ => None
   221         }
   222      inc(symbol_range) orElse inc(symbol_range - 1)
   223     }
   224   }
   225 
   226   // file name and position information, *without* persistent text
   227   class File(val file_name: String, text: CharSequence) extends Chunk
   228   {
   229     val length = text.length
   230     val range = Text.Range(0, length)
   231     private val symbol_index = Symbol.Index(text)
   232     def decode(symbol_range: Symbol.Range): Text.Range = symbol_index.decode(symbol_range)
   233 
   234     private val hash: Int = (file_name, length, symbol_index).hashCode
   235     override def hashCode: Int = hash
   236     override def equals(that: Any): Boolean =
   237       that match {
   238         case other: File =>
   239           hash == other.hash &&
   240           file_name == other.file_name &&
   241           length == other.length &&
   242           symbol_index == other.symbol_index
   243         case _ => false
   244       }
   245     override def toString: String = "Command.File(" + file_name + ")"
   246   }
   247 
   248 
   249   /* make commands */
   250 
   251   def name(span: List[Token]): String =
   252     span.find(_.is_command) match { case Some(tok) => tok.source case _ => "" }
   253 
   254   private def source_span(span: List[Token]): (String, List[Token]) =
   255   {
   256     val source: String =
   257       span match {
   258         case List(tok) => tok.source
   259         case _ => span.map(_.source).mkString
   260       }
   261 
   262     val span1 = new mutable.ListBuffer[Token]
   263     var i = 0
   264     for (Token(kind, s) <- span) {
   265       val n = s.length
   266       val s1 = source.substring(i, i + n)
   267       span1 += Token(kind, s1)
   268       i += n
   269     }
   270     (source, span1.toList)
   271   }
   272 
   273   def apply(
   274     id: Document_ID.Command,
   275     node_name: Document.Node.Name,
   276     blobs: List[Blob],
   277     span: List[Token]): Command =
   278   {
   279     val (source, span1) = source_span(span)
   280     new Command(id, node_name, blobs, span1, source, Results.empty, Markup_Tree.empty)
   281   }
   282 
   283   val empty: Command = Command(Document_ID.none, Document.Node.Name.empty, Nil, Nil)
   284 
   285   def unparsed(
   286     id: Document_ID.Command,
   287     source: String,
   288     results: Results,
   289     markup: Markup_Tree): Command =
   290   {
   291     val (source1, span) = source_span(List(Token(Token.Kind.UNPARSED, source)))
   292     new Command(id, Document.Node.Name.empty, Nil, span, source1, results, markup)
   293   }
   294 
   295   def unparsed(source: String): Command =
   296     unparsed(Document_ID.none, source, Results.empty, Markup_Tree.empty)
   297 
   298   def rich_text(id: Document_ID.Command, results: Results, body: XML.Body): Command =
   299   {
   300     val text = XML.content(body)
   301     val markup = Markup_Tree.from_XML(body)
   302     unparsed(id, text, results, markup)
   303   }
   304 
   305 
   306   /* perspective */
   307 
   308   object Perspective
   309   {
   310     val empty: Perspective = Perspective(Nil)
   311   }
   312 
   313   sealed case class Perspective(commands: List[Command])  // visible commands in canonical order
   314   {
   315     def same(that: Perspective): Boolean =
   316     {
   317       val cmds1 = this.commands
   318       val cmds2 = that.commands
   319       require(!cmds1.exists(_.is_undefined))
   320       require(!cmds2.exists(_.is_undefined))
   321       cmds1.length == cmds2.length &&
   322         (cmds1.iterator zip cmds2.iterator).forall({ case (c1, c2) => c1.id == c2.id })
   323     }
   324   }
   325 }
   326 
   327 
   328 final class Command private(
   329     val id: Document_ID.Command,
   330     val node_name: Document.Node.Name,
   331     val blobs: List[Command.Blob],
   332     val span: List[Token],
   333     val source: String,
   334     val init_results: Command.Results,
   335     val init_markup: Markup_Tree)
   336   extends Command.Chunk
   337 {
   338   /* classification */
   339 
   340   def is_undefined: Boolean = id == Document_ID.none
   341   val is_unparsed: Boolean = span.exists(_.is_unparsed)
   342   val is_unfinished: Boolean = span.exists(_.is_unfinished)
   343 
   344   val is_ignored: Boolean = !span.exists(_.is_proper)
   345   val is_malformed: Boolean = !is_ignored && (!span.head.is_command || span.exists(_.is_error))
   346   def is_command: Boolean = !is_ignored && !is_malformed
   347 
   348   def name: String = Command.name(span)
   349 
   350   override def toString =
   351     id + "/" + (if (is_command) name else if (is_ignored) "IGNORED" else "MALFORMED")
   352 
   353 
   354   /* blobs */
   355 
   356   def blobs_changed(doc_blobs: Document.Blobs): Boolean =
   357     blobs.exists({ case Exn.Res((name, _)) => doc_blobs.changed(name) case _ => false })
   358 
   359   def blobs_names: List[Document.Node.Name] =
   360     for (Exn.Res((name, _)) <- blobs) yield name
   361 
   362   def blobs_digests: List[SHA1.Digest] =
   363     for (Exn.Res((_, Some((digest, _)))) <- blobs) yield digest
   364 
   365   val chunks: Map[String, Command.Chunk] =
   366     (("" -> this) ::
   367       (for (Exn.Res((name, Some((_, file)))) <- blobs) yield (name.node -> file))).toMap
   368 
   369 
   370   /* source */
   371 
   372   def file_name: String = ""
   373 
   374   def length: Int = source.length
   375   val range: Text.Range = Text.Range(0, length)
   376 
   377   val proper_range: Text.Range =
   378     Text.Range(0, (length /: span.reverse.iterator.takeWhile(_.is_improper))(_ - _.source.length))
   379 
   380   def source(range: Text.Range): String = source.substring(range.start, range.stop)
   381 
   382   private lazy val symbol_index = Symbol.Index(source)
   383   def decode(symbol_offset: Symbol.Offset): Text.Offset = symbol_index.decode(symbol_offset)
   384   def decode(symbol_range: Symbol.Range): Text.Range = symbol_index.decode(symbol_range)
   385 
   386 
   387   /* accumulated results */
   388 
   389   val init_state: Command.State =
   390     Command.State(this, results = init_results, markups = Command.Markups.init(init_markup))
   391 
   392   val empty_state: Command.State = Command.State(this)
   393 }