| author | krauss | 
| Fri, 25 Mar 2011 22:17:32 +0100 | |
| changeset 42120 | b8f176348f44 | 
| parent 40523 | 1050315f6ee2 | 
| child 42441 | 781c622af16a | 
| permissions | -rw-r--r-- | 
| 31648 | 1 | /* Title: Pure/General/scan.scala | 
| 2 | Author: Makarius | |
| 3 | ||
| 40523 
1050315f6ee2
simplified/robustified treatment of malformed symbols, which are now fully internalized (total Symbol.explode etc.);
 wenzelm parents: 
40290diff
changeset | 4 | Efficient scanning of keywords and tokens. | 
| 31648 | 5 | */ | 
| 6 | ||
| 7 | package isabelle | |
| 8 | ||
| 34187 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 wenzelm parents: 
34157diff
changeset | 9 | |
| 36011 
3ff725ac13a4
adapted to Scala 2.8.0 Beta1 -- with notable changes to scala.collection;
 wenzelm parents: 
34316diff
changeset | 10 | import scala.collection.generic.Addable | 
| 36679 
ac021aed685e
use IndexedSeq instead of deprecated RandomAccessSeq, which is merely an alias;
 wenzelm parents: 
36012diff
changeset | 11 | import scala.collection.IndexedSeq | 
| 34187 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 wenzelm parents: 
34157diff
changeset | 12 | 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: 
34157diff
changeset | 13 | import scala.util.parsing.input.{OffsetPosition, Position => InputPosition, Reader}
 | 
