| author | haftmann | 
| Fri, 03 Sep 2010 16:08:09 +0200 | |
| changeset 39121 | 6f6a9c8abbac | 
| parent 38878 | 1d5b3175fd30 | 
| child 40454 | 2516ea25a54b | 
| permissions | -rw-r--r-- | 
| 34268 | 1  | 
/* Title: Pure/Thy/thy_syntax.scala  | 
2  | 
Author: Makarius  | 
|
3  | 
||
| 38374 | 4  | 
Superficial theory syntax: tokens and spans.  | 
| 34268 | 5  | 
*/  | 
6  | 
||
7  | 
package isabelle  | 
|
8  | 
||
9  | 
||
| 
38239
 
89a4d1028fb3
parse_spans: somewhat faster low-level implementation;
 
wenzelm 
parents: 
36956 
diff
changeset
 | 
10  | 
import scala.collection.mutable  | 
| 38374 | 11  | 
import scala.annotation.tailrec  | 
| 
38239
 
89a4d1028fb3
parse_spans: somewhat faster low-level implementation;
 
wenzelm 
parents: 
36956 
diff
changeset
 | 
12  | 
|
| 
 
89a4d1028fb3
parse_spans: somewhat faster low-level implementation;
 
wenzelm 
parents: 
36956 
diff
changeset
 | 
