src/Tools/jEdit/src/prover/Command.scala
author immler@in.tum.de
Sun Dec 07 19:55:01 2008 +0100 (2008-12-07 ago)
changeset 34399 5b8b89b7e597
parent 34398 2d40e4067c37
child 34400 1b61a92f8675
permissions -rw-r--r--
interpretation of STATUS messages in one place, deleting inner syntax
wenzelm@34318
     1
package isabelle.prover
wenzelm@34318
     2
wenzelm@34318
     3
import isabelle.proofdocument.Token
immler@34393
     4
import isabelle.jedit.Plugin
wenzelm@34318
     5
import isabelle.{ YXML, XML }
immler@34393
     6
import sidekick.{SideKickParsedData}
immler@34393
     7
import sidekick.enhanced.SourceAsset
immler@34393
     8
import javax.swing.text.Position
immler@34393
     9
import javax.swing.tree.DefaultMutableTreeNode
wenzelm@34318
    10
wenzelm@34318
    11
object Command {
wenzelm@34318
    12
  object Phase extends Enumeration {
wenzelm@34318
    13
    val UNPROCESSED = Value("UNPROCESSED")
wenzelm@34318
    14
    val FINISHED = Value("FINISHED")
wenzelm@34318
    15
    val REMOVE = Value("REMOVE")
wenzelm@34318
    16
    val REMOVED = Value("REMOVED")
wenzelm@34318
    17
    val FAILED = Value("FAILED")
wenzelm@34318
    18
  }
wenzelm@34318
    19
wenzelm@34318
    20
  private var nextId : Long = 0
wenzelm@34318
    21
  def generateId : Long = {
wenzelm@34318
    22
    nextId = nextId + 1
wenzelm@34318
    23
    return nextId
wenzelm@34318
    24
  }
wenzelm@34318
    25
  
wenzelm@34318
    26
  def idFromString(id : String) = Long.unbox(java.lang.Long.valueOf(id, 36))
wenzelm@34318
    27
}
wenzelm@34318
    28
immler@34397
    29
