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