summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAdriaan Moors <adriaan.moors@epfl.ch>2008-02-08 15:55:46 +0000
committerAdriaan Moors <adriaan.moors@epfl.ch>2008-02-08 15:55:46 +0000
commit183c469b21688cc60993b84b66f04b76c648a763 (patch)
tree2aaef1c04cff3a34708edff767f85632c958a7c7 /src
parentf45ea36183741823c69ee47ef252a68b9dce5dd7 (diff)
downloadscala-183c469b21688cc60993b84b66f04b76c648a763.tar.gz
scala-183c469b21688cc60993b84b66f04b76c648a763.tar.bz2
scala-183c469b21688cc60993b84b66f04b76c648a763.zip
iterative version of rep1
(note: rep1sep and repN are still (non-tail-)recursive)
Diffstat (limited to 'src')
-rw-r--r--src/library/scala/util/parsing/combinator/Parsers.scala35
1 files changed, 21 insertions, 14 deletions
diff --git a/src/library/scala/util/parsing/combinator/Parsers.scala b/src/library/scala/util/parsing/combinator/Parsers.scala
index 15845c8e84..b566375280 100644
--- a/src/library/scala/util/parsing/combinator/Parsers.scala
+++ b/src/library/scala/util/parsing/combinator/Parsers.scala
@@ -564,25 +564,32 @@ trait Parsers {
* @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 }
+ def rep1[T](first: => Parser[T], p: => Parser[T]): Parser[List[T]] = Parser{ in0 =>
+ val xs = new scala.collection.mutable.ListBuffer[T]
+ var in = in0
- /* new Parser[List[T]] {
- def apply(in0: Input) = {
- val xs = new scala.collection.mutable.ListBuffer[T]
- var in = in0
+ var res = first(in)
- var res = first(in)
+ while(res.successful) {
+ xs += res.get
+ in = res.next
+ res = p(in)
+ }
- while(res.successful) {
- xs += res.get
- in = res.next
- res = p(in)
- }
+ // assert(res.isInstanceOf[NoSuccess])
- if (!xs.isEmpty) Success(xs.toList, res.next)
- else Failure("TODO", TODO)
+ if (!xs.isEmpty) {
+ // the next parser should start parsing where p failed,
+ // since `!p(in).successful', the next input to be consumed is `in'
+ Success(xs.toList, in) // TODO: I don't think in == res.next holds
+ }
+ else {
+ Failure(res.asInstanceOf[NoSuccess].msg, in0)
}
- }*/
+ }
+
+ //= first ~ rep(p) ^^ { case ~(x, xs) => x :: xs }
+
/** A parser generator for a specified number of repetitions.
*