src/Pure/PIDE/command.scala
author bulwahn
Tue Aug 31 08:00:53 2010 +0200 (2010-08-31)
changeset 38950 62578950e748
parent 38723 6a55b8978a56
child 38872 26c505765024
permissions -rw-r--r--
storing options for prolog code generation in the theory
     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 
    11 object Command
    12 {
    13   /** accumulated results from prover **/
    14 
    15   case class State(
    16     val command: Command,
    17     val status: List[Markup],
    18     val reverse_results: List[XML.Tree],
    19     val markup: Markup_Tree)
    20   {
    21     /* content */
    22 
    23     lazy val results = reverse_results.reverse
    24 
    25     def add_status(st: Markup): State = copy(status = st :: status)
    26     def add_markup(info: Text.Info[Any]): State = copy(markup = markup + info)
    27     def add_result(result: XML.Tree): State = copy(reverse_results = result :: reverse_results)
    28 
    29     def root_info: Text.Info[Any] =
    30       new Text.Info(command.range,
    31         XML.Elem(Markup(Markup.STATUS, Nil), status.reverse.map(XML.Elem(_, Nil))))
    32     def root_markup: Markup_Tree = markup + root_info
    33 
    34 
    35     /* message dispatch */
    36 
    37     def accumulate(message: XML.Tree): Command.State =
    38       message match {
    39         case XML.Elem(Markup(Markup.STATUS, _), msgs) =>
    40           (this /: msgs)((state, msg) =>
    41             msg match {
    42               case XML.Elem(markup, Nil) => state.add_status(markup)
    43               case _ => System.err.println("Ignored status message: " + msg); state
    44             })
    45 
    46         case XML.Elem(Markup(Markup.REPORT, _), msgs) =>
    47           (this /: msgs)((state, msg) =>
    48             msg match {
    49               case XML.Elem(Markup(name, atts @ Position.Range(range)), args)
    50               if Position.Id.unapply(atts) == Some(command.id) =>
    51                 val props = atts.filterNot(p => Markup.POSITION_PROPERTIES(p._1))
    52                 val info =
    53                   Text.Info[Any](command.decode(range), XML.Elem(Markup(name, props), args))
    54                 state.add_markup(info)
    55               case _ => System.err.println("Ignored report message: " + msg); state
    56             })
    57         case _ => add_result(message)
    58       }
    59   }
    60 
    61 
    62   /* unparsed dummy commands */
    63 
    64   def unparsed(source: String) =
    65     new Command(Document.NO_ID, List(Token(Token.Kind.UNPARSED, source)))
    66 }
    67 
    68 
    69 class Command(
    70     val id: Document.Command_ID,
    71     val span: List[Token])
    72 {
    73   /* classification */
    74 
    75   def is_command: Boolean = !span.isEmpty && span.head.is_command
    76   def is_ignored: Boolean = span.forall(_.is_ignored)
    77   def is_malformed: Boolean = !is_command && !is_ignored
    78 
    79   def is_unparsed = id == Document.NO_ID
    80 
    81   def name: String = if (is_command) span.head.content else ""
    82   override def toString =
    83     id + "/" + (if (is_command) name else if (is_ignored) "IGNORED" else "MALFORMED")
    84 
    85 
    86   /* source text */
    87 
    88   val source: String = span.map(_.source).mkString
    89   def source(range: Text.Range): String = source.substring(range.start, range.stop)
    90   def length: Int = source.length
    91 
    92   val range: Text.Range = Text.Range(0, length)
    93 
    94   lazy val symbol_index = new Symbol.Index(source)
    95   def decode(i: Text.Offset): Text.Offset = symbol_index.decode(i)
    96   def decode(r: Text.Range): Text.Range = symbol_index.decode(r)
    97 
    98 
    99   /* accumulated results */
   100 
   101   val empty_state: Command.State = Command.State(this, Nil, Nil, Markup_Tree.empty)
   102 }