src/Pure/Thy/thy_syntax.scala
author wenzelm
Mon Aug 11 22:43:26 2014 +0200 (2014-08-11)
changeset 57902 3f1fd41ee821
parent 57901 e1abca2527da
child 57904 922273b7bf8a
permissions -rw-r--r--
tuned output, in accordance to transaction name in ML;
     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   /** spans **/
    17 
    18   sealed abstract class Span_Kind {
    19     override def toString: String =
    20       this match {
    21         case Command_Span(name) => if (name != "") name else "<command>"
    22         case Ignored_Span => "<ignored>"
    23         case Malformed_Span => "<malformed>"
    24       }
    25   }
    26   case class Command_Span(name: String) extends Span_Kind
    27   case object Ignored_Span extends Span_Kind
    28   case object Malformed_Span extends Span_Kind
    29 
    30   sealed case class Span(kind: Span_Kind, content: List[Token])
    31   {
    32     def compact_source: (String, Span) =
    33     {
    34       val source: String =
    35         content match {
    36           case List(tok) => tok.source
    37           case toks => toks.map(_.source).mkString
    38         }
    39 
    40       val content1 = new mutable.ListBuffer[Token]
    41       var i = 0
    42       for (Token(kind, s) <- content) {
    43         val n = s.length
    44         val s1 = source.substring(i, i + n)
    45         content1 += Token(kind, s1)
    46         i += n
    47       }
    48       (source, Span(kind, content1.toList))
    49     }
    50   }
    51 
    52   val empty_span: Span = Span(Ignored_Span, Nil)
    53 
    54   def unparsed_span(source: String): Span =
    55     Span(Malformed_Span, List(Token(Token.Kind.UNPARSED, source)))
    56 
    57 
    58   /* parse */
    59 
    60   def parse_spans(toks: List[Token]): List[Span] =
    61   {
    62     val result = new mutable.ListBuffer[Span]
    63     val content = new mutable.ListBuffer[Token]
    64     val improper = new mutable.ListBuffer[Token]
    65 
    66     def ship(span: List[Token])
    67     {
    68       val kind =
    69         if (!span.isEmpty && span.head.is_command && !span.exists(_.is_error))
    70           Command_Span(span.head.source)
    71         else if (span.forall(_.is_improper)) Ignored_Span
    72         else Malformed_Span
    73       result += Span(kind, span)
    74     }
    75 
    76     def flush()
    77     {
    78       if (!content.isEmpty) { ship(content.toList); content.clear }
    79       if (!improper.isEmpty) { ship(improper.toList); improper.clear }
    80     }
    81 
    82     for (tok <- toks) {
    83       if (tok.is_command) { flush(); content += tok }
    84       else if (tok.is_improper) improper += tok
    85       else { content ++= improper; improper.clear; content += tok }
    86     }
    87     flush()
    88 
    89     result.toList
    90   }
    91 
    92 
    93 
    94   /** perspective **/
    95 
    96   def command_perspective(
    97       node: Document.Node,
    98       perspective: Text.Perspective,
    99       overlays: Document.Node.Overlays): (Command.Perspective, Command.Perspective) =
   100   {
   101     if (perspective.is_empty && overlays.is_empty)
   102       (Command.Perspective.empty, Command.Perspective.empty)
   103     else {
   104       val has_overlay = overlays.commands
   105       val visible = new mutable.ListBuffer[Command]
   106       val visible_overlay = new mutable.ListBuffer[Command]
   107       @tailrec
   108       def check_ranges(ranges: List[Text.Range], commands: Stream[(Command, Text.Offset)])
   109       {
   110         (ranges, commands) match {
   111           case (range :: more_ranges, (command, offset) #:: more_commands) =>
   112             val command_range = command.range + offset
   113             range compare command_range match {
   114               case 0 =>
   115                 visible += command
   116                 visible_overlay += command
   117                 check_ranges(ranges, more_commands)
   118               case c =>
   119                 if (has_overlay(command)) visible_overlay += command
   120 
   121                 if (c < 0) check_ranges(more_ranges, commands)
   122                 else check_ranges(ranges, more_commands)
   123             }
   124 
   125           case (Nil, (command, _) #:: more_commands) =>
   126             if (has_overlay(command)) visible_overlay += command
   127 
   128             check_ranges(Nil, more_commands)
   129 
   130           case _ =>
   131         }
   132       }
   133 
   134       val commands =
   135         (if (overlays.is_empty) node.command_iterator(perspective.range)
   136          else node.command_iterator()).toStream
   137       check_ranges(perspective.ranges, commands)
   138       (Command.Perspective(visible.toList), Command.Perspective(visible_overlay.toList))
   139     }
   140   }
   141 
   142 
   143 
   144   /** header edits: structure and outer syntax **/
   145 
   146   private def header_edits(
   147     resources: Resources,
   148     previous: Document.Version,
   149     edits: List[Document.Edit_Text]):
   150     (Prover.Syntax, Boolean, Boolean, List[Document.Node.Name], Document.Nodes,
   151       List[Document.Edit_Command]) =
   152   {
   153     var updated_imports = false
   154     var updated_keywords = false
   155     var nodes = previous.nodes
   156     val doc_edits = new mutable.ListBuffer[Document.Edit_Command]
   157 
   158     edits foreach {
   159       case (name, Document.Node.Deps(header)) =>
   160         val node = nodes(name)
   161         val update_header =
   162           !node.header.errors.isEmpty || !header.errors.isEmpty || node.header != header
   163         if (update_header) {
   164           val node1 = node.update_header(header)
   165           updated_imports = updated_imports || (node.header.imports != node1.header.imports)
   166           updated_keywords = updated_keywords || (node.header.keywords != node1.header.keywords)
   167           nodes += (name -> node1)
   168           doc_edits += (name -> Document.Node.Deps(header))
   169         }
   170       case _ =>
   171     }
   172 
   173     val (syntax, syntax_changed) =
   174       previous.syntax match {
   175         case Some(syntax) if !updated_keywords =>
   176           (syntax, false)
   177         case _ =>
   178           val syntax =
   179             (resources.base_syntax /: nodes.iterator) {
   180               case (syn, (_, node)) => syn.add_keywords(node.header.keywords)
   181             }
   182           (syntax, true)
   183       }
   184 
   185     val reparse =
   186       if (updated_imports || updated_keywords)
   187         nodes.descendants(doc_edits.iterator.map(_._1).toList)
   188       else Nil
   189 
   190     (syntax, syntax_changed, updated_imports, reparse, nodes, doc_edits.toList)
   191   }
   192 
   193 
   194 
   195   /** text edits **/
   196 
   197   /* edit individual command source */
   198 
   199   @tailrec def edit_text(eds: List[Text.Edit], commands: Linear_Set[Command]): Linear_Set[Command] =
   200   {
   201     eds match {
   202       case e :: es =>
   203         Document.Node.Commands.starts(commands.iterator).find {
   204           case (cmd, cmd_start) =>
   205             e.can_edit(cmd.source, cmd_start) ||
   206               e.is_insert && e.start == cmd_start + cmd.length
   207         } match {
   208           case Some((cmd, cmd_start)) if e.can_edit(cmd.source, cmd_start) =>
   209             val (rest, text) = e.edit(cmd.source, cmd_start)
   210             val new_commands = commands.insert_after(Some(cmd), Command.unparsed(text)) - cmd
   211             edit_text(rest.toList ::: es, new_commands)
   212 
   213           case Some((cmd, cmd_start)) =>
   214             edit_text(es, commands.insert_after(Some(cmd), Command.unparsed(e.text)))
   215 
   216           case None =>
   217             require(e.is_insert && e.start == 0)
   218             edit_text(es, commands.insert_after(None, Command.unparsed(e.text)))
   219         }
   220       case Nil => commands
   221     }
   222   }
   223 
   224 
   225   /* inlined files */
   226 
   227   private def find_file(tokens: List[Token]): Option[String] =
   228   {
   229     def clean(toks: List[Token]): List[Token] =
   230       toks match {
   231         case t :: _ :: ts if t.is_keyword && (t.source == "%" || t.source == "--") => clean(ts)
   232         case t :: ts => t :: clean(ts)
   233         case Nil => Nil
   234       }
   235     clean(tokens.filter(_.is_proper)) match {
   236       case tok :: toks if tok.is_command => toks.find(_.is_name).map(_.content)
   237       case _ => None
   238     }
   239   }
   240 
   241   def span_files(syntax: Prover.Syntax, span: Span): List[String] =
   242     span.kind match {
   243       case Command_Span(name) =>
   244         syntax.load_command(name) match {
   245           case Some(exts) =>
   246             find_file(span.content) match {
   247               case Some(file) =>
   248                 if (exts.isEmpty) List(file)
   249                 else exts.map(ext => file + "." + ext)
   250               case None => Nil
   251             }
   252           case None => Nil
   253         }
   254       case _ => Nil
   255     }
   256 
   257   def resolve_files(
   258       resources: Resources,
   259       syntax: Prover.Syntax,
   260       node_name: Document.Node.Name,
   261       span: Span,
   262       get_blob: Document.Node.Name => Option[Document.Blob])
   263     : List[Command.Blob] =
   264   {
   265     span_files(syntax, span).map(file_name =>
   266       Exn.capture {
   267         val name =
   268           Document.Node.Name(resources.append(node_name.master_dir, Path.explode(file_name)))
   269         val blob = get_blob(name).map(blob => ((blob.bytes.sha1_digest, blob.chunk)))
   270         (name, blob)
   271       })
   272   }
   273 
   274 
   275   /* reparse range of command spans */
   276 
   277   @tailrec private def chop_common(
   278       cmds: List[Command],
   279       blobs_spans: List[(List[Command.Blob], Span)])
   280     : (List[Command], List[(List[Command.Blob], Span)]) =
   281   {
   282     (cmds, blobs_spans) match {
   283       case (cmd :: cmds, (blobs, span) :: rest) if cmd.blobs == blobs && cmd.span == span =>
   284         chop_common(cmds, rest)
   285       case _ => (cmds, blobs_spans)
   286     }
   287   }
   288 
   289   private def reparse_spans(
   290     resources: Resources,
   291     syntax: Prover.Syntax,
   292     get_blob: Document.Node.Name => Option[Document.Blob],
   293     name: Document.Node.Name,
   294     commands: Linear_Set[Command],
   295     first: Command, last: Command): Linear_Set[Command] =
   296   {
   297     val cmds0 = commands.iterator(first, last).toList
   298     val blobs_spans0 =
   299       parse_spans(syntax.scan(cmds0.iterator.map(_.source).mkString)).
   300         map(span => (resolve_files(resources, syntax, name, span, get_blob), span))
   301 
   302     val (cmds1, blobs_spans1) = chop_common(cmds0, blobs_spans0)
   303 
   304     val (rev_cmds2, rev_blobs_spans2) = chop_common(cmds1.reverse, blobs_spans1.reverse)
   305     val cmds2 = rev_cmds2.reverse
   306     val blobs_spans2 = rev_blobs_spans2.reverse
   307 
   308     cmds2 match {
   309       case Nil =>
   310         assert(blobs_spans2.isEmpty)
   311         commands
   312       case cmd :: _ =>
   313         val hook = commands.prev(cmd)
   314         val inserted =
   315           blobs_spans2.map({ case (blobs, span) => Command(Document_ID.make(), name, blobs, span) })
   316         (commands /: cmds2)(_ - _).append_after(hook, inserted)
   317     }
   318   }
   319 
   320 
   321   /* recover command spans after edits */
   322 
   323   // FIXME somewhat slow
   324   private def recover_spans(
   325     resources: Resources,
   326     syntax: Prover.Syntax,
   327     get_blob: Document.Node.Name => Option[Document.Blob],
   328     name: Document.Node.Name,
   329     perspective: Command.Perspective,
   330     commands: Linear_Set[Command]): Linear_Set[Command] =
   331   {
   332     val visible = perspective.commands.toSet
   333 
   334     def next_invisible_command(cmds: Linear_Set[Command], from: Command): Command =
   335       cmds.iterator(from).dropWhile(cmd => !cmd.is_command || visible(cmd))
   336         .find(_.is_command) getOrElse cmds.last
   337 
   338     @tailrec def recover(cmds: Linear_Set[Command]): Linear_Set[Command] =
   339       cmds.find(_.is_unparsed) match {
   340         case Some(first_unparsed) =>
   341           val first = next_invisible_command(cmds.reverse, first_unparsed)
   342           val last = next_invisible_command(cmds, first_unparsed)
   343           recover(reparse_spans(resources, syntax, get_blob, name, cmds, first, last))
   344         case None => cmds
   345       }
   346     recover(commands)
   347   }
   348 
   349 
   350   /* consolidate unfinished spans */
   351 
   352   private def consolidate_spans(
   353     resources: Resources,
   354     syntax: Prover.Syntax,
   355     get_blob: Document.Node.Name => Option[Document.Blob],
   356     reparse_limit: Int,
   357     name: Document.Node.Name,
   358     perspective: Command.Perspective,
   359     commands: Linear_Set[Command]): Linear_Set[Command] =
   360   {
   361     if (perspective.commands.isEmpty) commands
   362     else {
   363       commands.find(_.is_unfinished) match {
   364         case Some(first_unfinished) =>
   365           val visible = perspective.commands.toSet
   366           commands.reverse.find(visible) match {
   367             case Some(last_visible) =>
   368               val it = commands.iterator(last_visible)
   369               var last = last_visible
   370               var i = 0
   371               while (i < reparse_limit && it.hasNext) {
   372                 last = it.next
   373                 i += last.length
   374               }
   375               reparse_spans(resources, syntax, get_blob, name, commands, first_unfinished, last)
   376             case None => commands
   377           }
   378         case None => commands
   379       }
   380     }
   381   }
   382 
   383 
   384   /* main */
   385 
   386   def diff_commands(old_cmds: Linear_Set[Command], new_cmds: Linear_Set[Command])
   387     : List[Command.Edit] =
   388   {
   389     val removed = old_cmds.iterator.filter(!new_cmds.contains(_)).toList
   390     val inserted = new_cmds.iterator.filter(!old_cmds.contains(_)).toList
   391 
   392     removed.reverse.map(cmd => (old_cmds.prev(cmd), None)) :::
   393     inserted.map(cmd => (new_cmds.prev(cmd), Some(cmd)))
   394   }
   395 
   396   private def text_edit(
   397     resources: Resources,
   398     syntax: Prover.Syntax,
   399     get_blob: Document.Node.Name => Option[Document.Blob],
   400     reparse_limit: Int,
   401     node: Document.Node, edit: Document.Edit_Text): Document.Node =
   402   {
   403     edit match {
   404       case (_, Document.Node.Clear()) => node.clear
   405 
   406       case (_, Document.Node.Blob(blob)) => node.init_blob(blob)
   407 
   408       case (name, Document.Node.Edits(text_edits)) =>
   409         if (name.is_theory) {
   410           val commands0 = node.commands
   411           val commands1 = edit_text(text_edits, commands0)
   412           val commands2 =
   413             recover_spans(resources, syntax, get_blob, name, node.perspective.visible, commands1)
   414           node.update_commands(commands2)
   415         }
   416         else node
   417 
   418       case (_, Document.Node.Deps(_)) => node
   419 
   420       case (name, Document.Node.Perspective(required, text_perspective, overlays)) =>
   421         val (visible, visible_overlay) = command_perspective(node, text_perspective, overlays)
   422         val perspective: Document.Node.Perspective_Command =
   423           Document.Node.Perspective(required, visible_overlay, overlays)
   424         if (node.same_perspective(perspective)) node
   425         else
   426           node.update_perspective(perspective).update_commands(
   427             consolidate_spans(resources, syntax, get_blob, reparse_limit,
   428               name, visible, node.commands))
   429     }
   430   }
   431 
   432   def parse_change(
   433       resources: Resources,
   434       reparse_limit: Int,
   435       previous: Document.Version,
   436       doc_blobs: Document.Blobs,
   437       edits: List[Document.Edit_Text]): Session.Change =
   438   {
   439     def get_blob(name: Document.Node.Name) =
   440       doc_blobs.get(name) orElse previous.nodes(name).get_blob
   441 
   442     val (syntax, syntax_changed, deps_changed, reparse0, nodes0, doc_edits0) =
   443       header_edits(resources, previous, edits)
   444 
   445     val (doc_edits, version) =
   446       if (edits.isEmpty) (Nil, Document.Version.make(Some(syntax), previous.nodes))
   447       else {
   448         val reparse =
   449           (reparse0 /: nodes0.iterator)({
   450             case (reparse, (name, node)) =>
   451               if (node.load_commands.exists(_.blobs_changed(doc_blobs)))
   452                 name :: reparse
   453               else reparse
   454             })
   455         val reparse_set = reparse.toSet
   456 
   457         var nodes = nodes0
   458         val doc_edits = new mutable.ListBuffer[Document.Edit_Command]; doc_edits ++= doc_edits0
   459 
   460         val node_edits =
   461           (edits ::: reparse.map((_, Document.Node.Edits(Nil)))).groupBy(_._1)
   462             .asInstanceOf[Map[Document.Node.Name, List[Document.Edit_Text]]]  // FIXME ???
   463 
   464         node_edits foreach {
   465           case (name, edits) =>
   466             val node = nodes(name)
   467             val commands = node.commands
   468 
   469             val node1 =
   470               if (reparse_set(name) && !commands.isEmpty)
   471                 node.update_commands(
   472                   reparse_spans(resources, syntax, get_blob,
   473                     name, commands, commands.head, commands.last))
   474               else node
   475             val node2 =
   476               (node1 /: edits)(text_edit(resources, syntax, get_blob, reparse_limit, _, _))
   477 
   478             if (!(node.same_perspective(node2.perspective)))
   479               doc_edits += (name -> node2.perspective)
   480 
   481             doc_edits += (name -> Document.Node.Edits(diff_commands(commands, node2.commands)))
   482 
   483             nodes += (name -> node2)
   484         }
   485         (doc_edits.toList.filterNot(_._2.is_void), Document.Version.make(Some(syntax), nodes))
   486       }
   487 
   488     Session.Change(previous, syntax_changed, deps_changed, doc_edits, version)
   489   }
   490 }