src/Pure/PIDE/command.scala
author wenzelm
Sat Mar 01 22:46:31 2014 +0100 (2014-03-01)
changeset 55828 42ac3cfb89f6
parent 55822 ccf2d784be97
child 55884 f2c0eaedd579
permissions -rw-r--r--
clarified language markup: added "delimited" property;
type Symbol_Pos.source preserves information about delimited outer tokens (e.g string, cartouche);
observe Completion.Language_Context only for delimited languages, which is important to complete keywords after undelimited inner tokens, e.g. "lemma A pro";
     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: Iterable[Results.Entry]): Results = (empty /: es.iterator)(_ + _)
    31     def merge(rs: Iterable[Results]): Results = (empty /: rs.iterator)(_ ++ _)
    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 entries: 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.entries)(_ + _)
    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 = entries.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     def ++ (other: Markups): Markups =
    85       new Markups(
    86         (rep.keySet ++ other.rep.keySet)
    87           .map(index => index -> (this(index) ++ other(index))).toMap)
    88 
    89     override def hashCode: Int = rep.hashCode
    90     override def equals(that: Any): Boolean =
    91       that match {
    92         case other: Markups => rep == other.rep
    93         case _ => false
    94       }
    95     override def toString: String = rep.iterator.mkString("Markups(", ", ", ")")
    96   }
    97 
    98 
    99   /* state */
   100 
   101   sealed case class State(
   102     command: Command,
   103     status: List[Markup] = Nil,
   104     results: Results = Results.empty,
   105     markups: Markups = Markups.empty)
   106   {
   107     /* markup */
   108 
   109     def markup(index: Markup_Index): Markup_Tree = markups(index)
   110 
   111     def markup_to_XML(filter: XML.Elem => Boolean): XML.Body =
   112       markup(Markup_Index.markup).to_XML(command.range, command.source, filter)
   113 
   114 
   115     /* content */
   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 + (alt_id: Document_ID.Generic, 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, atts @ Position.Reported(id, file_name, raw_range)), args)
   156                 if id == command.id || id == alt_id =>
   157                   command.chunks.get(file_name) match {
   158                     case Some(chunk) =>
   159                       chunk.incorporate(raw_range) match {
   160                         case Some(range) =>
   161                           val props = Position.purge(atts)
   162                           val info = Text.Info(range, XML.Elem(Markup(name, props), args))
   163                           state.add_markup(false, file_name, info)
   164                         case None => bad(); state
   165                       }
   166                     case None => bad(); state
   167                   }
   168 
   169                 case XML.Elem(Markup(name, atts), args)
   170                 if !atts.exists({ case (a, _) => Markup.POSITION_PROPERTIES(a) }) =>
   171                   val range = command.proper_range
   172                   val props = Position.purge(atts)
   173                   val info: Text.Markup = Text.Info(range, XML.Elem(Markup(name, props), args))
   174                   state.add_markup(false, "", info)
   175 
   176                 case _ => /* FIXME bad(); */ state
   177               }
   178             })
   179         case XML.Elem(Markup(name, props), body) =>
   180           props match {
   181             case Markup.Serial(i) =>
   182               val message1 = XML.Elem(Markup(Markup.message(name), props), body)
   183               val message2 = XML.Elem(Markup(name, props), body)
   184 
   185               var st = copy(results = results + (i -> message1))
   186               if (Protocol.is_inlined(message)) {
   187                 for {
   188                   (file_name, chunk) <- command.chunks
   189                   range <- Protocol.message_positions(command.id, alt_id, chunk, message)
   190                 } st = st.add_markup(false, file_name, Text.Info(range, message2))
   191               }
   192               st
   193 
   194             case _ =>
   195               System.err.println("Ignored message without serial number: " + message)
   196               this
   197           }
   198       }
   199 
   200     def ++ (other: State): State =
   201       copy(
   202         status = other.status ::: status,
   203         results = results ++ other.results,
   204         markups = markups ++ other.markups
   205       )
   206   }
   207 
   208 
   209 
   210   /** static content **/
   211 
   212   /* text chunks */
   213 
   214   abstract class Chunk
   215   {
   216     def file_name: String
   217     def length: Int
   218     def range: Text.Range
   219     def decode(raw_range: Text.Range): Text.Range
   220 
   221     def incorporate(raw_range: Text.Range): Option[Text.Range] =
   222     {
   223       def inc(r: Text.Range): Option[Text.Range] =
   224         range.try_restrict(decode(r)) match {
   225           case Some(r1) if !r1.is_singularity => Some(r1)
   226           case _ => None
   227         }
   228      inc(raw_range) orElse inc(raw_range - 1)
   229     }
   230   }
   231 
   232   class File(val file_name: String, text: CharSequence) extends Chunk
   233   {
   234     val length = text.length
   235     val range = Text.Range(0, length)
   236     private val symbol_index = Symbol.Index(text)
   237     def decode(raw_range: Text.Range): Text.Range = symbol_index.decode(raw_range)
   238 
   239     override def toString: String = "Command.File(" + file_name + ")"
   240   }
   241 
   242 
   243   /* make commands */
   244 
   245   def name(span: List[Token]): String =
   246     span.find(_.is_command) match { case Some(tok) => tok.source case _ => "" }
   247 
   248   private def source_span(span: List[Token]): (String, List[Token]) =
   249   {
   250     val source: String =
   251       span match {
   252         case List(tok) => tok.source
   253         case _ => span.map(_.source).mkString
   254       }
   255 
   256     val span1 = new mutable.ListBuffer[Token]
   257     var i = 0
   258     for (Token(kind, s) <- span) {
   259       val n = s.length
   260       val s1 = source.substring(i, i + n)
   261       span1 += Token(kind, s1)
   262       i += n
   263     }
   264     (source, span1.toList)
   265   }
   266 
   267   def apply(
   268     id: Document_ID.Command,
   269     node_name: Document.Node.Name,
   270     blobs: List[Blob],
   271     span: List[Token]): Command =
   272   {
   273     val (source, span1) = source_span(span)
   274     new Command(id, node_name, blobs, span1, source, Results.empty, Markup_Tree.empty)
   275   }
   276 
   277   val empty: Command = Command(Document_ID.none, Document.Node.Name.empty, Nil, Nil)
   278 
   279   def unparsed(
   280     id: Document_ID.Command,
   281     source: String,
   282     results: Results,
   283     markup: Markup_Tree): Command =
   284   {
   285     val (source1, span) = source_span(List(Token(Token.Kind.UNPARSED, source)))
   286     new Command(id, Document.Node.Name.empty, Nil, span, source1, results, markup)
   287   }
   288 
   289   def unparsed(source: String): Command =
   290     unparsed(Document_ID.none, source, Results.empty, Markup_Tree.empty)
   291 
   292   def rich_text(id: Document_ID.Command, results: Results, body: XML.Body): Command =
   293   {
   294     val text = XML.content(body)
   295     val markup = Markup_Tree.from_XML(body)
   296     unparsed(id, text, results, markup)
   297   }
   298 
   299 
   300   /* perspective */
   301 
   302   object Perspective
   303   {
   304     val empty: Perspective = Perspective(Nil)
   305   }
   306 
   307   sealed case class Perspective(commands: List[Command])  // visible commands in canonical order
   308   {
   309     def same(that: Perspective): Boolean =
   310     {
   311       val cmds1 = this.commands
   312       val cmds2 = that.commands
   313       require(!cmds1.exists(_.is_undefined))
   314       require(!cmds2.exists(_.is_undefined))
   315       cmds1.length == cmds2.length &&
   316         (cmds1.iterator zip cmds2.iterator).forall({ case (c1, c2) => c1.id == c2.id })
   317     }
   318   }
   319 }
   320 
   321 
   322 final class Command private(
   323     val id: Document_ID.Command,
   324     val node_name: Document.Node.Name,
   325     val blobs: List[Command.Blob],
   326     val span: List[Token],
   327     val source: String,
   328     val init_results: Command.Results,
   329     val init_markup: Markup_Tree)
   330   extends Command.Chunk
   331 {
   332   /* classification */
   333 
   334   def is_undefined: Boolean = id == Document_ID.none
   335   val is_unparsed: Boolean = span.exists(_.is_unparsed)
   336   val is_unfinished: Boolean = span.exists(_.is_unfinished)
   337 
   338   val is_ignored: Boolean = !span.exists(_.is_proper)
   339   val is_malformed: Boolean = !is_ignored && (!span.head.is_command || span.exists(_.is_error))
   340   def is_command: Boolean = !is_ignored && !is_malformed
   341 
   342   def name: String = Command.name(span)
   343 
   344   override def toString =
   345     id + "/" + (if (is_command) name else if (is_ignored) "IGNORED" else "MALFORMED")
   346 
   347 
   348   /* blobs */
   349 
   350   def blobs_changed(doc_blobs: Document.Blobs): Boolean =
   351     blobs.exists({ case Exn.Res((name, _)) => doc_blobs.changed(name) case _ => false })
   352 
   353   def blobs_names: List[Document.Node.Name] =
   354     for (Exn.Res((name, _)) <- blobs) yield name
   355 
   356   def blobs_digests: List[SHA1.Digest] =
   357     for (Exn.Res((_, Some((digest, _)))) <- blobs) yield digest
   358 
   359   val chunks: Map[String, Command.Chunk] =
   360     (("" -> this) ::
   361       (for (Exn.Res((name, Some((_, file)))) <- blobs) yield (name.node -> file))).toMap
   362 
   363 
   364   /* source */
   365 
   366   def file_name: String = ""
   367 
   368   def length: Int = source.length
   369   val range: Text.Range = Text.Range(0, length)
   370 
   371   val proper_range: Text.Range =
   372     Text.Range(0, (length /: span.reverse.iterator.takeWhile(_.is_improper))(_ - _.source.length))
   373 
   374   def source(range: Text.Range): String = source.substring(range.start, range.stop)
   375 
   376   private lazy val symbol_index = Symbol.Index(source)
   377   def decode(raw_offset: Text.Offset): Text.Offset = symbol_index.decode(raw_offset)
   378   def decode(raw_range: Text.Range): Text.Range = symbol_index.decode(raw_range)
   379 
   380 
   381   /* accumulated results */
   382 
   383   val init_state: Command.State =
   384     Command.State(this, results = init_results, markups = Command.Markups.init(init_markup))
   385 
   386   val empty_state: Command.State = Command.State(this)
   387 }