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