src/Pure/Thy/thy_syntax.scala
author wenzelm
Thu Nov 02 10:16:22 2017 +0100 (20 months ago)
changeset 66987 352b23c97ac8
parent 66772 a66f11a0b5b1
child 68336 09ac56914b29
permissions -rw-r--r--
support focus_session, for much faster startup of Isabelle/jEdit;
more options for "isabelle jedit";
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@59699
    66
  /** header edits: graph 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@59319
    82
          node.header.errors.nonEmpty || header.errors.nonEmpty || node.header != header
wenzelm@46946
    83
        if (update_header) {
wenzelm@46946
    84
          val node1 = node.update_header(header)
wenzelm@59695
    85
          if (node.header.imports.map(_._1) != node1.header.imports.map(_._1) ||
wenzelm@60197
    86
              node.header.keywords != node1.header.keywords ||
wenzelm@63579
    87
              node.header.abbrevs != node1.header.abbrevs ||
wenzelm@60197
    88
              node.header.errors != node1.header.errors) syntax_changed0 += name
wenzelm@46946
    89
          nodes += (name -> node1)
wenzelm@48707
    90
          doc_edits += (name -> Document.Node.Deps(header))
wenzelm@46946
    91
        }
wenzelm@46946
    92
      case _ =>
wenzelm@46946
    93
    }
wenzelm@46946
    94
wenzelm@59077
    95
    val syntax_changed = nodes.descendants(syntax_changed0.toList)
wenzelm@46946
    96
wenzelm@59077
    97
    for (name <- syntax_changed) {
wenzelm@59077
    98
      val node = nodes(name)
wenzelm@59077
    99
      val syntax =
wenzelm@59077
   100
        if (node.is_empty) None
wenzelm@59077
   101
        else {
wenzelm@59086
   102
          val header = node.header
wenzelm@66721
   103
          val imports_syntax =
wenzelm@66772
   104
            if (header.imports.nonEmpty) {
wenzelm@66772
   105
              Outer_Syntax.merge(
wenzelm@66772
   106
                header.imports.map(p => resources.session_base.node_syntax(nodes, p._1)))
wenzelm@66772
   107
            }
wenzelm@66772
   108
            else resources.session_base.overall_syntax
wenzelm@66721
   109
          Some(imports_syntax + header)
wenzelm@59077
   110
        }
wenzelm@59077
   111
      nodes += (name -> node.update_syntax(syntax))
wenzelm@59077
   112
    }
wenzelm@46946
   113
wenzelm@59077
   114
    (syntax_changed, nodes, doc_edits.toList)
wenzelm@46946
   115
  }
wenzelm@46946
   116
wenzelm@46946
   117
wenzelm@46946
   118
wenzelm@38374
   119
  /** text edits **/
wenzelm@38374
   120
wenzelm@48755
   121
  /* edit individual command source */
wenzelm@46946
   122
wenzelm@50761
   123
  @tailrec def edit_text(eds: List[Text.Edit], commands: Linear_Set[Command]): Linear_Set[Command] =
wenzelm@46946
   124
  {
wenzelm@46946
   125
    eds match {
wenzelm@46946
   126
      case e :: es =>
wenzelm@65074
   127
        def insert_text(cmd: Option[Command], text: String): Linear_Set[Command] =
wenzelm@65341
   128
          if (text == "") commands else commands.insert_after(cmd, Command.text(text))
wenzelm@65074
   129
wenzelm@52901
   130
        Document.Node.Commands.starts(commands.iterator).find {
wenzelm@46946
   131
          case (cmd, cmd_start) =>
wenzelm@46946
   132
            e.can_edit(cmd.source, cmd_start) ||
wenzelm@46946
   133
              e.is_insert && e.start == cmd_start + cmd.length
wenzelm@46946
   134
        } match {
wenzelm@46946
   135
          case Some((cmd, cmd_start)) if e.can_edit(cmd.source, cmd_start) =>
wenzelm@46946
   136
            val (rest, text) = e.edit(cmd.source, cmd_start)
wenzelm@65074
   137
            val new_commands = insert_text(Some(cmd), text) - cmd
wenzelm@46946
   138
            edit_text(rest.toList ::: es, new_commands)
wenzelm@46946
   139
wenzelm@46946
   140
          case Some((cmd, cmd_start)) =>
wenzelm@65074
   141
            edit_text(es, insert_text(Some(cmd), e.text))
wenzelm@46946
   142
wenzelm@46946
   143
          case None =>
wenzelm@46946
   144
            require(e.is_insert && e.start == 0)
wenzelm@65074
   145
            edit_text(es, insert_text(None, e.text))
wenzelm@46946
   146
        }
wenzelm@46946
   147
      case Nil => commands
wenzelm@46946
   148
    }
wenzelm@46946
   149
  }
