ParserType
public protocol ParserType
A protocol to which Parser
conforms. Exists as a workaround to the current
Swift limitation that extensions cannot make concrete type non-generic.
-
parse(_:)
Default implementationUndocumented
Default Implementation
Runs the parser on the passed in
sequence
.Parameter
Parameter sequence: The sequence to be parsed.
Throws
ParseError
if unable to parse.Returns
The resulting parsed value.
Declaration
Swift
public protocol ParserType
-
The type that results from the parsing.
Declaration
Swift
typealias Result
-
The type whose sequence is parsed by the parser.
Declaration
Swift
typealias Token
-
flatMap(_:)
Extension methodReturns a
Parser
that, on successful parse, continues parsing with the parser resulting from mappingtransform
over its result value; returns the result of this new parser.Can be used to chain parsers together sequentially.
Parameter
Parameter transform: The transform to map over the result.Declaration
Swift
@warn_unused_result public func flatMap<MappedResult>(transform: Result throws -> Parser<Token, MappedResult>) -> Parser<Token, MappedResult>
Parameters
transform
The transform to map over the result.
-
map(_:)
Extension methodReturns a
Parser
that, on successful parse, returns the result of mappingtransform
over its previous result valueParameter
Parameter transform: The transform to map over the result.Declaration
Swift
@warn_unused_result public func map<MappedResult>(transform: Result throws -> MappedResult) -> Parser<Token, MappedResult>
Parameters
transform
The transform to map over the result.
-
replace(_:)
Extension methodReturns a
Parser
that, on successful parse, discards its previous result and returnsvalue
instead.Parameter
Parameter value: The value to return on successful parse.Declaration
Swift
@warn_unused_result public func replace<NewResult>(value: NewResult) -> Parser<Token, NewResult>
Parameters
value
The value to return on successful parse.
-
discard()
Extension methodReturns a
Parser
that, on successful parse, discards its result.Declaration
Swift
@warn_unused_result public func discard() -> Parser<Token, ()>
-
peek(_:)
Extension methodReturns a
Parser
that calls the callbackglimpse
before returning its result.Parameter
Parameter glimpse: Callback that recieves the parser’s result as input.Declaration
Swift
@warn_unused_result public func peek(glimpse: Result throws -> ()) -> Parser<Token, Result>
Parameters
glimpse
Callback that recieves the parser’s result as input.
-
require(_:)
Extension methodReturns a
Parser
that verifies that its result passes the condition before returning its result. If the result fails the condition, throws an error.Parameter
Parameter condition: The condition used to test the result.Declaration
Swift
@warn_unused_result public func require(condition: Result -> Bool) -> Parser<Token, Result>
Parameters
condition
The condition used to test the result.
-
unwrap()
Extension methodUndocumented
Declaration
Swift
public protocol ParserType
-
recover(_:)
Extension methodConstructs a
Parser
that, on error, catches the error and attempts to recover by running theParser
obtained from passing the error torecovery
.Parameter
Parameter recovery: A function that, given the error, will return a new parser to attempt.Declaration
Swift
@warn_unused_result public func recover(recovery: ParseError throws -> Parser<Token, Result>) -> Parser<Token, Result>
Parameters
recovery
A function that, given the error, will return a new parser to attempt.
-
mapError(_:)
Extension methodConstructs a
Parser
that, on error, catches the error and rethrows the transformed error.Parameter
Parameter transform: The transform to map onto the caught message.Declaration
Swift
@warn_unused_result public func mapError(transform: ParseError -> ParseError) -> Parser<Token, Result>
Parameters
transform
The transform to map onto the caught message.
-
withError(_:)
Extension methodConstructs a
Parser
that, on error, discards the previously thrown error and throws instead a newUnableToMatch
error with the given message.Parameter
Parameter description: The description to include in the error.Declaration
Swift
@warn_unused_result public func withError(description: String) -> Parser<Token, Result>
Parameters
description
The description to include in the error.