src/Pure/Thy/thy_syntax.scala
author wenzelm
Sun Mar 15 19:39:31 2015 +0100 (2015-03-15)
changeset 59703 081c57f6b22c
parent 59702 58dfaa369c11
child 59704 b5eb7c688836
permissions -rw-r--r--
tuned;
     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     header: Document.Node.Header,
   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, header, span),
   170           span))
   171 
   172     val (cmds1, blobs_spans1) = chop_common(cmds0, blobs_spans0)
   173 
   174     val (rev_cmds2, rev_blobs_spans2) = chop_common(cmds1.reverse, blobs_spans1.reverse)
   175     val cmds2 = rev_cmds2.reverse
   176     val blobs_spans2 = rev_blobs_spans2.reverse
   177 
   178     cmds2 match {
   179       case Nil =>
   180         assert(blobs_spans2.isEmpty)
   181         commands
   182       case cmd :: _ =>
   183         val hook = commands.prev(cmd)
   184         val inserted =
   185           blobs_spans2.map({ case (blobs, span) =>
   186             Command(Document_ID.make(), node_name, blobs, span) })
   187         (commands /: cmds2)(_ - _).append_after(hook, inserted)
   188     }
   189   }
   190 
   191 
   192   /* recover command spans after edits */
   193 
   194   // FIXME somewhat slow
   195   private def recover_spans(
   196     resources: Resources,
   197     syntax: Prover.Syntax,
   198     get_blob: Document.Node.Name => Option[Document.Blob],
   199     can_import: Document.Node.Name => Boolean,
   200     node_name: Document.Node.Name,
   201     header: Document.Node.Header,
   202     perspective: Command.Perspective,
   203     commands: Linear_Set[Command]): Linear_Set[Command] =
   204   {
   205     val visible = perspective.commands.toSet
   206 
   207     def next_invisible_command(cmds: Linear_Set[Command], from: Command): Command =
   208       cmds.iterator(from).dropWhile(cmd => !cmd.is_proper || visible(cmd))
   209         .find(_.is_proper) getOrElse cmds.last
   210 
   211     @tailrec def recover(cmds: Linear_Set[Command]): Linear_Set[Command] =
   212       cmds.find(_.is_unparsed) match {
   213         case Some(first_unparsed) =>
   214           val first = next_invisible_command(cmds.reverse, first_unparsed)
   215           val last = next_invisible_command(cmds, first_unparsed)
   216           recover(reparse_spans(
   217             resources, syntax, get_blob, can_import, node_name, header, cmds, first, last))
   218         case None => cmds
   219       }
   220     recover(commands)
   221   }
   222 
   223 
   224   /* main */
   225 
   226   def diff_commands(old_cmds: Linear_Set[Command], new_cmds: Linear_Set[Command])
   227     : List[Command.Edit] =
   228   {
   229     val removed = old_cmds.iterator.filter(!new_cmds.contains(_)).toList
   230     val inserted = new_cmds.iterator.filter(!old_cmds.contains(_)).toList
   231 
   232     removed.reverse.map(cmd => (old_cmds.prev(cmd), None)) :::
   233     inserted.map(cmd => (new_cmds.prev(cmd), Some(cmd)))
   234   }
   235 
   236   private def text_edit(
   237     resources: Resources,
   238     syntax: Prover.Syntax,
   239     get_blob: Document.Node.Name => Option[Document.Blob],
   240     can_import: Document.Node.Name => Boolean,
   241     reparse_limit: Int,
   242     node: Document.Node, edit: Document.Edit_Text): Document.Node =
   243   {
   244     edit match {
   245       case (_, Document.Node.Clear()) => node.clear
   246 
   247       case (_, Document.Node.Blob(blob)) => node.init_blob(blob)
   248 
   249       case (name, Document.Node.Edits(text_edits)) =>
   250         if (name.is_theory) {
   251           val commands0 = node.commands
   252           val commands1 = edit_text(text_edits, commands0)
   253           val commands2 =
   254             recover_spans(resources, syntax, get_blob, can_import, name,
   255               node.header, node.perspective.visible, commands1)
   256           node.update_commands(commands2)
   257         }
   258         else node
   259 
   260       case (_, Document.Node.Deps(_)) => node
   261 
   262       case (name, Document.Node.Perspective(required, text_perspective, overlays)) =>
   263         val (visible, visible_overlay) = command_perspective(node, text_perspective, overlays)
   264         val perspective: Document.Node.Perspective_Command =
   265           Document.Node.Perspective(required, visible_overlay, overlays)
   266         if (node.same_perspective(text_perspective, perspective)) node
   267         else {
   268           /* consolidate unfinished spans */
   269           val is_visible = visible.commands.toSet
   270           val commands = node.commands
   271           val commands1 =
   272             if (is_visible.isEmpty) commands
   273             else {
   274               commands.find(_.is_unfinished) match {
   275                 case Some(first_unfinished) =>
   276                   commands.reverse.find(is_visible) match {
   277                     case Some(last_visible) =>
   278                       val it = commands.iterator(last_visible)
   279                       var last = last_visible
   280                       var i = 0
   281                       while (i < reparse_limit && it.hasNext) {
   282                         last = it.next
   283                         i += last.length
   284                       }
   285                       reparse_spans(resources, syntax, get_blob, can_import, name,
   286                         node.header, commands, first_unfinished, last)
   287                     case None => commands
   288                   }
   289                 case None => commands
   290               }
   291             }
   292           node.update_perspective(text_perspective, perspective).update_commands(commands1)
   293         }
   294     }
   295   }
   296 
   297   def parse_change(
   298       resources: Resources,
   299       reparse_limit: Int,
   300       previous: Document.Version,
   301       doc_blobs: Document.Blobs,
   302       edits: List[Document.Edit_Text]): Session.Change =
   303   {
   304     val (syntax_changed, nodes0, doc_edits0) = header_edits(resources, previous, edits)
   305 
   306     def get_blob(name: Document.Node.Name) =
   307       doc_blobs.get(name) orElse previous.nodes(name).get_blob
   308 
   309     def can_import(name: Document.Node.Name): Boolean =
   310       resources.loaded_theories(name.theory) || nodes0(name).has_header
   311 
   312     val (doc_edits, version) =
   313       if (edits.isEmpty) (Nil, Document.Version.make(previous.nodes))
   314       else {
   315         val reparse =
   316           (syntax_changed /: nodes0.iterator)({
   317             case (reparse, (name, node)) =>
   318               if (node.load_commands.exists(_.blobs_changed(doc_blobs)))
   319                 name :: reparse
   320               else reparse
   321             })
   322         val reparse_set = reparse.toSet
   323 
   324         var nodes = nodes0
   325         val doc_edits = new mutable.ListBuffer[Document.Edit_Command]; doc_edits ++= doc_edits0
   326 
   327         val node_edits =
   328           (edits ::: reparse.map((_, Document.Node.Edits(Nil)))).groupBy(_._1)
   329             .asInstanceOf[Map[Document.Node.Name, List[Document.Edit_Text]]]  // FIXME ???
   330 
   331         node_edits foreach {
   332           case (name, edits) =>
   333             val node = nodes(name)
   334             val syntax = node.syntax getOrElse resources.base_syntax
   335             val commands = node.commands
   336 
   337             val node1 =
   338               if (reparse_set(name) && commands.nonEmpty)
   339                 node.update_commands(
   340                   reparse_spans(resources, syntax, get_blob, can_import, name,
   341                     node.header, commands, commands.head, commands.last))
   342               else node
   343             val node2 =
   344               (node1 /: edits)(
   345                 text_edit(resources, syntax, get_blob, can_import, reparse_limit, _, _))
   346             val node3 =
   347               if (reparse_set.contains(name))
   348                 text_edit(resources, syntax, get_blob, can_import, reparse_limit,
   349                   node2, (name, node2.edit_perspective))
   350               else node2
   351 
   352             if (!(node.same_perspective(node3.text_perspective, node3.perspective)))
   353               doc_edits += (name -> node3.perspective)
   354 
   355             doc_edits += (name -> Document.Node.Edits(diff_commands(commands, node3.commands)))
   356 
   357             nodes += (name -> node3)
   358         }
   359         (doc_edits.toList.filterNot(_._2.is_void), Document.Version.make(nodes))
   360       }
   361 
   362     Session.Change(previous, syntax_changed, syntax_changed.nonEmpty, doc_edits, version)
   363   }
   364 }