Functions

The following functions are available globally.

  • Return a collection containing x and all elements of xs. Works with strings and arrays.

    Declaration

    Swift

    public func extend
        <A, C: RangeReplaceableCollection>
        (_ xs: C) -> (A) -> C where C.Iterator.Element == A
  • Return a collection containing x and all elements of xs. Works with strings and arrays.

    Declaration

    Swift

    public func extend
        <A, C: RangeReplaceableCollection >
        (_ x: A) -> (C) -> C where C.Iterator.Element == A
  • Join 2 collections together.

    Declaration

    Swift

    public func extend
        <C1: RangeReplaceableCollection, C2: Collection >
        (_ xs1: C1) -> (C2) -> C1 where C1.Iterator.Element == C2.Iterator.Element
  • Create a tuple of the arguments.

    Declaration

    Swift

    public func tuple<A, B>(_ a: A) -> (B) -> (A, B)
  • Undocumented

    Declaration

    Swift

    public func tuple<A, B, C>(_ a: A) -> (B) -> (C) -> (A, B, C)
  • Undocumented

    Declaration

    Swift

    public func tuple<A, B, C, D>(_ a: A) -> (B) -> (C) -> (D) -> (A, B, C, D)
  • Undocumented

    Declaration

    Swift

    public func tuple<A, B, C, D, E>(_ a: A) -> (B) -> (C) -> (D) -> (E) -> (A, B, C, D, E)
  • Create a curried function of a normal function.

    Usage:

    func myFunc(a: A, b: B) -> C { ... }
    
    let parser = curry(myFunc) <^> string("hello") <*> string("world")
    

    Declaration

    Swift

    public func curry<A,B,C>(_ f: @escaping (A, B) -> C) -> (A) -> (B) -> C
  • Create a curried function of a normal function.

    Usage:

    func myFunc(a: A, b: B, c: C) -> D { ... }
    
    let parser = curry(myFunc) <^> string("hello,") <*> string("dear") <*> string("world")
    

    Declaration

    Swift

    public func curry<A,B,C,D>(_ f: @escaping (A, B, C) -> D) -> (A) -> (B) -> (C) -> D
  • Create a curried function of a normal function.

    Usage:

    func myFunc(a: A, b: B, c: C, d: D) -> E { ... }
    
    let parser = curry(myFunc) <^> string("a") <*> string("b") <*> string("c") <*> string("d")
    

    Declaration

    Swift

    public func curry<A, B, C, D, E>(_ f: @escaping (A, B, C, D) -> E) -> (A) -> (B) -> (C) -> (D) -> E
  • Create a curried function of a normal function.

    Usage:

    func myFunc(a: A, b: B, c: C, d: D) -> E { ... }
    
    let parser = curry(myFunc) <^> string("a") <*> string("b") <*> string("c") <*> string("d")
    

    Declaration

    Swift

    public func curry<A, B, C, D, E, F>(_ f: @escaping (A, B, C, D, E) -> F) -> (A) -> (B) -> (C) -> (D) -> (E) -> F
  • FlatMap a function over a parser.

    • If the parser fails, the function will not be evaluated and the error is returned.
    • If the parser succeeds, the function will be applied to the output, and the resulting parser is run with the remaining input.

    Declaration

    Swift

    public func >>- <T,A,B> (p: Parser<T,A>, f: @escaping (A) throws -> Parser<T,B>) -> Parser<T,B>

    Parameters

    p

    A parser of type Parser

    f

    A transformation function from type A to Parser

    Return Value

    A parser of type Parser

  • Map a function over a parser

    • If the parser fails, the function will not be evaluated and the parser error is returned.
    • If the parser succeeds, the function will be applied to the output.

    Declaration

    Swift

    public func <^> <T,A,B> (f: @escaping (A) throws -> B, p: Parser<T,A>) -> Parser<T,B>

    Parameters

    f

    A function from type A to type B

    p

    A parser of type Parser

    Return Value

    A parser of type Parser

  • Apply a parser returning a function to another parser.

    • If the first parser fails, its error will be returned.
    • If it succeeds, the resulting function will be applied to the 2nd parser.

    Declaration

    Swift

    public func <*> <T, A, B>(fp: Parser<T, (A) -> B>, p: Parser<T, A>) -> Parser<T, B>

    Parameters

    fp

    A parser with a function A->B as output

    p

    A parser of type Parser

    Return Value

    A parser of type Parser

  • Apply both parsers, but only return the output from the first one.

    • If the first parser fails, its error will be returned.
    • If the 2nd parser fails, its error will be returned.

    Declaration

    Swift

    public func <* <T, A, B>(p1: Parser<T, A>, p2: Parser<T, B>) -> Parser<T, A>

    Return Value

    A parser of the same type as the first parser.

  • Apply both parsers, but only return the output from the 2nd one.

    • If the first parser fails, its error will be returned.
    • If the 2nd parser fails, its error will be returned.

    Declaration

    Swift

    public func *> <T, A, B>(p1: Parser<T, A>, p2: Parser<T, B>) -> Parser<T, B>

    Return Value

    A parser of the same type as the 2nd parser.

  • Create a parser which doesn’t consume input and returns this parameter.

    Declaration

    Swift

    public func pure<T, A>(_ a: A) -> Parser<T, A>

    Parameters

    a

    A value of type A

  • Apply one of 2 parsers.

    • If the first parser succeeds, return its results.
    • Else if the 2nd parser succeeds, return its results.
    • If they both fail, return the failure from the parser that got the furthest.

    Has infinite lookahead. The 2nd parser starts from the same position in the input as the first one.

    Declaration

    Swift

    public func <|> <T, A>(l: Parser<T, A>, r: Parser<T, A>) -> Parser<T, A>
  • Undocumented

    Declaration

    Swift

    public func satisfy<T>
        (expect: @autoclosure @escaping () -> String, condition: @escaping (T) -> Bool) -> Parser<T, T>
  • Undocumented

    Declaration

    Swift

    public func token<T>(_ token: T) -> Parser<T, T> where T : Equatable
  • Match several tokens in a row.

    Declaration

    Swift

    public func tokens<T, C>(_ xs: C) -> Parser<T, C> where T : Equatable, T == C.Element, C : Collection
  • Return whatever the next token is.

    Declaration

    Swift

    public func any<T>() -> Parser<T, T>
  • Try parser, if it fails return ‘otherwise’ without consuming input.

    Declaration

    Swift

    public func optional<T, A>(_ p: Parser<T, A>, otherwise: A) -> Parser<T, A>
  • Try parser, if it fails return nil without consuming input.

    Declaration

    Swift

    public func optional<T, A>(_ p: Parser<T, A>) -> Parser<T, A?>
  • Delay creation of parser until it is needed.

    Declaration

    Swift

    public func lazy <T,A> (_ f: @autoclosure @escaping () -> Parser<T,A>) -> Parser<T,A>
  • Apply parser once, then repeat until it fails. Returns an array of the results.

    Declaration

    Swift

    public func oneOrMore<T, A>(_ p: Parser<T, A>) -> Parser<T, [A]>
  • Repeat parser until it fails. Returns an array of the results.

    Declaration

    Swift

    public func zeroOrMore<T, A>(_ p: Parser<T, A>) -> Parser<T, [A]>
  • Repeat parser ‘n’ times. If 'n’ == 0 it always succeeds and returns [].

    Declaration

    Swift

    public func count<T, A>(_ n: Int, _ p: Parser<T, A>) -> Parser<T, [A]>
  • Repeat parser as many times as possible within the given range. count(2…2, p) is identical to count(2, p)

    Declaration

    Swift

    public func count<T, A>(_ r: ClosedRange<Int>, _ p: Parser<T, A>) -> Parser<T, [A]>

    Parameters

    r

    A positive closed integer range.

  • Repeat parser as many times as possible within the given range. count(2..<3, p) is identical to count(2, p)

    Declaration

    Swift

    public func count<T, A>(_ r: Range<Int>, _ p: Parser<T, A>) -> Parser<T, [A]>

    Parameters

    r

    A positive half open integer range.

  • Succeed if the next token is in the provided collection.

    Declaration

    Swift

    public func oneOf<T, C>(_ collection: C) -> Parser<T, T> where T : Equatable, T == C.Element, C : Collection
  • Succeed if the next token is not in the provided collection.

    Declaration

    Swift

    public func noneOf<T, C>(_ collection: C) -> Parser<T, T> where T : Equatable, T == C.Element, C : Collection
  • Match anything but this.

    Declaration

    Swift

    public func not<T>(_ token: T) -> Parser<T, T> where T : Equatable
  • Verify that input is empty.

    Declaration

    Swift

    public func eof<T>() -> Parser<T, ()>
  • Fail with the given error message. Ignores input.

    Declaration

    Swift

    public func fail<T, A>(_ error: ParseError<T>) -> Parser<T, A>
  • Parse all of input with parser. Failure to consume all of input will result in a ParserError.

    Throws

    ParserError.

    Declaration

    Swift

    public func parse<A, T>(_ p: Parser<T, A>, _ c: [T]) throws -> A

    Parameters

    p

    A parser.

    input

    A collection, like a string or an array.

    Return Value

    Output from the parser.

  • Match a single character.

    Declaration

    Swift

    public func char(_ c: Character) -> Parser<Character, Character>
  • Join two strings

    Declaration

    Swift

    public func extend(_ a: String) -> (String) -> String
  • Join a character with a string.

    Declaration

    Swift

    public func extend(_ a: Character) -> (String) -> String
  • Apply character parser once, then repeat until it fails. Returns a string.

    Declaration

    Swift

    public func oneOrMore<T>(_ p: Parser<T, Character>) -> Parser<T, String>
  • Repeat character parser until it fails. Returns a string.

    Declaration

    Swift

    public func zeroOrMore<T>(_ p: Parser<T, Character>) -> Parser<T, String>
  • Repeat character parser ‘n’ times and return as string. If 'n’ == 0 it always succeeds and returns “”.

    Declaration

    Swift

    public func count<T>(_ n: Int, _ p: Parser<T, Character>) -> Parser<T, String>
  • Repeat parser as many times as possible within the given range. count(2…2, p) is identical to count(2, p)

    Declaration

    Swift

    public func count<T>(_ r: ClosedRange<Int>, _ p: Parser<T, Character>) -> Parser<T, String>

    Parameters

    r

    A positive closed integer range.

  • Repeat parser as many times as possible within the given range. count(2..<3, p) is identical to count(2, p)

    Declaration

    Swift

    public func count<T>(_ r: Range<Int>, _ p: Parser<T, Character>) -> Parser<T, String>

    Parameters

    r

    A positive half open integer range.

  • Match a string

    Note

    consumes either the full string or nothing, even on a partial match.

    Declaration

    Swift

    public func string(_ s: String) -> Parser<Character, String>
  • Succeed if the next character is in the provided string.

    Declaration

    Swift

    public func oneOf(_ s: String) -> Parser<Character, Character>
  • Succeed if the next character is not in the provided string.

    Declaration

    Swift

    public func noneOf(_ s: String) -> Parser<Character, Character>
  • Produces a character if the character and succeeding do not match any of the strings.

    Note

    consumes only produced characters

    Declaration

    Swift

    public func noneOf(_ strings: [String]) -> Parser<Character, Character>
  • Undocumented

    Declaration

    Swift

    public func char(_ set: CharacterSet, name: String) -> Parser<Character, Character>
  • Parse all of the string with parser. Failure to consume all of input will result in a ParserError.

    Throws

    A ParserError.

    Declaration

    Swift

    public func parse<A>(_ p: Parser<Character, A>, _ s: String) throws -> A

    Parameters

    p

    A parser of characters.

    input

    A string.

    Return Value

    Output from the parser.

  • Undocumented

    Declaration

    Swift

    public func print(error: ParseError<Character>, in s: String)