src/Pure/Thy/thy_syntax.scala
author wenzelm
Thu Sep 01 13:34:45 2011 +0200 (2011-09-01)
changeset 44615 a4ff8a787202
parent 44607 274eff0ea12e
child 45644 8634b4e61b88
permissions -rw-r--r--
more abstract Document.Node.Name;
tuned signature;
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@44615
    30
    def parse(syntax: Outer_Syntax, node_name: Document.Node.Name, text: CharSequence)
wenzelm@44607
    31
      : Entry =
wenzelm@40454
    32
    {
wenzelm@40454
    33
      /* stack operations */
wenzelm@40454
    34
wenzelm@40454
    35
      def buffer(): mutable.ListBuffer[Entry] = new mutable.ListBuffer[Entry]
wenzelm@44615
    36
      var stack: List[(Int, String, mutable.ListBuffer[Entry])] =
wenzelm@44615
    37
        List((0, "theory " + node_name.theory, buffer()))
wenzelm@40454
    38
wenzelm@40454
    39
      @tailrec def close(level: Int => Boolean)
wenzelm@40454
    40
      {
wenzelm@40454
    41
        stack match {
wenzelm@40454
    42
          case (lev, name, body) :: (_, _, body2) :: rest if level(lev) =>
wenzelm@40454
    43
            body2 += Block(name, body.toList)
wenzelm@40454
    44
            stack = stack.tail
wenzelm@40454
    45
            close(level)
wenzelm@40454
    46
          case _ =>
wenzelm@40454
    47
        }
wenzelm@40454
    48
      }
wenzelm@40454
    49
wenzelm@40454
    50
      def result(): Entry =
wenzelm@40454
    51
      {
wenzelm@40454
    52
        close(_ => true)
wenzelm@40454
    53
        val (_, name, body) = stack.head
wenzelm@40454
    54
        Block(name, body.toList)
wenzelm@40454
    55
      }
wenzelm@40454
    56
wenzelm@40454
    57
      def add(command: Command)
wenzelm@40454
    58
      {
wenzelm@40454
    59
        syntax.heading_level(command) match {
wenzelm@40454
    60
          case Some(i) =>
wenzelm@40457
    61
            close(_ >= i)
wenzelm@40454
    62
            stack = (i, command.source, buffer()) :: stack
wenzelm@40454
    63
          case None =>
wenzelm@40454
    64
        }
wenzelm@40454
    65
        stack.head._3 += Atom(command)
wenzelm@40454
    66
      }
wenzelm@40454
    67
wenzelm@40454
    68
wenzelm@40454
    69
      /* result structure */
wenzelm@40454
    70
wenzelm@40454
    71
      val spans = parse_spans(syntax.scan(text))
wenzelm@44607
    72
      spans.foreach(span => add(Command.span(node_name, span)))
wenzelm@40454
    73
      result()
wenzelm@40454
    74
    }
wenzelm@40454
    75
  }
wenzelm@40454
    76
wenzelm@40454
    77
wenzelm@40454
    78
wenzelm@38374
    79
  /** parse spans **/
wenzelm@38374
    80
wenzelm@38373
    81
  def parse_spans(toks: List[Token]): List[List[Token]] =
wenzelm@34268
    82
  {
wenzelm@38373
    83
    val result = new mutable.ListBuffer[List[Token]]
wenzelm@38239
    84
    val span = new mutable.ListBuffer[Token]
wenzelm@38239
    85
    val whitespace = new mutable.ListBuffer[Token]
wenzelm@34268
    86
wenzelm@38239
    87
    def flush(buffer: mutable.ListBuffer[Token])
wenzelm@38239
    88
    {
wenzelm@38239
    89
      if (!buffer.isEmpty) { result += buffer.toList; buffer.clear }
wenzelm@34268
    90
    }
wenzelm@38239
    91
    for (tok <- toks) {
wenzelm@38239
    92
      if (tok.is_command) { flush(span); flush(whitespace); span += tok }
wenzelm@38239
    93
      else if (tok.is_ignored) whitespace += tok
wenzelm@38239
    94
      else { span ++= whitespace; whitespace.clear; span += tok }
wenzelm@38239
    95
    }
wenzelm@38239
    96
    flush(span); flush(whitespace)
wenzelm@38239
    97
    result.toList
wenzelm@34268
    98
  }
