Skip to content
Browse files

Switched from Stream[Char] to LineStream

  • Loading branch information...
1 parent aa03224 commit 28d767388c45cee9c3a8edf524a5d1ef1f94e5fa @djspiewak committed May 5, 2009
View
38 src/main/scala/edu/uwm/cs/gll/CharSequenceConversions.scala
@@ -1,38 +0,0 @@
-package edu.uwm.cs.gll
-
-trait CharSequenceConversions {
- implicit def stream2CharSequence(s: Stream[Char]): CharSequence = new StreamCharSequence(s)
-
- /**
- * An amortized O(1) view of a character stream. This
- * allows efficient prefix matching for regular expressions.
- * We could make this even more efficient by sharing pages
- * between views on the same stream.
- */
- private class StreamCharSequence(s: Stream[Char], private var page: String) extends CharSequence {
- lazy val length = s.length
-
- def this(s: Stream[Char]) = this(s, "")
-
- def charAt(index: Int) = {
- while (index > page.length - 1)
- paginate()
-
- page(index)
- }
-
- def subSequence(start: Int, end: Int) = {
- import Math.{min, max}
-
- val newPage = page.subSequence(min(start, page.length - 1), max(page.length - 1, end))
- new StreamCharSequence(s.take(end) drop start, newPage toString)
- }
-
- override def toString = s.mkString
-
- private def paginate() {
- val length = if (page.length == 0) 10 else page.length * 2
- page = s take length mkString
- }
- }
-}
View
48 src/main/scala/edu/uwm/cs/gll/Parsers.scala
@@ -20,11 +20,11 @@ trait Parsers {
def rep1[A](p: Parser[A]) = p+
- protected def processTail(tail: Stream[Char]) = if (tail.isEmpty) Some(tail) else None
+ protected def processTail(tail: LineStream) = if (tail.isEmpty) Some(tail) else None
//////////////////////////////////////////////////////////////////////////////
- sealed trait Parser[+R] extends (Stream[Char]=>List[Result[R]]) { self =>
+ sealed trait Parser[+R] extends (LineStream=>List[Result[R]]) { self =>
val terminal: Boolean
lazy val first = computeFirst(Set()) getOrElse Set()
@@ -35,18 +35,18 @@ trait Parsers {
*/
def computeFirst(seen: Set[Parser[Any]]): Option[Set[Char]]
- def queue(t: Trampoline, in: Stream[Char])(f: Result[R]=>Unit)
+ def queue(t: Trampoline, in: LineStream)(f: Result[R]=>Unit)
// syntax
- def apply(str: String): List[Result[R]] = apply(str toProperStream)
+ def apply(str: String): List[Result[R]] = apply(LineStream(str))
def map[R2](f: R=>R2): Parser[R2]
def flatMap[R2](f1: R=>Parser[R2]): Parser[R2] = new NonTerminalParser[R2] {
def computeFirst(seen: Set[Parser[Any]]) = self.computeFirst(seen + this)
- def queue(t: Trampoline, in: Stream[Char])(f2: Result[R2]=>Unit) {
+ def queue(t: Trampoline, in: LineStream)(f2: Result[R2]=>Unit) {
self.queue(t, in) {
case Success(res1, tail) => f1(res1).queue(t, tail)(f2)
case f: Failure => f2(f)
@@ -57,7 +57,7 @@ trait Parsers {
def filter(f: R=>Boolean): Parser[R] = new NonTerminalParser[R] {
def computeFirst(seen: Set[Parser[Any]]) = self.computeFirst(seen + this)
- def queue(t: Trampoline, in: Stream[Char])(f2: Result[R]=>Unit) {
+ def queue(t: Trampoline, in: LineStream)(f2: Result[R]=>Unit) {
self.queue(t, in) {
case s @ Success(res, _) => {
if (f(res))
@@ -86,7 +86,7 @@ trait Parsers {
def +(): Parser[List[R]] = new NonTerminalParser[List[R]] {
def computeFirst(seen: Set[Parser[Any]]) = self.computeFirst(seen + this)
- def queue(t: Trampoline, in: Stream[Char])(f: Result[List[R]]=>Unit) {
+ def queue(t: Trampoline, in: LineStream)(f: Result[List[R]]=>Unit) {
t.add(self, in) {
case Success(res1, tail) => {
f(Success(res1 :: Nil, tail))
@@ -109,7 +109,7 @@ trait Parsers {
def ?(): Parser[Option[R]] = new NonTerminalParser[Option[R]] {
def computeFirst(seen: Set[Parser[Any]]) = None
- def queue(t: Trampoline, in: Stream[Char])(f: Result[Option[R]]=>Unit) {
+ def queue(t: Trampoline, in: LineStream)(f: Result[Option[R]]=>Unit) {
f(Success(None, in))
t.add(self, in) {
@@ -129,7 +129,7 @@ trait Parsers {
trait TerminalParser[+R] extends Parser[R] { self =>
final val terminal = true
- final def apply(in: Stream[Char]) = List(parse(in) match {
+ final def apply(in: LineStream) = List(parse(in) match {
case Success(res, tail) => processTail(tail) match {
case Some(tail) => Success(res, tail)
case None => Failure(TAIL_ERROR_PATTERN.format(tail.mkString), tail)
@@ -141,11 +141,11 @@ trait Parsers {
/**
* For terminal parsing, this just delegates back to apply()
*/
- def queue(t: Trampoline, in: Stream[Char])(f: Result[R]=>Unit) {
+ def queue(t: Trampoline, in: LineStream)(f: Result[R]=>Unit) {
f(parse(in))
}
- protected def parse(in: Stream[Char]): Result[R]
+ protected def parse(in: LineStream): Result[R]
override def ~[R2](other: Parser[R2]) = other match {
case other: TerminalParser[R2] => {
@@ -160,7 +160,7 @@ trait Parsers {
}
}
- def parse(in: Stream[Char]) = self.parse(in) match {
+ def parse(in: LineStream) = self.parse(in) match {
case Success(res1, tail) => other.parse(tail) match {
case Success(res2, tail) => Success(new ~(res1, res2), tail)
case f: Failure => f
@@ -175,7 +175,7 @@ trait Parsers {
}
def map[R2](f: R=>R2): Parser[R2] = new MappedParser[R, R2](self, f) with TerminalParser[R2] {
- def parse(in: Stream[Char]) = self.parse(in) match {
+ def parse(in: LineStream) = self.parse(in) match {
case Success(res, tail) => Success(f(res), tail)
case x: Failure => x
}
@@ -195,7 +195,7 @@ trait Parsers {
* we define any Success with a non-empty tail to be a
* Failure
*/
- final def apply(in: Stream[Char]) = {
+ final def apply(in: LineStream) = {
val t = new Trampoline
var successes = Set[Success[R]]()
@@ -222,7 +222,7 @@ trait Parsers {
}
def map[R2](f1: R=>R2): Parser[R2] = new MappedParser[R, R2](self, f1) with NonTerminalParser[R2] {
- def queue(t: Trampoline, in: Stream[Char])(f2: Result[R2]=>Unit) {
+ def queue(t: Trampoline, in: LineStream)(f2: Result[R2]=>Unit) {
self.queue(t, in) {
case Success(res, tail) => f2(Success(f1(res), tail))
case f: Failure => f2(f)
@@ -271,7 +271,7 @@ trait Parsers {
Some(if (str.length > 0) Set(str charAt 0) else Set())
}
- def parse(in: Stream[Char]) = {
+ def parse(in: LineStream) = {
val trunc = in take str.length
lazy val errorMessage = "Expected '%s' got '%s'".format(str, trunc.mkString)
@@ -315,7 +315,7 @@ trait Parsers {
}
}
- def queue(t: Trampoline, in: Stream[Char])(f: Result[A ~ B]=>Unit) {
+ def queue(t: Trampoline, in: LineStream)(f: Result[A ~ B]=>Unit) {
left.queue(t, in) {
case Success(res1, tail) => {
right.queue(t, tail) {
@@ -404,7 +404,7 @@ trait Parsers {
}
}
- def queue(t: Trampoline, in: Stream[Char])(f: Result[A]=>Unit) {
+ def queue(t: Trampoline, in: LineStream)(f: Result[A]=>Unit) {
val UNEXPECTED_PATTERN = "Unexpected value in stream: '%s'"
if (isLL1) { // graceful degrade to LL(1)
@@ -421,7 +421,7 @@ trait Parsers {
}
} else {
val thunk = new ThunkParser(this) {
- def queue(t: Trampoline, in: Stream[Char])(f: Result[A]=>Unit) {
+ def queue(t: Trampoline, in: LineStream)(f: Result[A]=>Unit) {
var predicted = false
val results = mutable.Set[Result[A]]() // merge results
@@ -503,16 +503,16 @@ trait Parsers {
private type FSet[A] = mutable.Set[Result[A]=>Unit]
// R
- private val queue = new mutable.Queue[(Parser[Any], Stream[Char])]
+ private val queue = new mutable.Queue[(Parser[Any], LineStream)]
// U_j
- private val done = mutable.Map[Stream[Char], mutable.Set[Parser[Any]]]()
+ private val done = mutable.Map[LineStream, mutable.Set[Parser[Any]]]()
// P
- private val popped = mutable.Map[Stream[Char], HOMap[Parser, RSet]]()
+ private val popped = mutable.Map[LineStream, HOMap[Parser, RSet]]()
// GSS back edges
- private val backlinks = mutable.Map[Stream[Char], HOMap[Parser, FSet]]()
+ private val backlinks = mutable.Map[LineStream, HOMap[Parser, FSet]]()
// prevents divergence in cyclic GSS traversal
private val saved = HOMap[Result, FSet]()
@@ -545,7 +545,7 @@ trait Parsers {
}
}
- def add[A](p: Parser[A], s: Stream[Char])(f: Result[A]=>Unit) {
+ def add[A](p: Parser[A], s: LineStream)(f: Result[A]=>Unit) {
val tuple = (p, s)
lazy val containsSuccess = popped(s)(p) exists {
View
12 src/main/scala/edu/uwm/cs/gll/RegexParsers.scala
@@ -5,36 +5,36 @@ import scala.util.matching.Regex
import util._
// TODO need to handle trailing whitespace (somehow)
-trait RegexParsers extends Parsers with ImplicitConversions with CharSequenceConversions {
+trait RegexParsers extends Parsers with ImplicitConversions {
protected val whitespace = """\s+"""r
override implicit def literal(str: String): Parser[String] = new LiteralParser(str) {
override def computeFirst(seen: Set[Parser[Any]]) = Some(UniversalCharSet) // because of whitespace
// there should be a way to do this with traits, but I haven't found it yet
- override def parse(s: Stream[Char]) = super.parse(handleWhitespace(s))
+ override def parse(s: LineStream) = super.parse(handleWhitespace(s))
}
implicit def regex(r: Regex): Parser[String] = new RegexParser(r) {
override def computeFirst(seen: Set[Parser[Any]]) = Some(UniversalCharSet)
- override def parse(s: Stream[Char]) = super.parse(handleWhitespace(s))
+ override def parse(s: LineStream) = super.parse(handleWhitespace(s))
}
implicit def disjunctiveRegex(left: Regex) = new RichParser(regex(left))
implicit def funRegexSyntax(p: Regex) = new RichSyntax1(regex(p))
- override protected def processTail(tail: Stream[Char]) =
+ override protected def processTail(tail: LineStream) =
super.processTail(handleWhitespace(tail))
- private def handleWhitespace(s: Stream[Char]) =
+ private def handleWhitespace(s: LineStream) =
s.drop(whitespace findPrefixOf s map { _.length } getOrElse 0)
case class RegexParser(private val regex: Regex) extends TerminalParser[String] with CharSequenceConversions {
def computeFirst(s: Set[Parser[Any]]) = Some(UniversalCharSet)
- def parse(in: Stream[Char]) = {
+ def parse(in: LineStream) = {
val res = regex findPrefixOf in map { str => Success(str, in drop str.length) }
res getOrElse Failure("Expected /%s/".format(regex), in)
}
View
6 src/main/scala/edu/uwm/cs/gll/result.scala
@@ -1,9 +1,9 @@
package edu.uwm.cs.gll
sealed trait Result[+R] {
- val tail: Stream[Char]
+ val tail: LineStream
}
-case class Success[+R](value: R, tail: Stream[Char]) extends Result[R]
+case class Success[+R](value: R, tail: LineStream) extends Result[R]
-case class Failure(msg: String, tail: Stream[Char]) extends Result[Nothing]
+case class Failure(msg: String, tail: LineStream) extends Result[Nothing]
View
32 src/spec/scala/ArithmeticSpecs.scala
@@ -14,7 +14,7 @@ object ArithmeticSpecs extends Specification with ScalaCheck with RegexParsers {
"parse numbers" in {
val prop = forAll { x: Int =>
expr(x.toString) match {
- case Success(e, Stream()) :: Nil => e.solve == x
+ case Success(e, LineStream()) :: Nil => e.solve == x
case _ => false
}
}
@@ -30,17 +30,17 @@ object ArithmeticSpecs extends Specification with ScalaCheck with RegexParsers {
res.length mustBe 2
res must have {
- case Success(e @ Add(Neg(e1), e2), Stream()) => e.solve == x + y
+ case Success(e @ Add(Neg(e1), e2), LineStream()) => e.solve == x + y
case _ => false
}
res must have {
- case Success(e @ Neg(Add(e1, e2)), Stream()) => e.solve == -(-x + y)
+ case Success(e @ Neg(Add(e1, e2)), LineStream()) => e.solve == -(-x + y)
case _ => false
}
} else {
res must beLike {
- case Success(e, Stream()) :: Nil => e.solve == x + y
+ case Success(e, LineStream()) :: Nil => e.solve == x + y
case _ => false
}
}
@@ -57,17 +57,17 @@ object ArithmeticSpecs extends Specification with ScalaCheck with RegexParsers {
res.length mustBe 2
res must have {
- case Success(e @ Sub(Neg(e1), e2), Stream()) => e.solve == x - y
+ case Success(e @ Sub(Neg(e1), e2), LineStream()) => e.solve == x - y
case _ => false
}
res must have {
- case Success(e @ Neg(Sub(e1, e2)), Stream()) => e.solve == -(-x - y)
+ case Success(e @ Neg(Sub(e1, e2)), LineStream()) => e.solve == -(-x - y)
case _ => false
}
} else {
res must beLike {
- case Success(e, Stream()) :: Nil => e.solve == x - y
+ case Success(e, LineStream()) :: Nil => e.solve == x - y
case _ => false
}
}
@@ -84,17 +84,17 @@ object ArithmeticSpecs extends Specification with ScalaCheck with RegexParsers {
res.length mustBe 2
res must have {
- case Success(e @ Mul(Neg(e1), e2), Stream()) => e.solve == x * y
+ case Success(e @ Mul(Neg(e1), e2), LineStream()) => e.solve == x * y
case _ => false
}
res must have {
- case Success(e @ Neg(Mul(e1, e2)), Stream()) => e.solve == -(-x * y)
+ case Success(e @ Neg(Mul(e1, e2)), LineStream()) => e.solve == -(-x * y)
case _ => false
}
} else {
res must beLike {
- case Success(e, Stream()) :: Nil => e.solve == x * y
+ case Success(e, LineStream()) :: Nil => e.solve == x * y
case _ => false
}
}
@@ -112,17 +112,17 @@ object ArithmeticSpecs extends Specification with ScalaCheck with RegexParsers {
res.length mustBe 2
res must have {
- case Success(e @ Div(Neg(e1), e2), Stream()) => e.solve == x / y
+ case Success(e @ Div(Neg(e1), e2), LineStream()) => e.solve == x / y
case _ => false
}
res must have {
- case Success(e @ Neg(Div(e1, e2)), Stream()) => e.solve == -(-x / y)
+ case Success(e @ Neg(Div(e1, e2)), LineStream()) => e.solve == -(-x / y)
case _ => false
}
} else {
res must beLike {
- case Success(e, Stream()) :: Nil => e.solve == x / y
+ case Success(e, LineStream()) :: Nil => e.solve == x / y
case _ => false
}
}
@@ -134,7 +134,7 @@ object ArithmeticSpecs extends Specification with ScalaCheck with RegexParsers {
"produce both associativity configurations" in {
val res = expr("42 + 13 + 12") map {
- case Success(e, Stream()) => e
+ case Success(e, LineStream()) => e
case r => fail("%s does not match the expected pattern".format(r))
}
@@ -146,7 +146,7 @@ object ArithmeticSpecs extends Specification with ScalaCheck with RegexParsers {
"produce both binary precedence configurations" in {
val res = expr("42 + 13 - 12") map {
- case Success(e, Stream()) => e
+ case Success(e, LineStream()) => e
case r => fail("%s does not match the expected pattern".format(r))
}
val target = Set(Add(IntLit(42), Sub(IntLit(13), IntLit(12))),
@@ -157,7 +157,7 @@ object ArithmeticSpecs extends Specification with ScalaCheck with RegexParsers {
"produce both unary precedence configurations" in {
val res = expr("-42 + 13") map {
- case Success(e, Stream()) => e.solve
+ case Success(e, LineStream()) => e.solve
case r => fail("%s does not match the expected pattern".format(r))
}
View
74 src/spec/scala/CompoundSpecs.scala
@@ -17,7 +17,7 @@ object CompoundSpecs extends Specification with ImplicitConversions with ScalaCh
// assumes data =~ /a+/
def check(data: String) {
p(data) must beLike {
- case Success(`data`, Stream()) :: Nil => true
+ case Success(`data`, LineStream()) :: Nil => true
case _ => false
}
}
@@ -39,7 +39,7 @@ object CompoundSpecs extends Specification with ImplicitConversions with ScalaCh
// assumes data =~ /a+/
def check(data: String) {
p(data) must beLike {
- case Success(`data`, Stream()) :: Nil => true
+ case Success(`data`, LineStream()) :: Nil => true
case _ => false
}
}
@@ -61,7 +61,7 @@ object CompoundSpecs extends Specification with ImplicitConversions with ScalaCh
// assumes data =~ /a+/
def check(data: String) {
p(data) must beLike {
- case Success(`data`, Stream()) :: Nil => true
+ case Success(`data`, LineStream()) :: Nil => true
case _ => false
}
}
@@ -91,7 +91,7 @@ object CompoundSpecs extends Specification with ImplicitConversions with ScalaCh
// assumes data =~ /a+/
def check(data: String) {
s(data) must beLike {
- case Success(`data`, Stream()) :: _ => true // we don't care how many derivations, just that it works
+ case Success(`data`, LineStream()) :: _ => true // we don't care how many derivations, just that it works
case _ => false
}
}
@@ -119,7 +119,7 @@ object CompoundSpecs extends Specification with ImplicitConversions with ScalaCh
// assumes data =~ /a+/
def check(data: String) {
s(data) must beLike {
- case Success(`data`, Stream()) :: _ => true // we don't care how many derivations, just that it works
+ case Success(`data`, LineStream()) :: _ => true // we don't care how many derivations, just that it works
case _ => false
}
}
@@ -141,7 +141,7 @@ object CompoundSpecs extends Specification with ImplicitConversions with ScalaCh
// assumes data =~ /a+/
def check(data: String) {
s(data) must beLike {
- case Success(`data`, Stream()) :: _ => true // we don't care how many derivations, just that it works
+ case Success(`data`, LineStream()) :: _ => true // we don't care how many derivations, just that it works
case _ => false
}
}
@@ -181,7 +181,7 @@ object CompoundSpecs extends Specification with ImplicitConversions with ScalaCh
(1 + 2)"""
MathParser.expr(input) must beLike {
- case Success(8, Stream()) :: Nil => true
+ case Success(8, LineStream()) :: Nil => true
case _ => false
}
}
@@ -205,7 +205,7 @@ object CompoundSpecs extends Specification with ImplicitConversions with ScalaCh
import ComplexParser._
exp("(0,0) 2") must beLike {
- case Success(_, Stream()) :: Nil => true
+ case Success(_, LineStream()) :: Nil => true
case _ => false
}
}
@@ -216,17 +216,17 @@ object CompoundSpecs extends Specification with ImplicitConversions with ScalaCh
val p = literal("123")*
p("123") must beLike {
- case Success(List("123"), Stream()) :: Nil => true
+ case Success(List("123"), LineStream()) :: Nil => true
case _ => false
}
p("123123123123123123123123123123123") must beLike {
- case Success(List("123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123"), Stream()) :: Nil => true
+ case Success(List("123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123"), LineStream()) :: Nil => true
case _ => false
}
- p(Stream()) must beLike {
- case Success(Nil, Stream()) :: Nil => true
+ p(LineStream()) must beLike {
+ case Success(Nil, LineStream()) :: Nil => true
case _ => false
}
}
@@ -235,12 +235,12 @@ object CompoundSpecs extends Specification with ImplicitConversions with ScalaCh
val p = literal("123")+
p("123") must beLike {
- case Success(List("123"), Stream()) :: Nil => true
+ case Success(List("123"), LineStream()) :: Nil => true
case _ => false
}
p("123123123123123123123123123123123") must beLike {
- case Success(List("123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123"), Stream()) :: Nil => true
+ case Success(List("123", "123", "123", "123", "123", "123", "123", "123", "123", "123", "123"), LineStream()) :: Nil => true
case _ => false
}
}
@@ -249,12 +249,12 @@ object CompoundSpecs extends Specification with ImplicitConversions with ScalaCh
val p = literal("123")?
p("123") must beLike {
- case Success(Some("123"), Stream()) :: Nil => true
+ case Success(Some("123"), LineStream()) :: Nil => true
case _ => false
}
- p(Stream()) must beLike {
- case Success(None, Stream()) :: Nil => true
+ p(LineStream()) must beLike {
+ case Success(None, LineStream()) :: Nil => true
case _ => false
}
}
@@ -264,13 +264,13 @@ object CompoundSpecs extends Specification with ImplicitConversions with ScalaCh
"compose using bind" in {
val p = literal("a") flatMap { _ => literal("b") }
- p(Stream('a', 'b')) must beLike {
- case Success("b", Stream()) :: Nil => true
+ p(LineStream('a', 'b')) must beLike {
+ case Success("b", LineStream()) :: Nil => true
case _ => false
}
- p(Stream('a', 'c')) must beLike {
- case Failure("Expected 'b' got 'c'", Stream('c')) :: Nil => true
+ p(LineStream('a', 'c')) must beLike {
+ case Failure("Expected 'b' got 'c'", LineStream('c')) :: Nil => true
case _ => false
}
@@ -280,13 +280,13 @@ object CompoundSpecs extends Specification with ImplicitConversions with ScalaCh
"compose using map" in {
val p = literal("1") map { _.toInt }
- p(Stream('1')) must beLike {
- case Success(1, Stream()) :: Nil => true
+ p(LineStream('1')) must beLike {
+ case Success(1, LineStream()) :: Nil => true
case _ => false
}
- p(Stream('2')) must beLike {
- case Failure("Expected '1' got '2'", Stream('2')) :: Nil => true
+ p(LineStream('2')) must beLike {
+ case Failure("Expected '1' got '2'", LineStream('2')) :: Nil => true
case _ => false
}
@@ -296,18 +296,18 @@ object CompoundSpecs extends Specification with ImplicitConversions with ScalaCh
"compose using orElse" in {
val p = literal("a") orElse literal("b")
- p(Stream('a')) must beLike {
- case Success("a", Stream()) :: Nil => true
+ p(LineStream('a')) must beLike {
+ case Success("a", LineStream()) :: Nil => true
case _ => false
}
- p(Stream('b')) must beLike {
- case Success("b", Stream()) :: Nil => true
+ p(LineStream('b')) must beLike {
+ case Success("b", LineStream()) :: Nil => true
case _ => false
}
- p(Stream('c')) must beLike {
- case Failure("Unexpected value in stream: 'c'", Stream('c')) :: Nil => true
+ p(LineStream('c')) must beLike {
+ case Failure("Unexpected value in stream: 'c'", LineStream('c')) :: Nil => true
case _ => false
}
@@ -317,18 +317,18 @@ object CompoundSpecs extends Specification with ImplicitConversions with ScalaCh
"filter" in {
val p = ("a" | "b") filter { _ == "a" }
- p(Stream('a')) must beLike {
- case Success("a", Stream()) :: Nil => true
+ p(LineStream('a')) must beLike {
+ case Success("a", LineStream()) :: Nil => true
case _ => false
}
- p(Stream('b')) must beLike {
- case Failure("Syntax error", Stream('b')) :: Nil => true
+ p(LineStream('b')) must beLike {
+ case Failure("Syntax error", LineStream('b')) :: Nil => true
case _ => false
}
- p(Stream('c')) must beLike {
- case Failure("Unexpected value in stream: 'c'", Stream('c')) :: Nil => true
+ p(LineStream('c')) must beLike {
+ case Failure("Unexpected value in stream: 'c'", LineStream('c')) :: Nil => true
case _ => false
}
View
44 src/spec/scala/DisjunctionSpecs.scala
@@ -74,12 +74,12 @@ object DisjunctionSpecs extends Specification with ImplicitConversions with Scal
val p = "daniel" | "chris"
p("daniel") must beLike {
- case Success("daniel", Stream()) :: Nil => true
+ case Success("daniel", LineStream()) :: Nil => true
case _ => false
}
p("chris") must beLike {
- case Success("chris", Stream()) :: Nil => true
+ case Success("chris", LineStream()) :: Nil => true
case _ => false
}
}
@@ -88,7 +88,7 @@ object DisjunctionSpecs extends Specification with ImplicitConversions with Scal
val p = "" | ""
p("") must beLike {
- case Success("", Stream()) :: Nil => true
+ case Success("", LineStream()) :: Nil => true
case _ => false
}
}
@@ -97,27 +97,27 @@ object DisjunctionSpecs extends Specification with ImplicitConversions with Scal
"detect PREDCIT failure for LL(1)" in {
val p = "daniel" | "chris"
- p(Stream('j')) must beLike {
- case Failure("Unexpected value in stream: 'j'", Stream('j')) :: Nil => true
+ p(LineStream('j')) must beLike {
+ case Failure("Unexpected value in stream: 'j'", LineStream('j')) :: Nil => true
case _ => false
}
- p(Stream()) must beLike {
- case Failure("Unexpected end of stream", Stream()) :: Nil => true
+ p(LineStream()) must beLike {
+ case Failure("Unexpected end of stream", LineStream()) :: Nil => true
case _ => false
}
}
"detect PREDICT failure for non-LL(1)" in {
val p = "daniel" | "danielle"
- p(Stream('j')) must beLike {
- case Failure("Unexpected value in stream: 'j'", Stream('j')) :: Nil => true
+ p(LineStream('j')) must beLike {
+ case Failure("Unexpected value in stream: 'j'", LineStream('j')) :: Nil => true
case _ => false
}
- p(Stream()) must beLike {
- case Failure("Unexpected end of stream", Stream()) :: Nil => true
+ p(LineStream()) must beLike {
+ case Failure("Unexpected end of stream", LineStream()) :: Nil => true
case _ => false
}
}
@@ -126,12 +126,12 @@ object DisjunctionSpecs extends Specification with ImplicitConversions with Scal
val p = "daniel" | "chris"
p("dan") must beLike {
- case Failure("Unexpected end of stream (expected 'daniel')", Stream('d', 'a', 'n')) :: Nil => true
+ case Failure("Unexpected end of stream (expected 'daniel')", LineStream('d', 'a', 'n')) :: Nil => true
case _ => false
}
p("dancin") must beLike {
- case Failure("Expected 'daniel' got 'dancin'", Stream('d', 'a', 'n', 'c', 'i', 'n')) :: Nil => true
+ case Failure("Expected 'daniel' got 'dancin'", LineStream('d', 'a', 'n', 'c', 'i', 'n')) :: Nil => true
case _ => false
}
}
@@ -140,15 +140,15 @@ object DisjunctionSpecs extends Specification with ImplicitConversions with Scal
val p = "foobar" | "foobaz"
{
- val data = "foo".foldRight(Stream[Char]()) { Stream.cons(_, _) }
+ val data = LineStream("foo")
p(data) must haveTheSameElementsAs(List(
Failure("Unexpected end of stream (expected 'foobar')", data),
Failure("Unexpected end of stream (expected 'foobaz')", data)))
}
{
- val data = "foobat".foldRight(Stream[Char]()) { Stream.cons(_, _) }
+ val data = LineStream("foobat")
p(data) must haveTheSameElementsAs(List(
Failure("Expected 'foobar' got 'foobat'", data),
@@ -208,7 +208,7 @@ object DisjunctionSpecs extends Specification with ImplicitConversions with Scal
def check(p: Parser[Any], data: String*) = {
for (str <- data) {
p(str) must beLike {
- case Success(`str`, Stream()) :: Nil => true
+ case Success(`str`, LineStream()) :: Nil => true
case _ => false
}
}
@@ -236,7 +236,7 @@ object DisjunctionSpecs extends Specification with ImplicitConversions with Scal
val pattern = "Expected '%s' got '%s'"
for (str <- data) {
- val stream = str.foldRight(Stream[Char]()) { Stream.cons(_, _) }
+ val stream = LineStream(str)
val res = p(stream)
val failures = expect.foldRight(List[String]()) { _ :: _ } map { ex =>
@@ -251,7 +251,7 @@ object DisjunctionSpecs extends Specification with ImplicitConversions with Scal
val pattern = "Unexpected end of stream (expected '%s')"
for (str <- data) {
- val stream = str.foldRight(Stream[Char]()) { Stream.cons(_, _) }
+ val stream = LineStream(str)
val res = p(stream)
val failures = expect.foldRight(List[String]()) { _ :: _ } map { ex =>
@@ -296,12 +296,12 @@ object DisjunctionSpecs extends Specification with ImplicitConversions with Scal
) ^^ f
p(left) must beLike {
- case Success(v, Stream()) :: Nil => v == f(left)
+ case Success(v, LineStream()) :: Nil => v == f(left)
case _ => false
}
p(right) must beLike {
- case Success(v, Stream()) :: Nil => v == f(right)
+ case Success(v, LineStream()) :: Nil => v == f(right)
case _ => false
}
}
@@ -333,7 +333,7 @@ object DisjunctionSpecs extends Specification with ImplicitConversions with Scal
val v = head + suffix
result must have {
- case Success(`v`, Stream()) => true
+ case Success(`v`, LineStream()) => true
case _ => false
}
}
@@ -342,7 +342,7 @@ object DisjunctionSpecs extends Specification with ImplicitConversions with Scal
val v = head + " " + suffix
result must have {
- case Success(`v`, Stream()) => true
+ case Success(`v`, LineStream()) => true
case _ => false
}
}
View
28 src/spec/scala/RegexSpecs.scala
@@ -12,7 +12,7 @@ object RegexSpecs extends Specification with ScalaCheck with RegexParsers {
val p: Parser[String] = """(\d{1,3}\.){3}\d{1,3}"""r
p("192.168.101.2") must beLike {
- case Success("192.168.101.2", Stream()) :: Nil => true
+ case Success("192.168.101.2", LineStream()) :: Nil => true
case _ => false
}
}
@@ -21,15 +21,15 @@ object RegexSpecs extends Specification with ScalaCheck with RegexParsers {
val p: Parser[String] = """\d+"""r
var passed = false
- p.queue(null, "1234daniel".foldRight(Stream[Char]()) { Stream.cons(_, _) }) {
+ p.queue(null, LineStream("1234daniel")) {
case Success(res, tail) => {
if (passed) {
fail("Produced more than one result")
} else {
passed = true
res mustEqual "1234"
- tail zip Stream('d', 'a', 'n', 'i', 'e', 'l') forall { case (a, b) => a == b } mustBe true
+ tail zip LineStream('d', 'a', 'n', 'i', 'e', 'l') forall { case (a, b) => a == b } mustBe true
}
}
@@ -44,7 +44,7 @@ object RegexSpecs extends Specification with ScalaCheck with RegexParsers {
val p: Parser[String] = """(\d{1,3}\.){3}\d{1,3}"""r
{
- val data = "123.457.321".foldRight(Stream[Char]()) { Stream.cons(_, _) }
+ val data = LineStream("123.457.321")
p(data) must beLike {
case Failure("""Expected /(\d{1,3}\.){3}\d{1,3}/""", `data`) :: Nil => true
@@ -53,7 +53,7 @@ object RegexSpecs extends Specification with ScalaCheck with RegexParsers {
}
{
- val data = "123.457.321.sdf".foldRight(Stream[Char]()) { Stream.cons(_, _) }
+ val data = LineStream("123.457.321.sdf")
p(data) must beLike {
case Failure("""Expected /(\d{1,3}\.){3}\d{1,3}/""", `data`) :: Nil => true
@@ -62,8 +62,8 @@ object RegexSpecs extends Specification with ScalaCheck with RegexParsers {
}
{
- p(Stream()) must beLike {
- case Failure("""Expected /(\d{1,3}\.){3}\d{1,3}/""", Stream()) :: Nil => true
+ p(LineStream()) must beLike {
+ case Failure("""Expected /(\d{1,3}\.){3}\d{1,3}/""", LineStream()) :: Nil => true
case _ => false
}
}
@@ -73,23 +73,23 @@ object RegexSpecs extends Specification with ScalaCheck with RegexParsers {
val p = literal("daniel")
p("daniel") must beLike {
- case Success("daniel", Stream()) :: Nil => true
+ case Success("daniel", LineStream()) :: Nil => true
case _ => false
}
p(" daniel") must beLike {
- case Success("daniel", Stream()) :: Nil => true
+ case Success("daniel", LineStream()) :: Nil => true
case _ => false
}
p("\tdaniel") must beLike {
- case Success("daniel", Stream()) :: Nil => true
+ case Success("daniel", LineStream()) :: Nil => true
case _ => false
}
p("""
daniel""") must beLike {
- case Success("daniel", Stream()) :: Nil => true
+ case Success("daniel", LineStream()) :: Nil => true
case _ => false
}
}
@@ -98,17 +98,17 @@ object RegexSpecs extends Specification with ScalaCheck with RegexParsers {
val p = literal("daniel")
p("daniel ") must beLike {
- case Success("daniel", Stream()) :: Nil => true
+ case Success("daniel", LineStream()) :: Nil => true
case _ => false
}
p("daniel\t") must beLike {
- case Success("daniel", Stream()) :: Nil => true
+ case Success("daniel", LineStream()) :: Nil => true
case _ => false
}
p("daniel\n") must beLike {
- case Success("daniel", Stream()) :: Nil => true
+ case Success("daniel", LineStream()) :: Nil => true
case _ => false
}
}
View
40 src/spec/scala/TerminalSpecs.scala
@@ -11,7 +11,7 @@ object TerminalSpecs extends Specification with ScalaCheck with ImplicitConversi
val p = literal("test")
p("test") must beLike {
- case Success("test", Stream()) :: Nil => true
+ case Success("test", LineStream()) :: Nil => true
case _ => false
}
}
@@ -20,35 +20,35 @@ object TerminalSpecs extends Specification with ScalaCheck with ImplicitConversi
val p = literal("foo")
p("bar") must beLike {
- case Failure("Expected 'foo' got 'bar'", Stream('b', 'a', 'r')) :: Nil => true
+ case Failure("Expected 'foo' got 'bar'", LineStream('b', 'a', 'r')) :: Nil => true
case _ => false
}
p("test") must beLike {
- case Failure("Expected 'foo' got 'tes'", Stream('t', 'e', 's', 't')) :: Nil => true
+ case Failure("Expected 'foo' got 'tes'", LineStream('t', 'e', 's', 't')) :: Nil => true
case _ => false
}
}
"detect an unexpected end of stream" in {
val p = literal("foo")
- p(Stream('f')) must beLike {
- case Failure("Unexpected end of stream (expected 'foo')", Stream('f')) :: Nil => true
+ p(LineStream('f')) must beLike {
+ case Failure("Unexpected end of stream (expected 'foo')", LineStream('f')) :: Nil => true
case _ => false
}
- p(Stream()) must beLike {
- case Failure("Unexpected end of stream (expected 'foo')", Stream()) :: Nil => true
+ p(LineStream()) must beLike {
+ case Failure("Unexpected end of stream (expected 'foo')", LineStream()) :: Nil => true
case _ => false
}
}
"parse the empty string" in {
val p = literal("")
- p(Stream()) must beLike {
- case Success("", Stream()) :: Nil => true
+ p(LineStream()) must beLike {
+ case Success("", LineStream()) :: Nil => true
case _ => false
}
}
@@ -68,7 +68,7 @@ object TerminalSpecs extends Specification with ScalaCheck with ImplicitConversi
val p = "test" ^^ { _.length }
p("test") match {
- case Success(4, Stream()) :: Nil => true
+ case Success(4, LineStream()) :: Nil => true
case _ => false
}
}
@@ -77,7 +77,7 @@ object TerminalSpecs extends Specification with ScalaCheck with ImplicitConversi
val p = "test" ^^^ 42
p("test") match {
- case Success(42, Stream()) :: Nil => true
+ case Success(42, LineStream()) :: Nil => true
case _ => false
}
}
@@ -88,7 +88,7 @@ object TerminalSpecs extends Specification with ScalaCheck with ImplicitConversi
val p = "te" ~ "st"
p("test") must beLike {
- case Success("te" ~ "st", Stream()) :: Nil => true
+ case Success("te" ~ "st", LineStream()) :: Nil => true
case _ => false
}
}
@@ -97,36 +97,36 @@ object TerminalSpecs extends Specification with ScalaCheck with ImplicitConversi
val p = "te" ~ "st"
p("foo") must beLike {
- case Failure("Expected 'te' got 'fo'", Stream('f', 'o', 'o')) :: Nil => true
+ case Failure("Expected 'te' got 'fo'", LineStream('f', 'o', 'o')) :: Nil => true
case _ => false
}
p("tefoo") must beLike {
- case Failure("Expected 'st' got 'fo'", Stream('f', 'o', 'o')) :: Nil => true
+ case Failure("Expected 'st' got 'fo'", LineStream('f', 'o', 'o')) :: Nil => true
case _ => false
}
}
"detect an unexpected end of stream" in {
val p = "te" ~ "st"
- p(Stream('t')) must beLike {
- case Failure("Unexpected end of stream (expected 'te')", Stream('t')) :: Nil => true
+ p(LineStream('t')) must beLike {
+ case Failure("Unexpected end of stream (expected 'te')", LineStream('t')) :: Nil => true
case _ => false
}
- p(Stream()) must beLike {
- case Failure("Unexpected end of stream (expected 'te')", Stream()) :: Nil => true
+ p(LineStream()) must beLike {
+ case Failure("Unexpected end of stream (expected 'te')", LineStream()) :: Nil => true
case _ => false
}
p("tes") must beLike {
- case Failure("Unexpected end of stream (expected 'st')", Stream('s')) :: Nil => true
+ case Failure("Unexpected end of stream (expected 'st')", LineStream('s')) :: Nil => true
case _ => false
}
p("te") must beLike {
- case Failure("Unexpected end of stream (expected 'st')", Stream()) :: Nil => true
+ case Failure("Unexpected end of stream (expected 'st')", LineStream()) :: Nil => true
case _ => false
}
}

0 comments on commit 28d7673

Please sign in to comment.
Something went wrong with that request. Please try again.