src/Pure/PIDE/command.scala
author wenzelm
Sat Apr 01 15:35:32 2017 +0200 (2017-04-01 ago)
changeset 65341 c82a1620b274
parent 65335 7634d33c1a79
child 65359 9ca34f0407a9
permissions -rw-r--r--
tuned signature;
     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     /* 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   /* markup */
    72 
    73   object Markup_Index
    74   {
    75     val markup: Markup_Index = Markup_Index(false, Symbol.Text_Chunk.Default)
    76   }
    77 
    78   sealed case class Markup_Index(status: Boolean, chunk_name: Symbol.Text_Chunk.Name)
    79 
    80   object Markups
    81   {
    82     val empty: Markups = new Markups(Map.empty)
    83     def init(markup: Markup_Tree): Markups = new Markups(Map(Markup_Index.markup -> markup))
    84     def merge(args: TraversableOnce[Markups]): Markups = (empty /: args)(_ ++ _)
    85 
    86 
    87     /* XML data representation */
    88 
    89     def encode(source_length: Int): XML.Encode.T[Markups] = (markups: Markups) =>
    90     {
    91       import XML.Encode._
    92 
    93       val markup_index: T[Markup_Index] = (index: Markup_Index) =>
    94         pair(bool, Symbol.Text_Chunk.encode_name)(index.status, index.chunk_name)
    95 
    96       val markup_tree: T[Markup_Tree] =
    97         _.to_XML(Text.Range(0, source_length), Symbol.spaces(source_length), Markup.Elements.full)
    98 
    99       list(pair(markup_index, markup_tree))(markups.rep.toList)
   100     }
   101 
   102     val decode: XML.Decode.T[Markups] = (body: XML.Body) =>
   103     {
   104       import XML.Decode._
   105 
   106       val markup_index: T[Markup_Index] = (body: XML.Body) =>
   107       {
   108         val (status, chunk_name) = pair(bool, Symbol.Text_Chunk.decode_name)(body)
   109         Markup_Index(status, chunk_name)
   110       }
   111 
   112       (Markups.empty /: list(pair(markup_index, Markup_Tree.from_XML(_)))(body))(_ + _)
   113     }
   114   }
   115 
   116   final class Markups private(private val rep: Map[Markup_Index, Markup_Tree])
   117   {
   118     def is_empty: Boolean = rep.isEmpty
   119 
   120     def apply(index: Markup_Index): Markup_Tree =
   121       rep.getOrElse(index, Markup_Tree.empty)
   122 
   123     def add(index: Markup_Index, markup: Text.Markup): Markups =
   124       new Markups(rep + (index -> (this(index) + markup)))
   125 
   126     def + (entry: (Markup_Index, Markup_Tree)): Markups =
   127     {
   128       val (index, tree) = entry
   129       new Markups(rep + (index -> (this(index).merge(tree, Text.Range.full, Markup.Elements.full))))
   130     }
   131 
   132     def ++ (other: Markups): Markups =
   133       if (this eq other) this
   134       else if (rep.isEmpty) other
   135       else (this /: other.rep.iterator)(_ + _)
   136 
   137     def redirection_iterator: Iterator[Document_ID.Generic] =
   138       for (Markup_Index(_, Symbol.Text_Chunk.Id(id)) <- rep.keysIterator)
   139         yield id
   140 
   141     def redirect(other_id: Document_ID.Generic): Markups =
   142     {
   143       val rep1 =
   144         (for {
   145           (Markup_Index(status, Symbol.Text_Chunk.Id(id)), markup) <- rep.iterator
   146           if other_id == id
   147         } yield (Markup_Index(status, Symbol.Text_Chunk.Default), markup)).toMap
   148       if (rep1.isEmpty) Markups.empty else new Markups(rep1)
   149     }
   150 
   151     override def hashCode: Int = rep.hashCode
   152     override def equals(that: Any): Boolean =
   153       that match {
   154         case other: Markups => rep == other.rep
   155         case _ => false
   156       }
   157     override def toString: String = rep.iterator.mkString("Markups(", ", ", ")")
   158   }
   159 
   160 
   161   /* state */
   162 
   163   object State
   164   {
   165     def merge_results(states: List[State]): Results =
   166       Results.merge(states.map(_.results))
   167 
   168     def merge_markups(states: List[State]): Markups =
   169       Markups.merge(states.map(_.markups))
   170 
   171     def merge_markup(states: List[State], index: Markup_Index,
   172         range: Text.Range, elements: Markup.Elements): Markup_Tree =
   173       Markup_Tree.merge(states.map(_.markup(index)), range, elements)
   174 
   175     def merge(command: Command, states: List[State]): State =
   176       State(command, states.flatMap(_.status), merge_results(states), merge_markups(states))
   177 
   178 
   179     /* XML data representation */
   180 
   181     val encode: XML.Encode.T[State] = (st: State) =>
   182     {
   183       import XML.Encode._
   184 
   185       val command = st.command
   186       val blobs_names = command.blobs_names.map(_.node)
   187       val blobs_index = command.blobs_index
   188       require(command.blobs_ok)
   189 
   190       pair(long, pair(string, pair(pair(list(string), int), pair(Command_Span.encode,
   191           pair(list(Markup.encode), pair(Results.encode, Markups.encode(command.source.length)))))))(
   192         (command.id, (command.node_name.node, ((blobs_names, blobs_index), (command.span,
   193           (st.status, (st.results, st.markups)))))))
   194     }
   195 
   196     def decode(node_name: String => Document.Node.Name): XML.Decode.T[State] = (body: XML.Body) =>
   197     {
   198       import XML.Decode._
   199       val (id, (node, ((blobs_names, blobs_index), (span, (status, (results, markups)))))) =
   200         pair(long, pair(string, pair(pair(list(string), int), pair(Command_Span.decode,
   201           pair(list(Markup.decode), pair(Results.decode, Markups.decode))))))(body)
   202 
   203       val blobs_info: Blobs_Info =
   204         (blobs_names.map(name => Exn.Res((node_name(name), None)): Blob), blobs_index)
   205       val command = Command(id, node_name(node), blobs_info, span)
   206       State(command, status, results, markups)
   207     }
   208   }
   209 
   210   sealed case class State(
   211     command: Command,
   212     status: List[Markup] = Nil,
   213     results: Results = Results.empty,
   214     markups: Markups = Markups.empty)
   215   {
   216     lazy val protocol_status: Protocol.Status =
   217     {
   218       val warnings =
   219         if (results.iterator.exists(p => Protocol.is_warning(p._2) || Protocol.is_legacy(p._2)))
   220           List(Markup(Markup.WARNING, Nil))
   221         else Nil
   222       val errors =
   223         if (results.iterator.exists(p => Protocol.is_error(p._2)))
   224           List(Markup(Markup.ERROR, Nil))
   225         else Nil
   226       Protocol.Status.make((warnings ::: errors ::: status).iterator)
   227     }
   228 
   229     def markup(index: Markup_Index): Markup_Tree = markups(index)
   230 
   231     def redirect(other_command: Command): Option[State] =
   232     {
   233       val markups1 = markups.redirect(other_command.id)
   234       if (markups1.is_empty) None
   235       else Some(new State(other_command, Nil, Results.empty, markups1))
   236     }
   237 
   238     private def add_status(st: Markup): State =
   239       copy(status = st :: status)
   240 
   241     private def add_markup(
   242       status: Boolean, chunk_name: Symbol.Text_Chunk.Name, m: Text.Markup): State =
   243     {
   244       val markups1 =
   245         if (status || Protocol.liberal_status_elements(m.info.name))
   246           markups.add(Markup_Index(true, chunk_name), m)
   247         else markups
   248       copy(markups = markups1.add(Markup_Index(false, chunk_name), m))
   249     }
   250 
   251     def accumulate(
   252         self_id: Document_ID.Generic => Boolean,
   253         other_id: Document_ID.Generic => Option[(Symbol.Text_Chunk.Id, Symbol.Text_Chunk)],
   254         message: XML.Elem): State =
   255       message match {
   256         case XML.Elem(Markup(Markup.STATUS, _), msgs) =>
   257           (this /: msgs)((state, msg) =>
   258             msg match {
   259               case elem @ XML.Elem(markup, Nil) =>
   260                 state.
   261                   add_status(markup).
   262                   add_markup(true, Symbol.Text_Chunk.Default, Text.Info(command.proper_range, elem))
   263               case _ =>
   264                 Output.warning("Ignored status message: " + msg)
   265                 state
   266             })
   267 
   268         case XML.Elem(Markup(Markup.REPORT, _), msgs) =>
   269           (this /: msgs)((state, msg) =>
   270             {
   271               def bad(): Unit = Output.warning("Ignored report message: " + msg)
   272 
   273               msg match {
   274                 case XML.Elem(Markup(name, atts @ Position.Identified(id, chunk_name)), args) =>
   275 
   276                   val target =
   277                     if (self_id(id) && command.chunks.isDefinedAt(chunk_name))
   278                       Some((chunk_name, command.chunks(chunk_name)))
   279                     else if (chunk_name == Symbol.Text_Chunk.Default) other_id(id)
   280                     else None
   281 
   282                   (target, atts) match {
   283                     case (Some((target_name, target_chunk)), Position.Range(symbol_range)) =>
   284                       target_chunk.incorporate(symbol_range) match {
   285                         case Some(range) =>
   286                           val props = Position.purge(atts)
   287                           val info = Text.Info(range, XML.Elem(Markup(name, props), args))
   288                           state.add_markup(false, target_name, info)
   289                         case None => bad(); state
   290                       }
   291                     case _ =>
   292                       // silently ignore excessive reports
   293                       state
   294                   }
   295 
   296                 case XML.Elem(Markup(name, atts), args)
   297                 if !atts.exists({ case (a, _) => Markup.POSITION_PROPERTIES(a) }) =>
   298                   val range = command.proper_range
   299                   val props = Position.purge(atts)
   300                   val info: Text.Markup = Text.Info(range, XML.Elem(Markup(name, props), args))
   301                   state.add_markup(false, Symbol.Text_Chunk.Default, info)
   302 
   303                 case _ => bad(); state
   304               }
   305             })
   306         case XML.Elem(Markup(name, props), body) =>
   307           props match {
   308             case Markup.Serial(i) =>
   309               val message1 = XML.Elem(Markup(Markup.message(name), props), body)
   310               val message2 = XML.Elem(Markup(name, props), body)
   311 
   312               var st = copy(results = results + (i -> message1))
   313               if (Protocol.is_inlined(message)) {
   314                 for {
   315                   (chunk_name, chunk) <- command.chunks.iterator
   316                   range <- Protocol_Message.positions(
   317                     self_id, command.span.position, chunk_name, chunk, message)
   318                 } st = st.add_markup(false, chunk_name, Text.Info(range, message2))
   319               }
   320               st
   321 
   322             case _ =>
   323               Output.warning("Ignored message without serial number: " + message)
   324               this
   325           }
   326     }
   327   }
   328 
   329 
   330 
   331   /** static content **/
   332 
   333   /* make commands */
   334 
   335   def apply(
   336     id: Document_ID.Command,
   337     node_name: Document.Node.Name,
   338     blobs_info: Blobs_Info,
   339     span: Command_Span.Span): Command =
   340   {
   341     val (source, span1) = span.compact_source
   342     new Command(id, node_name, blobs_info, span1, source, Results.empty, Markup_Tree.empty)
   343   }
   344 
   345   val empty: Command =
   346     Command(Document_ID.none, Document.Node.Name.empty, no_blobs, Command_Span.empty)
   347 
   348   def unparsed(
   349     id: Document_ID.Command,
   350     source: String,
   351     results: Results,
   352     markup: Markup_Tree): Command =
   353   {
   354     val (source1, span1) = Command_Span.unparsed(source).compact_source
   355     new Command(id, Document.Node.Name.empty, no_blobs, span1, source1, results, markup)
   356   }
   357 
   358   def text(source: String): Command =
   359     unparsed(Document_ID.none, source, Results.empty, Markup_Tree.empty)
   360 
   361   def rich_text(id: Document_ID.Command, results: Results, body: XML.Body): Command =
   362   {
   363     val text = XML.content(body)
   364     val markup = Markup_Tree.from_XML(body)
   365     unparsed(id, text, results, markup)
   366   }
   367 
   368 
   369   /* perspective */
   370 
   371   object Perspective
   372   {
   373     val empty: Perspective = Perspective(Nil)
   374   }
   375 
   376   sealed case class Perspective(commands: List[Command])  // visible commands in canonical order
   377   {
   378     def is_empty: Boolean = commands.isEmpty
   379 
   380     def same(that: Perspective): Boolean =
   381     {
   382       val cmds1 = this.commands
   383       val cmds2 = that.commands
   384       require(!cmds1.exists(_.is_undefined))
   385       require(!cmds2.exists(_.is_undefined))
   386       cmds1.length == cmds2.length &&
   387         (cmds1.iterator zip cmds2.iterator).forall({ case (c1, c2) => c1.id == c2.id })
   388     }
   389   }
   390 
   391 
   392   /* blobs: inlined errors and auxiliary files */
   393 
   394   private def clean_tokens(tokens: List[Token]): List[(Token, Int)] =
   395   {
   396     val markers = Set("%", "--", Symbol.comment, Symbol.comment_decoded)
   397     def clean(toks: List[(Token, Int)]): List[(Token, Int)] =
   398       toks match {
   399         case (t1, i1) :: (t2, i2) :: rest =>
   400           if (t1.is_keyword && markers(t1.source)) clean(rest)
   401           else (t1, i1) :: clean((t2, i2) :: rest)
   402         case _ => toks
   403       }
   404     clean(tokens.zipWithIndex.filter({ case (t, _) => t.is_proper }))
   405   }
   406 
   407   private def find_file(tokens: List[(Token, Int)]): Option[(String, Int)] =
   408     if (tokens.exists({ case (t, _) => t.is_command })) {
   409       tokens.dropWhile({ case (t, _) => !t.is_command }).
   410         collectFirst({ case (t, i) if t.is_embedded => (t.content, i) })
   411     }
   412     else None
   413 
   414   def span_files(syntax: Outer_Syntax, span: Command_Span.Span): (List[String], Int) =
   415     syntax.load_command(span.name) match {
   416       case Some(exts) =>
   417         find_file(clean_tokens(span.content)) match {
   418           case Some((file, i)) =>
   419             if (exts.isEmpty) (List(file), i)
   420             else (exts.map(ext => file + "." + ext), i)
   421           case None => (Nil, -1)
   422         }
   423       case None => (Nil, -1)
   424     }
   425 
   426   def blobs_info(
   427     resources: Resources,
   428     syntax: Outer_Syntax,
   429     get_blob: Document.Node.Name => Option[Document.Blob],
   430     can_import: Document.Node.Name => Boolean,
   431     node_name: Document.Node.Name,
   432     span: Command_Span.Span): Blobs_Info =
   433   {
   434     span.name match {
   435       // inlined errors
   436       case Thy_Header.THEORY =>
   437         val reader = Scan.char_reader(Token.implode(span.content))
   438         val header = resources.check_thy_reader("", node_name, reader)
   439         val errors =
   440           for ((imp, pos) <- header.imports if !can_import(imp)) yield {
   441             val msg =
   442               "Bad theory import " +
   443                 Markup.Path(imp.node).markup(quote(imp.toString)) + Position.here(pos)
   444             Exn.Exn(ERROR(msg)): Command.Blob
   445           }
   446         (errors, -1)
   447 
   448       // auxiliary files
   449       case _ =>
   450         val (files, index) = span_files(syntax, span)
   451         val blobs =
   452           files.map(file =>
   453             (Exn.capture {
   454               val name =
   455                 Document.Node.Name(resources.append(node_name.master_dir, Path.explode(file)))
   456               val blob = get_blob(name).map(blob => ((blob.bytes.sha1_digest, blob.chunk)))
   457               (name, blob)
   458             }).user_error)
   459         (blobs, index)
   460     }
   461   }
   462 }
   463 
   464 
   465 final class Command private(
   466     val id: Document_ID.Command,
   467     val node_name: Document.Node.Name,
   468     val blobs_info: Command.Blobs_Info,
   469     val span: Command_Span.Span,
   470     val source: String,
   471     val init_results: Command.Results,
   472     val init_markup: Markup_Tree)
   473 {
   474   override def toString: String = id + "/" + span.kind.toString
   475 
   476 
   477   /* classification */
   478 
   479   def is_proper: Boolean = span.kind.isInstanceOf[Command_Span.Command_Span]
   480   def is_ignored: Boolean = span.kind == Command_Span.Ignored_Span
   481 
   482   def is_undefined: Boolean = id == Document_ID.none
   483   val is_unparsed: Boolean = span.content.exists(_.is_unparsed)
   484   val is_unfinished: Boolean = span.content.exists(_.is_unfinished)
   485 
   486 
   487   /* blobs */
   488 
   489   def blobs: List[Command.Blob] = blobs_info._1
   490   def blobs_index: Int = blobs_info._2
   491 
   492   def blobs_ok: Boolean =
   493     blobs.forall({ case Exn.Res(_) => true case _ => false })
   494 
   495   def blobs_names: List[Document.Node.Name] =
   496     for (Exn.Res((name, _)) <- blobs) yield name
   497 
   498   def blobs_undefined: List[Document.Node.Name] =
   499     for (Exn.Res((name, None)) <- blobs) yield name
   500 
   501   def blobs_defined: List[(Document.Node.Name, SHA1.Digest)] =
   502     for (Exn.Res((name, Some((digest, _)))) <- blobs) yield (name, digest)
   503 
   504   def blobs_changed(doc_blobs: Document.Blobs): Boolean =
   505     blobs.exists({ case Exn.Res((name, _)) => doc_blobs.changed(name) case _ => false })
   506 
   507 
   508   /* source chunks */
   509 
   510   val chunk: Symbol.Text_Chunk = Symbol.Text_Chunk(source)
   511 
   512   val chunks: Map[Symbol.Text_Chunk.Name, Symbol.Text_Chunk] =
   513     ((Symbol.Text_Chunk.Default -> chunk) ::
   514       (for (Exn.Res((name, Some((_, file)))) <- blobs)
   515         yield Symbol.Text_Chunk.File(name.node) -> file)).toMap
   516 
   517   def length: Int = source.length
   518   def range: Text.Range = chunk.range
   519 
   520   val proper_range: Text.Range =
   521     Text.Range(0,
   522       (length /: span.content.reverse.iterator.takeWhile(_.is_improper))(_ - _.source.length))
   523 
   524   def source(range: Text.Range): String = source.substring(range.start, range.stop)
   525 
   526 
   527   /* accumulated results */
   528 
   529   val init_state: Command.State =
   530     Command.State(this, results = init_results, markups = Command.Markups.init(init_markup))
   531 
   532   val empty_state: Command.State = Command.State(this)
   533 }