src/Pure/Thy/thy_syntax.scala
author wenzelm
Sat Aug 14 12:01:50 2010 +0200 (2010-08-14)
changeset 38374 7eb0f6991e25
parent 38373 e8197eea3cd0
child 38417 b8922ae21111
permissions -rw-r--r--
moved Document.text_edits to Thy_Syntax;
wenzelm@34268
     1
/*  Title:      Pure/Thy/thy_syntax.scala
wenzelm@34268
     2
    Author:     Makarius
wenzelm@34268
     3
wenzelm@38374
     4
Superficial theory syntax: tokens and spans.
wenzelm@34268
     5
*/
wenzelm@34268
     6
wenzelm@34268
     7
package isabelle
wenzelm@34268
     8
wenzelm@34268
     9
wenzelm@38239
    10
import scala.collection.mutable
wenzelm@38374
    11
import scala.annotation.tailrec
wenzelm@38239
    12
wenzelm@38239
    13
wenzelm@34303
    14
object Thy_Syntax
wenzelm@34268
    15
{
wenzelm@38374
    16
  /** parse spans **/
wenzelm@38374
    17
wenzelm@38373
    18
  def parse_spans(toks: List[Token]): List[List[Token]] =
wenzelm@34268
    19
  {
wenzelm@38373
    20
    val result = new mutable.ListBuffer[List[Token]]
wenzelm@38239
    21
    val span = new mutable.ListBuffer[Token]
wenzelm@38239
    22
    val whitespace = new mutable.ListBuffer[Token]
wenzelm@34268
    23
wenzelm@38239
    24
    def flush(buffer: mutable.ListBuffer[Token])
wenzelm@38239
    25
    {
wenzelm@38239
    26
      if (!buffer.isEmpty) { result += buffer.toList; buffer.clear }
wenzelm@34268
    27
    }
wenzelm@38239
    28
    for (tok <- toks) {
wenzelm@38239
    29
      if (tok.is_command) { flush(span); flush(whitespace); span += tok }
wenzelm@38239
    30
      else if (tok.is_ignored) whitespace += tok
wenzelm@38239
    31
      else { span ++= whitespace; whitespace.clear; span += tok }
wenzelm@38239
    32
    }
wenzelm@38239
    33
    flush(span); flush(whitespace)
wenzelm@38239
    34
    result.toList
wenzelm@34268
    35
  }
wenzelm@38374
    36
wenzelm@38374
    37
wenzelm@38374
    38
wenzelm@38374
    39
  /** text edits **/
wenzelm@38374
    40
wenzelm@38374
    41
  def text_edits(session: Session, old_doc: Document, edits: List[Document.Node_Text_Edit])
wenzelm@38374
    42
      : (List[Document.Edit[Command]], Document) =
wenzelm@38374
    43
  {
wenzelm@38374
    44
    /* phase 1: edit individual command source */
wenzelm@38374
    45
wenzelm@38374
    46
    @tailrec def edit_text(eds: List[Text_Edit], commands: Linear_Set[Command])
wenzelm@38374
    47
        : Linear_Set[Command] =
wenzelm@38374
    48
    {
wenzelm@38374
    49
      eds match {
wenzelm@38374
    50
        case e :: es =>
wenzelm@38374
    51
          Document.Node.command_starts(commands.iterator).find {
wenzelm@38374
    52
            case (cmd, cmd_start) =>
wenzelm@38374
    53
              e.can_edit(cmd.source, cmd_start) ||
wenzelm@38374
    54
                e.is_insert && e.start == cmd_start + cmd.length
wenzelm@38374
    55
          } match {
wenzelm@38374
    56
            case Some((cmd, cmd_start)) if e.can_edit(cmd.source, cmd_start) =>
wenzelm@38374
    57
              val (rest, text) = e.edit(cmd.source, cmd_start)
wenzelm@38374
    58
              val new_commands = commands.insert_after(Some(cmd), Command.unparsed(text)) - cmd
wenzelm@38374
    59
              edit_text(rest.toList ::: es, new_commands)
wenzelm@38374
    60
wenzelm@38374
    61
            case Some((cmd, cmd_start)) =>
wenzelm@38374
    62
              edit_text(es, commands.insert_after(Some(cmd), Command.unparsed(e.text)))
wenzelm@38374
    63
wenzelm@38374
    64
            case None =>
wenzelm@38374
    65
              require(e.is_insert && e.start == 0)
wenzelm@38374
    66
              edit_text(es, commands.insert_after(None, Command.unparsed(e.text)))
wenzelm@38374
    67
          }
wenzelm@38374
    68
        case Nil => commands
wenzelm@38374
    69
      }
wenzelm@38374
    70
    }
wenzelm@38374
    71
wenzelm@38374
    72
wenzelm@38374
    73
    /* phase 2: recover command spans */
wenzelm@38374
    74
wenzelm@38374
    75
    @tailrec def recover_spans(commands: Linear_Set[Command]): Linear_Set[Command] =
wenzelm@38374
    76
    {
wenzelm@38374
    77
      commands.iterator.find(_.is_unparsed) match {
wenzelm@38374
    78
        case Some(first_unparsed) =>
wenzelm@38374
    79
          val first =
wenzelm@38374
    80
            commands.reverse_iterator(first_unparsed).find(_.is_command) getOrElse commands.head
wenzelm@38374
    81
          val last =
wenzelm@38374
    82
            commands.iterator(first_unparsed).find(_.is_command) getOrElse commands.last
wenzelm@38374
    83
          val range =
wenzelm@38374
    84
            commands.iterator(first).takeWhile(_ != last).toList ::: List(last)
wenzelm@38374
    85
wenzelm@38374
    86
          val sources = range.flatMap(_.span.map(_.source))
wenzelm@38374
    87
          val spans0 = parse_spans(session.current_syntax.scan(sources.mkString))
wenzelm@38374
    88
wenzelm@38374
    89
          val (before_edit, spans1) =
wenzelm@38374
    90
            if (!spans0.isEmpty && first.is_command && first.span == spans0.head)
wenzelm@38374
    91
              (Some(first), spans0.tail)
wenzelm@38374
    92
            else (commands.prev(first), spans0)
wenzelm@38374
    93
wenzelm@38374
    94
          val (after_edit, spans2) =
wenzelm@38374
    95
            if (!spans1.isEmpty && last.is_command && last.span == spans1.last)
wenzelm@38374
    96
              (Some(last), spans1.take(spans1.length - 1))
wenzelm@38374
    97
            else (commands.next(last), spans1)
wenzelm@38374
    98
wenzelm@38374
    99
          val inserted = spans2.map(span => new Command(session.create_id(), span))
wenzelm@38374
   100
          val new_commands =
wenzelm@38374
   101
            commands.delete_between(before_edit, after_edit).append_after(before_edit, inserted)
wenzelm@38374
   102
          recover_spans(new_commands)
wenzelm@38374
   103
wenzelm@38374
   104
        case None => commands
wenzelm@38374
   105
      }
wenzelm@38374
   106
    }
wenzelm@38374
   107
wenzelm@38374
   108
wenzelm@38374
   109
    /* resulting document edits */
wenzelm@38374
   110
wenzelm@38374
   111
    {
wenzelm@38374
   112
      val doc_edits = new mutable.ListBuffer[Document.Edit[Command]]
wenzelm@38374
   113
      var nodes = old_doc.nodes
wenzelm@38374
   114
wenzelm@38374
   115
      for ((name, text_edits) <- edits) {
wenzelm@38374
   116
        val commands0 = nodes(name).commands
wenzelm@38374
   117
        val commands1 = edit_text(text_edits, commands0)
wenzelm@38374
   118
        val commands2 = recover_spans(commands1)   // FIXME somewhat slow
wenzelm@38374
   119
wenzelm@38374
   120
        val removed_commands = commands0.iterator.filter(!commands2.contains(_)).toList
wenzelm@38374
   121
        val inserted_commands = commands2.iterator.filter(!commands0.contains(_)).toList
wenzelm@38374
   122
wenzelm@38374
   123
        val cmd_edits =
wenzelm@38374
   124
          removed_commands.reverse.map(cmd => (commands0.prev(cmd), None)) :::
wenzelm@38374
   125
          inserted_commands.map(cmd => (commands2.prev(cmd), Some(cmd)))
wenzelm@38374
   126
wenzelm@38374
   127
        doc_edits += (name -> Some(cmd_edits))
wenzelm@38374
   128
        nodes += (name -> new Document.Node(commands2))
wenzelm@38374
   129
      }
wenzelm@38374
   130
      (doc_edits.toList, new Document(session.create_id(), nodes))
wenzelm@38374
   131
    }
wenzelm@38374
   132
  }
wenzelm@34268
   133
}