src/Pure/Thy/thy_syntax.scala
author wenzelm
Thu Apr 03 21:08:00 2014 +0200 (2014-04-03)
changeset 56394 bbf4d512f395
parent 56393 22f533e6a049
child 56473 5b5c750e9763
permissions -rw-r--r--
clarified Version.syntax -- avoid guessing initial situation;
     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_iterator(perspective.range)
   145          else node.command_iterator()).toStream
   146       check_ranges(perspective.ranges, commands)
   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     resources: Resources,
   157     previous: Document.Version,
   158     edits: List[Document.Edit_Text]):
   159     (Prover.Syntax, Boolean, Boolean, List[Document.Node.Name], Document.Nodes,
   160       List[Document.Edit_Command]) =
   161   {
   162     var updated_imports = false
   163     var updated_keywords = false
   164     var nodes = previous.nodes
   165     val doc_edits = new mutable.ListBuffer[Document.Edit_Command]
   166 
   167     edits foreach {
   168       case (name, Document.Node.Deps(header)) =>
   169         val node = nodes(name)
   170         val update_header =
   171           !node.header.errors.isEmpty || !header.errors.isEmpty || node.header != header
   172         if (update_header) {
   173           val node1 = node.update_header(header)
   174           updated_imports = updated_imports || (node.header.imports != node1.header.imports)
   175           updated_keywords = updated_keywords || (node.header.keywords != node1.header.keywords)
   176           nodes += (name -> node1)
   177           doc_edits += (name -> Document.Node.Deps(header))
   178         }
   179       case _ =>
   180     }
   181 
   182     val (syntax, syntax_changed) =
   183       previous.syntax match {
   184         case Some(syntax) if !updated_keywords =>
   185           (syntax, false)
   186         case _ =>
   187           val syntax =
   188             (resources.base_syntax /: nodes.iterator) {
   189               case (syn, (_, node)) => syn.add_keywords(node.header.keywords)
   190             }
   191           (syntax, true)
   192       }
   193 
   194     val reparse =
   195       if (updated_imports || updated_keywords)
   196         nodes.descendants(doc_edits.iterator.map(_._1).toList)
   197       else Nil
   198 
   199     (syntax, syntax_changed, updated_imports, reparse, nodes, doc_edits.toList)
   200   }
   201 
   202 
   203 
   204   /** text edits **/
   205 
   206   /* edit individual command source */
   207 
   208   @tailrec def edit_text(eds: List[Text.Edit], commands: Linear_Set[Command]): Linear_Set[Command] =
   209   {
   210     eds match {
   211       case e :: es =>
   212         Document.Node.Commands.starts(commands.iterator).find {
   213           case (cmd, cmd_start) =>
   214             e.can_edit(cmd.source, cmd_start) ||
   215               e.is_insert && e.start == cmd_start + cmd.length
   216         } match {
   217           case Some((cmd, cmd_start)) if e.can_edit(cmd.source, cmd_start) =>
   218             val (rest, text) = e.edit(cmd.source, cmd_start)
   219             val new_commands = commands.insert_after(Some(cmd), Command.unparsed(text)) - cmd
   220             edit_text(rest.toList ::: es, new_commands)
   221 
   222           case Some((cmd, cmd_start)) =>
   223             edit_text(es, commands.insert_after(Some(cmd), Command.unparsed(e.text)))
   224 
   225           case None =>
   226             require(e.is_insert && e.start == 0)
   227             edit_text(es, commands.insert_after(None, Command.unparsed(e.text)))
   228         }
   229       case Nil => commands
   230     }
   231   }
   232 
   233 
   234   /* inlined files */
   235 
   236   private def find_file(tokens: List[Token]): Option[String] =
   237   {
   238     def clean(toks: List[Token]): List[Token] =
   239       toks match {
   240         case t :: _ :: ts if t.is_keyword && (t.source == "%" || t.source == "--") => clean(ts)
   241         case t :: ts => t :: clean(ts)
   242         case Nil => Nil
   243       }
   244     clean(tokens.filter(_.is_proper)) match {
   245       case tok :: toks if tok.is_command => toks.find(_.is_name).map(_.content)
   246       case _ => None
   247     }
   248   }
   249 
   250   def span_files(syntax: Prover.Syntax, span: List[Token]): List[String] =
   251     syntax.load(span) match {
   252       case Some(exts) =>
   253         find_file(span) match {
   254           case Some(file) =>
   255             if (exts.isEmpty) List(file)
   256             else exts.map(ext => file + "." + ext)
   257           case None => Nil
   258         }
   259       case None => Nil
   260     }
   261 
   262   def resolve_files(
   263       resources: Resources,
   264       syntax: Prover.Syntax,
   265       node_name: Document.Node.Name,
   266       span: List[Token],
   267       get_blob: Document.Node.Name => Option[Document.Blob])
   268     : List[Command.Blob] =
   269   {
   270     span_files(syntax, span).map(file_name =>
   271       Exn.capture {
   272         val name =
   273           Document.Node.Name(resources.append(node_name.master_dir, Path.explode(file_name)))
   274         val blob = get_blob(name).map(blob => ((blob.bytes.sha1_digest, blob.file)))
   275         (name, blob)
   276       })
   277   }
   278 
   279 
   280   /* reparse range of command spans */
   281 
   282   @tailrec private def chop_common(
   283       cmds: List[Command],
   284       blobs_spans: List[(List[Command.Blob], List[Token])])
   285     : (List[Command], List[(List[Command.Blob], List[Token])]) =
   286   {
   287     (cmds, blobs_spans) match {
   288       case (cmd :: cmds, (blobs, span) :: rest) if cmd.blobs == blobs && cmd.span == span =>
   289         chop_common(cmds, rest)
   290       case _ => (cmds, blobs_spans)
   291     }
   292   }
   293 
   294   private def reparse_spans(
   295     resources: Resources,
   296     syntax: Prover.Syntax,
   297     get_blob: Document.Node.Name => Option[Document.Blob],
   298     name: Document.Node.Name,
   299     commands: Linear_Set[Command],
   300     first: Command, last: Command): Linear_Set[Command] =
   301   {
   302     val cmds0 = commands.iterator(first, last).toList
   303     val blobs_spans0 =
   304       parse_spans(syntax.scan(cmds0.iterator.map(_.source).mkString)).
   305         map(span => (resolve_files(resources, syntax, name, span, get_blob), span))
   306 
   307     val (cmds1, blobs_spans1) = chop_common(cmds0, blobs_spans0)
   308 
   309     val (rev_cmds2, rev_blobs_spans2) = chop_common(cmds1.reverse, blobs_spans1.reverse)
   310     val cmds2 = rev_cmds2.reverse
   311     val blobs_spans2 = rev_blobs_spans2.reverse
   312 
   313     cmds2 match {
   314       case Nil =>
   315         assert(blobs_spans2.isEmpty)
   316         commands
   317       case cmd :: _ =>
   318         val hook = commands.prev(cmd)
   319         val inserted =
   320           blobs_spans2.map({ case (blobs, span) => Command(Document_ID.make(), name, blobs, span) })
   321         (commands /: cmds2)(_ - _).append_after(hook, inserted)
   322     }
   323   }
   324 
   325 
   326   /* recover command spans after edits */
   327 
   328   // FIXME somewhat slow
   329   private def recover_spans(
   330     resources: Resources,
   331     syntax: Prover.Syntax,
   332     get_blob: Document.Node.Name => Option[Document.Blob],
   333     name: Document.Node.Name,
   334     perspective: Command.Perspective,
   335     commands: Linear_Set[Command]): Linear_Set[Command] =
   336   {
   337     val visible = perspective.commands.toSet
   338 
   339     def next_invisible_command(cmds: Linear_Set[Command], from: Command): Command =
   340       cmds.iterator(from).dropWhile(cmd => !cmd.is_command || visible(cmd))
   341         .find(_.is_command) getOrElse cmds.last
   342 
   343     @tailrec def recover(cmds: Linear_Set[Command]): Linear_Set[Command] =
   344       cmds.find(_.is_unparsed) match {
   345         case Some(first_unparsed) =>
   346           val first = next_invisible_command(cmds.reverse, first_unparsed)
   347           val last = next_invisible_command(cmds, first_unparsed)
   348           recover(reparse_spans(resources, syntax, get_blob, name, cmds, first, last))
   349         case None => cmds
   350       }
   351     recover(commands)
   352   }
   353 
   354 
   355   /* consolidate unfinished spans */
   356 
   357   private def consolidate_spans(
   358     resources: Resources,
   359     syntax: Prover.Syntax,
   360     get_blob: Document.Node.Name => Option[Document.Blob],
   361     reparse_limit: Int,
   362     name: Document.Node.Name,
   363     perspective: Command.Perspective,
   364     commands: Linear_Set[Command]): Linear_Set[Command] =
   365   {
   366     if (perspective.commands.isEmpty) commands
   367     else {
   368       commands.find(_.is_unfinished) match {
   369         case Some(first_unfinished) =>
   370           val visible = perspective.commands.toSet
   371           commands.reverse.find(visible) match {
   372             case Some(last_visible) =>
   373               val it = commands.iterator(last_visible)
   374               var last = last_visible
   375               var i = 0
   376               while (i < reparse_limit && it.hasNext) {
   377                 last = it.next
   378                 i += last.length
   379               }
   380               reparse_spans(resources, syntax, get_blob, name, commands, first_unfinished, last)
   381             case None => commands
   382           }
   383         case None => commands
   384       }
   385     }
   386   }
   387 
   388 
   389   /* main */
   390 
   391   def diff_commands(old_cmds: Linear_Set[Command], new_cmds: Linear_Set[Command])
   392     : List[Command.Edit] =
   393   {
   394     val removed = old_cmds.iterator.filter(!new_cmds.contains(_)).toList
   395     val inserted = new_cmds.iterator.filter(!old_cmds.contains(_)).toList
   396 
   397     removed.reverse.map(cmd => (old_cmds.prev(cmd), None)) :::
   398     inserted.map(cmd => (new_cmds.prev(cmd), Some(cmd)))
   399   }
   400 
   401   private def text_edit(
   402     resources: Resources,
   403     syntax: Prover.Syntax,
   404     get_blob: Document.Node.Name => Option[Document.Blob],
   405     reparse_limit: Int,
   406     node: Document.Node, edit: Document.Edit_Text): Document.Node =
   407   {
   408     edit match {
   409       case (_, Document.Node.Clear()) => node.clear
   410 
   411       case (_, Document.Node.Blob(blob)) => node.init_blob(blob)
   412 
   413       case (name, Document.Node.Edits(text_edits)) =>
   414         if (name.is_theory) {
   415           val commands0 = node.commands
   416           val commands1 = edit_text(text_edits, commands0)
   417           val commands2 =
   418             recover_spans(resources, syntax, get_blob, name, node.perspective.visible, commands1)
   419           node.update_commands(commands2)
   420         }
   421         else node
   422 
   423       case (_, Document.Node.Deps(_)) => node
   424 
   425       case (name, Document.Node.Perspective(required, text_perspective, overlays)) =>
   426         val (visible, visible_overlay) = command_perspective(node, text_perspective, overlays)
   427         val perspective: Document.Node.Perspective_Command =
   428           Document.Node.Perspective(required, visible_overlay, overlays)
   429         if (node.same_perspective(perspective)) node
   430         else
   431           node.update_perspective(perspective).update_commands(
   432             consolidate_spans(resources, syntax, get_blob, reparse_limit,
   433               name, visible, node.commands))
   434     }
   435   }
   436 
   437   def parse_change(
   438       resources: Resources,
   439       reparse_limit: Int,
   440       previous: Document.Version,
   441       doc_blobs: Document.Blobs,
   442       edits: List[Document.Edit_Text]): Session.Change =
   443   {
   444     def get_blob(name: Document.Node.Name) =
   445       doc_blobs.get(name) orElse previous.nodes(name).get_blob
   446 
   447     val (syntax, syntax_changed, deps_changed, reparse0, nodes0, doc_edits0) =
   448       header_edits(resources, previous, edits)
   449 
   450     val (doc_edits, version) =
   451       if (edits.isEmpty) (Nil, Document.Version.make(Some(syntax), previous.nodes))
   452       else {
   453         val reparse =
   454           (reparse0 /: nodes0.iterator)({
   455             case (reparse, (name, node)) =>
   456               if (node.load_commands.exists(_.blobs_changed(doc_blobs)))
   457                 name :: reparse
   458               else reparse
   459             })
   460         val reparse_set = reparse.toSet
   461 
   462         var nodes = nodes0
   463         val doc_edits = new mutable.ListBuffer[Document.Edit_Command]; doc_edits ++= doc_edits0
   464 
   465         val node_edits =
   466           (edits ::: reparse.map((_, Document.Node.Edits(Nil)))).groupBy(_._1)
   467             .asInstanceOf[Map[Document.Node.Name, List[Document.Edit_Text]]]  // FIXME ???
   468 
   469         node_edits foreach {
   470           case (name, edits) =>
   471             val node = nodes(name)
   472             val commands = node.commands
   473 
   474             val node1 =
   475               if (reparse_set(name) && !commands.isEmpty)
   476                 node.update_commands(
   477                   reparse_spans(resources, syntax, get_blob,
   478                     name, commands, commands.head, commands.last))
   479               else node
   480             val node2 =
   481               (node1 /: edits)(text_edit(resources, syntax, get_blob, reparse_limit, _, _))
   482 
   483             if (!(node.same_perspective(node2.perspective)))
   484               doc_edits += (name -> node2.perspective)
   485 
   486             doc_edits += (name -> Document.Node.Edits(diff_commands(commands, node2.commands)))
   487 
   488             nodes += (name -> node2)
   489         }
   490         (doc_edits.toList, Document.Version.make(Some(syntax), nodes))
   491       }
   492 
   493     Session.Change(previous, doc_blobs, syntax_changed, deps_changed, doc_edits, version)
   494   }
   495 }