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 /** context of partial scans **/
23 sealed abstract class Context
24 case object Finished extends Context
25 case class Quoted(quote: String) extends Context
26 case object Verbatim extends Context
27 case class Comment(depth: Int) extends Context
31 /** Lexicon -- position tree **/
37 sealed case class Tree(val branches: Map[Char, (String, Tree)])
38 private val empty_tree = Tree(Map())
40 val empty: Lexicon = new Lexicon(empty_tree)
41 def apply(elems: String*): Lexicon = empty ++ elems
44 class Lexicon private(private val main_tree: Lexicon.Tree)
45 extends Addable[String, Lexicon] with RegexParsers
50 /* auxiliary operations */
52 private def content(tree: Tree, result: List[String]): List[String] =
53 (result /: tree.branches.toList) ((res, entry) =>
54 entry match { case (_, (s, tr)) =>
55 if (s.isEmpty) content(tr, res) else content(tr, s :: res) })
57 private def lookup(str: CharSequence): Option[(Boolean, Tree)] =
60 def look(tree: Tree, tip: Boolean, i: Int): Option[(Boolean, Tree)] =
63 tree.branches.get(str.charAt(i)) match {
64 case Some((s, tr)) => look(tr, !s.isEmpty, i + 1)
67 } else Some(tip, tree)
69 look(main_tree, false, 0)
72 def completions(str: CharSequence): List[String] =
74 case Some((true, tree)) => content(tree, List(str.toString))
75 case Some((false, tree)) => content(tree, Nil)
80 /* pseudo Set methods */
82 def iterator: Iterator[String] = content(main_tree, Nil).sorted.iterator
84 override def toString: String = iterator.mkString("Lexicon(", ", ", ")")
86 def empty: Lexicon = Lexicon.empty
87 def isEmpty: Boolean = main_tree.branches.isEmpty
89 def contains(elem: String): Boolean =
91 case Some((tip, _)) => tip
100 def + (elem: String): Lexicon =
101 if (contains(elem)) this
103 val len = elem.length
104 def extend(tree: Tree, i: Int): Tree =
106 val c = elem.charAt(i)
107 val end = (i + 1 == len)
108 tree.branches.get(c) match {
109 case Some((s, tr)) =>
111 (c -> (if (end) elem else s, extend(tr, i + 1))))
114 (c -> (if (end) elem else "", extend(Lexicon.empty_tree, i + 1))))
119 new Lexicon(extend(old.main_tree, 0))
124 /** RegexParsers methods **/
126 override val whiteSpace = "".r
129 /* optional termination */
131 def opt_term[T](p: => Parser[T]): Parser[Option[T]] =
132 p ^^ (x => Some(x)) | """\z""".r ^^ (_ => None)
135 /* keywords from lexicon */
137 def keyword: Parser[String] = new Parser[String]
139 def apply(in: Input) =
141 val source = in.source
142 val offset = in.offset
143 val len = source.length - offset
145 def scan(tree: Tree, result: String, i: Int): String =
148 tree.branches.get(source.charAt(offset + i)) match {
149 case Some((s, tr)) => scan(tr, if (s.isEmpty) result else s, i + 1)
155 val result = scan(main_tree, "", 0)
156 if (result.isEmpty) Failure("keyword expected", in)
157 else Success(result, in.drop(result.length))
164 def symbol_range(pred: Symbol.Symbol => Boolean, min_count: Int, max_count: Int): Parser[String] =
167 def apply(in: Input) =
169 val start = in.offset
170 val end = in.source.length
171 val matcher = new Symbol.Matcher(in.source)
177 if (i < end && count < max_count) {
178 val n = matcher(i, end)
179 val sym = in.source.subSequence(i, i + n).toString
180 if (pred(sym)) { i += n; count += 1 }
185 if (count < min_count) Failure("bad input", in)
186 else Success(in.source.subSequence(start, i).toString, in.drop(i - start))
188 }.named("symbol_range")
190 def one(pred: Symbol.Symbol => Boolean): Parser[String] =
191 symbol_range(pred, 1, 1)
193 def many(pred: Symbol.Symbol => Boolean): Parser[String] =
194 symbol_range(pred, 0, Integer.MAX_VALUE)
196 def many1(pred: Symbol.Symbol => Boolean): Parser[String] =
197 symbol_range(pred, 1, Integer.MAX_VALUE)
202 private def quoted_body(quote: Symbol.Symbol): Parser[String] =
204 rep(many1(sym => sym != quote && sym != "\\") | "\\" + quote | "\\\\" |
205 (("""\\\d\d\d""".r) ^? { case x if x.substring(1, 4).toInt <= 255 => x })) ^^ (_.mkString)
208 private def quoted(quote: Symbol.Symbol): Parser[String] =
210 quote ~ quoted_body(quote) ~ quote ^^ { case x ~ y ~ z => x + y + z }
213 def quoted_content(quote: Symbol.Symbol, source: String): String =
215 require(parseAll(quoted(quote), source).successful)
216 val body = source.substring(1, source.length - 1)
217 if (body.exists(_ == '\\')) {
219 rep(many1(sym => sym != quote && sym != "\\") |
220 "\\" ~> (quote | "\\" | """\d\d\d""".r ^^ { case x => x.toInt.toChar.toString }))
221 parseAll(content ^^ (_.mkString), body).get
226 def quoted_context(quote: Symbol.Symbol, ctxt: Context): Parser[(String, Context)] =
230 quote ~ quoted_body(quote) ~ opt_term(quote) ^^
231 { case x ~ y ~ Some(z) => (x + y + z, Finished)
232 case x ~ y ~ None => (x + y, Quoted(quote)) }
233 case Quoted(q) if q == quote =>
234 quoted_body(quote) ~ opt_term(quote) ^^
235 { case x ~ Some(y) => (x + y, Finished)
236 case x ~ None => (x, ctxt) }
237 case _ => failure("")
239 }.named("quoted_context")
244 private def verbatim_body: Parser[String] =
245 rep(many1(sym => sym != "*") | """\*(?!\})""".r) ^^ (_.mkString)
247 private def verbatim: Parser[String] =
249 "{*" ~ verbatim_body ~ "*}" ^^ { case x ~ y ~ z => x + y + z }
252 def verbatim_content(source: String): String =
254 require(parseAll(verbatim, source).successful)
255 source.substring(2, source.length - 2)
258 def verbatim_context(ctxt: Context): Parser[(String, Context)] =
262 "{*" ~ verbatim_body ~ opt_term("*}") ^^
263 { case x ~ y ~ Some(z) => (x + y + z, Finished)
264 case x ~ y ~ None => (x + y, Verbatim) }
266 verbatim_body ~ opt_term("*}") ^^
267 { case x ~ Some(y) => (x + y, Finished)
268 case x ~ None => (x, Verbatim) }
269 case _ => failure("")
271 }.named("verbatim_context")
274 /* nested comments */
276 private def comment_depth(depth: Int): Parser[(String, Int)] = new Parser[(String, Int)]
281 rep1(many1(sym => sym != "*" && sym != "(") | """\*(?!\))|\((?!\*)""".r)
283 def apply(in: Input) =
286 def try_parse[A](p: Parser[A]): Boolean =
288 parse(p ^^^ (), rest) match {
289 case Success(_, next) => { rest = next; true }
296 if (try_parse("(*")) d += 1
297 else if (d > 0 && try_parse("*)")) d -= 1
298 else if (d == 0 || !try_parse(comment_text)) finished = true
300 if (in.offset < rest.offset)
301 Success((in.source.subSequence(in.offset, rest.offset).toString, d), rest)
302 else Failure("comment expected", in)
304 }.named("comment_depth")
306 def comment: Parser[String] =
307 comment_depth(0) ^? { case (x, d) if d == 0 => x }
309 def comment_context(ctxt: Context): Parser[(String, Context)] =
318 comment_depth(depth) ^^
319 { case (x, 0) => (x, Finished)
320 case (x, d) => (x, Comment(d)) }
324 def comment_content(source: String): String =
326 require(parseAll(comment, source).successful)
327 source.substring(2, source.length - 2)
331 /* outer syntax tokens */
333 private def delimited_token: Parser[Token] =
335 val string = quoted("\"") ^^ (x => Token(Token.Kind.STRING, x))
336 val alt_string = quoted("`") ^^ (x => Token(Token.Kind.ALT_STRING, x))
337 val verb = verbatim ^^ (x => Token(Token.Kind.VERBATIM, x))
338 val cmt = comment ^^ (x => Token(Token.Kind.COMMENT, x))
340 string | (alt_string | (verb | cmt))
343 private def other_token(is_command: String => Boolean)
346 val id = one(Symbol.is_letter) ~ many(Symbol.is_letdig) ^^ { case x ~ y => x + y }
347 val nat = many1(Symbol.is_digit)
348 val natdot = nat ~ "." ~ nat ^^ { case x ~ y ~ z => x + y + z }
349 val id_nat = id ~ opt("." ~ nat) ^^ { case x ~ Some(y ~ z) => x + y + z case x ~ None => x }
351 val ident = id ~ rep("." ~> id) ^^
352 { case x ~ Nil => Token(Token.Kind.IDENT, x)
353 case x ~ ys => Token(Token.Kind.LONG_IDENT, (x :: ys).mkString(".")) }
355 val var_ = "?" ~ id_nat ^^ { case x ~ y => Token(Token.Kind.VAR, x + y) }
356 val type_ident = "'" ~ id ^^ { case x ~ y => Token(Token.Kind.TYPE_IDENT, x + y) }
357 val type_var = "?'" ~ id_nat ^^ { case x ~ y => Token(Token.Kind.TYPE_VAR, x + y) }
358 val nat_ = nat ^^ (x => Token(Token.Kind.NAT, x))
360 ("-" ~ natdot ^^ { case x ~ y => x + y } | natdot) ^^ (x => Token(Token.Kind.FLOAT, x))
363 (many1(Symbol.is_symbolic_char) | one(sym => Symbol.is_symbolic(sym))) ^^
364 (x => Token(Token.Kind.SYM_IDENT, x))
366 val space = many1(Symbol.is_blank) ^^ (x => Token(Token.Kind.SPACE, x))
369 val junk = many(sym => !(Symbol.is_blank(sym)))
370 val junk1 = many1(sym => !(Symbol.is_blank(sym)))
373 ("\"" | "`" | "{*" | "(*") ~ junk ^^ { case x ~ y => Token(Token.Kind.UNPARSED, x + y) }
374 val bad = junk1 ^^ (x => Token(Token.Kind.UNPARSED, x))
376 val command_keyword =
377 keyword ^^ (x => Token(if (is_command(x)) Token.Kind.COMMAND else Token.Kind.KEYWORD, x))
379 space | (bad_delimiter |
380 (((ident | (var_ | (type_ident | (type_var | (float | (nat_ | sym_ident)))))) |||
381 command_keyword) | bad))
384 def token(is_command: String => Boolean): Parser[Token] =
385 delimited_token | other_token(is_command)
387 def token_context(is_command: String => Boolean, ctxt: Context): Parser[(Token, Context)] =
390 quoted_context("\"", ctxt) ^^ { case (x, c) => (Token(Token.Kind.STRING, x), c) }
392 quoted_context("`", ctxt) ^^ { case (x, c) => (Token(Token.Kind.ALT_STRING, x), c) }
393 val verb = verbatim_context(ctxt) ^^ { case (x, c) => (Token(Token.Kind.VERBATIM, x), c) }
394 val cmt = comment_context(ctxt) ^^ { case (x, c) => (Token(Token.Kind.COMMENT, x), c) }
395 val other = other_token(is_command) ^^ { case x => (x, Finished) }
397 string | (alt_string | (verb | (cmt | other)))
403 /** read file without decoding -- enables efficient length operation **/
405 private class Restricted_Seq(seq: IndexedSeq[Char], start: Int, end: Int)
408 def charAt(i: Int): Char =
409 if (0 <= i && i < length) seq(start + i)
410 else throw new IndexOutOfBoundsException
412 def length: Int = end - start // avoid potentially expensive seq.length
414 def subSequence(i: Int, j: Int): CharSequence =
415 if (0 <= i && i <= j && j <= length) new Restricted_Seq(seq, start + i, start + j)
416 else throw new IndexOutOfBoundsException
418 override def toString: String =
420 val buf = new StringBuilder(length)
421 for (offset <- start until end) buf.append(seq(offset))
426 abstract class Byte_Reader extends Reader[Char] { def close: Unit }
428 def byte_reader(file: File): Byte_Reader =
430 val stream = new BufferedInputStream(new FileInputStream(file))
431 val seq = new PagedSeq(
432 (buf: Array[Char], offset: Int, length: Int) =>
437 while (!eof && i < length) {
439 if (c == -1) eof = true
440 else { buf(offset + i) = c.toChar; i += 1 }
444 val restricted_seq = new Restricted_Seq(seq, 0, file.length.toInt)
446 class Paged_Reader(override val offset: Int) extends Byte_Reader
448 override lazy val source: CharSequence = restricted_seq
449 def first: Char = if (seq.isDefinedAt(offset)) seq(offset) else '\032'
450 def rest: Paged_Reader = if (seq.isDefinedAt(offset)) new Paged_Reader(offset + 1) else this
451 def pos: InputPosition = new OffsetPosition(source, offset)
452 def atEnd: Boolean = !seq.isDefinedAt(offset)
453 override def drop(n: Int): Paged_Reader = new Paged_Reader(offset + n)
454 def close { stream.close }