author | wenzelm |
Thu, 18 Jun 2009 14:02:47 +0200 | |
changeset 31700 | b44912113c83 |
parent 31650 | cfaed41ee2c5 |
child 31701 | d3d2e417fb5e |
permissions | -rw-r--r-- |
31648 | 1 |
/* Title: Pure/General/scan.scala |
2 |
Author: Makarius |
|
3 |
||
31649
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
4 |
Efficient scanning of keywords. |
31648 | 5 |
*/ |
6 |
||
7 |
package isabelle |
|
8 |
||
9 |
import scala.util.parsing.combinator.RegexParsers |
|
10 |
||
11 |
||
12 |
object Scan |
|
13 |
{ |
|
14 |
||
31649
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
15 |
/** Lexicon -- position tree **/ |
31648 | 16 |
|
31649
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
17 |
object Lexicon |
31648 | 18 |
{ |
31649
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
19 |
private case class Tree(val branches: Map[Char, (String, Tree)]) |
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
20 |
private val empty_tree = Tree(Map()) |
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
21 |
|
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
22 |
private def make(tree: Tree, max: Int): Lexicon = |
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
23 |
new Lexicon { |
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
24 |
override val main_tree = tree |
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
25 |
override val max_entry = max |
31648 | 26 |
} |
31649
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
27 |
|
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
28 |
val empty: Lexicon = new Lexicon |
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
29 |
def apply(strs: String*): Lexicon = (empty /: strs) ((lex, str) => lex + str) |
31648 | 30 |
} |
31 |
||
31649
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
32 |
class Lexicon extends scala.collection.immutable.Set[String] with RegexParsers |
31648 | 33 |
{ |
31649
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
34 |
/* representation */ |
31648 | 35 |
|
31649
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
36 |
import Lexicon.Tree |
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
37 |
val main_tree: Tree = Lexicon.empty_tree |
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
38 |
val max_entry = -1 |
31648 | 39 |
|
40 |
||
31650 | 41 |
/* auxiliary operations */ |
42 |
||
43 |
private def content(tree: Tree, result: List[String]): List[String] = |
|
44 |
(result /: tree.branches.toList) ((res, entry) => |
|
45 |
entry match { case (_, (s, tr)) => |
|
46 |
if (s.isEmpty) content(tr, res) else content(tr, s :: res) }) |
|
47 |
||
48 |
private def lookup(str: CharSequence): Option[(Boolean, Tree)] = |
|
49 |
{ |
|
50 |
val len = str.length |
|
51 |
def look(tree: Tree, tip: Boolean, i: Int): Option[(Boolean, Tree)] = |
|
52 |
{ |
|
53 |
if (i < len) { |
|
54 |
tree.branches.get(str.charAt(i)) match { |
|
55 |
case Some((s, tr)) => look(tr, !s.isEmpty, i + 1) |
|
56 |
case None => None |
|
57 |
} |
|
58 |
} else Some(tip, tree) |
|
59 |
} |
|
60 |
look(main_tree, false, 0) |
|
61 |
} |
|
62 |
||
63 |
def completions(str: CharSequence): List[String] = |
|
64 |
{ |
|
65 |
(lookup(str) match { |
|
66 |
case Some((true, tree)) => content(tree, List(str.toString)) |
|
67 |
case Some((false, tree)) => content(tree, Nil) |
|
68 |
case None => Nil |
|
69 |
}).sort((s1, s2) => s1.length < s2.length || s1.length == s2.length && s1 <= s2) |
|
70 |
} |
|
71 |
||
72 |
||
31649
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
73 |
/* Set methods */ |
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
74 |
|
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
75 |
override def stringPrefix = "Lexicon" |
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
76 |
|
31650 | 77 |
override def isEmpty: Boolean = { max_entry < 0 } |
31649
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
78 |
|
31650 | 79 |
def size: Int = content(main_tree, Nil).length |
80 |
def elements: Iterator[String] = content(main_tree, Nil).sort(_ <= _).elements |
|
31648 | 81 |
|
31649
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
82 |
def contains(str: String): Boolean = |
31650 | 83 |
lookup(str) match { |
84 |
case Some((tip, _)) => tip |
|
85 |
case _ => false |
|
86 |
} |
|
31648 | 87 |
|
31649
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
88 |
def +(str: String): Lexicon = |
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
89 |
{ |
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
90 |
val len = str.length |
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
91 |
def extend(tree: Tree, i: Int): Tree = |
31648 | 92 |
{ |
93 |
if (i < len) { |
|
31649
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
94 |
val c = str.charAt(i) |
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
95 |
val end = (i + 1 == len) |
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
96 |
tree.branches.get(c) match { |
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
97 |
case Some((s, tr)) => |
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
98 |
Tree(tree.branches + (c -> (if (end) str else s, extend(tr, i + 1)))) |
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
99 |
case None => |
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
100 |
Tree(tree.branches + (c -> (if (end) str else "", extend(Lexicon.empty_tree, i + 1)))) |
31648 | 101 |
} |
31649
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
102 |
} else tree |
31648 | 103 |
} |
31649
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
104 |
if (contains(str)) this |
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
105 |
else Lexicon.make(extend(main_tree, 0), max_entry max str.length) |
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
106 |
} |
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
107 |
|
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
108 |
def empty[A]: Set[A] = error("Undefined") |
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
109 |
def -(str: String): Lexicon = error("Undefined") |
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
110 |
|
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
111 |
|
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
112 |
/* RegexParsers methods */ |
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
113 |
|
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
114 |
override val whiteSpace = "".r |
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
115 |
|
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
116 |
def keyword: Parser[String] = new Parser[String] { |
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
117 |
def apply(in: Input) = |
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
118 |
{ |
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
119 |
val source = in.source |
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
120 |
val offset = in.offset |
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
121 |
val len = source.length - offset |
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
122 |
|
31650 | 123 |
def scan(tree: Tree, text: String, i: Int): String = |
31649
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
124 |
{ |
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
125 |
if (i < len) { |
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
126 |
tree.branches.get(source.charAt(offset + i)) match { |
31650 | 127 |
case Some((s, tr)) => scan(tr, if (s.isEmpty) text else s, i + 1) |
128 |
case None => text |
|
31649
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
129 |
} |
31650 | 130 |
} else text |
31649
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
131 |
} |
31650 | 132 |
val text = scan(main_tree, "", 0) |
133 |
if (text.isEmpty) Failure("keyword expected", in) |
|
134 |
else Success(text, in.drop(text.length)) |
|
31648 | 135 |
} |
31649
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
136 |
}.named("keyword") |
31648 | 137 |
|
31649
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
138 |
} |
31700 | 139 |
|
140 |
||
141 |
/** reverse CharSequence **/ |
|
142 |
||
143 |
class Reverse(text: CharSequence, start: Int, end: Int) extends CharSequence |
|
144 |
{ |
|
145 |
require(0 <= start && start <= end && end <= text.length) |
|
146 |
||
147 |
def this(text: CharSequence) = this(text, 0, text.length) |
|
148 |
||
149 |
def length: Int = end - start |
|
150 |
def charAt(i: Int): Char = text.charAt(end - i - 1) |
|
151 |
def subSequence(i: Int, j: Int): CharSequence = new Reverse(text, end - j, end - i) |
|
152 |
||
153 |
override def toString: String = |
|
154 |
{ |
|
155 |
val buf = new StringBuffer(length) |
|
156 |
for (i <- 0 until length) |
|
157 |
buf.append(charAt(i)) |
|
158 |
buf.toString |
|
159 |
} |
|
160 |
} |
|
161 |
||
31648 | 162 |
} |
163 |