class Command(val document : Document, val first : Token[Command], val last : Token[Command]) {
wenzelm@34318
    30
  import Command._
wenzelm@34318
    31
  
wenzelm@34318
    32
  {
wenzelm@34318
    33
    var t = first
wenzelm@34318
    34
    while(t != null) {
wenzelm@34318
    35
      t.command = this
wenzelm@34318
    36
      t = if (t == last) null else t.next
wenzelm@34318
    37
    }
wenzelm@34318
    38
  }
wenzelm@34318
    39
  
wenzelm@34318
    40
  val id : Long = generateId
immler@34399
    41
immler@34399
    42
  private var p = Phase.UNPROCESSED
immler@34399
    43
  def phase = p
immler@34399
    44
  def phase_=(p_new : Phase.Value) = {
immler@34399
    45
    if(p_new == Phase.UNPROCESSED){
immler@34399
    46
      //delete inner syntax
immler@34399
    47
      val children = root_node.children
immler@34399
    48
      while (children.hasMoreElements){
immler@34399
    49
        val child = children.nextElement.asInstanceOf[DefaultMutableTreeNode]
immler@34399
    50
        child.removeAllChildren
immler@34399
    51
      }
immler@34399
    52
    }
immler@34399
    53
    p = p_new
immler@34399
    54
  }
wenzelm@34318
    55
	
wenzelm@34318
    56
  var results = Nil : List[XML.Tree]
immler@34391
    57
wenzelm@34318
    58
  def idString = java.lang.Long.toString(id, 36)
immler@34397
    59
  def results_xml = XML.document(results match {
wenzelm@34318
    60
                                case Nil => XML.Elem("message", List(), List())
wenzelm@34318
    61
                                case List(elem) => elem
immler@34391
    62
                                case _ =>
wenzelm@34318
    63
                                  XML.Elem("messages", List(), List(results.first,
wenzelm@34318
    64
                                                                    results.last))
wenzelm@34318
    65
                              }, "style")
wenzelm@34318
    66
  def addResult(tree : XML.Tree) {
wenzelm@34318
    67
    results = results ::: List(tree)
wenzelm@34318
    68
  }
immler@34393
    69
  
immler@34393
    70
  val root_node = {
immler@34397
    71
    val content = document.getContent(this)
immler@34399
    72
    val ra = new RelativeAsset(this, 0, stop - start, "Command", content)
immler@34393
    73
    new DefaultMutableTreeNode(ra)
immler@34393
    74
  }
wenzelm@34318
    75
immler@34393
    76
  // does cand fit into node?
immler@34393
    77
  private def fitting(cand : DefaultMutableTreeNode, node : DefaultMutableTreeNode) : boolean = {
immler@34393
    78
    val node_asset = node.getUserObject.asInstanceOf[RelativeAsset]
immler@34393
    79
    val n_start = node_asset.rel_start
immler@34393
    80
    val n_end = node_asset.rel_end
immler@34393
    81
    val cand_asset = cand.getUserObject.asInstanceOf[RelativeAsset]
immler@34393
    82
    val c_start = cand_asset.rel_start
immler@34393
    83
    val c_end = cand_asset.rel_end
immler@34393
    84
    return c_start >= n_start && c_end <= n_end
immler@34393
    85
  }
immler@34393
    86
immler@34399
    87
  private def insert_node(new_node : DefaultMutableTreeNode, node : DefaultMutableTreeNode) {
immler@34393
    88
    assert(fitting(new_node, node))
immler@34393
    89
    val children = node.children
immler@34393
    90
    while (children.hasMoreElements){
immler@34393
    91
      val child = children.nextElement.asInstanceOf[DefaultMutableTreeNode]
immler@34393
    92
      if(fitting(new_node, child)) {
immler@34393
    93
        insert_node(new_node, child)
immler@34393
    94
      }
immler@34393
    95
    }
immler@34393
    96
    if(new_node.getParent == null){
immler@34393
    97
      while(children.hasMoreElements){
immler@34393
    98
        val child = children.nextElement.asInstanceOf[DefaultMutableTreeNode]
immler@34393
    99
        if(fitting(child, new_node)) {
immler@34393
   100
          node.remove(child.asInstanceOf[DefaultMutableTreeNode])
immler@34393
   101
          new_node.add(child)
immler@34393
   102
        }
immler@34393
   103
      }
immler@34393
   104
      node.add(new_node)
immler@34393
   105
    }
immler@34393
   106
  }
immler@34393
   107
immler@34399
   108
  def add_node(node : DefaultMutableTreeNode) = insert_node(node, root_node)
immler@34399
   109
immler@34399
   110
  def node_from(kind : String, begin : Int, end : Int) : DefaultMutableTreeNode = {
immler@34399
   111
    val markup_content = /*content.substring(begin, end)*/
immler@34399
   112
    ""
immler@34399
   113
    val asset = new RelativeAsset(this, begin, end, kind, markup_content)
immler@34399
   114
    new DefaultMutableTreeNode(asset)
immler@34391
   115
  }
immler@34391
   116
immler@34397
   117
  def content = document.getContent(this)
immler@34396
   118
wenzelm@34318
   119
  def next = if (last.next != null) last.next.command else null
wenzelm@34318
   120
  def previous = if (first.previous != null) first.previous.command else null
wenzelm@34318
   121
wenzelm@34318
   122
  def start = first start
wenzelm@34318
   123
  def stop = last stop
wenzelm@34318
   124
  
wenzelm@34318
   125
  def properStart = start
wenzelm@34318
   126
  def properStop = {
wenzelm@34318
   127
    var i = last
immler@34388
   128
    while (i != first && i.kind.equals(Token.Kind.COMMENT))
wenzelm@34318
   129
      i = i.previous
wenzelm@34318
   130
    i.stop
wenzelm@34318
   131
  }  	
wenzelm@34318
   132
}