src/Pure/Thy/thy_syntax.scala
author wenzelm
Fri Aug 12 15:28:30 2011 +0200 (2011-08-12)
changeset 44160 8848867501fb
parent 44157 a21d3e1e64fd
child 44180 a6dc270d3edb
permissions -rw-r--r--
clarified document model header: master_dir (native wrt. editor, potentially URL) and node_name (full canonical path);
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@40454
    16
  /** nested structure **/
wenzelm@40454
    17
wenzelm@40454
    18
  object Structure
wenzelm@40454
    19
  {
wenzelm@40478
    20
    sealed abstract class Entry { def length: Int }
wenzelm@40454
    21
    case class Block(val name: String, val body: List[Entry]) extends Entry
wenzelm@40454
    22
    {
wenzelm@40454
    23
      val length: Int = (0 /: body)(_ + _.length)
wenzelm@40454
    24
    }
wenzelm@40454
    25
    case class Atom(val command: Command) extends Entry
wenzelm@40454
    26
    {
wenzelm@40454
    27
      def length: Int = command.length
wenzelm@40454
    28
    }
wenzelm@40454
    29
wenzelm@40792
    30
    def parse(syntax: Outer_Syntax, root_name: String, text: CharSequence): Entry =
wenzelm@40454
    31
    {
wenzelm@40454
    32
      /* stack operations */
wenzelm@40454
    33
wenzelm@40454
    34
      def buffer(): mutable.ListBuffer[Entry] = new mutable.ListBuffer[Entry]
wenzelm@40454
    35
      var stack: List[(Int, String, mutable.ListBuffer[Entry])] = List((0, root_name, buffer()))
wenzelm@40454
    36
wenzelm@40454
    37
      @tailrec def close(level: Int => Boolean)
wenzelm@40454
    38
      {
wenzelm@40454
    39
        stack match {
wenzelm@40454
    40
          case (lev, name, body) :: (_, _, body2) :: rest if level(lev) =>
wenzelm@40454
    41
            body2 += Block(name, body.toList)
wenzelm@40454
    42
            stack = stack.tail
wenzelm@40454
    43
            close(level)
wenzelm@40454
    44
          case _ =>
wenzelm@40454
    45
        }
wenzelm@40454
    46
      }
wenzelm@40454
    47
wenzelm@40454
    48
      def result(): Entry =
wenzelm@40454
    49
      {
wenzelm@40454
    50
        close(_ => true)
wenzelm@40454
    51
        val (_, name, body) = stack.head
wenzelm@40454
    52
        Block(name, body.toList)
wenzelm@40454
    53
      }
wenzelm@40454
    54
wenzelm@40454
    55
      def add(command: Command)
wenzelm@40454
    56
      {
wenzelm@40454
    57
        syntax.heading_level(command) match {
wenzelm@40454
    58
          case Some(i) =>
wenzelm@40457
    59
            close(_ >= i)
wenzelm@40454
    60
            stack = (i, command.source, buffer()) :: stack
wenzelm@40454
    61
          case None =>
wenzelm@40454
    62
        }
wenzelm@40454
    63
        stack.head._3 += Atom(command)
wenzelm@40454
    64
      }
wenzelm@40454
    65
wenzelm@40454
    66
wenzelm@40454
    67
      /* result structure */
wenzelm@40454
    68
wenzelm@40454
    69
      val spans = parse_spans(syntax.scan(text))
wenzelm@40454
    70
      spans.foreach(span => add(Command.span(span)))
wenzelm@40454
    71
      result()
wenzelm@40454
    72
    }
wenzelm@40454
    73
  }
wenzelm@40454
    74
wenzelm@40454
    75
wenzelm@40454
    76
wenzelm@38374
    77
  /** parse spans **/
wenzelm@38374
    78
wenzelm@38373
    79
  def parse_spans(toks: List[Token]): List[List[Token]] =