wenzelm@46946
   150
wenzelm@46946
   151
wenzelm@48755
   152
  /* reparse range of command spans */
wenzelm@46946
   153
wenzelm@48748
   154
  @tailrec private def chop_common(
wenzelm@55779
   155
      cmds: List[Command],
wenzelm@59702
   156
      blobs_spans: List[(Command.Blobs_Info, Command_Span.Span)])
wenzelm@59702
   157
    : (List[Command], List[(Command.Blobs_Info, Command_Span.Span)]) =
wenzelm@55779
   158
  {
wenzelm@55779
   159
    (cmds, blobs_spans) match {
wenzelm@59803
   160
      case (cmd :: cmds, (blobs_info, span) :: rest)
wenzelm@59803
   161
      if cmd.blobs_info == blobs_info && cmd.span == span => chop_common(cmds, rest)
wenzelm@55779
   162
      case _ => (cmds, blobs_spans)
wenzelm@48748
   163
    }
wenzelm@55779
   164
  }
wenzelm@48748
   165
wenzelm@48754
   166
  private def reparse_spans(
wenzelm@56208
   167
    resources: Resources,
wenzelm@63584
   168
    syntax: Outer_Syntax,
wenzelm@56336
   169
    get_blob: Document.Node.Name => Option[Document.Blob],
wenzelm@59702
   170
    can_import: Document.Node.Name => Boolean,
wenzelm@59699
   171
    node_name: Document.Node.Name,
wenzelm@48754
   172
    commands: Linear_Set[Command],
wenzelm@48754
   173
    first: Command, last: Command): Linear_Set[Command] =
wenzelm@48748
   174
  {
wenzelm@48754
   175
    val cmds0 = commands.iterator(first, last).toList
wenzelm@55779
   176
    val blobs_spans0 =
wenzelm@59702
   177
      syntax.parse_spans(cmds0.iterator.map(_.source).mkString).map(span =>
wenzelm@59705
   178
        (Command.blobs_info(resources, syntax, get_blob, can_import, node_name, span), span))
wenzelm@48754
   179
wenzelm@55779
   180
    val (cmds1, blobs_spans1) = chop_common(cmds0, blobs_spans0)
wenzelm@48754
   181
wenzelm@55779
   182
    val (rev_cmds2, rev_blobs_spans2) = chop_common(cmds1.reverse, blobs_spans1.reverse)
wenzelm@48754
   183
    val cmds2 = rev_cmds2.reverse
wenzelm@55779
   184
    val blobs_spans2 = rev_blobs_spans2.reverse
wenzelm@48754
   185
wenzelm@48754
   186
    cmds2 match {
wenzelm@48754
   187
      case Nil =>
wenzelm@55779
   188
        assert(blobs_spans2.isEmpty)
wenzelm@48754
   189
        commands
wenzelm@48754
   190
      case cmd :: _ =>
wenzelm@48754
   191
        val hook = commands.prev(cmd)
wenzelm@54462
   192
        val inserted =
wenzelm@59684
   193
          blobs_spans2.map({ case (blobs, span) =>
wenzelm@59702
   194
            Command(Document_ID.make(), node_name, blobs, span) })
wenzelm@48754
   195
        (commands /: cmds2)(_ - _).append_after(hook, inserted)
wenzelm@48754
   196
    }
wenzelm@48748
   197
  }
wenzelm@48748
   198
wenzelm@48754
   199
wenzelm@48755
   200
  /* main */
wenzelm@46946
   201
wenzelm@50761
   202
  def diff_commands(old_cmds: Linear_Set[Command], new_cmds: Linear_Set[Command])
wenzelm@52849
   203
    : List[Command.Edit] =
wenzelm@48754
   204
  {
wenzelm@48754
   205
    val removed = old_cmds.iterator.filter(!new_cmds.contains(_)).toList
wenzelm@48754
   206
    val inserted = new_cmds.iterator.filter(!old_cmds.contains(_)).toList
wenzelm@48754
   207
wenzelm@63866
   208
    removed.map(cmd => (old_cmds.prev(cmd), None)) reverse_:::
wenzelm@48754
   209
    inserted.map(cmd => (new_cmds.prev(cmd), Some(cmd)))
wenzelm@48754
   210
  }
wenzelm@48754
   211
wenzelm@54513
   212
  private def text_edit(
wenzelm@56208
   213
    resources: Resources,
wenzelm@63584
   214
    syntax: Outer_Syntax,
wenzelm@56336
   215
    get_blob: Document.Node.Name => Option[Document.Blob],
wenzelm@59702
   216
    can_import: Document.Node.Name => Boolean,
wenzelm@54513
   217
    reparse_limit: Int,
wenzelm@48755
   218
    node: Document.Node, edit: Document.Edit_Text): Document.Node =
