From 8e8beb0cdc6c929341c94d742f8fc0e53352f0bb Mon Sep 17 00:00:00 2001 From: Paul Phillips Date: Sun, 7 Jun 2009 14:40:56 +0000 Subject: Fix for #1993. --- .../parsing/combinator/lexical/StdLexical.scala | 26 +++++++++------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/src/library/scala/util/parsing/combinator/lexical/StdLexical.scala b/src/library/scala/util/parsing/combinator/lexical/StdLexical.scala index 2489c623e9..9cacf37ca2 100644 --- a/src/library/scala/util/parsing/combinator/lexical/StdLexical.scala +++ b/src/library/scala/util/parsing/combinator/lexical/StdLexical.scala @@ -71,23 +71,19 @@ class StdLexical extends Lexical with StdTokens { protected def processIdent(name: String) = if (reserved contains name) Keyword(name) else Identifier(name) - private var _delim: Parser[Token] = null - protected def delim: Parser[Token] = { - if (_delim eq null) { // construct parser for delimiters by |'ing together the parsers for the individual delimiters, - // starting with the longest one (hence the sort + reverse) -- otherwise a delimiter D will never be matched if - // there is another delimiter that is a prefix of D - def parseDelim(s: String): Parser[Token] = accept(s.toList) ^^ { x => Keyword(s) } - - val d = new Array[String](delimiters.size) - delimiters.copyToArray(d,0) - scala.util.Sorting.quickSort(d) - _delim = d.toList.reverse.map(parseDelim).reduceRight[Parser[Token]](_ | _) // no offence :-) - } - - _delim + private lazy val _delim: Parser[Token] = { + // construct parser for delimiters by |'ing together the parsers for the individual delimiters, + // starting with the longest one -- otherwise a delimiter D will never be matched if there is + // another delimiter that is a prefix of D + def parseDelim(s: String): Parser[Token] = accept(s.toList) ^^ { x => Keyword(s) } + + val d = new Array[String](delimiters.size) + delimiters.copyToArray(d, 0) + scala.util.Sorting.quickSort(d) + (d.toList map parseDelim).foldRight(failure("no matching delimiter"): Parser[Token])((x, y) => y | x) } + protected def delim: Parser[Token] = _delim private def lift[T](f: String => T)(xs: List[Char]): T = f(xs.mkString("", "", "")) - private def lift2[T](f: String => T)(p: ~[Char, List[Char]]): T = lift(f)(p._1 :: p._2) } -- cgit v1.2.3