Skip to content

Add withErrorMessage and withFailureMessage to Parsers.Parser, #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions src/library/scala/util/parsing/combinator/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions test/files/run/parserNoSuccessMessage.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[1.2] failure: string matching regex `\d+' expected but `x' found

-x
^
[1.1] failure: string matching regex `\d+' expected but `x' found

x
^
[1.3] parsed: (Some(-)~5)
[1.2] parsed: (None~5)
[1.2] error: Number expected!

-x
^
[1.1] error: Number expected!

x
^
[1.3] parsed: (Some(-)~5)
[1.2] parsed: (None~5)
19 changes: 19 additions & 0 deletions test/files/run/parserNoSuccessMessage.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
object Test extends scala.util.parsing.combinator.RegexParsers {
def sign = "-"
def number = "\\d+".r
def p = sign.? ~ number withErrorMessage "Number expected!"
def q = sign.? ~! number withErrorMessage "Number expected!"

def main(args: Array[String]) {
println(parseAll(p, "-x"))
println(parseAll(p, "x"))
println(parseAll(p, "-5"))
println(parseAll(p, "5"))
println(parseAll(q, "-x"))
println(parseAll(q, "x"))
println(parseAll(q, "-5"))
println(parseAll(q, "5"))
}
}