gigaparsec-0.3.0.0: Refreshed parsec-style library for compatibility with Scala parsley
LicenseBSD-3-Clause
MaintainerJamie Willis, Gigaparsec Maintainers
Stabilityexperimental
Safe HaskellSafe
LanguageHaskell2010

Text.Gigaparsec.Token.Errors

Description

This module contains the relevant functionality for configuring the error messages generated by the parsers provided by the Lexer.

Since: 0.2.2.0

Synopsis

Error Configuration

This is the main type that defines the configuration for errors from the Lexer.

data ErrorConfig Source #

An ErrorConfig specifies how errors should be produced by the Lexer.

The Lexer is set up to produce a variety of different errors via label-ing, explain-ing, and filter-ing, and some applications of the Verified and Preventative error patterns. The exact content of those errors can be configured here. Errors can be suppressed or specified with different levels of detail, or even switching between vanilla or specialised errors.

A custom ErrorConfig should be created by extending the defaultErrorConfig with record updates to override the relevant default fields. Not configuring something does not mean it will not appear in the message, but will mean it uses the underlying base errors.

Labelling and Explanation Configuration

Labels and Explanations make parser errors more descriptive, by giving a name to the parser that failed, or providing a reason a particular parser failed. These help make error messages more descriptive than the usual 'unexpected symbol' messages.

LabelConfigurable types describe errors that can provide labels upon failure, ExplainConfigurable types those that can providing reaons for failure, and LabelWithExplainConfigurable types those that can provide both. These configs are used by the ErrorConfig, whose fields include combinators that configure both labels and/or explains for simple description configurations.

Label Configurations

Labels provide names to a parser to be used upon failure. A LabelConfig or LabelConfigurable type is able to produce these sorts of errors.

data LabelConfig Source #

This type configures errors that make labels.

class LabelConfigurable config where Source #

A type config is ExplainConfigurable when it is able to configure errors that make labels.

This means config is able to behave like a LabelConfig

Methods

label Source #

Arguments

:: Set String

The labels to produce.

-> config 

The configuration produces the labels in the given set, which should not be empty.

hidden :: config Source #

Configure a label by stating it must be hidden.

Explain Configurations

Explains provide a reason that a parser has failed. An ExplainConfig or ExplainConfigurable type is able to produce these sorts of errors.

data ExplainConfig Source #

This type configures errors that give reasons.

class ExplainConfigurable config where Source #

A type config is ExplainConfigurable when it is able to configure an error which provides a reason.

This means config is able to behave like an ExplainConfig

Methods

reason Source #

Arguments

:: String

The reason a parser failed.

-> config 

The error should be displayed using the given reason.

Label-with-Explain Configurations

LabelWithExplainConfig combines LabelConfig and ExplainConfig, describing configs that are able to give labels and/or reasons to errors.

LabelWithExplainConfigurable types are those able to produce an error providing both labels and a reason.

class LabelWithExplainConfigurable config where Source #

A type config is LabelWithExplainConfigurable when it is able to configure an error which provides both a reason and labels.

Methods

labelAndReason Source #

Arguments

:: Set String

The labels to produce

-> String

The reason for the error.

-> config 

The configuration produces the labels in the given set, and provides the given reason.

No-special-error-generating Configs

Sometimes it is best to defer to the default errors, instead of generating specialised ones. Configs that allow this are NotConfigurable.

class NotConfigurable config where Source #

A type config is NotConfigurable when it is able to specify that no special error should be generated, and instead use default errors.

Methods

notConfigured :: config Source #

No special error should be generated, and default errors should be used instead.

Numeric Errors

These control the errors generated with the numeric (NumericDesc) component of the Lexer.

labelNumericBreakChar :: ErrorConfig -> LabelWithExplainConfig Source #

How a numeric break character should (like _) be referred to or explained within an error.

labelIntegerUnsignedDecimal :: ErrorConfig -> Maybe Bits -> LabelWithExplainConfig Source #

How unsigned decimal integers (of a possibly given bit-width) should be referred to or explained within an error.

labelIntegerUnsignedHexadecimal :: ErrorConfig -> Maybe Bits -> LabelWithExplainConfig Source #

How unsigned hexadecimal integers (of a possibly given bit-width) should be referred to or explained within an error.

labelIntegerUnsignedOctal :: ErrorConfig -> Maybe Bits -> LabelWithExplainConfig Source #

How unsigned octal integers (of a possibly given bit-width) should be referred to or explained within an error.

labelIntegerUnsignedBinary :: ErrorConfig -> Maybe Bits -> LabelWithExplainConfig Source #

How unsigned binary integers (of a possibly given bit-width) should be referred to or explained within an error.

labelIntegerUnsignedNumber :: ErrorConfig -> Maybe Bits -> LabelWithExplainConfig Source #

