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