ErrorGen

parsley.errors.ErrorGen
sealed abstract class ErrorGen[-A]

This class can be used to generate hand-tuned error messages without using flatMap.

This class, and its subclasses, describe special primitives that can use the results of a previous parser to form an error message and then raise it. This is not something that is normally possible with raw combinators, without using flatMap, which is expensive.

Primarily, these are designed to be used with filterWith/verifiedWith/preventWith but can be used in other parsers as well. See the methods of the class for information.

Attributes

Since

4.4.0

Source
ErrorGen.scala
Graph
Supertypes
class Object
trait Matchable
class Any
Known subtypes
class SpecializedGen[A]
class VanillaGen[A]

Members list

Value members

Concrete methods

def adjustWidth(x: A, width: Int): Int

This method can be overridden to control how wide an error is based on the value and width that produces it.

This method can be overridden to control how wide an error is based on the value and width that produces it.

The width provides to this error generator likely comes directly from the span of the parser used to produce the required result. However, this may not be entirely accurate for how the user might want the error to be sized (perhaps there was whitespace, or the parser consumed more input than was necessary to pin-point the problem). In these cases, this method allows for custom logic to derive the actual width of the error message. By default, just returns the given width.

Attributes

Since

4.4.0

Source
ErrorGen.scala
final def apply(p: Parsley[(A, Int)]): Parsley[Nothing]

This combinator takes a given parser and raises an error based on its returned results.

This combinator takes a given parser and raises an error based on its returned results.

The given parser produces a value and a width, which are used to synthesise and raise an error message derived from the value with the given width. This is a safe way of using parser, since it ensures that the result of the given parser p is not optimised out. errGen(p) is similar to withWidth(p).flatMap { case (x, w) => failCombinator(...) }, in that it generates errors in a context-sensitive way. However, this is much more efficient than using the expensive flatMap, so it is provided as a primitive operation.

Attributes

Since

4.4.0

Source
ErrorGen.scala
final def parser: Parsley[((A, Int)) => Nothing]

This parser can be applied (postfix) to a parser returning a value and a width to generate an error message tailored to them.

This parser can be applied (postfix) to a parser returning a value and a width to generate an error message tailored to them.

This is not a generally safe operation to be performing, and should only be used within a combinator that is guaranteed to use its results. The optimiser is not aware that the results of the parser this will be applied to will actually be needed, and so may optimise them out. Using this parser inside an arm of select or branch, say, would be safe, because these combinators force the result of their condition to be generated, but p <**> this.parser is not generally safe without a use of impure to guard it. This is what apply accomplishes more safely.

Attributes

Since

4.4.0

Source
ErrorGen.scala