src/Pure/PIDE/command.scala
author wenzelm
Thu Dec 13 18:00:24 2012 +0100 (2012-12-13 ago)
changeset 50503 50f141b34bb7
parent 50501 6f41f1646617
child 50507 9605b0d93d1e
permissions -rw-r--r--
enable Isabelle/ML to produce uninterpreted result messages as well;
     1 /*  Title:      Pure/PIDE/command.scala
     2     Author:     Fabian Immler, TU Munich
     3     Author:     Makarius
     4 
     5 Prover commands with semantic state.
     6 */
     7 
     8 package isabelle
     9 
    10 import java.lang.System
    11 
    12 import scala.collection.mutable
    13 import scala.collection.immutable.SortedMap
    14 
    15 
    16 object Command
    17 {
    18   /** accumulated results from prover **/
    19 
    20   type Results = SortedMap[Long, XML.Tree]
    21   val empty_results: Results = SortedMap.empty
    22 
    23   sealed case class State(
    24     command: Command,
    25     status: List[Markup] = Nil,
    26     results: Results = empty_results,
    27     markup: Markup_Tree = Markup_Tree.empty)
    28   {
    29     def markup_to_XML(filter: XML.Elem => Boolean): XML.Body =
    30       markup.to_XML(command.range, command.source, filter)
    31 
    32 
    33     /* accumulate content */
    34 
    35     private def add_status(st: Markup): State = copy(status = st :: status)
    36     private def add_markup(m: Text.Markup): State = copy(markup = markup + m)
    37 
    38     def + (alt_id: Document.ID, message: XML.Elem): Command.State =
    39       message match {
    40         case XML.Elem(Markup(Markup.STATUS, _), msgs) =>
    41           (this /: msgs)((state, msg) =>
    42             msg match {
    43               case elem @ XML.Elem(markup, Nil) =>
    44                 state.add_status(markup)
    45                   .add_markup(Text.Info(command.proper_range, elem))  // FIXME cumulation order!?
    46 
    47               case _ => System.err.println("Ignored status message: " + msg); state
    48             })
    49 
    50         case XML.Elem(Markup(Markup.REPORT, _), msgs) =>
    51           (this /: msgs)((state, msg) =>
    52             msg match {
    53               case XML.Elem(Markup(name, atts @ Position.Id_Range(id, raw_range)), args)
    54               if (id == command.id || id == alt_id) &&
    55                   command.range.contains(command.decode(raw_range)) =>
    56                 val range = command.decode(raw_range)
    57                 val props = Position.purge(atts)
    58                 val info: Text.Markup = Text.Info(range, XML.Elem(Markup(name, props), args))
    59                 state.add_markup(info)
    60               case XML.Elem(Markup(name, atts), args)
    61               if !atts.exists({ case (a, _) => Markup.POSITION_PROPERTIES(a) }) =>
    62                 val range = command.proper_range
    63                 val props = Position.purge(atts)
    64                 val info: Text.Markup = Text.Info(range, XML.Elem(Markup(name, props), args))
    65                 state.add_markup(info)
    66               case _ =>
    67                 // FIXME System.err.println("Ignored report message: " + msg)
    68                 state
    69             })
    70         case XML.Elem(Markup(name, atts), body) =>
    71           atts match {
    72             case Markup.Serial(i) =>
    73               val props = Position.purge(atts)
    74               val message1 = XML.Elem(Markup(Markup.message(name), props), body)
    75               val message2 = XML.Elem(Markup(name, props), body)
    76 
    77               val st0 = copy(results = results + (i -> message1))
    78               val st1 =
    79                 if (Protocol.is_inlined(message))
    80                   (st0 /: Protocol.message_positions(command, message))(
    81                     (st, range) => st.add_markup(Text.Info(range, message2)))
    82                 else st0
    83 
    84               st1
    85             case _ => System.err.println("Ignored message without serial number: " + message); this
    86           }
    87       }
    88   }
    89 
    90 
    91   /* make commands */
    92 
    93   type Span = List[Token]
    94 
    95   def apply(id: Document.Command_ID, node_name: Document.Node.Name, span: Span,
    96     results: Results = empty_results, markup: Markup_Tree = Markup_Tree.empty): Command =
    97   {
    98     val source: String =
    99       span match {
   100         case List(tok) => tok.source
   101         case _ => span.map(_.source).mkString
   102       }
   103 
   104     val span1 = new mutable.ListBuffer[Token]
   105     var i = 0
   106     for (Token(kind, s) <- span) {
   107       val n = s.length
   108       val s1 = source.substring(i, i + n)
   109       span1 += Token(kind, s1)
   110       i += n
   111     }
   112 
   113     new Command(id, node_name, span1.toList, source, results, markup)
   114   }
   115 
   116   val empty = Command(Document.no_id, Document.Node.Name.empty, Nil)
   117 
   118   def unparsed(id: Document.Command_ID, source: String, results: Results, markup: Markup_Tree)
   119       : Command =
   120     Command(id, Document.Node.Name.empty, List(Token(Token.Kind.UNPARSED, source)), results, markup)
   121 
   122   def unparsed(source: String): Command =
   123     unparsed(Document.no_id, source, empty_results, Markup_Tree.empty)
   124 
   125   def rich_text(id: Document.Command_ID, results: Results, body: XML.Body): Command =
   126   {
   127     val text = XML.content(body)
   128     val markup = Markup_Tree.from_XML(body)
   129     unparsed(id, text, results, markup)
   130   }
   131 
   132 
   133   /* perspective */
   134 
   135   object Perspective
   136   {
   137     val empty: Perspective = Perspective(Nil)
   138   }
   139 
   140   sealed case class Perspective(commands: List[Command])  // visible commands in canonical order
   141   {
   142     def same(that: Perspective): Boolean =
   143     {
   144       val cmds1 = this.commands
   145       val cmds2 = that.commands
   146       require(!cmds1.exists(_.is_undefined))
   147       require(!cmds2.exists(_.is_undefined))
   148       cmds1.length == cmds2.length &&
   149         (cmds1.iterator zip cmds2.iterator).forall({ case (c1, c2) => c1.id == c2.id })
   150     }
   151   }
   152 }
   153 
   154 
   155 final class Command private(
   156     val id: Document.Command_ID,
   157     val node_name: Document.Node.Name,
   158     val span: Command.Span,
   159     val source: String,
   160     val init_results: Command.Results,
   161     val init_markup: Markup_Tree)
   162 {
   163   /* classification */
   164 
   165   def is_undefined: Boolean = id == Document.no_id
   166   val is_unparsed: Boolean = span.exists(_.is_unparsed)
   167   val is_unfinished: Boolean = span.exists(_.is_unfinished)
   168 
   169   val is_ignored: Boolean = !span.exists(_.is_proper)
   170   val is_malformed: Boolean = !is_ignored && (!span.head.is_command || span.exists(_.is_error))
   171   def is_command: Boolean = !is_ignored && !is_malformed
   172 
   173   def name: String =
   174     span.find(_.is_command) match { case Some(tok) => tok.source case _ => "" }
   175 
   176   override def toString =
   177     id + "/" + (if (is_command) name else if (is_ignored) "IGNORED" else "MALFORMED")
   178 
   179 
   180   /* source text */
   181 
   182   def length: Int = source.length
   183   val range: Text.Range = Text.Range(0, length)
   184 
   185   val proper_range: Text.Range =
   186     Text.Range(0, (length /: span.reverse.iterator.takeWhile(_.is_space))(_ - _.source.length))
   187 
   188   def source(range: Text.Range): String = source.substring(range.start, range.stop)
   189 
   190   lazy val symbol_index = new Symbol.Index(source)
   191   def decode(i: Text.Offset): Text.Offset = symbol_index.decode(i)
   192   def decode(r: Text.Range): Text.Range = symbol_index.decode(r)
   193 
   194 
   195   /* accumulated results */
   196 
   197   val init_state: Command.State =
   198     Command.State(this, results = init_results, markup = init_markup)
   199 }