author | wenzelm |
Sun, 27 Dec 2009 21:33:35 +0100 | |
changeset 34187 | 7b659c1561f1 |
parent 34157 | 0a0a19153626 |
child 34189 | 10c5871ec684 |
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 |
||
34187
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
wenzelm
parents:
34157
diff
changeset
|
9 |
|
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
wenzelm
parents:
34157
diff
changeset
|
10 |
import scala.collection.immutable.PagedSeq |
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
wenzelm
parents:
34157
diff
changeset
|
11 |
import scala.util.parsing.input.{OffsetPosition, Position => InputPosition, Reader} |
31648 | 12 |
import scala.util.parsing.combinator.RegexParsers |
13 |
||
34187
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
wenzelm
parents:
34157
diff
changeset
|
14 |
import java.io.{File, InputStream, BufferedInputStream, FileInputStream} |
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
wenzelm
parents:
34157
diff
changeset
|
15 |
|
31648 | 16 |
|
17 |
object Scan |
|
18 |
{ |
|
31649
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
19 |
/** Lexicon -- position tree **/ |
31648 | 20 |
|
31649
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
21 |
object Lexicon |
31648 | 22 |
{ |
31649
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
23 |
private case class Tree(val branches: Map[Char, (String, Tree)]) |
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
24 |
private val empty_tree = Tree(Map()) |
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
25 |
|
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
26 |
val empty: Lexicon = new Lexicon |
31762
20745ab5b79a
more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
wenzelm
parents:
31753
diff
changeset
|
27 |
def apply(elems: String*): Lexicon = empty ++ elems |
31648 | 28 |
} |
29 |
||
31764 | 30 |
class Lexicon extends scala.collection.Set[String] with RegexParsers |
31648 | 31 |
{ |
31649
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
32 |
/* representation */ |
31648 | 33 |
|
31649
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
34 |
import Lexicon.Tree |
31764 | 35 |
protected val main_tree: Tree = Lexicon.empty_tree |
31648 | 36 |
|
37 |
||
31650 | 38 |
/* auxiliary operations */ |
39 |
||
40 |
private def content(tree: Tree, result: List[String]): List[String] = |
|
41 |
(result /: tree.branches.toList) ((res, entry) => |
|
42 |
entry match { case (_, (s, tr)) => |
|
43 |
if (s.isEmpty) content(tr, res) else content(tr, s :: res) }) |
|
44 |
||
45 |
private def lookup(str: CharSequence): Option[(Boolean, Tree)] = |
|
46 |
{ |
|
47 |
val len = str.length |
|
48 |
def look(tree: Tree, tip: Boolean, i: Int): Option[(Boolean, Tree)] = |
|
49 |
{ |
|
50 |
if (i < len) { |
|
51 |
tree.branches.get(str.charAt(i)) match { |
|
52 |
case Some((s, tr)) => look(tr, !s.isEmpty, i + 1) |
|
53 |
case None => None |
|
54 |
} |
|
55 |
} else Some(tip, tree) |
|
56 |
} |
|
57 |
look(main_tree, false, 0) |
|
58 |
} |
|
59 |
||
60 |
def completions(str: CharSequence): List[String] = |
|
31764 | 61 |
lookup(str) match { |
31650 | 62 |
case Some((true, tree)) => content(tree, List(str.toString)) |
63 |
case Some((false, tree)) => content(tree, Nil) |
|
64 |
case None => Nil |
|
31764 | 65 |
} |
31650 | 66 |
|
67 |
||
31649
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
68 |
/* Set methods */ |
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
69 |
|
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
70 |
override def stringPrefix = "Lexicon" |
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
71 |
|
31753 | 72 |
override def isEmpty: Boolean = { main_tree.branches.isEmpty } |
31649
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
73 |
|
31650 | 74 |
def size: Int = content(main_tree, Nil).length |
75 |
def elements: Iterator[String] = content(main_tree, Nil).sort(_ <= _).elements |
|
31648 | 76 |
|
31762
20745ab5b79a
more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
wenzelm
parents:
31753
diff
changeset
|
77 |
def contains(elem: String): Boolean = |
20745ab5b79a
more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
wenzelm
parents:
31753
diff
changeset
|
78 |
lookup(elem) match { |
31650 | 79 |
case Some((tip, _)) => tip |
80 |
case _ => false |
|
81 |
} |
|
31648 | 82 |
|
31762
20745ab5b79a
more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
wenzelm
parents:
31753
diff
changeset
|
83 |
def + (elem: String): Lexicon = |
20745ab5b79a
more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
wenzelm
parents:
31753
diff
changeset
|
84 |
if (contains(elem)) this |
20745ab5b79a
more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
wenzelm
parents:
31753
diff
changeset
|
85 |
else { |
20745ab5b79a
more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
wenzelm
parents:
31753
diff
changeset
|
86 |
val len = elem.length |
20745ab5b79a
more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
wenzelm
parents:
31753
diff
changeset
|
87 |
def extend(tree: Tree, i: Int): Tree = |
20745ab5b79a
more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
wenzelm
parents:
31753
diff
changeset
|
88 |
if (i < len) { |
20745ab5b79a
more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
wenzelm
parents:
31753
diff
changeset
|
89 |
val c = elem.charAt(i) |
20745ab5b79a
more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
wenzelm
parents:
31753
diff
changeset
|
90 |
val end = (i + 1 == len) |
20745ab5b79a
more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
wenzelm
parents:
31753
diff
changeset
|
91 |
tree.branches.get(c) match { |
20745ab5b79a
more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
wenzelm
parents:
31753
diff
changeset
|
92 |
case Some((s, tr)) => |
20745ab5b79a
more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
wenzelm
parents:
31753
diff
changeset
|
93 |
Tree(tree.branches + |
20745ab5b79a
more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
wenzelm
parents:
31753
diff
changeset
|
94 |
(c -> (if (end) elem else s, extend(tr, i + 1)))) |
20745ab5b79a
more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
wenzelm
parents:
31753
diff
changeset
|
95 |
case None => |
20745ab5b79a
more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
wenzelm
parents:
31753
diff
changeset
|
96 |
Tree(tree.branches + |
20745ab5b79a
more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
wenzelm
parents:
31753
diff
changeset
|
97 |
(c -> (if (end) elem else "", extend(Lexicon.empty_tree, i + 1)))) |
20745ab5b79a
more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
wenzelm
parents:
31753
diff
changeset
|
98 |
} |
34135 | 99 |
} |
100 |
else tree |
|
31762
20745ab5b79a
more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
wenzelm
parents:
31753
diff
changeset
|
101 |
val old = this |
20745ab5b79a
more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
wenzelm
parents:
31753
diff
changeset
|
102 |
new Lexicon { override val main_tree = extend(old.main_tree, 0) } |
31648 | 103 |
} |
31762
20745ab5b79a
more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
wenzelm
parents:
31753
diff
changeset
|
104 |
|
31764 | 105 |
def + (elem1: String, elem2: String, elems: String*): Lexicon = |
31762
20745ab5b79a
more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
wenzelm
parents:
31753
diff
changeset
|
106 |
this + elem1 + elem2 ++ elems |
31764 | 107 |
def ++ (elems: Iterable[String]): Lexicon = (this /: elems) ((s, elem) => s + elem) |
108 |
def ++ (elems: Iterator[String]): Lexicon = (this /: elems) ((s, elem) => s + elem) |
|
31649
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
109 |
|
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
110 |
|
34137 | 111 |
/** RegexParsers methods **/ |
31649
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
112 |
|
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
113 |
override val whiteSpace = "".r |
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
114 |
|
34137 | 115 |
|
116 |
/* keywords from lexicon */ |
|
117 |
||
34140 | 118 |
def keyword: Parser[String] = new Parser[String] |
34135 | 119 |
{ |
31649
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
120 |
def apply(in: Input) = |
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
121 |
{ |
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
122 |
val source = in.source |
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
123 |
val offset = in.offset |
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
124 |
val len = source.length - offset |
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
125 |
|
34140 | 126 |
def scan(tree: Tree, result: String, i: Int): String = |
31649
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
127 |
{ |
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
128 |
if (i < len) { |
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
129 |
tree.branches.get(source.charAt(offset + i)) match { |
34140 | 130 |
case Some((s, tr)) => scan(tr, if (s.isEmpty) result else s, i + 1) |
34135 | 131 |
case None => result |
31649
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
132 |
} |
34135 | 133 |
} |
134 |
else result |
|
31649
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
135 |
} |
34140 | 136 |
val result = scan(main_tree, "", 0) |
137 |
if (result.isEmpty) Failure("keyword expected", in) |
|
138 |
else Success(result, in.drop(result.length)) |
|
31648 | 139 |
} |
31649
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
140 |
}.named("keyword") |
31648 | 141 |
|
34137 | 142 |
|
34157
0a0a19153626
explicit representation of Token_Kind -- cannot really depend on runtime types due to erasure;
wenzelm
parents:
34144
diff
changeset
|
143 |
/* symbol range */ |
34137 | 144 |
|
34157
0a0a19153626
explicit representation of Token_Kind -- cannot really depend on runtime types due to erasure;
wenzelm
parents:
34144
diff
changeset
|
145 |
def symbol_range(pred: String => Boolean, min_count: Int, max_count: Int): Parser[String] = |
34140 | 146 |
new Parser[String] |
34135 | 147 |
{ |
34137 | 148 |
def apply(in: Input) = |
149 |
{ |
|
150 |
val start = in.offset |
|
151 |
val end = in.source.length |
|
152 |
val matcher = new Symbol.Matcher(in.source) |
|
153 |
||
154 |
var i = start |
|
155 |
var count = 0 |
|
156 |
var finished = false |
|
157 |
while (!finished) { |
|
158 |
if (i < end && count < max_count) { |
|
159 |
val n = matcher(i, end) |
|
160 |
val sym = in.source.subSequence(i, i + n).toString |
|
161 |
if (pred(sym)) { i += n; count += 1 } |
|
162 |
else finished = true |
|
163 |
} |
|
164 |
else finished = true |
|
165 |
} |
|
34140 | 166 |
if (count < min_count) Failure("bad input", in) |
167 |
else Success(in.source.subSequence(start, i).toString, in.drop(i - start)) |
|
34137 | 168 |
} |
34157
0a0a19153626
explicit representation of Token_Kind -- cannot really depend on runtime types due to erasure;
wenzelm
parents:
34144
diff
changeset
|
169 |
}.named("symbol_range") |
34137 | 170 |
|
34157
0a0a19153626
explicit representation of Token_Kind -- cannot really depend on runtime types due to erasure;
wenzelm
parents:
34144
diff
changeset
|
171 |
def one(pred: String => Boolean): Parser[String] = symbol_range(pred, 1, 1) |
0a0a19153626
explicit representation of Token_Kind -- cannot really depend on runtime types due to erasure;
wenzelm
parents:
34144
diff
changeset
|
172 |
def many(pred: String => Boolean): Parser[String] = symbol_range(pred, 0, Integer.MAX_VALUE) |
0a0a19153626
explicit representation of Token_Kind -- cannot really depend on runtime types due to erasure;
wenzelm
parents:
34144
diff
changeset
|
173 |
def many1(pred: String => Boolean): Parser[String] = symbol_range(pred, 1, Integer.MAX_VALUE) |
34140 | 174 |
|
175 |
||
34143 | 176 |
/* quoted strings */ |
34140 | 177 |
|
34143 | 178 |
private def quoted(quote: String): Parser[String] = |
179 |
{ |
|
34140 | 180 |
quote ~ |
181 |
rep(many1(sym => sym != quote && sym != "\\" && Symbol.is_closed(sym)) | |
|
182 |
"\\" + quote | "\\\\" | |
|
183 |
(("""\\\d\d\d""".r) ^? { case x if x.substring(1, 4).toInt <= 255 => x })) ~ |
|
184 |
quote ^^ { case x ~ ys ~ z => x + ys.mkString + z } |
|
34143 | 185 |
}.named("quoted") |
34140 | 186 |
|
187 |
def quoted_content(quote: String, source: String): String = |
|
188 |
{ |
|
189 |
require(parseAll(quoted(quote), source).successful) |
|
34187
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
wenzelm
parents:
34157
diff
changeset
|
190 |
source.substring(1, source.length - 1) // FIXME proper escapes |
34140 | 191 |
} |
192 |
||
193 |
||
34143 | 194 |
/* verbatim text */ |
195 |
||
196 |
private def verbatim: Parser[String] = |
|
197 |
{ |
|
34140 | 198 |
"{*" ~ rep(many1(sym => sym != "*" && Symbol.is_closed(sym)) | """\*(?!\})""".r) ~ "*}" ^^ |
199 |
{ case x ~ ys ~ z => x + ys.mkString + z } |
|
34143 | 200 |
}.named("verbatim") |
34140 | 201 |
|
202 |
def verbatim_content(source: String): String = |
|
203 |
{ |
|
204 |
require(parseAll(verbatim, source).successful) |
|
205 |
source.substring(2, source.length - 2) |
|
206 |
} |
|
207 |
||
208 |
||
34143 | 209 |
/* nested comments */ |
210 |
||
211 |
def comment: Parser[String] = new Parser[String] |
|
212 |
{ |
|
213 |
val comment_text = |
|
214 |
rep(many1(sym => sym != "*" && sym != "(" && Symbol.is_closed(sym)) | |
|
215 |
"""\*(?!\))|\((?!\*)""".r) |
|
216 |
val comment_open = "(*" ~ comment_text ^^ (_ => ()) |
|
217 |
val comment_close = comment_text ~ "*)" ^^ (_ => ()) |
|
218 |
||
219 |
def apply(in: Input) = |
|
220 |
{ |
|
221 |
var rest = in |
|
222 |
def try_parse(p: Parser[Unit]): Boolean = |
|
223 |
{ |
|
224 |
parse(p, rest) match { |
|
225 |
case Success(_, next) => { rest = next; true } |
|
226 |
case _ => false |
|
227 |
} |
|
228 |
} |
|
229 |
var depth = 0 |
|
230 |
var finished = false |
|
231 |
while (!finished) { |
|
232 |
if (try_parse(comment_open)) depth += 1 |
|
233 |
else if (depth > 0 && try_parse(comment_close)) depth -= 1 |
|
234 |
else finished = true |
|
235 |
} |
|
236 |
if (in.offset < rest.offset && depth == 0) |
|
237 |
Success(in.source.subSequence(in.offset, rest.offset).toString, rest) |
|
238 |
else Failure("comment expected", in) |
|
239 |
} |
|
240 |
}.named("comment") |
|
241 |
||
242 |
def comment_content(source: String): String = |
|
243 |
{ |
|
244 |
require(parseAll(comment, source).successful) |
|
245 |
source.substring(2, source.length - 2) |
|
246 |
} |
|
247 |
||
248 |
||
34140 | 249 |
/* outer syntax tokens */ |
250 |
||
251 |
def token(symbols: Symbol.Interpretation, is_command: String => Boolean): |
|
252 |
Parser[Outer_Lex.Token] = |
|
253 |
{ |
|
34157
0a0a19153626
explicit representation of Token_Kind -- cannot really depend on runtime types due to erasure;
wenzelm
parents:
34144
diff
changeset
|
254 |
import Outer_Lex.Token_Kind, Outer_Lex.Token |
34140 | 255 |
|
256 |
val id = one(symbols.is_letter) ~ many(symbols.is_letdig) ^^ { case x ~ y => x + y } |
|
257 |
val nat = many1(symbols.is_digit) |
|
258 |
val id_nat = id ~ opt("." ~ nat) ^^ { case x ~ Some(y ~ z) => x + y + z case x ~ None => x } |
|
259 |
||
260 |
val ident = id ~ rep("." ~> id) ^^ |
|
34157
0a0a19153626
explicit representation of Token_Kind -- cannot really depend on runtime types due to erasure;
wenzelm
parents:
34144
diff
changeset
|
261 |
{ case x ~ Nil => Token(Token_Kind.IDENT, x) |
0a0a19153626
explicit representation of Token_Kind -- cannot really depend on runtime types due to erasure;
wenzelm
parents:
34144
diff
changeset
|
262 |
case x ~ ys => Token(Token_Kind.LONG_IDENT, (x :: ys).mkString(".")) } |
34140 | 263 |
|
34157
0a0a19153626
explicit representation of Token_Kind -- cannot really depend on runtime types due to erasure;
wenzelm
parents:
34144
diff
changeset
|
264 |
val var_ = "?" ~ id_nat ^^ { case x ~ y => Token(Token_Kind.VAR, x + y) } |
0a0a19153626
explicit representation of Token_Kind -- cannot really depend on runtime types due to erasure;
wenzelm
parents:
34144
diff
changeset
|
265 |
val type_ident = "'" ~ id ^^ { case x ~ y => Token(Token_Kind.TYPE_IDENT, x + y) } |
0a0a19153626
explicit representation of Token_Kind -- cannot really depend on runtime types due to erasure;
wenzelm
parents:
34144
diff
changeset
|
266 |
val type_var = "?'" ~ id_nat ^^ { case x ~ y => Token(Token_Kind.TYPE_VAR, x + y) } |
0a0a19153626
explicit representation of Token_Kind -- cannot really depend on runtime types due to erasure;
wenzelm
parents:
34144
diff
changeset
|
267 |
val nat_ = nat ^^ (x => Token(Token_Kind.NAT, x)) |
34140 | 268 |
|
269 |
val sym_ident = |
|
270 |
(many1(symbols.is_symbolic_char) | |
|
34157
0a0a19153626
explicit representation of Token_Kind -- cannot really depend on runtime types due to erasure;
wenzelm
parents:
34144
diff
changeset
|
271 |
one(sym => symbols.is_symbolic(sym) & Symbol.is_closed(sym))) ^^ |
0a0a19153626
explicit representation of Token_Kind -- cannot really depend on runtime types due to erasure;
wenzelm
parents:
34144
diff
changeset
|
272 |
(x => Token(Token_Kind.SYM_IDENT, x)) |
34140 | 273 |
|
34157
0a0a19153626
explicit representation of Token_Kind -- cannot really depend on runtime types due to erasure;
wenzelm
parents:
34144
diff
changeset
|
274 |
val space = many1(symbols.is_blank) ^^ (x => Token(Token_Kind.SPACE, x)) |
34140 | 275 |
|
34157
0a0a19153626
explicit representation of Token_Kind -- cannot really depend on runtime types due to erasure;
wenzelm
parents:
34144
diff
changeset
|
276 |
val string = quoted("\"") ^^ (x => Token(Token_Kind.STRING, x)) |
0a0a19153626
explicit representation of Token_Kind -- cannot really depend on runtime types due to erasure;
wenzelm
parents:
34144
diff
changeset
|
277 |
val alt_string = quoted("`") ^^ (x => Token(Token_Kind.ALT_STRING, x)) |
34140 | 278 |
|
34157
0a0a19153626
explicit representation of Token_Kind -- cannot really depend on runtime types due to erasure;
wenzelm
parents:
34144
diff
changeset
|
279 |
val bad_input = many1(sym => !(symbols.is_blank(sym))) ^^ |
0a0a19153626
explicit representation of Token_Kind -- cannot really depend on runtime types due to erasure;
wenzelm
parents:
34144
diff
changeset
|
280 |
(x => Token(Token_Kind.BAD_INPUT, x)) |
34140 | 281 |
|
282 |
||
283 |
/* tokens */ |
|
284 |
||
34157
0a0a19153626
explicit representation of Token_Kind -- cannot really depend on runtime types due to erasure;
wenzelm
parents:
34144
diff
changeset
|
285 |
(space | (string | (alt_string | (verbatim ^^ (x => Token(Token_Kind.VERBATIM, x)) | |
0a0a19153626
explicit representation of Token_Kind -- cannot really depend on runtime types due to erasure;
wenzelm
parents:
34144
diff
changeset
|
286 |
comment ^^ (x => Token(Token_Kind.COMMENT, x)))))) | |
34144
bd7b3b91abab
improve performance by reordering of parser combinators;
wenzelm
parents:
34143
diff
changeset
|
287 |
((ident | (var_ | (type_ident | (type_var | (nat_ | sym_ident))))) ||| |
34157
0a0a19153626
explicit representation of Token_Kind -- cannot really depend on runtime types due to erasure;
wenzelm
parents:
34144
diff
changeset
|
288 |
keyword ^^ (x => Token(if (is_command(x)) Token_Kind.COMMAND else Token_Kind.KEYWORD, x))) | |
34140 | 289 |
bad_input |
290 |
} |
|
31649
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
291 |
} |
34187
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
wenzelm
parents:
34157
diff
changeset
|
292 |
|
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
wenzelm
parents:
34157
diff
changeset
|
293 |
|
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
wenzelm
parents:
34157
diff
changeset
|
294 |
|
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
wenzelm
parents:
34157
diff
changeset
|
295 |
/** read file without decoding -- enables efficient length operation **/ |
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
wenzelm
parents:
34157
diff
changeset
|
296 |
|
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
wenzelm
parents:
34157
diff
changeset
|
297 |
private class Restricted_Seq(seq: RandomAccessSeq[Char], start: Int, end: Int) |
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
wenzelm
parents:
34157
diff
changeset
|
298 |
extends CharSequence |
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
wenzelm
parents:
34157
diff
changeset
|
299 |
{ |
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
wenzelm
parents:
34157
diff
changeset
|
300 |
def charAt(i: Int): Char = |
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
wenzelm
parents:
34157
diff
changeset
|
301 |
if (0 <= i && i < length) seq(start + i) |
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
wenzelm
parents:
34157
diff
changeset
|
302 |
else throw new IndexOutOfBoundsException |
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
wenzelm
parents:
34157
diff
changeset
|
303 |
|
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
wenzelm
parents:
34157
diff
changeset
|
304 |
def length: Int = end - start // avoid potentially expensive seq.length |
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
wenzelm
parents:
34157
diff
changeset
|
305 |
|
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
wenzelm
parents:
34157
diff
changeset
|
306 |
def subSequence(i: Int, j: Int): CharSequence = |
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
wenzelm
parents:
34157
diff
changeset
|
307 |
if (0 <= i && i <= j && j <= length) new Restricted_Seq(seq, start + i, start + j) |
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
wenzelm
parents:
34157
diff
changeset
|
308 |
else throw new IndexOutOfBoundsException |
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
wenzelm
parents:
34157
diff
changeset
|
309 |
|
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
wenzelm
parents:
34157
diff
changeset
|
310 |
override def toString: String = |
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
wenzelm
parents:
34157
diff
changeset
|
311 |
{ |
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
wenzelm
parents:
34157
diff
changeset
|
312 |
val buf = new StringBuilder(length) |
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
wenzelm
parents:
34157
diff
changeset
|
313 |
for (offset <- start until end) buf.append(seq(offset)) |
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
wenzelm
parents:
34157
diff
changeset
|
314 |
buf.toString |
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
wenzelm
parents:
34157
diff
changeset
|
315 |
} |
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
wenzelm
parents:
34157
diff
changeset
|
316 |
} |
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
wenzelm
parents:
34157
diff
changeset
|
317 |
|
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
wenzelm
parents:
34157
diff
changeset
|
318 |
abstract class Byte_Reader extends Reader[Char] { def close: Unit } |
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
wenzelm
parents:
34157
diff
changeset
|
319 |
|
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
wenzelm
parents:
34157
diff
changeset
|
320 |
def byte_reader(file: File): Byte_Reader = |
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
wenzelm
parents:
34157
diff
changeset
|
321 |
{ |
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
wenzelm
parents:
34157
diff
changeset
|
322 |
val stream = new BufferedInputStream(new FileInputStream(file)) |
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
wenzelm
parents:
34157
diff
changeset
|
323 |
val seq = new PagedSeq( |
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
wenzelm
parents:
34157
diff
changeset
|
324 |
(buf: Array[Char], offset: Int, length: Int) => |
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
wenzelm
parents:
34157
diff
changeset
|
325 |
{ |
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
wenzelm
parents:
34157
diff
changeset
|
326 |
var i = 0 |
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
wenzelm
parents:
34157
diff
changeset
|
327 |
var c = 0 |
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
wenzelm
parents:
34157
diff
changeset
|
328 |
var eof = false |
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
wenzelm
parents:
34157
diff
changeset
|
329 |
while (!eof && i < length) { |
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
wenzelm
parents:
34157
diff
changeset
|
330 |
c = stream.read |
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
wenzelm
parents:
34157
diff
changeset
|
331 |
if (c == -1) eof = true |
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
wenzelm
parents:
34157
diff
changeset
|
332 |
else { buf(offset + i) = c.toChar; i += 1 } |
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
wenzelm
parents:
34157
diff
changeset
|
333 |
} |
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
wenzelm
parents:
34157
diff
changeset
|
334 |
if (i > 0) i else -1 |
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
wenzelm
parents:
34157
diff
changeset
|
335 |
}) |
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
wenzelm
parents:
34157
diff
changeset
|
336 |
val restricted_seq = new Restricted_Seq(seq, 0, file.length.toInt) |
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
wenzelm
parents:
34157
diff
changeset
|
337 |
|
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
wenzelm
parents:
34157
diff
changeset
|
338 |
class Paged_Reader(override val offset: Int) extends Byte_Reader |
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
wenzelm
parents:
34157
diff
changeset
|
339 |
{ |
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
wenzelm
parents:
34157
diff
changeset
|
340 |
override lazy val source: CharSequence = restricted_seq |
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
wenzelm
parents:
34157
diff
changeset
|
341 |
def first: Char = if (seq.isDefinedAt(offset)) seq(offset) else '\032' |
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
wenzelm
parents:
34157
diff
changeset
|
342 |
def rest: Paged_Reader = if (seq.isDefinedAt(offset)) new Paged_Reader(offset + 1) else this |
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
wenzelm
parents:
34157
diff
changeset
|
343 |
def pos: InputPosition = new OffsetPosition(source, offset) |
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
wenzelm
parents:
34157
diff
changeset
|
344 |
def atEnd: Boolean = !seq.isDefinedAt(offset) |
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
wenzelm
parents:
34157
diff
changeset
|
345 |
override def drop(n: Int): Paged_Reader = new Paged_Reader(offset + n) |
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
wenzelm
parents:
34157
diff
changeset
|
346 |
def close { stream.close } |
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
wenzelm
parents:
34157
diff
changeset
|
347 |
} |
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
wenzelm
parents:
34157
diff
changeset
|
348 |
new Paged_Reader(0) |
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
wenzelm
parents:
34157
diff
changeset
|
349 |
} |
31648 | 350 |
} |