| author | haftmann | 
| Wed, 17 Feb 2010 09:48:53 +0100 | |
| changeset 35160 | 6eb2b6c1d2d5 | 
| parent 34316 | f879b649ac4c | 
| child 36011 | 3ff725ac13a4 | 
| permissions | -rw-r--r-- | 
| 31648 | 1  | 
/* Title: Pure/General/scan.scala  | 
2  | 
Author: Makarius  | 
|
3  | 
||
| 
31649
 
a11ea667d676
reorganized and abstracted version, via Set trait;
 
wenzelm 
parents: 
31648 
diff
changeset
 | 
4  | 
Efficient scanning of keywords.  | 
| 31648 | 5  | 
*/  | 
6  | 
||
7  | 
package isabelle  | 
|
8  | 
||
| 
34187
 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 
wenzelm 
parents: 
34157 
diff
changeset
 | 
9  | 
|
| 
 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 
wenzelm 
parents: 
34157 
diff
changeset
 | 
10  | 
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
 | 
11  | 
import scala.util.parsing.input.{OffsetPosition, Position => InputPosition, Reader}
 | 
| 31648 | 12  | 
import scala.util.parsing.combinator.RegexParsers  | 
13  | 
||
| 
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
 | 
14  | 
import java.io.{File, InputStream, BufferedInputStream, FileInputStream}
 | 
| 
 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 
wenzelm 
parents: 
34157 
diff
changeset
 | 
