author | wenzelm |
Tue, 08 Dec 2009 14:49:01 +0100 | |
changeset 34759 | bfea7839d9e1 |
parent 34741 | src/Tools/jEdit/src/proofdocument/ProofDocument.scala@4431c498726d |
child 34760 | dc7f5e0d9d27 |
permissions | -rw-r--r-- |
34407 | 1 |
/* |
34485 | 2 |
* Document as list of commands, consisting of lists of tokens |
34407 | 3 |
* |
4 |
* @author Johannes Hölzl, TU Munich |
|
34532 | 5 |
* @author Fabian Immler, TU Munich |
34485 | 6 |
* @author Makarius |
34407 | 7 |
*/ |
8 |
||
34318
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
9 |
package isabelle.proofdocument |
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
10 |
|
34703 | 11 |
import scala.actors.Actor, Actor._ |
12 |
||
34318
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
13 |
import java.util.regex.Pattern |
34703 | 14 |
|
34674 | 15 |
import isabelle.prover.{Prover, Command, Command_State} |
34318
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
16 |
|
34485 | 17 |
|
34483 | 18 |
object ProofDocument |
19 |
{ |
|
34582 | 20 |
// Be careful when changing this regex. Not only must it handle the |
34318
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
21 |
// spurious end of a token but also: |
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
22 |
// Bug ID: 5050507 Pattern.matches throws StackOverflow Error |
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
23 |
// http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5050507 |
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
24 |
|
34483 | 25 |
val token_pattern = |
34318
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
26 |
Pattern.compile( |
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
27 |
"\\{\\*([^*]|\\*[^}]|\\*\\z)*(\\z|\\*\\})|" + |
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
28 |
"\\(\\*([^*]|\\*[^)]|\\*\\z)*(\\z|\\*\\))|" + |
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
29 |
"(\\?'?|')[A-Za-z_0-9.]*|" + |
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
30 |
"[A-Za-z_0-9.]+|" + |
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
31 |
"[!#$%&*+-/<=>?@^_|~]+|" + |
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
32 |
"\"([^\\\\\"]?(\\\\(.|\\z))?)*+(\"|\\z)|" + |
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
33 |
"`([^\\\\`]?(\\\\(.|\\z))?)*+(`|\\z)|" + |
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
34 |
"[()\\[\\]{}:;]", Pattern.MULTILINE) |
34485 | 35 |
|
34660 | 36 |
val empty = |
37 |
new ProofDocument(isabelle.jedit.Isabelle.system.id(), |
|
34724
b1079c3ba1da
Prover: keep command_change/document_change event buses here, not in ProofDocument, Command, State, Plugin;
wenzelm
parents:
34722
diff
changeset
|
38 |
Linear_Set(), Map(), Linear_Set(), Map(), _ => false) |
34660 | 39 |
|
40 |
type StructureChange = List[(Option[Command], Option[Command])] |
|
34538
20bfcca24658
Prover as actor managing ProofDocument-versions (removed EventBus structural_changes);
immler@in.tum.de
parents:
34532
diff
changeset
|
41 |
|
34318
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
42 |
} |
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
43 |
|
34582 | 44 |
class ProofDocument( |
45 |
val id: String, |
|
34689 | 46 |
val tokens: Linear_Set[Token], |
34582 | 47 |
val token_start: Map[Token, Int], |
34689 | 48 |
val commands: Linear_Set[Command], |
34722 | 49 |
var states: Map[Command, Command_State], // FIXME immutable |
34724
b1079c3ba1da
Prover: keep command_change/document_change event buses here, not in ProofDocument, Command, State, Plugin;
wenzelm
parents:
34722
diff
changeset
|
50 |
is_command_keyword: String => Boolean) |
34483 | 51 |
{ |
34660 | 52 |
import ProofDocument.StructureChange |
34653 | 53 |
|
34532 | 54 |
def set_command_keyword(f: String => Boolean): ProofDocument = |
34724
b1079c3ba1da
Prover: keep command_change/document_change event buses here, not in ProofDocument, Command, State, Plugin;
wenzelm
parents:
34722
diff
changeset
|
55 |
new ProofDocument(id, tokens, token_start, commands, states, f) |
34485 | 56 |
|
34582 | 57 |
def content = Token.string_from_tokens(Nil ++ tokens, token_start) |
34657 | 58 |
|
59 |
||
34693 | 60 |
|
34485 | 61 |
/** token view **/ |
62 |
||
34693 | 63 |
def text_changed(change: Change): (ProofDocument, StructureChange) = |
34485 | 64 |
{ |
34660 | 65 |
def edit_doc(doc_chgs: (ProofDocument, StructureChange), edit: Edit) = { |
66 |
val (doc, chgs) = doc_chgs |
|
34693 | 67 |
val (new_doc, chg) = doc.text_edit(edit, change.id) |
34660 | 68 |
(new_doc, chgs ++ chg) |
69 |
} |
|
34693 | 70 |
((this, Nil: StructureChange) /: change.edits)(edit_doc) |
34660 | 71 |
} |
72 |
||
34741 | 73 |
def text_edit(e: Edit, id: String): (ProofDocument, StructureChange) = |
34660 | 74 |
{ |
75 |
case class TextChange(start: Int, added: String, removed: String) |
|
76 |
val change = e match { |
|
77 |
case Insert(s, a) => TextChange(s, a, "") |
|
78 |
case Remove(s, r) => TextChange(s, "", r) |
|
79 |
} |
|
34551
bd2b8fde9e25
incomplete changes of immutable tokens and commands
immler@in.tum.de
parents:
34550
diff
changeset
|
80 |
//indices of tokens |
bd2b8fde9e25
incomplete changes of immutable tokens and commands
immler@in.tum.de
parents:
34550
diff
changeset
|
81 |
var start: Map[Token, Int] = token_start |
bd2b8fde9e25
incomplete changes of immutable tokens and commands
immler@in.tum.de
parents:
34550
diff
changeset
|
82 |
def stop(t: Token) = start(t) + t.length |
bd2b8fde9e25
incomplete changes of immutable tokens and commands
immler@in.tum.de
parents:
34550
diff
changeset
|
83 |
// split old token lists |
34582 | 84 |
val tokens = Nil ++ this.tokens |
34551
bd2b8fde9e25
incomplete changes of immutable tokens and commands
immler@in.tum.de
parents:
34550
diff
changeset
|
85 |
val (begin, remaining) = tokens.span(stop(_) < change.start) |
34648 | 86 |
val (removed, end) = remaining.span(token_start(_) <= change.start + change.removed.length) |
34551
bd2b8fde9e25
incomplete changes of immutable tokens and commands
immler@in.tum.de
parents:
34550
diff
changeset
|
87 |
// update indices |
34582 | 88 |
start = end.foldLeft(start)((s, t) => |
34648 | 89 |
s + (t -> (s(t) + change.added.length - change.removed.length))) |
34485 | 90 |
|
34551
bd2b8fde9e25
incomplete changes of immutable tokens and commands
immler@in.tum.de
parents:
34550
diff
changeset
|
91 |
val split_begin = removed.takeWhile(start(_) < change.start). |
34554
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
92 |
map (t => { |
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
93 |
val split_tok = new Token(t.content.substring(0, change.start - start(t)), t.kind) |
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
94 |
start += (split_tok -> start(t)) |
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
95 |
split_tok |
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
96 |
}) |
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
97 |
|
34648 | 98 |
val split_end = removed.dropWhile(stop(_) < change.start + change.removed.length). |
34554
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
99 |
map (t => { |
34582 | 100 |
val split_tok = |
34648 | 101 |
new Token(t.content.substring(change.start + change.removed.length - start(t)), t.kind) |
34554
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
102 |
start += (split_tok -> start(t)) |
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
103 |
split_tok |
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
104 |
}) |
34551
bd2b8fde9e25
incomplete changes of immutable tokens and commands
immler@in.tum.de
parents:
34550
diff
changeset
|
105 |
// update indices |
34554
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
106 |
start = removed.foldLeft (start) ((s, t) => s - t) |
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
107 |
start = split_end.foldLeft (start) ((s, t) => |
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
108 |
s + (t -> (change.start + change.added.length))) |
34318
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
109 |
|
34551
bd2b8fde9e25
incomplete changes of immutable tokens and commands
immler@in.tum.de
parents:
34550
diff
changeset
|
110 |
val ins = new Token(change.added, Token.Kind.OTHER) |
bd2b8fde9e25
incomplete changes of immutable tokens and commands
immler@in.tum.de
parents:
34550
diff
changeset
|
111 |
start += (ins -> change.start) |
bd2b8fde9e25
incomplete changes of immutable tokens and commands
immler@in.tum.de
parents:
34550
diff
changeset
|
112 |
|
34582 | 113 |
var invalid_tokens = split_begin ::: ins :: split_end ::: end |
114 |
var new_tokens: List[Token] = Nil |
|
115 |
var old_suffix: List[Token] = Nil |
|
34318
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
116 |
|
34551
bd2b8fde9e25
incomplete changes of immutable tokens and commands
immler@in.tum.de
parents:
34550
diff
changeset
|
117 |
val match_start = invalid_tokens.firstOption.map(start(_)).getOrElse(0) |
34582 | 118 |
val matcher = |
119 |
ProofDocument.token_pattern.matcher(Token.string_from_tokens(invalid_tokens, start)) |
|
34526 | 120 |
|
121 |
while (matcher.find() && invalid_tokens != Nil) { |
|
34485 | 122 |
val kind = |
34505
87f4f70d61bb
ProofDocument: pass is_command_keyword directly, not via full-blown Prover object;
wenzelm
parents:
34496
diff
changeset
|
123 |
if (is_command_keyword(matcher.group)) |
34485 | 124 |
Token.Kind.COMMAND_START |
34494 | 125 |
else if (matcher.end - matcher.start > 2 && matcher.group.substring(0, 2) == "(*") |
34485 | 126 |
Token.Kind.COMMENT |
127 |
else |
|
128 |
Token.Kind.OTHER |
|
34551
bd2b8fde9e25
incomplete changes of immutable tokens and commands
immler@in.tum.de
parents:
34550
diff
changeset
|
129 |
val new_token = new Token(matcher.group, kind) |
bd2b8fde9e25
incomplete changes of immutable tokens and commands
immler@in.tum.de
parents:
34550
diff
changeset
|
130 |
start += (new_token -> (match_start + matcher.start)) |
34526 | 131 |
new_tokens ::= new_token |
34318
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
132 |
|
34660 | 133 |
invalid_tokens = invalid_tokens dropWhile (stop(_) < stop(new_token)) |
34526 | 134 |
invalid_tokens match { |
34582 | 135 |
case t :: ts => |
136 |
if (start(t) == start(new_token) && |
|
137 |
start(t) > change.start + change.added.length) { |
|
34597 | 138 |
old_suffix = t :: ts |
34592 | 139 |
new_tokens = new_tokens.tail |
34526 | 140 |
invalid_tokens = Nil |
141 |
} |
|
142 |
case _ => |
|
34318
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
143 |
} |
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
144 |
} |
34526 | 145 |
val insert = new_tokens.reverse |
34544
56217d219e27
proofdocument-versions get id from changes
immler@in.tum.de
parents:
34541
diff
changeset
|
146 |
val new_token_list = begin ::: insert ::: old_suffix |
34660 | 147 |
token_changed(id, begin.lastOption, insert, |
34597 | 148 |
old_suffix.firstOption, new_token_list, start) |
34318
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
149 |
} |
34582 | 150 |
|
34318
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
151 |
|
34485 | 152 |
/** command view **/ |
153 |
||
34582 | 154 |
private def token_changed( |
155 |
new_id: String, |
|
156 |
before_change: Option[Token], |
|
157 |
inserted_tokens: List[Token], |
|
158 |
after_change: Option[Token], |
|
34597 | 159 |
new_tokens: List[Token], |
34660 | 160 |
new_token_start: Map[Token, Int]): |
161 |
(ProofDocument, StructureChange) = |
|
34485 | 162 |
{ |
34689 | 163 |
val new_tokenset = Linear_Set[Token]() ++ new_tokens |
34593 | 164 |
val cmd_before_change = before_change match { |
165 |
case None => None |
|
166 |
case Some(bc) => |
|
167 |
val cmd_with_bc = commands.find(_.contains(bc)).get |
|
168 |
if (cmd_with_bc.tokens.last == bc) { |
|
169 |
if (new_tokenset.next(bc).map(_.is_start).getOrElse(true)) |
|
170 |
Some(cmd_with_bc) |
|
171 |
else commands.prev(cmd_with_bc) |
|
172 |
} |
|
173 |
else commands.prev(cmd_with_bc) |
|
174 |
} |
|
34544
56217d219e27
proofdocument-versions get id from changes
immler@in.tum.de
parents:
34541
diff
changeset
|
175 |
|
34593 | 176 |
val cmd_after_change = after_change match { |
177 |
case None => None |
|
178 |
case Some(ac) => |
|
179 |
val cmd_with_ac = commands.find(_.contains(ac)).get |
|
180 |
if (ac.is_start) |
|
181 |
Some(cmd_with_ac) |
|
182 |
else |
|
183 |
commands.next(cmd_with_ac) |
|
184 |
} |
|
34485 | 185 |
|
34593 | 186 |
val removed_commands = commands.dropWhile(Some(_) != cmd_before_change).drop(1). |
34554
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
187 |
takeWhile(Some(_) != cmd_after_change) |
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
188 |
|
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
189 |
// calculate inserted commands |
34526 | 190 |
def tokens_to_commands(tokens: List[Token]): List[Command]= { |
191 |
tokens match { |
|
192 |
case Nil => Nil |
|
34582 | 193 |
case t :: ts => |
194 |
val (cmd, rest) = |
|
195 |
ts.span(t => t.kind != Token.Kind.COMMAND_START && t.kind != Token.Kind.COMMENT) |
|
34724
b1079c3ba1da
Prover: keep command_change/document_change event buses here, not in ProofDocument, Command, State, Plugin;
wenzelm
parents:
34722
diff
changeset
|
196 |
new Command(t :: cmd, new_token_start) :: tokens_to_commands(rest) |
34485 | 197 |
} |
198 |
} |
|
199 |
||
34593 | 200 |
val split_begin = |
201 |
if (before_change.isDefined) { |
|
202 |
val changed = |
|
203 |
if (cmd_before_change.isDefined) |
|
34595 | 204 |
new_tokens.dropWhile(_ != cmd_before_change.get.tokens.last).drop(1) |
34593 | 205 |
else new_tokenset |
206 |
if (changed.exists(_ == before_change.get)) |
|
34597 | 207 |
changed.takeWhile(_ != before_change.get).toList ::: |
208 |
List(before_change.get) |
|
34593 | 209 |
else Nil |
210 |
} else Nil |
|
34554
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
211 |
|
34593 | 212 |
val split_end = |
34667 | 213 |
if (after_change.isDefined) { |
34595 | 214 |
val unchanged = new_tokens.dropWhile(_ != after_change.get) |
34667 | 215 |
if(cmd_after_change.isDefined) { |
216 |
if (unchanged.exists(_ == cmd_after_change.get.tokens.first)) |
|
217 |
unchanged.takeWhile(_ != cmd_after_change.get.tokens.first).toList |
|
218 |
else Nil |
|
219 |
} else { |
|
220 |
unchanged |
|
221 |
} |
|
34593 | 222 |
} else Nil |
223 |
||
34597 | 224 |
val rescan_begin = |
225 |
split_begin ::: |
|
226 |
before_change.map(bc => new_tokens.dropWhile(_ != bc).drop(1)).getOrElse(new_tokens) |
|
34582 | 227 |
val rescanning_tokens = |
34597 | 228 |
after_change.map(ac => rescan_begin.takeWhile(_ != ac)).getOrElse(rescan_begin) ::: |
229 |
split_end |
|
34593 | 230 |
val inserted_commands = tokens_to_commands(rescanning_tokens.toList) |
34554
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
231 |
|
34550
171c8c6e5707
prepared proofdocument for only needed changes
immler@in.tum.de
parents:
34544
diff
changeset
|
232 |
// build new document |
34739 | 233 |
val new_commandset = commands. |
234 |
delete_between(cmd_before_change, cmd_after_change). |
|
235 |
append_after(cmd_before_change, inserted_commands) |
|
236 |
||
34554
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
237 |
|
34544
56217d219e27
proofdocument-versions get id from changes
immler@in.tum.de
parents:
34541
diff
changeset
|
238 |
val doc = |
34674 | 239 |
new ProofDocument(new_id, new_tokenset, new_token_start, new_commandset, |
34724
b1079c3ba1da
Prover: keep command_change/document_change event buses here, not in ProofDocument, Command, State, Plugin;
wenzelm
parents:
34722
diff
changeset
|
240 |
states -- removed_commands, is_command_keyword) |
34660 | 241 |
|
242 |
val removes = |
|
243 |
for (cmd <- removed_commands) yield (cmd_before_change -> None) |
|
244 |
val inserts = |
|
245 |
for (cmd <- inserted_commands) yield (doc.commands.prev(cmd) -> Some(cmd)) |
|
246 |
||
247 |
return (doc, removes.toList ++ inserts) |
|
34485 | 248 |
} |
34596 | 249 |
|
250 |
val commands_offsets = { |
|
251 |
var last_stop = 0 |
|
252 |
(for (c <- commands) yield { |
|
253 |
val r = c -> (last_stop,c.stop(this)) |
|
254 |
last_stop = c.stop(this) |
|
255 |
r |
|
256 |
}).toArray |
|
257 |
} |
|
258 |
||
34712
4f0ee5ab0380
replaced find_command_at by command_at -- null-free, proper Option;
wenzelm
parents:
34703
diff
changeset
|
259 |
def command_at(pos: Int): Option[Command] = |
4f0ee5ab0380
replaced find_command_at by command_at -- null-free, proper Option;
wenzelm
parents:
34703
diff
changeset
|
260 |
find_command(pos, 0, commands_offsets.length) |
4f0ee5ab0380
replaced find_command_at by command_at -- null-free, proper Option;
wenzelm
parents:
34703
diff
changeset
|
261 |
|
34596 | 262 |
// use a binary search to find commands for a given offset |
34712
4f0ee5ab0380
replaced find_command_at by command_at -- null-free, proper Option;
wenzelm
parents:
34703
diff
changeset
|
263 |
private def find_command(pos: Int, array_start: Int, array_stop: Int): Option[Command] = |
4f0ee5ab0380
replaced find_command_at by command_at -- null-free, proper Option;
wenzelm
parents:
34703
diff
changeset
|
264 |
{ |
34596 | 265 |
val middle_index = (array_start + array_stop) / 2 |
34712
4f0ee5ab0380
replaced find_command_at by command_at -- null-free, proper Option;
wenzelm
parents:
34703
diff
changeset
|
266 |
if (middle_index >= commands_offsets.length) return None |
34596 | 267 |
val (middle, (start, stop)) = commands_offsets(middle_index) |
268 |
// does middle contain pos? |
|
34712
4f0ee5ab0380
replaced find_command_at by command_at -- null-free, proper Option;
wenzelm
parents:
34703
diff
changeset
|
269 |
if (start <= pos && pos < stop) |
4f0ee5ab0380
replaced find_command_at by command_at -- null-free, proper Option;
wenzelm
parents:
34703
diff
changeset
|
270 |
Some(middle) |
34596 | 271 |
else if (start > pos) |
34712
4f0ee5ab0380
replaced find_command_at by command_at -- null-free, proper Option;
wenzelm
parents:
34703
diff
changeset
|
272 |
find_command(pos, array_start, middle_index) |
34596 | 273 |
else if (stop <= pos) |
34712
4f0ee5ab0380
replaced find_command_at by command_at -- null-free, proper Option;
wenzelm
parents:
34703
diff
changeset
|
274 |
find_command(pos, middle_index + 1, array_stop) |
4f0ee5ab0380
replaced find_command_at by command_at -- null-free, proper Option;
wenzelm
parents:
34703
diff
changeset
|
275 |
else error("impossible") |
34596 | 276 |
} |
34318
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
277 |
} |