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
parse
A 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
input
the 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
input
calls 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
value
the 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
err
the 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
message
the 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
tranform
function 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
transform
transforming 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
transform
a 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
transform
a 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
other
another 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
others
a 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
parsers
the 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
defaultValue
a 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
defaultValue
the 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
sep
separator 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
count
succeeds.Declaration
Swift
public func atLeast(count: Int) -> Parser<T, [R]>
Parameters
count
the number of required succeeds
Return Value
a parser that parses self repetitive with at least
count
succeeds -
Parses self exactly count times and return the results in an array
Declaration
Swift
public func exactly(count: Int) -> Parser<T, [R]>
Parameters
count
the 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]>