src/Pure/Thy/thy_syntax.scala
author wenzelm
Thu Mar 15 17:45:54 2012 +0100 (2012-03-15)
changeset 46946 acc8ebf980ca
parent 46942 f5c2d66faa04
child 46969 481b7d9ad6fe
permissions -rw-r--r--
more explicit header_edits before main text_edits;
handle reparses caused by syntax update;
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@46811
    30
    def parse(syntax: Outer_Syntax, node_name: Document.Node.Name, 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@44615
    35
      var stack: List[(Int, String, mutable.ListBuffer[Entry])] =
wenzelm@44615
    36
        List((0, "theory " + node_name.theory, buffer()))
wenzelm@40454
    37
wenzelm@40454
    38
      @tailrec def close(level: Int => Boolean)
wenzelm@40454
    39
      {
wenzelm@40454
    40
        stack match {
wenzelm@40454
    41
          case (lev, name, body) :: (_, _, body2) :: rest if level(lev) =>
wenzelm@40454
    42
            body2 += Block(name, body.toList)
wenzelm@40454
    43
            stack = stack.tail
wenzelm@40454
    44
            close(level)
wenzelm@40454
    45
          case _ =>
wenzelm@40454
    46
        }
wenzelm@40454
    47
      }
wenzelm@40454
    48
wenzelm@40454
    49
      def result(): Entry =
wenzelm@40454
    50
      {
wenzelm@40454
    51
        close(_ => true)
wenzelm@40454
    52
        val (_, name, body) = stack.head
wenzelm@40454
    53
        Block(name, body.toList)
wenzelm@40454
    54
      }
wenzelm@40454
    55
wenzelm@40454
    56
      def add(command: Command)
wenzelm@40454
    57
      {
wenzelm@40454
    58
        syntax.heading_level(command) match {
wenzelm@40454
    59
          case Some(i) =>
wenzelm@40457
    60
            close(_ >= i)
wenzelm@40454
    61
            stack = (i, command.source, buffer()) :: stack
wenzelm@40454
    62
          case None =>
wenzelm@40454
    63
        }
wenzelm@40454
    64
        stack.head._3 += Atom(command)
wenzelm@40454
    65
      }
wenzelm@40454
    66
wenzelm@40454
    67
wenzelm@40454
    68
      /* result structure */
wenzelm@40454
    69
wenzelm@40454
    70
      val spans = parse_spans(syntax.scan(text))
wenzelm@45644
    71
      spans.foreach(span => add(Command(node_name, span)))
wenzelm@40454
    72
      result()
wenzelm@40454
    73
    }
wenzelm@40454
    74
  }
wenzelm@40454
    75
wenzelm@40454
    76
wenzelm@40454
    77
wenzelm@38374
    78
  /** parse spans **/
wenzelm@38374
    79
wenzelm@38373
    80
  def parse_spans(toks: List[Token]): List[List[Token]] =
wenzelm@34268
    81
  {
wenzelm@38373
    82
    val result = new mutable.ListBuffer[List[Token]]
wenzelm@38239
    83
    val span = new mutable.ListBuffer[Token]
wenzelm@34268
    84
wenzelm@46811
    85
    def flush() { if (!span.isEmpty) { result += span.toList; span.clear } }
wenzelm@46811
    86
    for (tok <- toks) { if (tok.is_command) flush(); span += tok }
wenzelm@46811
    87
    flush()
wenzelm@38239
    88
    result.toList
wenzelm@34268
    89
  }
wenzelm@38374
    90
wenzelm@38374
    91
wenzelm@38374
    92
wenzelm@44436
    93
  /** perspective **/
wenzelm@44388
    94
wenzelm@44388
    95
  def command_perspective(node: Document.Node, perspective: Text.Perspective): Command.Perspective =
