src/Pure/General/scan.scala
author paulson <lp15@cam.ac.uk>
Fri, 31 Jan 2014 16:58:58 +0000
changeset 55215 b6c926e67350
parent 55034 04b443d54aee
child 55492 28d4db6c6e79
permissions -rw-r--r--
Restoring some proofs from the equivalent file in Old_Number_Theory.
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
46627
fbe2cb05bdb3 avoid trait Addable, which is deprecated in scala-2.9.x;
wenzelm
parents: 45900
diff changeset
    10
import scala.collection.{IndexedSeq, TraversableOnce}
34187
7b659c1561f1 added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
wenzelm
parents: 34157
diff changeset
    11
import scala.collection.immutable.PagedSeq
7b659c1561f1 added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
wenzelm
parents: 34157
diff changeset
    12
import scala.util.parsing.input.{OffsetPosition, Position => InputPosition, Reader}
31648
31b1f296515b Efficient scanning of literals.
wenzelm
parents:
diff changeset
    13
import scala.util.parsing.combinator.RegexParsers
31b1f296515b Efficient scanning of literals.
wenzelm
parents:
diff changeset
    14
48409
0d2114eb412a more explicit java.io.{File => JFile};
wenzelm
parents: 48342
diff changeset
    15
import java.io.{File => JFile, BufferedInputStream, FileInputStream}
34187
7b659c1561f1 added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
wenzelm
parents: 34157
diff changeset
    16
31648
31b1f296515b Efficient scanning of literals.
wenzelm
parents:
diff changeset
    17
31b1f296515b Efficient scanning of literals.
wenzelm
parents:
diff changeset
    18
