src/Pure/PIDE/command.scala
author wenzelm
Fri Aug 09 10:41:17 2013 +0200 (2013-08-09 ago)
changeset 52930 5fab62ae3532
parent 52849 199e9fa5a5c2
child 54462 c9bb76303348
permissions -rw-r--r--
retain original messages properties, e.g. for retrieval via Command.Results;
     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 
    20   /** accumulated results from prover **/
    21 
    22   /* results */
    23 
    24   object Results
    25   {
    26     type Entry = (Long, XML.Tree)
    27     val empty = new Results(SortedMap.empty)
    28     def make(es: Iterable[Results.Entry]): Results = (empty /: es.iterator)(_ + _)
    29     def merge(rs: Iterable[Results]): Results = (empty /: rs.iterator)(_ ++ _)
    30   }
    31 
    32   final class Results private(private val rep: SortedMap[Long, XML.Tree])
    33   {
    34     def defined(serial: Long): Boolean = rep.isDefinedAt(serial)
    35     def get(serial: Long): Option[XML.Tree] = rep.get(serial)
    36     def entries: Iterator[Results.Entry] = rep.iterator
    37 
    38     def + (entry: Results.Entry): Results =
    39       if (defined(entry._1)) this
    40       else new Results(rep + entry)
    41 
    42     def ++ (other: Results): Results =
    43       if (this eq other) this
    44       else if (rep.isEmpty) other
    45       else (this /: other.entries)(_ + _)
    46 
    47     override def hashCode: Int = rep.hashCode
    48     override def equals(that: Any): Boolean =
    49       that match {
    50         case other: Results => rep == other.rep
    51         case _ => false
    52       }
    53     override def toString: String = entries.mkString("Results(", ", ", ")")
    54   }
    55 
    56 
    57   /* state */
    58 
    59   sealed case class State(
    60     command: Command,
    61     status: List[Markup] = Nil,
    62     results: Results = Results.empty,
    63     markup: Markup_Tree = Markup_Tree.empty)
    64   {
    65     def markup_to_XML(filter: XML.Elem => Boolean): XML.Body =
    66       markup.to_XML(command.range, command.source, filter)
    67 
    68 
    69     /* content */
    70 
    71     def eq_content(other: State): Boolean =
    72       command.source == other.command.source &&
    73       status == other.status &&
    74       results == other.results &&
    75       markup == other.markup
    76 
    77     private def add_status(st: Markup): State = copy(status = st :: status)
    78     private def add_markup(m: Text.Markup): State = copy(markup = markup + m)
    79 
    80     def + (alt_id: Document_ID.Generic, message: XML.Elem): State =
    81       message match {
    82         case XML.Elem(Markup(Markup.STATUS, _), msgs) =>
    83           (this /: msgs)((state, msg) =>
    84             msg match {
    85               case elem @ XML.Elem(markup, Nil) =>
    86                 state.add_status(markup)
    87                   .add_markup(Text.Info(command.proper_range, elem))  // FIXME cumulation order!?
    88 
    89               case _ =>
    90                 java.lang.System.err.println("Ignored status message: " + msg)
    91                 state
    92             })
    93 
    94         case XML.Elem(Markup(Markup.REPORT, _), msgs) =>
    95           (this /: msgs)((state, msg) =>
    96             msg match {
    97               case XML.Elem(Markup(name, atts @ Position.Id_Range(id, raw_range)), args)
    98               if (id == command.id || id == alt_id) &&
    99                   command.range.contains(command.decode(raw_range)) =>
   100                 val range = command.decode(raw_range)
   101                 val props = Position.purge(atts)
   102                 val info: Text.Markup = Text.Info(range, XML.Elem(Markup(name, props), args))
   103                 state.add_markup(info)
   104               case XML.Elem(Markup(name, atts), args)
   105               if !atts.exists({ case (a, _) => Markup.POSITION_PROPERTIES(a) }) =>
   106                 val range = command.proper_range
   107                 val props = Position.purge(atts)
   108                 val info: Text.Markup = Text.Info(range, XML.Elem(Markup(name, props), args))
   109                 state.add_markup(info)
   110               case _ =>
   111                 // FIXME java.lang.System.err.println("Ignored report message: " + msg)
   112                 state
   113             })
   114         case XML.Elem(Markup(name, props), body) =>
   115           props match {
   116             case Markup.Serial(i) =>
   117               val message1 = XML.Elem(Markup(Markup.message(name), props), body)
   118               val message2 = XML.Elem(Markup(name, props), body)
   119 
   120               val st0 = copy(results = results + (i -> message1))
   121               val st1 =
   122                 if (Protocol.is_inlined(message))
   123                   (st0 /: Protocol.message_positions(command, message))(
   124                     (st, range) => st.add_markup(Text.Info(range, message2)))
   125                 else st0
   126 
   127               st1
   128             case _ =>
   129               java.lang.System.err.println("Ignored message without serial number: " + message)
   130               this
   131           }
   132       }
   133 
   134     def ++ (other: State): State =
   135       copy(
   136         status = other.status ::: status,
   137         results = results ++ other.results,
   138         markup = markup ++ other.markup)
   139   }
   140 
   141 
   142   /* make commands */
   143 
   144   type Span = List[Token]
   145 
   146   def apply(
   147     id: Document_ID.Command,
   148     node_name: Document.Node.Name,
   149     span: Span,
   150     results: Results = Results.empty,
   151     markup: Markup_Tree = Markup_Tree.empty): Command =
   152   {
   153     val source: String =
   154       span match {
   155         case List(tok) => tok.source
   156         case _ => span.map(_.source).mkString
   157       }
   158 
   159     val span1 = new mutable.ListBuffer[Token]
   160     var i = 0
   161     for (Token(kind, s) <- span) {
   162       val n = s.length
   163       val s1 = source.substring(i, i + n)
   164       span1 += Token(kind, s1)
   165       i += n
   166     }
   167 
   168     new Command(id, node_name, span1.toList, source, results, markup)
   169   }
   170 
   171   val empty = Command(Document_ID.none, Document.Node.Name.empty, Nil)
   172 
   173   def unparsed(id: Document_ID.Command, source: String, results: Results, markup: Markup_Tree)
   174       : Command =
   175     Command(id, Document.Node.Name.empty, List(Token(Token.Kind.UNPARSED, source)), results, markup)
   176 
   177   def unparsed(source: String): Command =
   178     unparsed(Document_ID.none, source, Results.empty, Markup_Tree.empty)
   179 
   180   def rich_text(id: Document_ID.Command, results: Results, body: XML.Body): Command =
   181   {
   182     val text = XML.content(body)
   183     val markup = Markup_Tree.from_XML(body)
   184     unparsed(id, text, results, markup)
   185   }
   186 
   187 
   188   /* perspective */
   189 
   190   object Perspective
   191   {
   192     val empty: Perspective = Perspective(Nil)
   193   }
   194 
   195   sealed case class Perspective(commands: List[Command])  // visible commands in canonical order
   196   {
   197     def same(that: Perspective): Boolean =
   198     {
   199       val cmds1 = this.commands
   200       val cmds2 = that.commands
   201       require(!cmds1.exists(_.is_undefined))
   202       require(!cmds2.exists(_.is_undefined))
   203       cmds1.length == cmds2.length &&
   204         (cmds1.iterator zip cmds2.iterator).forall({ case (c1, c2) => c1.id == c2.id })
   205     }
   206   }
   207 }
   208 
   209 
   210 final class Command private(
   211     val id: Document_ID.Command,
   212     val node_name: Document.Node.Name,
   213     val span: List[Token],
   214     val source: String,
   215     val init_results: Command.Results,
   216     val init_markup: Markup_Tree)
   217 {
   218   /* classification */
   219 
   220   def is_undefined: Boolean = id == Document_ID.none
   221   val is_unparsed: Boolean = span.exists(_.is_unparsed)
   222   val is_unfinished: Boolean = span.exists(_.is_unfinished)
   223 
   224   val is_ignored: Boolean = !span.exists(_.is_proper)
   225   val is_malformed: Boolean = !is_ignored && (!span.head.is_command || span.exists(_.is_error))
   226   def is_command: Boolean = !is_ignored && !is_malformed
   227 
   228   def name: String =
   229     span.find(_.is_command) match { case Some(tok) => tok.source case _ => "" }
   230 
   231   override def toString =
   232     id + "/" + (if (is_command) name else if (is_ignored) "IGNORED" else "MALFORMED")
   233 
   234 
   235   /* source */
   236 
   237   def length: Int = source.length
   238   val range: Text.Range = Text.Range(0, length)
   239 
   240   val proper_range: Text.Range =
   241     Text.Range(0, (length /: span.reverse.iterator.takeWhile(_.is_improper))(_ - _.source.length))
   242 
   243   def source(range: Text.Range): String = source.substring(range.start, range.stop)
   244 
   245   lazy val symbol_index = Symbol.Index(source)
   246   def decode(i: Text.Offset): Text.Offset = symbol_index.decode(i)
   247   def decode(r: Text.Range): Text.Range = symbol_index.decode(r)
   248 
   249 
   250   /* accumulated results */
   251 
   252   val init_state: Command.State =
   253     Command.State(this, results = init_results, markup = init_markup)
   254 
   255   val empty_state: Command.State = Command.State(this)
   256 }