src/Pure/Thy/thy_syntax.scala
author wenzelm
Mon Aug 05 15:03:52 2013 +0200 (2013-08-05)
changeset 52861 e93d73b51fd0
parent 52849 199e9fa5a5c2
child 52887 98eac7b7eec3
permissions -rw-r--r--
commands with overlay remain visible, to avoid loosing printed output;
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@48718
    36
        List((0, 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@46969
    60
            close(_ > i)
wenzelm@46969
    61
            stack = (i + 1, 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@52530
    71
      spans.foreach(span => add(Command(Document_ID.none, 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@52535
    80
  def parse_spans(toks: List[Token]): List[List[Token]] =
wenzelm@34268
    81
  {
wenzelm@52535
    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@52861
    95
  def command_perspective(
wenzelm@52861
    96
      node: Document.Node,
wenzelm@52861
    97
      perspective: Text.Perspective,
wenzelm@52861
    98
      overlays: Document.Overlays): (Command.Perspective, Command.Perspective) =
wenzelm@44388
    99
  {
wenzelm@52861
   100
    if (perspective.is_empty && overlays.is_empty)
wenzelm@52861
   101
      (Command.Perspective.empty, Command.Perspective.empty)
wenzelm@44388
   102
    else {
wenzelm@52861
   103
      val has_overlay = overlays.commands
wenzelm@52861
   104
      val visible = new mutable.ListBuffer[Command]
wenzelm@52861
   105
      val visible_overlay = new mutable.ListBuffer[Command]
wenzelm@44388
   106
      @tailrec
wenzelm@44388
   107
      def check_ranges(ranges: List[Text.Range], commands: Stream[(Command, Text.Offset)])
wenzelm@44388
   108
      {
wenzelm@44388
   109
        (ranges, commands) match {
wenzelm@44388
   110
          case (range :: more_ranges, (command, offset) #:: more_commands) =>
wenzelm@44388
   111
            val command_range = command.range + offset
wenzelm@44388
   112
            range compare command_range match {
wenzelm@44388
   113
              case 0 =>
wenzelm@52861
   114
                visible += command
wenzelm@52861
   115
                visible_overlay += command
wenzelm@44388
   116
                check_ranges(ranges, more_commands)
wenzelm@52861
   117
              case c =>
wenzelm@52861
   118
                if (has_overlay(command)) visible_overlay += command
wenzelm@52861
   119
wenzelm@52861
   120
                if (c < 0) check_ranges(more_ranges, commands)
wenzelm@52861
   121
                else check_ranges(ranges, more_commands)
wenzelm@44388
   122
            }
wenzelm@52861
   123
wenzelm@52861
   124
          case (Nil, (command, _) #:: more_commands) =>
wenzelm@52861
   125
            if (has_overlay(command)) visible_overlay += command
wenzelm@52861
   126
wenzelm@52861
   127
            check_ranges(Nil, more_commands)
wenzelm@52861
   128
wenzelm@44388
   129
          case _ =>
wenzelm@44388
   130
        }
wenzelm@44388
   131
      }
wenzelm@52861
   132
wenzelm@52861
   133
      val commands =
wenzelm@52861
   134
        if (overlays.is_empty) node.command_range(perspective.range)
wenzelm@52861
   135
        else node.command_range()
wenzelm@52861
   136
      check_ranges(perspective.ranges, commands.toStream)
wenzelm@52861
   137
      (Command.Perspective(visible.toList), Command.Perspective(visible_overlay.toList))
wenzelm@44388
   138
    }
wenzelm@44388
   139
  }
wenzelm@44388
   140
wenzelm@44388
   141
wenzelm@44388
   142
wenzelm@46946
   143
  /** header edits: structure and outer syntax **/
wenzelm@46946
   144
wenzelm@46946
   145
  private def header_edits(
wenzelm@46946
   146
    base_syntax: Outer_Syntax,
wenzelm@46946
   147
    previous: Document.Version,
wenzelm@46946
   148
    edits: List[Document.Edit_Text])
wenzelm@46946
   149
    : (Outer_Syntax, List[Document.Node.Name], Document.Nodes, List[Document.Edit_Command]) =
wenzelm@46946
   150
  {
wenzelm@47987
   151
    var updated_imports = false
wenzelm@47987
   152
    var updated_keywords = false
wenzelm@46946
   153
    var nodes = previous.nodes
wenzelm@46946
   154
    val doc_edits = new mutable.ListBuffer[Document.Edit_Command]
wenzelm@46946
   155
wenzelm@46946
   156
    edits foreach {
wenzelm@48707
   157
      case (name, Document.Node.Deps(header)) =>
wenzelm@46946
   158
        val node = nodes(name)
wenzelm@46946
   159
        val update_header =
wenzelm@48707
   160
          !node.header.errors.isEmpty || !header.errors.isEmpty || node.header != header
wenzelm@46946
   161
        if (update_header) {
wenzelm@46946
   162
          val node1 = node.update_header(header)
wenzelm@48707
   163
          updated_imports = updated_imports || (node.header.imports != node1.header.imports)
wenzelm@48707
   164
          updated_keywords = updated_keywords || (node.header.keywords != node1.header.keywords)
wenzelm@46946
   165
          nodes += (name -> node1)
wenzelm@48707
   166
          doc_edits += (name -> Document.Node.Deps(header))
wenzelm@46946
   167
        }
wenzelm@46946
   168
      case _ =>
wenzelm@46946
   169
    }
wenzelm@46946
   170
wenzelm@46946
   171
    val syntax =
wenzelm@47987
   172
      if (previous.is_init || updated_keywords)
wenzelm@48873
   173
        (base_syntax /: nodes.entries) {
wenzelm@48873
   174
          case (syn, (_, node)) => syn.add_keywords(node.header.keywords)
wenzelm@48873
   175
        }
wenzelm@46946
   176
      else previous.syntax
wenzelm@46946
   177
wenzelm@46946
   178
    val reparse =
wenzelm@47987
   179
      if (updated_imports || updated_keywords)
wenzelm@47987
   180
        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@48755
   190
  /* edit individual command source */
wenzelm@46946
   191
wenzelm@50761
   192
  @tailrec def edit_text(eds: List[Text.Edit], commands: Linear_Set[Command]): Linear_Set[Command] =
wenzelm@46946
   193
  {
wenzelm@46946
   194
    eds match {
wenzelm@46946
   195
      case e :: es =>
wenzelm@46946
   196
        Document.Node.command_starts(commands.iterator).find {
wenzelm@46946
   197
          case (cmd, cmd_start) =>
wenzelm@46946
   198
            e.can_edit(cmd.source, cmd_start) ||
wenzelm@46946
   199
              e.is_insert && e.start == cmd_start + cmd.length
wenzelm@46946
   200
        } match {
wenzelm@46946
   201
          case Some((cmd, cmd_start)) if e.can_edit(cmd.source, cmd_start) =>
wenzelm@46946
   202
            val (rest, text) = e.edit(cmd.source, cmd_start)
wenzelm@46946
   203
            val new_commands = commands.insert_after(Some(cmd), Command.unparsed(text)) - cmd
wenzelm@46946
   204
            edit_text(rest.toList ::: es, new_commands)
wenzelm@46946
   205
wenzelm@46946
   206
          case Some((cmd, cmd_start)) =>
wenzelm@46946
   207
            edit_text(es, commands.insert_after(Some(cmd), Command.unparsed(e.text)))
wenzelm@46946
   208
wenzelm@46946
   209
          case None =>
wenzelm@46946
   210
            require(e.is_insert && e.start == 0)
wenzelm@46946
   211
            edit_text(es, commands.insert_after(None, Command.unparsed(e.text)))
wenzelm@46946
   212
        }
wenzelm@46946
   213
      case Nil => commands
wenzelm@46946
   214
    }
wenzelm@46946
   215
  }
wenzelm@46946
   216
wenzelm@46946
   217
wenzelm@48755
   218
  /* reparse range of command spans */
wenzelm@46946
   219
wenzelm@48748
   220
  @tailrec private def chop_common(
wenzelm@52535
   221
      cmds: List[Command], spans: List[List[Token]]): (List[Command], List[List[Token]]) =
wenzelm@48748
   222
    (cmds, spans) match {
wenzelm@48748
   223
      case (c :: cs, s :: ss) if c.span == s => chop_common(cs, ss)
wenzelm@48748
   224
      case _ => (cmds, spans)
wenzelm@48748
   225
    }
wenzelm@48748
   226
wenzelm@48754
   227
  private def reparse_spans(
wenzelm@48754
   228
    syntax: Outer_Syntax,
wenzelm@48754
   229
    name: Document.Node.Name,
wenzelm@48754
   230
    commands: Linear_Set[Command],
wenzelm@48754
   231
    first: Command, last: Command): Linear_Set[Command] =
wenzelm@48748
   232
  {
wenzelm@48754
   233
    val cmds0 = commands.iterator(first, last).toList
wenzelm@48754
   234
    val spans0 = parse_spans(syntax.scan(cmds0.iterator.map(_.source).mkString))
wenzelm@48754
   235
wenzelm@48754
   236
    val (cmds1, spans1) = chop_common(cmds0, spans0)
wenzelm@48754
   237
wenzelm@48748
   238
    val (rev_cmds2, rev_spans2) = chop_common(cmds1.reverse, spans1.reverse)
wenzelm@48754
   239
    val cmds2 = rev_cmds2.reverse
wenzelm@48754
   240
    val spans2 = rev_spans2.reverse
wenzelm@48754
   241
wenzelm@48754
   242
    cmds2 match {
wenzelm@48754
   243
      case Nil =>
wenzelm@48754
   244
        assert(spans2.isEmpty)
wenzelm@48754
   245
        commands
wenzelm@48754
   246
      case cmd :: _ =>
wenzelm@48754
   247
        val hook = commands.prev(cmd)
wenzelm@52530
   248
        val inserted = spans2.map(span => Command(Document_ID.make(), name, span))
wenzelm@48754
   249
        (commands /: cmds2)(_ - _).append_after(hook, inserted)
wenzelm@48754
   250
    }
wenzelm@48748
   251
  }
wenzelm@48748
   252
wenzelm@48754
   253
wenzelm@48755
   254
  /* recover command spans after edits */
wenzelm@48754
   255
wenzelm@48755
   256
  // FIXME somewhat slow
wenzelm@48746
   257
  private def recover_spans(
wenzelm@46946
   258
    syntax: Outer_Syntax,
wenzelm@48754
   259
    name: Document.Node.Name,
wenzelm@48748
   260
    perspective: Command.Perspective,
wenzelm@48754
   261
    commands: Linear_Set[Command]): Linear_Set[Command] =
wenzelm@46946
   262
  {
wenzelm@48754
   263
    val visible = perspective.commands.toSet
wenzelm@48748
   264
wenzelm@48754
   265
    def next_invisible_command(cmds: Linear_Set[Command], from: Command): Command =
wenzelm@48754
   266
      cmds.iterator(from).dropWhile(cmd => !cmd.is_command || visible(cmd))
wenzelm@48754
   267
        .find(_.is_command) getOrElse cmds.last
wenzelm@48746
   268
wenzelm@48754
   269
    @tailrec def recover(cmds: Linear_Set[Command]): Linear_Set[Command] =
wenzelm@48754
   270
      cmds.find(_.is_unparsed) match {
wenzelm@48754
   271
        case Some(first_unparsed) =>
wenzelm@48754
   272
          val first = next_invisible_command(cmds.reverse, first_unparsed)
wenzelm@48754
   273
          val last = next_invisible_command(cmds, first_unparsed)
wenzelm@48754
   274
          recover(reparse_spans(syntax, name, cmds, first, last))
wenzelm@48754
   275
        case None => cmds
wenzelm@48746
   276
      }
wenzelm@48754
   277
    recover(commands)
wenzelm@46946
   278
  }
wenzelm@46946
   279
wenzelm@46946
   280
wenzelm@48755
   281
  /* consolidate unfinished spans */
wenzelm@46946
   282
wenzelm@48754
   283
  private def consolidate_spans(
wenzelm@46946
   284
    syntax: Outer_Syntax,
wenzelm@49524
   285
    reparse_limit: Int,
wenzelm@48754
   286
    name: Document.Node.Name,
wenzelm@48754
   287
    perspective: Command.Perspective,
wenzelm@46946
   288
    commands: Linear_Set[Command]): Linear_Set[Command] =
wenzelm@46946
   289
  {
wenzelm@48754
   290
    if (perspective.commands.isEmpty) commands
wenzelm@48754
   291
    else {
wenzelm@48754
   292
      commands.find(_.is_unfinished) match {
wenzelm@48754
   293
        case Some(first_unfinished) =>
wenzelm@48754
   294
          val visible = perspective.commands.toSet
wenzelm@48754
   295
          commands.reverse.find(visible) match {
wenzelm@48754
   296
            case Some(last_visible) =>
wenzelm@49524
   297
              val it = commands.iterator(last_visible)
wenzelm@49524
   298
              var last = last_visible
wenzelm@49524
   299
              var i = 0
wenzelm@49524
   300
              while (i < reparse_limit && it.hasNext) {
wenzelm@49524
   301
                last = it.next
wenzelm@49524
   302
                i += last.length
wenzelm@49524
   303
              }
wenzelm@49524
   304
              reparse_spans(syntax, name, commands, first_unfinished, last)
wenzelm@48754
   305
            case None => commands
wenzelm@48754
   306
          }
wenzelm@48754
   307
        case None => commands
wenzelm@48754
   308
      }
wenzelm@48754
   309
    }
wenzelm@46946
   310
  }
wenzelm@46946
   311
wenzelm@46946
   312
wenzelm@48755
   313
  /* main */
wenzelm@46946
   314
wenzelm@50761
   315
  def diff_commands(old_cmds: Linear_Set[Command], new_cmds: Linear_Set[Command])
wenzelm@52849
   316
    : List[Command.Edit] =
wenzelm@48754
   317
  {
wenzelm@48754
   318
    val removed = old_cmds.iterator.filter(!new_cmds.contains(_)).toList
wenzelm@48754
   319
    val inserted = new_cmds.iterator.filter(!old_cmds.contains(_)).toList
wenzelm@48754
   320
wenzelm@48754
   321
    removed.reverse.map(cmd => (old_cmds.prev(cmd), None)) :::
wenzelm@48754
   322
    inserted.map(cmd => (new_cmds.prev(cmd), Some(cmd)))
wenzelm@48754
   323
  }
wenzelm@48754
   324
wenzelm@49524
   325
  private def text_edit(syntax: Outer_Syntax, reparse_limit: Int,
wenzelm@48755
   326
    node: Document.Node, edit: Document.Edit_Text): Document.Node =
wenzelm@48755
   327
  {
wenzelm@48755
   328
    edit match {
wenzelm@48755
   329
      case (_, Document.Node.Clear()) => node.clear
wenzelm@48755
   330
wenzelm@48755
   331
      case (name, Document.Node.Edits(text_edits)) =>
wenzelm@48755
   332
        val commands0 = node.commands
wenzelm@48755
   333
        val commands1 = edit_text(text_edits, commands0)
wenzelm@52849
   334
        val commands2 = recover_spans(syntax, name, node.perspective.visible, commands1)
wenzelm@48755
   335
        node.update_commands(commands2)
wenzelm@48755
   336
wenzelm@48755
   337
      case (_, Document.Node.Deps(_)) => node
wenzelm@48755
   338
wenzelm@52849
   339
      case (name, Document.Node.Perspective(required, text_perspective, overlays)) =>
wenzelm@52861
   340
        val (visible, visible_overlay) = command_perspective(node, text_perspective, overlays)
wenzelm@52849
   341
        val perspective: Document.Node.Perspective_Command =
wenzelm@52861
   342
          Document.Node.Perspective(required, visible_overlay, overlays)
wenzelm@52808
   343
        if (node.same_perspective(perspective)) node
wenzelm@48755
   344
        else
wenzelm@49524
   345
          node.update_perspective(perspective).update_commands(
wenzelm@52861
   346
            consolidate_spans(syntax, reparse_limit, name, visible, node.commands))
wenzelm@48755
   347
    }
wenzelm@48755
   348
  }
wenzelm@48755
   349
wenzelm@43722
   350
  def text_edits(
wenzelm@46942
   351
      base_syntax: Outer_Syntax,
wenzelm@49524
   352
      reparse_limit: Int,
wenzelm@43722
   353
      previous: Document.Version,
wenzelm@44157
   354
      edits: List[Document.Edit_Text])
wenzelm@44157
   355
    : (List[Document.Edit_Command], Document.Version) =
wenzelm@38374
   356
  {
wenzelm@46946
   357
    val (syntax, reparse, nodes0, doc_edits0) = header_edits(base_syntax, previous, edits)
wenzelm@46946
   358
    val reparse_set = reparse.toSet
wenzelm@46946
   359
wenzelm@46946
   360
    var nodes = nodes0
wenzelm@46946
   361
    val doc_edits = new mutable.ListBuffer[Document.Edit_Command]; doc_edits ++= doc_edits0
wenzelm@38374
   362
wenzelm@48755
   363
    val node_edits =
wenzelm@48755
   364
      (edits ::: reparse.map((_, Document.Node.Edits(Nil)))).groupBy(_._1)
wenzelm@48755
   365
        .asInstanceOf[Map[Document.Node.Name, List[Document.Edit_Text]]]  // FIXME ???
wenzelm@48754
   366
wenzelm@48755
   367
    node_edits foreach {
wenzelm@48755
   368
      case (name, edits) =>
wenzelm@48755
   369
        val node = nodes(name)
wenzelm@48755
   370
        val commands = node.commands
wenzelm@38374
   371
wenzelm@48755
   372
        val node1 =
wenzelm@48755
   373
          if (reparse_set(name) && !commands.isEmpty)
wenzelm@48755
   374
            node.update_commands(reparse_spans(syntax, name, commands, commands.head, commands.last))
wenzelm@48755
   375
          else node
wenzelm@49524
   376
        val node2 = (node1 /: edits)(text_edit(syntax, reparse_limit, _, _))
wenzelm@38374
   377
wenzelm@52808
   378
        if (!(node.same_perspective(node2.perspective)))
wenzelm@52849
   379
          doc_edits += (name -> node2.perspective)
wenzelm@48755
   380
wenzelm@48755
   381
        doc_edits += (name -> Document.Node.Edits(diff_commands(commands, node2.commands)))
wenzelm@48755
   382
wenzelm@48755
   383
        nodes += (name -> node2)
wenzelm@38374
   384
    }
wenzelm@48754
   385
wenzelm@46946
   386
    (doc_edits.toList, Document.Version.make(syntax, nodes))
wenzelm@38374
   387
  }
wenzelm@34268
   388
}