author | wenzelm |
Thu, 25 Aug 2011 11:41:48 +0200 | |
changeset 44474 | 681447a9ffe5 |
parent 44473 | 4f264fdf8d0e |
child 45240 | 9d97bd3c086a |
permissions | -rw-r--r-- |
38425 | 1 |
/* Title: Pure/PIDE/text.scala |
34276 | 2 |
Author: Fabian Immler, TU Munich |
3 |
Author: Makarius |
|
4 |
||
38425 | 5 |
Basic operations on plain text. |
34276 | 6 |
*/ |
7 |
||
8 |
package isabelle |
|
9 |
||
10 |
||
44379 | 11 |
import scala.collection.mutable |
12 |
import scala.math.Ordering |
|
13 |
import scala.util.Sorting |
|
14 |
||
15 |
||
38425 | 16 |
object Text |
34276 | 17 |
{ |
38477 | 18 |
/* offset */ |
38426 | 19 |
|
20 |
type Offset = Int |
|
21 |
||
38477 | 22 |
|
38565
32b924a832c4
further clarification/unification of Position.Range and Text.Range concerning singularities: start offset is always included;
wenzelm
parents:
38564
diff
changeset
|
23 |
/* range -- with total quasi-ordering */ |
38477 | 24 |
|
38568 | 25 |
object Range |
26 |
{ |
|
27 |
def apply(start: Offset): Range = Range(start, start) |
|
44379 | 28 |
|
29 |
object Ordering extends scala.math.Ordering[Text.Range] |
|
30 |
{ |
|
31 |
def compare(r1: Text.Range, r2: Text.Range): Int = r1 compare r2 |
|
32 |
} |
|
38568 | 33 |
} |
34 |
||
38426 | 35 |
sealed case class Range(val start: Offset, val stop: Offset) |
38427 | 36 |
{ |
38565
32b924a832c4
further clarification/unification of Position.Range and Text.Range concerning singularities: start offset is always included;
wenzelm
parents:
38564
diff
changeset
|
37 |
// denotation: {start} Un {i. start < i & i < stop} |
43425 | 38 |
if (start > stop) |
39 |
error("Bad range: [" + start.toString + ":" + stop.toString + "]") |
|
38477 | 40 |
|
38563 | 41 |
override def toString = "[" + start.toString + ":" + stop.toString + "]" |
42 |
||
38427 | 43 |
def map(f: Offset => Offset): Range = Range(f(start), f(stop)) |
44 |
def +(i: Offset): Range = map(_ + i) |
|
38570 | 45 |
def -(i: Offset): Range = map(_ - i) |
38662 | 46 |
|
38725 | 47 |
def is_singularity: Boolean = start == stop |
38662 | 48 |
|
38565
32b924a832c4
further clarification/unification of Position.Range and Text.Range concerning singularities: start offset is always included;
wenzelm
parents:
38564
diff
changeset
|
49 |
def contains(i: Offset): Boolean = start == i || start < i && i < stop |
32b924a832c4
further clarification/unification of Position.Range and Text.Range concerning singularities: start offset is always included;
wenzelm
parents:
38564
diff
changeset
|
50 |
def contains(that: Range): Boolean = this.contains(that.start) && that.stop <= this.stop |
32b924a832c4
further clarification/unification of Position.Range and Text.Range concerning singularities: start offset is always included;
wenzelm
parents:
38564
diff
changeset
|
51 |
def overlaps(that: Range): Boolean = this.contains(that.start) || that.contains(this.start) |
32b924a832c4
further clarification/unification of Position.Range and Text.Range concerning singularities: start offset is always included;
wenzelm
parents:
38564
diff
changeset
|
52 |
def compare(that: Range): Int = if (overlaps(that)) 0 else this.start compare that.start |
38485 | 53 |
|
38564 | 54 |
def restrict(that: Range): Range = |
38485 | 55 |
Range(this.start max that.start, this.stop min that.stop) |
43428
b41dea5772c6
more robust treatment of partial range restriction;
wenzelm
parents:
43425
diff
changeset
|
56 |
|
b41dea5772c6
more robust treatment of partial range restriction;
wenzelm
parents:
43425
diff
changeset
|
57 |
def try_restrict(that: Range): Option[Range] = |
b41dea5772c6
more robust treatment of partial range restriction;
wenzelm
parents:
43425
diff
changeset
|
58 |
try { Some (restrict(that)) } |
43650 | 59 |
catch { case ERROR(_) => None } |
38427 | 60 |
} |
38426 | 61 |
|
62 |
||
44379 | 63 |
/* perspective */ |
64 |
||
44473 | 65 |
object Perspective |
44379 | 66 |
{ |
44474 | 67 |
val empty: Perspective = Perspective(Nil) |
44379 | 68 |
|
44473 | 69 |
def apply(ranges: Seq[Range]): Perspective = |
44379 | 70 |
{ |
44473 | 71 |
val sorted_ranges = ranges.toArray |
72 |
Sorting.quickSort(sorted_ranges)(Range.Ordering) |
|
73 |
||
74 |
val result = new mutable.ListBuffer[Text.Range] |
|
75 |
var last: Option[Text.Range] = None |
|
76 |
for (range <- sorted_ranges) |
|
77 |
{ |
|
78 |
last match { |
|
79 |
case Some(last_range) |
|
80 |
if ((last_range overlaps range) || last_range.stop == range.start) => |
|
81 |
last = Some(Text.Range(last_range.start, range.stop)) |
|
82 |
case _ => |
|
83 |
result ++= last |
|
84 |
last = Some(range) |
|
85 |
} |
|
44379 | 86 |
} |
44473 | 87 |
result ++= last |
88 |
new Perspective(result.toList) |
|
44379 | 89 |
} |
44473 | 90 |
} |
91 |
||
92 |
sealed case class Perspective(ranges: List[Range]) // visible text partitioning in canonical order |
|
93 |
{ |
|
94 |
def is_empty: Boolean = ranges.isEmpty |
|
95 |
def range: Range = |
|
96 |
if (is_empty) Range(0) |
|
97 |
else Range(ranges.head.start, ranges.last.stop) |
|
44379 | 98 |
} |
99 |
||
100 |
||
38577 | 101 |
/* information associated with text range */ |
102 |
||
43714 | 103 |
sealed case class Info[A](val range: Text.Range, val info: A) |
38577 | 104 |
{ |
105 |
def restrict(r: Text.Range): Info[A] = Info(range.restrict(r), info) |
|
43428
b41dea5772c6
more robust treatment of partial range restriction;
wenzelm
parents:
43425
diff
changeset
|
106 |
def try_restrict(r: Text.Range): Option[Info[A]] = |
b41dea5772c6
more robust treatment of partial range restriction;
wenzelm
parents:
43425
diff
changeset
|
107 |
try { Some(Info(range.restrict(r), info)) } |
43650 | 108 |
catch { case ERROR(_) => None } |
38577 | 109 |
} |
110 |
||
111 |
||
38426 | 112 |
/* editing */ |
34286 | 113 |
|
38425 | 114 |
object Edit |
115 |
{ |
|
38426 | 116 |
def insert(start: Offset, text: String): Edit = new Edit(true, start, text) |
117 |
def remove(start: Offset, text: String): Edit = new Edit(false, start, text) |
|
38425 | 118 |
} |
34286 | 119 |
|
38426 | 120 |
class Edit(val is_insert: Boolean, val start: Offset, val text: String) |
38425 | 121 |
{ |
122 |
override def toString = |
|
123 |
(if (is_insert) "Insert(" else "Remove(") + (start, text).toString + ")" |
|
34286 | 124 |
|
125 |
||
38425 | 126 |
/* transform offsets */ |
34286 | 127 |
|
38426 | 128 |
private def transform(do_insert: Boolean, i: Offset): Offset = |
129 |
if (i < start) i |
|
43425 | 130 |
else if (do_insert) i + text.length |
38426 | 131 |
else (i - text.length) max start |
34286 | 132 |
|
43425 | 133 |
def convert(i: Offset): Offset = transform(is_insert, i) |
134 |
def revert(i: Offset): Offset = transform(!is_insert, i) |
|
135 |
def convert(range: Range): Range = range.map(convert) |
|
136 |
def revert(range: Range): Range = range.map(revert) |
|
38425 | 137 |
|
34286 | 138 |
|
38425 | 139 |
/* edit strings */ |
140 |
||
38426 | 141 |
private def insert(i: Offset, string: String): String = |
142 |
string.substring(0, i) + text + string.substring(i) |
|
34276 | 143 |
|
38426 | 144 |
private def remove(i: Offset, count: Int, string: String): String = |
145 |
string.substring(0, i) + string.substring(i + count) |
|
38425 | 146 |
|
147 |
def can_edit(string: String, shift: Int): Boolean = |
|
148 |
shift <= start && start < shift + string.length |
|
149 |
||
150 |
def edit(string: String, shift: Int): (Option[Edit], String) = |
|
151 |
if (!can_edit(string, shift)) (Some(this), string) |
|
152 |
else if (is_insert) (None, insert(start - shift, string)) |
|
153 |
else { |
|
38426 | 154 |
val i = start - shift |
155 |
val count = text.length min (string.length - i) |
|
38425 | 156 |
val rest = |
157 |
if (count == text.length) None |
|
158 |
else Some(Edit.remove(start, text.substring(count))) |
|
38426 | 159 |
(rest, remove(i, count, string)) |
38425 | 160 |
} |
161 |
} |
|
34276 | 162 |
} |