src/Pure/Thy/thy_syntax.scala
author wenzelm
Sun, 03 Jul 2011 15:10:17 +0200
changeset 44524 42b98a59ec30
parent 41044 1d71a45590e4
child 44531 bfc0bb115fa1
permissions -rw-r--r--
tuned signature;
     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, root_name: String, 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])] = List((0, root_name, buffer()))
    36 
    37       @tailrec def close(level: Int => Boolean)
    38       {
    39         stack match {
    40           case (lev, name, body) :: (_, _, body2) :: rest if level(lev) =>
    41             body2 += Block(name, body.toList)
    42             stack = stack.tail
    43             close(level)
    44           case _ =>
    45         }
    46       }
    47 
    48       def result(): Entry =
    49       {
    50         close(_ => true)
    51         val (_, name, body) = stack.head
    52         Block(name, body.toList)
    53       }
    54 
    55       def add(command: Command)
    56       {
    57         syntax.heading_level(command) match {
    58           case Some(i) =>
    59             close(_ >= i)
    60             stack = (i, command.source, buffer()) :: stack
    61           case None =>
    62         }
    63         stack.head._3 += Atom(command)
    64       }
    65 
    66 
    67       /* result structure */
    68 
    69       val spans = parse_spans(syntax.scan(text))
    70       spans.foreach(span => add(Command.span(span)))
    71       result()
    72     }
    73   }
    74 
    75 
    76 
    77   /** parse spans **/
    78 
    79   def parse_spans(toks: List[Token]): List[List[Token]] =
    80   {
    81     val result = new mutable.ListBuffer[List[Token]]
    82     val span = new mutable.ListBuffer[Token]
    83     val whitespace = new mutable.ListBuffer[Token]
    84 
    85     def flush(buffer: mutable.ListBuffer[Token])
    86     {
    87       if (!buffer.isEmpty) { result += buffer.toList; buffer.clear }
    88     }
    89     for (tok <- toks) {
    90       if (tok.is_command) { flush(span); flush(whitespace); span += tok }
    91       else if (tok.is_ignored) whitespace += tok
    92       else { span ++= whitespace; whitespace.clear; span += tok }
    93     }
    94     flush(span); flush(whitespace)
    95     result.toList
    96   }
    97 
    98 
    99 
   100   /** text edits **/
   101 
   102   def text_edits(syntax: Outer_Syntax, new_id: () => Document.ID, previous: Document.Version,
   103       edits: List[Document.Edit_Text]): (List[Document.Edit_Command], Document.Version) =
   104   {
   105     /* phase 1: edit individual command source */
   106 
   107     @tailrec def edit_text(eds: List[Text.Edit], commands: Linear_Set[Command])
   108         : Linear_Set[Command] =
   109     {
   110       eds match {
   111         case e :: es =>
   112           Document.Node.command_starts(commands.iterator).find {
   113             case (cmd, cmd_start) =>
   114               e.can_edit(cmd.source, cmd_start) ||
   115                 e.is_insert && e.start == cmd_start + cmd.length
   116           } match {
   117             case Some((cmd, cmd_start)) if e.can_edit(cmd.source, cmd_start) =>
   118               val (rest, text) = e.edit(cmd.source, cmd_start)
   119               val new_commands = commands.insert_after(Some(cmd), Command.unparsed(text)) - cmd
   120               edit_text(rest.toList ::: es, new_commands)
   121 
   122             case Some((cmd, cmd_start)) =>
   123               edit_text(es, commands.insert_after(Some(cmd), Command.unparsed(e.text)))
   124 
   125             case None =>
   126               require(e.is_insert && e.start == 0)
   127               edit_text(es, commands.insert_after(None, Command.unparsed(e.text)))
   128           }
   129         case Nil => commands
   130       }
   131     }
   132 
   133 
   134     /* phase 2: recover command spans */
   135 
   136     @tailrec def recover_spans(commands: Linear_Set[Command]): Linear_Set[Command] =
   137     {
   138       commands.iterator.find(_.is_unparsed) match {
   139         case Some(first_unparsed) =>
   140           val first =
   141             commands.reverse_iterator(first_unparsed).
   142               dropWhile(_.newlines == 0).find(_.is_command) getOrElse commands.head
   143           val last =
   144             commands.iterator(first_unparsed).
   145               dropWhile(_.newlines == 0).find(_.is_command) getOrElse commands.last
   146           val range =
   147             commands.iterator(first).takeWhile(_ != last).toList ::: List(last)
   148 
   149           val sources = range.flatMap(_.span.map(_.source))
   150           val spans0 = parse_spans(syntax.scan(sources.mkString))
   151 
   152           val (before_edit, spans1) =
   153             if (!spans0.isEmpty && first.is_command && first.span == spans0.head)
   154               (Some(first), spans0.tail)
   155             else (commands.prev(first), spans0)
   156 
   157           val (after_edit, spans2) =
   158             if (!spans1.isEmpty && last.is_command && last.span == spans1.last)
   159               (Some(last), spans1.take(spans1.length - 1))
   160             else (commands.next(last), spans1)
   161 
   162           val inserted = spans2.map(span => new Command(new_id(), span))
   163           val new_commands =
   164             commands.delete_between(before_edit, after_edit).append_after(before_edit, inserted)
   165           recover_spans(new_commands)
   166 
   167         case None => commands
   168       }
   169     }
   170 
   171 
   172     /* resulting document edits */
   173 
   174     {
   175       val doc_edits = new mutable.ListBuffer[Document.Edit_Command]
   176       var nodes = previous.nodes
   177 
   178       edits foreach {
   179         case (name, None) =>
   180           doc_edits += (name -> None)
   181           nodes -= name
   182 
   183         case (name, Some(text_edits)) =>
   184           val commands0 = nodes(name).commands
   185           val commands1 = edit_text(text_edits, commands0)
   186           val commands2 = recover_spans(commands1)   // FIXME somewhat slow
   187 
   188           val removed_commands = commands0.iterator.filter(!commands2.contains(_)).toList
   189           val inserted_commands = commands2.iterator.filter(!commands0.contains(_)).toList
   190 
   191           val cmd_edits =
   192             removed_commands.reverse.map(cmd => (commands0.prev(cmd), None)) :::
   193             inserted_commands.map(cmd => (commands2.prev(cmd), Some(cmd)))
   194 
   195           doc_edits += (name -> Some(cmd_edits))
   196           nodes += (name -> new Document.Node(commands2))
   197       }
   198       (doc_edits.toList, new Document.Version(new_id(), nodes))
   199     }
   200   }
   201 }