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