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