6.7 FParsec.Error

6.7.1 Interface

// FParsec.dll

[<AutoOpen>] // module is automatically opened when FParsec namespace is opened
module FParsec.Error

// The following type abbreviations and active patterns allow you to
// treat the ErrorMessage type almost as if it was defined as:
//
// [<CustomEquality; NoComparison>]
// type ErrorMessage =
//      | Expected           of string
//      | ExpectedString     of string
//      | ExpectedStringCI   of string
//      | Unexpected         of string
//      | UnexpectedString   of string
//      | UnexpectedStringCI of string
//      | Message            of string
//      | NestedError        of Position * obj * ErrorMessageList
//      | CompoundError      of string * Position * obj * ErrorMessageList
//      | OtherErrorMessage  of obj

type Expected           = ErrorMessage.Expected
type ExpectedString     = ErrorMessage.ExpectedString
type ExpectedStringCI   = ErrorMessage.ExpectedCaseInsensitiveString
type Unexpected         = ErrorMessage.Unexpected
type UnexpectedString   = ErrorMessage.UnexpectedString
type UnexpectedStringCI = ErrorMessage.UnexpectedCaseInsensitiveString
type Message            = ErrorMessage.Message
type NestedError        = ErrorMessage.NestedError
type CompoundError      = ErrorMessage.CompoundError
type OtherErrorMessage  = ErrorMessage.Other

// Unfortunately, F# currently doesn't support active patterns with more
// than 7 cases, so we have to use partial patterns.

val (|Expected|_|):           ErrorMessage -> string option
val (|ExpectedString|_|):     ErrorMessage -> string option
val (|ExpectedStringCI|_|):   ErrorMessage -> string option
val (|Unexpected|_|):         ErrorMessage -> string option
val (|UnexpectedString|_|):   ErrorMessage -> string option
val (|UnexpectedStringCI|_|): ErrorMessage -> string option
val (|Message|_|):            ErrorMessage -> string option
val (|NestedError|_|):        ErrorMessage
                           -> (Position * obj * ErrorMessageList) option
val (|CompoundError|_|):      ErrorMessage
                           -> (string * Position * obj * ErrorMessageList) option
val (|OtherErrorMessage|_|):  ErrorMessage -> obj option


// The following literal definition and active pattern allow you to
// treat the ErrorMessageList type as if it was defined as:
//
// [<CompilationRepresentation(CompilationRepresentationFlags.UseNullAsTrueValue);
//   CustomEquality; NoComparison>]
// type ErrorMessageList =
//     | AddErrorMessage of ErrorMessage * ErrorMessageList
//     | NoErrorMessages
// with
//   static member Merge: ErrorMessageList * ErrorMessageList -> ErrorMessageList
//   static member ToHashSet: ErrorMessageList -> HashSet<ErrorMessage>
//   static member ToSortedArray: ErrorMessageList -> ErrorMessage[]

[<Literal>]
val NoErrorMessages: ErrorMessageList = null

val (|ErrorMessageList|NoErrorMessages|):
    ErrorMessageList -> Choice<ErrorMessage*ErrorMessageList,unit>


// Helper functions for creating an ErrorMessageList with a single ErrorMessage
val expected:                string -> ErrorMessageList
val expectedStringError:     string -> ErrorMessageList
val expectedStringCIError:   string -> ErrorMessageList
val unexpected:              string -> ErrorMessageList
val unexpectedStringError:   string -> ErrorMessageList
val unexpectedStringCIError: string -> ErrorMessageList
val messageError:            string -> ErrorMessageList
val otherError:              obj    -> ErrorMessageList
val nestedError:
              CharStream<_> -> ErrorMessageList -> ErrorMessageList
val compoundError:
    string -> CharStream<_> -> ErrorMessageList -> ErrorMessageList


// Two convenient helper functions
val mergeErrors: ErrorMessageList -> ErrorMessageList -> ErrorMessageList
val isSingleErrorMessageOfType: ErrorMessageType -> ErrorMessageList


// A simple container type for holding an ErrorMessageList
// together with its associated input stream position and user state
[<Sealed>]
type ParserError =
  new:   position: Position
       * userState: obj
       * messages: ErrorMessageList
      -> ParserError

  member Position:  Position
  member UserState: obj
  member Messages:  ErrorMessageList

  override ToString: unit -> string
  member   ToString: streamWhereErrorOccurred: CharStream -> string

  member WriteTo:
           textWriter: System.IO.TextWriter
         * streamWhereErrorOccurred: CharStream
         * ?tabSize: int
         * ?columnWidth: int
         * ?initialIndentation: string * ?indentationIncrement: string
         -> unit

  member WriteTo:
           textWriter: System.IO.TextWriter
         * getStream: (position -> CharStream)
         * ?tabSize: int
         * ?columnWidth: int
         * ?initialIndentation: string * ?indentationIncrement: string
         -> unit

  member WriteTo:
           textWriter: System.IO.TextWriter
         * ?positionPrinter:
             (System.IO.TextWriter -> Position -> string -> int -> unit)
         * ?columnWidth: int
         * ?initialIndentation: string * ?indentationIncrement: string
         -> unit

  override Equals: obj -> bool
  override GetHashCode: unit -> int

