src/Pure/Thy/thy_syntax.scala
author wenzelm
Sun Mar 15 20:35:47 2015 +0100 (2015-03-15)
changeset 59705 740a0ca7e09b
parent 59704 b5eb7c688836
child 59803 88a89f01fc27
permissions -rw-r--r--
clarified span position;
     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) syntax_changed0 += name
    87           nodes += (name -> node1)
    88           doc_edits += (name -> Document.Node.Deps(header))
    89         }
    90       case _ =>
    91     }
    92 
    93     val syntax_changed = nodes.descendants(syntax_changed0.toList)
    94 
    95     for (name <- syntax_changed) {
    96       val node = nodes(name)
    97       val syntax =
    98         if (node.is_empty) None
    99         else {
   100           val header = node.header
   101           val imports_syntax = header.imports.flatMap(a => nodes(a._1).syntax)
   102           Some((resources.base_syntax /: imports_syntax)(_ ++ _).add_keywords(header.keywords))
   103         }
   104       nodes += (name -> node.update_syntax(syntax))
   105     }
   106 
   107     (syntax_changed, nodes, doc_edits.toList)
   108   }
   109 
   110 
   111 
   112   /** text edits **/
   113 
   114   /* edit individual command source */
   115 
   116   @tailrec def edit_text(eds: List[Text.Edit], commands: Linear_Set[Command]): Linear_Set[Command] =
   117   {
   118     eds match {
   119       case e :: es =>
   120         Document.Node.Commands.starts(commands.iterator).find {
   121           case (cmd, cmd_start) =>
   122             e.can_edit(cmd.source, cmd_start) ||
   123               e.is_insert && e.start == cmd_start + cmd.length
   124         } match {
   125           case Some((cmd, cmd_start)) if e.can_edit(cmd.source, cmd_start) =>
   126             val (rest, text) = e.edit(cmd.source, cmd_start)
   127             val new_commands = commands.insert_after(Some(cmd), Command.unparsed(text)) - cmd
   128             edit_text(rest.toList ::: es, new_commands)
   129 
   130           case Some((cmd, cmd_start)) =>
   131             edit_text(es, commands.insert_after(Some(cmd), Command.unparsed(e.text)))
   132 
   133           case None =>
   134             require(e.is_insert && e.start == 0)
   135             edit_text(es, commands.insert_after(None, Command.unparsed(e.text)))
   136         }
   137       case Nil => commands
   138     }
   139   }
   140 
   141 
   142   /* reparse range of command spans */
   143 
   144   @tailrec private def chop_common(
   145       cmds: List[Command],
   146       blobs_spans: List[(Command.Blobs_Info, Command_Span.Span)])
   147     : (List[Command], List[(Command.Blobs_Info, Command_Span.Span)]) =
   148   {
   149     (cmds, blobs_spans) match {
   150       case (cmd :: cmds, (blobs, span) :: rest) if cmd.blobs == blobs && cmd.span == span =>
   151         chop_common(cmds, rest)
   152       case _ => (cmds, blobs_spans)
   153     }
   154   }
   155 
   156   private def reparse_spans(
   157     resources: Resources,
   158     syntax: Prover.Syntax,
   159     get_blob: Document.Node.Name => Option[Document.Blob],
   160     can_import: Document.Node.Name => Boolean,
   161     node_name: Document.Node.Name,
   162     commands: Linear_Set[Command],
   163     first: Command, last: Command): Linear_Set[Command] =
   164   {
   165     val cmds0 = commands.iterator(first, last).toList
   166     val blobs_spans0 =
   167       syntax.parse_spans(cmds0.iterator.map(_.source).mkString).map(span =>
   168         (Command.blobs_info(resources, syntax, get_blob, can_import, node_name, span), span))
   169 
   170     val (cmds1, blobs_spans1) = chop_common(cmds0, blobs_spans0)
   171 
   172     val (rev_cmds2, rev_blobs_spans2) = chop_common(cmds1.reverse, blobs_spans1.reverse)
   173     val cmds2 = rev_cmds2.reverse
   174     val blobs_spans2 = rev_blobs_spans2.reverse
   175 
   176     cmds2 match {
   177       case Nil =>
   178         assert(blobs_spans2.isEmpty)
   179         commands
   180       case cmd :: _ =>
   181         val hook = commands.prev(cmd)
   182         val inserted =
   183           blobs_spans2.map({ case (blobs, span) =>
   184             Command(Document_ID.make(), node_name, blobs, span) })
   185         (commands /: cmds2)(_ - _).append_after(hook, inserted)
   186     }
   187   }
   188 
   189 
   190   /* main */
   191 
   192   def diff_commands(old_cmds: Linear_Set[Command], new_cmds: Linear_Set[Command])
   193     : List[Command.Edit] =
   194   {
   195     val removed = old_cmds.iterator.filter(!new_cmds.contains(_)).toList
   196     val inserted = new_cmds.iterator.filter(!old_cmds.contains(_)).toList
   197 
   198     removed.reverse.map(cmd => (old_cmds.prev(cmd), None)) :::
   199     inserted.map(cmd => (new_cmds.prev(cmd), Some(cmd)))
   200   }
   201 
   202   private def text_edit(
   203     resources: Resources,
   204     syntax: Prover.Syntax,
   205     get_blob: Document.Node.Name => Option[Document.Blob],
   206     can_import: Document.Node.Name => Boolean,
   207     reparse_limit: Int,
   208     node: Document.Node, edit: Document.Edit_Text): Document.Node =
   209   {
   210     /* recover command spans after edits */
   211     // FIXME somewhat slow
   212     def recover_spans(
   213       name: Document.Node.Name,
   214       perspective: Command.Perspective,
   215       commands: Linear_Set[Command]): Linear_Set[Command] =
   216     {
   217       val is_visible = perspective.commands.toSet
   218 
   219       def next_invisible(cmds: Linear_Set[Command], from: Command): Command =
   220         cmds.iterator(from).dropWhile(cmd => !cmd.is_proper || is_visible(cmd))
   221           .find(_.is_proper) getOrElse cmds.last
   222 
   223       @tailrec def recover(cmds: Linear_Set[Command]): Linear_Set[Command] =
   224         cmds.find(_.is_unparsed) match {
   225           case Some(first_unparsed) =>
   226             val first = next_invisible(cmds.reverse, first_unparsed)
   227             val last = next_invisible(cmds, first_unparsed)
   228             recover(
   229               reparse_spans(resources, syntax, get_blob, can_import, name, cmds, first, last))
   230           case None => cmds
   231         }
   232       recover(commands)
   233     }
   234 
   235     edit match {
   236       case (_, Document.Node.Clear()) => node.clear
   237 
   238       case (_, Document.Node.Blob(blob)) => node.init_blob(blob)
   239 
   240       case (name, Document.Node.Edits(text_edits)) =>
   241         if (name.is_theory) {
   242           val commands0 = node.commands
   243           val commands1 = edit_text(text_edits, commands0)
   244           val commands2 = recover_spans(name, node.perspective.visible, commands1)
   245           node.update_commands(commands2)
   246         }
   247         else node
   248 
   249       case (_, Document.Node.Deps(_)) => node
   250 
   251       case (name, Document.Node.Perspective(required, text_perspective, overlays)) =>
   252         val (visible, visible_overlay) = command_perspective(node, text_perspective, overlays)
   253         val perspective: Document.Node.Perspective_Command =
   254           Document.Node.Perspective(required, visible_overlay, overlays)
   255         if (node.same_perspective(text_perspective, perspective)) node
   256         else {
   257           /* consolidate unfinished spans */
   258           val is_visible = visible.commands.toSet
   259           val commands = node.commands
   260           val commands1 =
   261             if (is_visible.isEmpty) commands
   262             else {
   263               commands.find(_.is_unfinished) match {
   264                 case Some(first_unfinished) =>
   265                   commands.reverse.find(is_visible) match {
   266                     case Some(last_visible) =>
   267                       val it = commands.iterator(last_visible)
   268                       var last = last_visible
   269                       var i = 0
   270                       while (i < reparse_limit && it.hasNext) {
   271                         last = it.next
   272                         i += last.length
   273                       }
   274                       reparse_spans(resources, syntax, get_blob, can_import,
   275                         name, commands, first_unfinished, last)
   276                     case None => commands
   277                   }
   278                 case None => commands
   279               }
   280             }
   281           node.update_perspective(text_perspective, perspective).update_commands(commands1)
   282         }
   283     }
   284   }
   285 
   286   def parse_change(
   287       resources: Resources,
   288       reparse_limit: Int,
   289       previous: Document.Version,
   290       doc_blobs: Document.Blobs,
   291       edits: List[Document.Edit_Text]): Session.Change =
   292   {
   293     val (syntax_changed, nodes0, doc_edits0) = header_edits(resources, previous, edits)
   294 
   295     def get_blob(name: Document.Node.Name) =
   296       doc_blobs.get(name) orElse previous.nodes(name).get_blob
   297 
   298     def can_import(name: Document.Node.Name): Boolean =
   299       resources.loaded_theories(name.theory) || nodes0(name).has_header
   300 
   301     val (doc_edits, version) =
   302       if (edits.isEmpty) (Nil, Document.Version.make(previous.nodes))
   303       else {
   304         val reparse =
   305           (syntax_changed /: nodes0.iterator)({
   306             case (reparse, (name, node)) =>
   307               if (node.load_commands.exists(_.blobs_changed(doc_blobs)))
   308                 name :: reparse
   309               else reparse
   310             })
   311         val reparse_set = reparse.toSet
   312 
   313         var nodes = nodes0
   314         val doc_edits = new mutable.ListBuffer[Document.Edit_Command]; doc_edits ++= doc_edits0
   315 
   316         val node_edits =
   317           (edits ::: reparse.map((_, Document.Node.Edits(Nil)))).groupBy(_._1)
   318             .asInstanceOf[Map[Document.Node.Name, List[Document.Edit_Text]]]  // FIXME ???
   319 
   320         node_edits foreach {
   321           case (name, edits) =>
   322             val node = nodes(name)
   323             val syntax = node.syntax getOrElse resources.base_syntax
   324             val commands = node.commands
   325 
   326             val node1 =
   327               if (reparse_set(name) && commands.nonEmpty)
   328                 node.update_commands(
   329                   reparse_spans(resources, syntax, get_blob, can_import, name,
   330                     commands, commands.head, commands.last))
   331               else node
   332             val node2 =
   333               (node1 /: edits)(
   334                 text_edit(resources, syntax, get_blob, can_import, reparse_limit, _, _))
   335             val node3 =
   336               if (reparse_set.contains(name))
   337                 text_edit(resources, syntax, get_blob, can_import, reparse_limit,
   338                   node2, (name, node2.edit_perspective))
   339               else node2
   340 
   341             if (!(node.same_perspective(node3.text_perspective, node3.perspective)))
   342               doc_edits += (name -> node3.perspective)
   343 
   344             doc_edits += (name -> Document.Node.Edits(diff_commands(commands, node3.commands)))
   345 
   346             nodes += (name -> node3)
   347         }
   348         (doc_edits.toList.filterNot(_._2.is_void), Document.Version.make(nodes))
   349       }
   350 
   351     Session.Change(previous, syntax_changed, syntax_changed.nonEmpty, doc_edits, version)
   352   }
   353 }