src/Pure/Thy/thy_syntax.scala
author wenzelm
Sat Jan 25 15:29:40 2014 +0100 (2014-01-25)
changeset 55134 1b67b17cdad5
parent 55118 7df949045dc5
child 55431 e0f20a44ff9d
permissions -rw-r--r--
propagate update of outer syntax keywords: global propertiesChanged, buffer TokenMarker.markTokens, text area repainting;
     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 =>
   268       Exn.capture {
   269         val name =
   270           Document.Node.Name(thy_load.append(node_name.master_dir, Path.explode(file)))
   271         (name, doc_blobs.get(name).map(_.sha1_digest))
   272       }
   273     )
   274   }
   275 
   276 
   277   /* reparse range of command spans */
   278 
   279   @tailrec private def chop_common(
   280       cmds: List[Command], spans: List[(List[Command.Blob], List[Token])])
   281       : (List[Command], List[(List[Command.Blob], List[Token])]) =
   282     (cmds, spans) match {
   283       case (c :: cs, (blobs, span) :: ps) if c.blobs == blobs && c.span == span =>
   284         chop_common(cs, ps)
   285       case _ => (cmds, spans)
   286     }
   287 
   288   private def reparse_spans(
   289     thy_load: Thy_Load,
   290     syntax: Outer_Syntax,
   291     doc_blobs: Document.Blobs,
   292     name: Document.Node.Name,
   293     commands: Linear_Set[Command],
   294     first: Command, last: Command): Linear_Set[Command] =
   295   {
   296     val cmds0 = commands.iterator(first, last).toList
   297     val spans0 =
   298       parse_spans(syntax.scan(cmds0.iterator.map(_.source).mkString)).
   299         map(span => (resolve_files(thy_load, syntax, name, span, doc_blobs), span))
   300 
   301     val (cmds1, spans1) = chop_common(cmds0, spans0)
   302 
   303     val (rev_cmds2, rev_spans2) = chop_common(cmds1.reverse, spans1.reverse)
   304     val cmds2 = rev_cmds2.reverse
   305     val spans2 = rev_spans2.reverse
   306 
   307     cmds2 match {
   308       case Nil =>
   309         assert(spans2.isEmpty)
   310         commands
   311       case cmd :: _ =>
   312         val hook = commands.prev(cmd)
   313         val inserted =
   314           spans2.map({ case (blobs, span) => Command(Document_ID.make(), name, blobs, span) })
   315         (commands /: cmds2)(_ - _).append_after(hook, inserted)
   316     }
   317   }
   318 
   319 
   320   /* recover command spans after edits */
   321 
   322   // FIXME somewhat slow
   323   private def recover_spans(
   324     thy_load: Thy_Load,
   325     syntax: Outer_Syntax,
   326     doc_blobs: Document.Blobs,
   327     name: Document.Node.Name,
   328     perspective: Command.Perspective,
   329     commands: Linear_Set[Command]): Linear_Set[Command] =
   330   {
   331     val visible = perspective.commands.toSet
   332 
   333     def next_invisible_command(cmds: Linear_Set[Command], from: Command): Command =
   334       cmds.iterator(from).dropWhile(cmd => !cmd.is_command || visible(cmd))
   335         .find(_.is_command) getOrElse cmds.last
   336 
   337     @tailrec def recover(cmds: Linear_Set[Command]): Linear_Set[Command] =
   338       cmds.find(_.is_unparsed) match {
   339         case Some(first_unparsed) =>
   340           val first = next_invisible_command(cmds.reverse, first_unparsed)
   341           val last = next_invisible_command(cmds, first_unparsed)
   342           recover(reparse_spans(thy_load, syntax, doc_blobs, name, cmds, first, last))
   343         case None => cmds
   344       }
   345     recover(commands)
   346   }
   347 
   348 
   349   /* consolidate unfinished spans */
   350 
   351   private def consolidate_spans(
   352     thy_load: Thy_Load,
   353     syntax: Outer_Syntax,
   354     doc_blobs: Document.Blobs,
   355     reparse_limit: Int,
   356     name: Document.Node.Name,
   357     perspective: Command.Perspective,
   358     commands: Linear_Set[Command]): Linear_Set[Command] =
   359   {
   360     if (perspective.commands.isEmpty) commands
   361     else {
   362       commands.find(_.is_unfinished) match {
   363         case Some(first_unfinished) =>
   364           val visible = perspective.commands.toSet
   365           commands.reverse.find(visible) match {
   366             case Some(last_visible) =>
   367               val it = commands.iterator(last_visible)
   368               var last = last_visible
   369               var i = 0
   370               while (i < reparse_limit && it.hasNext) {
   371                 last = it.next
   372                 i += last.length
   373               }
   374               reparse_spans(thy_load, syntax, doc_blobs, name, commands, first_unfinished, last)
   375             case None => commands
   376           }
   377         case None => commands
   378       }
   379     }
   380   }
   381 
   382 
   383   /* main */
   384 
   385   def diff_commands(old_cmds: Linear_Set[Command], new_cmds: Linear_Set[Command])
   386     : List[Command.Edit] =
   387   {
   388     val removed = old_cmds.iterator.filter(!new_cmds.contains(_)).toList
   389     val inserted = new_cmds.iterator.filter(!old_cmds.contains(_)).toList
   390 
   391     removed.reverse.map(cmd => (old_cmds.prev(cmd), None)) :::
   392     inserted.map(cmd => (new_cmds.prev(cmd), Some(cmd)))
   393   }
   394 
   395   private def text_edit(
   396     thy_load: Thy_Load,
   397     syntax: Outer_Syntax,
   398     doc_blobs: Document.Blobs,
   399     reparse_limit: Int,
   400     node: Document.Node, edit: Document.Edit_Text): Document.Node =
   401   {
   402     edit match {
   403       case (_, Document.Node.Clear()) => node.clear
   404 
   405       case (_, Document.Node.Blob()) => node
   406 
   407       case (name, Document.Node.Edits(text_edits)) =>
   408         val commands0 = node.commands
   409         val commands1 = edit_text(text_edits, commands0)
   410         val commands2 =
   411           recover_spans(thy_load, syntax, doc_blobs, name, node.perspective.visible, commands1)
   412         node.update_commands(commands2)
   413 
   414       case (_, Document.Node.Deps(_)) => node
   415 
   416       case (name, Document.Node.Perspective(required, text_perspective, overlays)) =>
   417         val (visible, visible_overlay) = command_perspective(node, text_perspective, overlays)
   418         val perspective: Document.Node.Perspective_Command =
   419           Document.Node.Perspective(required, visible_overlay, overlays)
   420         if (node.same_perspective(perspective)) node
   421         else
   422           node.update_perspective(perspective).update_commands(
   423             consolidate_spans(thy_load, syntax, doc_blobs, reparse_limit,
   424               name, visible, node.commands))
   425     }
   426   }
   427 
   428   def text_edits(
   429       thy_load: Thy_Load,
   430       reparse_limit: Int,
   431       previous: Document.Version,
   432       doc_blobs: Document.Blobs,
   433       edits: List[Document.Edit_Text])
   434     : (Boolean, List[Document.Edit_Command], Document.Version) =
   435   {
   436     val ((syntax, syntax_changed), reparse0, nodes0, doc_edits0) =
   437       header_edits(thy_load.base_syntax, previous, edits)
   438 
   439     if (edits.isEmpty)
   440       (false, Nil, Document.Version.make(syntax, previous.nodes))
   441     else {
   442       val reparse =
   443         (reparse0 /: nodes0.entries)({
   444           case (reparse, (name, node)) =>
   445             if (node.thy_load_commands.isEmpty) reparse
   446             else name :: reparse
   447           })
   448       val reparse_set = reparse.toSet
   449 
   450       var nodes = nodes0
   451       val doc_edits = new mutable.ListBuffer[Document.Edit_Command]; doc_edits ++= doc_edits0
   452 
   453       val node_edits =
   454         (edits ::: reparse.map((_, Document.Node.Edits(Nil)))).groupBy(_._1)
   455           .asInstanceOf[Map[Document.Node.Name, List[Document.Edit_Text]]]  // FIXME ???
   456 
   457       node_edits foreach {
   458         case (name, edits) =>
   459           val node = nodes(name)
   460           val commands = node.commands
   461 
   462           val node1 =
   463             if (reparse_set(name) && !commands.isEmpty)
   464               node.update_commands(
   465                 reparse_spans(thy_load, syntax, doc_blobs,
   466                   name, commands, commands.head, commands.last))
   467             else node
   468           val node2 = (node1 /: edits)(text_edit(thy_load, syntax, doc_blobs, reparse_limit, _, _))
   469 
   470           if (!(node.same_perspective(node2.perspective)))
   471             doc_edits += (name -> node2.perspective)
   472 
   473           doc_edits += (name -> Document.Node.Edits(diff_commands(commands, node2.commands)))
   474 
   475           nodes += (name -> node2)
   476       }
   477 
   478       (syntax_changed, doc_edits.toList, Document.Version.make(syntax, nodes))
   479     }
   480   }
   481 }