How generic unsigned integers (of a possibly given bit-width) should be referred to or explained within an error.

labelIntegerSignedDecimal :: ErrorConfig -> Maybe Bits -> LabelWithExplainConfig Source #

How signed decimal integers (of a possibly given bit-width) should be referred to or explained within an error.

labelIntegerSignedHexadecimal :: ErrorConfig -> Maybe Bits -> LabelWithExplainConfig Source #

How signed hexadecimal integers (of a possibly given bit-width) should be referred to or explained within an error.

labelIntegerSignedOctal :: ErrorConfig -> Maybe Bits -> LabelWithExplainConfig Source #

How signed octal integers (of a possibly given bit-width) should be referred to or explained within an error.

labelIntegerSignedBinary :: ErrorConfig -> Maybe Bits -> LabelWithExplainConfig Source #

How signed binary integers (of a possibly given bit-width) should be referred to or explained within an error.

labelIntegerSignedNumber :: ErrorConfig -> Maybe Bits -> LabelWithExplainConfig Source #

How generic signed integers (of a possibly given bit-width) should be referred to or explained within an error.

labelIntegerDecimalEnd :: ErrorConfig -> LabelConfig Source #

How the fact that the end of a decimal integer literal is expected should be referred to within an error.

labelIntegerHexadecimalEnd :: ErrorConfig -> LabelConfig Source #

How the fact that the end of a hexadecimal integer literal is expected should be referred to within an error.

labelIntegerOctalEnd :: ErrorConfig -> LabelConfig Source #

How the fact that the end of an octal integer literal is expected should be referred to within an error.

labelIntegerBinaryEnd :: ErrorConfig -> LabelConfig Source #

How the fact that the end of a binary integer literal is expected should be referred to within an error.

labelIntegerNumberEnd :: ErrorConfig -> LabelConfig Source #

How the fact that the end of a generic integer literal is expected should be referred to within an error.

filterIntegerOutOfBounds :: ErrorConfig -> Integer -> Integer -> Int -> FilterConfig Integer Source #

Describes the content of the error when an integer literal is parsed and it is not within the required bit-width.

In filterIntegerOutOfBounds x y r:

  • x is the smallest value the integer could have taken,
  • y is the largest value the integer could have taken, and
  • r is the radix that the integer was parsed using

Bit-Widths

Some of the numeric errors can take into account the supposed bit-width of the parsed data.

Using Bits achieves this.

data Bits Source #

The bit-width of certain data.

This is used to help enforce parsers of bounded precision to only return types that can losslessly accomodate that precision.

Constructors

B8

8 bits of data

B16

16 bits of data

B32

32 bits of data

B64

64 bits of data

Name Errors

These control the errors generated with the names (NameDesc) component of the Lexer.

labelNameIdentifier :: ErrorConfig -> String Source #

How an identifier should be referred to in an error message.

labelNameOperator :: ErrorConfig -> String Source #

How a user-defined operator should be referred to in an error message.

unexpectedNameIllegalIdentifier :: ErrorConfig -> String -> String Source #

How an illegally parsed hard keyword should be referred to as an unexpected component.

unexpectedNameIllegalOperator :: ErrorConfig -> String -> String Source #

How an illegally parsed hard operator should be referred to as an unexpected component.

filterNameIllFormedIdentifier :: ErrorConfig -> FilterConfig String Source #

When parsing identifiers that are required to have specific start characters, how bad identifiers should be reported.

filterNameIllFormedOperator :: ErrorConfig -> FilterConfig String Source #

When parsing operators that are required to have specific start/end characters, how bad operators should be reported.

Text Errors

These control the errors generated with the text (TextDesc) component of the Lexer.

labelCharAscii :: ErrorConfig -> LabelWithExplainConfig Source #

How an ASCII character literal should be referred to or explained in error messages.

labelCharLatin1 :: ErrorConfig -> LabelWithExplainConfig Source #

How a Latin1 (extended ASCII) character literal should be referred to or explained in error messages.

labelCharUnicode :: ErrorConfig -> LabelWithExplainConfig Source #

How a UTF-16 character literal should be referred to or explained in error messages.

labelCharAsciiEnd :: ErrorConfig -> LabelConfig Source #

How the closing quote of an ASCII character literal should be referred to in error messages.

labelCharLatin1End :: ErrorConfig -> LabelConfig Source #

How the closing quote of a Latin1 (extended ASCII) character literal should be referred to in error messages.

labelCharUnicodeEnd :: ErrorConfig -> LabelConfig Source #

How the closing quote of a UTF-16 character literal should be referred to in error messages.

labelStringAscii :: ErrorConfig -> Bool -> Bool -> LabelWithExplainConfig Source #

