Skip to content

Instantly share code, notes, and snippets.

@themodernlife
Created June 23, 2016 15:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save themodernlife/28cc4842c293d554f670abffad0aa8fa to your computer and use it in GitHub Desktop.
Save themodernlife/28cc4842c293d554f670abffad0aa8fa to your computer and use it in GitHub Desktop.
class AddingParser(val input: ParserInput) extends Parser {
def num = rule {
capture(oneOrMore(CharPredicate.Digit)) ~> (_.toInt)
}
def root = rule {
push(0) ~ zeroOrMore(num ~> ((_: Int) + _)).separatedBy(" ") ~ EOI
}
}
class XyzTest extends FlatSpec with Matchers {
"A parser" should "foldLeft" in {
val ret1 = new AddingParser("").root.run()
val ret2 = new AddingParser("1 2 3").root.run()
val ret3 = new AddingParser("x").root.run()
ret1 should be(Success(0))
ret2 should be(Success(6))
ret3 should be('failure)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment