src/Pure/General/scan.scala
author wenzelm
Sat, 16 Jan 2021 22:52:43 +0100
changeset 73136 ca17e9ebfdf1
parent 73120 c3589f2dff31
child 73340 0ffcad1f6130
permissions -rw-r--r--
updated to scala-2.13.4;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31648
31b1f296515b Efficient scanning of literals.
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/General/scan.scala
31b1f296515b Efficient scanning of literals.
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
31b1f296515b Efficient scanning of literals.
wenzelm
parents:
diff changeset
     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
31b1f296515b Efficient scanning of literals.
wenzelm
parents:
diff changeset
     5
*/
31b1f296515b Efficient scanning of literals.
wenzelm
parents:
diff changeset
     6
31b1f296515b Efficient scanning of literals.
wenzelm
parents:
diff changeset
     7
package isabelle
31b1f296515b Efficient scanning of literals.
wenzelm
parents:
diff changeset
     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
wenzelm
parents: 55510
diff changeset
    10
import scala.annotation.tailrec
63579
73939a9b70a3 support 'abbrevs' within theory header;
wenzelm
parents: 60215
diff changeset
    11
import scala.collection.{IndexedSeq, Traversable, TraversableOnce}
71601
97ccf48c2f0c misc tuning based on hints by IntelliJ IDEA;
wenzelm
parents: 71383
diff changeset
    12
import scala.util.matching.Regex
64824
330ec9bc4b75 tuned signature;
wenzelm
parents: 63579
diff changeset
    13
import scala.util.parsing.input.{OffsetPosition, Position => InputPosition,
73136
ca17e9ebfdf1 updated to scala-2.13.4;
wenzelm
parents: 73120
diff changeset
    14
  Reader, CharSequenceReader, PagedSeq}
31648
31b1f296515b Efficient scanning of literals.
wenzelm
parents:
diff changeset
    15
import scala.util.parsing.combinator.RegexParsers
56822
6101243e6740 support URLs as well;
wenzelm
parents: 56821
diff changeset
    16
import java.io.{File => JFile, BufferedInputStream, FileInputStream, InputStream}
6101243e6740 support URLs as well;
wenzelm
parents: 56821
diff changeset
    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
31b1f296515b Efficient scanning of literals.
wenzelm
parents:
diff changeset
    19
31b1f296515b Efficient scanning of literals.
wenzelm
parents:
diff changeset
    20
object Scan
31b1f296515b Efficient scanning of literals.
wenzelm
parents:
diff changeset
    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
67438
fdb7b995974d clarified modules;
wenzelm
parents: 67365
diff changeset
    29
  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
    30
  case class Cartouche_Comment(depth: Int) extends Line_Context
55510
1585a65aad64 tuned signature -- emphasize line-oriented aspect;
wenzelm
parents: 55499
diff changeset
    31
  case class Comment(depth: Int) extends Line_Context
43411
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
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
    34
55492
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
    35
  /** parser combinators **/
31650
cfaed41ee2c5 added completions;
wenzelm
parents: 31649
diff changeset
    36
55492
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
    37
  object Parsers extends Parsers
36011
3ff725ac13a4 adapted to Scala 2.8.0 Beta1 -- with notable changes to scala.collection;
wenzelm
parents: 34316
diff changeset
    38
