src/Pure/PIDE/command.scala
author wenzelm
Sun Mar 15 19:21:15 2015 +0100 (2015-03-15 ago)
changeset 59704 58dfaa369c11
parent 59701 a6efad6acafd
child 59707 740a0ca7e09b
permissions -rw-r--r--
hybrid use of command blobs: inlined errors and auxiliary files;
static check of theory imports;
     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 
    19   type Blob = Exn.Result[(Document.Node.Name, Option[(SHA1.Digest, Symbol.Text_Chunk)])]
    20   type Blobs_Info = (List[Blob], Int)
    21   val no_blobs: Blobs_Info = (Nil, -1)
    22 
    23 
    24   /** accumulated results from prover **/
    25 
    26   /* results */
    27 
    28   object Results
    29   {
    30     type Entry = (Long, XML.Tree)
    31     val empty = new Results(SortedMap.empty)
    32     def make(args: TraversableOnce[Results.Entry]): Results = (empty /: args)(_ + _)
    33     def merge(args: TraversableOnce[Results]): Results = (empty /: args)(_ ++ _)
    34   }
    35 
    36   final class Results private(private val rep: SortedMap[Long, XML.Tree])
    37   {
    38     def defined(serial: Long): Boolean = rep.isDefinedAt(serial)
    39     def get(serial: Long): Option[XML.Tree] = rep.get(serial)
    40     def iterator: Iterator[Results.Entry] = rep.iterator
    41 
    42     def + (entry: Results.Entry): Results =
    43       if (defined(entry._1)) this
    44       else new Results(rep + entry)
    45 
    46     def ++ (other: Results): Results =
    47       if (this eq other) this
    48       else if (rep.isEmpty) other
    49       else (this /: other.iterator)(_ + _)
    50 
    51     override def hashCode: Int = rep.hashCode
    52     override def equals(that: Any): Boolean =
    53       that match {
    54         case other: Results => rep == other.rep
    55         case _ => false
    56       }
    57     override def toString: String = iterator.mkString("Results(", ", ", ")")
    58   }
    59 
    60 
    61   /* markup */
    62 
    63   object Markup_Index
    64   {
    65     val markup: Markup_Index = Markup_Index(false, Symbol.Text_Chunk.Default)
    66   }
    67 
    68   sealed case class Markup_Index(status: Boolean, chunk_name: Symbol.Text_Chunk.Name)
    69 
    70   object Markups
    71   {
    72     val empty: Markups = new Markups(Map.empty)
    73 
    74     def init(markup: Markup_Tree): Markups =
    75       new Markups(Map(Markup_Index.markup -> markup))
    76   }
    77 
    78   final class Markups private(private val rep: Map[Markup_Index, Markup_Tree])
    79   {
    80     def is_empty: Boolean = rep.isEmpty
    81 
    82     def apply(index: Markup_Index): Markup_Tree =
    83       rep.getOrElse(index, Markup_Tree.empty)
    84 
    85     def add(index: Markup_Index, markup: Text.Markup): Markups =
    86       new Markups(rep + (index -> (this(index) + markup)))
    87 
    88     def redirection_iterator: Iterator[Document_ID.Generic] =
    89       for (Markup_Index(_, Symbol.Text_Chunk.Id(id)) <- rep.keysIterator)
    90         yield id
    91 
    92     def redirect(other_id: Document_ID.Generic): Markups =
    93     {
    94       val rep1 =
    95         (for {
    96           (Markup_Index(status, Symbol.Text_Chunk.Id(id)), markup) <- rep.iterator
    97           if other_id == id
    98         } yield (Markup_Index(status, Symbol.Text_Chunk.Default), markup)).toMap
    99       if (rep1.isEmpty) Markups.empty else new Markups(rep1)
   100     }
   101 
   102     override def hashCode: Int = rep.hashCode
   103     override def equals(that: Any): Boolean =
   104       that match {
   105         case other: Markups => rep == other.rep
   106         case _ => false
   107       }
   108     override def toString: String = rep.iterator.mkString("Markups(", ", ", ")")
   109   }
   110 
   111 
   112   /* state */
   113 
   114   object State
   115   {
   116     def merge_results(states: List[State]): Command.Results =
   117       Results.merge(states.map(_.results))
   118 
   119     def merge_markup(states: List[State], index: Markup_Index,
   120         range: Text.Range, elements: Markup.Elements): Markup_Tree =
   121       Markup_Tree.merge(states.map(_.markup(index)), range, elements)
   122   }
   123 
   124   sealed case class State(
   125     command: Command,
   126     status: List[Markup] = Nil,
   127     results: Results = Results.empty,
   128     markups: Markups = Markups.empty)
   129   {
   130     lazy val protocol_status: Protocol.Status =
   131     {
   132       val warnings =
   133         if (results.iterator.exists(p => Protocol.is_warning(p._2) || Protocol.is_legacy(p._2)))
   134           List(Markup(Markup.WARNING, Nil))
   135         else Nil
   136       val errors =
   137         if (results.iterator.exists(p => Protocol.is_error(p._2)))
   138           List(Markup(Markup.ERROR, Nil))
   139         else Nil
   140       Protocol.Status.make((warnings ::: errors ::: status).iterator)
   141     }
   142 
   143     def markup(index: Markup_Index): Markup_Tree = markups(index)
   144 
   145     def redirect(other_command: Command): Option[State] =
   146     {
   147       val markups1 = markups.redirect(other_command.id)
   148       if (markups1.is_empty) None
   149       else Some(new State(other_command, Nil, Results.empty, markups1))
   150     }
   151 
   152     def eq_content(other: State): Boolean =
   153       command.source == other.command.source &&
   154       status == other.status &&
   155       results == other.results &&
   156       markups == other.markups
   157 
   158     private def add_status(st: Markup): State =
   159       copy(status = st :: status)
   160 
   161     private def add_markup(
   162       status: Boolean, chunk_name: Symbol.Text_Chunk.Name, m: Text.Markup): State =
   163     {
   164       val markups1 =
   165         if (status || Protocol.liberal_status_elements(m.info.name))
   166           markups.add(Markup_Index(true, chunk_name), m)
   167         else markups
   168       copy(markups = markups1.add(Markup_Index(false, chunk_name), m))
   169     }
   170 
   171     def accumulate(
   172         self_id: Document_ID.Generic => Boolean,
   173         other_id: Document_ID.Generic => Option[(Symbol.Text_Chunk.Id, Symbol.Text_Chunk)],
   174         message: XML.Elem): State =
   175       message match {
   176         case XML.Elem(Markup(Markup.STATUS, _), msgs) =>
   177           (this /: msgs)((state, msg) =>
   178             msg match {
   179               case elem @ XML.Elem(markup, Nil) =>
   180                 state.
   181                   add_status(markup).
   182                   add_markup(true, Symbol.Text_Chunk.Default, Text.Info(command.proper_range, elem))
   183               case _ =>
   184                 Output.warning("Ignored status message: " + msg)
   185                 state
   186             })
   187 
   188         case XML.Elem(Markup(Markup.REPORT, _), msgs) =>
   189           (this /: msgs)((state, msg) =>
   190             {
   191               def bad(): Unit = Output.warning("Ignored report message: " + msg)
   192 
   193               msg match {
   194                 case XML.Elem(Markup(name, atts @ Position.Identified(id, chunk_name)), args) =>
   195 
   196                   val target =
   197                     if (self_id(id) && command.chunks.isDefinedAt(chunk_name))
   198                       Some((chunk_name, command.chunks(chunk_name)))
   199                     else if (chunk_name == Symbol.Text_Chunk.Default) other_id(id)
   200                     else None
   201 
   202                   (target, atts) match {
   203                     case (Some((target_name, target_chunk)), Position.Range(symbol_range)) =>
   204                       target_chunk.incorporate(symbol_range) match {
   205                         case Some(range) =>
   206                           val props = Position.purge(atts)
   207                           val info = Text.Info(range, XML.Elem(Markup(name, props), args))
   208                           state.add_markup(false, target_name, info)
   209                         case None => bad(); state
   210                       }
   211                     case _ =>
   212                       // silently ignore excessive reports
   213                       state
   214                   }
   215 
   216                 case XML.Elem(Markup(name, atts), args)
   217                 if !atts.exists({ case (a, _) => Markup.POSITION_PROPERTIES(a) }) =>
   218                   val range = command.proper_range
   219                   val props = Position.purge(atts)
   220                   val info: Text.Markup = Text.Info(range, XML.Elem(Markup(name, props), args))
   221                   state.add_markup(false, Symbol.Text_Chunk.Default, info)
   222 
   223                 case _ => bad(); state
   224               }
   225             })
   226         case XML.Elem(Markup(name, props), body) =>
   227           props match {
   228             case Markup.Serial(i) =>
   229               val message1 = XML.Elem(Markup(Markup.message(name), props), body)
   230               val message2 = XML.Elem(Markup(name, props), body)
   231 
   232               var st = copy(results = results + (i -> message1))
   233               if (Protocol.is_inlined(message)) {
   234                 for {
   235                   (chunk_name, chunk) <- command.chunks.iterator
   236                   range <- Protocol.message_positions(
   237                     self_id, command.position, chunk_name, chunk, message)
   238                 } st = st.add_markup(false, chunk_name, Text.Info(range, message2))
   239               }
   240               st
   241 
   242             case _ =>
   243               Output.warning("Ignored message without serial number: " + message)
   244               this
   245           }
   246     }
   247   }
   248 
   249 
   250 
   251   /** static content **/
   252 
   253   /* make commands */
   254 
   255   def apply(
   256     id: Document_ID.Command,
   257     node_name: Document.Node.Name,
   258     blobs_info: Blobs_Info,
   259     span: Command_Span.Span): Command =
   260   {
   261     val (source, span1) = span.compact_source
   262     new Command(id, node_name, blobs_info, span1, source, Results.empty, Markup_Tree.empty)
   263   }
   264 
   265   val empty: Command =
   266     Command(Document_ID.none, Document.Node.Name.empty, no_blobs, Command_Span.empty)
   267 
   268   def unparsed(
   269     id: Document_ID.Command,
   270     source: String,
   271     results: Results,
   272     markup: Markup_Tree): Command =
   273   {
   274     val (source1, span1) = Command_Span.unparsed(source).compact_source
   275     new Command(id, Document.Node.Name.empty, no_blobs, span1, source1, results, markup)
   276   }
   277 
   278   def unparsed(source: String): Command =
   279     unparsed(Document_ID.none, source, Results.empty, Markup_Tree.empty)
   280 
   281   def rich_text(id: Document_ID.Command, results: Results, body: XML.Body): Command =
   282   {
   283     val text = XML.content(body)
   284     val markup = Markup_Tree.from_XML(body)
   285     unparsed(id, text, results, markup)
   286   }
   287 
   288 
   289   /* perspective */
   290 
   291   object Perspective
   292   {
   293     val empty: Perspective = Perspective(Nil)
   294   }
   295 
   296   sealed case class Perspective(commands: List[Command])  // visible commands in canonical order
   297   {
   298     def is_empty: Boolean = commands.isEmpty
   299 
   300     def same(that: Perspective): Boolean =
   301     {
   302       val cmds1 = this.commands
   303       val cmds2 = that.commands
   304       require(!cmds1.exists(_.is_undefined))
   305       require(!cmds2.exists(_.is_undefined))
   306       cmds1.length == cmds2.length &&
   307         (cmds1.iterator zip cmds2.iterator).forall({ case (c1, c2) => c1.id == c2.id })
   308     }
   309   }
   310 
   311 
   312   /* blobs: inlined errors and auxiliary files */
   313 
   314   private def clean_tokens(tokens: List[Token]): List[(Token, Int)] =
   315   {
   316     def clean(toks: List[(Token, Int)]): List[(Token, Int)] =
   317       toks match {
   318         case (t1, i1) :: (t2, i2) :: rest =>
   319           if (t1.is_keyword && (t1.source == "%" || t1.source == "--")) clean(rest)
   320           else (t1, i1) :: clean((t2, i2) :: rest)
   321         case _ => toks
   322       }
   323     clean(tokens.zipWithIndex.filter({ case (t, _) => t.is_proper }))
   324   }
   325 
   326   private def find_file(tokens: List[(Token, Int)]): Option[(String, Int)] =
   327     tokens match {
   328       case (tok, _) :: toks =>
   329         if (tok.is_command)
   330           toks.collectFirst({ case (t, i) if t.is_name => (t.content, i) })
   331         else None
   332       case Nil => None
   333     }
   334 
   335   def span_files(syntax: Prover.Syntax, span: Command_Span.Span): (List[String], Int) =
   336     span.kind match {
   337       case Command_Span.Command_Span(name, _) =>
   338         syntax.load_command(name) match {
   339           case Some(exts) =>
   340             find_file(clean_tokens(span.content)) match {
   341               case Some((file, i)) =>
   342                 if (exts.isEmpty) (List(file), i)
   343                 else (exts.map(ext => file + "." + ext), i)
   344               case None => (Nil, -1)
   345             }
   346           case None => (Nil, -1)
   347         }
   348       case _ => (Nil, -1)
   349     }
   350 
   351   def blobs_info(
   352     resources: Resources,
   353     syntax: Prover.Syntax,
   354     get_blob: Document.Node.Name => Option[Document.Blob],
   355     can_import: Document.Node.Name => Boolean,
   356     node_name: Document.Node.Name,
   357     header: Document.Node.Header,
   358     span: Command_Span.Span): Blobs_Info =
   359   {
   360     span.kind match {
   361       // inlined errors
   362       case Command_Span.Command_Span(name, _) if syntax.is_theory_begin(name) =>
   363         val import_errors =
   364           for ((imp, pos) <- header.imports if !can_import(imp))
   365             yield "Bad theory import " + quote(imp.node) + Position.here(pos)
   366         ((header.errors ::: import_errors).map(msg => Exn.Exn(ERROR(msg)): Command.Blob), -1)
   367 
   368       // auxiliary files
   369       case _ =>
   370         val (files, index) = span_files(syntax, span)
   371         val blobs =
   372           files.map(file =>
   373             (Exn.capture {
   374               val name =
   375                 Document.Node.Name(resources.append(node_name.master_dir, Path.explode(file)))
   376               val blob = get_blob(name).map(blob => ((blob.bytes.sha1_digest, blob.chunk)))
   377               (name, blob)
   378             }).user_error)
   379         (blobs, index)
   380     }
   381   }
   382 }
   383 
   384 
   385 final class Command private(
   386     val id: Document_ID.Command,
   387     val node_name: Document.Node.Name,
   388     val blobs_info: Command.Blobs_Info,
   389     val span: Command_Span.Span,
   390     val source: String,
   391     val init_results: Command.Results,
   392     val init_markup: Markup_Tree)
   393 {
   394   /* name */
   395 
   396   def name: String =
   397     span.kind match { case Command_Span.Command_Span(name, _) => name case _ => "" }
   398 
   399   def position: Position.T =
   400     span.kind match { case Command_Span.Command_Span(_, pos) => pos case _ => Position.none }
   401 
   402   override def toString: String = id + "/" + span.kind.toString
   403 
   404 
   405   /* classification */
   406 
   407   def is_proper: Boolean = span.kind.isInstanceOf[Command_Span.Command_Span]
   408   def is_ignored: Boolean = span.kind == Command_Span.Ignored_Span
   409 
   410   def is_undefined: Boolean = id == Document_ID.none
   411   val is_unparsed: Boolean = span.content.exists(_.is_unparsed)
   412   val is_unfinished: Boolean = span.content.exists(_.is_unfinished)
   413 
   414 
   415   /* blobs */
   416 
   417   def blobs: List[Command.Blob] = blobs_info._1
   418   def blobs_index: Int = blobs_info._2
   419 
   420   def blobs_names: List[Document.Node.Name] =
   421     for (Exn.Res((name, _)) <- blobs) yield name
   422 
   423   def blobs_defined: List[(Document.Node.Name, SHA1.Digest)] =
   424     for (Exn.Res((name, Some((digest, _)))) <- blobs) yield (name, digest)
   425 
   426   def blobs_changed(doc_blobs: Document.Blobs): Boolean =
   427     blobs.exists({ case Exn.Res((name, _)) => doc_blobs.changed(name) case _ => false })
   428 
   429 
   430   /* source chunks */
   431 
   432   val chunk: Symbol.Text_Chunk = Symbol.Text_Chunk(source)
   433 
   434   val chunks: Map[Symbol.Text_Chunk.Name, Symbol.Text_Chunk] =
   435     ((Symbol.Text_Chunk.Default -> chunk) ::
   436       (for (Exn.Res((name, Some((_, file)))) <- blobs)
   437         yield (Symbol.Text_Chunk.File(name.node) -> file))).toMap
   438 
   439   def length: Int = source.length
   440   def range: Text.Range = chunk.range
   441 
   442   val proper_range: Text.Range =
   443     Text.Range(0,
   444       (length /: span.content.reverse.iterator.takeWhile(_.is_improper))(_ - _.source.length))
   445 
   446   def source(range: Text.Range): String = source.substring(range.start, range.stop)
   447 
   448 
   449   /* accumulated results */
   450 
   451   val init_state: Command.State =
   452     Command.State(this, results = init_results, markups = Command.Markups.init(init_markup))
   453 
   454   val empty_state: Command.State = Command.State(this)
   455 }