6.7.2 Members

val expected: string -> ErrorMessageList

expected label creates an ErrorMessageList with a single Expected label message.

val expectedStringError: string -> ErrorMessageList

expectedStringError str creates an ErrorMessageList with a single ExpectedString str message.

val expectedStringCIError: string -> ErrorMessageList

expectedStringCIError str creates an ErrorMessageList with a single ExpectedStringCI str message.

val unexpected: string -> ErrorMessageList

unexpected label creates an ErrorMessageList with a single Unexpected label message.

val unexpectedStringError: string -> ErrorMessageList

unexpectedStringError str creates an ErrorMessageList with a single UnexpectedString str message.

val unexpectedStringCIError: string -> ErrorMessageList

unexpectedStringCIError str creates an ErrorMessageList with a single UnexpectedStringCI str message.

val messageError: string -> ErrorMessageList

messageError msg creates an ErrorMessageList with a single Message msg message.

val otherError: obj -> ErrorMessageList

otherError o creates an ErrorMessageList with a single OtherError o message.

val nestedError:
              CharStream<_> -> ErrorMessageList -> ErrorMessageList

nestedError stream msgs creates an ErrorMessageList with a single NestedError(stream.Position, stream.UserState, msgs) message, except if msgs is already an ErrorMessageList with a single NestedError message, in which case msgs is returned instead.

val compoundError:
    string -> CharStream<_> -> ErrorMessageList -> ErrorMessageList

compoundError label stream msgs creates an ErrorMessageList with a single CompoundError(label, stream.Position, stream.UserState, msgs) message, except if msgs is an ErrorMessageList with a single NestedError(pos2, ustate2, msgs2) message, in which case an ErrorMessageList with a single CompoundError(label, pos2, ustate2, msgs2) message is returned instead.

mergeErrors error1 error2 is an abbreviation for ErrorMessageList.Merge(error1, error2).

val isSingleErrorMessageOfType: ErrorMessageType -> ErrorMessageList

isSingleErrorMessageOfType ty msgs returns true if and only ifmsgs is an ErrorMessageList with a single ErrorMessage with the ErrorMessageType ty.

[<Sealed>]
type ParserError

ParserError is a simple container type for holding an ErrorMessageList together with its associated input stream position and user state.

The ParserError class has the following members:

new:   position: Position
     * userState: obj
     * messages: ErrorMessageList
    -> ParserError

Constructs a ParserError from an ErrorMessageList and its associated position.

member Position: Position

The input stream position of the parser error.

member UserState: obj

The user state associated with the parser error.

member Messages: ErrorMessageList

The error messages of the parser error.

override ToString: unit -> string

Is equivalent to

use sw = new System.IO.StringWriter()
WriteTo(sw)
sw.ToString()
member ToString: streamWhereErrorOccurred: CharStream -> string

Is equivalent to

use sw = new System.IO.StringWriter()
WriteTo(sw, streamWhereErrorOccurred)
sw.ToString()
member WriteTo:
         textWriter: System.IO.TextWriter
       * streamWhereErrorOccurred: CharStream
       * ?tabSize: int
       * ?columnWidth: int
       * ?initialIndentation: string * ?indentationIncrement: string
       -> unit

Is equivalent to

let getStream (pos: Position) =
    if pos.StreamName = Position.StreamName then streamWhereErrorOccurred
    else null

WriteTo(textWriter,
        getStream,
        ?tabSize = tabSize,
        ?columWidth = columnWidth,
        ?initialIndentation = initialIndentation,
        ?indentationIncrement = indentationIncrement)
member WriteTo:
         textWriter: System.IO.TextWriter
       * getStream: (position -> CharStream)
       * ?tabSize: int
       * ?columnWidth: int
       * ?initialIndentation: string * ?indentationIncrement: string
       -> unit

Writes a string representation of the ParserError to the given TextWriter value.

For each error getStream is called with the error position. The returned CharStream must be null or contain the content of the CharStream for which the error was generated (at the original indices).

If getStream returns a non‐null CharStream, the printed error position information is augmented with the line of text surrounding the error position, together with a ‘^’‐marker pointing to the exact location of the error in the input stream.

The tabSize parameter (default value: 8) specifies the tab stop distance that this method assumes when counting text columns. This parameter only has an effect for error positions where getStream returns a non‐null CharStream.

The columnWidth parameter (default value: 79) specifies the number of char columns that this method should try to fit its output to.

member WriteTo:
         textWriter: System.IO.TextWriter
       * ?positionPrinter:
           (System.IO.TextWriter -> Position -> string -> int -> unit)
       * ?columnWidth: int
       * ?initialIndentation: string * ?indentationIncrement: string
       -> unit

Writes a string representation of the ParserError to the given TextWriter value.

The format of the position information can be customized by specifying the positionPrinter argument. The given function is expected to print a representation of the passed Position value to the passed TextWriter value. If possible, it should indent text lines with the passed string and take into account the maximum column count (including indentation) passed as the last argument.