How an ASCII-only string should literal be referred to or explained in error messages.

With the arguments labelStringAscii multi raw:

  • multi: whether this is for a multi-line string
  • raw: whether this is for a raw string

labelStringLatin1 :: ErrorConfig -> Bool -> Bool -> LabelWithExplainConfig Source #

How a Latin1-only string should literal be referred to or explained in error messages.

With the arguments labelStringLatin1 multi raw:

  • multi: whether this is for a multi-line string
  • raw: whether this is for a raw string

labelStringUnicode :: ErrorConfig -> Bool -> Bool -> LabelWithExplainConfig Source #

How a UTF-16-only string should literal be referred to or explained in error messages.

With the arguments labelStringUnicode multi raw:

  • multi: whether this is for a multi-line string
  • raw: whether this is for a raw string

labelStringAsciiEnd :: ErrorConfig -> Bool -> Bool -> LabelConfig Source #

How the closing quote(s) of an ASCII string literal should be referred to in error messages.

With the arguments labelStringAsciiEnd multi raw:

  • multi: whether this is for a multi-line string
  • raw: whether this is for a raw string

labelStringLatin1End :: ErrorConfig -> Bool -> Bool -> LabelConfig Source #

How the closing quote(s) of a Latin1 string literal should be referred to in error messages.

With the arguments labelStringLatin1End multi raw:

  • multi: whether this is for a multi-line string
  • raw: whether this is for a raw string

labelStringUnicodeEnd :: ErrorConfig -> Bool -> Bool -> LabelConfig Source #

How the closing quote(s) of a UTF-16 string literal should be referred to in error messages.

With the arguments labelStringUnicodeEnd multi raw:

  • multi: whether this is for a multi-line string
  • raw: whether this is for a raw string

labelStringCharacter :: ErrorConfig -> LabelConfig Source #

How general string characters should be referred to in error messages.

labelGraphicCharacter :: ErrorConfig -> LabelWithExplainConfig Source #

How a graphic character (a regular character in the literal) should be referred to or explained in error messages.

labelEscapeSequence :: ErrorConfig -> LabelWithExplainConfig Source #

How an escape sequence should be referred to or explained in error messages.

labelEscapeNumeric :: ErrorConfig -> Int -> LabelWithExplainConfig Source #

How a numeric escape sequence (after the opening character) should be referred to or explained in error messages.

In labelEscapeNumeric radix:

  • radix: the radix this specific configuration applies to.

labelEscapeNumericEnd :: ErrorConfig -> Char -> Int -> LabelWithExplainConfig Source #

How the end of a numeric escape sequence (after a prefix) should be referred to or explained in error messages.

In labelEscapeNumericEnd prefix radix:

  • prefix: the character that started this sequence
  • radix: the radix this specific configuration applies to.

labelEscapeEnd :: ErrorConfig -> LabelWithExplainConfig Source #

How the end of an escape sequence (anything past the opening character) should be referred to or explained within an error message.

labelStringEscapeEmpty :: ErrorConfig -> LabelConfig Source #

How zero-width escape characters should be referred to within error messages.

labelStringEscapeGap :: ErrorConfig -> LabelConfig Source #

How string gaps should be referred to within error messages.

labelStringEscapeGapEnd :: ErrorConfig -> LabelConfig Source #

How the end of a string gap (the closing slash) should be referred to within error messages.

Filtering Errors

These configs and combinators describe how to generate the filters to rule out specific parses. They can generate types of vanilla error or specialised errors.

Filtering Configs

data FilterConfig a Source #

Configures how filters should be used within the Lexer.

class BasicFilterConfigurable (config :: Type -> Type) where Source #

A type config is BasicFilterConfigurable when it is able to provide no error configuration, and instead defer to a regular filter.

Methods

basicFilter :: config a Source #

No error configuration for the filter is specified; a regular filter is used instead.

data VanillaFilterConfig a Source #

Specifies that only filters generating vanilla errors can be used.

class VanillaFilterConfigurable (config :: Type -> Type) where Source #

A type config is VanillaFilterConfigurable when it is able to generate vanilla errors.

This means config is able to behave like a VanillaFilterConfig

Methods

unexpected Source #

Arguments

:: (a -> String)

a function producing the unexpected label for the given value.

-> config a 

Ensure the filter generates a vanilla unexpected item for the given failing parse.

because Source #

Arguments

:: (a -> String)

a function producing the reason for the given value.

-> config a 

Ensure the filter generates a vanilla reason for the given failing parse.

unexpectedBecause Source #

Arguments

:: (a -> String)

reason, a function producing the reason for the given value.

-> (a -> String)

unexpected, a function producing the unexpected label for the given value.

-> config a 

Ensure the filter generates a vanilla unexpected item and a reason for the given failing parse.

