summaryrefslogtreecommitdiff
path: root/test/files/run
diff options
context:
space:
mode:
authorDaniel Capo Sobral <dcs@dcs-132-CK-NF79.(none)>2011-11-02 18:13:24 -0200
committerPaul Phillips <paulp@improving.org>2011-12-02 14:16:00 -0800
commit2f02cdaaf3674de490bed30e795f778180329dd9 (patch)
tree131690db2b5da5b84f56338d98960ece6fa8681c /test/files/run
parent8be1cfc777352a2e65344ebc1413dd9398379081 (diff)
downloadscala-2f02cdaaf3674de490bed30e795f778180329dd9.tar.gz
scala-2f02cdaaf3674de490bed30e795f778180329dd9.tar.bz2
scala-2f02cdaaf3674de490bed30e795f778180329dd9.zip
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.
Diffstat (limited to 'test/files/run')
-rw-r--r--test/files/run/parserFilter.check9
-rw-r--r--test/files/run/parserFilter.scala15
-rw-r--r--test/files/run/parserForFilter.check1
-rw-r--r--test/files/run/parserForFilter.scala12
4 files changed, 37 insertions, 0 deletions
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"))
+ }
+}
+