src/Pure/Thy/thy_syntax.scala
author wenzelm
Tue Aug 02 17:35:18 2016 +0200 (2016-08-02)
changeset 63579 73939a9b70a3
parent 60215 5fb4990dfc73
child 63584 68751fe1c036
permissions -rw-r--r--
support 'abbrevs' within theory header;
simplified 'keywords': no abbreviations here;
     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.base_syntax /: imports_syntax)(_ ++ _)
   105             .add_keywords(header.keywords).add_abbrevs(header.abbrevs))
   106         }
   107       nodes += (name -> node.update_syntax(syntax))
   108     }
   109 
   110     (syntax_changed, nodes, doc_edits.toList)
   111   }
   112 
   113 
   114 
   115   /** text edits **/
   116 
   117   /* edit individual command source */
   118 
   119   @tailrec def edit_text(eds: List[Text.Edit], commands: Linear_Set[Command]): Linear_Set[Command] =
   120   {
   121     eds match {
   122       case e :: es =>
   123         Document.Node.Commands.starts(commands.iterator).find {
   124           case (cmd, cmd_start) =>
   125             e.can_edit(cmd.source, cmd_start) ||
   126               e.is_insert && e.start == cmd_start + cmd.length
   127         } match {
   128           case Some((cmd, cmd_start)) if e.can_edit(cmd.source, cmd_start) =>
   129             val (rest, text) = e.edit(cmd.source, cmd_start)
   130             val new_commands = commands.insert_after(Some(cmd), Command.unparsed(text)) - cmd
   131             edit_text(rest.toList ::: es, new_commands)
   132 
   133           case Some((cmd, cmd_start)) =>
   134             edit_text(es, commands.insert_after(Some(cmd), Command.unparsed(e.text)))
   135 
   136           case None =>
   137             require(e.is_insert && e.start == 0)
   138             edit_text(es, commands.insert_after(None, Command.unparsed(e.text)))
   139         }
   140       case Nil => commands
   141     }
   142   }
   143 
   144 
   145   /* reparse range of command spans */
   146 
   147   @tailrec private def chop_common(
   148       cmds: List[Command],
   149       blobs_spans: List[(Command.Blobs_Info, Command_Span.Span)])
   150     : (List[Command], List[(Command.Blobs_Info, Command_Span.Span)]) =
   151   {
   152     (cmds, blobs_spans) match {
   153       case (cmd :: cmds, (blobs_info, span) :: rest)
   154       if cmd.blobs_info == blobs_info && cmd.span == span => chop_common(cmds, rest)
   155       case _ => (cmds, blobs_spans)
   156     }
   157   }
   158 
   159   private def reparse_spans(
   160     resources: Resources,
   161     syntax: Prover.Syntax,
   162     get_blob: Document.Node.Name => Option[Document.Blob],
   163     can_import: Document.Node.Name => Boolean,
   164     node_name: Document.Node.Name,
   165     commands: Linear_Set[Command],
   166     first: Command, last: Command): Linear_Set[Command] =
   167   {
   168     val cmds0 = commands.iterator(first, last).toList
   169     val blobs_spans0 =
   170       syntax.parse_spans(cmds0.iterator.map(_.source).mkString).map(span =>
   171         (Command.blobs_info(resources, syntax, get_blob, can_import, node_name, span), span))
   172 
   173     val (cmds1, blobs_spans1) = chop_common(cmds0, blobs_spans0)
   174 
   175     val (rev_cmds2, rev_blobs_spans2) = chop_common(cmds1.reverse, blobs_spans1.reverse)
   176     val cmds2 = rev_cmds2.reverse
   177     val blobs_spans2 = rev_blobs_spans2.reverse
   178 
   179     cmds2 match {
   180       case Nil =>
   181         assert(blobs_spans2.isEmpty)
   182         commands
   183       case cmd :: _ =>
   184         val hook = commands.prev(cmd)
   185         val inserted =
   186           blobs_spans2.map({ case (blobs, span) =>
   187             Command(Document_ID.make(), node_name, blobs, span) })
   188         (commands /: cmds2)(_ - _).append_after(hook, inserted)
   189     }
   190   }
   191 
   192 
   193   /* main */
   194 
   195   def diff_commands(old_cmds: Linear_Set[Command], new_cmds: Linear_Set[Command])
   196     : List[Command.Edit] =
   197   {
   198     val removed = old_cmds.iterator.filter(!new_cmds.contains(_)).toList
   199     val inserted = new_cmds.iterator.filter(!old_cmds.contains(_)).toList
   200 
   201     removed.reverse.map(cmd => (old_cmds.prev(cmd), None)) :::
   202     inserted.map(cmd => (new_cmds.prev(cmd), Some(cmd)))
   203   }
   204 
   205   private def text_edit(
   206     resources: Resources,
   207     syntax: Prover.Syntax,
   208     get_blob: Document.Node.Name => Option[Document.Blob],
   209     can_import: Document.Node.Name => Boolean,
   210     reparse_limit: Int,
   211     node: Document.Node, edit: Document.Edit_Text): Document.Node =
   212   {
   213     /* recover command spans after edits */
   214     // FIXME somewhat slow
   215     def recover_spans(
   216       name: Document.Node.Name,
   217       perspective: Command.Perspective,
   218       commands: Linear_Set[Command]): Linear_Set[Command] =
   219     {
   220       val is_visible = perspective.commands.toSet
   221 
   222       def next_invisible(cmds: Linear_Set[Command], from: Command): Command =
   223         cmds.iterator(from).dropWhile(cmd => !cmd.is_proper || is_visible(cmd))
   224           .find(_.is_proper) getOrElse cmds.last
   225 
   226       @tailrec def recover(cmds: Linear_Set[Command]): Linear_Set[Command] =
   227         cmds.find(_.is_unparsed) match {
   228           case Some(first_unparsed) =>
   229             val first = next_invisible(cmds.reverse, first_unparsed)
   230             val last = next_invisible(cmds, first_unparsed)
   231             recover(
   232               reparse_spans(resources, syntax, get_blob, can_import, name, cmds, first, last))
   233           case None => cmds
   234         }
   235       recover(commands)
   236     }
   237 
   238     edit match {
   239       case (_, Document.Node.Clear()) => node.clear
   240 
   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.loaded_theories(name.theory) || 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.exists(_.blobs_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.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 }