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