implicit final class ErrorMethods[P, +A] extends AnyRef
This class exposes helpful combinators that are specialised for generating more helpful errors messages.
This extension class operates on values that are convertible to parsers. It enables the use of error combinators, which can be used for data validation, error annotation, or immediate failing.
- P
the type of base value that this class is used on (the conversion to
Parsley
) is summoned automatically.
- Source
- combinator.scala
- Version
3.0.0
- Grouped
- Alphabetic
- By Inheritance
- ErrorMethods
- AnyRef
- Any
- Hide All
- Show All
- Public
- Protected
Instance Constructors
- new ErrorMethods(p: P)(implicit con: (P) => Parsley[A])
This constructor should not be called manually, it is designed to be used via Scala's implicit resolution.
This constructor should not be called manually, it is designed to be used via Scala's implicit resolution.
- p
the value that this class is enabling methods on.
- con
a conversion that allows values convertible to parsers to be used.
Value Members
- final def !=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- final def ##: Int
- Definition Classes
- AnyRef → Any
- final def ==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- def ?(item: String): Parsley[A]
This combinator changes the expected component of any errors generated by this parser.
This combinator changes the expected component of any errors generated by this parser.
This is just an alias for the
label
combinator.Known as
<?>
in Haskell.- Since
3.0.0
- See also
- final def asInstanceOf[T0]: T0
- Definition Classes
- Any
- def clone(): AnyRef
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.CloneNotSupportedException]) @native()
- def collectMsg[B](msggen: (A) => Seq[String])(pf: PartialFunction[A, B]): Parsley[B]
This combinator applies a partial function
pf
to the result of this parser if its result is defined forpf
, failing if it is not.This combinator applies a partial function
pf
to the result of this parser if its result is defined forpf
, failing if it is not.First, parse this parser. If it succeeds, test whether its result
x
is in the domain of the partial functionpf
. If it is defined forpf
, returnpf(x)
. Otherwise, if the result was undefined then fail producing a specialised error message withmsggen(x)
. Equivalent to aguardAgainst
followed by amap
.- msggen
a function that generates the error messages to use if the filtering fails.
- pf
the partial function used to both filter the result of this parser and transform it.
- returns
a parser which returns the result of this parser applied to pf, if possible.
A good example of this combinator in use is for handling overflow in numeric literals.
val integer: Parsley[BigInt] = ... // this should be amended/entrenched for best results val int16: Parsley[Short] = integer.collectMsg(n => Seq(s"integer literal $n is not within the range -2^16 to +2^16-1")) { case x if x >= Short.MinValue && x <= Short.MaxValue => x.toShort }
- Since
4.0.0
- Note
when this combinator fails (and not this parser itself), it will generate errors rooted at the start of the parse (as if
,amend
had been used) and the caret will span the entire successful parse of this parser.implemented in terms of
collectWith
.- See also
collect
, which is a basic version of this same combinator with no customised error message.guardAgainst
, which is similar tocollectMsg
, except it does not transform the data.
Example: - def collectMsg[B](msg0: String, msgs: String*)(pf: PartialFunction[A, B]): Parsley[B]
This combinator applies a partial function
pf
to the result of this parser if its result is defined forpf
, failing if it is not.This combinator applies a partial function
pf
to the result of this parser if its result is defined forpf
, failing if it is not.First, parse this parser. If it succeeds, test whether its result
x
is in the domain of the partial functionpf
. If it is defined forpf
, returnpf(x)
. Otherwise, if the result was undefined then fail producing a specialised error message withmsg
. Equivalent to aguardAgainst
(whosemsggen
ignores its argument) followed by amap
.- msg0
the first error message to use if the filtering fails.
- msgs
the remaining error messages to use if the filtering fails.
- pf
the partial function used to both filter the result of this parser and transform it.
- returns
a parser which returns the result of this parser applied to pf, if possible.
A good example of this combinator in use is for handling overflow in numeric literals.
val integer: Parsley[BigInt] = ... // this should be amended/entrenched for best results val int16: Parsley[Short] = integer.collectMsg("integer literal should within the range -2^16 to +2^16-1") { case x if x >= Short.MinValue && x <= Short.MaxValue => x.toShort }
- Since
3.0.0
- Note
when this combinator fails (and not this parser itself), it will generate errors rooted at the start of the parse (as if
,amend
had been used) and the caret will span the entire successful parse of this parser.implemented in terms of
collectWith
.- See also
collect
, which is a basic version of this same combinator with no customised error message.guardAgainst
, which is similar tocollectMsg
, except it does not transform the data.
Example: - def collectWith[B](errGen: ErrorGen[A])(pf: PartialFunction[A, B]): Parsley[B]
This combinator conditionally transforms the result of this parser with a given partial function, generating an error with the given error generator if the function is not defined on the result of this parser.
This combinator conditionally transforms the result of this parser with a given partial function, generating an error with the given error generator if the function is not defined on the result of this parser.
Like
collect
, except allows for the error message generated to be fine-tuned with respect to the parsers result and width of input consumed using anErrorGen
object.- errGen
how to generate error messages based on the result of this parser.
- pf
the partial function used to both filter the result of this parser and transform it.
- Since
4.4.0
- final def eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- def equals(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef → Any
- def explain(reason: String): Parsley[A]
This combinator adds a reason to error messages generated by this parser.
This combinator adds a reason to error messages generated by this parser.
When this parser fails having not observably* consumed input, this combinator adds
reason
to the error message, which should justify why the error occured. Unlike error labels, which may persist if more progress is made having not consumed input, reasons are not carried forward in the error message, and are lost.*a parser is said to observably consume input when error messages generated by a parser
p
occur at a deeper offset thanp
originally started at. While this sounds like it is the same as "having consumed input" for the purposes of backtracking, they are disjoint concepts:- in
attempt(p)
,p
can observably consume input even though the wider parser does not consume input due to theattempt
. - in
amend(p)
,p
can consume input and may not backtrack even though the consumption is not observable in the error message due to theamend
.
- reason
the reason why a parser failed.
- returns
a parser that produces the given reason for failure if it fails.
- Since
3.0.0
- in
- def filterOut(pred: PartialFunction[A, String]): Parsley[A]
This combinator filters the result of this parser using the given partial-predicate, succeeding only when the predicate is undefined.
This combinator filters the result of this parser using the given partial-predicate, succeeding only when the predicate is undefined.
First, parse this parser. If it succeeds then take its result
x
and test ifpred.isDefinedAt(x)
is true. If it is false, the parser succeeds, returningx
. Otherwise,pred(x)
will yield a reasonreason
and the parser will fail withreason
provided to the generated error message à laexplain
.This is useful for performing data validation, but where a definitive reason can be given for the failure. In this instance, the rest of the error message is generated as normal, with the expected and unexpected components still given, along with any other generated reasons.
- pred
the predicate that is tested against the parser result, which also generates errors.
- returns
a parser that returns the result of this parser if it fails the predicate.
scala> import parsley.character.letter scala> val keywords = Set("if", "then", "else") scala> val ident = stringOfSome(letter).filterOut { case v if keywords.contains(v) => s"keyword $v cannot be an identifier" } scala> ident.parse("hello") val res0 = Success("hello") scala> ident.parse("if") val res1 = Failure(..)
- Since
3.0.0
- Note
implemented in terms of
,filterWith
.when this combinator fails (and not this parser itself), it will generate errors rooted at the start of the parse (as if
amend
had been used) and the caret will span the entire successful parse of this parser.- See also
filterNot
, which is a basic version of this same combinator with no customised reason.guardAgainst
, which is similar tofilterOut
, except it generates a specialised error as opposed to just a reason.
Example: - def filterWith(errGen: ErrorGen[A])(pred: (A) => Boolean): Parsley[A]
This combinator filters the result of this parser with the given predicate, generating an error with the given error generator if the function returned
false
.This combinator filters the result of this parser with the given predicate, generating an error with the given error generator if the function returned
false
.Like
filter
, except allows for the error message generated to be fine-tuned with respect to the parsers result and width of input consumed using anErrorGen
object.- errGen
how to generate error messages based on the result of this parser.
- pred
the predicate that is tested against the parser result.
- Since
4.4.0
- def finalize(): Unit
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.Throwable])
- final def getClass(): Class[_ <: AnyRef]
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
- def guardAgainst(pred: PartialFunction[A, Seq[String]]): Parsley[A]
This combinator filters the result of this parser using the given partial-predicate, succeeding only when the predicate is undefined.
This combinator filters the result of this parser using the given partial-predicate, succeeding only when the predicate is undefined.
First, parse this parser. If it succeeds then take its result
x
and test ofpred.isDefinedAt(x)
is true. If it is false, the parser succeeds, returningx
. Otherwisepred(x)
will yield an error messagemsg
and the parser will fail, producing a specialised error only consisting of the messagemsg
à lafail
.This is useful for performing data validation, but where failure is not tied to the grammar but some other property of the results. For instance, with the identifier example given for
filterOut
, it is reasonable to suggest that an identifier was expected, and a keyword is not a valid identifier: i.e. these components still make sense. WhereguardAgainst
shines, however, is in scenarios where the expected alternatives, or the unexpected component itself distract from the cause of the error, or are irrelevant in some way. This might be becauseguardAgainst
is checking some property of the data that is possible to encode in the grammar, but otherwise impractical, either because it is hard to maintain or generates poor error messages for the user.- pred
the predicate that is tested against the parser result, which also generates errors.
- returns
a parser that returns the result of this parser if it fails the predicate.
Suppose we are parsing a data-format for graphs, and a restriction has been placed that ensures that the numeric identifiers of each declared node must be ordered. This has, for whatever reason, been specified as a syntactic property of the data. This is possible to encode using context-sensitive parsing (since each new node can only be parsed according to the previous one), but is fairly difficult and impractical. Instead, when all the declarations have been read, a
guardAgainst
can be used to prevent mis-ordering:val node = integer val nodes = many(node).guardAgainst { case ns if ns.nonEmpty && ns.zip(ns.tail).exists { case (x, y) => x == y } => val Some((x, _)) = ns.zip(ns.tail).find { case (x, y) => x == y } Seq(s"node $x has been declared twice") case ns if ns.nonEmpty && ns.zip(ns.tail).exists { case (x, y) => x > y } => val Some((x, y)) = ns.zip(ns.tail).find { case (x, y) => x > y } Seq(s"nodes $x and $y are declared in the wrong order", "all nodes should be ordered") }
- Since
4.0.0
- Note
when this combinator fails (and not this parser itself), it will generate errors rooted at the start of the parse (as if
,amend
had been used) and the caret will span the entire successful parse of this parser.implemented in terms of
filterWith
.- See also
filterNot
, which is a basic version of this same combinator with no customised error message.filterOut
, which is similar toguardAgainst
, except it generates a reason for failure and not a specialised error.collectMsg
, which is similar toguardAgainst
, but can also transform the data on success.
Example: - def hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
- def hide: Parsley[A]
This combinator hides the expected component of errors generated by this parser.
This combinator hides the expected component of errors generated by this parser.
When this parser fails having not observably* consumed input, this combinator hides any error labels assigned to the expected item by any
label
combinators, or indeed the base raw labels produced by the input consuming combinators themselves.This can be useful, say, for hiding whitespace labels, which are not normally useful information to include in an error message for whitespace insensitive grammars.
*a parser is said to observably consume input when error messages generated by a parser
p
occur at a deeper offset thanp
originally started at. While this sounds like it is the same as "having consumed input" for the purposes of backtracking, they are disjoint concepts:- in
attempt(p)
,p
can observably consume input even though the wider parser does not consume input due to theattempt
. - in
amend(p)
,p
can consume input and may not backtrack even though the consumption is not observable in the error message due to theamend
.
- returns
a parser that does not produce an expected component on failure.
- Since
3.0.0
- in
- final def isInstanceOf[T0]: Boolean
- Definition Classes
- Any
- def label(item: String, items: String*): Parsley[A]
This combinator changes the expected component of any errors generated by this parser.
This combinator changes the expected component of any errors generated by this parser.
When this parser fails having not observably* consumed input, the expected component of the generated error message is set to be the given
item
.*a parser is said to observably consume input when error messages generated by a parser
p
occur at a deeper offset thanp
originally started at. While this sounds like it is the same as "having consumed input" for the purposes of backtracking, they are disjoint concepts:- in
attempt(p)
,p
can observably consume input even though the wider parser does not consume input due to theattempt
. - in
amend(p)
,p
can consume input and may not backtrack even though the consumption is not observable in the error message due to theamend
.
- item
the name to give to the expected component of any qualifying errors.
- items
any further labels to assign to this parser.
- returns
a parser that expects
item
on failure.
- Since
3.0.0
- in
- def mapFilterWith[B](errGen: ErrorGen[A])(f: (A) => Option[B]): Parsley[B]
This combinator conditionally transforms the result of this parser with a given function, generating an error with the given error generator if the function returns
None
given the result of this parser.This combinator conditionally transforms the result of this parser with a given function, generating an error with the given error generator if the function returns
None
given the result of this parser.Like
mapFilter
, except allows for the error message generated to be fine-tuned with respect to the parsers result and width of input consumed using anErrorGen
object.- errGen
how to generate error messages based on the result of this parser.
- f
the function used to both filter the result of this parser and transform it.
- Since
4.4.0
- final def ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- final def notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
- final def notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
- final def synchronized[T0](arg0: => T0): T0
- Definition Classes
- AnyRef
- def toString(): String
- Definition Classes
- AnyRef → Any
- def unexpectedWhen(pred: PartialFunction[A, String]): Parsley[A]
This combinator filters the result of this parser using the given partial-predicate, succeeding only when the predicate is undefined.
This combinator filters the result of this parser using the given partial-predicate, succeeding only when the predicate is undefined.
First, parse this parser. If it succeeds then take its result
x
and test ifpred.isDefinedAt(x)
is true. If it is false, the parser succeeds, returningx
. Otherwise,pred(x)
will yield a unexpected label and the parser will fail usingunexpected
and that label.This is useful for performing data validation, but where a the failure results in the entire token being unexpected. In this instance, the rest of the error message is generated as normal, with the expected components still given, along with any generated reasons.
- pred
the predicate that is tested against the parser result, which also generates errors.
- returns
a parser that returns the result of this parser if it fails the predicate.
scala> import parsley.character.letter scala> val keywords = Set("if", "then", "else") scala> val ident = stringOfSome(letter).unexpectedWhen { case v if keywords.contains(v) => s"keyword $v" } scala> ident.parse("hello") val res0 = Success("hello") scala> ident.parse("if") val res1 = Failure(..)
- Since
3.0.0
- Note
when this combinator fails (and not this parser itself), it will generate errors rooted at the start of the parse (as if
,amend
had been used) and the caret will span the entire successful parse of this parser.implemented in terms of
filterWith
.- See also
filterNot
, which is a basic version of this same combinator with no unexpected message.filterOut
, which is a variant that produces a reason for failure as opposed to an unexpected message.guardAgainst
, which is similar tounexpectedWhen
, except it generates a specialised error instead.unexpectedWithReasonWhen
, which is similar, but also has a reason associated.
Example: - def unexpectedWithReasonWhen(pred: PartialFunction[A, (String, String)]): Parsley[A]
This combinator filters the result of this parser using the given partial-predicate, succeeding only when the predicate is undefined.
This combinator filters the result of this parser using the given partial-predicate, succeeding only when the predicate is undefined.
First, parse this parser. If it succeeds then take its result
x
and test ifpred.isDefinedAt(x)
is true. If it is false, the parser succeeds, returningx
. Otherwise,pred(x)
will yield a unexpected label and the parser will fail usingunexpected
and that label as well as a reason.This is useful for performing data validation, but where a the failure results in the entire token being unexpected. In this instance, the rest of the error message is generated as normal, with the expected components still given, along with any generated reasons.
- pred
the predicate that is tested against the parser result, which also generates errors.
- returns
a parser that returns the result of this parser if it fails the predicate.
scala> import parsley.character.letter scala> val keywords = Set("if", "then", "else") scala> val ident = stringOfSome(letter).unexpectedWhenWithReason { case v if keywords.contains(v) => (s"keyword $v", "keywords cannot be identifiers") } scala> ident.parse("hello") val res0 = Success("hello") scala> ident.parse("if") val res1 = Failure(..)
- Since
4.2.0
- Note
implemented in terms of
filterWith
.- See also
filterNot
, which is a basic version of this same combinator with no unexpected message or reason.filterOut
, which is a variant that just produces a reason for failure with no unexpected message.guardAgainst
, which is similar tounexpectedWhen
, except it generates a specialised error instead.unexpectedWhen
, which is similar, but with no associated reason.
Example: - final def wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException]) @native()
Deprecated Value Members
- def !(msggen: (A) => String): Parsley[Nothing]
This combinator parses this parser and then fails, using the result of this parser to customise the error message.
This combinator parses this parser and then fails, using the result of this parser to customise the error message.
Similar to
fail
, but first parses this parser: if it succeeded, then its resultx
is used to form the error message for thefail
combinator by callingmsggen(x)
. If this parser fails, however, its error message will be generated instead.- msggen
the generator function for error message, creating a message based on the result of this parser.
- returns
a parser that always fails, with the given generator used to produce the error message if this parser succeeded.
- Annotations
- @deprecated
- Deprecated
(Since version 4.2.0) This combinator will be removed in 5.0.0, without direct replacement
- Note
this combinator will generate error messages rooted at the start of the previously successful parse of this parser, but only in terms of their position: the actual error is generated at the end of the parse, which means it takes priority over sibling errors. This is because the error concerns the whole parse (for caret) and morally starts where this parser started (as it caused the failure), however, if it had full
amend
-like behaviour these errors would often disappear.
- def unexpected(msggen: (A) => String): Parsley[Nothing]
This combinator parses this parser and then fails, using the result of this parser to customise the unexpected component of the error message.
This combinator parses this parser and then fails, using the result of this parser to customise the unexpected component of the error message.
Similar to
unexpected
, but first parses this parser: if it succeeded, then its resultx
is used to form the unexpected component of the generated error by callingmsggen(x)
. If this parser fails, however, its error message will be returned untouched.- msggen
the generator function for error message, creating a message based on the result of this parser.
- returns
a parser that always fails, with the given generator used to produce an unexpected message if this parser succeeded.
- Annotations
- @deprecated
- Deprecated
(Since version 4.2.0) This combinator will be removed in 5.0.0, without direct replacement
- Since
4.2.0
- Note
this combinator will generate error messages rooted at the start of the previously successful parse of this parser, but only in terms of their position: the actual error is generated at the end of the parse, which means it takes priority over sibling errors. This is because the error concerns the whole parse (for caret) and morally starts where this parser started (as it caused the failure), however, if it had full
amend
-like behaviour these errors would often disappear.
Error Enrichment Combinators
These combinators add additional information - or refine the existing information within - to an error message that has been generated within the scope of the parser they have been called on. These are a very basic, but effective, way of improving the quality of error messages generated by Parsley.
Filtering Combinators
These combinators perform filtering on a parser, with particular emphasis on generating meaningful
error messages if the filtering fails. This is particularly useful for data validation within the
parser, as very instructive error messages describing what went wrong can be generated. These combinators
often filter using a PartialFunction
: this may be because they combine filtering with mapping (in which
case, the error message is provided separately), or the function may produce a String
.
In these cases, the partial function is producing the error messages: if the input to the function is
defined, this means that it is invalid and the filtering will fail using the message obtained from the
succesful partial function invocation.
Generic Filtering Combinators
This combinators generalise the combinators from above, which are all special cases of them. Each of these
takes the characteristic predicate or function of the regular variants, but takes an errGen
object that
can be used to fine-tune the error messages. These offer some flexiblity not offered by the specialised
filtering combinators, but are a little more verbose to use.
Failure Combinators
These combinator immediately fail the parser, with a more bespoke message.
This is the documentation for Parsley.
Package structure
The parsley package contains the
Parsley
class, as well as theResult
,Success
, andFailure
types. In addition to these, it also contains the following packages and "modules" (a module is defined as being an object which mocks a package):parsley.Parsley
contains the bulk of the core "function-style" combinators.parsley.combinator
contains many helpful combinators that simplify some common parser patterns.parsley.character
contains the combinators needed to read characters and strings, as well as combinators to match specific sub-sets of characters.parsley.debug
contains debugging combinators, helpful for identifying faults in parsers.parsley.extension
contains syntactic sugar combinators exposed as implicit classes.parsley.io
contains extension methods to run parsers with input sourced from IO sources.parsley.expr
contains the following sub modules:parsley.expr.chain
contains combinators used in expression parsingparsley.expr.precedence
is a builder for expression parsers built on a precedence table.parsley.expr.infix
contains combinators used in expression parsing, but with more permissive types than their equivalents inchain
.parsley.expr.mixed
contains combinators that can be used for expression parsing, but where different fixities may be mixed on the same level: this is rare in practice.parsley.implicits
contains several implicits to add syntactic sugar to the combinators. These are sub-categorised into the following sub modules:parsley.implicits.character
contains implicits to allow you to use character and string literals as parsers.parsley.implicits.combinator
contains implicits related to combinators, such as the ability to make any parser into aParsley[Unit]
automatically.parsley.implicits.lift
enables postfix application of the lift combinator onto a function (or value).parsley.implicits.zipped
enables boths a reversed form of lift where the function appears on the right and is applied on a tuple (useful when type inference has failed) as well as a.zipped
method for building tuples out of several combinators.parsley.errors
contains modules to deal with error messages, their refinement and generation.parsley.errors.combinator
provides combinators that can be used to either produce more detailed errors as well as refine existing errors.parsley.errors.tokenextractors
provides mixins for common token extraction strategies during error message generation: these can be used to avoid implementingunexpectedToken
in theErrorBuilder
.parsley.lift
contains functions which lift functions that work on regular types to those which now combine the results of parsers returning those same types. these are ubiquitous.parsley.ap
contains functions which allow for the application of a parser returning a function to several parsers returning each of the argument types.parsley.registers
contains combinators that interact with the context-sensitive functionality in the form of registers.parsley.token
contains theLexer
class that provides a host of helpful lexing combinators when provided with the description of a language.parsley.position
contains parsers for extracting position information.parsley.genericbridges
contains some basic implementations of the Parser Bridge pattern (see Design Patterns for Parser Combinators in Scala, or the parsley wiki): these can be used before more specialised generic bridge traits can be constructed.