package errors
This package contains various functionality relating to the generation and formatting of error messages.
In particular, it includes a collection of combinators for improving error messages within the parser,
including labelling and providing additional information. It also contains combinators that can be used
to valid data produced by a parser, to ensure it conforms to expected invariances, producing good quality
error messages if this is not the case. Finally, this package contains ways of changing the formatting
of error messages: this can either be changing how the default String
-based errors are formatted, or
by injectiing Parsley's errors into a custom error object.
- Source
- package.scala
- Grouped
- Alphabetic
- By Inheritance
- errors
- AnyRef
- Any
- Hide All
- Show All
- Public
- All
Type Members
-
abstract
class
DefaultErrorBuilder extends ErrorBuilder[String]
This class us used to build Parsley's default error messages.
This class us used to build Parsley's default error messages.
While it compiles with the
ErrorBuilder
typeclass, it should not be considered a stable contract: the formatting can be changed at any time and without notice. The API, however, will remain stable.- Since
3.0.0
- Note
this class is abstract as it does not implement
unexpectedToken
: when creating an instance mix-in an appropriate token extractor fromparsley.errors.tokenextractors
.
-
trait
ErrorBuilder[+Err] extends AnyRef
This typeclass specifies how to construct an error from a parser as a specified type.
This typeclass specifies how to construct an error from a parser as a specified type.
An instance of this trait is required when calling
parse
(or similar). By default, Parsley defines its own instance forErrorBuilder[String]
found in theErrorBuilder
companion object.To implement this trait, a number of methods must be defined, as well the representation types for a variety of different components; the relation between the various methods is closely linked to the types that they both produce and consume. To only change the basics of formatting without having to define the entire instance, inherit from
DefaultErrorBuilder
: this will mean, however, that the representation types cannot be overriden.How an Error is Structured
There are two kinds of error messages that are generated by Parsley: Specialized and Vanilla. These are produced by different combinators and can be merged with other errors of the same type if both errors appear at the same offset. However, Specialized errors will take precedence over Vanilla errors if they appear at the same offset. The most common form of error is the Vanilla variant, which is generated by most combinators, except for some in
errors.combinator
.Both types of error share some common structure, namely:
- The error preamble, which has the file and the position.
- The content lines, the specifics of which differ between the two types of error.
- The context lines, which has the surrounding lines of input for contextualisation.
Vanilla Errors
There are three kinds of content line found in a Vanilla error:
- Unexpected info: this contains information about the kind of token that caused the error.
- Expected info: this contains the information about what kinds of token could have avoided the error.
- Reasons: these are the bespoke reasons that an error has occurred (as generated by
explain
).
There can be at most one unexpected line, at most one expected line, and zero or more reasons. Both of the unexpected and expected info are built up of error items, which are either: the end of input, a named token, raw input taken from the parser definition. These can all be constructed separately.
The overall structure of a Vanilla error is given in the following diagram:
┌───────────────────────────────────────────────────────────────────────┐ │ Vanilla Error │ │ ┌────────────────┐◄──────── position │ │ source │ │ │ │ │ │ line col│ │ │ ▼ │ │ ││ │ │ ┌─────┐ │ ▼ ▼│ end of input │ │ In foo.txt (line 1, column 5): │ │ │ ┌─────────────────────┐ │ │ │unexpected ─────►│ │ │ ┌───── expected │ │ │ ┌──────────┐ ◄──────────┘ │ │ │ unexpected end of input ▼ │ │ ┌──────────────────────────────────────┐ │ │ expected "(", "negate", digit, or letter │ │ │ └──────┘ └───┘ └────┘ ◄────── named│ │ │ ▲ └──────────┘ │ │ │ │ │ │ │ │ │ raw │ │ │ └─────────────────┬───────────┘ │ │ '-' is a binary operator │ │ │ └──────────────────────┘ │ │ │ ┌──────┐ ▲ │ │ │ │>3+4- │ │ expected items │ │ │ ^│ │ │ │ └──────┘ └───────────────── reason │ │ ▲ │ │ │ │ │ line info │ └───────────────────────────────────────────────────────────────────────┘
Specialized Errors
There is only one kind of content found in a Specialized error: a message. These are completely free-form, and are generated by the
fail
combinator, as well as its derived combinators. There can be one or more messages in a Specialized error.The overall structure of a Specialized error is given in the following diagram:
┌───────────────────────────────────────────────────────────────────────┐ │ Specialized Error │ │ ┌────────────────┐◄──────── position │ │ source │ │ │ │ │ │ line col │ │ ▼ │ │ │ │ │ ┌─────┐ │ ▼ ▼ │ │ In foo.txt (line 1, column 5): │ │ │ │ ┌───► something went wrong │ │ │ │ │ message ──┼───► it looks like a binary operator has no argument │ │ │ │ │ └───► '-' is a binary operator │ │ ┌──────┐ │ │ │>3+4- │ │ │ │ ^│ │ │ └──────┘ │ │ ▲ │ │ │ │ │ line info │ └───────────────────────────────────────────────────────────────────────┘
- Err
The final result type of the error message
- Since
3.0.0
-
sealed abstract
class
ErrorGen[-A] extends AnyRef
This class can be used to generate hand-tuned error messages without using
flatMap
.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.- Since
4.4.0
-
abstract
class
SpecializedGen[-A] extends ErrorGen[A]
An error generate for Specialized errors, which can tune the freeform messages of the error.
An error generate for Specialized errors, which can tune the freeform messages of the error.
- Since
4.4.0
-
sealed abstract
class
Token extends AnyRef
This class represents an extracted token returned by
unexpectedToken
inErrorBuilder
.This class represents an extracted token returned by
unexpectedToken
inErrorBuilder
.There is deliberately no analogue for
EndOfInput
because we guarantee that non-empty residual input is provided to token extraction.- Since
4.0.0
-
class
VanillaGen[-A] extends ErrorGen[A]
An error generator for Vanilla errors, which can tune the unexpected message and a generated reason.
An error generator for Vanilla errors, which can tune the unexpected message and a generated reason.
- Since
4.4.0
Value Members
-
object
DefaultErrorBuilder
Helper functions used to build the
DefaultErrorBuilder
error messages.Helper functions used to build the
DefaultErrorBuilder
error messages.- Since
4.3.0
-
object
ErrorBuilder
Contains the default instance for the
ErrorBuilder
typeclass, which will be automatically available without import. -
object
Token
This object contains the sub-types of
Token
.This object contains the sub-types of
Token
.- Since
4.0.0
-
object
VanillaGen
This object contain the
UnexpectedItem
classes, needed to define theunexpected
component ofVanillaGen
.This object contain the
UnexpectedItem
classes, needed to define theunexpected
component ofVanillaGen
.- Since
4.4.0
-
object
combinator
This module contains combinators that can be used to directly influence error messages of parsers.
This module contains combinators that can be used to directly influence error messages of parsers.
Error messages are, by default, not particularly descriptive. However, the combinators in this module can be used to improve the generation of error messages by providing labels for expected items, explanations for why things went wrong, custom error messages, custom unexpected error messages, as well as correcting the offsets that error messages actually occurred at.
- Since
3.0.0
-
object
patterns
This module contains combinators that help facilitate the error message generational patterns Verified Errors and Preventative Errors.
This module contains combinators that help facilitate the error message generational patterns Verified Errors and Preventative Errors.
In particular, exposes an extension class
VerifiedErrors
that facilitates creating verified errors in many different formats.- Since
4.2.0
Error Combinators
These are combinators associated with influencing how error messages are generated during a parse.
Error Formatting and Construction
These classes control how error messages are constructed by Parsley - via the ErrorBuilder
typeclass.
Unexpected Token Description
These are classes used to describe unexpected tokens that are extracted from residual input after a failed parse.
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.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.syntax
contains several implicits to add syntactic sugar to the combinators. These are sub-categorised into the following sub modules:parsley.syntax.character
contains implicits to allow you to use character and string literals as parsers.parsley.syntax.lift
enables postfix application of the lift combinator onto a function (or value).parsley.syntax.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.syntax.extension
contains syntactic sugar combinators exposed as implicit classes.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.state
contains combinators that interact with the context-sensitive functionality in the form of state.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.generic
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.