Parser

public struct Parser<Token, Result>: ParserType

A parser whose parse function mutates a ParseState<Token> and returns a Result.

  • Construct a Parser that will run implementation.

    Declaration

    Swift

    public init(_ implementation: ParseState<Token> throws -> Result)
  • Runs the parser on the passed in state, potentially mutating the state.

    Parameter

    Parameter state: The state representing remaining input to be parsed.

    Throws

    ParseError if unable to parse.

    Returns

    The resulting parsed value.

    Declaration

    Swift

    public func parse(state: ParseState<Token>) throws -> Result

    Parameters

    state

    The state representing remaining input to be parsed.

    Return Value

    The resulting parsed value.

  • Converts a parser that results in an elemnt into a parser that results in an Array.

    Declaration

    Swift

    @warn_unused_result public func lift() -> Parser<Token, [Result]>
  • Constructs a Parser that catches an error and returns recovery instead.

    Parameter

    Parameter recovery: Result to be returned in case of an error.

    Declaration

    Swift

    @warn_unused_result public func otherwise(recovery: Result) -> Parser

    Parameters

    recovery

    Result to be returned in case of an error.

  • Converts a parser that results in a sequence of characters into a parser that results in a String.

    Declaration

    Swift

    @warn_unused_result public func stringify() -> Parser<Token, String>