diff options
author | Martin Odersky <odersky@gmail.com> | 2008-03-10 19:34:38 +0000 |
---|---|---|
committer | Martin Odersky <odersky@gmail.com> | 2008-03-10 19:34:38 +0000 |
commit | 6137ba4276f4eb57a6799a1233ae76c949778cf0 (patch) | |
tree | 1cb5fa60c48efc9acddc7d5b37b9c4b78ca4dcd5 | |
parent | a311262c6796bb0e27fa0d216bcc2a5e06a97bcb (diff) | |
download | scala-6137ba4276f4eb57a6799a1233ae76c949778cf0.tar.gz scala-6137ba4276f4eb57a6799a1233ae76c949778cf0.tar.bz2 scala-6137ba4276f4eb57a6799a1233ae76c949778cf0.zip |
added support for regexps """<regexp>""".r.
6 files changed, 49 insertions, 22 deletions
diff --git a/src/library/scala/reflect/Manifest.scala b/src/library/scala/reflect/Manifest.scala index 5cf188b7dd..c44ea512ec 100755 --- a/src/library/scala/reflect/Manifest.scala +++ b/src/library/scala/reflect/Manifest.scala @@ -49,5 +49,7 @@ object Manifest { def intersectionType[T](parents: Manifest[_]*): Manifest[T] = null } +/** A Manifest[T] is a descriptor for the type T. + */ abstract class Manifest[T] diff --git a/src/library/scala/runtime/RichString.scala b/src/library/scala/runtime/RichString.scala index 67cd75154c..bf14a652a5 100644 --- a/src/library/scala/runtime/RichString.scala +++ b/src/library/scala/runtime/RichString.scala @@ -11,8 +11,8 @@ package scala.runtime - import Predef._ +import scala.util.matching.Regex final class RichString(val self: String) extends Proxy with RandomAccessSeq[Char] with Ordered[String] { import RichString._ @@ -200,6 +200,13 @@ final class RichString(val self: String) extends Proxy with RandomAccessSeq[Char self.split(re) } + /** You can follow a string with `.r', turning + * it into a Regex. E.g. + * + * """A\w*""".r is the regular expression for identifiers starting with `A'. + */ + def r: Regex = new Regex(self) + def toBoolean: Boolean = parseBoolean(self) def toByte: Byte = java.lang.Byte.parseByte(self) def toShort: Short = java.lang.Short.parseShort(self) diff --git a/src/library/scala/util/matching/Regex.scala b/src/library/scala/util/matching/Regex.scala index 66a27cfdef..f5a871d213 100644 --- a/src/library/scala/util/matching/Regex.scala +++ b/src/library/scala/util/matching/Regex.scala @@ -12,7 +12,7 @@ import java.util.regex.{Pattern, Matcher} * @param regex A string representing a regular expression * @param groupNames A mapping from names to indices in capture groups */ -class Regex(regex: String, private var groupNames: Map[String, Int]) { +class Regex(val regex: String, private var groupNames: Map[String, Int]) { private def buildMap(res: Map[String, Int], groupNames: Seq[String], i: Int): Map[String, Int] = if (i > groupNames.size) res @@ -41,8 +41,8 @@ class Regex(regex: String, private var groupNames: Map[String, Int]) { groupNames = buildMap(Map[String, Int](), groups, 1) } - /* Store the compiled pattern at instantiation time */ - private val pattern = Pattern.compile(regex) + /* Stores the compiled pattern at instantiation time */ + val pattern = Pattern.compile(regex) /* Builds a MatchData[String] from a Matcher */ private def buildSeq(l: List[String], m: Matcher, i: Int): MatchData[String] = @@ -153,6 +153,8 @@ class Regex(regex: String, private var groupNames: Map[String, Int]) { val m = pattern.matcher(target) m.matches() } + + override def toString = """regex"""+".r" } /** Provides implicit conversions for regular expressions. diff --git a/src/library/scala/util/parsing/combinator/Parsers.scala b/src/library/scala/util/parsing/combinator/Parsers.scala index 450fc73d86..5ae48d3cd9 100644 --- a/src/library/scala/util/parsing/combinator/Parsers.scala +++ b/src/library/scala/util/parsing/combinator/Parsers.scala @@ -728,7 +728,7 @@ trait Parsers { def mkList[T] = (_: ~[T, List[T]]) match { case x ~ xs => x :: xs } case class ~[+a, +b](_1: a, _2: b) { - override def toString = "("+ _1 +" ~ "+ _2 +")" + override def toString = "("+ _1 +"~"+ _2 +")" } /** A parser whose ~ combinator disallows back-tracking. diff --git a/src/library/scala/util/parsing/combinator/RegexParsers.scala b/src/library/scala/util/parsing/combinator/RegexParsers.scala index 6d42bb2794..4b76a8ee79 100755 --- a/src/library/scala/util/parsing/combinator/RegexParsers.scala +++ b/src/library/scala/util/parsing/combinator/RegexParsers.scala @@ -10,7 +10,8 @@ package scala.util.parsing.combinator -import java.util.regex._ +import java.util.regex.Pattern +import scala.util.matching.Regex import scala.util.parsing.input.CharSequenceReader trait RegexParsers extends Parsers { @@ -50,33 +51,20 @@ trait RegexParsers extends Parsers { } /** A parser that matches a regex string */ - def regex(s: String): Parser[String] = new Parser[String] { - private val pattern = Pattern.compile(s) + implicit def regex(r: Regex): Parser[String] = new Parser[String] { def apply(in: Input) = { val source = in.source val offset = in.offset val start = handleWhiteSpace(source, offset) - val pm = pattern.matcher(source.subSequence(start, source.length)) + val pm = r.pattern.matcher(source.subSequence(start, source.length)) if (pm.lookingAt) Success(source.subSequence(start, start + pm.end).toString, in.drop(start + pm.end - offset)) else - Failure("string matching regex `"+s+"' expected", in.drop(start - offset)) + Failure("string matching regex `"+r.regex+"' expected", in.drop(start - offset)) } } - /** A helper class to turn strings into regexes */ - class PreRegex(s: String) { - def r: Parser[String] = regex(s) - } - - /** An implicit definition which lets you follow a string with `.r', turning - * it into a regex parser. E.g. - * - * """A\w*""".r is a regex parser for identifiers starting with `A'. - */ - implicit def mkPreRegex(s: String) = new PreRegex(s) - /** Parse some prefix of character sequence `in' with parser `p' */ def parse[T](p: Parser[T])(in: CharSequence): ParseResult[T] = p(new CharSequenceReader(in)) diff --git a/src/library/scala/util/parsing/combinator/StdRegexParsers.scala b/src/library/scala/util/parsing/combinator/StdRegexParsers.scala new file mode 100644 index 0000000000..4b6b15af52 --- /dev/null +++ b/src/library/scala/util/parsing/combinator/StdRegexParsers.scala @@ -0,0 +1,28 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2006-2007, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +// $Id: Parsers.scala 12357 2007-07-18 21:55:08Z moors $ + +package scala.util.parsing.combinator + +import java.util.regex._ +import scala.util.parsing.input.CharSequenceReader + +trait StdRegexParsers extends RegexParsers { + def ident: Parser[String] = + """[a-zA-Z_]\w*""".r + def wholeNumber: Parser[String] = + """\d+""".r + def decimalNumber: Parser[String] = + """(\d+(\.\d*)?|\d*.\d+)""".r + def stringLiteral: Parser[String] = + "\""+"""([^"\p{Cntrl}\\]|\\[\\/bfnrt]|\\u[a-fA-F0-9]{4})*"""+"\"".r + def floatingPointNumber: Parser[String] = + """-?(\d+(\.\d*)?|\d*\.\d+)[eEfFdD]?([+-]?\d+)?""".r + +} |