wenzelm@48755
   219
  {
wenzelm@59704
   220
    /* recover command spans after edits */
wenzelm@59704
   221
    // FIXME somewhat slow
wenzelm@59704
   222
    def recover_spans(
wenzelm@59704
   223
      name: Document.Node.Name,
wenzelm@59704
   224
      perspective: Command.Perspective,
wenzelm@59704
   225
      commands: Linear_Set[Command]): Linear_Set[Command] =
wenzelm@59704
   226
    {
wenzelm@59704
   227
      val is_visible = perspective.commands.toSet
wenzelm@59704
   228
wenzelm@59704
   229
      def next_invisible(cmds: Linear_Set[Command], from: Command): Command =
wenzelm@59704
   230
        cmds.iterator(from).dropWhile(cmd => !cmd.is_proper || is_visible(cmd))
wenzelm@59704
   231
          .find(_.is_proper) getOrElse cmds.last
wenzelm@59704
   232
wenzelm@59704
   233
      @tailrec def recover(cmds: Linear_Set[Command]): Linear_Set[Command] =
wenzelm@59704
   234
        cmds.find(_.is_unparsed) match {
wenzelm@59704
   235
          case Some(first_unparsed) =>
wenzelm@59704
   236
            val first = next_invisible(cmds.reverse, first_unparsed)
wenzelm@59704
   237
            val last = next_invisible(cmds, first_unparsed)
wenzelm@59704
   238
            recover(
wenzelm@59705
   239
              reparse_spans(resources, syntax, get_blob, can_import, name, cmds, first, last))
wenzelm@59704
   240
          case None => cmds
wenzelm@59704
   241
        }
wenzelm@59704
   242
      recover(commands)
wenzelm@59704
   243
    }
wenzelm@59704
   244
wenzelm@48755
   245
    edit match {
wenzelm@56335
   246
      case (_, Document.Node.Blob(blob)) => node.init_blob(blob)
wenzelm@54562
   247
wenzelm@48755
   248
      case (name, Document.Node.Edits(text_edits)) =>
wenzelm@56335
   249
        if (name.is_theory) {
wenzelm@55435
   250
          val commands0 = node.commands
wenzelm@55435
   251
          val commands1 = edit_text(text_edits, commands0)
wenzelm@59705
   252
          val commands2 = recover_spans(name, node.perspective.visible, commands1)
wenzelm@55435
   253
          node.update_commands(commands2)
wenzelm@55435
   254
        }
wenzelm@56335
   255
        else node
wenzelm@48755
   256
wenzelm@48755
   257
      case (_, Document.Node.Deps(_)) => node
wenzelm@48755
   258
wenzelm@52849
   259
      case (name, Document.Node.Perspective(required, text_perspective, overlays)) =>
wenzelm@52861
   260
        val (visible, visible_overlay) = command_perspective(node, text_perspective, overlays)
wenzelm@52849
   261
        val perspective: Document.Node.Perspective_Command =
wenzelm@52861
   262
          Document.Node.Perspective(required, visible_overlay, overlays)
wenzelm@59372
   263
        if (node.same_perspective(text_perspective, perspective)) node
wenzelm@59703
   264
        else {
wenzelm@59703
   265
          /* consolidate unfinished spans */
wenzelm@59703
   266
          val is_visible = visible.commands.toSet
wenzelm@59703
   267
          val commands = node.commands
wenzelm@59703
   268
          val commands1 =
wenzelm@59703
   269
            if (is_visible.isEmpty) commands
wenzelm@59703
   270
            else {
wenzelm@59703
   271
              commands.find(_.is_unfinished) match {
wenzelm@59703
   272
                case Some(first_unfinished) =>
wenzelm@59703
   273
                  commands.reverse.find(is_visible) match {
wenzelm@59703
   274
                    case Some(last_visible) =>
wenzelm@59703
   275
                      val it = commands.iterator(last_visible)
wenzelm@59703
   276
                      var last = last_visible
wenzelm@59703
   277
                      var i = 0
wenzelm@59703
   278
                      while (i < reparse_limit && it.hasNext) {
wenzelm@59703
   279
                        last = it.next
wenzelm@59703
   280
                        i += last.length
wenzelm@59703
   281
                      }
wenzelm@59705
   282
                      reparse_spans(resources, syntax, get_blob, can_import,
wenzelm@59705
   283
                        name, commands, first_unfinished, last)
wenzelm@59703
   284
                    case None => commands
wenzelm@59703
   285
                  }
wenzelm@59703
   286
                case None => commands
wenzelm@59703
   287
              }
wenzelm@59703
   288
            }
wenzelm@59703
   289
          node.update_perspective(text_perspective, perspective).update_commands(commands1)
wenzelm@59703
   290
        }
wenzelm@48755
   291
    }
wenzelm@48755
   292
  }