data SpecializedFilterConfig a Source #

Specifies that only filters generating specialised errors can be used.

class SpecializedFilterConfigurable (config :: Type -> Type) where Source #

A type config is SpecializedFilterConfigurable when it is able to generate specialised errors.

This means config is able to behave like a SpecializedFilterConfig

Methods

specializedFilter Source #

Arguments

:: (a -> NonEmpty String)

message: a function producing the message for the given value.

-> config a 

Ensure the filter generates specialised messages for the given failing parse.

Filtering Combinators

filterCharNonAscii :: ErrorConfig -> VanillaFilterConfig Char Source #

When a non-ASCII character is found in a ASCII-only character literal, specifies how this should be reported.

filterCharNonLatin1 :: ErrorConfig -> VanillaFilterConfig Char Source #

When a non-Latin1 character is found in a Latin1-only character literal, specifies how this should be reported.

filterStringNonAscii :: ErrorConfig -> SpecializedFilterConfig String Source #

When a non-ASCII character is found in a ASCII-only string literal, specifies how this should be reported.

filterStringNonLatin1 :: ErrorConfig -> SpecializedFilterConfig String Source #

When a non-Latin1 character is found in a Latin1-only string literal, specifies how this should be reported.

filterEscapeCharRequiresExactDigits :: ErrorConfig -> Int -> NonEmpty Word -> SpecializedFilterConfig Word Source #

When a numeric escape sequence requires a specific number of digits but this was not successfully parsed, this describes how to report that error given the number of successfully parsed digits up this point.

In filterEscapeCharRequiresExactDigits radix needed:

  • radix: the radix used for this numeric escape sequence.
  • needed: the possible numbers of digits required.

filterEscapeCharNumericSequenceIllegal :: ErrorConfig -> Char -> Int -> SpecializedFilterConfig Integer Source #

When a numeric escape sequence is not legal, this describes how to report that error, given the original illegal character.

In filterEscapeCharNumericSequenceIllegal maxEscape radix:

  • maxEscape: the largest legal escape character.
  • radix: the radix used for this numeric escape sequence.

Verifying Bad Characters

These types and combinators help implement the Verified Error™ pattern for illegal string and literal characters.

Configs

These types help configure the Verified Error pattern for illegal string and character literal characters, used by verifiedCharBadCharsUsedInLiteral and verifiedStringBadCharsUsedInLiteral.

data VerifiedBadChars Source #

Configures what error should be generated when illegal characters in a string or character literal are parsable.

Instances

Instances details
Unverified VerifiedBadChars Source # 
Instance details

Defined in Text.Gigaparsec.Token.Errors

class Unverified config where Source #

A type config is Unverified when it can disable the verified error for bad characters: this may improve parsing performance slightly on the failure case.

Methods

unverified :: config Source #

A configuration which disables the verified error for bad characters.

Instances

Instances details
Unverified VerifiedBadChars Source # 
Instance details

Defined in Text.Gigaparsec.Token.Errors

Combinators

badCharsFail :: Map Char (NonEmpty String) -> VerifiedBadChars Source #

Makes "bad literal chars" generate a bunch of given messages in a specialised error. Requires a map from bad characters to their messages.

badCharsReason :: Map Char String -> VerifiedBadChars Source #

Makes "bad literal chars" generate a reason in a vanilla error. Requires a map from bad characters to their reasons.

verifiedCharBadCharsUsedInLiteral :: ErrorConfig -> VerifiedBadChars Source #

Character literals parse either graphic characters or escape characters.

verifiedStringBadCharsUsedInLiteral :: ErrorConfig -> VerifiedBadChars Source #

String literals parse either graphic characters or escape characters.

Symbol Errors

These control the errors generated with the symbol (SymbolDesc) component of the Lexer

labelSymbol :: ErrorConfig -> Map String LabelWithExplainConfig Source #

Gives names and/or reasons to symbols.

Symbols that do not appear in the map are assumed to be NotConfigurable.

labelSymbolEndOfKeyword :: ErrorConfig -> String -> String Source #

How the required end of a given keyword should be specified in an error.

labelSymbolEndOfOperator :: ErrorConfig -> String -> String Source #

How the required end of a given operator should be specified in an error.

Space Errors

These control the errors generated with the space (SpaceDesc) component of the Lexer.

labelSpaceEndOfLineComment :: ErrorConfig -> LabelWithExplainConfig Source #

How the end of a single-line comment should be described or explained.

labelSpaceEndOfMultiComment :: ErrorConfig -> LabelWithExplainConfig Source #

How the end of a multi-line comment should be described or explained.

The Default Configuration

defaultErrorConfig :: ErrorConfig Source #

The default configuration. This serves as the base configuration, to be customised using record field updates.