wenzelm@34268
    80
  {
wenzelm@38373
    81
    val result = new mutable.ListBuffer[List[Token]]
wenzelm@38239
    82
    val span = new mutable.ListBuffer[Token]
wenzelm@38239
    83
    val whitespace = new mutable.ListBuffer[Token]
wenzelm@34268
    84
wenzelm@38239
    85
    def flush(buffer: mutable.ListBuffer[Token])
wenzelm@38239
    86
    {
wenzelm@38239
    87
      if (!buffer.isEmpty) { result += buffer.toList; buffer.clear }
wenzelm@34268
    88
    }
wenzelm@38239
    89
    for (tok <- toks) {
wenzelm@38239
    90
      if (tok.is_command) { flush(span); flush(whitespace); span += tok }
wenzelm@38239
    91
      else if (tok.is_ignored) whitespace += tok
wenzelm@38239
    92
      else { span ++= whitespace; whitespace.clear; span += tok }
wenzelm@38239
    93
    }
wenzelm@38239
    94
    flush(span); flush(whitespace)
wenzelm@38239
    95
    result.toList
wenzelm@34268
    96
  }
wenzelm@38374
    97
wenzelm@38374
    98
wenzelm@38374
    99
wenzelm@38374
   100
  /** text edits **/
wenzelm@38374
   101
wenzelm@43722
   102
  def text_edits(
wenzelm@43722
   103
      syntax: Outer_Syntax,
wenzelm@43722
   104
      previous: Document.Version,
wenzelm@44157
   105
      edits: List[Document.Edit_Text])
wenzelm@44157
   106
    : (List[Document.Edit_Command], Document.Version) =