wenzelm@44388
    96
  {
wenzelm@44474
    97
    if (perspective.is_empty) Command.Perspective.empty
wenzelm@44388
    98
    else {
wenzelm@44388
    99
      val result = new mutable.ListBuffer[Command]
wenzelm@44388
   100
      @tailrec
wenzelm@44388
   101
      def check_ranges(ranges: List[Text.Range], commands: Stream[(Command, Text.Offset)])
wenzelm@44388
   102
      {
wenzelm@44388
   103
        (ranges, commands) match {
wenzelm@44388
   104
          case (range :: more_ranges, (command, offset) #:: more_commands) =>
wenzelm@44388
   105
            val command_range = command.range + offset
wenzelm@44388
   106
            range compare command_range match {
wenzelm@44388
   107
              case -1 => check_ranges(more_ranges, commands)
wenzelm@44388
   108
              case 0 =>
wenzelm@44388
   109
                result += command
wenzelm@44388
   110
                check_ranges(ranges, more_commands)
wenzelm@44388
   111
              case 1 => check_ranges(ranges, more_commands)
wenzelm@44388
   112
            }
wenzelm@44388
   113
          case _ =>
wenzelm@44388
   114
        }
wenzelm@44388
   115
      }
wenzelm@44473
   116
      check_ranges(perspective.ranges, node.command_range(perspective.range).toStream)
wenzelm@44474
   117
      Command.Perspective(result.toList)
wenzelm@44388
   118
    }
wenzelm@44388
   119
  }
wenzelm@44388
   120
wenzelm@44615
   121
  def update_perspective(nodes: Document.Nodes,
wenzelm@44615
   122
      name: Document.Node.Name, text_perspective: Text.Perspective)
wenzelm@44436
   123
    : (Command.Perspective, Option[Document.Nodes]) =
wenzelm@44436
   124
  {
wenzelm@44436
   125
    val node = nodes(name)
wenzelm@44436
   126
    val perspective = command_perspective(node, text_perspective)
wenzelm@44436
   127
    val new_nodes =
wenzelm@44474
   128
      if (node.perspective same perspective) None
wenzelm@46680
   129
      else Some(nodes + (name -> node.update_perspective(perspective)))
wenzelm@44436
   130
    (perspective, new_nodes)
wenzelm@44436
   131
  }
wenzelm@44436
   132
wenzelm@44615
   133
  def edit_perspective(previous: Document.Version,
wenzelm@44615
   134
      name: Document.Node.Name, text_perspective: Text.Perspective)
wenzelm@44436
   135
    : (Command.Perspective, Document.Version) =
wenzelm@44436
   136
  {
wenzelm@44436
   137
    val nodes = previous.nodes
wenzelm@44436
   138
    val (perspective, new_nodes) = update_perspective(nodes, name, text_perspective)
wenzelm@46941
   139
    val version = Document.Version.make(previous.syntax, new_nodes getOrElse nodes)
wenzelm@44436
   140
    (perspective, version)
wenzelm@44436
   141
  }
wenzelm@44436
   142
wenzelm@44388
   143
wenzelm@44388
   144
wenzelm@46946
   145
  /** header edits: structure and outer syntax **/
wenzelm@46946
   146
wenzelm@46946
   147
  private def header_edits(
wenzelm@46946
   148
    base_syntax: Outer_Syntax,
wenzelm@46946
   149
    previous: Document.Version,
wenzelm@46946
   150
    edits: List[Document.Edit_Text])
wenzelm@46946
   151
    : (Outer_Syntax, List[Document.Node.Name], Document.Nodes, List[Document.Edit_Command]) =
wenzelm@46946
   152
  {
wenzelm@46946
   153
    var rebuild_syntax = previous.is_init
wenzelm@46946
   154
    var nodes = previous.nodes
wenzelm@46946
   155
    val doc_edits = new mutable.ListBuffer[Document.Edit_Command]
wenzelm@46946
   156
wenzelm@46946
   157
    edits foreach {
wenzelm@46946
   158
      case (name, Document.Node.Header(header)) =>
wenzelm@46946
   159
        val node = nodes(name)
wenzelm@46946
   160
        val update_header =
wenzelm@46946
   161
          (node.header, header) match {
wenzelm@46946
   162
            case (Exn.Res(deps0), Exn.Res(deps)) => deps0 != deps
wenzelm@46946
   163
            case _ => true
wenzelm@46946
   164
          }
wenzelm@46946
   165
        if (update_header) {
wenzelm@46946
   166
          val node1 = node.update_header(header)
wenzelm@46946
   167
          rebuild_syntax = rebuild_syntax || (node.keywords != node1.keywords)
wenzelm@46946
   168
          nodes += (name -> node1)
wenzelm@46946
   169
          doc_edits += (name -> Document.Node.Header(header))
wenzelm@46946
   170
        }
wenzelm@46946
   171
      case _ =>
wenzelm@46946
   172
    }
wenzelm@46946
   173
wenzelm@46946
   174
    val syntax =
wenzelm@46946
   175
      if (rebuild_syntax)
wenzelm@46946
   176
        (base_syntax /: nodes.entries)({ case (syn, (_, node)) => (syn /: node.keywords)(_ + _) })
wenzelm@46946
   177
      else previous.syntax
wenzelm@46946
   178
wenzelm@46946
   179
    val reparse =
wenzelm@46946
   180
      if (rebuild_syntax) nodes.descendants(doc_edits.iterator.map(_._1).toList)
wenzelm@46946
   181
      else Nil
wenzelm@46946
   182
wenzelm@46946
   183
    (syntax, reparse, nodes, doc_edits.toList)
wenzelm@46946
   184
  }
wenzelm@46946
   185
wenzelm@46946
   186
wenzelm@46946
   187
wenzelm@38374
   188
  /** text edits **/
wenzelm@38374
   189
wenzelm@46946
   190
  /* phase 1: edit individual command source */
wenzelm@46946
   191
wenzelm@46946
   192
  @tailrec private def edit_text(eds: List[Text.Edit], commands: Linear_Set[Command])
wenzelm@46946
   193
      : Linear_Set[Command] =
wenzelm@46946
   194
  {
wenzelm@46946
   195
    eds match {
wenzelm@46946
   196
      case e :: es =>
wenzelm@46946
   197
        Document.Node.command_starts(commands.iterator).find {
wenzelm@46946
   198
          case (cmd, cmd_start) =>
wenzelm@46946
   199
            e.can_edit(cmd.source, cmd_start) ||
wenzelm@46946
   200
              e.is_insert && e.start == cmd_start + cmd.length
wenzelm@46946
   201
        } match {
wenzelm@46946
   202
          case Some((cmd, cmd_start)) if e.can_edit(cmd.source, cmd_start) =>
wenzelm@46946
   203
            val (rest, text) = e.edit(cmd.source, cmd_start)
wenzelm@46946
   204
            val new_commands = commands.insert_after(Some(cmd), Command.unparsed(text)) - cmd
wenzelm@46946
   205
            edit_text(rest.toList ::: es, new_commands)
wenzelm@46946
   206
wenzelm@46946
   207
          case Some((cmd, cmd_start)) =>
wenzelm@46946
   208
            edit_text(es, commands.insert_after(Some(cmd), Command.unparsed(e.text)))
wenzelm@46946
   209
wenzelm@46946
   210
          case None =>
wenzelm@46946
   211
            require(e.is_insert && e.start == 0)
wenzelm@46946
   212
            edit_text(es, commands.insert_after(None, Command.unparsed(e.text)))
wenzelm@46946
   213
        }
wenzelm@46946
   214
      case Nil => commands
wenzelm@46946
   215
    }
wenzelm@46946
   216
  }
wenzelm@46946
   217
wenzelm@46946
   218
wenzelm@46946
   219
  /* phase 2: recover command spans */
wenzelm@46946
   220
wenzelm@46946
   221
  @tailrec private def recover_spans(
wenzelm@46946
   222
    syntax: Outer_Syntax,
wenzelm@46946
   223
    node_name: Document.Node.Name,
wenzelm@46946
   224
    commands: Linear_Set[Command]): Linear_Set[Command] =
wenzelm@46946
   225
  {
wenzelm@46946
   226
    commands.iterator.find(cmd => !cmd.is_defined) match {
wenzelm@46946
   227
      case Some(first_unparsed) =>
wenzelm@46946
   228
        val first =
wenzelm@46946
   229
          commands.reverse_iterator(first_unparsed).
wenzelm@46946
   230
            dropWhile(_.newlines == 0).find(_.is_command) getOrElse commands.head
wenzelm@46946
   231
        val last =
wenzelm@46946
   232
          commands.iterator(first_unparsed).
wenzelm@46946
   233
            dropWhile(_.newlines == 0).find(_.is_command) getOrElse commands.last
wenzelm@46946
   234
        val range =
wenzelm@46946
   235
          commands.iterator(first).takeWhile(_ != last).toList ::: List(last)
wenzelm@46946
   236
wenzelm@46946
   237
        val spans0 = parse_spans(syntax.scan(range.map(_.source).mkString))
wenzelm@46946
   238
wenzelm@46946
   239
        val (before_edit, spans1) =
wenzelm@46946
   240
          if (!spans0.isEmpty && first.is_command && first.span == spans0.head)
wenzelm@46946
   241
            (Some(first), spans0.tail)
wenzelm@46946
   242
          else (commands.prev(first), spans0)
wenzelm@46946
   243
wenzelm@46946
   244
        val (after_edit, spans2) =
wenzelm@46946
   245
          if (!spans1.isEmpty && last.is_command && last.span == spans1.last)
wenzelm@46946
   246
            (Some(last), spans1.take(spans1.length - 1))
wenzelm@46946
   247
          else (commands.next(last), spans1)
wenzelm@46946
   248
wenzelm@46946
   249
        val inserted = spans2.map(span => Command(Document.new_id(), node_name, span))
wenzelm@46946
   250
        val new_commands =
wenzelm@46946
   251
          commands.delete_between(before_edit, after_edit).append_after(before_edit, inserted)
wenzelm@46946
   252
        recover_spans(syntax, node_name, new_commands)
wenzelm@46946
   253
wenzelm@46946
   254
      case None => commands
wenzelm@46946
   255
    }
wenzelm@46946
   256
  }
wenzelm@46946
   257
wenzelm@46946
   258
wenzelm@46946
   259
  /* phase 3: full reparsing after syntax change */
wenzelm@46946
   260
wenzelm@46946
   261
  private def reparse_spans(
wenzelm@46946
   262
    syntax: Outer_Syntax,
wenzelm@46946
   263
    node_name: Document.Node.Name,
wenzelm@46946
   264
    commands: Linear_Set[Command]): Linear_Set[Command] =
wenzelm@46946
   265
  {
wenzelm@46946
   266
    val cmds = commands.toList
wenzelm@46946
   267
    val spans1 = parse_spans(syntax.scan(cmds.map(_.source).mkString))
wenzelm@46946
   268
    if (cmds.map(_.span) == spans1) commands
wenzelm@46946
   269
    else Linear_Set(spans1.map(span => Command(Document.new_id(), node_name, span)): _*)
wenzelm@46946
   270
  }
wenzelm@46946
   271
wenzelm@46946
   272
wenzelm@46946
   273
  /* main phase */
wenzelm@46946
   274
wenzelm@43722
   275
  def text_edits(
wenzelm@46942
   276
      base_syntax: Outer_Syntax,
wenzelm@43722
   277
      previous: Document.Version,
wenzelm@44157
   278
      edits: List[Document.Edit_Text])
wenzelm@44157
   279
    : (List[Document.Edit_Command], Document.Version) =
wenzelm@38374
   280
  {
wenzelm@46946
   281
    val (syntax, reparse, nodes0, doc_edits0) = header_edits(base_syntax, previous, edits)
wenzelm@46946
   282
    val reparse_set = reparse.toSet
wenzelm@46946
   283
wenzelm@46946
   284
    var nodes = nodes0
wenzelm@46946
   285
    val doc_edits = new mutable.ListBuffer[Document.Edit_Command]; doc_edits ++= doc_edits0
wenzelm@38374
   286
wenzelm@46946
   287
    (edits ::: reparse.map((_, Document.Node.Edits(Nil)))) foreach {
wenzelm@46946
   288
      case (name, Document.Node.Clear()) =>
wenzelm@46946
   289
        doc_edits += (name -> Document.Node.Clear())
wenzelm@46946
   290
        nodes += (name -> nodes(name).clear)
wenzelm@38374
   291
wenzelm@46946
   292
      case (name, Document.Node.Edits(text_edits)) =>
wenzelm@46946
   293
        val node = nodes(name)
wenzelm@46946
   294
        val commands0 = node.commands
wenzelm@46946
   295
        val commands1 = edit_text(text_edits, commands0)
wenzelm@46946
   296
        val commands2 = recover_spans(syntax, name, commands1)   // FIXME somewhat slow
wenzelm@46946
   297
        val commands3 =
wenzelm@46946
   298
          if (reparse_set.contains(name)) reparse_spans(syntax, name, commands2)  // slow
wenzelm@46946
   299
          else commands2
wenzelm@38374
   300
wenzelm@46946
   301
        val removed_commands = commands0.iterator.filter(!commands3.contains(_)).toList
wenzelm@46946
   302
        val inserted_commands = commands3.iterator.filter(!commands0.contains(_)).toList
wenzelm@46946
   303
wenzelm@46946
   304
        val cmd_edits =
wenzelm@46946
   305
          removed_commands.reverse.map(cmd => (commands0.prev(cmd), None)) :::
wenzelm@46946
   306
          inserted_commands.map(cmd => (commands3.prev(cmd), Some(cmd)))
wenzelm@46946
   307
wenzelm@46946
   308
        doc_edits += (name -> Document.Node.Edits(cmd_edits))
wenzelm@46946
   309
        nodes += (name -> node.update_commands(commands3))
wenzelm@38374
   310
wenzelm@46946
   311
      case (name, Document.Node.Header(_)) =>
wenzelm@38374
   312
wenzelm@46946
   313
      case (name, Document.Node.Perspective(text_perspective)) =>
wenzelm@46946
   314
        update_perspective(nodes, name, text_perspective) match {
wenzelm@46946
   315
          case (_, None) =>
wenzelm@46946
   316
          case (perspective, Some(nodes1)) =>
wenzelm@46946
   317
            doc_edits += (name -> Document.Node.Perspective(perspective))
wenzelm@46946
   318
            nodes = nodes1
wenzelm@46946
   319
        }
wenzelm@38374
   320
    }
wenzelm@46946
   321
    (doc_edits.toList, Document.Version.make(syntax, nodes))
wenzelm@38374
   322
  }
wenzelm@34268
   323
}