From 2f02cdaaf3674de490bed30e795f778180329dd9 Mon Sep 17 00:00:00 2001 From: Daniel Capo Sobral Date: Wed, 2 Nov 2011 18:13:24 -0200 Subject: Add filter/withFilter method to Parser Complement map and flatMap when used in for comprehensions. This is required when pattern matching is used on the result of the generators. It is implemented through a new filterWithError method on ParseResult. --- test/files/run/parserFilter.check | 9 +++++++++ test/files/run/parserFilter.scala | 15 +++++++++++++++ test/files/run/parserForFilter.check | 1 + test/files/run/parserForFilter.scala | 12 ++++++++++++ 4 files changed, 37 insertions(+) create mode 100644 test/files/run/parserFilter.check create mode 100644 test/files/run/parserFilter.scala create mode 100644 test/files/run/parserForFilter.check create mode 100644 test/files/run/parserForFilter.scala (limited to 'test/files/run') diff --git a/test/files/run/parserFilter.check b/test/files/run/parserFilter.check new file mode 100644 index 0000000000..be04454426 --- /dev/null +++ b/test/files/run/parserFilter.check @@ -0,0 +1,9 @@ +[1.3] failure: Input doesn't match filter: false + +if false + ^ +[1.1] failure: Input doesn't match filter: not + +not true +^ +[1.8] parsed: (if~true) diff --git a/test/files/run/parserFilter.scala b/test/files/run/parserFilter.scala new file mode 100644 index 0000000000..d007d441f4 --- /dev/null +++ b/test/files/run/parserFilter.scala @@ -0,0 +1,15 @@ +object Test extends scala.util.parsing.combinator.RegexParsers { + val keywords = Set("if", "false") + def word: Parser[String] = "\\w+".r + + def keyword: Parser[String] = word filter (keywords.contains) + def ident: Parser[String] = word filter(!keywords.contains(_)) + + def test = keyword ~ ident + + def main(args: Array[String]) { + println(parseAll(test, "if false")) + println(parseAll(test, "not true")) + println(parseAll(test, "if true")) + } +} diff --git a/test/files/run/parserForFilter.check b/test/files/run/parserForFilter.check new file mode 100644 index 0000000000..a53c147719 --- /dev/null +++ b/test/files/run/parserForFilter.check @@ -0,0 +1 @@ +[1.13] parsed: (second,first) diff --git a/test/files/run/parserForFilter.scala b/test/files/run/parserForFilter.scala new file mode 100644 index 0000000000..1bc44f8033 --- /dev/null +++ b/test/files/run/parserForFilter.scala @@ -0,0 +1,12 @@ +object Test extends scala.util.parsing.combinator.RegexParsers { + def word: Parser[String] = "\\w+".r + + def twoWords = for { + (a ~ b) <- word ~ word + } yield (b, a) + + def main(args: Array[String]) { + println(parseAll(twoWords, "first second")) + } +} + -- cgit v1.2.3