ParseResult

public enum ParseResult<Token, Result> where Token: Sequence

The result of a parse process.

  • Parse was successful. Contains the result and the rest of the token sequence that is unprocessed

    Declaration

    Swift

    case success(result: Result, rest: Token)
  • Parse was not successful. Contains the ParseError

    Declaration

    Swift

    case fail(ParseError)
  • Transforms the result with f, if successful.

    Declaration

    Swift

    public func map<B>(_ transform: (Result, Token) -> B) -> ParseResult<Token, B>

    Parameters

    transform

    a function to use to transform result

    Return Value

    ParseResult with transformed result or fail with unchanged error.

  • Transforms the error if the result is in failed state

    Declaration

    Swift

    public func mapError(_ transform: (ParseError) -> ParseError) -> ParseResult<Token, Result>

    Parameters

    transform

    a function that takes the wrapped error and returns a new one

    Return Value

    the result with transformed error or unchanged if successful

  • Create a new ParseResult for the result.

    Declaration

    Swift

    public func flatMap<B>(_ transform: (Result, Token) -> ParseResult<Token, B>) -> ParseResult<Token, B>

    Parameters

    transform

    a function that takes a result and returns a parse result

    Return Value

    the value that was produced by f if self was success or still fail if not

  • Checks whether or not the result is successful

    Declaration

    Swift

    public func isSuccess() -> Bool

    Return Value

    true if successful

  • Checks whether or not the result is failed

    Declaration

    Swift

    public func isFailed() -> Bool

    Return Value

    true if failed

  • Unwraps the result from the success case.

    Throws

    Errors.unwrappedFailedResult if not successful

    Declaration

    Swift

    public func unwrap() throws -> Result

    Return Value

    the result if success

  • Unwraps the wrapped result and uses fallback if .fail

    Declaration

    Swift

    public func unwrap(fallback: @autoclosure () -> Result) -> Result

    Parameters

    fallback

    the fallback value to use if parse failed

    Return Value

    either the parse result or fallback

  • Returns the rest of the parsing operation in success case.

    Throws

    Errors.unwrappedFailedResult if not successful

    Declaration

    Swift

    public func rest() throws -> Token

    Return Value

    the rest if successful

  • Unwraps the error if not successful

    Throws

    Errors.errorFromSuccessfulResult if successful

    Declaration

    Swift

    public func error() throws -> ParseError

    Return Value

    the parsing error if not successful