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