simplified/robustified treatment of malformed symbols, which are now fully internalized (total Symbol.explode etc.);
allow malformed symbols inside quoted material, comments etc. -- for improved user experience with incremental re-parsing;
refined treatment of malformed surrogates (Scala);
1 /* Title: Pure/General/scan.scala
4 Efficient scanning of keywords and tokens.
10 import scala.collection.generic.Addable
11 import scala.collection.IndexedSeq
12 import scala.collection.immutable.PagedSeq
13 import scala.util.parsing.input.{OffsetPosition, Position => InputPosition, Reader}
14 import scala.util.parsing.combinator.RegexParsers
16 import java.io.{File, InputStream, BufferedInputStream, FileInputStream}
21 /** Lexicon -- position tree **/
25 private case class Tree(val branches: Map[Char, (String, Tree)])
26 private val empty_tree = Tree(Map())
28 val empty: Lexicon = new Lexicon
29 def apply(elems: String*): Lexicon = empty ++ elems
32 class Lexicon extends Addable[String, Lexicon] with RegexParsers
37 protected val main_tree: Tree = Lexicon.empty_tree
40 /* auxiliary operations */
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) })
47 private def lookup(str: CharSequence): Option[(Boolean, Tree)] =
50 def look(tree: Tree, tip: Boolean, i: Int): Option[(Boolean, Tree)] =
53 tree.branches.get(str.charAt(i)) match {
54 case Some((s, tr)) => look(tr, !s.isEmpty, i + 1)
57 } else Some(tip, tree)
59 look(main_tree, false, 0)
62 def completions(str: CharSequence): List[String] =
64 case Some((true, tree)) => content(tree, List(str.toString))
65 case Some((false, tree)) => content(tree, Nil)
70 /* pseudo Set methods */
72 def iterator: Iterator[String] = content(main_tree, Nil).sortWith(_ <= _).iterator
74 override def toString: String = iterator.mkString("Lexicon(", ", ", ")")
76 def empty: Lexicon = Lexicon.empty
77 def isEmpty: Boolean = main_tree.branches.isEmpty
79 def contains(elem: String): Boolean =
81 case Some((tip, _)) => tip
90 def + (elem: String): Lexicon =
91 if (contains(elem)) this
94 def extend(tree: Tree, i: Int): Tree =
96 val c = elem.charAt(i)
97 val end = (i + 1 == len)
98 tree.branches.get(c) match {
101 (c -> (if (end) elem else s, extend(tr, i + 1))))
104 (c -> (if (end) elem else "", extend(Lexicon.empty_tree, i + 1))))
109 new Lexicon { override val main_tree = extend(old.main_tree, 0) }
114 /** RegexParsers methods **/
116 override val whiteSpace = "".r
119 /* keywords from lexicon */
121 def keyword: Parser[String] = new Parser[String]
123 def apply(in: Input) =
125 val source = in.source
126 val offset = in.offset
127 val len = source.length - offset
129 def scan(tree: Tree, result: String, i: Int): String =
132 tree.branches.get(source.charAt(offset + i)) match {
133 case Some((s, tr)) => scan(tr, if (s.isEmpty) result else s, i + 1)
139 val result = scan(main_tree, "", 0)
140 if (result.isEmpty) Failure("keyword expected", in)
141 else Success(result, in.drop(result.length))
148 def symbol_range(pred: String => Boolean, min_count: Int, max_count: Int): Parser[String] =
151 def apply(in: Input) =
153 val start = in.offset
154 val end = in.source.length
155 val matcher = new Symbol.Matcher(in.source)
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 }
169 if (count < min_count) Failure("bad input", in)
170 else Success(in.source.subSequence(start, i).toString, in.drop(i - start))
172 }.named("symbol_range")
174 def one(pred: String => Boolean): Parser[String] = symbol_range(pred, 1, 1)
175 def many(pred: String => Boolean): Parser[String] = symbol_range(pred, 0, Integer.MAX_VALUE)
176 def many1(pred: String => Boolean): Parser[String] = symbol_range(pred, 1, Integer.MAX_VALUE)
181 private def quoted(quote: String): Parser[String] =
184 rep(many1(sym => sym != quote && sym != "\\") | "\\" + quote | "\\\\" |
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 }
189 def quoted_content(quote: String, source: String): String =
191 require(parseAll(quoted(quote), source).successful)
192 val body = source.substring(1, source.length - 1)
193 if (body.exists(_ == '\\')) {
195 rep(many1(sym => sym != quote && sym != "\\") |
196 "\\" ~> (quote | "\\" | """\d\d\d""".r ^^ { case x => x.toInt.toChar.toString }))
197 parseAll(content ^^ (_.mkString), body).get
205 private def verbatim: Parser[String] =
207 "{*" ~ rep(many1(sym => sym != "*") | """\*(?!\})""".r) ~ "*}" ^^
208 { case x ~ ys ~ z => x + ys.mkString + z }
211 def verbatim_content(source: String): String =
213 require(parseAll(verbatim, source).successful)
214 source.substring(2, source.length - 2)
218 /* nested comments */
220 def comment: Parser[String] = new Parser[String]
223 rep(many1(sym => sym != "*" && sym != "(") | """\*(?!\))|\((?!\*)""".r)
224 val comment_open = "(*" ~ comment_text ^^^ ()
225 val comment_close = comment_text ~ "*)" ^^^ ()
227 def apply(in: Input) =
230 def try_parse(p: Parser[Unit]): Boolean =
232 parse(p, rest) match {
233 case Success(_, next) => { rest = next; true }
240 if (try_parse(comment_open)) depth += 1
241 else if (depth > 0 && try_parse(comment_close)) depth -= 1
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)
250 def comment_content(source: String): String =
252 require(parseAll(comment, source).successful)
253 source.substring(2, source.length - 2)
257 /* outer syntax tokens */
259 def token(symbols: Symbol.Interpretation, is_command: String => Boolean): Parser[Token] =
261 val id = one(symbols.is_letter) ~ many(symbols.is_letdig) ^^ { case x ~ y => x + y }
262 val nat = many1(symbols.is_digit)
263 val natdot = nat ~ "." ~ nat ^^ { case x ~ y ~ z => x + y + z }
264 val id_nat = id ~ opt("." ~ nat) ^^ { case x ~ Some(y ~ z) => x + y + z case x ~ None => x }
266 val ident = id ~ rep("." ~> id) ^^
267 { case x ~ Nil => Token(Token.Kind.IDENT, x)
268 case x ~ ys => Token(Token.Kind.LONG_IDENT, (x :: ys).mkString(".")) }
270 val var_ = "?" ~ id_nat ^^ { case x ~ y => Token(Token.Kind.VAR, x + y) }
271 val type_ident = "'" ~ id ^^ { case x ~ y => Token(Token.Kind.TYPE_IDENT, x + y) }
272 val type_var = "?'" ~ id_nat ^^ { case x ~ y => Token(Token.Kind.TYPE_VAR, x + y) }
273 val nat_ = nat ^^ (x => Token(Token.Kind.NAT, x))
275 ("-" ~ natdot ^^ { case x ~ y => x + y } | natdot) ^^ (x => Token(Token.Kind.FLOAT, x))
278 (many1(symbols.is_symbolic_char) | one(sym => symbols.is_symbolic(sym))) ^^
279 (x => Token(Token.Kind.SYM_IDENT, x))
281 val space = many1(symbols.is_blank) ^^ (x => Token(Token.Kind.SPACE, x))
283 val string = quoted("\"") ^^ (x => Token(Token.Kind.STRING, x))
284 val alt_string = quoted("`") ^^ (x => Token(Token.Kind.ALT_STRING, x))
286 val junk = many1(sym => !(symbols.is_blank(sym)))
288 ("\"" | "`" | "{*" | "(*") ~ junk ^^ { case x ~ y => Token(Token.Kind.UNPARSED, x + y) }
289 val bad = junk ^^ (x => Token(Token.Kind.UNPARSED, x))
294 (space | (string | (alt_string | (verbatim ^^ (x => Token(Token.Kind.VERBATIM, x)) |
295 comment ^^ (x => Token(Token.Kind.COMMENT, x)))))) |
297 ((ident | (var_ | (type_ident | (type_var | (float | (nat_ | sym_ident)))))) |||
298 keyword ^^ (x => Token(if (is_command(x)) Token.Kind.COMMAND else Token.Kind.KEYWORD, x))) |
305 /** read file without decoding -- enables efficient length operation **/
307 private class Restricted_Seq(seq: IndexedSeq[Char], start: Int, end: Int)
310 def charAt(i: Int): Char =
311 if (0 <= i && i < length) seq(start + i)
312 else throw new IndexOutOfBoundsException
314 def length: Int = end - start // avoid potentially expensive seq.length
316 def subSequence(i: Int, j: Int): CharSequence =
317 if (0 <= i && i <= j && j <= length) new Restricted_Seq(seq, start + i, start + j)
318 else throw new IndexOutOfBoundsException
320 override def toString: String =
322 val buf = new StringBuilder(length)
323 for (offset <- start until end) buf.append(seq(offset))
328 abstract class Byte_Reader extends Reader[Char] { def close: Unit }
330 def byte_reader(file: File): Byte_Reader =
332 val stream = new BufferedInputStream(new FileInputStream(file))
333 val seq = new PagedSeq(
334 (buf: Array[Char], offset: Int, length: Int) =>
339 while (!eof && i < length) {
341 if (c == -1) eof = true
342 else { buf(offset + i) = c.toChar; i += 1 }
346 val restricted_seq = new Restricted_Seq(seq, 0, file.length.toInt)
348 class Paged_Reader(override val offset: Int) extends Byte_Reader
350 override lazy val source: CharSequence = restricted_seq
351 def first: Char = if (seq.isDefinedAt(offset)) seq(offset) else '\032'
352 def rest: Paged_Reader = if (seq.isDefinedAt(offset)) new Paged_Reader(offset + 1) else this
353 def pos: InputPosition = new OffsetPosition(source, offset)
354 def atEnd: Boolean = !seq.isDefinedAt(offset)
355 override def drop(n: Int): Paged_Reader = new Paged_Reader(offset + n)
356 def close { stream.close }