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