Parser
open class Parser<T, R> where T: Sequence
A Parser parses sequences of T (Token) to one or multiple R (Result)
-
ParseFunction is the type of the wrapped function type
Declaration
Swift
public typealias ParseFunction = (T) -> ParseResult<T, R> -
Initialize a parser with the given wrapping function.
Declaration
Swift
public init(parse: @escaping ParseFunction)Parameters
parseA function that describes how to parse from T to R
-
Start the parsing with that parser
Declaration
Swift
public func parse(_ input: T) -> ParseResult<T, R>Parameters
inputthe token sequence that should be parsed
Return Value
the result of the parsing operation
-
Starts the parsing with that parser
Declaration
Swift
public subscript(_ input: T) -> ParseResult<T, R>Parameters
inputcalls self.parse with the given input
-
just creates a parser that parses the given value as success
Declaration
Swift
public static func just<B>(_ value: B) -> Parser<T, B>Parameters
valuethe result to produce
Return Value
a parser that just produces this value as success
-
fail creates a parser that fails with the given error.
Declaration
Swift
public static func fail(error: ParseError) -> Parser<T, R>Parameters
errthe error that should be used to fail
Return Value
a parser that always fails
-
Creates a parser that always fails with a GenericParseError.
Declaration
Swift
public static func fail(message: String) -> Parser<T, R>Parameters
messagethe message to use for GenericParseError
Return Value
a parser that always fails
-
Produce a new parser for every succeeded parsing process.
Declaration
Swift
public func flatMap<B>(_ tranform: @escaping (R) -> Parser<T, B>) -> Parser<T, B>Parameters
tranformfunction that maps a parse result to a new parser
Return Value
a new parser that combines both parse operations.
-
Produce a new parser which calls f on each successful parsing operation.
Declaration
Swift
public func map<B>(_ transform: @escaping (R) -> B) -> Parser<T, B>Parameters
transformtransforming function that maps from R to B
Return Value
a new parser that calls f on each successful parsing operation
-
Return another error when parsing failed
Declaration
Swift
public func mapError(_ transform: @escaping (ParseError) -> ParseError) -> Parser<T, R>Parameters
transforma function that maps from the occured error to another one
Return Value
a parser that parses self and transforms the error if failed
-
Filter the result of the parser. Could be used for validation.
Declaration
Swift
public func filter(_ transform: @escaping (R) -> ParseError?) -> Parser<T, R>Parameters
transforma function that produces an error if the given result should fail instead
Return Value
a parser that parses self and re-evaluates the result with f
-
Discards the result of self and executes other afterwards on the rest.
Declaration
Swift
public func then<B>(_ other: @escaping @autoclosure () -> Parser<T, B>) -> Parser<T, B>Parameters
otheranother parser to execute afterwards
Return Value
a parser that first parses self and then other on the rest of self
-
Erases the type of the parser
Declaration
Swift
public var typeErased: Parser<T, ()> -
Tries to parse self and consumes it if success. If self fails, the parser still succeeds and consumes nothing.
Declaration
Swift
public var optional: Parser<T, R?>
-
Tries to parse all given parsers in the order they are given The first of all that succeedes will be used.
Declaration
Parameters
othersa sequence of parsers to use as fallback
Return Value
a parser that tries all concatenates parsers in order
-
Concatenates the results of given parsers with an or operation. Will parse Errors.conjunctionOfEmptyCollection if given collection is empty!
Declaration
Parameters
parsersthe parsers to combine
Return Value
a parser that tries to parse the given parsers in order until one succeeds
-
Provides a fallback if the parser fails.
NOTE If parsing fails, there are no tokens consumed! NOTE This parser never fails!
Declaration
Parameters
defaultValuea value that should be parsed instead.
Return Value
a parser that tries to parse and uses defaultValue if parsing failed.
-
Provides a fallback parser that is being used if self.parse fails.
Declaration
Parameters
defaultValuethe parser to use in case of failure
Return Value
a parser that first tries self.parse and only uses defaultValue if self failed.
-
Parses self repetitive with at least one success
Declaration
Swift
public var atLeastOne: Parser<T, [R]> -
Parses self repetitive with separator with at least one success
Declaration
Swift
public func atLeastOnce<B>(sep: Parser<T, B>) -> Parser<T, [R]>Parameters
sepseparator to use between parse operations
Return Value
a parser that parses self repetitive with separator with at least one success.
-
Parses self repetitive with at least
countsucceeds.Declaration
Swift
public func atLeast(count: Int) -> Parser<T, [R]>Parameters
countthe number of required succeeds
Return Value
a parser that parses self repetitive with at least
countsucceeds -
Parses self exactly count times and return the results in an array
Declaration
Swift
public func exactly(count: Int) -> Parser<T, [R]>Parameters
countthe count how often self should be parsed
Return Value
a parser that tries to parse self exactly count times
-
Parses self repetitive and returns results in array
Declaration
Swift
public var rep: Parser<T, [R]>
View on GitHub
Parser Class Reference