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