src/Pure/PIDE/command.scala
author wenzelm
Mon May 28 17:40:34 2018 +0200 (15 months ago ago)
changeset 68307 d575281e18d0
parent 68119 ce7f35406f37
child 68324 bf7336731981
permissions -rw-r--r--
clarified signature: Known.theories retains Document.Node.Entry (with header);
     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: Results = 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     /* XML data representation */
    37 
    38     val encode: XML.Encode.T[Results] = (results: Results) =>
    39     { import XML.Encode._; list(pair(long, tree))(results.rep.toList) }
    40 
    41     val decode: XML.Decode.T[Results] = (body: XML.Body) =>
    42     { import XML.Decode._; make(list(pair(long, tree))(body)) }
    43   }
    44 
    45   final class Results private(private val rep: SortedMap[Long, XML.Tree])
    46   {
    47     def is_empty: Boolean = rep.isEmpty
    48     def defined(serial: Long): Boolean = rep.isDefinedAt(serial)
    49     def get(serial: Long): Option[XML.Tree] = rep.get(serial)
    50     def iterator: Iterator[Results.Entry] = rep.iterator
    51 
    52     def + (entry: Results.Entry): Results =
    53       if (defined(entry._1)) this
    54       else new Results(rep + entry)
    55 
    56     def ++ (other: Results): Results =
    57       if (this eq other) this
    58       else if (rep.isEmpty) other
    59       else (this /: other.iterator)(_ + _)
    60 
    61     override def hashCode: Int = rep.hashCode
    62     override def equals(that: Any): Boolean =
    63       that match {
    64         case other: Results => rep == other.rep
    65         case _ => false
    66       }
    67     override def toString: String = iterator.mkString("Results(", ", ", ")")
    68   }
    69 
    70 
    71   /* exports */
    72 
    73   object Exports
    74   {
    75     type Entry = (Long, Export.Entry)
    76     val empty: Exports = new Exports(SortedMap.empty)
    77     def merge(args: TraversableOnce[Exports]): Exports = (empty /: args)(_ ++ _)
    78   }
    79 
    80   final class Exports private(private val rep: SortedMap[Long, Export.Entry])
    81   {
    82     def iterator: Iterator[Exports.Entry] = rep.iterator
    83 
    84     def + (entry: Exports.Entry): Exports =
    85       if (rep.isDefinedAt(entry._1)) this
    86       else new Exports(rep + entry)
    87 
    88     def ++ (other: Exports): Exports =
    89       if (this eq other) this
    90       else if (rep.isEmpty) other
    91       else (this /: other.iterator)(_ + _)
    92 
    93     override def hashCode: Int = rep.hashCode
    94     override def equals(that: Any): Boolean =
    95       that match {
    96         case other: Exports => rep == other.rep
    97         case _ => false
    98       }
    99     override def toString: String = iterator.mkString("Exports(", ", ", ")")
   100   }
   101 
   102 
   103   /* markups */
   104 
   105   object Markup_Index
   106   {
   107     val markup: Markup_Index = Markup_Index(false, Symbol.Text_Chunk.Default)
   108   }
   109 
   110   sealed case class Markup_Index(status: Boolean, chunk_name: Symbol.Text_Chunk.Name)
   111 
   112   object Markups
   113   {
   114     val empty: Markups = new Markups(Map.empty)
   115     def init(markup: Markup_Tree): Markups = new Markups(Map(Markup_Index.markup -> markup))
   116     def merge(args: TraversableOnce[Markups]): Markups = (empty /: args)(_ ++ _)
   117 
   118 
   119     /* XML data representation */
   120 
   121     def encode(source_length: Int): XML.Encode.T[Markups] = (markups: Markups) =>
   122     {
   123       import XML.Encode._
   124 
   125       val markup_index: T[Markup_Index] = (index: Markup_Index) =>
   126         pair(bool, Symbol.Text_Chunk.encode_name)(index.status, index.chunk_name)
   127 
   128       val markup_tree: T[Markup_Tree] =
   129         _.to_XML(Text.Range(0, source_length), Symbol.spaces(source_length), Markup.Elements.full)
   130 
   131       list(pair(markup_index, markup_tree))(markups.rep.toList)
   132     }
   133 
   134     val decode: XML.Decode.T[Markups] = (body: XML.Body) =>
   135     {
   136       import XML.Decode._
   137 
   138       val markup_index: T[Markup_Index] = (body: XML.Body) =>
   139       {
   140         val (status, chunk_name) = pair(bool, Symbol.Text_Chunk.decode_name)(body)
   141         Markup_Index(status, chunk_name)
   142       }
   143 
   144       (Markups.empty /: list(pair(markup_index, Markup_Tree.from_XML(_)))(body))(_ + _)
   145     }
   146   }
   147 
   148   final class Markups private(private val rep: Map[Markup_Index, Markup_Tree])
   149   {
   150     def is_empty: Boolean = rep.isEmpty
   151 
   152     def apply(index: Markup_Index): Markup_Tree =
   153       rep.getOrElse(index, Markup_Tree.empty)
   154 
   155     def add(index: Markup_Index, markup: Text.Markup): Markups =
   156       new Markups(rep + (index -> (this(index) + markup)))
   157 
   158     def + (entry: (Markup_Index, Markup_Tree)): Markups =
   159     {
   160       val (index, tree) = entry
   161       new Markups(rep + (index -> (this(index).merge(tree, Text.Range.full, Markup.Elements.full))))
   162     }
   163 
   164     def ++ (other: Markups): Markups =
   165       if (this eq other) this
   166       else if (rep.isEmpty) other
   167       else (this /: other.rep.iterator)(_ + _)
   168 
   169     def redirection_iterator: Iterator[Document_ID.Generic] =
   170       for (Markup_Index(_, Symbol.Text_Chunk.Id(id)) <- rep.keysIterator)
   171         yield id
   172 
   173     def redirect(other_id: Document_ID.Generic): Markups =
   174     {
   175       val rep1 =
   176         (for {
   177           (Markup_Index(status, Symbol.Text_Chunk.Id(id)), markup) <- rep.iterator
   178           if other_id == id
   179         } yield (Markup_Index(status, Symbol.Text_Chunk.Default), markup)).toMap
   180       if (rep1.isEmpty) Markups.empty else new Markups(rep1)
   181     }
   182 
   183     override def hashCode: Int = rep.hashCode
   184     override def equals(that: Any): Boolean =
   185       that match {
   186         case other: Markups => rep == other.rep
   187         case _ => false
   188       }
   189     override def toString: String = rep.iterator.mkString("Markups(", ", ", ")")
   190   }
   191 
   192 
   193   /* state */
   194 
   195   object State
   196   {
   197     def get_result(states: List[State], serial: Long): Option[XML.Tree] =
   198       states.find(st => st.results.defined(serial)).map(st => st.results.get(serial).get)
   199 
   200     def get_result_proper(states: List[State], props: Properties.T): Option[Results.Entry] =
   201       for {
   202         serial <- Markup.Serial.unapply(props)
   203         tree @ XML.Elem(_, body) <- get_result(states, serial)
   204         if body.nonEmpty
   205       } yield (serial -> tree)
   206 
   207     def merge_results(states: List[State]): Results =
   208       Results.merge(states.map(_.results))
   209 
   210     def merge_exports(states: List[State]): Exports =
   211       Exports.merge(states.map(_.exports))
   212 
   213     def merge_markups(states: List[State]): Markups =
   214       Markups.merge(states.map(_.markups))
   215 
   216     def merge_markup(states: List[State], index: Markup_Index,
   217         range: Text.Range, elements: Markup.Elements): Markup_Tree =
   218       Markup_Tree.merge(states.map(_.markup(index)), range, elements)
   219 
   220     def merge(command: Command, states: List[State]): State =
   221       State(command, states.flatMap(_.status), merge_results(states),
   222         merge_exports(states), merge_markups(states))
   223 
   224 
   225     /* XML data representation */
   226 
   227     val encode: XML.Encode.T[State] = (st: State) =>
   228     {
   229       import XML.Encode._
   230 
   231       val command = st.command
   232       val blobs_names = command.blobs_names.map(_.node)
   233       val blobs_index = command.blobs_index
   234       require(command.blobs_ok)
   235 
   236       pair(long, pair(string, pair(pair(list(string), int), pair(Command_Span.encode,
   237           pair(list(Markup.encode), pair(Results.encode, Markups.encode(command.source.length)))))))(
   238         (command.id, (command.node_name.node, ((blobs_names, blobs_index), (command.span,
   239           (st.status, (st.results, st.markups)))))))
   240     }
   241 
   242     def decode(node_name: String => Document.Node.Name): XML.Decode.T[State] = (body: XML.Body) =>
   243     {
   244       import XML.Decode._
   245       val (id, (node, ((blobs_names, blobs_index), (span, (status, (results, markups)))))) =
   246         pair(long, pair(string, pair(pair(list(string), int), pair(Command_Span.decode,
   247           pair(list(Markup.decode), pair(Results.decode, Markups.decode))))))(body)
   248 
   249       val blobs_info: Blobs_Info =
   250         (blobs_names.map(name => Exn.Res((node_name(name), None)): Blob), blobs_index)
   251       val command = Command(id, node_name(node), blobs_info, span)
   252       State(command, status, results, Exports.empty, markups)
   253     }
   254   }
   255 
   256   sealed case class State(
   257     command: Command,
   258     status: List[Markup] = Nil,
   259     results: Results = Results.empty,
   260     exports: Exports = Exports.empty,
   261     markups: Markups = Markups.empty)
   262   {
   263     lazy val consolidated: Boolean =
   264       status.exists(markup => markup.name == Markup.CONSOLIDATED)
   265 
   266     lazy val protocol_status: Protocol.Status =
   267     {
   268       val warnings =
   269         if (results.iterator.exists(p => Protocol.is_warning(p._2) || Protocol.is_legacy(p._2)))
   270           List(Markup(Markup.WARNING, Nil))
   271         else Nil
   272       val errors =
   273         if (results.iterator.exists(p => Protocol.is_error(p._2)))
   274           List(Markup(Markup.ERROR, Nil))
   275         else Nil
   276       Protocol.Status.make((warnings ::: errors ::: status).iterator)
   277     }
   278 
   279     def markup(index: Markup_Index): Markup_Tree = markups(index)
   280 
   281     def redirect(other_command: Command): Option[State] =
   282     {
   283       val markups1 = markups.redirect(other_command.id)
   284       if (markups1.is_empty) None
   285       else Some(new State(other_command, markups = markups1))
   286     }
   287 
   288     private def add_status(st: Markup): State =
   289       copy(status = st :: status)
   290 
   291     private def add_result(entry: Results.Entry): State =
   292       copy(results = results + entry)
   293 
   294     def add_export(entry: Exports.Entry): Option[State] =
   295       if (command.node_name.theory == entry._2.theory_name) Some(copy(exports = exports + entry))
   296       else None
   297 
   298     private def add_markup(
   299       status: Boolean, chunk_name: Symbol.Text_Chunk.Name, m: Text.Markup): State =
   300     {
   301       val markups1 =
   302         if (status || Protocol.liberal_status_elements(m.info.name))
   303           markups.add(Markup_Index(true, chunk_name), m)
   304         else markups
   305       copy(markups = markups1.add(Markup_Index(false, chunk_name), m))
   306     }
   307 
   308     def accumulate(
   309         self_id: Document_ID.Generic => Boolean,
   310         other_id: Document_ID.Generic => Option[(Symbol.Text_Chunk.Id, Symbol.Text_Chunk)],
   311         message: XML.Elem,
   312         xml_cache: XML.Cache): State =
   313       message match {
   314         case XML.Elem(Markup(Markup.STATUS, _), msgs) =>
   315           (this /: msgs)((state, msg) =>
   316             msg match {
   317               case elem @ XML.Elem(markup, Nil) =>
   318                 state.
   319                   add_status(markup).
   320                   add_markup(true, Symbol.Text_Chunk.Default, Text.Info(command.proper_range, elem))
   321               case _ =>
   322                 Output.warning("Ignored status message: " + msg)
   323                 state
   324             })
   325 
   326         case XML.Elem(Markup(Markup.REPORT, _), msgs) =>
   327           (this /: msgs)((state, msg) =>
   328             {
   329               def bad(): Unit = Output.warning("Ignored report message: " + msg)
   330 
   331               msg match {
   332                 case XML.Elem(Markup(name, atts @ Position.Identified(id, chunk_name)), args) =>
   333 
   334                   val target =
   335                     if (self_id(id) && command.chunks.isDefinedAt(chunk_name))
   336                       Some((chunk_name, command.chunks(chunk_name)))
   337                     else if (chunk_name == Symbol.Text_Chunk.Default) other_id(id)
   338                     else None
   339 
   340                   (target, atts) match {
   341                     case (Some((target_name, target_chunk)), Position.Range(symbol_range)) =>
   342                       target_chunk.incorporate(symbol_range) match {
   343                         case Some(range) =>
   344                           val props = Position.purge(atts)
   345                           val elem = xml_cache.elem(XML.Elem(Markup(name, props), args))
   346                           state.add_markup(false, target_name, Text.Info(range, elem))
   347                         case None => bad(); state
   348                       }
   349                     case _ =>
   350                       // silently ignore excessive reports
   351                       state
   352                   }
   353 
   354                 case XML.Elem(Markup(name, atts), args)
   355                 if !atts.exists({ case (a, _) => Markup.POSITION_PROPERTIES(a) }) =>
   356                   val range = command.proper_range
   357                   val props = Position.purge(atts)
   358                   val elem = xml_cache.elem(XML.Elem(Markup(name, props), args))
   359                   state.add_markup(false, Symbol.Text_Chunk.Default, Text.Info(range, elem))
   360 
   361                 case _ => bad(); state
   362               }
   363             })
   364         case XML.Elem(Markup(name, props), body) =>
   365           props match {
   366             case Markup.Serial(i) =>
   367               val markup_message =
   368                 xml_cache.elem(XML.Elem(Markup(Markup.message(name), props), body))
   369               val message_markup =
   370                 xml_cache.elem(XML.elem(Markup(name, props.filter(p => p._1 == Markup.SERIAL))))
   371 
   372               var st = add_result(i -> markup_message)
   373               if (Protocol.is_inlined(message)) {
   374                 for {
   375                   (chunk_name, chunk) <- command.chunks.iterator
   376                   range <- Protocol_Message.positions(
   377                     self_id, command.span.position, chunk_name, chunk, message)
   378                 } st = st.add_markup(false, chunk_name, Text.Info(range, message_markup))
   379               }
   380               st
   381 
   382             case _ =>
   383               Output.warning("Ignored message without serial number: " + message)
   384               this
   385           }
   386       }
   387   }
   388 
   389 
   390 
   391   /** static content **/
   392 
   393   /* make commands */
   394 
   395   def apply(
   396     id: Document_ID.Command,
   397     node_name: Document.Node.Name,
   398     blobs_info: Blobs_Info,
   399     span: Command_Span.Span): Command =
   400   {
   401     val (source, span1) = span.compact_source
   402     new Command(id, node_name, blobs_info, span1, source, Results.empty, Markup_Tree.empty)
   403   }
   404 
   405   val empty: Command =
   406     Command(Document_ID.none, Document.Node.Name.empty, no_blobs, Command_Span.empty)
   407 
   408   def unparsed(
   409     id: Document_ID.Command,
   410     source: String,
   411     results: Results,
   412     markup: Markup_Tree): Command =
   413   {
   414     val (source1, span1) = Command_Span.unparsed(source).compact_source
   415     new Command(id, Document.Node.Name.empty, no_blobs, span1, source1, results, markup)
   416   }
   417 
   418   def text(source: String): Command =
   419     unparsed(Document_ID.none, source, Results.empty, Markup_Tree.empty)
   420 
   421   def rich_text(id: Document_ID.Command, results: Results, body: XML.Body): Command =
   422   {
   423     val text = XML.content(body)
   424     val markup = Markup_Tree.from_XML(body)
   425     unparsed(id, text, results, markup)
   426   }
   427 
   428 
   429   /* perspective */
   430 
   431   object Perspective
   432   {
   433     val empty: Perspective = Perspective(Nil)
   434   }
   435 
   436   sealed case class Perspective(commands: List[Command])  // visible commands in canonical order
   437   {
   438     def is_empty: Boolean = commands.isEmpty
   439 
   440     def same(that: Perspective): Boolean =
   441     {
   442       val cmds1 = this.commands
   443       val cmds2 = that.commands
   444       require(!cmds1.exists(_.is_undefined))
   445       require(!cmds2.exists(_.is_undefined))
   446       cmds1.length == cmds2.length &&
   447         (cmds1.iterator zip cmds2.iterator).forall({ case (c1, c2) => c1.id == c2.id })
   448     }
   449   }
   450 
   451 
   452   /* blobs: inlined errors and auxiliary files */
   453 
   454   private def clean_tokens(tokens: List[Token]): List[(Token, Int)] =
   455   {
   456     def clean(toks: List[(Token, Int)]): List[(Token, Int)] =
   457       toks match {
   458         case (t1, i1) :: (t2, i2) :: rest =>
   459           if (t1.is_keyword && t1.source == "%" && t2.is_name) clean(rest)
   460           else (t1, i1) :: clean((t2, i2) :: rest)
   461         case _ => toks
   462       }
   463     clean(tokens.zipWithIndex.filter({ case (t, _) => t.is_proper }))
   464   }
   465 
   466   private def find_file(tokens: List[(Token, Int)]): Option[(String, Int)] =
   467     if (tokens.exists({ case (t, _) => t.is_command })) {
   468       tokens.dropWhile({ case (t, _) => !t.is_command }).
   469         collectFirst({ case (t, i) if t.is_embedded => (t.content, i) })
   470     }
   471     else None
   472 
   473   def span_files(syntax: Outer_Syntax, span: Command_Span.Span): (List[String], Int) =
   474     syntax.load_command(span.name) match {
   475       case Some(exts) =>
   476         find_file(clean_tokens(span.content)) match {
   477           case Some((file, i)) =>
   478             if (exts.isEmpty) (List(file), i)
   479             else (exts.map(ext => file + "." + ext), i)
   480           case None => (Nil, -1)
   481         }
   482       case None => (Nil, -1)
   483     }
   484 
   485   def blobs_info(
   486     resources: Resources,
   487     syntax: Outer_Syntax,
   488     get_blob: Document.Node.Name => Option[Document.Blob],
   489     can_import: Document.Node.Name => Boolean,
   490     node_name: Document.Node.Name,
   491     span: Command_Span.Span): Blobs_Info =
   492   {
   493     span.name match {
   494       // inlined errors
   495       case Thy_Header.THEORY =>
   496         val reader = Scan.char_reader(Token.implode(span.content))
   497         val imports = resources.check_thy_reader(node_name, reader).imports
   498         val raw_imports =
   499           try {
   500             val imports1 = Thy_Header.read(reader, Token.Pos.none).imports
   501             if (imports.length == imports1.length) imports1.map(_._1) else error("")
   502           }
   503           catch { case exn: Throwable => List.fill(imports.length)("") }
   504 
   505         val errors =
   506           for { ((import_name, pos), s) <- imports zip raw_imports if !can_import(import_name) }
   507           yield {
   508             val completion =
   509               if (Thy_Header.is_base_name(s)) {
   510                 val completed = Completion.completed(import_name.theory_base_name)
   511                 val qualifier = resources.session_base.theory_qualifier(node_name)
   512                 val dir = node_name.master_dir
   513                 for {
   514                   known_name <- resources.session_base.known.theory_names
   515                   if completed(known_name.theory_base_name)
   516                 }
   517                 yield {
   518                   resources.standard_import(
   519                     resources.session_base, qualifier, dir, known_name.theory)
   520                 }
   521               }.sorted
   522               else Nil
   523             val msg =
   524               "Bad theory import " +
   525                 Markup.Path(import_name.node).markup(quote(import_name.toString)) +
   526                 Position.here(pos) + Completion.report_theories(pos, completion)
   527             Exn.Exn(ERROR(msg)): Command.Blob
   528           }
   529         (errors, -1)
   530 
   531       // auxiliary files
   532       case _ =>
   533         val (files, index) = span_files(syntax, span)
   534         val blobs =
   535           files.map(file =>
   536             (Exn.capture {
   537               val name = Document.Node.Name(resources.append(node_name, Path.explode(file)))
   538               val blob = get_blob(name).map(blob => ((blob.bytes.sha1_digest, blob.chunk)))
   539               (name, blob)
   540             }).user_error)
   541         (blobs, index)
   542     }
   543   }
   544 }
   545 
   546 
   547 final class Command private(
   548     val id: Document_ID.Command,
   549     val node_name: Document.Node.Name,
   550     val blobs_info: Command.Blobs_Info,
   551     val span: Command_Span.Span,
   552     val source: String,
   553     val init_results: Command.Results,
   554     val init_markup: Markup_Tree)
   555 {
   556   override def toString: String = id + "/" + span.kind.toString
   557 
   558 
   559   /* classification */
   560 
   561   def is_proper: Boolean = span.kind.isInstanceOf[Command_Span.Command_Span]
   562   def is_ignored: Boolean = span.kind == Command_Span.Ignored_Span
   563 
   564   def is_undefined: Boolean = id == Document_ID.none
   565   val is_unparsed: Boolean = span.content.exists(_.is_unparsed)
   566   val is_unfinished: Boolean = span.content.exists(_.is_unfinished)
   567 
   568 
   569   /* blobs */
   570 
   571   def blobs: List[Command.Blob] = blobs_info._1
   572   def blobs_index: Int = blobs_info._2
   573 
   574   def blobs_ok: Boolean =
   575     blobs.forall({ case Exn.Res(_) => true case _ => false })
   576 
   577   def blobs_names: List[Document.Node.Name] =
   578     for (Exn.Res((name, _)) <- blobs) yield name
   579 
   580   def blobs_undefined: List[Document.Node.Name] =
   581     for (Exn.Res((name, None)) <- blobs) yield name
   582 
   583   def blobs_defined: List[(Document.Node.Name, SHA1.Digest)] =
   584     for (Exn.Res((name, Some((digest, _)))) <- blobs) yield (name, digest)
   585 
   586   def blobs_changed(doc_blobs: Document.Blobs): Boolean =
   587     blobs.exists({ case Exn.Res((name, _)) => doc_blobs.changed(name) case _ => false })
   588 
   589 
   590   /* source chunks */
   591 
   592   val chunk: Symbol.Text_Chunk = Symbol.Text_Chunk(source)
   593 
   594   val chunks: Map[Symbol.Text_Chunk.Name, Symbol.Text_Chunk] =
   595     ((Symbol.Text_Chunk.Default -> chunk) ::
   596       (for (Exn.Res((name, Some((_, file)))) <- blobs)
   597         yield Symbol.Text_Chunk.File(name.node) -> file)).toMap
   598 
   599   def length: Int = source.length
   600   def range: Text.Range = chunk.range
   601 
   602   val proper_range: Text.Range =
   603     Text.Range(0,
   604       (length /: span.content.reverse.iterator.takeWhile(_.is_improper))(_ - _.source.length))
   605 
   606   def source(range: Text.Range): String = range.substring(source)
   607 
   608 
   609   /* accumulated results */
   610 
   611   val init_state: Command.State =
   612     Command.State(this, results = init_results, markups = Command.Markups.init(init_markup))
   613 
   614   val empty_state: Command.State = Command.State(this)
   615 }