src/Pure/Thy/thy_syntax.scala
author wenzelm
Wed Dec 03 22:34:28 2014 +0100 (2014-12-03)
changeset 59086 94b2690ad494
parent 59077 7e0d3da6e6d8
child 59319 677615cba30d
permissions -rw-r--r--
node-specific keywords, with session base syntax as default;
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@44436
    16
  /** perspective **/
wenzelm@44388
    17
wenzelm@52861
    18
  def command_perspective(
wenzelm@52861
    19
      node: Document.Node,
wenzelm@52861
    20
      perspective: Text.Perspective,
wenzelm@52887
    21
      overlays: Document.Node.Overlays): (Command.Perspective, Command.Perspective) =
wenzelm@44388
    22
  {
wenzelm@52861
    23
    if (perspective.is_empty && overlays.is_empty)
wenzelm@52861
    24
      (Command.Perspective.empty, Command.Perspective.empty)
wenzelm@44388
    25
    else {
wenzelm@52861
    26
      val has_overlay = overlays.commands
wenzelm@52861
    27
      val visible = new mutable.ListBuffer[Command]
wenzelm@52861
    28
      val visible_overlay = new mutable.ListBuffer[Command]
wenzelm@44388
    29
      @tailrec
wenzelm@44388
    30
      def check_ranges(ranges: List[Text.Range], commands: Stream[(Command, Text.Offset)])
wenzelm@44388
    31
      {
wenzelm@44388
    32
        (ranges, commands) match {
wenzelm@44388
    33
          case (range :: more_ranges, (command, offset) #:: more_commands) =>
wenzelm@44388
    34
            val command_range = command.range + offset
wenzelm@44388
    35
            range compare command_range match {
wenzelm@44388
    36
              case 0 =>
wenzelm@52861
    37
                visible += command
wenzelm@52861
    38
                visible_overlay += command
wenzelm@44388
    39
                check_ranges(ranges, more_commands)
wenzelm@52861
    40
              case c =>
wenzelm@52861
    41
                if (has_overlay(command)) visible_overlay += command
wenzelm@52861
    42
wenzelm@52861
    43
                if (c < 0) check_ranges(more_ranges, commands)
wenzelm@52861
    44
                else check_ranges(ranges, more_commands)
wenzelm@44388
    45
            }
wenzelm@52861
    46
wenzelm@52861
    47
          case (Nil, (command, _) #:: more_commands) =>
wenzelm@52861
    48
            if (has_overlay(command)) visible_overlay += command
wenzelm@52861
    49
wenzelm@52861
    50
            check_ranges(Nil, more_commands)
wenzelm@52861
    51
wenzelm@44388
    52
          case _ =>
wenzelm@44388
    53
        }
wenzelm@44388
    54
      }
wenzelm@52861
    55
wenzelm@52861
    56
      val commands =
wenzelm@56373
    57
        (if (overlays.is_empty) node.command_iterator(perspective.range)
wenzelm@56373
    58
         else node.command_iterator()).toStream
wenzelm@56373
    59
      check_ranges(perspective.ranges, commands)
wenzelm@52861
    60
      (Command.Perspective(visible.toList), Command.Perspective(visible_overlay.toList))
wenzelm@44388
    61
    }
wenzelm@44388
    62
  }
wenzelm@44388
    63
wenzelm@44388
    64
wenzelm@44388
    65
wenzelm@46946
    66
  /** header edits: structure and outer syntax **/
wenzelm@46946
    67
wenzelm@46946
    68
  private def header_edits(
wenzelm@56394
    69
    resources: Resources,
wenzelm@46946
    70
    previous: Document.Version,
wenzelm@55134
    71
    edits: List[Document.Edit_Text]):
wenzelm@59077
    72
    (List[Document.Node.Name], Document.Nodes, List[Document.Edit_Command]) =
wenzelm@46946
    73
  {
wenzelm@59077
    74
    val syntax_changed0 = new mutable.ListBuffer[Document.Node.Name]
wenzelm@46946
    75
    var nodes = previous.nodes
wenzelm@46946
    76
    val doc_edits = new mutable.ListBuffer[Document.Edit_Command]
wenzelm@46946
    77
wenzelm@46946
    78
    edits foreach {
wenzelm@48707
    79
      case (name, Document.Node.Deps(header)) =>
wenzelm@46946
    80
        val node = nodes(name)
wenzelm@46946
    81
        val update_header =
wenzelm@48707
    82
          !node.header.errors.isEmpty || !header.errors.isEmpty || node.header != header
wenzelm@46946
    83
        if (update_header) {
wenzelm@46946
    84
          val node1 = node.update_header(header)
wenzelm@59077
    85
          if (node.header.imports != node1.header.imports ||
wenzelm@59077
    86
              node.header.keywords != node1.header.keywords) syntax_changed0 += name
wenzelm@46946
    87
          nodes += (name -> node1)
wenzelm@48707
    88
          doc_edits += (name -> Document.Node.Deps(header))
wenzelm@46946
    89
        }
wenzelm@46946
    90
      case _ =>
wenzelm@46946
    91
    }
wenzelm@46946
    92
wenzelm@59077
    93
    val syntax_changed = nodes.descendants(syntax_changed0.toList)
wenzelm@46946
    94
wenzelm@59077
    95
    for (name <- syntax_changed) {
wenzelm@59077
    96
      val node = nodes(name)
wenzelm@59077
    97
      val syntax =
wenzelm@59077
    98
        if (node.is_empty) None
wenzelm@59077
    99
        else {
wenzelm@59086
   100
          val header = node.header
wenzelm@59086
   101
          val imports_syntax = header.imports.flatMap(a => nodes(a).syntax)
wenzelm@59086
   102
          Some((resources.base_syntax /: imports_syntax)(_ ++ _).add_keywords(header.keywords))
wenzelm@59077
   103
        }
wenzelm@59077
   104
      nodes += (name -> node.update_syntax(syntax))
wenzelm@59077
   105
    }
wenzelm@46946
   106
wenzelm@59077
   107
    (syntax_changed, nodes, doc_edits.toList)
wenzelm@46946
   108
  }
wenzelm@46946
   109
wenzelm@46946
   110
wenzelm@46946
   111
wenzelm@38374
   112
  /** text edits **/
wenzelm@38374
   113
wenzelm@48755
   114
  /* edit individual command source */
wenzelm@46946
   115
wenzelm@50761
   116
  @tailrec def edit_text(eds: List[Text.Edit], commands: Linear_Set[Command]): Linear_Set[Command] =
wenzelm@46946
   117
  {
wenzelm@46946
   118
    eds match {
wenzelm@46946
   119
      case e :: es =>
wenzelm@52901
   120
        Document.Node.Commands.starts(commands.iterator).find {
wenzelm@46946
   121
          case (cmd, cmd_start) =>
wenzelm@46946
   122
            e.can_edit(cmd.source, cmd_start) ||
wenzelm@46946
   123
              e.is_insert && e.start == cmd_start + cmd.length
wenzelm@46946
   124
        } match {
wenzelm@46946
   125
          case Some((cmd, cmd_start)) if e.can_edit(cmd.source, cmd_start) =>
wenzelm@46946
   126
            val (rest, text) = e.edit(cmd.source, cmd_start)
wenzelm@46946
   127
            val new_commands = commands.insert_after(Some(cmd), Command.unparsed(text)) - cmd
wenzelm@46946
   128
            edit_text(rest.toList ::: es, new_commands)
wenzelm@46946
   129
wenzelm@46946
   130
          case Some((cmd, cmd_start)) =>
wenzelm@46946
   131
            edit_text(es, commands.insert_after(Some(cmd), Command.unparsed(e.text)))
wenzelm@46946
   132
wenzelm@46946
   133
          case None =>
wenzelm@46946
   134
            require(e.is_insert && e.start == 0)
wenzelm@46946
   135
            edit_text(es, commands.insert_after(None, Command.unparsed(e.text)))
wenzelm@46946
   136
        }
wenzelm@46946
   137
      case Nil => commands
wenzelm@46946
   138
    }
wenzelm@46946
   139
  }
wenzelm@46946
   140
wenzelm@46946
   141
wenzelm@48755
   142
  /* reparse range of command spans */
wenzelm@46946
   143
wenzelm@48748
   144
  @tailrec private def chop_common(
wenzelm@55779
   145
      cmds: List[Command],
wenzelm@57905
   146
      blobs_spans: List[(List[Command.Blob], Command_Span.Span)])
wenzelm@57905
   147
    : (List[Command], List[(List[Command.Blob], Command_Span.Span)]) =
wenzelm@55779
   148
  {
wenzelm@55779
   149
    (cmds, blobs_spans) match {
wenzelm@55779
   150
      case (cmd :: cmds, (blobs, span) :: rest) if cmd.blobs == blobs && cmd.span == span =>
wenzelm@55779
   151
        chop_common(cmds, rest)
wenzelm@55779
   152
      case _ => (cmds, blobs_spans)
wenzelm@48748
   153
    }
wenzelm@55779
   154
  }
wenzelm@48748
   155
wenzelm@48754
   156
  private def reparse_spans(
wenzelm@56208
   157
    resources: Resources,
wenzelm@56393
   158
    syntax: Prover.Syntax,
wenzelm@56336
   159
    get_blob: Document.Node.Name => Option[Document.Blob],
wenzelm@48754
   160
    name: Document.Node.Name,
wenzelm@48754
   161
    commands: Linear_Set[Command],
wenzelm@48754
   162
    first: Command, last: Command): Linear_Set[Command] =
wenzelm@48748
   163
  {
wenzelm@48754
   164
    val cmds0 = commands.iterator(first, last).toList
wenzelm@55779
   165
    val blobs_spans0 =
wenzelm@57906
   166
      syntax.parse_spans(cmds0.iterator.map(_.source).mkString).
wenzelm@57905
   167
        map(span => (Command_Span.resolve_files(resources, syntax, name, span, get_blob), span))
wenzelm@48754
   168
wenzelm@55779
   169
    val (cmds1, blobs_spans1) = chop_common(cmds0, blobs_spans0)
wenzelm@48754
   170
wenzelm@55779
   171
    val (rev_cmds2, rev_blobs_spans2) = chop_common(cmds1.reverse, blobs_spans1.reverse)
wenzelm@48754
   172
    val cmds2 = rev_cmds2.reverse
wenzelm@55779
   173
    val blobs_spans2 = rev_blobs_spans2.reverse
wenzelm@48754
   174
wenzelm@48754
   175
    cmds2 match {
wenzelm@48754
   176
      case Nil =>
wenzelm@55779
   177
        assert(blobs_spans2.isEmpty)
wenzelm@48754
   178
        commands
wenzelm@48754
   179
      case cmd :: _ =>
wenzelm@48754
   180
        val hook = commands.prev(cmd)
wenzelm@54462
   181
        val inserted =
wenzelm@55779
   182
          blobs_spans2.map({ case (blobs, span) => Command(Document_ID.make(), name, blobs, span) })
wenzelm@48754
   183
        (commands /: cmds2)(_ - _).append_after(hook, inserted)
wenzelm@48754
   184
    }
wenzelm@48748
   185
  }
wenzelm@48748
   186
wenzelm@48754
   187
wenzelm@48755
   188
  /* recover command spans after edits */
wenzelm@48754
   189
wenzelm@48755
   190
  // FIXME somewhat slow
wenzelm@48746
   191
  private def recover_spans(
wenzelm@56208
   192
    resources: Resources,
wenzelm@56393
   193
    syntax: Prover.Syntax,
wenzelm@56336
   194
    get_blob: Document.Node.Name => Option[Document.Blob],
wenzelm@48754
   195
    name: Document.Node.Name,
wenzelm@48748
   196
    perspective: Command.Perspective,
wenzelm@48754
   197
    commands: Linear_Set[Command]): Linear_Set[Command] =
wenzelm@46946
   198
  {
wenzelm@48754
   199
    val visible = perspective.commands.toSet
wenzelm@48748
   200
wenzelm@48754
   201
    def next_invisible_command(cmds: Linear_Set[Command], from: Command): Command =
wenzelm@57904
   202
      cmds.iterator(from).dropWhile(cmd => !cmd.is_proper || visible(cmd))
wenzelm@57904
   203
        .find(_.is_proper) getOrElse cmds.last
wenzelm@48746
   204
wenzelm@48754
   205
    @tailrec def recover(cmds: Linear_Set[Command]): Linear_Set[Command] =
wenzelm@48754
   206
      cmds.find(_.is_unparsed) match {
wenzelm@48754
   207
        case Some(first_unparsed) =>
wenzelm@48754
   208
          val first = next_invisible_command(cmds.reverse, first_unparsed)
wenzelm@48754
   209
          val last = next_invisible_command(cmds, first_unparsed)
wenzelm@56336
   210
          recover(reparse_spans(resources, syntax, get_blob, name, cmds, first, last))
wenzelm@48754
   211
        case None => cmds
wenzelm@48746
   212
      }
wenzelm@48754
   213
    recover(commands)
wenzelm@46946
   214
  }
wenzelm@46946
   215
wenzelm@46946
   216
wenzelm@48755
   217
  /* consolidate unfinished spans */
wenzelm@46946
   218
wenzelm@48754
   219
  private def consolidate_spans(
wenzelm@56208
   220
    resources: Resources,
wenzelm@56393
   221
    syntax: Prover.Syntax,
wenzelm@56336
   222
    get_blob: Document.Node.Name => Option[Document.Blob],
wenzelm@49524
   223
    reparse_limit: Int,
wenzelm@48754
   224
    name: Document.Node.Name,
wenzelm@48754
   225
    perspective: Command.Perspective,
wenzelm@46946
   226
    commands: Linear_Set[Command]): Linear_Set[Command] =
wenzelm@46946
   227
  {
wenzelm@48754
   228
    if (perspective.commands.isEmpty) commands
wenzelm@48754
   229
    else {
wenzelm@48754
   230
      commands.find(_.is_unfinished) match {
wenzelm@48754
   231
        case Some(first_unfinished) =>
wenzelm@48754
   232
          val visible = perspective.commands.toSet
wenzelm@48754
   233
          commands.reverse.find(visible) match {
wenzelm@48754
   234
            case Some(last_visible) =>
wenzelm@49524
   235
              val it = commands.iterator(last_visible)
wenzelm@49524
   236
              var last = last_visible
wenzelm@49524
   237
              var i = 0
wenzelm@49524
   238
              while (i < reparse_limit && it.hasNext) {
wenzelm@49524
   239
                last = it.next
wenzelm@49524
   240
                i += last.length
wenzelm@49524
   241
              }
wenzelm@56336
   242
              reparse_spans(resources, syntax, get_blob, name, commands, first_unfinished, last)
wenzelm@48754
   243
            case None => commands
wenzelm@48754
   244
          }
wenzelm@48754
   245
        case None => commands
wenzelm@48754
   246
      }
wenzelm@48754
   247
    }
wenzelm@46946
   248
  }
wenzelm@46946
   249
wenzelm@46946
   250
wenzelm@48755
   251
  /* main */
wenzelm@46946
   252
wenzelm@50761
   253
  def diff_commands(old_cmds: Linear_Set[Command], new_cmds: Linear_Set[Command])
wenzelm@52849
   254
    : List[Command.Edit] =
wenzelm@48754
   255
  {
wenzelm@48754
   256
    val removed = old_cmds.iterator.filter(!new_cmds.contains(_)).toList
wenzelm@48754
   257
    val inserted = new_cmds.iterator.filter(!old_cmds.contains(_)).toList
wenzelm@48754
   258
wenzelm@48754
   259
    removed.reverse.map(cmd => (old_cmds.prev(cmd), None)) :::
wenzelm@48754
   260
    inserted.map(cmd => (new_cmds.prev(cmd), Some(cmd)))
wenzelm@48754
   261
  }
wenzelm@48754
   262
wenzelm@54513
   263
  private def text_edit(
wenzelm@56208
   264
    resources: Resources,
wenzelm@56393
   265
    syntax: Prover.Syntax,
wenzelm@56336
   266
    get_blob: Document.Node.Name => Option[Document.Blob],
wenzelm@54513
   267
    reparse_limit: Int,
wenzelm@48755
   268
    node: Document.Node, edit: Document.Edit_Text): Document.Node =
wenzelm@48755
   269
  {
wenzelm@48755
   270
    edit match {
wenzelm@48755
   271
      case (_, Document.Node.Clear()) => node.clear
wenzelm@48755
   272
wenzelm@56335
   273
      case (_, Document.Node.Blob(blob)) => node.init_blob(blob)
wenzelm@54562
   274
wenzelm@48755
   275
      case (name, Document.Node.Edits(text_edits)) =>
wenzelm@56335
   276
        if (name.is_theory) {
wenzelm@55435
   277
          val commands0 = node.commands
wenzelm@55435
   278
          val commands1 = edit_text(text_edits, commands0)
wenzelm@55435
   279
          val commands2 =
wenzelm@56336
   280
            recover_spans(resources, syntax, get_blob, name, node.perspective.visible, commands1)
wenzelm@55435
   281
          node.update_commands(commands2)
wenzelm@55435
   282
        }
wenzelm@56335
   283
        else node
wenzelm@48755
   284
wenzelm@48755
   285
      case (_, Document.Node.Deps(_)) => node
wenzelm@48755
   286
wenzelm@52849
   287
      case (name, Document.Node.Perspective(required, text_perspective, overlays)) =>
wenzelm@52861
   288
        val (visible, visible_overlay) = command_perspective(node, text_perspective, overlays)
wenzelm@52849
   289
        val perspective: Document.Node.Perspective_Command =
wenzelm@52861
   290
          Document.Node.Perspective(required, visible_overlay, overlays)
wenzelm@52808
   291
        if (node.same_perspective(perspective)) node
wenzelm@48755
   292
        else
wenzelm@49524
   293
          node.update_perspective(perspective).update_commands(
wenzelm@56336
   294
            consolidate_spans(resources, syntax, get_blob, reparse_limit,
wenzelm@54517
   295
              name, visible, node.commands))
wenzelm@48755
   296
    }
wenzelm@48755
   297
  }
wenzelm@48755
   298
wenzelm@56316
   299
  def parse_change(
wenzelm@56208
   300
      resources: Resources,
wenzelm@49524
   301
      reparse_limit: Int,
wenzelm@43722
   302
      previous: Document.Version,
wenzelm@54521
   303
      doc_blobs: Document.Blobs,
wenzelm@56315
   304
      edits: List[Document.Edit_Text]): Session.Change =
wenzelm@38374
   305
  {
wenzelm@56336
   306
    def get_blob(name: Document.Node.Name) =
wenzelm@56336
   307
      doc_blobs.get(name) orElse previous.nodes(name).get_blob
wenzelm@56336
   308
wenzelm@59077
   309
    val (syntax_changed, nodes0, doc_edits0) = header_edits(resources, previous, edits)
wenzelm@54518
   310
wenzelm@56316
   311
    val (doc_edits, version) =
wenzelm@59077
   312
      if (edits.isEmpty) (Nil, Document.Version.make(previous.nodes))
wenzelm@56316
   313
      else {
wenzelm@56316
   314
        val reparse =
wenzelm@59077
   315
          (syntax_changed /: nodes0.iterator)({
wenzelm@56316
   316
            case (reparse, (name, node)) =>
wenzelm@56316
   317
              if (node.load_commands.exists(_.blobs_changed(doc_blobs)))
wenzelm@56316
   318
                name :: reparse
wenzelm@56316
   319
              else reparse
wenzelm@56316
   320
            })
wenzelm@56316
   321
        val reparse_set = reparse.toSet
wenzelm@46946
   322
wenzelm@56316
   323
        var nodes = nodes0
wenzelm@56316
   324
        val doc_edits = new mutable.ListBuffer[Document.Edit_Command]; doc_edits ++= doc_edits0
wenzelm@54521
   325
wenzelm@56316
   326
        val node_edits =
wenzelm@56316
   327
          (edits ::: reparse.map((_, Document.Node.Edits(Nil)))).groupBy(_._1)
wenzelm@56316
   328
            .asInstanceOf[Map[Document.Node.Name, List[Document.Edit_Text]]]  // FIXME ???
wenzelm@38374
   329
wenzelm@56316
   330
        node_edits foreach {
wenzelm@56316
   331
          case (name, edits) =>
wenzelm@56316
   332
            val node = nodes(name)
wenzelm@59077
   333
            val syntax = node.syntax getOrElse resources.base_syntax
wenzelm@56316
   334
            val commands = node.commands
wenzelm@48754
   335
wenzelm@56316
   336
            val node1 =
wenzelm@56316
   337
              if (reparse_set(name) && !commands.isEmpty)
wenzelm@56316
   338
                node.update_commands(
wenzelm@56336
   339
                  reparse_spans(resources, syntax, get_blob,
wenzelm@56316
   340
                    name, commands, commands.head, commands.last))
wenzelm@56316
   341
              else node
wenzelm@56316
   342
            val node2 =
wenzelm@56336
   343
              (node1 /: edits)(text_edit(resources, syntax, get_blob, reparse_limit, _, _))
wenzelm@54521
   344
wenzelm@56316
   345
            if (!(node.same_perspective(node2.perspective)))
wenzelm@56316
   346
              doc_edits += (name -> node2.perspective)
wenzelm@54521
   347
wenzelm@56316
   348
            doc_edits += (name -> Document.Node.Edits(diff_commands(commands, node2.commands)))
wenzelm@54521
   349
wenzelm@56316
   350
            nodes += (name -> node2)
wenzelm@56316
   351
        }
wenzelm@59077
   352
        (doc_edits.toList.filterNot(_._2.is_void), Document.Version.make(nodes))
wenzelm@54513
   353
      }
wenzelm@54513
   354
wenzelm@59077
   355
    Session.Change(previous, syntax_changed, !syntax_changed.isEmpty, doc_edits, version)
wenzelm@38374
   356
  }
wenzelm@34268
   357
}