src/Pure/Thy/thy_syntax.scala
author wenzelm
Fri Aug 10 13:33:07 2012 +0200 (2012-08-10)
changeset 48754 c2c1e5944536
parent 48748 89b4e7d83d6f
child 48755 393a37003851
permissions -rw-r--r--
clarified undefined, unparsed, unfinished command spans;
common reparse_spans, diff_commands;
some support for consolidate_spans after change of perspective;
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@47012
    71
      spans.foreach(span => add(Command(Document.no_id, 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@48748
    80
  def parse_spans(toks: List[Token]): List[Command.Span] =
wenzelm@34268
    81
  {
wenzelm@48748
    82
    val result = new mutable.ListBuffer[Command.Span]
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@44388
   121
wenzelm@44388
   122
wenzelm@46946
   123
  /** header edits: structure and outer syntax **/
wenzelm@46946
   124
wenzelm@46946
   125
  private def header_edits(
wenzelm@46946
   126
    base_syntax: Outer_Syntax,
wenzelm@46946
   127
    previous: Document.Version,
wenzelm@46946
   128
    edits: List[Document.Edit_Text])
wenzelm@46946
   129
    : (Outer_Syntax, List[Document.Node.Name], Document.Nodes, List[Document.Edit_Command]) =
wenzelm@46946
   130
  {
wenzelm@47987
   131
    var updated_imports = false
wenzelm@47987
   132
    var updated_keywords = false
wenzelm@46946
   133
    var nodes = previous.nodes
wenzelm@46946
   134
    val doc_edits = new mutable.ListBuffer[Document.Edit_Command]
wenzelm@46946
   135
wenzelm@46946
   136
    edits foreach {
wenzelm@48707
   137
      case (name, Document.Node.Deps(header)) =>
wenzelm@46946
   138
        val node = nodes(name)
wenzelm@46946
   139
        val update_header =
wenzelm@48707
   140
          !node.header.errors.isEmpty || !header.errors.isEmpty || node.header != header
wenzelm@46946
   141
        if (update_header) {
wenzelm@46946
   142
          val node1 = node.update_header(header)
wenzelm@48707
   143
          updated_imports = updated_imports || (node.header.imports != node1.header.imports)
wenzelm@48707
   144
          updated_keywords = updated_keywords || (node.header.keywords != node1.header.keywords)
wenzelm@46946
   145
          nodes += (name -> node1)
wenzelm@48707
   146
          doc_edits += (name -> Document.Node.Deps(header))
wenzelm@46946
   147
        }
wenzelm@46946
   148
      case _ =>
wenzelm@46946
   149
    }
wenzelm@46946
   150
wenzelm@46946
   151
    val syntax =
wenzelm@47987
   152
      if (previous.is_init || updated_keywords)
wenzelm@48706
   153
        (base_syntax /: nodes.entries) { case (syn, (_, node)) => syn.add_keywords(node.header) }
wenzelm@46946
   154
      else previous.syntax
wenzelm@46946
   155
wenzelm@46946
   156
    val reparse =
wenzelm@47987
   157
      if (updated_imports || updated_keywords)
wenzelm@47987
   158
        nodes.descendants(doc_edits.iterator.map(_._1).toList)
wenzelm@46946
   159
      else Nil
wenzelm@46946
   160
wenzelm@46946
   161
    (syntax, reparse, nodes, doc_edits.toList)
wenzelm@46946
   162
  }
wenzelm@46946
   163
wenzelm@46946
   164
wenzelm@46946
   165
wenzelm@38374
   166
  /** text edits **/
wenzelm@38374
   167
wenzelm@46946
   168
  /* phase 1: edit individual command source */
wenzelm@46946
   169
wenzelm@46946
   170
  @tailrec private def edit_text(eds: List[Text.Edit], commands: Linear_Set[Command])
wenzelm@46946
   171
      : Linear_Set[Command] =
wenzelm@46946
   172
  {
wenzelm@46946
   173
    eds match {
wenzelm@46946
   174
      case e :: es =>
wenzelm@46946
   175
        Document.Node.command_starts(commands.iterator).find {
wenzelm@46946
   176
          case (cmd, cmd_start) =>
wenzelm@46946
   177
            e.can_edit(cmd.source, cmd_start) ||
wenzelm@46946
   178
              e.is_insert && e.start == cmd_start + cmd.length
wenzelm@46946
   179
        } match {
wenzelm@46946
   180
          case Some((cmd, cmd_start)) if e.can_edit(cmd.source, cmd_start) =>
wenzelm@46946
   181
            val (rest, text) = e.edit(cmd.source, cmd_start)
wenzelm@46946
   182
            val new_commands = commands.insert_after(Some(cmd), Command.unparsed(text)) - cmd
wenzelm@46946
   183
            edit_text(rest.toList ::: es, new_commands)
wenzelm@46946
   184
wenzelm@46946
   185
          case Some((cmd, cmd_start)) =>
wenzelm@46946
   186
            edit_text(es, commands.insert_after(Some(cmd), Command.unparsed(e.text)))
wenzelm@46946
   187
wenzelm@46946
   188
          case None =>
wenzelm@46946
   189
            require(e.is_insert && e.start == 0)
wenzelm@46946
   190
            edit_text(es, commands.insert_after(None, Command.unparsed(e.text)))
wenzelm@46946
   191
        }
wenzelm@46946
   192
      case Nil => commands
wenzelm@46946
   193
    }
wenzelm@46946
   194
  }
wenzelm@46946
   195
wenzelm@46946
   196
wenzelm@48754
   197
  /* phase 2a: reparse range of command spans */
wenzelm@46946
   198
wenzelm@48748
   199
  @tailrec private def chop_common(
wenzelm@48748
   200
      cmds: List[Command], spans: List[Command.Span]): (List[Command], List[Command.Span]) =
wenzelm@48748
   201
    (cmds, spans) match {
wenzelm@48748
   202
      case (c :: cs, s :: ss) if c.span == s => chop_common(cs, ss)
wenzelm@48748
   203
      case _ => (cmds, spans)
wenzelm@48748
   204
    }
wenzelm@48748
   205
wenzelm@48754
   206
  private def reparse_spans(
wenzelm@48754
   207
    syntax: Outer_Syntax,
wenzelm@48754
   208
    name: Document.Node.Name,
wenzelm@48754
   209
    commands: Linear_Set[Command],
wenzelm@48754
   210
    first: Command, last: Command): Linear_Set[Command] =
wenzelm@48748
   211
  {
wenzelm@48754
   212
    val cmds0 = commands.iterator(first, last).toList
wenzelm@48754
   213
    val spans0 = parse_spans(syntax.scan(cmds0.iterator.map(_.source).mkString))
wenzelm@48754
   214
wenzelm@48754
   215
    val (cmds1, spans1) = chop_common(cmds0, spans0)
wenzelm@48754
   216
wenzelm@48748
   217
    val (rev_cmds2, rev_spans2) = chop_common(cmds1.reverse, spans1.reverse)
wenzelm@48754
   218
    val cmds2 = rev_cmds2.reverse
wenzelm@48754
   219
    val spans2 = rev_spans2.reverse
wenzelm@48754
   220
wenzelm@48754
   221
    cmds2 match {
wenzelm@48754
   222
      case Nil =>
wenzelm@48754
   223
        assert(spans2.isEmpty)
wenzelm@48754
   224
        commands
wenzelm@48754
   225
      case cmd :: _ =>
wenzelm@48754
   226
        val hook = commands.prev(cmd)
wenzelm@48754
   227
        val inserted = spans2.map(span => Command(Document.new_id(), name, span))
wenzelm@48754
   228
        (commands /: cmds2)(_ - _).append_after(hook, inserted)
wenzelm@48754
   229
    }
wenzelm@48748
   230
  }
wenzelm@48748
   231
wenzelm@48754
   232
wenzelm@48754
   233
  /* phase 2b: recover command spans after edits */
wenzelm@48754
   234
wenzelm@48746
   235
  private def recover_spans(
wenzelm@46946
   236
    syntax: Outer_Syntax,
wenzelm@48754
   237
    name: Document.Node.Name,
wenzelm@48748
   238
    perspective: Command.Perspective,
wenzelm@48754
   239
    commands: Linear_Set[Command]): Linear_Set[Command] =
wenzelm@46946
   240
  {
wenzelm@48754
   241
    val visible = perspective.commands.toSet
wenzelm@48748
   242
wenzelm@48754
   243
    def next_invisible_command(cmds: Linear_Set[Command], from: Command): Command =
wenzelm@48754
   244
      cmds.iterator(from).dropWhile(cmd => !cmd.is_command || visible(cmd))
wenzelm@48754
   245
        .find(_.is_command) getOrElse cmds.last
wenzelm@48746
   246
wenzelm@48754
   247
    @tailrec def recover(cmds: Linear_Set[Command]): Linear_Set[Command] =
wenzelm@48754
   248
      cmds.find(_.is_unparsed) match {
wenzelm@48754
   249
        case Some(first_unparsed) =>
wenzelm@48754
   250
          val first = next_invisible_command(cmds.reverse, first_unparsed)
wenzelm@48754
   251
          val last = next_invisible_command(cmds, first_unparsed)
wenzelm@48754
   252
          recover(reparse_spans(syntax, name, cmds, first, last))
wenzelm@48754
   253
        case None => cmds
wenzelm@48746
   254
      }
wenzelm@48754
   255
    recover(commands)
wenzelm@46946
   256
  }
wenzelm@46946
   257
wenzelm@46946
   258
wenzelm@48754
   259
  /* phase 2c: consolidate unfinished spans */
wenzelm@46946
   260
wenzelm@48754
   261
  private def consolidate_spans(
wenzelm@46946
   262
    syntax: Outer_Syntax,
wenzelm@48754
   263
    name: Document.Node.Name,
wenzelm@48754
   264
    perspective: Command.Perspective,
wenzelm@46946
   265
    commands: Linear_Set[Command]): Linear_Set[Command] =
wenzelm@46946
   266
  {
wenzelm@48754
   267
    if (perspective.commands.isEmpty) commands
wenzelm@48754
   268
    else {
wenzelm@48754
   269
      commands.find(_.is_unfinished) match {
wenzelm@48754
   270
        case Some(first_unfinished) =>
wenzelm@48754
   271
          val visible = perspective.commands.toSet
wenzelm@48754
   272
          commands.reverse.find(visible) match {
wenzelm@48754
   273
            case Some(last_visible) =>
wenzelm@48754
   274
              reparse_spans(syntax, name, commands, first_unfinished, last_visible)
wenzelm@48754
   275
            case None => commands
wenzelm@48754
   276
          }
wenzelm@48754
   277
        case None => commands
wenzelm@48754
   278
      }
wenzelm@48754
   279
    }
wenzelm@46946
   280
  }
wenzelm@46946
   281
wenzelm@46946
   282
wenzelm@46946
   283
  /* main phase */
wenzelm@46946
   284
wenzelm@48754
   285
  private def diff_commands(old_cmds: Linear_Set[Command], new_cmds: Linear_Set[Command])
wenzelm@48754
   286
    : List[(Option[Command], Option[Command])] =
wenzelm@48754
   287
  {
wenzelm@48754
   288
    val removed = old_cmds.iterator.filter(!new_cmds.contains(_)).toList
wenzelm@48754
   289
    val inserted = new_cmds.iterator.filter(!old_cmds.contains(_)).toList
wenzelm@48754
   290
wenzelm@48754
   291
    removed.reverse.map(cmd => (old_cmds.prev(cmd), None)) :::
wenzelm@48754
   292
    inserted.map(cmd => (new_cmds.prev(cmd), Some(cmd)))
wenzelm@48754
   293
  }
wenzelm@48754
   294
wenzelm@43722
   295
  def text_edits(
wenzelm@46942
   296
      base_syntax: Outer_Syntax,
wenzelm@43722
   297
      previous: Document.Version,
wenzelm@44157
   298
      edits: List[Document.Edit_Text])
wenzelm@44157
   299
    : (List[Document.Edit_Command], Document.Version) =
wenzelm@38374
   300
  {
wenzelm@46946
   301
    val (syntax, reparse, nodes0, doc_edits0) = header_edits(base_syntax, previous, edits)
wenzelm@46946
   302
    val reparse_set = reparse.toSet
wenzelm@46946
   303
wenzelm@46946
   304
    var nodes = nodes0
wenzelm@46946
   305
    val doc_edits = new mutable.ListBuffer[Document.Edit_Command]; doc_edits ++= doc_edits0
wenzelm@38374
   306
wenzelm@46946
   307
    (edits ::: reparse.map((_, Document.Node.Edits(Nil)))) foreach {
wenzelm@46946
   308
      case (name, Document.Node.Clear()) =>
wenzelm@46946
   309
        doc_edits += (name -> Document.Node.Clear())
wenzelm@46946
   310
        nodes += (name -> nodes(name).clear)
wenzelm@38374
   311
wenzelm@46946
   312
      case (name, Document.Node.Edits(text_edits)) =>
wenzelm@46946
   313
        val node = nodes(name)
wenzelm@48754
   314
wenzelm@46946
   315
        val commands0 = node.commands
wenzelm@46946
   316
        val commands1 = edit_text(text_edits, commands0)
wenzelm@48748
   317
        val commands2 = recover_spans(syntax, name, node.perspective, commands1)   // FIXME somewhat slow
wenzelm@46946
   318
        val commands3 =
wenzelm@48754
   319
          if (reparse_set.contains(name) && !commands2.isEmpty)
wenzelm@48754
   320
            reparse_spans(syntax, name, commands2, commands2.head, commands2.last)  // FIXME somewhat slow
wenzelm@46946
   321
          else commands2
wenzelm@38374
   322
wenzelm@48754
   323
        doc_edits += (name -> Document.Node.Edits(diff_commands(commands0, commands3)))
wenzelm@46946
   324
        nodes += (name -> node.update_commands(commands3))
wenzelm@38374
   325
wenzelm@48707
   326
      case (name, Document.Node.Deps(_)) =>
wenzelm@38374
   327
wenzelm@46946
   328
      case (name, Document.Node.Perspective(text_perspective)) =>
wenzelm@47346
   329
        val node = nodes(name)
wenzelm@47346
   330
        val perspective = command_perspective(node, text_perspective)
wenzelm@47346
   331
        if (!(node.perspective same perspective)) {
wenzelm@48754
   332
/* FIXME
wenzelm@48754
   333
          val commands1 = consolidate_spans(syntax, name, perspective, node.commands)
wenzelm@48754
   334
          doc_edits += (name -> Document.Node.Edits(diff_commands(node.commands, commands1)))
wenzelm@48754
   335
*/
wenzelm@47346
   336
          doc_edits += (name -> Document.Node.Perspective(perspective))
wenzelm@47346
   337
          nodes += (name -> node.update_perspective(perspective))
wenzelm@46946
   338
        }
wenzelm@38374
   339
    }
wenzelm@48754
   340
wenzelm@46946
   341
    (doc_edits.toList, Document.Version.make(syntax, nodes))
wenzelm@38374
   342
  }
wenzelm@34268
   343
}