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