summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDaniel C. Sobral <dcsobral@gmail.com>2011-11-15 23:08:16 -0200
committerPaul Phillips <paulp@improving.org>2011-12-02 14:15:35 -0800
commit1b37a0b4f21834fb1d181f2b50da1728e49cf467 (patch)
treee63c4fae8d22e378f00b35259f1ae03eca7dc0c2 /src
parent8be1cfc777352a2e65344ebc1413dd9398379081 (diff)
downloadscala-1b37a0b4f21834fb1d181f2b50da1728e49cf467.tar.gz
scala-1b37a0b4f21834fb1d181f2b50da1728e49cf467.tar.bz2
scala-1b37a0b4f21834fb1d181f2b50da1728e49cf467.zip
Add withErrorMessage/withFailureMessage to Parser.
Can be used to reliably override the default no success messages.
Diffstat (limited to 'src')
-rw-r--r--src/library/scala/util/parsing/combinator/Parsers.scala56
1 files changed, 56 insertions, 0 deletions
diff --git a/src/library/scala/util/parsing/combinator/Parsers.scala b/src/library/scala/util/parsing/combinator/Parsers.scala
index e7ea9f598b..ad907f90e6 100644
--- a/src/library/scala/util/parsing/combinator/Parsers.scala
+++ b/src/library/scala/util/parsing/combinator/Parsers.scala
@@ -443,6 +443,62 @@ trait Parsers {
* @return opt(this)
*/
def ? = opt(this)
+
+ /** Changes the failure message produced by a parser.
+ *
+ * This doesn't change the behavior of a parser on neither
+ * success nor error, just on failure. The semantics are
+ * slightly different than those obtained by doing `| failure(msg)`,
+ * in that the message produced by this method will always
+ * replace the message produced, which is not guaranteed
+ * by that idiom.
+ *
+ * For example, parser `p` below will always produce the
+ * designated failure message, while `q` will not produce
+ * it if `sign` is parsed but `number` is not.
+ *
+ * {{{
+ * def p = sign.? ~ number withFailureMessage "Number expected!"
+ * def q = sign.? ~ number | failure("Number expected!")
+ * }}}
+ *
+ * @param msg The message that will replace the default failure message.
+ * @return A parser with the same properties and different failure message.
+ */
+ def withFailureMessage(msg: String) = Parser{ in =>
+ this(in) match {
+ case Failure(_, next) => Failure(msg, next)
+ case other => other
+ }
+ }
+
+ /** Changes the error message produced by a parser.
+ *
+ * This doesn't change the behavior of a parser on neither
+ * success nor failure, just on error. The semantics are
+ * slightly different than those obtained by doing `| error(msg)`,
+ * in that the message produced by this method will always
+ * replace the message produced, which is not guaranteed
+ * by that idiom.
+ *
+ * For example, parser `p` below will always produce the
+ * designated error message, while `q` will not produce
+ * it if `sign` is parsed but `number` is not.
+ *
+ * {{{
+ * def p = sign.? ~ number withErrorMessage "Number expected!"
+ * def q = sign.? ~ number | error("Number expected!")
+ * }}}
+ *
+ * @param msg The message that will replace the default error message.
+ * @return A parser with the same properties and different error message.
+ */
+ def withErrorMessage(msg: String) = Parser{ in =>
+ this(in) match {
+ case Error(_, next) => Error(msg, next)
+ case other => other
+ }
+ }
}
/** Wrap a parser so that its failures become errors (the `|` combinator