diff options
author | Adriaan Moors <adriaan.moors@epfl.ch> | 2007-07-18 09:07:54 +0000 |
---|---|---|
committer | Adriaan Moors <adriaan.moors@epfl.ch> | 2007-07-18 09:07:54 +0000 |
commit | 3d8003db990beea3fe1c5dc678cd511e6df5d033 (patch) | |
tree | bf22c26632e404eb34071b41c033b7605e7cb484 /src | |
parent | 02189a8d5b0e29da3ad5ebb6dfc4ac8bd028c70d (diff) | |
download | scala-3d8003db990beea3fe1c5dc678cd511e6df5d033.tar.gz scala-3d8003db990beea3fe1c5dc678cd511e6df5d033.tar.bz2 scala-3d8003db990beea3fe1c5dc678cd511e6df5d033.zip |
added a generalised version of rep1 that allows...
added a generalised version of rep1 that allows the first piece of input
to be matched by a different parser
Diffstat (limited to 'src')
-rw-r--r-- | src/library/scala/util/parsing/combinator/Parsers.scala | 18 |
1 files changed, 16 insertions, 2 deletions
diff --git a/src/library/scala/util/parsing/combinator/Parsers.scala b/src/library/scala/util/parsing/combinator/Parsers.scala index 407da2ac96..98d48972f8 100644 --- a/src/library/scala/util/parsing/combinator/Parsers.scala +++ b/src/library/scala/util/parsing/combinator/Parsers.scala @@ -713,13 +713,27 @@ trait Parsers { * @return A parser that returns a list of results produced by repeatedly applying `p' to the input * (and that only succeeds if `p' matches at least once). */ - def rep1[T](p: Parser[T]): Parser[List[T]] = p ~ rep(p) ^^ { case ~(x, xs) => x :: xs } + def rep1[T](p: Parser[T]): Parser[List[T]] = rep1(p, p) + + /** A parser generator for non-empty repetitions. + * + * <p> rep1(f, p) first uses `f' (which must succeed) and then repeatedly uses `p' to + * parse the input until `p' fails + * (the result is a `List' of the consecutive results of `f' and `p')</p> + * + * @param first a `Parser' that parses the first piece of input + * @param p a `Parser' that is to be applied successively to the rest of the input (if any) + * @return A parser that returns a list of results produced by first applying `f' and then + * repeatedly `p' to the input (it only succeeds if `f' matches). + */ + def rep1[T](first: Parser[T], p: Parser[T]): Parser[List[T]] = first ~ rep(p) ^^ { case ~(x, xs) => x :: xs } + /* new Parser[List[T]] { def apply(in0: Input) = { val xs = new scala.collection.mutable.ListBuffer[T] var in = in0 - var res = p(in) + var res = first(in) while(res.successful) { xs += res.get |