| 31648 | 14 | import scala.util.parsing.combinator.RegexParsers | 
| 15 | ||
| 34187 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 wenzelm parents: 
34157diff
changeset | 16 | 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: 
34157diff
changeset | 17 | |
| 31648 | 18 | |
| 19 | object Scan | |
| 20 | {
 | |
| 31649 
a11ea667d676
reorganized and abstracted version, via Set trait;
 wenzelm parents: 
31648diff
changeset | 21 | /** Lexicon -- position tree **/ | 
| 31648 | 22 | |
| 31649 
a11ea667d676
reorganized and abstracted version, via Set trait;
 wenzelm parents: 
31648diff
changeset | 23 | object Lexicon | 
| 31648 | 24 |   {
 | 
| 31649 
a11ea667d676
reorganized and abstracted version, via Set trait;
 wenzelm parents: 
31648diff
changeset | 25 | private case class Tree(val branches: Map[Char, (String, Tree)]) | 
| 
a11ea667d676
reorganized and abstracted version, via Set trait;
 wenzelm parents: 
31648diff
changeset | 26 | private val empty_tree = Tree(Map()) | 
| 
a11ea667d676
reorganized and abstracted version, via Set trait;
 wenzelm parents: 
31648diff
changeset | 27 | |
| 
a11ea667d676
reorganized and abstracted version, via Set trait;
 wenzelm parents: 
31648diff
changeset | 28 | val empty: Lexicon = new Lexicon | 
| 31762 
20745ab5b79a
more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
 wenzelm parents: 
31753diff
changeset | 29 | def apply(elems: String*): Lexicon = empty ++ elems | 
| 31648 | 30 | } | 
| 31 | ||
| 36011 
3ff725ac13a4
adapted to Scala 2.8.0 Beta1 -- with notable changes to scala.collection;
 wenzelm parents: 
34316diff
changeset | 32 | class Lexicon extends Addable[String, Lexicon] with RegexParsers | 
| 31648 | 33 |   {
 | 
| 31649 
a11ea667d676
reorganized and abstracted version, via Set trait;
 wenzelm parents: 
31648diff
changeset | 34 | /* representation */ | 
| 31648 | 35 | |
| 31649 
a11ea667d676
reorganized and abstracted version, via Set trait;
 wenzelm parents: 
31648diff
changeset | 36 | import Lexicon.Tree | 
| 31764 | 37 | protected val main_tree: Tree = Lexicon.empty_tree | 
| 31648 | 38 | |
| 39 | ||
| 31650 | 40 | /* auxiliary operations */ | 
| 41 | ||
| 42 | private def content(tree: Tree, result: List[String]): List[String] = | |
| 43 | (result /: tree.branches.toList) ((res, entry) => | |
| 44 |         entry match { case (_, (s, tr)) =>
 | |
| 45 | if (s.isEmpty) content(tr, res) else content(tr, s :: res) }) | |
| 46 | ||
| 47 | private def lookup(str: CharSequence): Option[(Boolean, Tree)] = | |
| 48 |     {
 | |
| 49 | val len = str.length | |
| 50 | def look(tree: Tree, tip: Boolean, i: Int): Option[(Boolean, Tree)] = | |
| 51 |       {
 | |
| 52 |         if (i < len) {
 | |
| 53 |           tree.branches.get(str.charAt(i)) match {
 | |
| 54 | case Some((s, tr)) => look(tr, !s.isEmpty, i + 1) | |
| 55 | case None => None | |
| 56 | } | |
| 57 | } else Some(tip, tree) | |
| 58 | } | |
| 59 | look(main_tree, false, 0) | |
| 60 | } | |
| 61 | ||
| 62 | def completions(str: CharSequence): List[String] = | |
| 31764 | 63 |       lookup(str) match {
 | 
| 31650 | 64 | case Some((true, tree)) => content(tree, List(str.toString)) | 
| 65 | case Some((false, tree)) => content(tree, Nil) | |
| 66 | case None => Nil | |
| 31764 | 67 | } | 
| 31650 | 68 | |
| 69 | ||
| 36011 
3ff725ac13a4
adapted to Scala 2.8.0 Beta1 -- with notable changes to scala.collection;
 wenzelm parents: 
34316diff
changeset | 70 | /* pseudo Set methods */ | 
| 31649 
a11ea667d676
reorganized and abstracted version, via Set trait;
 wenzelm parents: 
31648diff
changeset | 71 | |
| 36012 | 72 | def iterator: Iterator[String] = content(main_tree, Nil).sortWith(_ <= _).iterator | 
| 31649 
a11ea667d676
reorganized and abstracted version, via Set trait;
 wenzelm parents: 
31648diff
changeset | 73 | |
| 36011 
3ff725ac13a4
adapted to Scala 2.8.0 Beta1 -- with notable changes to scala.collection;
 wenzelm parents: 
34316diff
changeset | 74 |     override def toString: String = iterator.mkString("Lexicon(", ", ", ")")
 | 
| 31649 
a11ea667d676
reorganized and abstracted version, via Set trait;
 wenzelm parents: 
31648diff
changeset | 75 | |
| 36011 
3ff725ac13a4
adapted to Scala 2.8.0 Beta1 -- with notable changes to scala.collection;
 wenzelm parents: 
34316diff
changeset | 76 | def empty: Lexicon = Lexicon.empty | 
| 
3ff725ac13a4
adapted to Scala 2.8.0 Beta1 -- with notable changes to scala.collection;
 wenzelm parents: 
34316diff
changeset | 77 | def isEmpty: Boolean = main_tree.branches.isEmpty | 
| 31648 | 78 | |
| 31762 
20745ab5b79a
more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
 wenzelm parents: 
31753diff
changeset | 79 | def contains(elem: String): Boolean = | 
| 
20745ab5b79a
more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
 wenzelm parents: 
31753diff
changeset | 80 |       lookup(elem) match {
 | 
| 31650 | 81 | case Some((tip, _)) => tip | 
| 82 | case _ => false | |
| 83 | } | |
| 31648 | 84 | |
| 36011 
3ff725ac13a4
adapted to Scala 2.8.0 Beta1 -- with notable changes to scala.collection;
 wenzelm parents: 
34316diff
changeset | 85 | |
| 
3ff725ac13a4
adapted to Scala 2.8.0 Beta1 -- with notable changes to scala.collection;
 wenzelm parents: 
34316diff
changeset | 86 | /* Addable methods */ | 
| 
3ff725ac13a4
adapted to Scala 2.8.0 Beta1 -- with notable changes to scala.collection;
 wenzelm parents: 
34316diff
changeset | 87 | |
| 
3ff725ac13a4
adapted to Scala 2.8.0 Beta1 -- with notable changes to scala.collection;
 wenzelm parents: 
34316diff
changeset | 88 | def repr = this | 
| 
3ff725ac13a4
adapted to Scala 2.8.0 Beta1 -- with notable changes to scala.collection;
 wenzelm parents: 
34316diff
changeset | 89 | |
| 31762 
20745ab5b79a
more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
 wenzelm parents: 
31753diff
changeset | 90 | def + (elem: String): Lexicon = | 
| 
20745ab5b79a
more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
 wenzelm parents: 
31753diff
changeset | 91 | if (contains(elem)) this | 
| 
20745ab5b79a
more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
 wenzelm parents: 
31753diff
changeset | 92 |       else {
 | 
| 
20745ab5b79a
more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
 wenzelm parents: 
31753diff
changeset | 93 | val len = elem.length | 
| 
20745ab5b79a
more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
 wenzelm parents: 
31753diff
changeset | 94 | def extend(tree: Tree, i: Int): Tree = | 
| 
20745ab5b79a
more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
 wenzelm parents: 
31753diff
changeset | 95 |           if (i < len) {
 | 
| 
20745ab5b79a
more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
 wenzelm parents: 
31753diff
changeset | 96 | val c = elem.charAt(i) | 
| 
20745ab5b79a
more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
 wenzelm parents: 
31753diff
changeset | 97 | val end = (i + 1 == len) | 
| 
20745ab5b79a
more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
 wenzelm parents: 
31753diff
changeset | 98 |             tree.branches.get(c) match {
 | 
| 
20745ab5b79a
more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
 wenzelm parents: 
31753diff
changeset | 99 | case Some((s, tr)) => | 
| 
20745ab5b79a
more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
 wenzelm parents: 
31753diff
changeset | 100 | Tree(tree.branches + | 
| 
20745ab5b79a
more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
 wenzelm parents: 
31753diff
changeset | 101 | (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: 
31753diff
changeset | 102 | case None => | 
| 
20745ab5b79a
more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
 wenzelm parents: 
31753diff
changeset | 103 | Tree(tree.branches + | 
| 
20745ab5b79a
more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
 wenzelm parents: 
31753diff
changeset | 104 | (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: 
31753diff
changeset | 105 | } | 
| 34135 | 106 | } | 
| 107 | else tree | |
| 31762 
20745ab5b79a
more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
 wenzelm parents: 
31753diff
changeset | 108 | val old = this | 
| 
20745ab5b79a
more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
 wenzelm parents: 
31753diff
changeset | 109 |         new Lexicon { override val main_tree = extend(old.main_tree, 0) }
 | 
| 31648 | 110 | } | 
| 31762 
20745ab5b79a
more precise implementation of trait methods -- oddly this seems to require copy/paste for +, ++;
 wenzelm parents: 
31753diff
changeset | 111 | |
| 31649 
a11ea667d676
reorganized and abstracted version, via Set trait;
 wenzelm parents: 
31648diff
changeset | 112 | |
| 34301 | 113 | |
| 34137 | 114 | /** RegexParsers methods **/ | 
| 31649 
a11ea667d676
reorganized and abstracted version, via Set trait;
 wenzelm parents: 
31648diff
changeset | 115 | |
| 
a11ea667d676
reorganized and abstracted version, via Set trait;
 wenzelm parents: 
31648diff
changeset | 116 | override val whiteSpace = "".r | 
| 
a11ea667d676
reorganized and abstracted version, via Set trait;
 wenzelm parents: 
31648diff
changeset | 117 | |
| 34137 | 118 | |
| 119 | /* keywords from lexicon */ | |
| 120 | ||
| 34140 | 121 | def keyword: Parser[String] = new Parser[String] | 
| 34135 | 122 |     {
 | 
| 31649 
a11ea667d676
reorganized and abstracted version, via Set trait;
 wenzelm parents: 
31648diff
changeset | 123 | def apply(in: Input) = | 
| 
a11ea667d676
reorganized and abstracted version, via Set trait;
 wenzelm parents: 
31648diff
changeset | 124 |       {
 | 
| 
a11ea667d676
reorganized and abstracted version, via Set trait;
 wenzelm parents: 
31648diff
changeset | 125 | val source = in.source | 
| 
a11ea667d676
reorganized and abstracted version, via Set trait;
 wenzelm parents: 
31648diff
changeset | 126 | val offset = in.offset | 
| 
a11ea667d676
reorganized and abstracted version, via Set trait;
 wenzelm parents: 
31648diff
changeset | 127 | val len = source.length - offset | 
| 
a11ea667d676
reorganized and abstracted version, via Set trait;
 wenzelm parents: 
31648diff
changeset | 128 | |
| 34140 | 129 | def scan(tree: Tree, result: String, i: Int): String = | 
| 31649 
a11ea667d676
reorganized and abstracted version, via Set trait;
 wenzelm parents: 
31648diff
changeset | 130 |         {
 | 
| 
a11ea667d676
reorganized and abstracted version, via Set trait;
 wenzelm parents: 
31648diff
changeset | 131 |           if (i < len) {
 | 
| 
a11ea667d676
reorganized and abstracted version, via Set trait;
 wenzelm parents: 
31648diff
changeset | 132 |             tree.branches.get(source.charAt(offset + i)) match {
 | 
| 34140 | 133 | case Some((s, tr)) => scan(tr, if (s.isEmpty) result else s, i + 1) | 
| 34135 | 134 | case None => result | 
| 31649 
a11ea667d676
reorganized and abstracted version, via Set trait;
 wenzelm parents: 
31648diff
changeset | 135 | } | 
| 34135 | 136 | } | 
| 137 | else result | |
| 31649 
a11ea667d676
reorganized and abstracted version, via Set trait;
 wenzelm parents: 
31648diff
changeset | 138 | } | 
| 34140 | 139 | val result = scan(main_tree, "", 0) | 
| 140 |         if (result.isEmpty) Failure("keyword expected", in)
 | |
| 141 | else Success(result, in.drop(result.length)) | |
| 31648 | 142 | } | 
| 31649 
a11ea667d676
reorganized and abstracted version, via Set trait;
 wenzelm parents: 
31648diff
changeset | 143 |     }.named("keyword")
 | 
| 31648 | 144 | |
| 34137 | 145 | |
| 34157 
0a0a19153626
explicit representation of Token_Kind -- cannot really depend on runtime types due to erasure;
 wenzelm parents: 
34144diff
changeset | 146 | /* symbol range */ | 
| 34137 | 147 | |
| 34157 
0a0a19153626
explicit representation of Token_Kind -- cannot really depend on runtime types due to erasure;
 wenzelm parents: 
34144diff
changeset | 148 | def symbol_range(pred: String => Boolean, min_count: Int, max_count: Int): Parser[String] = | 
| 34140 | 149 | new Parser[String] | 
| 34135 | 150 |       {
 | 
| 34137 | 151 | def apply(in: Input) = | 
| 152 |         {
 | |
| 153 | val start = in.offset | |
| 154 | val end = in.source.length | |
| 155 | val matcher = new Symbol.Matcher(in.source) | |
| 156 | ||
| 157 | var i = start | |
| 158 | var count = 0 | |
| 159 | var finished = false | |
| 160 |           while (!finished) {
 | |
| 161 |             if (i < end && count < max_count) {
 | |
| 162 | val n = matcher(i, end) | |
| 163 | val sym = in.source.subSequence(i, i + n).toString | |
| 164 |               if (pred(sym)) { i += n; count += 1 }
 | |
| 165 | else finished = true | |
| 166 | } | |
| 167 | else finished = true | |
| 168 | } | |
| 34140 | 169 |           if (count < min_count) Failure("bad input", in)
 | 
| 170 | else Success(in.source.subSequence(start, i).toString, in.drop(i - start)) | |
| 34137 | 171 | } | 
| 34157 
0a0a19153626
explicit representation of Token_Kind -- cannot really depend on runtime types due to erasure;
 wenzelm parents: 
34144diff
changeset | 172 |       }.named("symbol_range")
 | 
| 34137 | 173 | |
| 34157 
0a0a19153626
explicit representation of Token_Kind -- cannot really depend on runtime types due to erasure;
 wenzelm parents: 
34144diff
changeset | 174 | 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: 
34144diff
changeset | 175 | 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: 
34144diff
changeset | 176 | def many1(pred: String => Boolean): Parser[String] = symbol_range(pred, 1, Integer.MAX_VALUE) | 
| 34140 | 177 | |
| 178 | ||
| 34143 | 179 | /* quoted strings */ | 
| 34140 | 180 | |
| 34143 | 181 | private def quoted(quote: String): Parser[String] = | 
| 182 |     {
 | |
| 34140 | 183 | quote ~ | 
| 40523 
1050315f6ee2
simplified/robustified treatment of malformed symbols, which are now fully internalized (total Symbol.explode etc.);
 wenzelm parents: 
40290diff
changeset | 184 | rep(many1(sym => sym != quote && sym != "\\") | "\\" + quote | "\\\\" | | 
| 34140 | 185 |           (("""\\\d\d\d""".r) ^? { case x if x.substring(1, 4).toInt <= 255 => x })) ~
 | 
| 186 |       quote ^^ { case x ~ ys ~ z => x + ys.mkString + z }
 | |
| 34143 | 187 |     }.named("quoted")
 | 
| 34140 | 188 | |
| 189 | def quoted_content(quote: String, source: String): String = | |
| 190 |     {
 | |
| 191 | require(parseAll(quoted(quote), source).successful) | |
| 34189 | 192 | val body = source.substring(1, source.length - 1) | 
| 193 |       if (body.exists(_ == '\\')) {
 | |
| 194 | val content = | |
| 40523 
1050315f6ee2
simplified/robustified treatment of malformed symbols, which are now fully internalized (total Symbol.explode etc.);
 wenzelm parents: 
40290diff
changeset | 195 | rep(many1(sym => sym != quote && sym != "\\") | | 
| 34189 | 196 |               "\\" ~> (quote | "\\" | """\d\d\d""".r ^^ { case x => x.toInt.toChar.toString }))
 | 
| 197 | parseAll(content ^^ (_.mkString), body).get | |
| 198 | } | |
| 199 | else body | |
| 34140 | 200 | } | 
| 201 | ||
| 202 | ||
| 34143 | 203 | /* verbatim text */ | 
| 204 | ||
| 205 | private def verbatim: Parser[String] = | |
| 206 |     {
 | |
| 40523 
1050315f6ee2
simplified/robustified treatment of malformed symbols, which are now fully internalized (total Symbol.explode etc.);
 wenzelm parents: 
40290diff
changeset | 207 |       "{*" ~ rep(many1(sym => sym != "*") | """\*(?!\})""".r) ~ "*}" ^^
 | 
| 34140 | 208 |         { case x ~ ys ~ z => x + ys.mkString + z }
 | 
| 34143 | 209 |     }.named("verbatim")
 | 
| 34140 | 210 | |
| 211 | def verbatim_content(source: String): String = | |
| 212 |     {
 | |
| 213 | require(parseAll(verbatim, source).successful) | |
| 214 | source.substring(2, source.length - 2) | |
| 215 | } | |
| 216 | ||
| 217 | ||
| 34143 | 218 | /* nested comments */ | 
| 219 | ||
| 220 | def comment: Parser[String] = new Parser[String] | |
| 221 |     {
 | |
| 222 | val comment_text = | |
| 40523 
1050315f6ee2
simplified/robustified treatment of malformed symbols, which are now fully internalized (total Symbol.explode etc.);
 wenzelm parents: 
40290diff
changeset | 223 |         rep(many1(sym => sym != "*" && sym != "(") | """\*(?!\))|\((?!\*)""".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 | ||
| 36956 
21be4832c362
renamed class Outer_Lex to Token and Token_Kind to Token.Kind;
 wenzelm parents: 
36679diff
changeset | 259 | def token(symbols: Symbol.Interpretation, is_command: String => Boolean): Parser[Token] = | 
| 34140 | 260 |     {
 | 
| 261 |       val id = one(symbols.is_letter) ~ many(symbols.is_letdig) ^^ { case x ~ y => x + y }
 | |
| 262 | val nat = many1(symbols.is_digit) | |
| 40290 
47f572aff50a
support for floating-point tokens in outer syntax (coinciding with inner syntax version);
 wenzelm parents: 
38367diff
changeset | 263 |       val natdot = nat ~ "." ~ nat ^^ { case x ~ y ~ z => x + y + z }
 | 
| 34140 | 264 |       val id_nat = id ~ opt("." ~ nat) ^^ { case x ~ Some(y ~ z) => x + y + z case x ~ None => x }
 | 
| 265 | ||
| 266 |       val ident = id ~ rep("." ~> id) ^^
 | |
| 36956 
21be4832c362
renamed class Outer_Lex to Token and Token_Kind to Token.Kind;
 wenzelm parents: 
36679diff
changeset | 267 |         { case x ~ Nil => Token(Token.Kind.IDENT, x)
 | 
| 
21be4832c362
renamed class Outer_Lex to Token and Token_Kind to Token.Kind;
 wenzelm parents: 
36679diff
changeset | 268 |           case x ~ ys => Token(Token.Kind.LONG_IDENT, (x :: ys).mkString(".")) }
 | 
| 34140 | 269 | |
| 36956 
21be4832c362
renamed class Outer_Lex to Token and Token_Kind to Token.Kind;
 wenzelm parents: 
36679diff
changeset | 270 |       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: 
36679diff
changeset | 271 |       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: 
36679diff
changeset | 272 |       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: 
36679diff
changeset | 273 | 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: 
38367diff
changeset | 274 | val float = | 
| 
47f572aff50a
support for floating-point tokens in outer syntax (coinciding with inner syntax version);
 wenzelm parents: 
38367diff
changeset | 275 |         ("-" ~ natdot ^^ { case x ~ y => x + y } | natdot) ^^ (x => Token(Token.Kind.FLOAT, x))
 | 
| 34140 | 276 | |
| 277 | val sym_ident = | |
| 40523 
1050315f6ee2
simplified/robustified treatment of malformed symbols, which are now fully internalized (total Symbol.explode etc.);
 wenzelm parents: 
40290diff
changeset | 278 | (many1(symbols.is_symbolic_char) | one(sym => symbols.is_symbolic(sym))) ^^ | 
| 36956 
21be4832c362
renamed class Outer_Lex to Token and Token_Kind to Token.Kind;
 wenzelm parents: 
36679diff
changeset | 279 | (x => Token(Token.Kind.SYM_IDENT, x)) | 
| 34140 | 280 | |
| 36956 
21be4832c362
renamed class Outer_Lex to Token and Token_Kind to Token.Kind;
 wenzelm parents: 
36679diff
changeset | 281 | val space = many1(symbols.is_blank) ^^ (x => Token(Token.Kind.SPACE, x)) | 
| 34140 | 282 | |
| 36956 
21be4832c362
renamed class Outer_Lex to Token and Token_Kind to Token.Kind;
 wenzelm parents: 
36679diff
changeset | 283 |       val string = quoted("\"") ^^ (x => Token(Token.Kind.STRING, x))
 | 
| 
21be4832c362
renamed class Outer_Lex to Token and Token_Kind to Token.Kind;
 wenzelm parents: 
36679diff
changeset | 284 |       val alt_string = quoted("`") ^^ (x => Token(Token.Kind.ALT_STRING, x))
 | 
| 34140 | 285 | |
| 34267 | 286 | val junk = many1(sym => !(symbols.is_blank(sym))) | 
| 287 | val bad_delimiter = | |
| 38367 | 288 |         ("\"" | "`" | "{*" | "(*") ~ junk ^^ { case x ~ y => Token(Token.Kind.UNPARSED, x + y) }
 | 
| 289 | val bad = junk ^^ (x => Token(Token.Kind.UNPARSED, x)) | |
| 34140 | 290 | |
| 291 | ||
| 292 | /* tokens */ | |
| 293 | ||
| 36956 
21be4832c362
renamed class Outer_Lex to Token and Token_Kind to Token.Kind;
 wenzelm parents: 
36679diff
changeset | 294 | (space | (string | (alt_string | (verbatim ^^ (x => Token(Token.Kind.VERBATIM, x)) | | 
| 
21be4832c362
renamed class Outer_Lex to Token and Token_Kind to Token.Kind;
 wenzelm parents: 
36679diff
changeset | 295 | comment ^^ (x => Token(Token.Kind.COMMENT, x)))))) | | 
| 34267 | 296 | bad_delimiter | | 
| 40290 
47f572aff50a
support for floating-point tokens in outer syntax (coinciding with inner syntax version);
 wenzelm parents: 
38367diff
changeset | 297 | ((ident | (var_ | (type_ident | (type_var | (float | (nat_ | sym_ident)))))) ||| | 
| 36956 
21be4832c362
renamed class Outer_Lex to Token and Token_Kind to Token.Kind;
 wenzelm parents: 
36679diff
changeset | 298 | keyword ^^ (x => Token(if (is_command(x)) Token.Kind.COMMAND else Token.Kind.KEYWORD, x))) | | 
| 34267 | 299 | bad | 
| 34140 | 300 | } | 
| 31649 
a11ea667d676
reorganized and abstracted version, via Set trait;
 wenzelm parents: 
31648diff
changeset | 301 | } | 
| 34187 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 wenzelm parents: 
34157diff
changeset | 302 | |
| 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 wenzelm parents: 
34157diff
changeset | 303 | |
| 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 wenzelm parents: 
34157diff
changeset | 304 | |
| 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 wenzelm parents: 
34157diff
changeset | 305 | /** 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: 
34157diff
changeset | 306 | |
| 36679 
ac021aed685e
use IndexedSeq instead of deprecated RandomAccessSeq, which is merely an alias;
 wenzelm parents: 
36012diff
changeset | 307 | private class Restricted_Seq(seq: IndexedSeq[Char], start: Int, end: Int) | 
| 34187 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 wenzelm parents: 
34157diff
changeset | 308 | extends CharSequence | 
| 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 wenzelm parents: 
34157diff
changeset | 309 |   {
 | 
| 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 wenzelm parents: 
34157diff
changeset | 310 | 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: 
34157diff
changeset | 311 | 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: 
34157diff
changeset | 312 | 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: 
34157diff
changeset | 313 | |
| 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 wenzelm parents: 
34157diff
changeset | 314 | 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: 
34157diff
changeset | 315 | |
| 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 wenzelm parents: 
34157diff
changeset | 316 | 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: 
34157diff
changeset | 317 | 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: 
34157diff
changeset | 318 | 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: 
34157diff
changeset | 319 | |
| 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 wenzelm parents: 
34157diff
changeset | 320 | 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: 
34157diff
changeset | 321 |     {
 | 
| 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 wenzelm parents: 
34157diff
changeset | 322 | 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: 
34157diff
changeset | 323 | 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: 
34157diff
changeset | 324 | buf.toString | 
| 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 wenzelm parents: 
34157diff
changeset | 325 | } | 
| 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 wenzelm parents: 
34157diff
changeset | 326 | } | 
| 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 wenzelm parents: 
34157diff
changeset | 327 | |
| 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 wenzelm parents: 
34157diff
changeset | 328 |   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: 
34157diff
changeset | 329 | |
| 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 wenzelm parents: 
34157diff
changeset | 330 | 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: 
34157diff
changeset | 331 |   {
 | 
| 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 wenzelm parents: 
34157diff
changeset | 332 | 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: 
34157diff
changeset | 333 | 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: 
34157diff
changeset | 334 | (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: 
34157diff
changeset | 335 |         {
 | 
| 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 wenzelm parents: 
34157diff
changeset | 336 | var i = 0 | 
| 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 wenzelm parents: 
34157diff
changeset | 337 | var c = 0 | 
| 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 wenzelm parents: 
34157diff
changeset | 338 | var eof = false | 
| 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 wenzelm parents: 
34157diff
changeset | 339 |           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: 
34157diff
changeset | 340 | c = stream.read | 
| 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 wenzelm parents: 
34157diff
changeset | 341 | 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: 
34157diff
changeset | 342 |             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: 
34157diff
changeset | 343 | } | 
| 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 wenzelm parents: 
34157diff
changeset | 344 | 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: 
34157diff
changeset | 345 | }) | 
| 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 wenzelm parents: 
34157diff
changeset | 346 | 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: 
34157diff
changeset | 347 | |
| 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 wenzelm parents: 
34157diff
changeset | 348 | 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: 
34157diff
changeset | 349 |     {
 | 
| 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 wenzelm parents: 
34157diff
changeset | 350 | 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: 
34157diff
changeset | 351 | 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: 
34157diff
changeset | 352 | 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: 
34157diff
changeset | 353 | 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: 
34157diff
changeset | 354 | 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: 
34157diff
changeset | 355 | 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: 
34157diff
changeset | 356 |       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: 
34157diff
changeset | 357 | } | 
| 
7b659c1561f1
added byte_reader, which works without decoding and enables efficient length operation (for scala.util.parsing.input.Reader);
 wenzelm parents: 
34157diff
changeset | 358 | 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: 
34157diff
changeset | 359 | } | 
| 31648 | 360 | } |