Permalink
Browse files

Fix handling of whitespace in a recursive parser

  • Loading branch information...
1 parent 0122fcd commit 262bcd6ddd37d25d6b1108e9d1c6926c87de1fe0 @puffnfresh puffnfresh committed Nov 5, 2012
@@ -700,7 +700,7 @@ trait Parsers {
}
}
- t.add(thunk, in)(f)
+ t.add(thunk, handleWhitespace(in))(f)
}
}
@@ -44,7 +44,10 @@ trait RegexParsers extends Parsers {
}
override protected def handleWhitespace(s: LineStream) =
- s.drop(whitespace findPrefixOf s map { _.length } getOrElse 0)
+ if (skipWhitespace)
+ s.drop(whitespace findPrefixOf s map { _.length } getOrElse 0)
+ else
+ s
class RichRegexParser(left: RegexParser) extends RichParser(left) {
@@ -88,6 +88,23 @@ object RegexSpecs extends Specification
}
}
+ "produce a location of after leading whitespace in seqential parser" in {
+ sealed trait A
+ case class B(loc: LineStream, id: String, left: String, right: A) extends A
+ case class C(loc: LineStream, id: String) extends A
+
+ lazy val p: Parser[A] = (
+ "daniel" ~ ":=" ~ "daniel" ~ p ^# { (loc, id, _, a, b) => B(loc, id, a, b) }
+ | "daniel" ^# { (loc, d) => C(loc, d) }
+ )
+
+ p("\n\n\n\n daniel := daniel daniel") must beLike {
+ case Success(B(l, "daniel", "daniel", C(l2, "daniel")), LineStream()) #:: SNil =>
+ l.lineNum mustEqual 5
+ l.colNum mustEqual 2
+ }
+ }
+
"eat leading whitespace" in {
val p = literal("daniel")

0 comments on commit 262bcd6

Please sign in to comment.