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