src/Pure/Thy/thy_syntax.scala
author wenzelm
Thu Feb 27 10:58:43 2014 +0100 (2014-02-27)
changeset 55779 30fb00b5a9d3
parent 55435 662e0fd39823
child 55783 da0513d95155
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   /** 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],
   286       blobs_spans: List[(List[Command.Blob], List[Token])])
   287     : (List[Command], List[(List[Command.Blob], List[Token])]) =
   288   {
   289     (cmds, blobs_spans) match {
   290       case (cmd :: cmds, (blobs, span) :: rest) if cmd.blobs == blobs && cmd.span == span =>
   291         chop_common(cmds, rest)
   292       case _ => (cmds, blobs_spans)
   293     }
   294   }
   295 
   296   private def reparse_spans(
   297     thy_load: Thy_Load,
   298     syntax: Outer_Syntax,
   299     doc_blobs: Document.Blobs,
   300     name: Document.Node.Name,
   301     commands: Linear_Set[Command],
   302     first: Command, last: Command): Linear_Set[Command] =
   303   {
   304     val cmds0 = commands.iterator(first, last).toList
   305     val blobs_spans0 =
   306       parse_spans(syntax.scan(cmds0.iterator.map(_.source).mkString)).
   307         map(span => (resolve_files(thy_load, syntax, name, span, doc_blobs), span))
   308 
   309     val (cmds1, blobs_spans1) = chop_common(cmds0, blobs_spans0)
   310 
   311     val (rev_cmds2, rev_blobs_spans2) = chop_common(cmds1.reverse, blobs_spans1.reverse)
   312     val cmds2 = rev_cmds2.reverse
   313     val blobs_spans2 = rev_blobs_spans2.reverse
   314 
   315     cmds2 match {
   316       case Nil =>
   317         assert(blobs_spans2.isEmpty)
   318         commands
   319       case cmd :: _ =>
   320         val hook = commands.prev(cmd)
   321         val inserted =
   322           blobs_spans2.map({ case (blobs, span) => Command(Document_ID.make(), name, blobs, span) })
   323         (commands /: cmds2)(_ - _).append_after(hook, inserted)
   324     }
   325   }
   326 
   327 
   328   /* recover command spans after edits */
   329 
   330   // FIXME somewhat slow
   331   private def recover_spans(
   332     thy_load: Thy_Load,
   333     syntax: Outer_Syntax,
   334     doc_blobs: Document.Blobs,
   335     name: Document.Node.Name,
   336     perspective: Command.Perspective,
   337     commands: Linear_Set[Command]): Linear_Set[Command] =
   338   {
   339     val visible = perspective.commands.toSet
   340 
   341     def next_invisible_command(cmds: Linear_Set[Command], from: Command): Command =
   342       cmds.iterator(from).dropWhile(cmd => !cmd.is_command || visible(cmd))
   343         .find(_.is_command) getOrElse cmds.last
   344 
   345     @tailrec def recover(cmds: Linear_Set[Command]): Linear_Set[Command] =
   346       cmds.find(_.is_unparsed) match {
   347         case Some(first_unparsed) =>
   348           val first = next_invisible_command(cmds.reverse, first_unparsed)
   349           val last = next_invisible_command(cmds, first_unparsed)
   350           recover(reparse_spans(thy_load, syntax, doc_blobs, name, cmds, first, last))
   351         case None => cmds
   352       }
   353     recover(commands)
   354   }
   355 
   356 
   357   /* consolidate unfinished spans */
   358 
   359   private def consolidate_spans(
   360     thy_load: Thy_Load,
   361     syntax: Outer_Syntax,
   362     doc_blobs: Document.Blobs,
   363     reparse_limit: Int,
   364     name: Document.Node.Name,
   365     perspective: Command.Perspective,
   366     commands: Linear_Set[Command]): Linear_Set[Command] =
   367   {
   368     if (perspective.commands.isEmpty) commands
   369     else {
   370       commands.find(_.is_unfinished) match {
   371         case Some(first_unfinished) =>
   372           val visible = perspective.commands.toSet
   373           commands.reverse.find(visible) match {
   374             case Some(last_visible) =>
   375               val it = commands.iterator(last_visible)
   376               var last = last_visible
   377               var i = 0
   378               while (i < reparse_limit && it.hasNext) {
   379                 last = it.next
   380                 i += last.length
   381               }
   382               reparse_spans(thy_load, syntax, doc_blobs, name, commands, first_unfinished, last)
   383             case None => commands
   384           }
   385         case None => commands
   386       }
   387     }
   388   }
   389 
   390 
   391   /* main */
   392 
   393   def diff_commands(old_cmds: Linear_Set[Command], new_cmds: Linear_Set[Command])
   394     : List[Command.Edit] =
   395   {
   396     val removed = old_cmds.iterator.filter(!new_cmds.contains(_)).toList
   397     val inserted = new_cmds.iterator.filter(!old_cmds.contains(_)).toList
   398 
   399     removed.reverse.map(cmd => (old_cmds.prev(cmd), None)) :::
   400     inserted.map(cmd => (new_cmds.prev(cmd), Some(cmd)))
   401   }
   402 
   403   private def text_edit(
   404     thy_load: Thy_Load,
   405     syntax: Outer_Syntax,
   406     doc_blobs: Document.Blobs,
   407     reparse_limit: Int,
   408     node: Document.Node, edit: Document.Edit_Text): Document.Node =
   409   {
   410     edit match {
   411       case (_, Document.Node.Clear()) => node.clear
   412 
   413       case (_, Document.Node.Blob()) => node.init_blob
   414 
   415       case (name, Document.Node.Edits(text_edits)) =>
   416         if (node.is_blob) node
   417         else {
   418           val commands0 = node.commands
   419           val commands1 = edit_text(text_edits, commands0)
   420           val commands2 =
   421             recover_spans(thy_load, syntax, doc_blobs, name, node.perspective.visible, commands1)
   422           node.update_commands(commands2)
   423         }
   424 
   425       case (_, Document.Node.Deps(_)) => node
   426 
   427       case (name, Document.Node.Perspective(required, text_perspective, overlays)) =>
   428         val (visible, visible_overlay) = command_perspective(node, text_perspective, overlays)
   429         val perspective: Document.Node.Perspective_Command =
   430           Document.Node.Perspective(required, visible_overlay, overlays)
   431         if (node.same_perspective(perspective)) node
   432         else
   433           node.update_perspective(perspective).update_commands(
   434             consolidate_spans(thy_load, syntax, doc_blobs, reparse_limit,
   435               name, visible, node.commands))
   436     }
   437   }
   438 
   439   def text_edits(
   440       thy_load: Thy_Load,
   441       reparse_limit: Int,
   442       previous: Document.Version,
   443       doc_blobs: Document.Blobs,
   444       edits: List[Document.Edit_Text])
   445     : (Boolean, List[Document.Edit_Command], Document.Version) =
   446   {
   447     val ((syntax, syntax_changed), reparse0, nodes0, doc_edits0) =
   448       header_edits(thy_load.base_syntax, previous, edits)
   449 
   450     if (edits.isEmpty)
   451       (false, Nil, Document.Version.make(syntax, previous.nodes))
   452     else {
   453       val reparse =
   454         (reparse0 /: nodes0.entries)({
   455           case (reparse, (name, node)) =>
   456             if (node.thy_load_commands.isEmpty) reparse
   457             else name :: reparse
   458           })
   459       val reparse_set = reparse.toSet
   460 
   461       var nodes = nodes0
   462       val doc_edits = new mutable.ListBuffer[Document.Edit_Command]; doc_edits ++= doc_edits0
   463 
   464       val node_edits =
   465         (edits ::: reparse.map((_, Document.Node.Edits(Nil)))).groupBy(_._1)
   466           .asInstanceOf[Map[Document.Node.Name, List[Document.Edit_Text]]]  // FIXME ???
   467 
   468       node_edits foreach {
   469         case (name, edits) =>
   470           val node = nodes(name)
   471           val commands = node.commands
   472 
   473           val node1 =
   474             if (reparse_set(name) && !commands.isEmpty)
   475               node.update_commands(
   476                 reparse_spans(thy_load, syntax, doc_blobs,
   477                   name, commands, commands.head, commands.last))
   478             else node
   479           val node2 = (node1 /: edits)(text_edit(thy_load, syntax, doc_blobs, reparse_limit, _, _))
   480 
   481           if (!(node.same_perspective(node2.perspective)))
   482             doc_edits += (name -> node2.perspective)
   483 
   484           doc_edits += (name -> Document.Node.Edits(diff_commands(commands, node2.commands)))
   485 
   486           nodes += (name -> node2)
   487       }
   488 
   489       (syntax_changed, doc_edits.toList, Document.Version.make(syntax, nodes))
   490     }
   491   }
   492 }