wenzelm@38374
   107
  {
wenzelm@38374
   108
    /* phase 1: edit individual command source */
wenzelm@38374
   109
wenzelm@38425
   110
    @tailrec def edit_text(eds: List[Text.Edit], commands: Linear_Set[Command])
wenzelm@38374
   111
        : Linear_Set[Command] =
wenzelm@38374
   112
    {
wenzelm@38374
   113
      eds match {
wenzelm@38374
   114
        case e :: es =>
wenzelm@38374
   115
          Document.Node.command_starts(commands.iterator).find {
wenzelm@38374
   116
            case (cmd, cmd_start) =>
wenzelm@38374
   117
              e.can_edit(cmd.source, cmd_start) ||
wenzelm@38374
   118
                e.is_insert && e.start == cmd_start + cmd.length
wenzelm@38374
   119
          } match {
wenzelm@38374
   120
            case Some((cmd, cmd_start)) if e.can_edit(cmd.source, cmd_start) =>
wenzelm@38374
   121
              val (rest, text) = e.edit(cmd.source, cmd_start)
wenzelm@38374
   122
              val new_commands = commands.insert_after(Some(cmd), Command.unparsed(text)) - cmd
wenzelm@38374
   123
              edit_text(rest.toList ::: es, new_commands)
wenzelm@38374
   124
wenzelm@38374
   125
            case Some((cmd, cmd_start)) =>
wenzelm@38374
   126
              edit_text(es, commands.insert_after(Some(cmd), Command.unparsed(e.text)))
wenzelm@38374
   127
wenzelm@38374
   128
            case None =>
wenzelm@38374
   129
              require(e.is_insert && e.start == 0)
wenzelm@38374
   130
              edit_text(es, commands.insert_after(None, Command.unparsed(e.text)))
wenzelm@38374
   131
          }
wenzelm@38374
   132
        case Nil => commands
wenzelm@38374
   133
      }
wenzelm@38374
   134
    }
wenzelm@38374
   135
wenzelm@38374
   136
wenzelm@38374
   137
    /* phase 2: recover command spans */
wenzelm@38374
   138
wenzelm@38374
   139
    @tailrec def recover_spans(commands: Linear_Set[Command]): Linear_Set[Command] =
wenzelm@38374
   140
    {
wenzelm@38374
   141
      commands.iterator.find(_.is_unparsed) match {
wenzelm@38374
   142
        case Some(first_unparsed) =>
wenzelm@38374
   143
          val first =
wenzelm@38878
   144
            commands.reverse_iterator(first_unparsed).
wenzelm@38878
   145
              dropWhile(_.newlines == 0).find(_.is_command) getOrElse commands.head
wenzelm@38374
   146
          val last =
wenzelm@38878
   147
            commands.iterator(first_unparsed).
wenzelm@38878
   148
              dropWhile(_.newlines == 0).find(_.is_command) getOrElse commands.last
wenzelm@38374
   149
          val range =
wenzelm@38374
   150
            commands.iterator(first).takeWhile(_ != last).toList ::: List(last)
wenzelm@38374
   151
wenzelm@38374
   152
          val sources = range.flatMap(_.span.map(_.source))
wenzelm@43647
   153
          val spans0 = parse_spans(syntax.scan(sources.mkString))
wenzelm@38374
   154
wenzelm@38374
   155
          val (before_edit, spans1) =
wenzelm@38374
   156
            if (!spans0.isEmpty && first.is_command && first.span == spans0.head)
wenzelm@38374
   157
              (Some(first), spans0.tail)
wenzelm@38374
   158
            else (commands.prev(first), spans0)
wenzelm@38374
   159
wenzelm@38374
   160
          val (after_edit, spans2) =
wenzelm@38374
   161
            if (!spans1.isEmpty && last.is_command && last.span == spans1.last)
wenzelm@38374
   162
              (Some(last), spans1.take(spans1.length - 1))
wenzelm@38374
   163
            else (commands.next(last), spans1)
wenzelm@38374
   164
wenzelm@43662
   165
          val inserted = spans2.map(span => new Command(Document.new_id(), span))
wenzelm@38374
   166
          val new_commands =
wenzelm@38374
   167
            commands.delete_between(before_edit, after_edit).append_after(before_edit, inserted)
wenzelm@38374
   168
          recover_spans(new_commands)
wenzelm@38374
   169
wenzelm@38374
   170
        case None => commands
wenzelm@38374
   171
      }
wenzelm@38374
   172
    }
wenzelm@38374
   173
wenzelm@38374
   174
wenzelm@38374
   175
    /* resulting document edits */
wenzelm@38374
   176
wenzelm@38374
   177
    {
wenzelm@40479
   178
      val doc_edits = new mutable.ListBuffer[Document.Edit_Command]
wenzelm@38417
   179
      var nodes = previous.nodes
wenzelm@38374
   180
wenzelm@40478
   181
      edits foreach {
wenzelm@44156
   182
        case (name, Document.Node.Remove()) =>
wenzelm@44156
   183
          doc_edits += (name -> Document.Node.Remove())
wenzelm@40478
   184
          nodes -= name
wenzelm@40478
   185
wenzelm@44156
   186
        case (name, Document.Node.Edits(text_edits)) =>
wenzelm@43697
   187
          val node = nodes(name)
wenzelm@43697
   188
          val commands0 = node.commands
wenzelm@40478
   189
          val commands1 = edit_text(text_edits, commands0)
wenzelm@40478
   190
          val commands2 = recover_spans(commands1)   // FIXME somewhat slow
wenzelm@38374
   191
wenzelm@40478
   192
          val removed_commands = commands0.iterator.filter(!commands2.contains(_)).toList
wenzelm@40478
   193
          val inserted_commands = commands2.iterator.filter(!commands0.contains(_)).toList
wenzelm@38374
   194
wenzelm@40478
   195
          val cmd_edits =
wenzelm@40478
   196
            removed_commands.reverse.map(cmd => (commands0.prev(cmd), None)) :::
wenzelm@40478
   197
            inserted_commands.map(cmd => (commands2.prev(cmd), Some(cmd)))
wenzelm@38374
   198
wenzelm@44156
   199
          doc_edits += (name -> Document.Node.Edits(cmd_edits))
wenzelm@44157
   200
          nodes += (name -> node.copy(commands = commands2))
wenzelm@43722
   201
wenzelm@44157
   202
        case (name, Document.Node.Update_Header(header)) =>
wenzelm@44157
   203
          val node = nodes(name)
wenzelm@44157
   204
          val update_header =
wenzelm@44157
   205
            (node.header.thy_header, header) match {
wenzelm@44160
   206
              case (Exn.Res(thy_header0), Document.Node.Header(_, Exn.Res(thy_header))) =>
wenzelm@44160
   207
                thy_header0 != thy_header
wenzelm@44160
   208
              case _ => true
wenzelm@44157
   209
            }
wenzelm@44157
   210
          if (update_header) doc_edits += (name -> Document.Node.Update_Header(header))
wenzelm@38374
   211
      }
wenzelm@44157
   212
      (doc_edits.toList, Document.Version(Document.new_id(), nodes))
wenzelm@38374
   213
    }
wenzelm@38374
   214
  }
wenzelm@34268
   215
}