wenzelm@48755
   293
wenzelm@56316
   294
  def parse_change(
wenzelm@56208
   295
      resources: Resources,
wenzelm@49524
   296
      reparse_limit: Int,
wenzelm@43722
   297
      previous: Document.Version,
wenzelm@54521
   298
      doc_blobs: Document.Blobs,
wenzelm@56315
   299
      edits: List[Document.Edit_Text]): Session.Change =
wenzelm@38374
   300
  {
wenzelm@59702
   301
    val (syntax_changed, nodes0, doc_edits0) = header_edits(resources, previous, edits)
wenzelm@59702
   302
wenzelm@56336
   303
    def get_blob(name: Document.Node.Name) =
wenzelm@56336
   304
      doc_blobs.get(name) orElse previous.nodes(name).get_blob
wenzelm@56336
   305
wenzelm@59702
   306
    def can_import(name: Document.Node.Name): Boolean =
wenzelm@65361
   307
      resources.session_base.loaded_theory(name) || nodes0(name).has_header
wenzelm@54518
   308
wenzelm@56316
   309
    val (doc_edits, version) =
wenzelm@59077
   310
      if (edits.isEmpty) (Nil, Document.Version.make(previous.nodes))
wenzelm@56316
   311
      else {
wenzelm@56316
   312
        val reparse =
wenzelm@59077
   313
          (syntax_changed /: nodes0.iterator)({
wenzelm@56316
   314
            case (reparse, (name, node)) =>
wenzelm@64799
   315
              if (node.load_commands_changed(doc_blobs) && !reparse.contains(name))
wenzelm@56316
   316
                name :: reparse
wenzelm@56316
   317
              else reparse
wenzelm@56316
   318
            })
wenzelm@56316
   319
        val reparse_set = reparse.toSet
wenzelm@46946
   320
wenzelm@56316
   321
        var nodes = nodes0
wenzelm@56316
   322
        val doc_edits = new mutable.ListBuffer[Document.Edit_Command]; doc_edits ++= doc_edits0
wenzelm@54521
   323
wenzelm@56316
   324
        val node_edits =
wenzelm@56316
   325
          (edits ::: reparse.map((_, Document.Node.Edits(Nil)))).groupBy(_._1)
wenzelm@56316
   326
            .asInstanceOf[Map[Document.Node.Name, List[Document.Edit_Text]]]  // FIXME ???
wenzelm@38374
   327
wenzelm@56316
   328
        node_edits foreach {
wenzelm@56316
   329
          case (name, edits) =>
wenzelm@56316
   330
            val node = nodes(name)
wenzelm@66770
   331
            val syntax = resources.session_base.node_syntax(nodes, name)
wenzelm@56316
   332
            val commands = node.commands
wenzelm@48754
   333
wenzelm@56316
   334
            val node1 =
wenzelm@59319
   335
              if (reparse_set(name) && commands.nonEmpty)
wenzelm@56316
   336
                node.update_commands(
wenzelm@59702
   337
                  reparse_spans(resources, syntax, get_blob, can_import, name,
wenzelm@59705
   338
                    commands, commands.head, commands.last))
wenzelm@56316
   339
              else node
wenzelm@56316
   340
            val node2 =
wenzelm@59702
   341
              (node1 /: edits)(
wenzelm@59702
   342
                text_edit(resources, syntax, get_blob, can_import, reparse_limit, _, _))
wenzelm@59372
   343
            val node3 =
wenzelm@59372
   344
              if (reparse_set.contains(name))
wenzelm@59702
   345
                text_edit(resources, syntax, get_blob, can_import, reparse_limit,
wenzelm@59372
   346
                  node2, (name, node2.edit_perspective))
wenzelm@59372
   347
              else node2
wenzelm@54521
   348
wenzelm@60215
   349
            if (!node.same_perspective(node3.text_perspective, node3.perspective))
wenzelm@59372
   350
              doc_edits += (name -> node3.perspective)
wenzelm@54521
   351
wenzelm@59372
   352
            doc_edits += (name -> Document.Node.Edits(diff_commands(commands, node3.commands)))
wenzelm@59372
   353
wenzelm@59372
   354
            nodes += (name -> node3)
wenzelm@56316
   355
        }
wenzelm@59077
   356
        (doc_edits.toList.filterNot(_._2.is_void), Document.Version.make(nodes))
wenzelm@54513
   357
      }
wenzelm@54513
   358
wenzelm@59319
   359
    Session.Change(previous, syntax_changed, syntax_changed.nonEmpty, doc_edits, version)
wenzelm@38374
   360
  }
wenzelm@34268
   361
}