13  | 
|
| 34303 | 14  | 
object Thy_Syntax  | 
| 34268 | 15  | 
{
 | 
| 38374 | 16  | 
/** parse spans **/  | 
17  | 
||
| 38373 | 18  | 
def parse_spans(toks: List[Token]): List[List[Token]] =  | 
| 34268 | 19  | 
  {
 | 
| 38373 | 20  | 
val result = new mutable.ListBuffer[List[Token]]  | 
| 
38239
 
89a4d1028fb3
parse_spans: somewhat faster low-level implementation;
 
wenzelm 
parents: 
36956 
diff
changeset
 | 
21  | 
val span = new mutable.ListBuffer[Token]  | 
| 
 
89a4d1028fb3
parse_spans: somewhat faster low-level implementation;
 
wenzelm 
parents: 
36956 
diff
changeset
 | 
22  | 
val whitespace = new mutable.ListBuffer[Token]  | 
| 34268 | 23  | 
|
| 
38239
 
89a4d1028fb3
parse_spans: somewhat faster low-level implementation;
 
wenzelm 
parents: 
36956 
diff
changeset
 | 
24  | 
def flush(buffer: mutable.ListBuffer[Token])  | 
| 
 
89a4d1028fb3
parse_spans: somewhat faster low-level implementation;
 
wenzelm 
parents: 
36956 
diff
changeset
 | 
25  | 
    {
 | 
| 
 
89a4d1028fb3
parse_spans: somewhat faster low-level implementation;
 
wenzelm 
parents: 
36956 
diff
changeset
 | 
26  | 
      if (!buffer.isEmpty) { result += buffer.toList; buffer.clear }
 | 
| 34268 | 27  | 
}  | 
| 
38239
 
89a4d1028fb3
parse_spans: somewhat faster low-level implementation;
 
wenzelm 
parents: 
36956 
diff
changeset
 | 
28  | 
    for (tok <- toks) {
 | 
| 
 
89a4d1028fb3
parse_spans: somewhat faster low-level implementation;
 
wenzelm 
parents: 
36956 
diff
changeset
 | 
29  | 
      if (tok.is_command) { flush(span); flush(whitespace); span += tok }
 | 
| 
 
89a4d1028fb3
parse_spans: somewhat faster low-level implementation;
 
wenzelm 
parents: 
36956 
diff
changeset
 | 
30  | 
else if (tok.is_ignored) whitespace += tok  | 
| 
 
89a4d1028fb3
parse_spans: somewhat faster low-level implementation;
 
wenzelm 
parents: 
36956 
diff
changeset
 | 
31  | 
      else { span ++= whitespace; whitespace.clear; span += tok }
 | 
| 
 
89a4d1028fb3
parse_spans: somewhat faster low-level implementation;
 
wenzelm 
parents: 
36956 
diff
changeset
 | 
32  | 
}  | 
| 
 
89a4d1028fb3
parse_spans: somewhat faster low-level implementation;
 
wenzelm 
parents: 
36956 
diff
changeset
 | 
33  | 
flush(span); flush(whitespace)  | 
| 
 
89a4d1028fb3
parse_spans: somewhat faster low-level implementation;
 
wenzelm 
parents: 
36956 
diff
changeset
 | 
34  | 
result.toList  | 
| 34268 | 35  | 
}  | 
| 38374 | 36  | 
|
37  | 
||
38  | 
||
39  | 
/** text edits **/  | 
|
40  | 
||
| 38417 | 41  | 
def text_edits(session: Session, previous: Document.Version,  | 
42  | 
edits: List[Document.Node_Text_Edit]): (List[Document.Edit[Command]], Document.Version) =  | 
|
| 38374 | 43  | 
  {
 | 
44  | 
/* phase 1: edit individual command source */  | 
|
45  | 
||
| 38425 | 46  | 
@tailrec def edit_text(eds: List[Text.Edit], commands: Linear_Set[Command])  | 
| 38374 | 47  | 
: Linear_Set[Command] =  | 
48  | 
    {
 | 
|
49  | 
      eds match {
 | 
|
50  | 
case e :: es =>  | 
|
51  | 
          Document.Node.command_starts(commands.iterator).find {
 | 
|
52  | 
case (cmd, cmd_start) =>  | 
|
53  | 
e.can_edit(cmd.source, cmd_start) ||  | 
|
54  | 
e.is_insert && e.start == cmd_start + cmd.length  | 
|
55  | 
          } match {
 | 
|
56  | 
case Some((cmd, cmd_start)) if e.can_edit(cmd.source, cmd_start) =>  | 
|
57  | 
val (rest, text) = e.edit(cmd.source, cmd_start)  | 
|
58  | 
val new_commands = commands.insert_after(Some(cmd), Command.unparsed(text)) - cmd  | 
|
59  | 
edit_text(rest.toList ::: es, new_commands)  | 
|
60  | 
||
61  | 
case Some((cmd, cmd_start)) =>  | 
|
62  | 
edit_text(es, commands.insert_after(Some(cmd), Command.unparsed(e.text)))  | 
|
63  | 
||
64  | 
case None =>  | 
|
65  | 
require(e.is_insert && e.start == 0)  | 
|
66  | 
edit_text(es, commands.insert_after(None, Command.unparsed(e.text)))  | 
|
67  | 
}  | 
|
68  | 
case Nil => commands  | 
|
69  | 
}  | 
|
70  | 
}  | 
|
71  | 
||
72  | 
||
73  | 
/* phase 2: recover command spans */  | 
|
74  | 
||
75  | 
@tailrec def recover_spans(commands: Linear_Set[Command]): Linear_Set[Command] =  | 
|
76  | 
    {
 | 
|
77  | 
      commands.iterator.find(_.is_unparsed) match {
 | 
|
78  | 
case Some(first_unparsed) =>  | 
|
79  | 
val first =  | 
|
| 
38878
 
1d5b3175fd30
text_edits/recover_spans: reparse at least until line boundary -- increases chance of recovery for bad ML text, for example;
 
wenzelm 
parents: 
38569 
diff
changeset
 | 
80  | 
commands.reverse_iterator(first_unparsed).  | 
| 
 
1d5b3175fd30
text_edits/recover_spans: reparse at least until line boundary -- increases chance of recovery for bad ML text, for example;
 
wenzelm 
parents: 
38569 
diff
changeset
 | 
81  | 
dropWhile(_.newlines == 0).find(_.is_command) getOrElse commands.head  | 
| 38374 | 82  | 
val last =  | 
| 
38878
 
1d5b3175fd30
text_edits/recover_spans: reparse at least until line boundary -- increases chance of recovery for bad ML text, for example;
 
wenzelm 
parents: 
38569 
diff
changeset
 | 
83  | 
commands.iterator(first_unparsed).  | 
| 
 
1d5b3175fd30
text_edits/recover_spans: reparse at least until line boundary -- increases chance of recovery for bad ML text, for example;
 
wenzelm 
parents: 
38569 
diff
changeset
 | 
84  | 
dropWhile(_.newlines == 0).find(_.is_command) getOrElse commands.last  | 
| 38374 | 85  | 
val range =  | 
86  | 
commands.iterator(first).takeWhile(_ != last).toList ::: List(last)  | 
|
87  | 
||
88  | 
val sources = range.flatMap(_.span.map(_.source))  | 
|
| 38569 | 89  | 
val spans0 = parse_spans(session.current_syntax().scan(sources.mkString))  | 
| 38374 | 90  | 
|
91  | 
val (before_edit, spans1) =  | 
|
92  | 
if (!spans0.isEmpty && first.is_command && first.span == spans0.head)  | 
|
93  | 
(Some(first), spans0.tail)  | 
|
94  | 
else (commands.prev(first), spans0)  | 
|
95  | 
||
96  | 
val (after_edit, spans2) =  | 
|
97  | 
if (!spans1.isEmpty && last.is_command && last.span == spans1.last)  | 
|
98  | 
(Some(last), spans1.take(spans1.length - 1))  | 
|
99  | 
else (commands.next(last), spans1)  | 
|
100  | 
||
| 38419 | 101  | 
val inserted = spans2.map(span => new Command(session.new_id(), span))  | 
| 38374 | 102  | 
val new_commands =  | 
103  | 
commands.delete_between(before_edit, after_edit).append_after(before_edit, inserted)  | 
|
104  | 
recover_spans(new_commands)  | 
|
105  | 
||
106  | 
case None => commands  | 
|
107  | 
}  | 
|
108  | 
}  | 
|
109  | 
||
110  | 
||
111  | 
/* resulting document edits */  | 
|
112  | 
||
113  | 
    {
 | 
|
114  | 
val doc_edits = new mutable.ListBuffer[Document.Edit[Command]]  | 
|
| 38417 | 115  | 
var nodes = previous.nodes  | 
| 38374 | 116  | 
|
117  | 
      for ((name, text_edits) <- edits) {
 | 
|
118  | 
val commands0 = nodes(name).commands  | 
|
119  | 
val commands1 = edit_text(text_edits, commands0)  | 
|
120  | 
val commands2 = recover_spans(commands1) // FIXME somewhat slow  | 
|
121  | 
||
122  | 
val removed_commands = commands0.iterator.filter(!commands2.contains(_)).toList  | 
|
123  | 
val inserted_commands = commands2.iterator.filter(!commands0.contains(_)).toList  | 
|
124  | 
||
125  | 
val cmd_edits =  | 
|
126  | 
removed_commands.reverse.map(cmd => (commands0.prev(cmd), None)) :::  | 
|
127  | 
inserted_commands.map(cmd => (commands2.prev(cmd), Some(cmd)))  | 
|
128  | 
||
129  | 
doc_edits += (name -> Some(cmd_edits))  | 
|
130  | 
nodes += (name -> new Document.Node(commands2))  | 
|
131  | 
}  | 
|
| 38419 | 132  | 
(doc_edits.toList, new Document.Version(session.new_id(), nodes))  | 
| 38374 | 133  | 
}  | 
134  | 
}  | 
|
| 34268 | 135  | 
}  |