55492
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
    39
  trait Parsers extends RegexParsers
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
    40
  {
71601
97ccf48c2f0c misc tuning based on hints by IntelliJ IDEA;
wenzelm
parents: 71383
diff changeset
    41
    override val whiteSpace: Regex = "".r
31649
a11ea667d676 reorganized and abstracted version, via Set trait;
wenzelm
parents: 31648
diff changeset
    42
34137
6cc9a0cbaf55 refined some Symbol operations/signatures;
wenzelm
parents: 34135
diff changeset
    43
43411
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
    44
    /* optional termination */
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
    45
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
    46
    def opt_term[T](p: => Parser[T]): Parser[Option[T]] =
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
    47
      p ^^ (x => Some(x)) | """\z""".r ^^ (_ => None)
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
    48
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
    49
55497
c0f8aebfb43d lexical syntax for SML (in Scala);
wenzelm
parents: 55494
diff changeset
    50
    /* repeated symbols */
34137
6cc9a0cbaf55 refined some Symbol operations/signatures;
wenzelm
parents: 34135
diff changeset
    51
55497
c0f8aebfb43d lexical syntax for SML (in Scala);
wenzelm
parents: 55494
diff changeset
    52
    def repeated(pred: Symbol.Symbol => Boolean, min_count: Int, max_count: Int): Parser[String] =
34140
31be1235d0fb simplified result of keyword and symbols parser;
wenzelm
parents: 34137
diff changeset
    53
      new Parser[String]
34135
63dd95e3b393 indicate final state of keywords;
wenzelm
parents: 32450
diff changeset
    54
      {
34137
6cc9a0cbaf55 refined some Symbol operations/signatures;
wenzelm
parents: 34135
diff changeset
    55
        def apply(in: Input) =
6cc9a0cbaf55 refined some Symbol operations/signatures;
wenzelm
parents: 34135
diff changeset
    56
        {
6cc9a0cbaf55 refined some Symbol operations/signatures;
wenzelm
parents: 34135
diff changeset
    57
          val start = in.offset
6cc9a0cbaf55 refined some Symbol operations/signatures;
wenzelm
parents: 34135
diff changeset
    58
          val end = in.source.length
6cc9a0cbaf55 refined some Symbol operations/signatures;
wenzelm
parents: 34135
diff changeset
    59
          val matcher = new Symbol.Matcher(in.source)
6cc9a0cbaf55 refined some Symbol operations/signatures;
wenzelm
parents: 34135
diff changeset
    60
6cc9a0cbaf55 refined some Symbol operations/signatures;
wenzelm
parents: 34135
diff changeset
    61
          var i = start
6cc9a0cbaf55 refined some Symbol operations/signatures;
wenzelm
parents: 34135
diff changeset
    62
          var count = 0
6cc9a0cbaf55 refined some Symbol operations/signatures;
wenzelm
parents: 34135
diff changeset
    63
          var finished = false
55034
wenzelm
parents: 55033
diff changeset
    64
          while (!finished && i < end && count < max_count) {
wenzelm
parents: 55033
diff changeset
    65
            val n = matcher(i, end)
wenzelm
parents: 55033
diff changeset
    66
            val sym = in.source.subSequence(i, i + n).toString
wenzelm
parents: 55033
diff changeset
    67
            if (pred(sym)) { i += n; count += 1 }
34137
6cc9a0cbaf55 refined some Symbol operations/signatures;
wenzelm
parents: 34135
diff changeset
    68
            else finished = true
6cc9a0cbaf55 refined some Symbol operations/signatures;
wenzelm
parents: 34135
diff changeset
    69
          }
34140
31be1235d0fb simplified result of keyword and symbols parser;
wenzelm
parents: 34137
diff changeset
    70
          if (count < min_count) Failure("bad input", in)
31be1235d0fb simplified result of keyword and symbols parser;
wenzelm
parents: 34137
diff changeset
    71
          else Success(in.source.subSequence(start, i).toString, in.drop(i - start))
34137
6cc9a0cbaf55 refined some Symbol operations/signatures;
wenzelm
parents: 34135
diff changeset
    72
        }
55497
c0f8aebfb43d lexical syntax for SML (in Scala);
wenzelm
parents: 55494
diff changeset
    73
      }.named("repeated")
34137
6cc9a0cbaf55 refined some Symbol operations/signatures;
wenzelm
parents: 34135
diff changeset
    74
43696
58bb7ca5c7a2 explicit indication of type Symbol.Symbol;
wenzelm
parents: 43695
diff changeset
    75
    def one(pred: Symbol.Symbol => Boolean): Parser[String] =
55497
c0f8aebfb43d lexical syntax for SML (in Scala);
wenzelm
parents: 55494
diff changeset
    76
      repeated(pred, 1, 1)
43696
58bb7ca5c7a2 explicit indication of type Symbol.Symbol;
wenzelm
parents: 43695
diff changeset
    77
66922
5a476a87a535 separate JSON lexer;
wenzelm
parents: 66918
diff changeset
    78
    def maybe(pred: Symbol.Symbol => Boolean): Parser[String] =
5a476a87a535 separate JSON lexer;
wenzelm
parents: 66918
diff changeset
    79
      repeated(pred, 0, 1)
5a476a87a535 separate JSON lexer;
wenzelm
parents: 66918
diff changeset
    80
43696
58bb7ca5c7a2 explicit indication of type Symbol.Symbol;
wenzelm
parents: 43695
diff changeset
    81
    def many(pred: Symbol.Symbol => Boolean): Parser[String] =
55497
c0f8aebfb43d lexical syntax for SML (in Scala);
wenzelm
parents: 55494
diff changeset
    82
      repeated(pred, 0, Integer.MAX_VALUE)
43696
58bb7ca5c7a2 explicit indication of type Symbol.Symbol;
wenzelm
parents: 43695
diff changeset
    83
58bb7ca5c7a2 explicit indication of type Symbol.Symbol;
wenzelm
parents: 43695
diff changeset
    84
    def many1(pred: Symbol.Symbol => Boolean): Parser[String] =
55497
c0f8aebfb43d lexical syntax for SML (in Scala);
wenzelm
parents: 55494
diff changeset
    85
      repeated(pred, 1, Integer.MAX_VALUE)
c0f8aebfb43d lexical syntax for SML (in Scala);
wenzelm
parents: 55494
diff changeset
    86
c0f8aebfb43d lexical syntax for SML (in Scala);
wenzelm
parents: 55494
diff changeset
    87
c0f8aebfb43d lexical syntax for SML (in Scala);
wenzelm
parents: 55494
diff changeset
    88
    /* character */
c0f8aebfb43d lexical syntax for SML (in Scala);
wenzelm
parents: 55494
diff changeset
    89
c0f8aebfb43d lexical syntax for SML (in Scala);
wenzelm
parents: 55494
diff changeset
    90
    def character(pred: Char => Boolean): Symbol.Symbol => Boolean =
c0f8aebfb43d lexical syntax for SML (in Scala);
wenzelm
parents: 55494
diff changeset
    91
      (s: Symbol. Symbol) => s.length == 1 && pred(s.charAt(0))
34140
31be1235d0fb simplified result of keyword and symbols parser;
wenzelm
parents: 34137
diff changeset
    92
31be1235d0fb simplified result of keyword and symbols parser;
wenzelm
parents: 34137
diff changeset
    93
34143
ded454429df3 added nested comments;
wenzelm
parents: 34140
diff changeset
    94
    /* quoted strings */
34140
31be1235d0fb simplified result of keyword and symbols parser;
wenzelm
parents: 34137
diff changeset
    95
43696
58bb7ca5c7a2 explicit indication of type Symbol.Symbol;
wenzelm
parents: 43695
diff changeset
    96
    private def quoted_body(quote: Symbol.Symbol): Parser[String] =
43411
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
    97
    {
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
    98
      rep(many1(sym => sym != quote && sym != "\\") | "\\" + quote | "\\\\" |
60215
5fb4990dfc73 misc tuning, based on warnings by IntelliJ IDEA;
wenzelm
parents: 59319
diff changeset
    99
        ("""\\\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
   100
    }
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   101
55494
009b71c1ed23 tuned signature (in accordance to ML version);
wenzelm
parents: 55492
diff changeset
   102
    def quoted(quote: Symbol.Symbol): Parser[String] =
34143
ded454429df3 added nested comments;
wenzelm
parents: 34140
diff changeset
   103
    {
43411
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   104
      quote ~ quoted_body(quote) ~ quote ^^ { case x ~ y ~ z => x + y + z }
34143
ded454429df3 added nested comments;
wenzelm
parents: 34140
diff changeset
   105
    }.named("quoted")
34140
31be1235d0fb simplified result of keyword and symbols parser;
wenzelm
parents: 34137
diff changeset
   106
43696
58bb7ca5c7a2 explicit indication of type Symbol.Symbol;
wenzelm
parents: 43695
diff changeset
   107
    def quoted_content(quote: Symbol.Symbol, source: String): String =
34140
31be1235d0fb simplified result of keyword and symbols parser;
wenzelm
parents: 34137
diff changeset
   108
    {
73120
c3589f2dff31 more informative errors: simplify diagnosis of spurious failures reported by users;
wenzelm
parents: 71601
diff changeset
   109
      require(parseAll(quoted(quote), source).successful, "no quoted text")
34189
10c5871ec684 quoted_content: handle escapes;
wenzelm
parents: 34187
diff changeset
   110
      val body = source.substring(1, source.length - 1)
10c5871ec684 quoted_content: handle escapes;
wenzelm
parents: 34187
diff changeset
   111
      if (body.exists(_ == '\\')) {
10c5871ec684 quoted_content: handle escapes;
wenzelm
parents: 34187
diff changeset
   112
        val content =
40523
1050315f6ee2 simplified/robustified treatment of malformed symbols, which are now fully internalized (total Symbol.explode etc.);
wenzelm
parents: 40290
diff changeset
   113
          rep(many1(sym => sym != quote && sym != "\\") |
34189
10c5871ec684 quoted_content: handle escapes;
wenzelm
parents: 34187
diff changeset
   114
              "\\" ~> (quote | "\\" | """\d\d\d""".r ^^ { case x => x.toInt.toChar.toString }))
10c5871ec684 quoted_content: handle escapes;
wenzelm
parents: 34187
diff changeset
   115
        parseAll(content ^^ (_.mkString), body).get
10c5871ec684 quoted_content: handle escapes;
wenzelm
parents: 34187
diff changeset
   116
      }
10c5871ec684 quoted_content: handle escapes;
wenzelm
parents: 34187
diff changeset
   117
      else body
34140
31be1235d0fb simplified result of keyword and symbols parser;
wenzelm
parents: 34137
diff changeset
   118
    }
31be1235d0fb simplified result of keyword and symbols parser;
wenzelm
parents: 34137
diff changeset
   119
55510
1585a65aad64 tuned signature -- emphasize line-oriented aspect;
wenzelm
parents: 55499
diff changeset
   120
    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
   121
    {
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   122
      ctxt match {
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   123
        case Finished =>
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   124
          quote ~ quoted_body(quote) ~ opt_term(quote) ^^
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   125
            { case x ~ y ~ Some(z) => (x + y + z, Finished)
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   126
              case x ~ y ~ None => (x + y, Quoted(quote)) }
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   127
        case Quoted(q) if q == quote =>
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   128
          quoted_body(quote) ~ opt_term(quote) ^^
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   129
            { case x ~ Some(y) => (x + y, Finished)
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   130
              case x ~ None => (x, ctxt) }
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   131
        case _ => failure("")
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   132
      }
55510
1585a65aad64 tuned signature -- emphasize line-oriented aspect;
wenzelm
parents: 55499
diff changeset
   133
    }.named("quoted_line")
43411
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   134
48763
de68fc11c245 more precise recover_quoted, recover_verbatim, recover_comment (cf. ML version) -- NB: context parsers expect explicit termination;
wenzelm
parents: 48754
diff changeset
   135
    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
   136
      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
   137
34140
31be1235d0fb simplified result of keyword and symbols parser;
wenzelm
parents: 34137
diff changeset
   138
34143
ded454429df3 added nested comments;
wenzelm
parents: 34140
diff changeset
   139
    /* verbatim text */
ded454429df3 added nested comments;
wenzelm
parents: 34140
diff changeset
   140
43411
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   141
    private def verbatim_body: Parser[String] =
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   142
      rep(many1(sym => sym != "*") | """\*(?!\})""".r) ^^ (_.mkString)
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   143
55494
009b71c1ed23 tuned signature (in accordance to ML version);
wenzelm
parents: 55492
diff changeset
   144
    def verbatim: Parser[String] =
34143
ded454429df3 added nested comments;
wenzelm
parents: 34140
diff changeset
   145
    {
43411
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   146
      "{*" ~ verbatim_body ~ "*}" ^^ { case x ~ y ~ z => x + y + z }
34143
ded454429df3 added nested comments;
wenzelm
parents: 34140
diff changeset
   147
    }.named("verbatim")
34140
31be1235d0fb simplified result of keyword and symbols parser;
wenzelm
parents: 34137
diff changeset
   148
31be1235d0fb simplified result of keyword and symbols parser;
wenzelm
parents: 34137
diff changeset
   149
    def verbatim_content(source: String): String =
31be1235d0fb simplified result of keyword and symbols parser;
wenzelm
parents: 34137
diff changeset
   150
    {
73120
c3589f2dff31 more informative errors: simplify diagnosis of spurious failures reported by users;
wenzelm
parents: 71601
diff changeset
   151
      require(parseAll(verbatim, source).successful, "no verbatim text")
34140
31be1235d0fb simplified result of keyword and symbols parser;
wenzelm
parents: 34137
diff changeset
   152
      source.substring(2, source.length - 2)
31be1235d0fb simplified result of keyword and symbols parser;
wenzelm
parents: 34137
diff changeset
   153
    }
31be1235d0fb simplified result of keyword and symbols parser;
wenzelm
parents: 34137
diff changeset
   154
55510
1585a65aad64 tuned signature -- emphasize line-oriented aspect;
wenzelm
parents: 55499
diff changeset
   155
    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
   156
    {
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   157
      ctxt match {
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   158
        case Finished =>
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   159
          "{*" ~ verbatim_body ~ opt_term("*}") ^^
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   160
            { case x ~ y ~ Some(z) => (x + y + z, Finished)
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   161
              case x ~ y ~ None => (x + y, Verbatim) }
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   162
        case Verbatim =>
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   163
          verbatim_body ~ opt_term("*}") ^^
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   164
            { case x ~ Some(y) => (x + y, Finished)
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   165
              case x ~ None => (x, Verbatim) }
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   166
        case _ => failure("")
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   167
      }
55510
1585a65aad64 tuned signature -- emphasize line-oriented aspect;
wenzelm
parents: 55499
diff changeset
   168
    }.named("verbatim_line")
43411
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   169
48763
de68fc11c245 more precise recover_quoted, recover_verbatim, recover_comment (cf. ML version) -- NB: context parsers expect explicit termination;
wenzelm
parents: 48754
diff changeset
   170
    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
   171
      "{*" ~ 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
   172
34140
31be1235d0fb simplified result of keyword and symbols parser;
wenzelm
parents: 34137
diff changeset
   173
55033
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   174
    /* nested text cartouches */
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   175
67438
fdb7b995974d clarified modules;
wenzelm
parents: 67365
diff changeset
   176
    def cartouche_depth(depth: Int): Parser[(String, Int)] = new Parser[(String, Int)]
55033
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   177
    {
73120
c3589f2dff31 more informative errors: simplify diagnosis of spurious failures reported by users;
wenzelm
parents: 71601
diff changeset
   178
      require(depth >= 0, "bad cartouche depth")
55033
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   179
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   180
      def apply(in: Input) =
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   181
      {
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   182
        val start = in.offset
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   183
        val end = in.source.length
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   184
        val matcher = new Symbol.Matcher(in.source)
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   185
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   186
        var i = start
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   187
        var d = depth
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   188
        var finished = false
55034
wenzelm
parents: 55033
diff changeset
   189
        while (!finished && i < end) {
wenzelm
parents: 55033
diff changeset
   190
          val n = matcher(i, end)
wenzelm
parents: 55033
diff changeset
   191
          val sym = in.source.subSequence(i, i + n).toString
wenzelm
parents: 55033
diff changeset
   192
          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
   193
          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
   194
          else if (d > 0) i += n
55033
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   195
          else finished = true
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   196
        }
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   197
        if (i == start) Failure("bad input", in)
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   198
        else Success((in.source.subSequence(start, i).toString, d), in.drop(i - start))
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   199
      }
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   200
    }.named("cartouche_depth")
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   201
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   202
    def cartouche: Parser[String] =
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   203
      cartouche_depth(0) ^? { case (x, d) if d == 0 => x }
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   204
55510
1585a65aad64 tuned signature -- emphasize line-oriented aspect;
wenzelm
parents: 55499
diff changeset
   205
    def cartouche_line(ctxt: Line_Context): Parser[(String, Line_Context)] =
55033
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   206
    {
67364
wenzelm
parents: 66922
diff changeset
   207
      def cartouche_context(d: Int): Line_Context =
wenzelm
parents: 66922
diff changeset
   208
        if (d == 0) Finished else Cartouche(d)
wenzelm
parents: 66922
diff changeset
   209
wenzelm
parents: 66922
diff changeset
   210
      ctxt match {
wenzelm
parents: 66922
diff changeset
   211
        case Finished =>
wenzelm
parents: 66922
diff changeset
   212
          cartouche_depth(0) ^^ { case (c, d) => (c, cartouche_context(d)) }
wenzelm
parents: 66922
diff changeset
   213
        case Cartouche(depth) =>
wenzelm
parents: 66922
diff changeset
   214
          cartouche_depth(depth) ^^ { case (c, d) => (c, cartouche_context(d)) }
wenzelm
parents: 66922
diff changeset
   215
        case _ => failure("")
wenzelm
parents: 66922
diff changeset
   216
      }
55033
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   217
    }
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   218
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   219
    val recover_cartouche: Parser[String] =
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   220
      cartouche_depth(0) ^^ (_._1)
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   221
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   222
    def cartouche_content(source: String): String =
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   223
    {
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   224
      def err(): Nothing = error("Malformed text cartouche: " + quote(source))
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   225
      val source1 =
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   226
        Library.try_unprefix(Symbol.open_decoded, source) orElse
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   227
          Library.try_unprefix(Symbol.open, source) getOrElse err()
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   228
      Library.try_unsuffix(Symbol.close_decoded, source1) orElse
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   229
        Library.try_unsuffix(Symbol.close, source1) getOrElse err()
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   230
    }
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   231
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   232
34143
ded454429df3 added nested comments;
wenzelm
parents: 34140
diff changeset
   233
    /* nested comments */
ded454429df3 added nested comments;
wenzelm
parents: 34140
diff changeset
   234
43412
81517eed8a78 partial scans of nested comments;
wenzelm
parents: 43411
diff changeset
   235
    private def comment_depth(depth: Int): Parser[(String, Int)] = new Parser[(String, Int)]
34143
ded454429df3 added nested comments;
wenzelm
parents: 34140
diff changeset
   236
    {
73120
c3589f2dff31 more informative errors: simplify diagnosis of spurious failures reported by users;
wenzelm
parents: 71601
diff changeset
   237
      require(depth >= 0, "bad comment depth")
43412
81517eed8a78 partial scans of nested comments;
wenzelm
parents: 43411
diff changeset
   238
71601
97ccf48c2f0c misc tuning based on hints by IntelliJ IDEA;
wenzelm
parents: 71383
diff changeset
   239
      val comment_text: Parser[List[String]] =
42441
781c622af16a more robust scanning of iterated comments, such as "(* (**) (**) *)";
wenzelm
parents: 40523
diff changeset
   240
        rep1(many1(sym => sym != "*" && sym != "(") | """\*(?!\))|\((?!\*)""".r)
34143
ded454429df3 added nested comments;
wenzelm
parents: 34140
diff changeset
   241
ded454429df3 added nested comments;
wenzelm
parents: 34140
diff changeset
   242
      def apply(in: Input) =
ded454429df3 added nested comments;
wenzelm
parents: 34140
diff changeset
   243
      {
ded454429df3 added nested comments;
wenzelm
parents: 34140
diff changeset
   244
        var rest = in
42441
781c622af16a more robust scanning of iterated comments, such as "(* (**) (**) *)";
wenzelm
parents: 40523
diff changeset
   245
        def try_parse[A](p: Parser[A]): Boolean =
34143
ded454429df3 added nested comments;
wenzelm
parents: 34140
diff changeset
   246
        {
56663
2d09b437c168 avoid "Adaptation of argument list by inserting ()" -- deprecated in scala-2.11.0;
wenzelm
parents: 55980
diff changeset
   247
          parse(p ^^^ (()), rest) match {
34143
ded454429df3 added nested comments;
wenzelm
parents: 34140
diff changeset
   248
            case Success(_, next) => { rest = next; true }
ded454429df3 added nested comments;
wenzelm
parents: 34140
diff changeset
   249
            case _ => false
ded454429df3 added nested comments;
wenzelm
parents: 34140
diff changeset
   250
          }
ded454429df3 added nested comments;
wenzelm
parents: 34140
diff changeset
   251
        }
43412
81517eed8a78 partial scans of nested comments;
wenzelm
parents: 43411
diff changeset
   252
        var d = depth
34143
ded454429df3 added nested comments;
wenzelm
parents: 34140
diff changeset
   253
        var finished = false
ded454429df3 added nested comments;
wenzelm
parents: 34140
diff changeset
   254
        while (!finished) {
43412
81517eed8a78 partial scans of nested comments;
wenzelm
parents: 43411
diff changeset
   255
          if (try_parse("(*")) d += 1
58505
d1fe47fe5401 actually finish after closing, e.g. relevant for consecutive (**)(**);
wenzelm
parents: 56827
diff changeset
   256
          else if (d > 0 && try_parse("*)")) { d -= 1; if (d == 0) finished = true }
43412
81517eed8a78 partial scans of nested comments;
wenzelm
parents: 43411
diff changeset
   257
          else if (d == 0 || !try_parse(comment_text)) finished = true
34143
ded454429df3 added nested comments;
wenzelm
parents: 34140
diff changeset
   258
        }
43412
81517eed8a78 partial scans of nested comments;
wenzelm
parents: 43411
diff changeset
   259
        if (in.offset < rest.offset)
81517eed8a78 partial scans of nested comments;
wenzelm
parents: 43411
diff changeset
   260
          Success((in.source.subSequence(in.offset, rest.offset).toString, d), rest)
34143
ded454429df3 added nested comments;
wenzelm
parents: 34140
diff changeset
   261
        else Failure("comment expected", in)
ded454429df3 added nested comments;
wenzelm
parents: 34140
diff changeset
   262
      }
43412
81517eed8a78 partial scans of nested comments;
wenzelm
parents: 43411
diff changeset
   263
    }.named("comment_depth")
81517eed8a78 partial scans of nested comments;
wenzelm
parents: 43411
diff changeset
   264
81517eed8a78 partial scans of nested comments;
wenzelm
parents: 43411
diff changeset
   265
    def comment: Parser[String] =
81517eed8a78 partial scans of nested comments;
wenzelm
parents: 43411
diff changeset
   266
      comment_depth(0) ^? { case (x, d) if d == 0 => x }
81517eed8a78 partial scans of nested comments;
wenzelm
parents: 43411
diff changeset
   267
55510
1585a65aad64 tuned signature -- emphasize line-oriented aspect;
wenzelm
parents: 55499
diff changeset
   268
    def comment_line(ctxt: Line_Context): Parser[(String, Line_Context)] =
43412
81517eed8a78 partial scans of nested comments;
wenzelm
parents: 43411
diff changeset
   269
    {
81517eed8a78 partial scans of nested comments;
wenzelm
parents: 43411
diff changeset
   270
      val depth =
81517eed8a78 partial scans of nested comments;
wenzelm
parents: 43411
diff changeset
   271
        ctxt match {
81517eed8a78 partial scans of nested comments;
wenzelm
parents: 43411
diff changeset
   272
          case Finished => 0
81517eed8a78 partial scans of nested comments;
wenzelm
parents: 43411
diff changeset
   273
          case Comment(d) => d
81517eed8a78 partial scans of nested comments;
wenzelm
parents: 43411
diff changeset
   274
          case _ => -1
81517eed8a78 partial scans of nested comments;
wenzelm
parents: 43411
diff changeset
   275
        }
81517eed8a78 partial scans of nested comments;
wenzelm
parents: 43411
diff changeset
   276
      if (depth >= 0)
81517eed8a78 partial scans of nested comments;
wenzelm
parents: 43411
diff changeset
   277
        comment_depth(depth) ^^
81517eed8a78 partial scans of nested comments;
wenzelm
parents: 43411
diff changeset
   278
          { case (x, 0) => (x, Finished)
81517eed8a78 partial scans of nested comments;
wenzelm
parents: 43411
diff changeset
   279
            case (x, d) => (x, Comment(d)) }
81517eed8a78 partial scans of nested comments;
wenzelm
parents: 43411
diff changeset
   280
      else failure("")
81517eed8a78 partial scans of nested comments;
wenzelm
parents: 43411
diff changeset
   281
    }
34143
ded454429df3 added nested comments;
wenzelm
parents: 34140
diff changeset
   282
48763
de68fc11c245 more precise recover_quoted, recover_verbatim, recover_comment (cf. ML version) -- NB: context parsers expect explicit termination;
wenzelm
parents: 48754
diff changeset
   283
    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
   284
      comment_depth(0) ^^ (_._1)
34143
ded454429df3 added nested comments;
wenzelm
parents: 34140
diff changeset
   285
55033
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   286
    def comment_content(source: String): String =
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   287
    {
73120
c3589f2dff31 more informative errors: simplify diagnosis of spurious failures reported by users;
wenzelm
parents: 71601
diff changeset
   288
      require(parseAll(comment, source).successful, "no comment")
55033
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   289
      source.substring(2, source.length - 2)
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   290
    }
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   291
34143
ded454429df3 added nested comments;
wenzelm
parents: 34140
diff changeset
   292
55492
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   293
    /* keyword */
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   294
55497
c0f8aebfb43d lexical syntax for SML (in Scala);
wenzelm
parents: 55494
diff changeset
   295
    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
   296
    {
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   297
      def apply(in: Input) =
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   298
      {
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   299
        val result = lexicon.scan(in)
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   300
        if (result.isEmpty) Failure("keyword expected", in)
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   301
        else Success(result, in.drop(result.length))
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
    }.named("keyword")
31649
a11ea667d676 reorganized and abstracted version, via Set trait;
wenzelm
parents: 31648
diff changeset
   304
  }
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
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   307
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   308
  /** Lexicon -- position tree **/
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   309
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   310
  object Lexicon
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
    /* representation */
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   313
60215
5fb4990dfc73 misc tuning, based on warnings by IntelliJ IDEA;
wenzelm
parents: 59319
diff changeset
   314
    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
   315
    private val empty_tree = Tree(Map())
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
    val empty: Lexicon = new Lexicon(empty_tree)
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   318
    def apply(elems: String*): Lexicon = empty ++ elems
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
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   321
  final class Lexicon private(rep: Lexicon.Tree)
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
    /* auxiliary operations */
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   324
59073
dcecfcc56dce more merge operations;
wenzelm
parents: 58505
diff changeset
   325
    private def dest(tree: Lexicon.Tree, result: List[String]): List[String] =
55492
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   326
      (result /: tree.branches.toList) ((res, entry) =>
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   327
        entry match { case (_, (s, tr)) =>
59073
dcecfcc56dce more merge operations;
wenzelm
parents: 58505
diff changeset
   328
          if (s.isEmpty) dest(tr, res) else dest(tr, s :: res) })
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
    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
   331
    {
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   332
      val len = str.length
55980
wenzelm
parents: 55510
diff changeset
   333
      @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
   334
      {
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   335
        if (i < len) {
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   336
          tree.branches.get(str.charAt(i)) match {
59319
wenzelm
parents: 59073
diff changeset
   337
            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
   338
            case None => None
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   339
          }
55980
wenzelm
parents: 55510
diff changeset
   340
        }
wenzelm
parents: 55510
diff changeset
   341
        else Some(tip, tree)
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
      look(rep, false, 0)
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   344
    }
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   345
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   346
    def completions(str: CharSequence): List[String] =
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   347
      lookup(str) match {
59073
dcecfcc56dce more merge operations;
wenzelm
parents: 58505
diff changeset
   348
        case Some((true, tree)) => dest(tree, List(str.toString))
dcecfcc56dce more merge operations;
wenzelm
parents: 58505
diff changeset
   349
        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
   350
        case None => Nil
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   351
      }
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
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   354
    /* pseudo Set methods */
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   355
59073
dcecfcc56dce more merge operations;
wenzelm
parents: 58505
diff changeset
   356
    def raw_iterator: Iterator[String] = dest(rep, Nil).iterator
dcecfcc56dce more merge operations;
wenzelm
parents: 58505
diff changeset
   357
    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
   358
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   359
    override def toString: String = iterator.mkString("Lexicon(", ", ", ")")
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   360
59073
dcecfcc56dce more merge operations;
wenzelm
parents: 58505
diff changeset
   361
    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
   362
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   363
    def contains(elem: String): Boolean =
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   364
      lookup(elem) match {
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   365
        case Some((tip, _)) => tip
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   366
        case _ => false
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   367
      }
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   368
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   369
59073
dcecfcc56dce more merge operations;
wenzelm
parents: 58505
diff changeset
   370
    /* build lexicon */
55492
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   371
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   372
    def + (elem: String): Lexicon =
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   373
      if (contains(elem)) this
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   374
      else {
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   375
        val len = elem.length
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   376
        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
   377
          if (i < len) {
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   378
            val c = elem.charAt(i)
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   379
            val end = (i + 1 == len)
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   380
            tree.branches.get(c) match {
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   381
              case Some((s, tr)) =>
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   382
                Lexicon.Tree(tree.branches +
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   383
                  (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
   384
              case None =>
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   385
                Lexicon.Tree(tree.branches +
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   386
                  (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
   387
            }
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   388
          }
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   389
          else tree
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   390
        new Lexicon(extend(rep, 0))
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
    def ++ (elems: TraversableOnce[String]): Lexicon = (this /: elems)(_ + _)
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   394
59073
dcecfcc56dce more merge operations;
wenzelm
parents: 58505
diff changeset
   395
    def ++ (other: Lexicon): Lexicon =
dcecfcc56dce more merge operations;
wenzelm
parents: 58505
diff changeset
   396
      if (this eq other) this
dcecfcc56dce more merge operations;
wenzelm
parents: 58505
diff changeset
   397
      else if (is_empty) other
dcecfcc56dce more merge operations;
wenzelm
parents: 58505
diff changeset
   398
      else this ++ other.raw_iterator
dcecfcc56dce more merge operations;
wenzelm
parents: 58505
diff changeset
   399
63579
73939a9b70a3 support 'abbrevs' within theory header;
wenzelm
parents: 60215
diff changeset
   400
    def -- (remove: Traversable[String]): Lexicon =
71601
97ccf48c2f0c misc tuning based on hints by IntelliJ IDEA;
wenzelm
parents: 71383
diff changeset
   401
      if (remove.exists(contains))
63579
73939a9b70a3 support 'abbrevs' within theory header;
wenzelm
parents: 60215
diff changeset
   402
        Lexicon.empty ++ iterator.filterNot(a => remove.exists(b => a == b))
73939a9b70a3 support 'abbrevs' within theory header;
wenzelm
parents: 60215
diff changeset
   403
      else this
73939a9b70a3 support 'abbrevs' within theory header;
wenzelm
parents: 60215
diff changeset
   404
55492
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
    /* scan */
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   407
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   408
    def scan(in: Reader[Char]): String =
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   409
    {
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   410
      val source = in.source
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   411
      val offset = in.offset
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   412
      val len = source.length - offset
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   413
71383
8313dca6dee9 misc tuning, following hint by IntelliJ;
wenzelm
parents: 69393
diff changeset
   414
      @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
   415
      {
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   416
        if (i < len) {
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   417
          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
   418
            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
   419
            case None => result
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   420
          }
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   421
        }
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   422
        else result
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   423
      }
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   424
      scan_tree(rep, "", 0)
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   425
    }
28d4db6c6e79 tuned signature -- separate Lexicon from Parsers (in accordance to ML version);
wenzelm
parents: 55034
diff changeset
   426
  }
56821
2e6d46a3a617 reclaimed Byte_Reader from 51560e392e1b;
wenzelm
parents: 56663
diff changeset
   427
2e6d46a3a617 reclaimed Byte_Reader from 51560e392e1b;
wenzelm
parents: 56663
diff changeset
   428
2e6d46a3a617 reclaimed Byte_Reader from 51560e392e1b;
wenzelm
parents: 56663
diff changeset
   429
56822
6101243e6740 support URLs as well;
wenzelm
parents: 56821
diff changeset
   430
  /** read stream without decoding: efficient length operation **/
56821
2e6d46a3a617 reclaimed Byte_Reader from 51560e392e1b;
wenzelm
parents: 56663
diff changeset
   431
2e6d46a3a617 reclaimed Byte_Reader from 51560e392e1b;
wenzelm
parents: 56663
diff changeset
   432
  private class Restricted_Seq(seq: IndexedSeq[Char], start: Int, end: Int)
2e6d46a3a617 reclaimed Byte_Reader from 51560e392e1b;
wenzelm
parents: 56663
diff changeset
   433
    extends CharSequence
2e6d46a3a617 reclaimed Byte_Reader from 51560e392e1b;
wenzelm
parents: 56663
diff changeset
   434
  {
2e6d46a3a617 reclaimed Byte_Reader from 51560e392e1b;
wenzelm
parents: 56663
diff changeset
   435
    def charAt(i: Int): Char =
2e6d46a3a617 reclaimed Byte_Reader from 51560e392e1b;
wenzelm
parents: 56663
diff changeset
   436
      if (0 <= i && i < length) seq(start + i)
2e6d46a3a617 reclaimed Byte_Reader from 51560e392e1b;
wenzelm
parents: 56663
diff changeset
   437
      else throw new IndexOutOfBoundsException
2e6d46a3a617 reclaimed Byte_Reader from 51560e392e1b;
wenzelm
parents: 56663
diff changeset
   438
56822
6101243e6740 support URLs as well;
wenzelm
parents: 56821
diff changeset
   439
    def length: Int = end - start  // avoid expensive seq.length
56821
2e6d46a3a617 reclaimed Byte_Reader from 51560e392e1b;
wenzelm
parents: 56663
diff changeset
   440
2e6d46a3a617 reclaimed Byte_Reader from 51560e392e1b;
wenzelm
parents: 56663
diff changeset
   441
    def subSequence(i: Int, j: Int): CharSequence =
2e6d46a3a617 reclaimed Byte_Reader from 51560e392e1b;
wenzelm
parents: 56663
diff changeset
   442
      if (0 <= i && i <= j && j <= length) new Restricted_Seq(seq, start + i, start + j)
2e6d46a3a617 reclaimed Byte_Reader from 51560e392e1b;
wenzelm
parents: 56663
diff changeset
   443
      else throw new IndexOutOfBoundsException
2e6d46a3a617 reclaimed Byte_Reader from 51560e392e1b;
wenzelm
parents: 56663
diff changeset
   444
2e6d46a3a617 reclaimed Byte_Reader from 51560e392e1b;
wenzelm
parents: 56663
diff changeset
   445
    override def toString: String =
2e6d46a3a617 reclaimed Byte_Reader from 51560e392e1b;
wenzelm
parents: 56663
diff changeset
   446
    {
2e6d46a3a617 reclaimed Byte_Reader from 51560e392e1b;
wenzelm
parents: 56663
diff changeset
   447
      val buf = new StringBuilder(length)
2e6d46a3a617 reclaimed Byte_Reader from 51560e392e1b;
wenzelm
parents: 56663
diff changeset
   448
      for (offset <- start until end) buf.append(seq(offset))
2e6d46a3a617 reclaimed Byte_Reader from 51560e392e1b;
wenzelm
parents: 56663
diff changeset
   449
      buf.toString
2e6d46a3a617 reclaimed Byte_Reader from 51560e392e1b;
wenzelm
parents: 56663
diff changeset
   450
    }
2e6d46a3a617 reclaimed Byte_Reader from 51560e392e1b;
wenzelm
parents: 56663
diff changeset
   451
  }
2e6d46a3a617 reclaimed Byte_Reader from 51560e392e1b;
wenzelm
parents: 56663
diff changeset
   452
69393
ed0824ef337e static type for Library.using: avoid Java 11 warnings on "Illegal reflective access";
wenzelm
parents: 67438
diff changeset
   453
  abstract class Byte_Reader extends Reader[Char] with AutoCloseable
56821
2e6d46a3a617 reclaimed Byte_Reader from 51560e392e1b;
wenzelm
parents: 56663
diff changeset
   454
56822
6101243e6740 support URLs as well;
wenzelm
parents: 56821
diff changeset
   455
  private def make_byte_reader(stream: InputStream, stream_length: Int): Byte_Reader =
56821
2e6d46a3a617 reclaimed Byte_Reader from 51560e392e1b;
wenzelm
parents: 56663
diff changeset
   456
  {
56822
6101243e6740 support URLs as well;
wenzelm
parents: 56821
diff changeset
   457
    val buffered_stream = new BufferedInputStream(stream)
56821
2e6d46a3a617 reclaimed Byte_Reader from 51560e392e1b;
wenzelm
parents: 56663
diff changeset
   458
    val seq = new PagedSeq(
2e6d46a3a617 reclaimed Byte_Reader from 51560e392e1b;
wenzelm
parents: 56663
diff changeset
   459
      (buf: Array[Char], offset: Int, length: Int) =>
2e6d46a3a617 reclaimed Byte_Reader from 51560e392e1b;
wenzelm
parents: 56663
diff changeset
   460
        {
2e6d46a3a617 reclaimed Byte_Reader from 51560e392e1b;
wenzelm
parents: 56663
diff changeset
   461
          var i = 0
2e6d46a3a617 reclaimed Byte_Reader from 51560e392e1b;
wenzelm
parents: 56663
diff changeset
   462
          var c = 0
2e6d46a3a617 reclaimed Byte_Reader from 51560e392e1b;
wenzelm
parents: 56663
diff changeset
   463
          var eof = false
2e6d46a3a617 reclaimed Byte_Reader from 51560e392e1b;
wenzelm
parents: 56663
diff changeset
   464
          while (!eof && i < length) {
56822
6101243e6740 support URLs as well;
wenzelm
parents: 56821
diff changeset
   465
            c = buffered_stream.read
56821
2e6d46a3a617 reclaimed Byte_Reader from 51560e392e1b;
wenzelm
parents: 56663
diff changeset
   466
            if (c == -1) eof = true
2e6d46a3a617 reclaimed Byte_Reader from 51560e392e1b;
wenzelm
parents: 56663
diff changeset
   467
            else { buf(offset + i) = c.toChar; i += 1 }
2e6d46a3a617 reclaimed Byte_Reader from 51560e392e1b;
wenzelm
parents: 56663
diff changeset
   468
          }
2e6d46a3a617 reclaimed Byte_Reader from 51560e392e1b;
wenzelm
parents: 56663
diff changeset
   469
          if (i > 0) i else -1
2e6d46a3a617 reclaimed Byte_Reader from 51560e392e1b;
wenzelm
parents: 56663
diff changeset
   470
        })
56822
6101243e6740 support URLs as well;
wenzelm
parents: 56821
diff changeset
   471
    val restricted_seq = new Restricted_Seq(seq, 0, stream_length)
56821
2e6d46a3a617 reclaimed Byte_Reader from 51560e392e1b;
wenzelm
parents: 56663
diff changeset
   472
2e6d46a3a617 reclaimed Byte_Reader from 51560e392e1b;
wenzelm
parents: 56663
diff changeset
   473
    class Paged_Reader(override val offset: Int) extends Byte_Reader
2e6d46a3a617 reclaimed Byte_Reader from 51560e392e1b;
wenzelm
parents: 56663
diff changeset
   474
    {
2e6d46a3a617 reclaimed Byte_Reader from 51560e392e1b;
wenzelm
parents: 56663
diff changeset
   475
      override lazy val source: CharSequence = restricted_seq
56827
853f1bcc3755 avoid deprecated Scala syntax;
wenzelm
parents: 56822
diff changeset
   476
      def first: Char = if (seq.isDefinedAt(offset)) seq(offset) else '\u001a'
56821
2e6d46a3a617 reclaimed Byte_Reader from 51560e392e1b;
wenzelm
parents: 56663
diff changeset
   477
      def rest: Paged_Reader = if (seq.isDefinedAt(offset)) new Paged_Reader(offset + 1) else this
2e6d46a3a617 reclaimed Byte_Reader from 51560e392e1b;
wenzelm
parents: 56663
diff changeset
   478
      def pos: InputPosition = new OffsetPosition(source, offset)
2e6d46a3a617 reclaimed Byte_Reader from 51560e392e1b;
wenzelm
parents: 56663
diff changeset
   479
      def atEnd: Boolean = !seq.isDefinedAt(offset)
2e6d46a3a617 reclaimed Byte_Reader from 51560e392e1b;
wenzelm
parents: 56663
diff changeset
   480
      override def drop(n: Int): Paged_Reader = new Paged_Reader(offset + n)
56822
6101243e6740 support URLs as well;
wenzelm
parents: 56821
diff changeset
   481
      def close { buffered_stream.close }
56821
2e6d46a3a617 reclaimed Byte_Reader from 51560e392e1b;
wenzelm
parents: 56663
diff changeset
   482
    }
2e6d46a3a617 reclaimed Byte_Reader from 51560e392e1b;
wenzelm
parents: 56663
diff changeset
   483
    new Paged_Reader(0)
2e6d46a3a617 reclaimed Byte_Reader from 51560e392e1b;
wenzelm
parents: 56663
diff changeset
   484
  }
56822
6101243e6740 support URLs as well;
wenzelm
parents: 56821
diff changeset
   485
6101243e6740 support URLs as well;
wenzelm
parents: 56821
diff changeset
   486
  def byte_reader(file: JFile): Byte_Reader =
6101243e6740 support URLs as well;
wenzelm
parents: 56821
diff changeset
   487
    make_byte_reader(new FileInputStream(file), file.length.toInt)
6101243e6740 support URLs as well;
wenzelm
parents: 56821
diff changeset
   488
6101243e6740 support URLs as well;
wenzelm
parents: 56821
diff changeset
   489
  def byte_reader(url: URL): Byte_Reader =
6101243e6740 support URLs as well;
wenzelm
parents: 56821
diff changeset
   490
  {
6101243e6740 support URLs as well;
wenzelm
parents: 56821
diff changeset
   491
    val connection = url.openConnection
6101243e6740 support URLs as well;
wenzelm
parents: 56821
diff changeset
   492
    val stream = connection.getInputStream
6101243e6740 support URLs as well;
wenzelm
parents: 56821
diff changeset
   493
    val stream_length = connection.getContentLength
6101243e6740 support URLs as well;
wenzelm
parents: 56821
diff changeset
   494
    make_byte_reader(stream, stream_length)
6101243e6740 support URLs as well;
wenzelm
parents: 56821
diff changeset
   495
  }
64824
330ec9bc4b75 tuned signature;
wenzelm
parents: 63579
diff changeset
   496
66918
ec2b50aeb0dd more robust treatment of UTF8 in raw byte sources;
wenzelm
parents: 64824
diff changeset
   497
  def reader_is_utf8(reader: Reader[Char]): Boolean =
ec2b50aeb0dd more robust treatment of UTF8 in raw byte sources;
wenzelm
parents: 64824
diff changeset
   498
    reader.isInstanceOf[Byte_Reader]
ec2b50aeb0dd more robust treatment of UTF8 in raw byte sources;
wenzelm
parents: 64824
diff changeset
   499
ec2b50aeb0dd more robust treatment of UTF8 in raw byte sources;
wenzelm
parents: 64824
diff changeset
   500
  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
   501
    if (is_utf8) UTF8.decode_permissive(s) else s
ec2b50aeb0dd more robust treatment of UTF8 in raw byte sources;
wenzelm
parents: 64824
diff changeset
   502
ec2b50aeb0dd more robust treatment of UTF8 in raw byte sources;
wenzelm
parents: 64824
diff changeset
   503
  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
   504
    reader_decode_utf8(reader_is_utf8(reader), s)
ec2b50aeb0dd more robust treatment of UTF8 in raw byte sources;
wenzelm
parents: 64824
diff changeset
   505
64824
330ec9bc4b75 tuned signature;
wenzelm
parents: 63579
diff changeset
   506
330ec9bc4b75 tuned signature;
wenzelm
parents: 63579
diff changeset
   507
  /* plain text reader */
330ec9bc4b75 tuned signature;
wenzelm
parents: 63579
diff changeset
   508
330ec9bc4b75 tuned signature;
wenzelm
parents: 63579
diff changeset
   509
  def char_reader(text: CharSequence): CharSequenceReader =
330ec9bc4b75 tuned signature;
wenzelm
parents: 63579
diff changeset
   510
    new CharSequenceReader(text)
31648
31b1f296515b Efficient scanning of literals.
wenzelm
parents:
diff changeset
   511
}