Skip to content

Commit a65cbec

Browse files
committed
code review PR #245
1 parent 96acad4 commit a65cbec

File tree

2 files changed

+10
-25
lines changed

2 files changed

+10
-25
lines changed

shared/src/main/scala/scala/util/parsing/combinator/Parsers.scala

+8-23
Original file line numberDiff line numberDiff line change
@@ -795,27 +795,26 @@ trait Parsers {
795795
/** A parser generator for a specified range of repetitions interleaved by a
796796
* separator.
797797
*
798-
* `repN(n, m, p, s)` uses `p` at least `m` times and up to `n` times, interleaved
798+
* `repNM(n, m, p, s)` uses `p` at least `n` times and up to `m` times, interleaved
799799
* with separator `s`, to parse the input
800-
* (the result is a `List` of at least `m` consecutive results of `p` and up to `n` results).
800+
* (the result is a `List` of at least `n` consecutive results of `p` and up to `m` results).
801801
*
802-
* @param m minimum number of repetitions
803-
* @param n maximum number of repetitions
802+
* @param n minimum number of repetitions
803+
* @param m maximum number of repetitions
804804
* @param p a `Parser` that is to be applied successively to the input
805805
* @param sep a `Parser` that interleaves with p
806806
* @return A parser that returns a list of results produced by repeatedly applying `p` interleaved
807-
* with `sep` to the input. The list has a size between `m` and up to `n`
807+
* with `sep` to the input. The list has a size between `n` and up to `m`
808808
* (and that only succeeds if `p` matches at least `n` times).
809809
*/
810-
def repMN[T](m: Int, n: Int, p: Parser[T], sep: Parser[Any]): Parser[List[T]] = Parser { in =>
811-
require(0 <= m && m <= n)
812-
val mandatory = if (m == 0) success(Nil) else (p ~ repN(m - 1, sep ~> p)).map { case head ~ tail => head :: tail }
810+
def repNM[T](n: Int, m: Int, p: Parser[T], sep: Parser[Any] = success(())): Parser[List[T]] = Parser { in =>
811+
val mandatory = if (n == 0) success(Nil) else (p ~ repN(n - 1, sep ~> p)).map { case head ~ tail => head :: tail }
813812
val elems = new ListBuffer[T]
814813

815814
def continue(in: Input): ParseResult[List[T]] = {
816815
val p0 = sep ~> p // avoid repeatedly re-evaluating by-name parser
817816
@tailrec def applyp(in0: Input): ParseResult[List[T]] = p0(in0) match {
818-
case Success(x, rest) => elems += x; if (elems.length == n) Success(elems.toList, rest) else applyp(rest)
817+
case Success(x, rest) => elems += x; if (elems.length == m) Success(elems.toList, rest) else applyp(rest)
819818
case e @ Error(_, _) => e // still have to propagate error
820819
case _ => Success(elems.toList, in0)
821820
}
@@ -829,20 +828,6 @@ trait Parsers {
829828
}
830829
}
831830

832-
/** A parser generator for a specified range of repetitions.
833-
*
834-
* `repN(n, m, p)` uses `p` at least `m` times and up to `n` times to parse the input
835-
* (the result is a `List` of at least `m` consecutive results of `p` and up to `n` results).
836-
*
837-
* @param m minimum number of repetitions
838-
* @param n maximum number of repetitions
839-
* @param p a `Parser` that is to be applied successively to the input
840-
* @return A parser that returns a list of results produced by repeatedly applying `p` to the input.
841-
* The list has a size between `m` and up to `n`
842-
* (and that only succeeds if `p` matches at least `n` times).
843-
*/
844-
def repMN[T](m: Int, n: Int, p: Parser[T]): Parser[List[T]] = repMN[T](m, n, p, success(()))
845-
846831
/** A parser generator for non-empty repetitions.
847832
*
848833
* `rep1sep(p, q)` repeatedly applies `p` interleaved with `q` to parse the

shared/src/test/scala/scala/util/parsing/combinator/gh242.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import scala.util.parsing.input.CharSequenceReader
77
class gh242 {
88
class TestWithSeparator extends Parsers {
99
type Elem = Char
10-
val csv: Parser[List[Char]] = repMN(5, 10, 'a', ',')
10+
val csv: Parser[List[Char]] = repNM(5, 10, 'a', ',')
1111
}
1212

1313
class TestWithoutSeparator extends Parsers {
1414
type Elem = Char
15-
val csv: Parser[List[Char]] = repMN(5, 10, 'a')
15+
val csv: Parser[List[Char]] = repNM(5, 10, 'a')
1616
}
1717

1818
@Test

0 commit comments

Comments
 (0)