wenzelm@38374
    99
wenzelm@38374
   100
wenzelm@38374
   101
wenzelm@44436
   102
  /** perspective **/
wenzelm@44388
   103
wenzelm@44388
   104
  def command_perspective(node: Document.Node, perspective: Text.Perspective): Command.Perspective =
wenzelm@44388
   105
  {
wenzelm@44474
   106
    if (perspective.is_empty) Command.Perspective.empty
wenzelm@44388
   107
    else {
wenzelm@44388
   108
      val result = new mutable.ListBuffer[Command]
wenzelm@44388
   109
      @tailrec
wenzelm@44388
   110
      def check_ranges(ranges: List[Text.Range], commands: Stream[(Command, Text.Offset)])
wenzelm@44388
   111
      {
wenzelm@44388
   112
        (ranges, commands) match {
wenzelm@44388
   113
          case (range :: more_ranges, (command, offset) #:: more_commands) =>
wenzelm@44388
   114
            val command_range = command.range + offset
wenzelm@44388
   115
            range compare command_range match {
wenzelm@44388
   116
              case -1 => check_ranges(more_ranges, commands)
wenzelm@44388
   117
              case 0 =>
wenzelm@44388
   118
                result += command
wenzelm@44388
   119
                check_ranges(ranges, more_commands)
wenzelm@44388
   120
              case 1 => check_ranges(ranges, more_commands)
wenzelm@44388
   121
            }
wenzelm@44388
   122
          case _ =>
wenzelm@44388
   123
        }
wenzelm@44388
   124
      }
wenzelm@44473
   125
      check_ranges(perspective.ranges, node.command_range(perspective.range).toStream)
wenzelm@44474
   126
      Command.Perspective(result.toList)
wenzelm@44388
   127
    }
wenzelm@44388
   128
  }
wenzelm@44388
   129
wenzelm@44615
   130
  def update_perspective(nodes: Document.Nodes,
wenzelm@44615
   131
      name: Document.Node.Name, text_perspective: Text.Perspective)
wenzelm@44436
   132
    : (Command.Perspective, Option[Document.Nodes]) =
wenzelm@44436
   133
  {
wenzelm@44436
   134
    val node = nodes(name)
wenzelm@44436
   135
    val perspective = command_perspective(node, text_perspective)
wenzelm@44436
   136
    val new_nodes =
wenzelm@44474
   137
      if (node.perspective same perspective) None
wenzelm@44436
   138
      else Some(nodes + (name -> node.copy(perspective = perspective)))
wenzelm@44436
   139
    (perspective, new_nodes)
wenzelm@44436
   140
  }
wenzelm@44436
   141
wenzelm@44615
   142
  def edit_perspective(previous: Document.Version,
wenzelm@44615
   143
      name: Document.Node.Name, text_perspective: Text.Perspective)
wenzelm@44436
   144
    : (Command.Perspective, Document.Version) =
wenzelm@44436
   145
  {
wenzelm@44436
   146
    val nodes = previous.nodes
wenzelm@44436
   147
    val (perspective, new_nodes) = update_perspective(nodes, name, text_perspective)
wenzelm@44436
   148
    val version = Document.Version(Document.new_id(), new_nodes getOrElse nodes)
wenzelm@44436
   149
    (perspective, version)
wenzelm@44436
   150
  }
wenzelm@44436
   151
wenzelm@44388
   152
wenzelm@44388
   153
wenzelm@38374
   154
  /** text edits **/
wenzelm@38374
   155
wenzelm@43722
   156
  def text_edits(
wenzelm@43722
   157
      syntax: Outer_Syntax,
wenzelm@43722
   158
      previous: Document.Version,
wenzelm@44157
   159
      edits: List[Document.Edit_Text])
wenzelm@44157
   160
    : (List[Document.Edit_Command], Document.Version) =
wenzelm@38374
   161
  {
wenzelm@38374
   162
    /* phase 1: edit individual command source */
wenzelm@38374
   163
wenzelm@38425
   164
    @tailrec def edit_text(eds: List[Text.Edit], commands: Linear_Set[Command])
wenzelm@38374
   165
        : Linear_Set[Command] =
wenzelm@38374
   166
    {
wenzelm@38374
   167
      eds match {
wenzelm@38374
   168
        case e :: es =>
wenzelm@38374
   169
          Document.Node.command_starts(commands.iterator).find {
wenzelm@38374
   170
            case (cmd, cmd_start) =>
wenzelm@38374
   171
              e.can_edit(cmd.source, cmd_start) ||
wenzelm@38374
   172
                e.is_insert && e.start == cmd_start + cmd.length
wenzelm@38374
   173
          } match {
wenzelm@38374
   174
            case Some((cmd, cmd_start)) if e.can_edit(cmd.source, cmd_start) =>
wenzelm@38374
   175
              val (rest, text) = e.edit(cmd.source, cmd_start)
wenzelm@38374
   176
              val new_commands = commands.insert_after(Some(cmd), Command.unparsed(text)) - cmd
wenzelm@38374
   177
              edit_text(rest.toList ::: es, new_commands)
wenzelm@38374
   178
wenzelm@38374
   179
            case Some((cmd, cmd_start)) =>
wenzelm@38374
   180
              edit_text(es, commands.insert_after(Some(cmd), Command.unparsed(e.text)))
wenzelm@38374
   181
wenzelm@38374
   182
            case None =>
wenzelm@38374
   183
              require(e.is_insert && e.start == 0)
wenzelm@38374
   184
              edit_text(es, commands.insert_after(None, Command.unparsed(e.text)))
wenzelm@38374
   185
          }
wenzelm@38374
   186
        case Nil => commands
wenzelm@38374
   187
      }
wenzelm@38374
   188
    }
wenzelm@38374
   189
wenzelm@38374
   190
wenzelm@38374
   191
    /* phase 2: recover command spans */
wenzelm@38374
   192
wenzelm@44615
   193
    @tailrec def recover_spans(node_name: Document.Node.Name, commands: Linear_Set[Command])
wenzelm@44607
   194
      : Linear_Set[Command] =
wenzelm@38374
   195
    {
wenzelm@44385
   196
      commands.iterator.find(cmd => !cmd.is_defined) match {
wenzelm@38374
   197
        case Some(first_unparsed) =>
wenzelm@38374
   198
          val first =
wenzelm@38878
   199
            commands.reverse_iterator(first_unparsed).
wenzelm@38878
   200
              dropWhile(_.newlines == 0).find(_.is_command) getOrElse commands.head
wenzelm@38374
   201
          val last =
wenzelm@38878
   202
            commands.iterator(first_unparsed).
wenzelm@38878
   203
              dropWhile(_.newlines == 0).find(_.is_command) getOrElse commands.last
wenzelm@38374
   204
          val range =
wenzelm@38374
   205
            commands.iterator(first).takeWhile(_ != last).toList ::: List(last)
wenzelm@38374
   206
wenzelm@38374
   207
          val sources = range.flatMap(_.span.map(_.source))
wenzelm@43647
   208
          val spans0 = parse_spans(syntax.scan(sources.mkString))
wenzelm@38374
   209
wenzelm@38374
   210
          val (before_edit, spans1) =
wenzelm@38374
   211
            if (!spans0.isEmpty && first.is_command && first.span == spans0.head)
wenzelm@38374
   212
              (Some(first), spans0.tail)
wenzelm@38374
   213
            else (commands.prev(first), spans0)
wenzelm@38374
   214
wenzelm@38374
   215
          val (after_edit, spans2) =
wenzelm@38374
   216
            if (!spans1.isEmpty && last.is_command && last.span == spans1.last)
wenzelm@38374
   217
              (Some(last), spans1.take(spans1.length - 1))
wenzelm@38374
   218
            else (commands.next(last), spans1)
wenzelm@38374
   219
wenzelm@44607
   220
          val inserted = spans2.map(span => new Command(Document.new_id(), node_name, span))
wenzelm@38374
   221
          val new_commands =
wenzelm@38374
   222
            commands.delete_between(before_edit, after_edit).append_after(before_edit, inserted)
wenzelm@44607
   223
          recover_spans(node_name, new_commands)
wenzelm@38374
   224
wenzelm@38374
   225
        case None => commands
wenzelm@38374
   226
      }
wenzelm@38374
   227
    }
wenzelm@38374
   228
wenzelm@38374
   229
wenzelm@38374
   230
    /* resulting document edits */
wenzelm@38374
   231
wenzelm@38374
   232
    {
wenzelm@40479
   233
      val doc_edits = new mutable.ListBuffer[Document.Edit_Command]
wenzelm@38417
   234
      var nodes = previous.nodes
wenzelm@38374
   235
wenzelm@40478
   236
      edits foreach {
wenzelm@44185
   237
        case (name, Document.Node.Clear()) =>
wenzelm@44185
   238
          doc_edits += (name -> Document.Node.Clear())
wenzelm@44443
   239
          nodes += (name -> nodes(name).clear)
wenzelm@40478
   240
wenzelm@44156
   241
        case (name, Document.Node.Edits(text_edits)) =>
wenzelm@43697
   242
          val node = nodes(name)
wenzelm@43697
   243
          val commands0 = node.commands
wenzelm@40478
   244
          val commands1 = edit_text(text_edits, commands0)
wenzelm@44607
   245
          val commands2 = recover_spans(name, commands1)   // FIXME somewhat slow
wenzelm@38374
   246
wenzelm@40478
   247
          val removed_commands = commands0.iterator.filter(!commands2.contains(_)).toList
wenzelm@40478
   248
          val inserted_commands = commands2.iterator.filter(!commands0.contains(_)).toList
wenzelm@38374
   249
wenzelm@40478
   250
          val cmd_edits =
wenzelm@40478
   251
            removed_commands.reverse.map(cmd => (commands0.prev(cmd), None)) :::
wenzelm@40478
   252
            inserted_commands.map(cmd => (commands2.prev(cmd), Some(cmd)))
wenzelm@38374
   253
wenzelm@44156
   254
          doc_edits += (name -> Document.Node.Edits(cmd_edits))
wenzelm@44157
   255
          nodes += (name -> node.copy(commands = commands2))
wenzelm@43722
   256
wenzelm@44182
   257
        case (name, Document.Node.Header(header)) =>
wenzelm@44157
   258
          val node = nodes(name)
wenzelm@44157
   259
          val update_header =
wenzelm@44182
   260
            (node.header, header) match {
wenzelm@44182
   261
              case (Exn.Res(thy_header0), Exn.Res(thy_header)) => thy_header0 != thy_header
wenzelm@44160
   262
              case _ => true
wenzelm@44157
   263
            }
wenzelm@44180
   264
          if (update_header) {
wenzelm@44182
   265
            doc_edits += (name -> Document.Node.Header(header))
wenzelm@44180
   266
            nodes += (name -> node.copy(header = header))
wenzelm@44180
   267
          }
wenzelm@44385
   268
wenzelm@44385
   269
        case (name, Document.Node.Perspective(text_perspective)) =>
wenzelm@44436
   270
          update_perspective(nodes, name, text_perspective) match {
wenzelm@44436
   271
            case (_, None) =>
wenzelm@44436
   272
            case (perspective, Some(nodes1)) =>
wenzelm@44436
   273
              doc_edits += (name -> Document.Node.Perspective(perspective))
wenzelm@44436
   274
              nodes = nodes1
wenzelm@44385
   275
          }
wenzelm@38374
   276
      }
wenzelm@44157
   277
      (doc_edits.toList, Document.Version(Document.new_id(), nodes))
wenzelm@38374
   278
    }
wenzelm@38374
   279
  }
wenzelm@34268
   280
}