object Scan
31b1f296515b Efficient scanning of literals.
wenzelm
parents:
diff changeset
    19
{
43411
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
    20
  /** context of partial scans **/
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
    21
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
    22
  sealed abstract class Context
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
    23
  case object Finished extends Context
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
    24
  case class Quoted(quote: String) extends Context
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
    25
  case object Verbatim extends Context
55033
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
    26
  case class Cartouche(depth: Int) extends Context
43411
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
    27
  case class Comment(depth: Int) extends Context
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
    28
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
    29
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
    30
31649
a11ea667d676 reorganized and abstracted version, via Set trait;
wenzelm
parents: 31648
diff changeset
    31
  /** Lexicon -- position tree **/
31648
31b1f296515b Efficient scanning of literals.
wenzelm
parents:
diff changeset
    32
31649
a11ea667d676 reorganized and abstracted version, via Set trait;
wenzelm
parents: 31648
diff changeset
    33
  object Lexicon
31648
31b1f296515b Efficient scanning of literals.
wenzelm
parents:
diff changeset
    34
  {
45252
6de58d947e57 class Lexicon as abstract datatype;
wenzelm
parents: 43696
diff changeset
    35
    /* representation */
6de58d947e57 class Lexicon as abstract datatype;
wenzelm
parents: 43696
diff changeset
    36
6de58d947e57 class Lexicon as abstract datatype;
wenzelm
parents: 43696
diff changeset
    37
    sealed case class Tree(val branches: Map[Char, (String, Tree)])
31649
a11ea667d676 reorganized and abstracted version, via Set trait;
wenzelm
parents: 31648
diff changeset
    38
    private val empty_tree = Tree(Map())
a11ea667d676 reorganized and abstracted version, via Set trait;
wenzelm
parents: 31648
diff changeset
    39
45252
6de58d947e57 class Lexicon as abstract datatype;
wenzelm
parents: 43696
diff changeset
    40
    val empty: Lexicon = new Lexicon(empty_tree)
31762
20745ab5b79a more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
wenzelm
parents: 31753
diff changeset
    41
    def apply(elems: String*): Lexicon = empty ++ elems
31648
31b1f296515b Efficient scanning of literals.
wenzelm
parents:
diff changeset
    42
  }
31b1f296515b Efficient scanning of literals.
wenzelm
parents:
diff changeset
    43
46712
8650d9a95736 prefer final ADTs -- prevent ooddities;
wenzelm
parents: 46627
diff changeset
    44
  final class Lexicon private(main_tree: Lexicon.Tree) extends RegexParsers
31648
31b1f296515b Efficient scanning of literals.
wenzelm
parents:
diff changeset
    45
  {
31649
a11ea667d676 reorganized and abstracted version, via Set trait;
wenzelm
parents: 31648
diff changeset
    46
    import Lexicon.Tree
31648
31b1f296515b Efficient scanning of literals.
wenzelm
parents:
diff changeset
    47
31b1f296515b Efficient scanning of literals.
wenzelm
parents:
diff changeset
    48
31650
cfaed41ee2c5 added completions;
wenzelm
parents: 31649
diff changeset
    49
    /* auxiliary operations */
cfaed41ee2c5 added completions;
wenzelm
parents: 31649
diff changeset
    50
cfaed41ee2c5 added completions;
wenzelm
parents: 31649
diff changeset
    51
    private def content(tree: Tree, result: List[String]): List[String] =
cfaed41ee2c5 added completions;
wenzelm
parents: 31649
diff changeset
    52
      (result /: tree.branches.toList) ((res, entry) =>
cfaed41ee2c5 added completions;
wenzelm
parents: 31649
diff changeset
    53
        entry match { case (_, (s, tr)) =>
cfaed41ee2c5 added completions;
wenzelm
parents: 31649
diff changeset
    54
          if (s.isEmpty) content(tr, res) else content(tr, s :: res) })
cfaed41ee2c5 added completions;
wenzelm
parents: 31649
diff changeset
    55
cfaed41ee2c5 added completions;
wenzelm
parents: 31649
diff changeset
    56
    private def lookup(str: CharSequence): Option[(Boolean, Tree)] =
cfaed41ee2c5 added completions;
wenzelm
parents: 31649
diff changeset
    57
    {
cfaed41ee2c5 added completions;
wenzelm
parents: 31649
diff changeset
    58
      val len = str.length
cfaed41ee2c5 added completions;
wenzelm
parents: 31649
diff changeset
    59
      def look(tree: Tree, tip: Boolean, i: Int): Option[(Boolean, Tree)] =
cfaed41ee2c5 added completions;
wenzelm
parents: 31649
diff changeset
    60
      {
cfaed41ee2c5 added completions;
wenzelm
parents: 31649
diff changeset
    61
        if (i < len) {
cfaed41ee2c5 added completions;
wenzelm
parents: 31649
diff changeset
    62
          tree.branches.get(str.charAt(i)) match {
cfaed41ee2c5 added completions;
wenzelm
parents: 31649
diff changeset
    63
            case Some((s, tr)) => look(tr, !s.isEmpty, i + 1)
cfaed41ee2c5 added completions;
wenzelm
parents: 31649
diff changeset
    64
            case None => None
cfaed41ee2c5 added completions;
wenzelm
parents: 31649
diff changeset
    65
          }
cfaed41ee2c5 added completions;
wenzelm
parents: 31649
diff changeset
    66
        } else Some(tip, tree)
cfaed41ee2c5 added completions;
wenzelm
parents: 31649
diff changeset
    67
      }
cfaed41ee2c5 added completions;
wenzelm
parents: 31649
diff changeset
    68
      look(main_tree, false, 0)
cfaed41ee2c5 added completions;
wenzelm
parents: 31649
diff changeset
    69
    }
cfaed41ee2c5 added completions;
wenzelm
parents: 31649
diff changeset
    70
cfaed41ee2c5 added completions;
wenzelm
parents: 31649
diff changeset
    71
    def completions(str: CharSequence): List[String] =
31764
e767fee21b22 moved string utilities to completion.scala;
wenzelm
parents: 31762
diff changeset
    72
      lookup(str) match {
31650
cfaed41ee2c5 added completions;
wenzelm
parents: 31649
diff changeset
    73
        case Some((true, tree)) => content(tree, List(str.toString))
cfaed41ee2c5 added completions;
wenzelm
parents: 31649
diff changeset
    74
        case Some((false, tree)) => content(tree, Nil)
cfaed41ee2c5 added completions;
wenzelm
parents: 31649
diff changeset
    75
        case None => Nil
31764
e767fee21b22 moved string utilities to completion.scala;
wenzelm
parents: 31762
diff changeset
    76
      }
31650
cfaed41ee2c5 added completions;
wenzelm
parents: 31649
diff changeset
    77
cfaed41ee2c5 added completions;
wenzelm
parents: 31649
diff changeset
    78
36011
3ff725ac13a4 adapted to Scala 2.8.0 Beta1 -- with notable changes to scala.collection;
wenzelm
parents: 34316
diff changeset
    79
    /* pseudo Set methods */
31649
a11ea667d676 reorganized and abstracted version, via Set trait;
wenzelm
parents: 31648
diff changeset
    80
45900
793bf5fa5fbf prefer sorting from Scala library;
wenzelm
parents: 45252
diff changeset
    81
    def iterator: Iterator[String] = content(main_tree, Nil).sorted.iterator
31649
a11ea667d676 reorganized and abstracted version, via Set trait;
wenzelm
parents: 31648
diff changeset
    82
36011
3ff725ac13a4 adapted to Scala 2.8.0 Beta1 -- with notable changes to scala.collection;
wenzelm
parents: 34316
diff changeset
    83
    override def toString: String = iterator.mkString("Lexicon(", ", ", ")")
31649
a11ea667d676 reorganized and abstracted version, via Set trait;
wenzelm
parents: 31648
diff changeset
    84
36011
3ff725ac13a4 adapted to Scala 2.8.0 Beta1 -- with notable changes to scala.collection;
wenzelm
parents: 34316
diff changeset
    85
    def empty: Lexicon = Lexicon.empty
3ff725ac13a4 adapted to Scala 2.8.0 Beta1 -- with notable changes to scala.collection;
wenzelm
parents: 34316
diff changeset
    86
    def isEmpty: Boolean = main_tree.branches.isEmpty
31648
31b1f296515b Efficient scanning of literals.
wenzelm
parents:
diff changeset
    87
31762
20745ab5b79a more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
wenzelm
parents: 31753
diff changeset
    88
    def contains(elem: String): Boolean =
20745ab5b79a more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
wenzelm
parents: 31753
diff changeset
    89
      lookup(elem) match {
31650
cfaed41ee2c5 added completions;
wenzelm
parents: 31649
diff changeset
    90
        case Some((tip, _)) => tip
cfaed41ee2c5 added completions;
wenzelm
parents: 31649
diff changeset
    91
        case _ => false
cfaed41ee2c5 added completions;
wenzelm
parents: 31649
diff changeset
    92
      }
31648
31b1f296515b Efficient scanning of literals.
wenzelm
parents:
diff changeset
    93
36011
3ff725ac13a4 adapted to Scala 2.8.0 Beta1 -- with notable changes to scala.collection;
wenzelm
parents: 34316
diff changeset
    94
46627
fbe2cb05bdb3 avoid trait Addable, which is deprecated in scala-2.9.x;
wenzelm
parents: 45900
diff changeset
    95
    /* add elements */
36011
3ff725ac13a4 adapted to Scala 2.8.0 Beta1 -- with notable changes to scala.collection;
wenzelm
parents: 34316
diff changeset
    96
31762
20745ab5b79a more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
wenzelm
parents: 31753
diff changeset
    97
    def + (elem: String): Lexicon =
20745ab5b79a more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
wenzelm
parents: 31753
diff changeset
    98
      if (contains(elem)) this
20745ab5b79a more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
wenzelm
parents: 31753
diff changeset
    99
      else {
20745ab5b79a more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
wenzelm
parents: 31753
diff changeset
   100
        val len = elem.length
20745ab5b79a more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
wenzelm
parents: 31753
diff changeset
   101
        def extend(tree: Tree, i: Int): Tree =
20745ab5b79a more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
wenzelm
parents: 31753
diff changeset
   102
          if (i < len) {
20745ab5b79a more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
wenzelm
parents: 31753
diff changeset
   103
            val c = elem.charAt(i)
20745ab5b79a more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
wenzelm
parents: 31753
diff changeset
   104
            val end = (i + 1 == len)
20745ab5b79a more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
wenzelm
parents: 31753
diff changeset
   105
            tree.branches.get(c) match {
20745ab5b79a more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
wenzelm
parents: 31753
diff changeset
   106
              case Some((s, tr)) =>
20745ab5b79a more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
wenzelm
parents: 31753
diff changeset
   107
                Tree(tree.branches +
20745ab5b79a more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
wenzelm
parents: 31753
diff changeset
   108
                  (c -> (if (end) elem else s, extend(tr, i + 1))))
20745ab5b79a more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
wenzelm
parents: 31753
diff changeset
   109
              case None =>
20745ab5b79a more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
wenzelm
parents: 31753
diff changeset
   110
                Tree(tree.branches +
20745ab5b79a more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
wenzelm
parents: 31753
diff changeset
   111
                  (c -> (if (end) elem else "", extend(Lexicon.empty_tree, i + 1))))
20745ab5b79a more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
wenzelm
parents: 31753
diff changeset
   112
            }
34135
63dd95e3b393 indicate final state of keywords;
wenzelm
parents: 32450
diff changeset
   113
          }
63dd95e3b393 indicate final state of keywords;
wenzelm
parents: 32450
diff changeset
   114
          else tree
46627
fbe2cb05bdb3 avoid trait Addable, which is deprecated in scala-2.9.x;
wenzelm
parents: 45900
diff changeset
   115
        new Lexicon(extend(main_tree, 0))
31648
31b1f296515b Efficient scanning of literals.
wenzelm
parents:
diff changeset
   116
      }
31762
20745ab5b79a more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
wenzelm
parents: 31753
diff changeset
   117
46627
fbe2cb05bdb3 avoid trait Addable, which is deprecated in scala-2.9.x;
wenzelm
parents: 45900
diff changeset
   118
    def ++ (elems: TraversableOnce[String]): Lexicon = (this /: elems)(_ + _)
fbe2cb05bdb3 avoid trait Addable, which is deprecated in scala-2.9.x;
wenzelm
parents: 45900
diff changeset
   119
31649
a11ea667d676 reorganized and abstracted version, via Set trait;
wenzelm
parents: 31648
diff changeset
   120
34301
wenzelm
parents: 34267
diff changeset
   121
34137
6cc9a0cbaf55 refined some Symbol operations/signatures;
wenzelm
parents: 34135
diff changeset
   122
    /** RegexParsers methods **/
31649
a11ea667d676 reorganized and abstracted version, via Set trait;
wenzelm
parents: 31648
diff changeset
   123
a11ea667d676 reorganized and abstracted version, via Set trait;
wenzelm
parents: 31648
diff changeset
   124
    override val whiteSpace = "".r
a11ea667d676 reorganized and abstracted version, via Set trait;
wenzelm
parents: 31648
diff changeset
   125
34137
6cc9a0cbaf55 refined some Symbol operations/signatures;
wenzelm
parents: 34135
diff changeset
   126
43411
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   127
    /* optional termination */
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   128
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   129
    def opt_term[T](p: => Parser[T]): Parser[Option[T]] =
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   130
      p ^^ (x => Some(x)) | """\z""".r ^^ (_ => None)
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   131
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   132
34137
6cc9a0cbaf55 refined some Symbol operations/signatures;
wenzelm
parents: 34135
diff changeset
   133
    /* keywords from lexicon */
6cc9a0cbaf55 refined some Symbol operations/signatures;
wenzelm
parents: 34135
diff changeset
   134
34140
31be1235d0fb simplified result of keyword and symbols parser;
wenzelm
parents: 34137
diff changeset
   135
    def keyword: Parser[String] = new Parser[String]
34135
63dd95e3b393 indicate final state of keywords;
wenzelm
parents: 32450
diff changeset
   136
    {
31649
a11ea667d676 reorganized and abstracted version, via Set trait;
wenzelm
parents: 31648
diff changeset
   137
      def apply(in: Input) =
a11ea667d676 reorganized and abstracted version, via Set trait;
wenzelm
parents: 31648
diff changeset
   138
      {
a11ea667d676 reorganized and abstracted version, via Set trait;
wenzelm
parents: 31648
diff changeset
   139
        val source = in.source
a11ea667d676 reorganized and abstracted version, via Set trait;
wenzelm
parents: 31648
diff changeset
   140
        val offset = in.offset
a11ea667d676 reorganized and abstracted version, via Set trait;
wenzelm
parents: 31648
diff changeset
   141
        val len = source.length - offset
a11ea667d676 reorganized and abstracted version, via Set trait;
wenzelm
parents: 31648
diff changeset
   142
34140
31be1235d0fb simplified result of keyword and symbols parser;
wenzelm
parents: 34137
diff changeset
   143
        def scan(tree: Tree, result: String, i: Int): String =
31649
a11ea667d676 reorganized and abstracted version, via Set trait;
wenzelm
parents: 31648
diff changeset
   144
        {
a11ea667d676 reorganized and abstracted version, via Set trait;
wenzelm
parents: 31648
diff changeset
   145
          if (i < len) {
a11ea667d676 reorganized and abstracted version, via Set trait;
wenzelm
parents: 31648
diff changeset
   146
            tree.branches.get(source.charAt(offset + i)) match {
34140
31be1235d0fb simplified result of keyword and symbols parser;
wenzelm
parents: 34137
diff changeset
   147
              case Some((s, tr)) => scan(tr, if (s.isEmpty) result else s, i + 1)
34135
63dd95e3b393 indicate final state of keywords;
wenzelm
parents: 32450
diff changeset
   148
              case None => result
31649
a11ea667d676 reorganized and abstracted version, via Set trait;
wenzelm
parents: 31648
diff changeset
   149
            }
34135
63dd95e3b393 indicate final state of keywords;
wenzelm
parents: 32450
diff changeset
   150
          }
63dd95e3b393 indicate final state of keywords;
wenzelm
parents: 32450
diff changeset
   151
          else result
31649
a11ea667d676 reorganized and abstracted version, via Set trait;
wenzelm
parents: 31648
diff changeset
   152
        }
34140
31be1235d0fb simplified result of keyword and symbols parser;
wenzelm
parents: 34137
diff changeset
   153
        val result = scan(main_tree, "", 0)
31be1235d0fb simplified result of keyword and symbols parser;
wenzelm
parents: 34137
diff changeset
   154
        if (result.isEmpty) Failure("keyword expected", in)
31be1235d0fb simplified result of keyword and symbols parser;
wenzelm
parents: 34137
diff changeset
   155
        else Success(result, in.drop(result.length))
31648
31b1f296515b Efficient scanning of literals.
wenzelm
parents:
diff changeset
   156
      }
31649
a11ea667d676 reorganized and abstracted version, via Set trait;
wenzelm
parents: 31648
diff changeset
   157
    }.named("keyword")
31648
31b1f296515b Efficient scanning of literals.
wenzelm
parents:
diff changeset
   158
34137
6cc9a0cbaf55 refined some Symbol operations/signatures;
wenzelm
parents: 34135
diff changeset
   159
34157
0a0a19153626 explicit representation of Token_Kind -- cannot really depend on runtime types due to erasure;
wenzelm
parents: 34144
diff changeset
   160
    /* symbol range */
34137
6cc9a0cbaf55 refined some Symbol operations/signatures;
wenzelm
parents: 34135
diff changeset
   161
43696
58bb7ca5c7a2 explicit indication of type Symbol.Symbol;
wenzelm
parents: 43695
diff changeset
   162
    def symbol_range(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
   163
      new Parser[String]
34135
63dd95e3b393 indicate final state of keywords;
wenzelm
parents: 32450
diff changeset
   164
      {
34137
6cc9a0cbaf55 refined some Symbol operations/signatures;
wenzelm
parents: 34135
diff changeset
   165
        def apply(in: Input) =
6cc9a0cbaf55 refined some Symbol operations/signatures;
wenzelm
parents: 34135
diff changeset
   166
        {
6cc9a0cbaf55 refined some Symbol operations/signatures;
wenzelm
parents: 34135
diff changeset
   167
          val start = in.offset
6cc9a0cbaf55 refined some Symbol operations/signatures;
wenzelm
parents: 34135
diff changeset
   168
          val end = in.source.length
6cc9a0cbaf55 refined some Symbol operations/signatures;
wenzelm
parents: 34135
diff changeset
   169
          val matcher = new Symbol.Matcher(in.source)
6cc9a0cbaf55 refined some Symbol operations/signatures;
wenzelm
parents: 34135
diff changeset
   170
6cc9a0cbaf55 refined some Symbol operations/signatures;
wenzelm
parents: 34135
diff changeset
   171
          var i = start
6cc9a0cbaf55 refined some Symbol operations/signatures;
wenzelm
parents: 34135
diff changeset
   172
          var count = 0
6cc9a0cbaf55 refined some Symbol operations/signatures;
wenzelm
parents: 34135
diff changeset
   173
          var finished = false
55034
wenzelm
parents: 55033
diff changeset
   174
          while (!finished && i < end && count < max_count) {
wenzelm
parents: 55033
diff changeset
   175
            val n = matcher(i, end)
wenzelm
parents: 55033
diff changeset
   176
            val sym = in.source.subSequence(i, i + n).toString
wenzelm
parents: 55033
diff changeset
   177
            if (pred(sym)) { i += n; count += 1 }
34137
6cc9a0cbaf55 refined some Symbol operations/signatures;
wenzelm
parents: 34135
diff changeset
   178
            else finished = true
6cc9a0cbaf55 refined some Symbol operations/signatures;
wenzelm
parents: 34135
diff changeset
   179
          }
34140
31be1235d0fb simplified result of keyword and symbols parser;
wenzelm
parents: 34137
diff changeset
   180
          if (count < min_count) Failure("bad input", in)
31be1235d0fb simplified result of keyword and symbols parser;
wenzelm
parents: 34137
diff changeset
   181
          else Success(in.source.subSequence(start, i).toString, in.drop(i - start))
34137
6cc9a0cbaf55 refined some Symbol operations/signatures;
wenzelm
parents: 34135
diff changeset
   182
        }
34157
0a0a19153626 explicit representation of Token_Kind -- cannot really depend on runtime types due to erasure;
wenzelm
parents: 34144
diff changeset
   183
      }.named("symbol_range")
34137
6cc9a0cbaf55 refined some Symbol operations/signatures;
wenzelm
parents: 34135
diff changeset
   184
43696
58bb7ca5c7a2 explicit indication of type Symbol.Symbol;
wenzelm
parents: 43695
diff changeset
   185
    def one(pred: Symbol.Symbol => Boolean): Parser[String] =
58bb7ca5c7a2 explicit indication of type Symbol.Symbol;
wenzelm
parents: 43695
diff changeset
   186
      symbol_range(pred, 1, 1)
58bb7ca5c7a2 explicit indication of type Symbol.Symbol;
wenzelm
parents: 43695
diff changeset
   187
58bb7ca5c7a2 explicit indication of type Symbol.Symbol;
wenzelm
parents: 43695
diff changeset
   188
    def many(pred: Symbol.Symbol => Boolean): Parser[String] =
58bb7ca5c7a2 explicit indication of type Symbol.Symbol;
wenzelm
parents: 43695
diff changeset
   189
      symbol_range(pred, 0, Integer.MAX_VALUE)
58bb7ca5c7a2 explicit indication of type Symbol.Symbol;
wenzelm
parents: 43695
diff changeset
   190
58bb7ca5c7a2 explicit indication of type Symbol.Symbol;
wenzelm
parents: 43695
diff changeset
   191
    def many1(pred: Symbol.Symbol => Boolean): Parser[String] =
58bb7ca5c7a2 explicit indication of type Symbol.Symbol;
wenzelm
parents: 43695
diff changeset
   192
      symbol_range(pred, 1, Integer.MAX_VALUE)
34140
31be1235d0fb simplified result of keyword and symbols parser;
wenzelm
parents: 34137
diff changeset
   193
31be1235d0fb simplified result of keyword and symbols parser;
wenzelm
parents: 34137
diff changeset
   194
34143
ded454429df3 added nested comments;
wenzelm
parents: 34140
diff changeset
   195
    /* quoted strings */
34140
31be1235d0fb simplified result of keyword and symbols parser;
wenzelm
parents: 34137
diff changeset
   196
43696
58bb7ca5c7a2 explicit indication of type Symbol.Symbol;
wenzelm
parents: 43695
diff changeset
   197
    private def quoted_body(quote: Symbol.Symbol): Parser[String] =
43411
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   198
    {
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   199
      rep(many1(sym => sym != quote && sym != "\\") | "\\" + quote | "\\\\" |
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   200
        (("""\\\d\d\d""".r) ^? { case x if x.substring(1, 4).toInt <= 255 => x })) ^^ (_.mkString)
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   201
    }
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   202
43696
58bb7ca5c7a2 explicit indication of type Symbol.Symbol;
wenzelm
parents: 43695
diff changeset
   203
    private def quoted(quote: Symbol.Symbol): Parser[String] =
34143
ded454429df3 added nested comments;
wenzelm
parents: 34140
diff changeset
   204
    {
43411
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   205
      quote ~ quoted_body(quote) ~ quote ^^ { case x ~ y ~ z => x + y + z }
34143
ded454429df3 added nested comments;
wenzelm
parents: 34140
diff changeset
   206
    }.named("quoted")
34140
31be1235d0fb simplified result of keyword and symbols parser;
wenzelm
parents: 34137
diff changeset
   207
43696
58bb7ca5c7a2 explicit indication of type Symbol.Symbol;
wenzelm
parents: 43695
diff changeset
   208
    def quoted_content(quote: Symbol.Symbol, source: String): String =
34140
31be1235d0fb simplified result of keyword and symbols parser;
wenzelm
parents: 34137
diff changeset
   209
    {
31be1235d0fb simplified result of keyword and symbols parser;
wenzelm
parents: 34137
diff changeset
   210
      require(parseAll(quoted(quote), source).successful)
34189
10c5871ec684 quoted_content: handle escapes;
wenzelm
parents: 34187
diff changeset
   211
      val body = source.substring(1, source.length - 1)
10c5871ec684 quoted_content: handle escapes;
wenzelm
parents: 34187
diff changeset
   212
      if (body.exists(_ == '\\')) {
10c5871ec684 quoted_content: handle escapes;
wenzelm
parents: 34187
diff changeset
   213
        val content =
40523
1050315f6ee2 simplified/robustified treatment of malformed symbols, which are now fully internalized (total Symbol.explode etc.);
wenzelm
parents: 40290
diff changeset
   214
          rep(many1(sym => sym != quote && sym != "\\") |
34189
10c5871ec684 quoted_content: handle escapes;
wenzelm
parents: 34187
diff changeset
   215
              "\\" ~> (quote | "\\" | """\d\d\d""".r ^^ { case x => x.toInt.toChar.toString }))
10c5871ec684 quoted_content: handle escapes;
wenzelm
parents: 34187
diff changeset
   216
        parseAll(content ^^ (_.mkString), body).get
10c5871ec684 quoted_content: handle escapes;
wenzelm
parents: 34187
diff changeset
   217
      }
10c5871ec684 quoted_content: handle escapes;
wenzelm
parents: 34187
diff changeset
   218
      else body
34140
31be1235d0fb simplified result of keyword and symbols parser;
wenzelm
parents: 34137
diff changeset
   219
    }
31be1235d0fb simplified result of keyword and symbols parser;
wenzelm
parents: 34137
diff changeset
   220
43696
58bb7ca5c7a2 explicit indication of type Symbol.Symbol;
wenzelm
parents: 43695
diff changeset
   221
    def quoted_context(quote: Symbol.Symbol, ctxt: Context): Parser[(String, Context)] =
43411
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   222
    {
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   223
      ctxt match {
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   224
        case Finished =>
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   225
          quote ~ quoted_body(quote) ~ opt_term(quote) ^^
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   226
            { case x ~ y ~ Some(z) => (x + y + z, Finished)
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   227
              case x ~ y ~ None => (x + y, Quoted(quote)) }
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   228
        case Quoted(q) if q == quote =>
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   229
          quoted_body(quote) ~ opt_term(quote) ^^
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   230
            { case x ~ Some(y) => (x + y, Finished)
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   231
              case x ~ None => (x, ctxt) }
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   232
        case _ => failure("")
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   233
      }
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   234
    }.named("quoted_context")
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   235
48763
de68fc11c245 more precise recover_quoted, recover_verbatim, recover_comment (cf. ML version) -- NB: context parsers expect explicit termination;
wenzelm
parents: 48754
diff changeset
   236
    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
   237
      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
   238
34140
31be1235d0fb simplified result of keyword and symbols parser;
wenzelm
parents: 34137
diff changeset
   239
34143
ded454429df3 added nested comments;
wenzelm
parents: 34140
diff changeset
   240
    /* verbatim text */
ded454429df3 added nested comments;
wenzelm
parents: 34140
diff changeset
   241
43411
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   242
    private def verbatim_body: Parser[String] =
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   243
      rep(many1(sym => sym != "*") | """\*(?!\})""".r) ^^ (_.mkString)
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   244
34143
ded454429df3 added nested comments;
wenzelm
parents: 34140
diff changeset
   245
    private def verbatim: Parser[String] =
ded454429df3 added nested comments;
wenzelm
parents: 34140
diff changeset
   246
    {
43411
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   247
      "{*" ~ verbatim_body ~ "*}" ^^ { case x ~ y ~ z => x + y + z }
34143
ded454429df3 added nested comments;
wenzelm
parents: 34140
diff changeset
   248
    }.named("verbatim")
34140
31be1235d0fb simplified result of keyword and symbols parser;
wenzelm
parents: 34137
diff changeset
   249
31be1235d0fb simplified result of keyword and symbols parser;
wenzelm
parents: 34137
diff changeset
   250
    def verbatim_content(source: String): String =
31be1235d0fb simplified result of keyword and symbols parser;
wenzelm
parents: 34137
diff changeset
   251
    {
31be1235d0fb simplified result of keyword and symbols parser;
wenzelm
parents: 34137
diff changeset
   252
      require(parseAll(verbatim, source).successful)
31be1235d0fb simplified result of keyword and symbols parser;
wenzelm
parents: 34137
diff changeset
   253
      source.substring(2, source.length - 2)
31be1235d0fb simplified result of keyword and symbols parser;
wenzelm
parents: 34137
diff changeset
   254
    }
31be1235d0fb simplified result of keyword and symbols parser;
wenzelm
parents: 34137
diff changeset
   255
43411
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   256
    def verbatim_context(ctxt: Context): Parser[(String, Context)] =
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   257
    {
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   258
      ctxt match {
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   259
        case Finished =>
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   260
          "{*" ~ verbatim_body ~ opt_term("*}") ^^
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   261
            { case x ~ y ~ Some(z) => (x + y + z, Finished)
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   262
              case x ~ y ~ None => (x + y, Verbatim) }
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   263
        case Verbatim =>
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   264
          verbatim_body ~ opt_term("*}") ^^
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   265
            { case x ~ Some(y) => (x + y, Finished)
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   266
              case x ~ None => (x, Verbatim) }
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   267
        case _ => failure("")
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   268
      }
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   269
    }.named("verbatim_context")
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   270
48763
de68fc11c245 more precise recover_quoted, recover_verbatim, recover_comment (cf. ML version) -- NB: context parsers expect explicit termination;
wenzelm
parents: 48754
diff changeset
   271
    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
   272
      "{*" ~ 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
   273
34140
31be1235d0fb simplified result of keyword and symbols parser;
wenzelm
parents: 34137
diff changeset
   274
55033
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   275
    /* nested text cartouches */
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   276
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   277
    private def cartouche_depth(depth: Int): Parser[(String, Int)] = new Parser[(String, Int)]
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   278
    {
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   279
      require(depth >= 0)
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   280
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   281
      def apply(in: Input) =
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   282
      {
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   283
        val start = in.offset
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   284
        val end = in.source.length
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   285
        val matcher = new Symbol.Matcher(in.source)
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   286
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   287
        var i = start
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   288
        var d = depth
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   289
        var finished = false
55034
wenzelm
parents: 55033
diff changeset
   290
        while (!finished && i < end) {
wenzelm
parents: 55033
diff changeset
   291
          val n = matcher(i, end)
wenzelm
parents: 55033
diff changeset
   292
          val sym = in.source.subSequence(i, i + n).toString
wenzelm
parents: 55033
diff changeset
   293
          if (Symbol.is_open(sym)) { i += n; d += 1 }
wenzelm
parents: 55033
diff changeset
   294
          else if (d > 0) { i += n; if (Symbol.is_close(sym)) d -= 1 }
55033
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   295
          else finished = true
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   296
        }
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   297
        if (i == start) Failure("bad input", in)
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   298
        else Success((in.source.subSequence(start, i).toString, d), in.drop(i - start))
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   299
      }
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   300
    }.named("cartouche_depth")
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   301
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   302
    def cartouche: Parser[String] =
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   303
      cartouche_depth(0) ^? { case (x, d) if d == 0 => x }
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   304
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   305
    def cartouche_context(ctxt: Context): Parser[(String, Context)] =
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   306
    {
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   307
      val depth =
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   308
        ctxt match {
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   309
          case Finished => 0
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   310
          case Cartouche(d) => d
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   311
          case _ => -1
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   312
        }
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   313
      if (depth >= 0)
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   314
        cartouche_depth(depth) ^^
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   315
          { case (x, 0) => (x, Finished)
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   316
            case (x, d) => (x, Cartouche(d)) }
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   317
      else failure("")
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   318
    }
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   319
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   320
    val recover_cartouche: Parser[String] =
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   321
      cartouche_depth(0) ^^ (_._1)
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   322
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   323
    def cartouche_content(source: String): String =
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   324
    {
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   325
      def err(): Nothing = error("Malformed text cartouche: " + quote(source))
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   326
      val source1 =
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   327
        Library.try_unprefix(Symbol.open_decoded, source) orElse
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   328
          Library.try_unprefix(Symbol.open, source) getOrElse err()
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   329
      Library.try_unsuffix(Symbol.close_decoded, source1) orElse
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   330
        Library.try_unsuffix(Symbol.close, source1) getOrElse err()
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   331
    }
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   332
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   333
34143
ded454429df3 added nested comments;
wenzelm
parents: 34140
diff changeset
   334
    /* nested comments */
ded454429df3 added nested comments;
wenzelm
parents: 34140
diff changeset
   335
43412
81517eed8a78 partial scans of nested comments;
wenzelm
parents: 43411
diff changeset
   336
    private def comment_depth(depth: Int): Parser[(String, Int)] = new Parser[(String, Int)]
34143
ded454429df3 added nested comments;
wenzelm
parents: 34140
diff changeset
   337
    {
43412
81517eed8a78 partial scans of nested comments;
wenzelm
parents: 43411
diff changeset
   338
      require(depth >= 0)
81517eed8a78 partial scans of nested comments;
wenzelm
parents: 43411
diff changeset
   339
34143
ded454429df3 added nested comments;
wenzelm
parents: 34140
diff changeset
   340
      val comment_text =
42441
781c622af16a more robust scanning of iterated comments, such as "(* (**) (**) *)";
wenzelm
parents: 40523
diff changeset
   341
        rep1(many1(sym => sym != "*" && sym != "(") | """\*(?!\))|\((?!\*)""".r)
34143
ded454429df3 added nested comments;
wenzelm
parents: 34140
diff changeset
   342
ded454429df3 added nested comments;
wenzelm
parents: 34140
diff changeset
   343
      def apply(in: Input) =
ded454429df3 added nested comments;
wenzelm
parents: 34140
diff changeset
   344
      {
ded454429df3 added nested comments;
wenzelm
parents: 34140
diff changeset
   345
        var rest = in
42441
781c622af16a more robust scanning of iterated comments, such as "(* (**) (**) *)";
wenzelm
parents: 40523
diff changeset
   346
        def try_parse[A](p: Parser[A]): Boolean =
34143
ded454429df3 added nested comments;
wenzelm
parents: 34140
diff changeset
   347
        {
42441
781c622af16a more robust scanning of iterated comments, such as "(* (**) (**) *)";
wenzelm
parents: 40523
diff changeset
   348
          parse(p ^^^ (), rest) match {
34143
ded454429df3 added nested comments;
wenzelm
parents: 34140
diff changeset
   349
            case Success(_, next) => { rest = next; true }
ded454429df3 added nested comments;
wenzelm
parents: 34140
diff changeset
   350
            case _ => false
ded454429df3 added nested comments;
wenzelm
parents: 34140
diff changeset
   351
          }
ded454429df3 added nested comments;
wenzelm
parents: 34140
diff changeset
   352
        }
43412
81517eed8a78 partial scans of nested comments;
wenzelm
parents: 43411
diff changeset
   353
        var d = depth
34143
ded454429df3 added nested comments;
wenzelm
parents: 34140
diff changeset
   354
        var finished = false
ded454429df3 added nested comments;
wenzelm
parents: 34140
diff changeset
   355
        while (!finished) {
43412
81517eed8a78 partial scans of nested comments;
wenzelm
parents: 43411
diff changeset
   356
          if (try_parse("(*")) d += 1
81517eed8a78 partial scans of nested comments;
wenzelm
parents: 43411
diff changeset
   357
          else if (d > 0 && try_parse("*)")) d -= 1
81517eed8a78 partial scans of nested comments;
wenzelm
parents: 43411
diff changeset
   358
          else if (d == 0 || !try_parse(comment_text)) finished = true
34143
ded454429df3 added nested comments;
wenzelm
parents: 34140
diff changeset
   359
        }
43412
81517eed8a78 partial scans of nested comments;
wenzelm
parents: 43411
diff changeset
   360
        if (in.offset < rest.offset)
81517eed8a78 partial scans of nested comments;
wenzelm
parents: 43411
diff changeset
   361
          Success((in.source.subSequence(in.offset, rest.offset).toString, d), rest)
34143
ded454429df3 added nested comments;
wenzelm
parents: 34140
diff changeset
   362
        else Failure("comment expected", in)
ded454429df3 added nested comments;
wenzelm
parents: 34140
diff changeset
   363
      }
43412
81517eed8a78 partial scans of nested comments;
wenzelm
parents: 43411
diff changeset
   364
    }.named("comment_depth")
81517eed8a78 partial scans of nested comments;
wenzelm
parents: 43411
diff changeset
   365
81517eed8a78 partial scans of nested comments;
wenzelm
parents: 43411
diff changeset
   366
    def comment: Parser[String] =
81517eed8a78 partial scans of nested comments;
wenzelm
parents: 43411
diff changeset
   367
      comment_depth(0) ^? { case (x, d) if d == 0 => x }
81517eed8a78 partial scans of nested comments;
wenzelm
parents: 43411
diff changeset
   368
81517eed8a78 partial scans of nested comments;
wenzelm
parents: 43411
diff changeset
   369
    def comment_context(ctxt: Context): Parser[(String, Context)] =
81517eed8a78 partial scans of nested comments;
wenzelm
parents: 43411
diff changeset
   370
    {
81517eed8a78 partial scans of nested comments;
wenzelm
parents: 43411
diff changeset
   371
      val depth =
81517eed8a78 partial scans of nested comments;
wenzelm
parents: 43411
diff changeset
   372
        ctxt match {
81517eed8a78 partial scans of nested comments;
wenzelm
parents: 43411
diff changeset
   373
          case Finished => 0
81517eed8a78 partial scans of nested comments;
wenzelm
parents: 43411
diff changeset
   374
          case Comment(d) => d
81517eed8a78 partial scans of nested comments;
wenzelm
parents: 43411
diff changeset
   375
          case _ => -1
81517eed8a78 partial scans of nested comments;
wenzelm
parents: 43411
diff changeset
   376
        }
81517eed8a78 partial scans of nested comments;
wenzelm
parents: 43411
diff changeset
   377
      if (depth >= 0)
81517eed8a78 partial scans of nested comments;
wenzelm
parents: 43411
diff changeset
   378
        comment_depth(depth) ^^
81517eed8a78 partial scans of nested comments;
wenzelm
parents: 43411
diff changeset
   379
          { case (x, 0) => (x, Finished)
81517eed8a78 partial scans of nested comments;
wenzelm
parents: 43411
diff changeset
   380
            case (x, d) => (x, Comment(d)) }
81517eed8a78 partial scans of nested comments;
wenzelm
parents: 43411
diff changeset
   381
      else failure("")
81517eed8a78 partial scans of nested comments;
wenzelm
parents: 43411
diff changeset
   382
    }
34143
ded454429df3 added nested comments;
wenzelm
parents: 34140
diff changeset
   383
48763
de68fc11c245 more precise recover_quoted, recover_verbatim, recover_comment (cf. ML version) -- NB: context parsers expect explicit termination;
wenzelm
parents: 48754
diff changeset
   384
    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
   385
      comment_depth(0) ^^ (_._1)
34143
ded454429df3 added nested comments;
wenzelm
parents: 34140
diff changeset
   386
55033
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   387
    def comment_content(source: String): String =
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   388
    {
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   389
      require(parseAll(comment, source).successful)
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   390
      source.substring(2, source.length - 2)
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   391
    }
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   392
34143
ded454429df3 added nested comments;
wenzelm
parents: 34140
diff changeset
   393
34140
31be1235d0fb simplified result of keyword and symbols parser;
wenzelm
parents: 34137
diff changeset
   394
    /* outer syntax tokens */
31be1235d0fb simplified result of keyword and symbols parser;
wenzelm
parents: 34137
diff changeset
   395
43411
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   396
    private def delimited_token: Parser[Token] =
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   397
    {
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   398
      val string = quoted("\"") ^^ (x => Token(Token.Kind.STRING, x))
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   399
      val alt_string = quoted("`") ^^ (x => Token(Token.Kind.ALT_STRING, x))
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   400
      val verb = verbatim ^^ (x => Token(Token.Kind.VERBATIM, x))
55033
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   401
      val cart = cartouche ^^ (x => Token(Token.Kind.CARTOUCHE, x))
43411
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   402
      val cmt = comment ^^ (x => Token(Token.Kind.COMMENT, x))
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   403
55033
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   404
      string | (alt_string | (verb | (cart | cmt)))
43411
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   405
    }
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   406
43695
5130dfe1b7be simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents: 43420
diff changeset
   407
    private def other_token(is_command: String => Boolean)
43411
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   408
      : Parser[Token] =
34140
31be1235d0fb simplified result of keyword and symbols parser;
wenzelm
parents: 34137
diff changeset
   409
    {
52616
3ac2878764f9 more robust identifier syntax: sub/superscript counts as modifier of LETDIG part instead of LETTER, both isub/isup and sub/sup are allowed;
wenzelm
parents: 48914
diff changeset
   410
      val letdigs1 = many1(Symbol.is_letdig)
53021
d0fa3f446b9d discontinued special treatment of \<^isub> and \<^isup> in rendering or editor front-end;
wenzelm
parents: 52920
diff changeset
   411
      val sub = one(s => s == Symbol.sub_decoded || s == "\\<^sub>")
52616
3ac2878764f9 more robust identifier syntax: sub/superscript counts as modifier of LETDIG part instead of LETTER, both isub/isup and sub/sup are allowed;
wenzelm
parents: 48914
diff changeset
   412
      val id =
3ac2878764f9 more robust identifier syntax: sub/superscript counts as modifier of LETDIG part instead of LETTER, both isub/isup and sub/sup are allowed;
wenzelm
parents: 48914
diff changeset
   413
        one(Symbol.is_letter) ~
52920
4539e4a06339 more strict identifier syntax: disallow superscripts, which tend to be used in notation such as \<^sup>\<omega>;
wenzelm
parents: 52616
diff changeset
   414
          (rep(letdigs1 | (sub ~ letdigs1 ^^ { case x ~ y => x + y })) ^^ (_.mkString)) ^^
52616
3ac2878764f9 more robust identifier syntax: sub/superscript counts as modifier of LETDIG part instead of LETTER, both isub/isup and sub/sup are allowed;
wenzelm
parents: 48914
diff changeset
   415
        { case x ~ y => x + y }
3ac2878764f9 more robust identifier syntax: sub/superscript counts as modifier of LETDIG part instead of LETTER, both isub/isup and sub/sup are allowed;
wenzelm
parents: 48914
diff changeset
   416
43695
5130dfe1b7be simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents: 43420
diff changeset
   417
      val nat = many1(Symbol.is_digit)
40290
47f572aff50a support for floating-point tokens in outer syntax (coinciding with inner syntax version);
wenzelm
parents: 38367
diff changeset
   418
      val natdot = nat ~ "." ~ nat ^^ { case x ~ y ~ z => x + y + z }
34140
31be1235d0fb simplified result of keyword and symbols parser;
wenzelm
parents: 34137
diff changeset
   419
      val id_nat = id ~ opt("." ~ nat) ^^ { case x ~ Some(y ~ z) => x + y + z case x ~ None => x }
31be1235d0fb simplified result of keyword and symbols parser;
wenzelm
parents: 34137
diff changeset
   420
31be1235d0fb simplified result of keyword and symbols parser;
wenzelm
parents: 34137
diff changeset
   421
      val ident = id ~ rep("." ~> id) ^^
36956
21be4832c362 renamed class Outer_Lex to Token and Token_Kind to Token.Kind;
wenzelm
parents: 36679
diff changeset
   422
        { case x ~ Nil => Token(Token.Kind.IDENT, x)
21be4832c362 renamed class Outer_Lex to Token and Token_Kind to Token.Kind;
wenzelm
parents: 36679
diff changeset
   423
          case x ~ ys => Token(Token.Kind.LONG_IDENT, (x :: ys).mkString(".")) }
34140
31be1235d0fb simplified result of keyword and symbols parser;
wenzelm
parents: 34137
diff changeset
   424
36956
21be4832c362 renamed class Outer_Lex to Token and Token_Kind to Token.Kind;
wenzelm
parents: 36679
diff changeset
   425
      val var_ = "?" ~ id_nat ^^ { case x ~ y => Token(Token.Kind.VAR, x + y) }
21be4832c362 renamed class Outer_Lex to Token and Token_Kind to Token.Kind;
wenzelm
parents: 36679
diff changeset
   426
      val type_ident = "'" ~ id ^^ { case x ~ y => Token(Token.Kind.TYPE_IDENT, x + y) }
21be4832c362 renamed class Outer_Lex to Token and Token_Kind to Token.Kind;
wenzelm
parents: 36679
diff changeset
   427
      val type_var = "?'" ~ id_nat ^^ { case x ~ y => Token(Token.Kind.TYPE_VAR, x + y) }
21be4832c362 renamed class Outer_Lex to Token and Token_Kind to Token.Kind;
wenzelm
parents: 36679
diff changeset
   428
      val nat_ = nat ^^ (x => Token(Token.Kind.NAT, x))
40290
47f572aff50a support for floating-point tokens in outer syntax (coinciding with inner syntax version);
wenzelm
parents: 38367
diff changeset
   429
      val float =
47f572aff50a support for floating-point tokens in outer syntax (coinciding with inner syntax version);
wenzelm
parents: 38367
diff changeset
   430
        ("-" ~ natdot ^^ { case x ~ y => x + y } | natdot) ^^ (x => Token(Token.Kind.FLOAT, x))
34140
31be1235d0fb simplified result of keyword and symbols parser;
wenzelm
parents: 34137
diff changeset
   431
31be1235d0fb simplified result of keyword and symbols parser;
wenzelm
parents: 34137
diff changeset
   432
      val sym_ident =
43695
5130dfe1b7be simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents: 43420
diff changeset
   433
        (many1(Symbol.is_symbolic_char) | one(sym => Symbol.is_symbolic(sym))) ^^
36956
21be4832c362 renamed class Outer_Lex to Token and Token_Kind to Token.Kind;
wenzelm
parents: 36679
diff changeset
   434
        (x => Token(Token.Kind.SYM_IDENT, x))
34140
31be1235d0fb simplified result of keyword and symbols parser;
wenzelm
parents: 34137
diff changeset
   435
48743
a72f8ffecf31 refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents: 48409
diff changeset
   436
      val command_keyword =
a72f8ffecf31 refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents: 48409
diff changeset
   437
        keyword ^^ (x => Token(if (is_command(x)) Token.Kind.COMMAND else Token.Kind.KEYWORD, x))
a72f8ffecf31 refined recovery of scan errors: longest prefix of delimited token after failure, otherwise just one symbol;
wenzelm
parents: 48409
diff changeset
   438
43695
5130dfe1b7be simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents: 43420
diff changeset
   439
      val space = many1(Symbol.is_blank) ^^ (x => Token(Token.Kind.SPACE, x))
34140
31be1235d0fb simplified result of keyword and symbols parser;
wenzelm
parents: 34137
diff changeset
   440
48744
wenzelm
parents: 48743
diff changeset
   441
      val recover_delimited =
55033
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   442
        (recover_quoted("\"") |
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   443
          (recover_quoted("`") |
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   444
            (recover_verbatim |
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   445
              (recover_cartouche | recover_comment)))) ^^ (x => Token(Token.Kind.ERROR, x))
34140
31be1235d0fb simplified result of keyword and symbols parser;
wenzelm
parents: 34137
diff changeset
   446
48754
c2c1e5944536 clarified undefined, unparsed, unfinished command spans;
wenzelm
parents: 48744
diff changeset
   447
      val bad = one(_ => true) ^^ (x => Token(Token.Kind.ERROR, x))
34140
31be1235d0fb simplified result of keyword and symbols parser;
wenzelm
parents: 34137
diff changeset
   448
48744
wenzelm
parents: 48743
diff changeset
   449
      space | (recover_delimited |
43411
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   450
        (((ident | (var_ | (type_ident | (type_var | (float | (nat_ | sym_ident)))))) |||
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   451
          command_keyword) | bad))
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   452
    }
34140
31be1235d0fb simplified result of keyword and symbols parser;
wenzelm
parents: 34137
diff changeset
   453
43695
5130dfe1b7be simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents: 43420
diff changeset
   454
    def token(is_command: String => Boolean): Parser[Token] =
5130dfe1b7be simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents: 43420
diff changeset
   455
      delimited_token | other_token(is_command)
34140
31be1235d0fb simplified result of keyword and symbols parser;
wenzelm
parents: 34137
diff changeset
   456
43695
5130dfe1b7be simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents: 43420
diff changeset
   457
    def token_context(is_command: String => Boolean, ctxt: Context): Parser[(Token, Context)] =
43411
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   458
    {
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   459
      val string =
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   460
        quoted_context("\"", ctxt) ^^ { case (x, c) => (Token(Token.Kind.STRING, x), c) }
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   461
      val alt_string =
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   462
        quoted_context("`", ctxt) ^^ { case (x, c) => (Token(Token.Kind.ALT_STRING, x), c) }
43412
81517eed8a78 partial scans of nested comments;
wenzelm
parents: 43411
diff changeset
   463
      val verb = verbatim_context(ctxt) ^^ { case (x, c) => (Token(Token.Kind.VERBATIM, x), c) }
55033
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   464
      val cart = cartouche_context(ctxt) ^^ { case (x, c) => (Token(Token.Kind.CARTOUCHE, x), c) }
43412
81517eed8a78 partial scans of nested comments;
wenzelm
parents: 43411
diff changeset
   465
      val cmt = comment_context(ctxt) ^^ { case (x, c) => (Token(Token.Kind.COMMENT, x), c) }
43695
5130dfe1b7be simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents: 43420
diff changeset
   466
      val other = other_token(is_command) ^^ { case x => (x, Finished) }
43411
0206466ee473 some support for partial scans with explicit context;
wenzelm
parents: 42718
diff changeset
   467
55033
8e8243975860 support for nested text cartouches;
wenzelm
parents: 53021
diff changeset
   468
      string | (alt_string | (verb | (cart | (cmt | other))))
34140
31be1235d0fb simplified result of keyword and symbols parser;
wenzelm
parents: 34137
diff changeset
   469
    }
31649
a11ea667d676 reorganized and abstracted version, via Set trait;
wenzelm
parents: 31648
diff changeset
   470
  }
31648
31b1f296515b Efficient scanning of literals.
wenzelm
parents:
diff changeset
   471
}