15  | 
|
| 31648 | 16  | 
|
17  | 
object Scan  | 
|
18  | 
{
 | 
|
| 
31649
 
a11ea667d676
reorganized and abstracted version, via Set trait;
 
wenzelm 
parents: 
31648 
diff
changeset
 | 
19  | 
/** Lexicon -- position tree **/  | 
| 31648 | 20  | 
|
| 
31649
 
a11ea667d676
reorganized and abstracted version, via Set trait;
 
wenzelm 
parents: 
31648 
diff
changeset
 | 
21  | 
object Lexicon  | 
| 31648 | 22  | 
  {
 | 
| 
31649
 
a11ea667d676
reorganized and abstracted version, via Set trait;
 
wenzelm 
parents: 
31648 
diff
changeset
 | 
23  | 
private case class Tree(val branches: Map[Char, (String, Tree)])  | 
| 
 
a11ea667d676
reorganized and abstracted version, via Set trait;
 
wenzelm 
parents: 
31648 
diff
changeset
 | 
24  | 
private val empty_tree = Tree(Map())  | 
| 
 
a11ea667d676
reorganized and abstracted version, via Set trait;
 
wenzelm 
parents: 
31648 
diff
changeset
 | 
25  | 
|
| 
 
a11ea667d676
reorganized and abstracted version, via Set trait;
 
wenzelm 
parents: 
31648 
diff
changeset
 | 
26  | 
val empty: Lexicon = new Lexicon  | 
| 
31762
 
20745ab5b79a
more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
 
wenzelm 
parents: 
31753 
diff
changeset
 | 
27  | 
def apply(elems: String*): Lexicon = empty ++ elems  | 
| 31648 | 28  | 
}  | 
29  | 
||
| 31764 | 30  | 
class Lexicon extends scala.collection.Set[String] with RegexParsers  | 
| 31648 | 31  | 
  {
 | 
| 
31649
 
a11ea667d676
reorganized and abstracted version, via Set trait;
 
wenzelm 
parents: 
31648 
diff
changeset
 | 
32  | 
/* representation */  | 
| 31648 | 33  | 
|
| 
31649
 
a11ea667d676
reorganized and abstracted version, via Set trait;
 
wenzelm 
parents: 
31648 
diff
changeset
 | 
34  | 
import Lexicon.Tree  | 
| 31764 | 35  | 
protected val main_tree: Tree = Lexicon.empty_tree  | 
| 31648 | 36  | 
|
37  | 
||
| 31650 | 38  | 
/* auxiliary operations */  | 
39  | 
||
40  | 
private def content(tree: Tree, result: List[String]): List[String] =  | 
|
41  | 
(result /: tree.branches.toList) ((res, entry) =>  | 
|
42  | 
        entry match { case (_, (s, tr)) =>
 | 
|
43  | 
if (s.isEmpty) content(tr, res) else content(tr, s :: res) })  | 
|
44  | 
||
45  | 
private def lookup(str: CharSequence): Option[(Boolean, Tree)] =  | 
|
46  | 
    {
 | 
|
47  | 
val len = str.length  | 
|
48  | 
def look(tree: Tree, tip: Boolean, i: Int): Option[(Boolean, Tree)] =  | 
|
49  | 
      {
 | 
|
50  | 
        if (i < len) {
 | 
|
51  | 
          tree.branches.get(str.charAt(i)) match {
 | 
|
52  | 
case Some((s, tr)) => look(tr, !s.isEmpty, i + 1)  | 
|
53  | 
case None => None  | 
|
54  | 
}  | 
|
55  | 
} else Some(tip, tree)  | 
|
56  | 
}  | 
|
57  | 
look(main_tree, false, 0)  | 
|
58  | 
}  | 
|
59  | 
||
60  | 
def completions(str: CharSequence): List[String] =  | 
|
| 31764 | 61  | 
      lookup(str) match {
 | 
| 31650 | 62  | 
case Some((true, tree)) => content(tree, List(str.toString))  | 
63  | 
case Some((false, tree)) => content(tree, Nil)  | 
|
64  | 
case None => Nil  | 
|
| 31764 | 65  | 
}  | 
| 31650 | 66  | 
|
67  | 
||
| 
31649
 
a11ea667d676
reorganized and abstracted version, via Set trait;
 
wenzelm 
parents: 
31648 
diff
changeset
 | 
68  | 
/* Set methods */  | 
| 
 
a11ea667d676
reorganized and abstracted version, via Set trait;
 
wenzelm 
parents: 
31648 
diff
changeset
 | 
69  | 
|
| 
 
a11ea667d676
reorganized and abstracted version, via Set trait;
 
wenzelm 
parents: 
31648 
diff
changeset
 | 
70  | 
override def stringPrefix = "Lexicon"  | 
| 
 
a11ea667d676
reorganized and abstracted version, via Set trait;
 
wenzelm 
parents: 
31648 
diff
changeset
 | 
71  | 
|
| 31753 | 72  | 
    override def isEmpty: Boolean = { main_tree.branches.isEmpty }
 | 
| 
31649
 
a11ea667d676
reorganized and abstracted version, via Set trait;
 
wenzelm 
parents: 
31648 
diff
changeset
 | 
73  | 
|
| 31650 | 74  | 
def size: Int = content(main_tree, Nil).length  | 
75  | 
def elements: Iterator[String] = content(main_tree, Nil).sort(_ <= _).elements  | 
|
| 31648 | 76  | 
|
| 
31762
 
20745ab5b79a
more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
 
wenzelm 
parents: 
31753 
diff
changeset
 | 
77  | 
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
 | 
78  | 
      lookup(elem) match {
 | 
| 31650 | 79  | 
case Some((tip, _)) => tip  | 
80  | 
case _ => false  | 
|
81  | 
}  | 
|
| 31648 | 82  | 
|
| 
31762
 
20745ab5b79a
more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
 
wenzelm 
parents: 
31753 
diff
changeset
 | 
83  | 
def + (elem: String): Lexicon =  | 
| 
 
20745ab5b79a
more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
 
wenzelm 
parents: 
31753 
diff
changeset
 | 
84  | 
if (contains(elem)) this  | 
| 
 
20745ab5b79a
more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
 
wenzelm 
parents: 
31753 
diff
changeset
 | 
85  | 
      else {
 | 
| 
 
20745ab5b79a
more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
 
wenzelm 
parents: 
31753 
diff
changeset
 | 
86  | 
val len = elem.length  | 
| 
 
20745ab5b79a
more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
 
wenzelm 
parents: 
31753 
diff
changeset
 | 
87  | 
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
 | 
88  | 
          if (i < len) {
 | 
| 
 
20745ab5b79a
more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
 
wenzelm 
parents: 
31753 
diff
changeset
 | 
89  | 
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
 | 
90  | 
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
 | 
91  | 
            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
 | 
92  | 
case Some((s, tr)) =>  | 
| 
 
20745ab5b79a
more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
 
wenzelm 
parents: 
31753 
diff
changeset
 | 
93  | 
Tree(tree.branches +  | 
| 
 
20745ab5b79a
more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
 
wenzelm 
parents: 
31753 
diff
changeset
 | 
94  | 
(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
 | 
95  | 
case None =>  | 
| 
 
20745ab5b79a
more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
 
wenzelm 
parents: 
31753 
diff
changeset
 | 
96  | 
Tree(tree.branches +  | 
| 
 
20745ab5b79a
more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
 
wenzelm 
parents: 
31753 
diff
changeset
 | 
97  | 
(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
 | 
98  | 
}  | 
| 34135 | 99  | 
}  | 
100  | 
else tree  | 
|
| 
31762
 
20745ab5b79a
more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
 
wenzelm 
parents: 
31753 
diff
changeset
 | 
101  | 
val old = this  | 
| 
 
20745ab5b79a
more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
 
wenzelm 
parents: 
31753 
diff
changeset
 | 
102  | 
        new Lexicon { override val main_tree = extend(old.main_tree, 0) }
 | 
| 31648 | 103  | 
}  | 
| 
31762
 
20745ab5b79a
more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
 
wenzelm 
parents: 
31753 
diff
changeset
 | 
104  | 
|
| 31764 | 105  | 
def + (elem1: String, elem2: String, elems: String*): Lexicon =  | 
| 
31762
 
20745ab5b79a
more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
 
wenzelm 
parents: 
31753 
diff
changeset
 | 
106  | 
this + elem1 + elem2 ++ elems  | 
| 31764 | 107  | 
def ++ (elems: Iterable[String]): Lexicon = (this /: elems) ((s, elem) => s + elem)  | 
108  | 
def ++ (elems: Iterator[String]): Lexicon = (this /: elems) ((s, elem) => s + elem)  | 
|
| 
31649
 
a11ea667d676
reorganized and abstracted version, via Set trait;
 
wenzelm 
parents: 
31648 
diff
changeset
 | 
109  | 
|
| 
 
a11ea667d676
reorganized and abstracted version, via Set trait;
 
wenzelm 
parents: 
31648 
diff
changeset
 | 
110  | 
|
| 34301 | 111  | 
|
| 34137 | 112  | 
/** RegexParsers methods **/  | 
| 
31649
 
a11ea667d676
reorganized and abstracted version, via Set trait;
 
wenzelm 
parents: 
31648 
diff
changeset
 | 
113  | 
|
| 
 
a11ea667d676
reorganized and abstracted version, via Set trait;
 
wenzelm 
parents: 
31648 
diff
changeset
 | 
114  | 
override val whiteSpace = "".r  | 
| 
 
a11ea667d676
reorganized and abstracted version, via Set trait;
 
wenzelm 
parents: 
31648 
diff
changeset
 | 
115  | 
|
| 34137 | 116  | 
|
117  | 
/* keywords from lexicon */  | 
|
118  | 
||
| 34140 | 119  | 
def keyword: Parser[String] = new Parser[String]  | 
| 34135 | 120  | 
    {
 | 
| 
31649
 
a11ea667d676
reorganized and abstracted version, via Set trait;
 
wenzelm 
parents: 
31648 
diff
changeset
 | 
121  | 
def apply(in: Input) =  | 
| 
 
a11ea667d676
reorganized and abstracted version, via Set trait;
 
wenzelm 
parents: 
31648 
diff
changeset
 | 
122  | 
      {
 | 
| 
 
a11ea667d676
reorganized and abstracted version, via Set trait;
 
wenzelm 
parents: 
31648 
diff
changeset
 | 
123  | 
val source = in.source  | 
| 
 
a11ea667d676
reorganized and abstracted version, via Set trait;
 
wenzelm 
parents: 
31648 
diff
changeset
 | 
124  | 
val offset = in.offset  | 
| 
 
a11ea667d676
reorganized and abstracted version, via Set trait;
 
wenzelm 
parents: 
31648 
diff
changeset
 | 
125  | 
val len = source.length - offset  | 
| 
 
a11ea667d676
reorganized and abstracted version, via Set trait;
 
wenzelm 
parents: 
31648 
diff
changeset
 | 
126  | 
|
| 34140 | 127  | 
def scan(tree: Tree, result: String, i: Int): String =  | 
| 
31649
 
a11ea667d676
reorganized and abstracted version, via Set trait;
 
wenzelm 
parents: 
31648 
diff
changeset
 | 
128  | 
        {
 | 
| 
 
a11ea667d676
reorganized and abstracted version, via Set trait;
 
wenzelm 
parents: 
31648 
diff
changeset
 | 
129  | 
          if (i < len) {
 | 
| 
 
a11ea667d676
reorganized and abstracted version, via Set trait;
 
wenzelm 
parents: 
31648 
diff
changeset
 | 
130  | 
            tree.branches.get(source.charAt(offset + i)) match {
 | 
| 34140 | 131  | 
case Some((s, tr)) => scan(tr, if (s.isEmpty) result else s, i + 1)  | 
| 34135 | 132  | 
case None => result  | 
| 
31649
 
a11ea667d676
reorganized and abstracted version, via Set trait;
 
wenzelm 
parents: 
31648 
diff
changeset
 | 
133  | 
}  | 
| 34135 | 134  | 
}  | 
135  | 
else result  | 
|
| 
31649
 
a11ea667d676
reorganized and abstracted version, via Set trait;
 
wenzelm 
parents: 
31648 
diff
changeset
 | 
136  | 
}  | 
| 34140 | 137  | 
val result = scan(main_tree, "", 0)  | 
138  | 
        if (result.isEmpty) Failure("keyword expected", in)
 | 
|
139  | 
else Success(result, in.drop(result.length))  | 
|
| 31648 | 140  | 
}  | 
| 
31649
 
a11ea667d676
reorganized and abstracted version, via Set trait;
 
wenzelm 
parents: 
31648 
diff
changeset
 | 
141  | 
    }.named("keyword")
 | 
| 31648 | 142  | 
|
| 34137 | 143  | 
|
| 
34157
 
0a0a19153626
explicit representation of Token_Kind -- cannot really depend on runtime types due to erasure;
 
wenzelm 
parents: 
34144 
diff
changeset
 | 
144  | 
/* symbol range */  | 
| 34137 | 145  | 
|
| 
34157
 
0a0a19153626
explicit representation of Token_Kind -- cannot really depend on runtime types due to erasure;
 
wenzelm 
parents: 
34144 
diff
changeset
 | 
146  | 
def symbol_range(pred: String => Boolean, min_count: Int, max_count: Int): Parser[String] =  | 
| 34140 | 147  | 
new Parser[String]  | 
| 34135 | 148  | 
      {
 | 
| 34137 | 149  | 
def apply(in: Input) =  | 
150  | 
        {
 | 
|
151  | 
val start = in.offset  | 
|
152  | 
val end = in.source.length  | 
|
153  | 
val matcher = new Symbol.Matcher(in.source)  | 
|
154  | 
||
155  | 
var i = start  | 
|
156  | 
var count = 0  | 
|
157  | 
var finished = false  | 
|
158  | 
          while (!finished) {
 | 
|
159  | 
            if (i < end && count < max_count) {
 | 
|
160  | 
val n = matcher(i, end)  | 
|
161  | 
val sym = in.source.subSequence(i, i + n).toString  | 
|
162  | 
              if (pred(sym)) { i += n; count += 1 }
 | 
|
163  | 
else finished = true  | 
|
164  | 
}  | 
|
165  | 
else finished = true  | 
|
166  | 
}  | 
|
| 34140 | 167  | 
          if (count < min_count) Failure("bad input", in)
 | 
168  | 
else Success(in.source.subSequence(start, i).toString, in.drop(i - start))  | 
|
| 34137 | 169  | 
}  | 
| 
34157
 
0a0a19153626
explicit representation of Token_Kind -- cannot really depend on runtime types due to erasure;
 
wenzelm 
parents: 
34144 
diff
changeset
 | 
170  | 
      }.named("symbol_range")
 | 
| 34137 | 171  | 
|
| 
34157
 
0a0a19153626
explicit representation of Token_Kind -- cannot really depend on runtime types due to erasure;
 
wenzelm 
parents: 
34144 
diff
changeset
 | 
172  | 
def one(pred: String => Boolean): Parser[String] = symbol_range(pred, 1, 1)  | 
| 
 
0a0a19153626
explicit representation of Token_Kind -- cannot really depend on runtime types due to erasure;
 
wenzelm 
parents: 
34144 
diff
changeset
 | 
173  | 
def many(pred: String => Boolean): Parser[String] = symbol_range(pred, 0, Integer.MAX_VALUE)  | 
| 
 
0a0a19153626
explicit representation of Token_Kind -- cannot really depend on runtime types due to erasure;
 
wenzelm 
parents: 
34144 
diff
changeset
 | 
174  | 
def many1(pred: String => Boolean): Parser[String] = symbol_range(pred, 1, Integer.MAX_VALUE)  | 
| 34140 | 175  | 
|
176  | 
||
| 34143 | 177  | 
/* quoted strings */  | 
| 34140 | 178  | 
|
| 34143 | 179  | 
private def quoted(quote: String): Parser[String] =  | 
180  | 
    {
 | 
|
| 34140 | 181  | 
quote ~  | 
| 
34316
 
f879b649ac4c
clarified Symbol.is_plain/is_wellformed -- is_closed was rejecting plain backslashes;
 
wenzelm 
parents: 
34301 
diff
changeset
 | 
182  | 
rep(many1(sym => sym != quote && sym != "\\" && Symbol.is_wellformed(sym)) |  | 
| 34140 | 183  | 
"\\" + quote | "\\\\" |  | 
184  | 
          (("""\\\d\d\d""".r) ^? { case x if x.substring(1, 4).toInt <= 255 => x })) ~
 | 
|
185  | 
      quote ^^ { case x ~ ys ~ z => x + ys.mkString + z }
 | 
|
| 34143 | 186  | 
    }.named("quoted")
 | 
| 34140 | 187  | 
|
188  | 
def quoted_content(quote: String, source: String): String =  | 
|
189  | 
    {
 | 
|
190  | 
require(parseAll(quoted(quote), source).successful)  | 
|
| 34189 | 191  | 
val body = source.substring(1, source.length - 1)  | 
192  | 
      if (body.exists(_ == '\\')) {
 | 
|
193  | 
val content =  | 
|
| 
34316
 
f879b649ac4c
clarified Symbol.is_plain/is_wellformed -- is_closed was rejecting plain backslashes;
 
wenzelm 
parents: 
34301 
diff
changeset
 | 
194  | 
rep(many1(sym => sym != quote && sym != "\\" && Symbol.is_wellformed(sym)) |  | 
| 34189 | 195  | 
              "\\" ~> (quote | "\\" | """\d\d\d""".r ^^ { case x => x.toInt.toChar.toString }))
 | 
196  | 
parseAll(content ^^ (_.mkString), body).get  | 
|
197  | 
}  | 
|
198  | 
else body  | 
|
| 34140 | 199  | 
}  | 
200  | 
||
201  | 
||
| 34143 | 202  | 
/* verbatim text */  | 
203  | 
||
204  | 
private def verbatim: Parser[String] =  | 
|
205  | 
    {
 | 
|
| 
34316
 
f879b649ac4c
clarified Symbol.is_plain/is_wellformed -- is_closed was rejecting plain backslashes;
 
wenzelm 
parents: 
34301 
diff
changeset
 | 
206  | 
      "{*" ~ rep(many1(sym => sym != "*" && Symbol.is_wellformed(sym)) | """\*(?!\})""".r) ~ "*}" ^^
 | 
| 34140 | 207  | 
        { case x ~ ys ~ z => x + ys.mkString + z }
 | 
| 34143 | 208  | 
    }.named("verbatim")
 | 
| 34140 | 209  | 
|
210  | 
def verbatim_content(source: String): String =  | 
|
211  | 
    {
 | 
|
212  | 
require(parseAll(verbatim, source).successful)  | 
|
213  | 
source.substring(2, source.length - 2)  | 
|
214  | 
}  | 
|
215  | 
||
216  | 
||
| 34143 | 217  | 
/* nested comments */  | 
218  | 
||
219  | 
def comment: Parser[String] = new Parser[String]  | 
|
220  | 
    {
 | 
|
221  | 
val comment_text =  | 
|
| 
34316
 
f879b649ac4c
clarified Symbol.is_plain/is_wellformed -- is_closed was rejecting plain backslashes;
 
wenzelm 
parents: 
34301 
diff
changeset
 | 
222  | 
        rep(many1(sym => sym != "*" && sym != "(" && Symbol.is_wellformed(sym)) |
 | 
| 34143 | 223  | 
"""\*(?!\))|\((?!\*)""".r)  | 
| 34263 | 224  | 
val comment_open = "(*" ~ comment_text ^^^ ()  | 
225  | 
val comment_close = comment_text ~ "*)" ^^^ ()  | 
|
| 34143 | 226  | 
|
227  | 
def apply(in: Input) =  | 
|
228  | 
      {
 | 
|
229  | 
var rest = in  | 
|
230  | 
def try_parse(p: Parser[Unit]): Boolean =  | 
|
231  | 
        {
 | 
|
232  | 
          parse(p, rest) match {
 | 
|
233  | 
            case Success(_, next) => { rest = next; true }
 | 
|
234  | 
case _ => false  | 
|
235  | 
}  | 
|
236  | 
}  | 
|
237  | 
var depth = 0  | 
|
238  | 
var finished = false  | 
|
239  | 
        while (!finished) {
 | 
|
240  | 
if (try_parse(comment_open)) depth += 1  | 
|
241  | 
else if (depth > 0 && try_parse(comment_close)) depth -= 1  | 
|
242  | 
else finished = true  | 
|
243  | 
}  | 
|
244  | 
if (in.offset < rest.offset && depth == 0)  | 
|
245  | 
Success(in.source.subSequence(in.offset, rest.offset).toString, rest)  | 
|
246  | 
        else Failure("comment expected", in)
 | 
|
247  | 
}  | 
|
248  | 
    }.named("comment")
 | 
|
249  | 
||
250  | 
def comment_content(source: String): String =  | 
|
251  | 
    {
 | 
|
252  | 
require(parseAll(comment, source).successful)  | 
|
253  | 
source.substring(2, source.length - 2)  | 
|
254  | 
}  | 
|
255  | 
||
256  | 
||
| 34140 | 257  | 
/* outer syntax tokens */  | 
258  | 
||
259  | 
def token(symbols: Symbol.Interpretation, is_command: String => Boolean):  | 
|
260  | 
Parser[Outer_Lex.Token] =  | 
|
261  | 
    {
 | 
|
| 
34157
 
0a0a19153626
explicit representation of Token_Kind -- cannot really depend on runtime types due to erasure;
 
wenzelm 
parents: 
34144 
diff
changeset
 | 
262  | 
import Outer_Lex.Token_Kind, Outer_Lex.Token  | 
| 34140 | 263  | 
|
264  | 
      val id = one(symbols.is_letter) ~ many(symbols.is_letdig) ^^ { case x ~ y => x + y }
 | 
|
265  | 
val nat = many1(symbols.is_digit)  | 
|
266  | 
      val id_nat = id ~ opt("." ~ nat) ^^ { case x ~ Some(y ~ z) => x + y + z case x ~ None => x }
 | 
|
267  | 
||
268  | 
      val ident = id ~ rep("." ~> id) ^^
 | 
|
| 
34157
 
0a0a19153626
explicit representation of Token_Kind -- cannot really depend on runtime types due to erasure;
 
wenzelm 
parents: 
34144 
diff
changeset
 | 
269  | 
        { case x ~ Nil => Token(Token_Kind.IDENT, x)
 | 
| 
 
0a0a19153626
explicit representation of Token_Kind -- cannot really depend on runtime types due to erasure;
 
wenzelm 
parents: 
34144 
diff
changeset
 | 
270  | 
          case x ~ ys => Token(Token_Kind.LONG_IDENT, (x :: ys).mkString(".")) }
 | 
| 34140 | 271  | 
|
| 
34157
 
0a0a19153626
explicit representation of Token_Kind -- cannot really depend on runtime types due to erasure;
 
wenzelm 
parents: 
34144 
diff
changeset
 | 
272  | 
      val var_ = "?" ~ id_nat ^^ { case x ~ y => Token(Token_Kind.VAR, x + y) }
 | 
| 
 
0a0a19153626
explicit representation of Token_Kind -- cannot really depend on runtime types due to erasure;
 
wenzelm 
parents: 
34144 
diff
changeset
 | 
273  | 
      val type_ident = "'" ~ id ^^ { case x ~ y => Token(Token_Kind.TYPE_IDENT, x + y) }
 | 
| 
 
0a0a19153626
explicit representation of Token_Kind -- cannot really depend on runtime types due to erasure;
 
wenzelm 
parents: 
34144 
diff
changeset
 | 
274  | 
      val type_var = "?'" ~ id_nat ^^ { case x ~ y => Token(Token_Kind.TYPE_VAR, x + y) }
 | 
| 
 
0a0a19153626
explicit representation of Token_Kind -- cannot really depend on runtime types due to erasure;
 
wenzelm 
parents: 
34144 
diff
changeset
 | 
275  | 
val nat_ = nat ^^ (x => Token(Token_Kind.NAT, x))  | 
| 34140 | 276  | 
|
277  | 
val sym_ident =  | 
|
278  | 
(many1(symbols.is_symbolic_char) |  | 
|
| 
34316
 
f879b649ac4c
clarified Symbol.is_plain/is_wellformed -- is_closed was rejecting plain backslashes;
 
wenzelm 
parents: 
34301 
diff
changeset
 | 
279  | 
one(sym => symbols.is_symbolic(sym) & Symbol.is_wellformed(sym))) ^^  | 
| 
34157
 
0a0a19153626
explicit representation of Token_Kind -- cannot really depend on runtime types due to erasure;
 
wenzelm 
parents: 
34144 
diff
changeset
 | 
280  | 
(x => Token(Token_Kind.SYM_IDENT, x))  | 
| 34140 | 281  | 
|
| 
34157
 
0a0a19153626
explicit representation of Token_Kind -- cannot really depend on runtime types due to erasure;
 
wenzelm 
parents: 
34144 
diff
changeset
 | 
282  | 
val space = many1(symbols.is_blank) ^^ (x => Token(Token_Kind.SPACE, x))  | 
| 34140 | 283  | 
|
| 
34157
 
0a0a19153626
explicit representation of Token_Kind -- cannot really depend on runtime types due to erasure;
 
wenzelm 
parents: 
34144 
diff
changeset
 | 
284  | 
      val string = quoted("\"") ^^ (x => Token(Token_Kind.STRING, x))
 | 
| 
 
0a0a19153626
explicit representation of Token_Kind -- cannot really depend on runtime types due to erasure;
 
wenzelm 
parents: 
34144 
diff
changeset
 | 
285  | 
      val alt_string = quoted("`") ^^ (x => Token(Token_Kind.ALT_STRING, x))
 | 
| 34140 | 286  | 
|
| 34267 | 287  | 
val junk = many1(sym => !(symbols.is_blank(sym)))  | 
288  | 
val bad_delimiter =  | 
|
289  | 
        ("\"" | "`" | "{*" | "(*") ~ junk ^^ { case x ~ y => Token(Token_Kind.BAD_INPUT, x + y) }
 | 
|
290  | 
val bad = junk ^^ (x => Token(Token_Kind.BAD_INPUT, x))  | 
|
| 34140 | 291  | 
|
292  | 
||
293  | 
/* tokens */  | 
|
294  | 
||
| 
34157
 
0a0a19153626
explicit representation of Token_Kind -- cannot really depend on runtime types due to erasure;
 
wenzelm 
parents: 
34144 
diff
changeset
 | 
295  | 
(space | (string | (alt_string | (verbatim ^^ (x => Token(Token_Kind.VERBATIM, x)) |  | 
| 
 
0a0a19153626
explicit representation of Token_Kind -- cannot really depend on runtime types due to erasure;
 
wenzelm 
parents: 
34144 
diff
changeset
 | 
296  | 
comment ^^ (x => Token(Token_Kind.COMMENT, x)))))) |  | 
| 34267 | 297  | 
bad_delimiter |  | 
| 
34144
 
bd7b3b91abab
improve performance by reordering of parser combinators;
 
wenzelm 
parents: 
34143 
diff
changeset
 | 
298  | 
((ident | (var_ | (type_ident | (type_var | (nat_ | sym_ident))))) |||  | 
| 
34157
 
0a0a19153626
explicit representation of Token_Kind -- cannot really depend on runtime types due to erasure;
 
wenzelm 
parents: 
34144 
diff
changeset
 | 
299  | 
keyword ^^ (x => Token(if (is_command(x)) Token_Kind.COMMAND else Token_Kind.KEYWORD, x))) |  | 
| 34267 | 300  | 
bad  | 
| 34140 | 301  | 
}  | 
| 
31649
 
a11ea667d676
reorganized and abstracted version, via Set trait;
 
wenzelm 
parents: 
31648 
diff
changeset
 | 
302  | 
}  | 
| 
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
 | 
303  | 
|
| 
 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 
wenzelm 
parents: 
34157 
diff
changeset
 | 
304  | 
|
| 
 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 
wenzelm 
parents: 
34157 
diff
changeset
 | 
305  | 
|
| 
 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 
wenzelm 
parents: 
34157 
diff
changeset
 | 
306  | 
/** read file without decoding -- enables efficient length operation **/  | 
| 
 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 
wenzelm 
parents: 
34157 
diff
changeset
 | 
307  | 
|
| 
 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 
wenzelm 
parents: 
34157 
diff
changeset
 | 
308  | 
private class Restricted_Seq(seq: RandomAccessSeq[Char], start: Int, end: Int)  | 
| 
 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 
wenzelm 
parents: 
34157 
diff
changeset
 | 
309  | 
extends CharSequence  | 
| 
 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 
wenzelm 
parents: 
34157 
diff
changeset
 | 
310  | 
  {
 | 
| 
 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 
wenzelm 
parents: 
34157 
diff
changeset
 | 
311  | 
def charAt(i: Int): Char =  | 
| 
 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 
wenzelm 
parents: 
34157 
diff
changeset
 | 
312  | 
if (0 <= i && i < length) seq(start + i)  | 
| 
 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 
wenzelm 
parents: 
34157 
diff
changeset
 | 
313  | 
else throw new IndexOutOfBoundsException  | 
| 
 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 
wenzelm 
parents: 
34157 
diff
changeset
 | 
314  | 
|
| 
 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 
wenzelm 
parents: 
34157 
diff
changeset
 | 
315  | 
def length: Int = end - start // avoid potentially expensive seq.length  | 
| 
 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 
wenzelm 
parents: 
34157 
diff
changeset
 | 
316  | 
|
| 
 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 
wenzelm 
parents: 
34157 
diff
changeset
 | 
317  | 
def subSequence(i: Int, j: Int): CharSequence =  | 
| 
 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 
wenzelm 
parents: 
34157 
diff
changeset
 | 
318  | 
if (0 <= i && i <= j && j <= length) new Restricted_Seq(seq, start + i, start + j)  | 
| 
 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 
wenzelm 
parents: 
34157 
diff
changeset
 | 
319  | 
else throw new IndexOutOfBoundsException  | 
| 
 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 
wenzelm 
parents: 
34157 
diff
changeset
 | 
320  | 
|
| 
 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 
wenzelm 
parents: 
34157 
diff
changeset
 | 
321  | 
override def toString: String =  | 
| 
 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 
wenzelm 
parents: 
34157 
diff
changeset
 | 
322  | 
    {
 | 
| 
 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 
wenzelm 
parents: 
34157 
diff
changeset
 | 
323  | 
val buf = new StringBuilder(length)  | 
| 
 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 
wenzelm 
parents: 
34157 
diff
changeset
 | 
324  | 
for (offset <- start until end) buf.append(seq(offset))  | 
| 
 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 
wenzelm 
parents: 
34157 
diff
changeset
 | 
325  | 
buf.toString  | 
| 
 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 
wenzelm 
parents: 
34157 
diff
changeset
 | 
326  | 
}  | 
| 
 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 
wenzelm 
parents: 
34157 
diff
changeset
 | 
327  | 
}  | 
| 
 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 
wenzelm 
parents: 
34157 
diff
changeset
 | 
328  | 
|
| 
 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 
wenzelm 
parents: 
34157 
diff
changeset
 | 
329  | 
  abstract class Byte_Reader extends Reader[Char] { def close: Unit }
 | 
| 
 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 
wenzelm 
parents: 
34157 
diff
changeset
 | 
330  | 
|
| 
 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 
wenzelm 
parents: 
34157 
diff
changeset
 | 
331  | 
def byte_reader(file: File): Byte_Reader =  | 
| 
 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 
wenzelm 
parents: 
34157 
diff
changeset
 | 
332  | 
  {
 | 
| 
 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 
wenzelm 
parents: 
34157 
diff
changeset
 | 
333  | 
val stream = new BufferedInputStream(new FileInputStream(file))  | 
| 
 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 
wenzelm 
parents: 
34157 
diff
changeset
 | 
334  | 
val seq = new 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
 | 
335  | 
(buf: Array[Char], offset: Int, length: Int) =>  | 
| 
 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 
wenzelm 
parents: 
34157 
diff
changeset
 | 
336  | 
        {
 | 
| 
 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 
wenzelm 
parents: 
34157 
diff
changeset
 | 
337  | 
var i = 0  | 
| 
 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 
wenzelm 
parents: 
34157 
diff
changeset
 | 
338  | 
var c = 0  | 
| 
 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 
wenzelm 
parents: 
34157 
diff
changeset
 | 
339  | 
var eof = false  | 
| 
 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 
wenzelm 
parents: 
34157 
diff
changeset
 | 
340  | 
          while (!eof && i < length) {
 | 
| 
 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 
wenzelm 
parents: 
34157 
diff
changeset
 | 
341  | 
c = stream.read  | 
| 
 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 
wenzelm 
parents: 
34157 
diff
changeset
 | 
342  | 
if (c == -1) eof = true  | 
| 
 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 
wenzelm 
parents: 
34157 
diff
changeset
 | 
343  | 
            else { buf(offset + i) = c.toChar; i += 1 }
 | 
| 
 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 
wenzelm 
parents: 
34157 
diff
changeset
 | 
344  | 
}  | 
| 
 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 
wenzelm 
parents: 
34157 
diff
changeset
 | 
345  | 
if (i > 0) i else -1  | 
| 
 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 
wenzelm 
parents: 
34157 
diff
changeset
 | 
346  | 
})  | 
| 
 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 
wenzelm 
parents: 
34157 
diff
changeset
 | 
347  | 
val restricted_seq = new Restricted_Seq(seq, 0, file.length.toInt)  | 
| 
 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 
wenzelm 
parents: 
34157 
diff
changeset
 | 
348  | 
|
| 
 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 
wenzelm 
parents: 
34157 
diff
changeset
 | 
349  | 
class Paged_Reader(override val offset: Int) extends Byte_Reader  | 
| 
 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 
wenzelm 
parents: 
34157 
diff
changeset
 | 
350  | 
    {
 | 
| 
 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 
wenzelm 
parents: 
34157 
diff
changeset
 | 
351  | 
override lazy val source: CharSequence = restricted_seq  | 
| 
 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 
wenzelm 
parents: 
34157 
diff
changeset
 | 
352  | 
def first: Char = if (seq.isDefinedAt(offset)) seq(offset) else '\032'  | 
| 
 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 
wenzelm 
parents: 
34157 
diff
changeset
 | 
353  | 
def rest: Paged_Reader = if (seq.isDefinedAt(offset)) new Paged_Reader(offset + 1) else this  | 
| 
 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 
wenzelm 
parents: 
34157 
diff
changeset
 | 
354  | 
def pos: InputPosition = new OffsetPosition(source, offset)  | 
| 
 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 
wenzelm 
parents: 
34157 
diff
changeset
 | 
355  | 
def atEnd: Boolean = !seq.isDefinedAt(offset)  | 
| 
 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 
wenzelm 
parents: 
34157 
diff
changeset
 | 
356  | 
override def drop(n: Int): Paged_Reader = new Paged_Reader(offset + n)  | 
| 
 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 
wenzelm 
parents: 
34157 
diff
changeset
 | 
357  | 
      def close { stream.close }
 | 
| 
 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 
wenzelm 
parents: 
34157 
diff
changeset
 | 
358  | 
}  | 
| 
 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 
wenzelm 
parents: 
34157 
diff
changeset
 | 
359  | 
new Paged_Reader(0)  | 
| 
 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 
wenzelm 
parents: 
34157 
diff
changeset
 | 
360  | 
}  | 
| 31648 | 361  | 
}  |