1 /* Title: Pure/Thy/thy_syntax.scala
4 Superficial theory syntax: tokens and spans.
10 import scala.collection.mutable
11 import scala.annotation.tailrec
16 /** nested structure **/
20 sealed abstract class Entry { def length: Int }
21 case class Block(val name: String, val body: List[Entry]) extends Entry
23 val length: Int = (0 /: body)(_ + _.length)
25 case class Atom(val command: Command) extends Entry
27 def length: Int = command.length
30 def parse(syntax: Outer_Syntax, root_name: String, text: CharSequence): Entry =
32 /* stack operations */
34 def buffer(): mutable.ListBuffer[Entry] = new mutable.ListBuffer[Entry]
35 var stack: List[(Int, String, mutable.ListBuffer[Entry])] = List((0, root_name, buffer()))
37 @tailrec def close(level: Int => Boolean)
40 case (lev, name, body) :: (_, _, body2) :: rest if level(lev) =>
41 body2 += Block(name, body.toList)
51 val (_, name, body) = stack.head
52 Block(name, body.toList)
55 def add(command: Command)
57 syntax.heading_level(command) match {
60 stack = (i, command.source, buffer()) :: stack
63 stack.head._3 += Atom(command)
67 /* result structure */
69 val spans = parse_spans(syntax.scan(text))
70 spans.foreach(span => add(Command.span(span)))
79 def parse_spans(toks: List[Token]): List[List[Token]] =
81 val result = new mutable.ListBuffer[List[Token]]
82 val span = new mutable.ListBuffer[Token]
83 val whitespace = new mutable.ListBuffer[Token]
85 def flush(buffer: mutable.ListBuffer[Token])
87 if (!buffer.isEmpty) { result += buffer.toList; buffer.clear }
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 }
94 flush(span); flush(whitespace)
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) =
105 /* phase 1: edit individual command source */
107 @tailrec def edit_text(eds: List[Text.Edit], commands: Linear_Set[Command])
108 : Linear_Set[Command] =
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
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)
122 case Some((cmd, cmd_start)) =>
123 edit_text(es, commands.insert_after(Some(cmd), Command.unparsed(e.text)))
126 require(e.is_insert && e.start == 0)
127 edit_text(es, commands.insert_after(None, Command.unparsed(e.text)))
134 /* phase 2: recover command spans */
136 @tailrec def recover_spans(commands: Linear_Set[Command]): Linear_Set[Command] =
138 commands.iterator.find(_.is_unparsed) match {
139 case Some(first_unparsed) =>
141 commands.reverse_iterator(first_unparsed).
142 dropWhile(_.newlines == 0).find(_.is_command) getOrElse commands.head
144 commands.iterator(first_unparsed).
145 dropWhile(_.newlines == 0).find(_.is_command) getOrElse commands.last
147 commands.iterator(first).takeWhile(_ != last).toList ::: List(last)
149 val sources = range.flatMap(_.span.map(_.source))
150 val spans0 = parse_spans(syntax.scan(sources.mkString))
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)
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)
162 val inserted = spans2.map(span => new Command(new_id(), span))
164 commands.delete_between(before_edit, after_edit).append_after(before_edit, inserted)
165 recover_spans(new_commands)
167 case None => commands
172 /* resulting document edits */
175 val doc_edits = new mutable.ListBuffer[Document.Edit_Command]
176 var nodes = previous.nodes
180 doc_edits += (name -> None)
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
188 val removed_commands = commands0.iterator.filter(!commands2.contains(_)).toList
189 val inserted_commands = commands2.iterator.filter(!commands0.contains(_)).toList
192 removed_commands.reverse.map(cmd => (commands0.prev(cmd), None)) :::
193 inserted_commands.map(cmd => (commands2.prev(cmd), Some(cmd)))
195 doc_edits += (name -> Some(cmd_edits))
196 nodes += (name -> new Document.Node(commands2))
198 (doc_edits.toList, new Document.Version(new_id(), nodes))