diff options
author | michelou <michelou@epfl.ch> | 2007-11-09 16:43:35 +0000 |
---|---|---|
committer | michelou <michelou@epfl.ch> | 2007-11-09 16:43:35 +0000 |
commit | 35bd7a8255fb8c00b3dd08932a224e68a5ae2bfb (patch) | |
tree | 0a5c5c29c179e2e686e7d157c65c2448a78f46c0 /src | |
parent | d3b764f220f3d083c4f2c36471ce68d5fbbb8854 (diff) | |
download | scala-35bd7a8255fb8c00b3dd08932a224e68a5ae2bfb.tar.gz scala-35bd7a8255fb8c00b3dd08932a224e68a5ae2bfb.tar.bz2 scala-35bd7a8255fb8c00b3dd08932a224e68a5ae2bfb.zip |
fixd ticket #136
Diffstat (limited to 'src')
-rw-r--r-- | src/library/scala/runtime/RichString.scala | 20 | ||||
-rw-r--r-- | src/library/scala/util/parsing/json/Lexer.scala | 36 | ||||
-rw-r--r-- | src/library/scala/util/parsing/json/Parser.scala | 10 |
3 files changed, 46 insertions, 20 deletions
diff --git a/src/library/scala/runtime/RichString.scala b/src/library/scala/runtime/RichString.scala index a16c550786..187f77e5d3 100644 --- a/src/library/scala/runtime/RichString.scala +++ b/src/library/scala/runtime/RichString.scala @@ -159,6 +159,25 @@ final class RichString(val self: String) extends Proxy with RandomAccessSeq[Char */ def stripMargin: String = stripMargin('|') + /** <p> + * Remove white space from both ends of each line and add + * a blank (" ") between lines before merging them. + * </p> + * <p> + * Equivalent to: <code>mergeLines(_.trim, " ")</code>. + * </p> + */ + def mergeLines: String = mergeLines(_.trim, " ") + + /** <p> + * Apply function <code>f</code> to each line and add + * the string <code>eol</code> between lines before + * merging them. + * </p> + */ + def mergeLines(f: String => String, eol: String): String = + lines map f mkString eol + private def escape(ch: Char): String = ch match { case '.' | '$' | '^' | '\\' => "\\" + ch case _ => "" + ch @@ -188,3 +207,4 @@ object RichString { private final val SU: Char = 0x1A } + diff --git a/src/library/scala/util/parsing/json/Lexer.scala b/src/library/scala/util/parsing/json/Lexer.scala index c709b37900..289551fe0b 100644 --- a/src/library/scala/util/parsing/json/Lexer.scala +++ b/src/library/scala/util/parsing/json/Lexer.scala @@ -22,10 +22,11 @@ import scala.util.parsing.input.CharArrayReader.EofCh class Lexer extends StdLexical with ImplicitConversions { override def token: Parser[Token] = - ('\"' ~ rep(charSeq | letter) ~ '\"' ^^ lift(StringLit) + //( '\"' ~ rep(charSeq | letter) ~ '\"' ^^ lift(StringLit) + ( string ^^ StringLit | number ~ letter ^^ { case n ~ l => ErrorToken("Invalid number format : " + n + l) } - | '-' ~ whitespace ~ number ~ letter ^^ {case ws ~ num ~ l => ErrorToken("Invalid number format : -" + num + l) } - | '-' ~ whitespace ~ number ^^ {case ws ~ num => NumericLit("-" + num)} + | '-' ~ whitespace ~ number ~ letter ^^ { case ws ~ num ~ l => ErrorToken("Invalid number format : -" + num + l) } + | '-' ~ whitespace ~ number ^^ { case ws ~ num => NumericLit("-" + num) } | number ^^ NumericLit | EofCh ^^ EOF | delim @@ -35,23 +36,28 @@ class Lexer extends StdLexical with ImplicitConversions { ) def checkKeyword(xs : List[Any]) = { - val strRep = xs.mkString("") - if (reserved.contains(strRep)) Keyword(strRep) else ErrorToken("Not a keyword: " + strRep) + val strRep = xs mkString "" + if (reserved contains strRep) Keyword(strRep) else ErrorToken("Not a keyword: " + strRep) } + /** A string is a collection of zero or more Unicode characters, wrapped in + * double quotes, using backslash escapes (cf. http://www.json.org/). + */ + def string = '\"' ~ rep(chrExcept('\"', '\n', EofCh)) ~ '\"' ^^ { _ mkString "" } + override def whitespace = rep(whitespaceChar) - def number = intPart ~ opt(fracPart) ~ opt(expPart) ^^ { case i ~ f ~ e => - i + optString(".",f) + optString("",e) + def number = intPart ~ opt(fracPart) ~ opt(expPart) ^^ { case i ~ f ~ e => + i + optString(".", f) + optString("", e) } def intPart = zero | intList def intList = nonzero ~ rep(digit) ^^ {case x ~ y => (x :: y) mkString ""} - def fracPart = '.' ~ rep(digit) ^^ {x => x.mkString("")} + def fracPart = '.' ~ rep(digit) ^^ { _ mkString "" } def expPart = exponent ~ opt(sign) ~ rep1(digit) ^^ { case e ~ s ~ d => - e + optString("",s) + d.mkString("") + e + optString("", s) + d.mkString("") } - def optString[A](pre: String, a: Option[A]) = a match { + private def optString[A](pre: String, a: Option[A]) = a match { case Some(x) => pre + x.toString case None => "" } @@ -75,10 +81,10 @@ class Lexer extends StdLexical with ImplicitConversions { val hexDigits = Set[Char]() ++ "0123456789abcdefABCDEF".toArray def hexDigit = elem("hex digit", hexDigits.contains(_)) - def unicodeBlock = hexDigit ~ hexDigit ~ hexDigit ~ hexDigit ^^ { - case a ~ b ~ c ~ d => - new String(io.UTF8Codec.encode(Integer.parseInt(List(a,b,c,d).mkString(""),16))) - } + private def unicodeBlock = hexDigit ~ hexDigit ~ hexDigit ~ hexDigit ^^ { + case a ~ b ~ c ~ d => + new String(io.UTF8Codec.encode(Integer.parseInt(List(a, b, c, d) mkString "", 16))) + } - private def lift[T](f: String => T)(xs: List[Any]): T = f(xs.mkString("")) + //private def lift[T](f: String => T)(xs: List[Any]): T = f(xs mkString "") } diff --git a/src/library/scala/util/parsing/json/Parser.scala b/src/library/scala/util/parsing/json/Parser.scala index 575f79a10c..50101c85eb 100644 --- a/src/library/scala/util/parsing/json/Parser.scala +++ b/src/library/scala/util/parsing/json/Parser.scala @@ -9,7 +9,7 @@ // $Id$ -package scala.util.parsing.json; +package scala.util.parsing.json import scala.util.parsing.combinator._ import scala.util.parsing.combinator.syntactical._ @@ -29,11 +29,11 @@ class Parser extends StdTokenParsers with ImplicitConversions { // Define the grammar def root = jsonObj | jsonArray - def jsonObj = "{" ~ repsep(objEntry,",") ~ "}" + def jsonObj = "{" ~ repsep(objEntry, ",") ~ "}" def jsonArray = "[" ~ repsep(value, ",") ~ "]" - def objEntry = stringVal ~ ":" ~ value ^^ { case x ~ y => (x,y) } + def objEntry = stringVal ~ ":" ~ value ^^ { case x ~ y => (x, y) } def value: Parser[Any] = (jsonObj | jsonArray | number | "true" ^^ true | "false" ^^ false | "null" ^^ null | stringVal) - def stringVal = accept("string", {case lexical.StringLit(n) => n}) - def number = accept("number", {case lexical.NumericLit(n) => n.toDouble}) + def stringVal = accept("string", { case lexical.StringLit(n) => n} ) + def number = accept("number", { case lexical.NumericLit(n) => n.toDouble} ) } |