src/Tools/jEdit/src/proofdocument/command.scala
author wenzelm
Tue Dec 08 16:30:20 2009 +0100 (2009-12-08)
changeset 34760 dc7f5e0d9d27
parent 34759 bfea7839d9e1
child 34769 826525fc5285
permissions -rw-r--r--
misc modernization of names;
     1 /*
     2  * Prover commands with semantic state
     3  *
     4  * @author Johannes Hölzl, TU Munich
     5  * @author Fabian Immler, TU Munich
     6  */
     7 
     8 package isabelle.proofdocument
     9 
    10 
    11 import scala.actors.Actor, Actor._
    12 
    13 import scala.collection.mutable
    14 
    15 import isabelle.jedit.{Isabelle, Plugin}
    16 import isabelle.XML
    17 
    18 
    19 trait Accumulator extends Actor
    20 {
    21   start() // start actor
    22 
    23   protected var _state: State
    24   def state = _state
    25 
    26   override def act() {
    27     loop {
    28       react {
    29         case (prover: Prover, message: XML.Tree) => _state = _state.+(prover, message)
    30         case bad => System.err.println("prover: ignoring bad message " + bad)
    31       }
    32     }
    33   }
    34 }
    35 
    36 
    37 object Command
    38 {
    39   object Status extends Enumeration
    40   {
    41     val UNPROCESSED = Value("UNPROCESSED")
    42     val FINISHED = Value("FINISHED")
    43     val FAILED = Value("FAILED")
    44   }
    45 
    46   case class HighlightInfo(highlight: String) { override def toString = highlight }
    47   case class TypeInfo(ty: String)
    48   case class RefInfo(file: Option[String], line: Option[Int],
    49     command_id: Option[String], offset: Option[Int])
    50 }
    51 
    52 
    53 class Command(
    54     val tokens: List[Token],
    55     val starts: Map[Token, Int]) extends Accumulator
    56 {
    57   require(!tokens.isEmpty)
    58 
    59   val id = Isabelle.system.id()
    60   override def hashCode = id.hashCode
    61 
    62 
    63   /* content */
    64 
    65   override def toString = name
    66 
    67   val name = tokens.head.content
    68   val content: String = Token.string_from_tokens(tokens, starts)
    69   def content(i: Int, j: Int): String = content.substring(i, j)
    70   val symbol_index = new Symbol.Index(content)
    71 
    72   def start(doc: Proof_Document) = doc.token_start(tokens.first)
    73   def stop(doc: Proof_Document) = doc.token_start(tokens.last) + tokens.last.length
    74 
    75   def contains(p: Token) = tokens.contains(p)
    76 
    77   protected override var _state = new State(this)
    78 
    79 
    80   /* markup */
    81 
    82   lazy val empty_markup = new Markup_Text(Nil, content)
    83 
    84   def markup_node(begin: Int, end: Int, info: Any): Markup_Tree =
    85   {
    86     val start = symbol_index.decode(begin)
    87     val stop = symbol_index.decode(end)
    88     new Markup_Tree(new Markup_Node(start, stop, info), Nil)
    89   }
    90 
    91 
    92   /* results, markup, ... */
    93 
    94   private val empty_cmd_state = new Command_State(this)
    95   private def cmd_state(doc: Proof_Document) =
    96     doc.states.getOrElse(this, empty_cmd_state)
    97 
    98   def status(doc: Proof_Document) = cmd_state(doc).state.status
    99   def results(doc: Proof_Document) = cmd_state(doc).results
   100   def markup_root(doc: Proof_Document) = cmd_state(doc).markup_root
   101   def highlight(doc: Proof_Document) = cmd_state(doc).highlight
   102   def type_at(doc: Proof_Document, offset: Int) = cmd_state(doc).type_at(offset)
   103   def ref_at(doc: Proof_Document, offset: Int) = cmd_state(doc).ref_at(offset)
   104 }
   105 
   106 
   107 class Command_State(val command: Command) extends Accumulator
   108 {
   109   protected override var _state = new State(command)
   110 
   111   def results: List[XML.Tree] =
   112     command.state.results ::: state.results
   113 
   114   def markup_root: Markup_Text =
   115     (command.state.markup_root /: state.markup_root.markup)(_ + _)
   116 
   117   def type_at(pos: Int): Option[String] = state.type_at(pos)
   118 
   119   def ref_at(pos: Int): Option[Markup_Node] = state.ref_at(pos)
   120 
   121   def highlight: Markup_Text =
   122     (command.state.highlight /: state.highlight.markup)(_ + _)
   123 }