author | wenzelm |
Mon, 18 Aug 2014 12:17:31 +0200 | |
changeset 57978 | 8f4a332500e4 |
parent 56827 | 853f1bcc3755 |
child 58505 | d1fe47fe5401 |
permissions | -rw-r--r-- |
31648 | 1 |
/* Title: Pure/General/scan.scala |
2 |
Author: Makarius |
|
3 |
||
40523
1050315f6ee2
simplified/robustified treatment of malformed symbols, which are now fully internalized (total Symbol.explode etc.);
wenzelm
parents:
40290
diff
changeset
|
4 |
Efficient scanning of keywords and tokens. |
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 |
|
55980 | 10 |
import scala.annotation.tailrec |
46627
fbe2cb05bdb3
avoid trait Addable, which is deprecated in scala-2.9.x;
wenzelm
parents:
45900
diff
changeset
|
11 |
import scala.collection.{IndexedSeq, TraversableOnce} |
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 |
||
56822 | 16 |
import java.io.{File => JFile, BufferedInputStream, FileInputStream, InputStream} |
17 |
import java.net.URL |
|
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
|
18 |
|
31648 | 19 |
|
20 |
object Scan |
|
21 |
{ |
|
55510
1585a65aad64
tuned signature -- emphasize line-oriented aspect;
wenzelm
parents:
55499
diff
changeset
|
22 |
/** context of partial line-oriented scans **/ |
43411
0206466ee473
some support for partial scans with explicit context;
wenzelm
parents:
42718
diff
changeset
|
23 |
|
55510
1585a65aad64
tuned signature -- emphasize line-oriented aspect;
wenzelm
parents:
55499
diff
changeset
|
24 |
abstract class Line_Context |
1585a65aad64
tuned signature -- emphasize line-oriented aspect;
wenzelm
parents:
55499
diff
changeset
|
25 |
case object Finished extends Line_Context |
1585a65aad64
tuned signature -- emphasize line-oriented aspect;
wenzelm
parents:
55499
diff
changeset
|
26 |
case class Quoted(quote: String) extends Line_Context |
1585a65aad64
tuned signature -- emphasize line-oriented aspect;
wenzelm
parents:
55499
diff
changeset
|
27 |
case object Verbatim extends Line_Context |
1585a65aad64
tuned signature -- emphasize line-oriented aspect;
wenzelm
parents:
55499
diff
changeset
|
28 |
case class Cartouche(depth: Int) extends Line_Context |
1585a65aad64
tuned signature -- emphasize line-oriented aspect;
wenzelm
parents:
55499
diff
changeset
|
29 |
case class Comment(depth: Int) extends Line_Context |
43411
0206466ee473
some support for partial scans with explicit context;
wenzelm
parents:
42718
diff
changeset
|
30 |
|
0206466ee473
some support for partial scans with explicit context;
wenzelm
parents:
42718
diff
changeset
|
31 |
|
0206466ee473
some support for partial scans with explicit context;
wenzelm
parents:
42718
diff
changeset
|
32 |
|
55492
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
33 |
/** parser combinators **/ |
31650 | 34 |
|
55492
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
35 |
object Parsers extends Parsers |
36011
3ff725ac13a4
adapted to Scala 2.8.0 Beta1 -- with notable changes to scala.collection;
wenzelm
parents:
34316
diff
changeset
|
36 |
|
55492
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
37 |
trait Parsers extends RegexParsers |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
38 |
{ |
31649
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
39 |
override val whiteSpace = "".r |
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
40 |
|
34137 | 41 |
|
43411
0206466ee473
some support for partial scans with explicit context;
wenzelm
parents:
42718
diff
changeset
|
42 |
/* optional termination */ |
0206466ee473
some support for partial scans with explicit context;
wenzelm
parents:
42718
diff
changeset
|
43 |
|
0206466ee473
some support for partial scans with explicit context;
wenzelm
parents:
42718
diff
changeset
|
44 |
def opt_term[T](p: => Parser[T]): Parser[Option[T]] = |
0206466ee473
some support for partial scans with explicit context;
wenzelm
parents:
42718
diff
changeset
|
45 |
p ^^ (x => Some(x)) | """\z""".r ^^ (_ => None) |
0206466ee473
some support for partial scans with explicit context;
wenzelm
parents:
42718
diff
changeset
|
46 |
|
0206466ee473
some support for partial scans with explicit context;
wenzelm
parents:
42718
diff
changeset
|
47 |
|
55497 | 48 |
/* repeated symbols */ |
34137 | 49 |
|
55497 | 50 |
def repeated(pred: Symbol.Symbol => Boolean, min_count: Int, max_count: Int): Parser[String] = |
34140 | 51 |
new Parser[String] |
34135 | 52 |
{ |
34137 | 53 |
def apply(in: Input) = |
54 |
{ |
|
55 |
val start = in.offset |
|
56 |
val end = in.source.length |
|
57 |
val matcher = new Symbol.Matcher(in.source) |
|
58 |
||
59 |
var i = start |
|
60 |
var count = 0 |
|
61 |
var finished = false |
|
55034 | 62 |
while (!finished && i < end && count < max_count) { |
63 |
val n = matcher(i, end) |
|
64 |
val sym = in.source.subSequence(i, i + n).toString |
|
65 |
if (pred(sym)) { i += n; count += 1 } |
|
34137 | 66 |
else finished = true |
67 |
} |
|
34140 | 68 |
if (count < min_count) Failure("bad input", in) |
69 |
else Success(in.source.subSequence(start, i).toString, in.drop(i - start)) |
|
34137 | 70 |
} |
55497 | 71 |
}.named("repeated") |
34137 | 72 |
|
43696 | 73 |
def one(pred: Symbol.Symbol => Boolean): Parser[String] = |
55497 | 74 |
repeated(pred, 1, 1) |
43696 | 75 |
|
76 |
def many(pred: Symbol.Symbol => Boolean): Parser[String] = |
|
55497 | 77 |
repeated(pred, 0, Integer.MAX_VALUE) |
43696 | 78 |
|
79 |
def many1(pred: Symbol.Symbol => Boolean): Parser[String] = |
|
55497 | 80 |
repeated(pred, 1, Integer.MAX_VALUE) |
81 |
||
82 |
||
83 |
/* character */ |
|
84 |
||
85 |
def character(pred: Char => Boolean): Symbol.Symbol => Boolean = |
|
86 |
(s: Symbol. Symbol) => s.length == 1 && pred(s.charAt(0)) |
|
34140 | 87 |
|
88 |
||
34143 | 89 |
/* quoted strings */ |
34140 | 90 |
|
43696 | 91 |
private def quoted_body(quote: Symbol.Symbol): Parser[String] = |
43411
0206466ee473
some support for partial scans with explicit context;
wenzelm
parents:
42718
diff
changeset
|
92 |
{ |
0206466ee473
some support for partial scans with explicit context;
wenzelm
parents:
42718
diff
changeset
|
93 |
rep(many1(sym => sym != quote && sym != "\\") | "\\" + quote | "\\\\" | |
0206466ee473
some support for partial scans with explicit context;
wenzelm
parents:
42718
diff
changeset
|
94 |
(("""\\\d\d\d""".r) ^? { case x if x.substring(1, 4).toInt <= 255 => x })) ^^ (_.mkString) |
0206466ee473
some support for partial scans with explicit context;
wenzelm
parents:
42718
diff
changeset
|
95 |
} |
0206466ee473
some support for partial scans with explicit context;
wenzelm
parents:
42718
diff
changeset
|
96 |
|
55494 | 97 |
def quoted(quote: Symbol.Symbol): Parser[String] = |
34143 | 98 |
{ |
43411
0206466ee473
some support for partial scans with explicit context;
wenzelm
parents:
42718
diff
changeset
|
99 |
quote ~ quoted_body(quote) ~ quote ^^ { case x ~ y ~ z => x + y + z } |
34143 | 100 |
}.named("quoted") |
34140 | 101 |
|
43696 | 102 |
def quoted_content(quote: Symbol.Symbol, source: String): String = |
34140 | 103 |
{ |
104 |
require(parseAll(quoted(quote), source).successful) |
|
34189 | 105 |
val body = source.substring(1, source.length - 1) |
106 |
if (body.exists(_ == '\\')) { |
|
107 |
val content = |
|
40523
1050315f6ee2
simplified/robustified treatment of malformed symbols, which are now fully internalized (total Symbol.explode etc.);
wenzelm
parents:
40290
diff
changeset
|
108 |
rep(many1(sym => sym != quote && sym != "\\") | |
34189 | 109 |
"\\" ~> (quote | "\\" | """\d\d\d""".r ^^ { case x => x.toInt.toChar.toString })) |
110 |
parseAll(content ^^ (_.mkString), body).get |
|
111 |
} |
|
112 |
else body |
|
34140 | 113 |
} |
114 |
||
55510
1585a65aad64
tuned signature -- emphasize line-oriented aspect;
wenzelm
parents:
55499
diff
changeset
|
115 |
def quoted_line(quote: Symbol.Symbol, ctxt: Line_Context): Parser[(String, Line_Context)] = |
43411
0206466ee473
some support for partial scans with explicit context;
wenzelm
parents:
42718
diff
changeset
|
116 |
{ |
0206466ee473
some support for partial scans with explicit context;
wenzelm
parents:
42718
diff
changeset
|
117 |
ctxt match { |
0206466ee473
some support for partial scans with explicit context;
wenzelm
parents:
42718
diff
changeset
|
118 |
case Finished => |
0206466ee473
some support for partial scans with explicit context;
wenzelm
parents:
42718
diff
changeset
|
119 |
quote ~ quoted_body(quote) ~ opt_term(quote) ^^ |
0206466ee473
some support for partial scans with explicit context;
wenzelm
parents:
42718
diff
changeset
|
120 |
{ case x ~ y ~ Some(z) => (x + y + z, Finished) |
0206466ee473
some support for partial scans with explicit context;
wenzelm
parents:
42718
diff
changeset
|
121 |
case x ~ y ~ None => (x + y, Quoted(quote)) } |
0206466ee473
some support for partial scans with explicit context;
wenzelm
parents:
42718
diff
changeset
|
122 |
case Quoted(q) if q == quote => |
0206466ee473
some support for partial scans with explicit context;
wenzelm
parents:
42718
diff
changeset
|
123 |
quoted_body(quote) ~ opt_term(quote) ^^ |
0206466ee473
some support for partial scans with explicit context;
wenzelm
parents:
42718
diff
changeset
|
124 |
{ case x ~ Some(y) => (x + y, Finished) |
0206466ee473
some support for partial scans with explicit context;
wenzelm
parents:
42718
diff
changeset
|
125 |
case x ~ None => (x, ctxt) } |
0206466ee473
some support for partial scans with explicit context;
wenzelm
parents:
42718
diff
changeset
|
126 |
case _ => failure("") |
0206466ee473
some support for partial scans with explicit context;
wenzelm
parents:
42718
diff
changeset
|
127 |
} |
55510
1585a65aad64
tuned signature -- emphasize line-oriented aspect;
wenzelm
parents:
55499
diff
changeset
|
128 |
}.named("quoted_line") |
43411
0206466ee473
some support for partial scans with explicit context;
wenzelm
parents:
42718
diff
changeset
|
129 |
|
48763
de68fc11c245
more precise recover_quoted, recover_verbatim, recover_comment (cf. ML version) -- NB: context parsers expect explicit termination;
wenzelm
parents:
48754
diff
changeset
|
130 |
def recover_quoted(quote: Symbol.Symbol): Parser[String] = |
de68fc11c245
more precise recover_quoted, recover_verbatim, recover_comment (cf. ML version) -- NB: context parsers expect explicit termination;
wenzelm
parents:
48754
diff
changeset
|
131 |
quote ~ quoted_body(quote) ^^ { case x ~ y => x + y } |
48743
a72f8ffecf31
refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents:
48409
diff
changeset
|
132 |
|
34140 | 133 |
|
34143 | 134 |
/* verbatim text */ |
135 |
||
43411
0206466ee473
some support for partial scans with explicit context;
wenzelm
parents:
42718
diff
changeset
|
136 |
private def verbatim_body: Parser[String] = |
0206466ee473
some support for partial scans with explicit context;
wenzelm
parents:
42718
diff
changeset
|
137 |
rep(many1(sym => sym != "*") | """\*(?!\})""".r) ^^ (_.mkString) |
0206466ee473
some support for partial scans with explicit context;
wenzelm
parents:
42718
diff
changeset
|
138 |
|
55494 | 139 |
def verbatim: Parser[String] = |
34143 | 140 |
{ |
43411
0206466ee473
some support for partial scans with explicit context;
wenzelm
parents:
42718
diff
changeset
|
141 |
"{*" ~ verbatim_body ~ "*}" ^^ { case x ~ y ~ z => x + y + z } |
34143 | 142 |
}.named("verbatim") |
34140 | 143 |
|
144 |
def verbatim_content(source: String): String = |
|
145 |
{ |
|
146 |
require(parseAll(verbatim, source).successful) |
|
147 |
source.substring(2, source.length - 2) |
|
148 |
} |
|
149 |
||
55510
1585a65aad64
tuned signature -- emphasize line-oriented aspect;
wenzelm
parents:
55499
diff
changeset
|
150 |
def verbatim_line(ctxt: Line_Context): Parser[(String, Line_Context)] = |
43411
0206466ee473
some support for partial scans with explicit context;
wenzelm
parents:
42718
diff
changeset
|
151 |
{ |
0206466ee473
some support for partial scans with explicit context;
wenzelm
parents:
42718
diff
changeset
|
152 |
ctxt match { |
0206466ee473
some support for partial scans with explicit context;
wenzelm
parents:
42718
diff
changeset
|
153 |
case Finished => |
0206466ee473
some support for partial scans with explicit context;
wenzelm
parents:
42718
diff
changeset
|
154 |
"{*" ~ verbatim_body ~ opt_term("*}") ^^ |
0206466ee473
some support for partial scans with explicit context;
wenzelm
parents:
42718
diff
changeset
|
155 |
{ case x ~ y ~ Some(z) => (x + y + z, Finished) |
0206466ee473
some support for partial scans with explicit context;
wenzelm
parents:
42718
diff
changeset
|
156 |
case x ~ y ~ None => (x + y, Verbatim) } |
0206466ee473
some support for partial scans with explicit context;
wenzelm
parents:
42718
diff
changeset
|
157 |
case Verbatim => |
0206466ee473
some support for partial scans with explicit context;
wenzelm
parents:
42718
diff
changeset
|
158 |
verbatim_body ~ opt_term("*}") ^^ |
0206466ee473
some support for partial scans with explicit context;
wenzelm
parents:
42718
diff
changeset
|
159 |
{ case x ~ Some(y) => (x + y, Finished) |
0206466ee473
some support for partial scans with explicit context;
wenzelm
parents:
42718
diff
changeset
|
160 |
case x ~ None => (x, Verbatim) } |
0206466ee473
some support for partial scans with explicit context;
wenzelm
parents:
42718
diff
changeset
|
161 |
case _ => failure("") |
0206466ee473
some support for partial scans with explicit context;
wenzelm
parents:
42718
diff
changeset
|
162 |
} |
55510
1585a65aad64
tuned signature -- emphasize line-oriented aspect;
wenzelm
parents:
55499
diff
changeset
|
163 |
}.named("verbatim_line") |
43411
0206466ee473
some support for partial scans with explicit context;
wenzelm
parents:
42718
diff
changeset
|
164 |
|
48763
de68fc11c245
more precise recover_quoted, recover_verbatim, recover_comment (cf. ML version) -- NB: context parsers expect explicit termination;
wenzelm
parents:
48754
diff
changeset
|
165 |
val recover_verbatim: Parser[String] = |
de68fc11c245
more precise recover_quoted, recover_verbatim, recover_comment (cf. ML version) -- NB: context parsers expect explicit termination;
wenzelm
parents:
48754
diff
changeset
|
166 |
"{*" ~ verbatim_body ^^ { case x ~ y => x + y } |
48743
a72f8ffecf31
refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents:
48409
diff
changeset
|
167 |
|
34140 | 168 |
|
55033 | 169 |
/* nested text cartouches */ |
170 |
||
171 |
private def cartouche_depth(depth: Int): Parser[(String, Int)] = new Parser[(String, Int)] |
|
172 |
{ |
|
173 |
require(depth >= 0) |
|
174 |
||
175 |
def apply(in: Input) = |
|
176 |
{ |
|
177 |
val start = in.offset |
|
178 |
val end = in.source.length |
|
179 |
val matcher = new Symbol.Matcher(in.source) |
|
180 |
||
181 |
var i = start |
|
182 |
var d = depth |
|
183 |
var finished = false |
|
55034 | 184 |
while (!finished && i < end) { |
185 |
val n = matcher(i, end) |
|
186 |
val sym = in.source.subSequence(i, i + n).toString |
|
187 |
if (Symbol.is_open(sym)) { i += n; d += 1 } |
|
188 |
else if (d > 0) { i += n; if (Symbol.is_close(sym)) d -= 1 } |
|
55033 | 189 |
else finished = true |
190 |
} |
|
191 |
if (i == start) Failure("bad input", in) |
|
192 |
else Success((in.source.subSequence(start, i).toString, d), in.drop(i - start)) |
|
193 |
} |
|
194 |
}.named("cartouche_depth") |
|
195 |
||
196 |
def cartouche: Parser[String] = |
|
197 |
cartouche_depth(0) ^? { case (x, d) if d == 0 => x } |
|
198 |
||
55510
1585a65aad64
tuned signature -- emphasize line-oriented aspect;
wenzelm
parents:
55499
diff
changeset
|
199 |
def cartouche_line(ctxt: Line_Context): Parser[(String, Line_Context)] = |
55033 | 200 |
{ |
201 |
val depth = |
|
202 |
ctxt match { |
|
203 |
case Finished => 0 |
|
204 |
case Cartouche(d) => d |
|
205 |
case _ => -1 |
|
206 |
} |
|
207 |
if (depth >= 0) |
|
208 |
cartouche_depth(depth) ^^ |
|
209 |
{ case (x, 0) => (x, Finished) |
|
210 |
case (x, d) => (x, Cartouche(d)) } |
|
211 |
else failure("") |
|
212 |
} |
|
213 |
||
214 |
val recover_cartouche: Parser[String] = |
|
215 |
cartouche_depth(0) ^^ (_._1) |
|
216 |
||
217 |
def cartouche_content(source: String): String = |
|
218 |
{ |
|
219 |
def err(): Nothing = error("Malformed text cartouche: " + quote(source)) |
|
220 |
val source1 = |
|
221 |
Library.try_unprefix(Symbol.open_decoded, source) orElse |
|
222 |
Library.try_unprefix(Symbol.open, source) getOrElse err() |
|
223 |
Library.try_unsuffix(Symbol.close_decoded, source1) orElse |
|
224 |
Library.try_unsuffix(Symbol.close, source1) getOrElse err() |
|
225 |
} |
|
226 |
||
227 |
||
34143 | 228 |
/* nested comments */ |
229 |
||
43412 | 230 |
private def comment_depth(depth: Int): Parser[(String, Int)] = new Parser[(String, Int)] |
34143 | 231 |
{ |
43412 | 232 |
require(depth >= 0) |
233 |
||
34143 | 234 |
val comment_text = |
42441
781c622af16a
more robust scanning of iterated comments, such as "(* (**) (**) *)";
wenzelm
parents:
40523
diff
changeset
|
235 |
rep1(many1(sym => sym != "*" && sym != "(") | """\*(?!\))|\((?!\*)""".r) |
34143 | 236 |
|
237 |
def apply(in: Input) = |
|
238 |
{ |
|
239 |
var rest = in |
|
42441
781c622af16a
more robust scanning of iterated comments, such as "(* (**) (**) *)";
wenzelm
parents:
40523
diff
changeset
|
240 |
def try_parse[A](p: Parser[A]): Boolean = |
34143 | 241 |
{ |
56663
2d09b437c168
avoid "Adaptation of argument list by inserting ()" -- deprecated in scala-2.11.0;
wenzelm
parents:
55980
diff
changeset
|
242 |
parse(p ^^^ (()), rest) match { |
34143 | 243 |
case Success(_, next) => { rest = next; true } |
244 |
case _ => false |
|
245 |
} |
|
246 |
} |
|
43412 | 247 |
var d = depth |
34143 | 248 |
var finished = false |
249 |
while (!finished) { |
|
43412 | 250 |
if (try_parse("(*")) d += 1 |
251 |
else if (d > 0 && try_parse("*)")) d -= 1 |
|
252 |
else if (d == 0 || !try_parse(comment_text)) finished = true |
|
34143 | 253 |
} |
43412 | 254 |
if (in.offset < rest.offset) |
255 |
Success((in.source.subSequence(in.offset, rest.offset).toString, d), rest) |
|
34143 | 256 |
else Failure("comment expected", in) |
257 |
} |
|
43412 | 258 |
}.named("comment_depth") |
259 |
||
260 |
def comment: Parser[String] = |
|
261 |
comment_depth(0) ^? { case (x, d) if d == 0 => x } |
|
262 |
||
55510
1585a65aad64
tuned signature -- emphasize line-oriented aspect;
wenzelm
parents:
55499
diff
changeset
|
263 |
def comment_line(ctxt: Line_Context): Parser[(String, Line_Context)] = |
43412 | 264 |
{ |
265 |
val depth = |
|
266 |
ctxt match { |
|
267 |
case Finished => 0 |
|
268 |
case Comment(d) => d |
|
269 |
case _ => -1 |
|
270 |
} |
|
271 |
if (depth >= 0) |
|
272 |
comment_depth(depth) ^^ |
|
273 |
{ case (x, 0) => (x, Finished) |
|
274 |
case (x, d) => (x, Comment(d)) } |
|
275 |
else failure("") |
|
276 |
} |
|
34143 | 277 |
|
48763
de68fc11c245
more precise recover_quoted, recover_verbatim, recover_comment (cf. ML version) -- NB: context parsers expect explicit termination;
wenzelm
parents:
48754
diff
changeset
|
278 |
val recover_comment: Parser[String] = |
de68fc11c245
more precise recover_quoted, recover_verbatim, recover_comment (cf. ML version) -- NB: context parsers expect explicit termination;
wenzelm
parents:
48754
diff
changeset
|
279 |
comment_depth(0) ^^ (_._1) |
34143 | 280 |
|
55033 | 281 |
def comment_content(source: String): String = |
282 |
{ |
|
283 |
require(parseAll(comment, source).successful) |
|
284 |
source.substring(2, source.length - 2) |
|
285 |
} |
|
286 |
||
34143 | 287 |
|
55492
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
288 |
/* keyword */ |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
289 |
|
55497 | 290 |
def literal(lexicon: Lexicon): Parser[String] = new Parser[String] |
55492
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
291 |
{ |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
292 |
def apply(in: Input) = |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
293 |
{ |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
294 |
val result = lexicon.scan(in) |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
295 |
if (result.isEmpty) Failure("keyword expected", in) |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
296 |
else Success(result, in.drop(result.length)) |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
297 |
} |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
298 |
}.named("keyword") |
31649
a11ea667d676
reorganized and abstracted version, via Set trait;
wenzelm
parents:
31648
diff
changeset
|
299 |
} |
55492
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
300 |
|
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
301 |
|
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
302 |
|
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
303 |
/** Lexicon -- position tree **/ |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
304 |
|
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
305 |
object Lexicon |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
306 |
{ |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
307 |
/* representation */ |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
308 |
|
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
309 |
private sealed case class Tree(val branches: Map[Char, (String, Tree)]) |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
310 |
private val empty_tree = Tree(Map()) |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
311 |
|
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
312 |
val empty: Lexicon = new Lexicon(empty_tree) |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
313 |
def apply(elems: String*): Lexicon = empty ++ elems |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
314 |
} |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
315 |
|
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
316 |
final class Lexicon private(rep: Lexicon.Tree) |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
317 |
{ |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
318 |
/* auxiliary operations */ |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
319 |
|
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
320 |
private def content(tree: Lexicon.Tree, result: List[String]): List[String] = |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
321 |
(result /: tree.branches.toList) ((res, entry) => |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
322 |
entry match { case (_, (s, tr)) => |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
323 |
if (s.isEmpty) content(tr, res) else content(tr, s :: res) }) |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
324 |
|
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
325 |
private def lookup(str: CharSequence): Option[(Boolean, Lexicon.Tree)] = |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
326 |
{ |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
327 |
val len = str.length |
55980 | 328 |
@tailrec def look(tree: Lexicon.Tree, tip: Boolean, i: Int): Option[(Boolean, Lexicon.Tree)] = |
55492
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
329 |
{ |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
330 |
if (i < len) { |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
331 |
tree.branches.get(str.charAt(i)) match { |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
332 |
case Some((s, tr)) => look(tr, !s.isEmpty, i + 1) |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
333 |
case None => None |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
334 |
} |
55980 | 335 |
} |
336 |
else Some(tip, tree) |
|
55492
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
337 |
} |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
338 |
look(rep, false, 0) |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
339 |
} |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
340 |
|
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
341 |
def completions(str: CharSequence): List[String] = |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
342 |
lookup(str) match { |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
343 |
case Some((true, tree)) => content(tree, List(str.toString)) |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
344 |
case Some((false, tree)) => content(tree, Nil) |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
345 |
case None => Nil |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
346 |
} |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
347 |
|
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
348 |
|
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
349 |
/* pseudo Set methods */ |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
350 |
|
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
351 |
def iterator: Iterator[String] = content(rep, Nil).sorted.iterator |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
352 |
|
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
353 |
override def toString: String = iterator.mkString("Lexicon(", ", ", ")") |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
354 |
|
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
355 |
def empty: Lexicon = Lexicon.empty |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
356 |
def isEmpty: Boolean = rep.branches.isEmpty |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
357 |
|
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
358 |
def contains(elem: String): Boolean = |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
359 |
lookup(elem) match { |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
360 |
case Some((tip, _)) => tip |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
361 |
case _ => false |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
362 |
} |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
363 |
|
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
364 |
|
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
365 |
/* add elements */ |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
366 |
|
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
367 |
def + (elem: String): Lexicon = |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
368 |
if (contains(elem)) this |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
369 |
else { |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
370 |
val len = elem.length |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
371 |
def extend(tree: Lexicon.Tree, i: Int): Lexicon.Tree = |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
372 |
if (i < len) { |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
373 |
val c = elem.charAt(i) |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
374 |
val end = (i + 1 == len) |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
375 |
tree.branches.get(c) match { |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
376 |
case Some((s, tr)) => |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
377 |
Lexicon.Tree(tree.branches + |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
378 |
(c -> (if (end) elem else s, extend(tr, i + 1)))) |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
379 |
case None => |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
380 |
Lexicon.Tree(tree.branches + |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
381 |
(c -> (if (end) elem else "", extend(Lexicon.empty_tree, i + 1)))) |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
382 |
} |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
383 |
} |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
384 |
else tree |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
385 |
new Lexicon(extend(rep, 0)) |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
386 |
} |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
387 |
|
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
388 |
def ++ (elems: TraversableOnce[String]): Lexicon = (this /: elems)(_ + _) |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
389 |
|
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
390 |
|
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
391 |
/* scan */ |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
392 |
|
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
393 |
def scan(in: Reader[Char]): String = |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
394 |
{ |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
395 |
val source = in.source |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
396 |
val offset = in.offset |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
397 |
val len = source.length - offset |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
398 |
|
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
399 |
def scan_tree(tree: Lexicon.Tree, result: String, i: Int): String = |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
400 |
{ |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
401 |
if (i < len) { |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
402 |
tree.branches.get(source.charAt(offset + i)) match { |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
403 |
case Some((s, tr)) => scan_tree(tr, if (s.isEmpty) result else s, i + 1) |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
404 |
case None => result |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
405 |
} |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
406 |
} |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
407 |
else result |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
408 |
} |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
409 |
scan_tree(rep, "", 0) |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
410 |
} |
28d4db6c6e79
tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents:
55034
diff
changeset
|
411 |
} |
56821 | 412 |
|
413 |
||
414 |
||
56822 | 415 |
/** read stream without decoding: efficient length operation **/ |
56821 | 416 |
|
417 |
private class Restricted_Seq(seq: IndexedSeq[Char], start: Int, end: Int) |
|
418 |
extends CharSequence |
|
419 |
{ |
|
420 |
def charAt(i: Int): Char = |
|
421 |
if (0 <= i && i < length) seq(start + i) |
|
422 |
else throw new IndexOutOfBoundsException |
|
423 |
||
56822 | 424 |
def length: Int = end - start // avoid expensive seq.length |
56821 | 425 |
|
426 |
def subSequence(i: Int, j: Int): CharSequence = |
|
427 |
if (0 <= i && i <= j && j <= length) new Restricted_Seq(seq, start + i, start + j) |
|
428 |
else throw new IndexOutOfBoundsException |
|
429 |
||
430 |
override def toString: String = |
|
431 |
{ |
|
432 |
val buf = new StringBuilder(length) |
|
433 |
for (offset <- start until end) buf.append(seq(offset)) |
|
434 |
buf.toString |
|
435 |
} |
|
436 |
} |
|
437 |
||
438 |
abstract class Byte_Reader extends Reader[Char] { def close: Unit } |
|
439 |
||
56822 | 440 |
private def make_byte_reader(stream: InputStream, stream_length: Int): Byte_Reader = |
56821 | 441 |
{ |
56822 | 442 |
val buffered_stream = new BufferedInputStream(stream) |
56821 | 443 |
val seq = new PagedSeq( |
444 |
(buf: Array[Char], offset: Int, length: Int) => |
|
445 |
{ |
|
446 |
var i = 0 |
|
447 |
var c = 0 |
|
448 |
var eof = false |
|
449 |
while (!eof && i < length) { |
|
56822 | 450 |
c = buffered_stream.read |
56821 | 451 |
if (c == -1) eof = true |
452 |
else { buf(offset + i) = c.toChar; i += 1 } |
|
453 |
} |
|
454 |
if (i > 0) i else -1 |
|
455 |
}) |
|
56822 | 456 |
val restricted_seq = new Restricted_Seq(seq, 0, stream_length) |
56821 | 457 |
|
458 |
class Paged_Reader(override val offset: Int) extends Byte_Reader |
|
459 |
{ |
|
460 |
override lazy val source: CharSequence = restricted_seq |
|
56827 | 461 |
def first: Char = if (seq.isDefinedAt(offset)) seq(offset) else '\u001a' |
56821 | 462 |
def rest: Paged_Reader = if (seq.isDefinedAt(offset)) new Paged_Reader(offset + 1) else this |
463 |
def pos: InputPosition = new OffsetPosition(source, offset) |
|
464 |
def atEnd: Boolean = !seq.isDefinedAt(offset) |
|
465 |
override def drop(n: Int): Paged_Reader = new Paged_Reader(offset + n) |
|
56822 | 466 |
def close { buffered_stream.close } |
56821 | 467 |
} |
468 |
new Paged_Reader(0) |
|
469 |
} |
|
56822 | 470 |
|
471 |
def byte_reader(file: JFile): Byte_Reader = |
|
472 |
make_byte_reader(new FileInputStream(file), file.length.toInt) |
|
473 |
||
474 |
def byte_reader(url: URL): Byte_Reader = |
|
475 |
{ |
|
476 |
val connection = url.openConnection |
|
477 |
val stream = connection.getInputStream |
|
478 |
val stream_length = connection.getContentLength |
|
479 |
make_byte_reader(stream, stream_length) |
|
480 |
} |
|
31648 | 481 |
} |