src/Tools/jEdit/src/proofdocument/command.scala
author wenzelm
Mon Jan 11 01:40:18 2010 +0100 (2010-01-11)
changeset 34859 f986d84dd44b
parent 34858 ad486bd8abf3
child 34860 847c00f5535a
permissions -rw-r--r--
renamed Command.content to source;
reorganization of document parsing, using command spans etc. -- initial setup;
wenzelm@34407
     1
/*
wenzelm@34407
     2
 * Prover commands with semantic state
wenzelm@34407
     3
 *
wenzelm@34407
     4
 * @author Johannes Hölzl, TU Munich
wenzelm@34407
     5
 * @author Fabian Immler, TU Munich
wenzelm@34407
     6
 */
wenzelm@34407
     7
wenzelm@34760
     8
package isabelle.proofdocument
wenzelm@34318
     9
wenzelm@34451
    10
wenzelm@34699
    11
import scala.actors.Actor, Actor._
wenzelm@34497
    12
import scala.collection.mutable
wenzelm@34486
    13
wenzelm@34777
    14
import isabelle.jedit.Isabelle
immler@34675
    15
wenzelm@34451
    16
wenzelm@34637
    17
object Command
wenzelm@34637
    18
{
wenzelm@34637
    19
  object Status extends Enumeration
wenzelm@34637
    20
  {
wenzelm@34318
    21
    val UNPROCESSED = Value("UNPROCESSED")
wenzelm@34318
    22
    val FINISHED = Value("FINISHED")
wenzelm@34318
    23
    val FAILED = Value("FAILED")
wenzelm@34318
    24
  }
wenzelm@34707
    25
wenzelm@34707
    26
  case class HighlightInfo(highlight: String) { override def toString = highlight }
wenzelm@34717
    27
  case class TypeInfo(ty: String)
wenzelm@34707
    28
  case class RefInfo(file: Option[String], line: Option[Int],
wenzelm@34717
    29
    command_id: Option[String], offset: Option[Int])
wenzelm@34318
    30
}
wenzelm@34318
    31
wenzelm@34451
    32
wenzelm@34697
    33
class Command(
wenzelm@34813
    34
    val id: Isar_Document.Command_ID,
wenzelm@34859
    35
    val span: Thy_Syntax.Span)
wenzelm@34815
    36
  extends Session.Entity
wenzelm@34451
    37
{
wenzelm@34813
    38
  // FIXME override equality as well
immler@34653
    39
  override def hashCode = id.hashCode
wenzelm@34637
    40
immler@34674
    41
wenzelm@34859
    42
  /* classification */
wenzelm@34500
    43
wenzelm@34859
    44
  def is_command: Boolean = !span.isEmpty && span.first.is_command
wenzelm@34859
    45
  def is_ignored: Boolean = span.forall(tok => tok.is_space || tok.is_comment)
wenzelm@34859
    46
  def is_malformed: Boolean = !is_command && !is_ignored
wenzelm@34859
    47
wenzelm@34859
    48
  def name: String = if (is_command) span.first.content else ""
wenzelm@34859
    49
  override def toString = if (is_command) name else if (is_ignored) "<ignored>" else "<malformed>"
wenzelm@34495
    50
wenzelm@34859
    51
wenzelm@34859
    52
  /* source text */
wenzelm@34451
    53
wenzelm@34859
    54
  val source: String = span.map(_.source).mkString
wenzelm@34859
    55
  def source(i: Int, j: Int): String = source.substring(i, j)
wenzelm@34859
    56
  def length: Int = source.length
wenzelm@34855
    57
wenzelm@34859
    58
  lazy val symbol_index = new Symbol.Index(source)
immler@34593
    59
wenzelm@34815
    60
wenzelm@34815
    61
  /* accumulated messages */
wenzelm@34815
    62
wenzelm@34815
    63
  @volatile protected var state = new State(this)
wenzelm@34815
    64
  def current_state: State = state
wenzelm@34815
    65
wenzelm@34815
    66
  private case class Consume(session: Session, message: XML.Tree)
wenzelm@34832
    67
  private case object Assign
wenzelm@34815
    68
wenzelm@34815
    69
  private val accumulator = actor {
wenzelm@34832
    70
    var assigned = false
wenzelm@34815
    71
    loop {
wenzelm@34815
    72
      react {
wenzelm@34832
    73
        case Consume(session: Session, message: XML.Tree) if !assigned =>
wenzelm@34815
    74
          state = state.+(session, message)
wenzelm@34815
    75
wenzelm@34832
    76
        case Assign =>
wenzelm@34835
    77
          assigned = true  // single assignment
wenzelm@34832
    78
          reply(())
wenzelm@34815
    79
wenzelm@34815
    80
        case bad => System.err.println("command accumulator: ignoring bad message " + bad)
wenzelm@34815
    81
      }
wenzelm@34815
    82
    }
wenzelm@34815
    83
  }
wenzelm@34815
    84
wenzelm@34815
    85
  def consume(session: Session, message: XML.Tree) { accumulator ! Consume(session, message) }
wenzelm@34815
    86
wenzelm@34832
    87
  def assign_state(state_id: Isar_Document.State_ID): Command =
wenzelm@34815
    88
  {
wenzelm@34859
    89
    val cmd = new Command(state_id, span)
wenzelm@34832
    90
    accumulator !? Assign
wenzelm@34815
    91
    cmd.state = current_state
wenzelm@34815
    92
    cmd
wenzelm@34815
    93
  }
immler@34676
    94
immler@34676
    95
immler@34676
    96
  /* markup */
wenzelm@34508
    97
wenzelm@34859
    98
  lazy val empty_markup = new Markup_Text(Nil, source)
immler@34676
    99
wenzelm@34717
   100
  def markup_node(begin: Int, end: Int, info: Any): Markup_Tree =
wenzelm@34699
   101
  {
wenzelm@34703
   102
    val start = symbol_index.decode(begin)
wenzelm@34703
   103
    val stop = symbol_index.decode(end)
wenzelm@34717
   104
    new Markup_Tree(new Markup_Node(start, stop, info), Nil)
wenzelm@34500
   105
  }
immler@34676
   106
}