Functions

The following functions are available globally.

  • Constructs a Parser that runs attempts to run parser and backtracks on failure. On successful parse, this parser returns the result of parse; on failure, this parser catches the error, rewinds the Stream back to the state before the parse, and rethrows the error.

    Parameter

    Parameter attempt: The parser to run with backtracking on failure.

    Declaration

    Swift

    @warn_unused_result public func attempt<Token, Result>(parser: Parser<Token, Result>) -> Parser<Token, Result>

    Parameters

    attempt

    The parser to run with backtracking on failure.

  • Constructs a Parser that will parse will each element of parsers, sequentially. Parsing only succeeds if every parser succeeds, and the resulting parser returns an array of the results.

    Parameter

    Parameter parsers: The sequence of parsers to sequentially run.

    Declaration

    Swift

    public func sequence<Token, Result, Sequence: SequenceType where Sequence.Generator.Element == Parser<Token, Result>> (parsers: Sequence) -> Parser<Token, [Result]>

    Parameters

    parsers

    The sequence of parsers to sequentially run.

  • Constructs a Parser that will parse will each element of parsers, sequentially. Parsing only succeeds if every parser succeeds, and the resulting parser returns an array of the results.

    Parameter

    Parameter parsers: The variadic list of parsers to sequentially run.

    Declaration

    Swift

    public func sequence<Token, Result>(parsers: Parser<Token, Result>...) -> Parser<Token, [Result]>

    Parameters

    parsers

    The variadic list of parsers to sequentially run.

  • Constructs a Parser that will run the passed-in parsers sequentially. Parsing only succeeds if both parsers succeed, and the resulting parser returns an tuple of the results.

    Declaration

    Swift

    @warn_unused_result public func pair<Token, LeftResult, RightResult>(leftParser: Parser<Token, LeftResult>, _ rightParser: Parser<Token, RightResult>) -> Parser<Token, (LeftResult, RightResult)>
  • Constructs a Parser that will parse each element of parsers, sequentially. Parsing only succeeds if every parser succeeds, and the resulting parser returns an array of the results.

    Parameter

    Parameter parsers: The sequence of parsers to sequentially run.

    Declaration

    Swift

    public func concat<Token, Result, Sequence: SequenceType where Sequence.Generator.Element == Parser<Token, [Result]>>(parsers: Sequence) -> Parser<Token, [Result]>

    Parameters

    parsers

    The sequence of parsers to sequentially run.

  • Constructs a Parser that will parse each element of parsers, sequentially. Parsing only succeeds if every parser succeeds, and the resulting parser returns an array of the results.

    Parameter

    Parameter parsers: The variadic list of parsers to sequentially run.

    Declaration

    Swift

    public func concat<Token, Result>(parsers: Parser<Token, [Result]>...) -> Parser<Token, [Result]>

    Parameters

    parsers

    The variadic list of parsers to sequentially run.

  • Constructs a Parser that will parse each element of parsers, sequentially. Parsing only succeeds if every parser succeeds, and the resulting parser returns an array of the results.

    Parameter

    Parameter parsers: The variadic list of parsers to sequentially run.
    See more

    Declaration

    Swift

    public func +<Token, Result>(left: Parser<Token, [Result]>, right: Parser<Token, [Result]>) -> Parser<Token, [Result]>

    Parameters

    parsers

    The variadic list of parsers to sequentially run.

  • Constructs a Parser that will parse first and then will parse others, prepending the result of first to the result of others.

    Parameter

    Parameter first: The first parser to run whose result will be prepended to the result of others.

    Parameter

    Parameter others: The second parser to run. The result should be an array.

    Declaration

    Swift

    public func prepend<Token, Result>(first: Parser<Token, Result>, _ others: Parser<Token, [Result]>) -> Parser<Token, [Result]>

    Parameters

    first

    The first parser to run whose result will be prepended to the result of others.

    others

    The second parser to run. The result should be an array.

  • Constructs a Parser that will parse others and then will parse last, appending the result of last to the result of others.

    Parameter

    Parameter others: The first parser to run. The result should be an array.

    Parameter

    Parameter last: The last parser to run whose result will be appended to the result of others.

    Declaration

    Swift

    public func append<Token, Result>(others: Parser<Token, [Result]>, _ last: Parser<Token, Result>) -> Parser<Token, [Result]>

    Parameters

    others

    The first parser to run. The result should be an array.

    last

    The last parser to run whose result will be appended to the result of others.

  • Constructs a Parser that consumes no input and returns value.

    Parameter

    Parameter value: The value to return on parse.

    Declaration

    Swift

    @warn_unused_result public func pure<Token, Result>(value: Result) -> Parser<Token, Result>

    Parameters

    value

    The value to return on parse.

  • Constructs a Parser that consumes no input and returns nothing.

    Declaration

    Swift

    @warn_unused_result public func none<Token>() -> Parser<Token, ()>
  • Constructs a Parser that consumes a single token and returns it.

    Declaration

    Swift

    @warn_unused_result public func any<Token>() -> Parser<Token, Token>
  • Constructs a Parser that succeeds if the input is empty. This parser consumes no input and returns nothing.

    Declaration

    Swift

    @warn_unused_result public func end<Token>() -> Parser<Token, ()>
  • Constructs a Parser that will run parser and ensure that no input remains upon parser’s completion. If any input does remain, an error is thrown.

    Parameter

    Parameter parser: The parser to be run.

    Declaration

    Swift

    public func terminating<Token, Result>(parser: Parser<Token, Result>) -> Parser<Token, Result>

    Parameters

    parser

    The parser to be run.

  • Constructs a Parser that consumes a single token and returns the token if it satisfies condition; otherwise, it throws a ParseError.

    Parameter

    Parameter condition: The condition that the token must satisfy.

    Declaration

    Swift

    @warn_unused_result public func satisfy<Token>(condition: Token -> Bool) -> Parser<Token, Token>

    Parameters

    condition

    The condition that the token must satisfy.

  • Constructs a Parser that consumes a single token and returns the token if it is equal to the argument token.

    Parameter

    Parameter token: The token that the input is tested against.

    Declaration

    Swift

    @warn_unused_result public func token<Token: Equatable>(token: Token) -> Parser<Token, Token>

    Parameters

    token

    The token that the input is tested against.

  • Constructs a Parser that consumes a single token and returns the token if it is within the interval interval.

    Parameter

    Parameter interval: The interval that the input is tested against.

    Declaration

    Swift

    @warn_unused_result public func within<I: IntervalType>(interval: I) -> Parser<I.Bound, I.Bound>

    Parameters

    interval

    The interval that the input is tested against.

  • Constructs a Parser that consumes a single token and returns the token if it is within the sequence sequence.

    Parameter

    Parameter sequence: The sequence that the input is tested against.

    Declaration

    Swift

    @warn_unused_result public func oneOf<S: SequenceType where S.Generator.Element: Equatable>(sequence: S) -> Parser<S.Generator.Element, S.Generator.Element>

    Parameters

    sequence

    The sequence that the input is tested against.

  • Constructs a Parser that consumes a single token and returns the token if it is within the list tokens.

    Parameter

    Parameter tokens: The list that the input is tested against.

    Declaration

    Swift

    @warn_unused_result public func oneOf<Token: Equatable>(tokens: Token...) -> Parser<Token, Token>

    Parameters

    tokens

    The list that the input is tested against.

  • Constructs a Parser that is able to recurse on itself.

    Parameter

    Parameter recurse: A function that receives its Parser return value as an argument.

    Declaration

    Swift

    @warn_unused_result public func recursive<Token, Result>(recurse: Parser<Token, Result> -> Parser<Token, Result>) -> Parser<Token, Result>

    Parameters

    recurse

    A function that receives its Parser return value as an argument.

  • Constructs a Parser that will parse with the first parser in parsers that succeeds.

    Parameter

    Parameter parsers: The sequence of parsers to attempt.

    Declaration

    Swift

    @warn_unused_result public func coalesce<Token, Result, Sequence: SequenceType where Sequence.Generator.Element == Parser<Token, Result>>
        (parsers: Sequence) -> Parser<Token, Result>

    Parameters

    parsers

    The sequence of parsers to attempt.

  • Constructs a Parser that will parse with the first element of parsers that succeeds.

    Parameter

    Parameter parsers: A variadic list of parsers to attempt.

    Declaration

    Swift

    @warn_unused_result public func coalesce<Token, Result>(parsers: (Parser<Token, Result>)...) -> Parser<Token, Result>

    Parameters

    parsers

    A variadic list of parsers to attempt.

  • Constructs a Parser that will parse with rightParser if and only if leftParser fails. Note that the construct parser will result in the same type as both other parsers.

    Parameter

    Parameter leftParser: The parser to run first.

    Parameter

    Parameter rightParser: The parser to run whenever the first parser fails.
    See more

    Declaration

    Swift

    @warn_unused_result public func ??<Token, Result>(leftParser: Parser<Token, Result>, rightParser: Parser<Token, Result>) -> Parser<Token, Result>

    Parameters

    leftParser

    The parser to run first.

    rightParser

    The parser to run whenever the first parser fails.

  • Constructs a Parser that will parse with rightParser whenever leftParser fails. Note that the two parsers need not be the same type as an Either type is the result.

    Parameter

    Parameter leftParser: The parser to run first.

    Parameter

    Parameter rightParser: The parser to run whenever the first parser fails.

    Declaration

    Swift

    @warn_unused_result public func either<Token, LeftResult, RightResult>(leftParser: Parser<Token, LeftResult>, _ rightParser: Parser<Token, RightResult>) -> Parser<Token, Either<LeftResult, RightResult>>

    Parameters

    leftParser

    The parser to run first.

    rightParser

    The parser to run whenever the first parser fails.

  • Constructs a Parser that will run leftParser followed by rightParser, discarding the result from rightParser and returning the result from leftParser.

    Parameter

    Parameter leftParser: The parser whose result will be propagated.

    Parameter

    Parameter rightParser: The parser whose result will be discarded.

    Declaration

    Swift

    public func dropRight<Token, LeftResult, RightResult>(leftParser: Parser<Token, LeftResult>, _ rightParser: Parser<Token, RightResult>) -> Parser<Token, LeftResult>

    Parameters

    leftParser

    The parser whose result will be propagated.

    rightParser

    The parser whose result will be discarded.

  • Constructs a Parser that will run leftParser followed by rightParser, discarding the result from leftParser and returning the result from rightParser.

    Parameter

    Parameter leftParser: The parser whose result will be discarded.

    Parameter

    Parameter rightParser: The parser whose result will be propagated.

    Declaration

    Swift

    public func dropLeft<Token, LeftResult, RightResult>(leftParser: Parser<Token, LeftResult>, _ rightParser: Parser<Token, RightResult>) -> Parser<Token, RightResult>

    Parameters

    leftParser

    The parser whose result will be discarded.

    rightParser

    The parser whose result will be propagated.

  • Constructs a Parser that will run parser 1 or more times, as many times as possible parsing parser separated by delimiter.

    Parameter

    Parameter parser: The parser to run repeatedly.

    Parameter

    Parameter delimiter: The parser to separate each occurance of parser.

    Declaration

    Swift

    @warn_unused_result public func separatedBy1<Token, Result, Discard>(parser: Parser<Token, Result>, delimiter: Parser<Token, Discard>) -> Parser<Token, [Result]>

    Parameters

    parser

    The parser to run repeatedly.

    delimiter

    The parser to separate each occurance of parser.

  • Constructs a Parser that will run parser 0 or more times, as many times as possible parsing parser separated by delimiter.

    Parameter

    Parameter parser: The parser to run repeatedly.

    Parameter

    Parameter delimiter: The parser to separate each occurance of parser.

    Declaration

    Swift

    @warn_unused_result public func separatedBy<Token, Result, Discard>(parser: Parser<Token, Result>, delimiter: Parser<Token, Discard>) -> Parser<Token, [Result]>

    Parameters

    parser

    The parser to run repeatedly.

    delimiter

    The parser to separate each occurance of parser.

  • Constructs a Parser that will run left followed by parser followed by right, discarding the result from left and right and returning the result from parser.

    Parameter

    Parameter left: The first parser whose result will be discarded.

    Parameter

    Parameter right: The second parser whose result will be discarded.

    Parameter

    Parameter parser: The parser that will be run between the other two parsers.

    Declaration

    Swift

    public func between<Token, LeftIgnore, RightIgnore, Result>(left: Parser<Token, LeftIgnore>, _ right: Parser<Token, RightIgnore>, parse parser: Parser<Token, Result>) -> Parser<Token, Result>

    Parameters

    left

    The first parser whose result will be discarded.

    right

    The second parser whose result will be discarded.

    parser

    The parser that will be run between the other two parsers.

  • Constructs a Parser that will run left followed by parser followed by right, discarding the result from left and right and returning the result from parser.

    Parameter

    Parameter side: The first and last parser whose result will be discarded.

    Parameter

    Parameter parser: The parser that will be run between the other two parsers.

    Declaration

    Swift

    public func between<Token, Ignore, Result>(side: Parser<Token, Ignore>, parse parser: Parser<Token, Result>) -> Parser<Token, Result>

    Parameters

    side

    The first and last parser whose result will be discarded.

    parser

    The parser that will be run between the other two parsers.

  • Constructs a Parser that will run parser 0 or more times, as many times as possible, and will result in an array of the results from each invocation.

    Parameter

    Parameter parser: The parser to run repeatedly.

    Declaration

    Swift

    @warn_unused_result public func many<Token, Result>(parser: Parser<Token, Result>) -> Parser<Token, [Result]>

    Parameters

    parser

    The parser to run repeatedly.

  • Constructs a Parser that will run parser 1 or more times, as many times as possible, and will result in an array of the results from each invocation.

    Parameter

    Parameter parser: The parser to run repeatedly.

    Declaration

    Swift

    @warn_unused_result public func many1<Token, Result>(parser: Parser<Token, Result>) -> Parser<Token, [Result]>

    Parameters

    parser

    The parser to run repeatedly.

  • Constructs a Parser that will run parser 0 or more times, as many times as possible, and then will run then. If then fails, the parser will undo runs of parser until then succeeds, otherwise will throw a ParseError. On success, returns a tuple with the resulting array from applying parser on the left and with the result of then on the right.

    Parameter

    Parameter parser: The parser to run repeatedly.

    Parameter

    Parameter then: The parser whose success is desired after applications of parser.

    Throws

    ParseError if then does not succeed for any number of times that parser succeeds.

    Declaration

    Swift

    @warn_unused_result public func many<Token, ManyResult, ThenResult>(parser: Parser<Token, ManyResult>, then: Parser<Token, ThenResult>) -> Parser<Token, ([ManyResult], ThenResult)>

    Parameters

    parser

    The parser to run repeatedly.

    then

    The parser whose success is desired after applications of parser.

  • Constructs a Parser that will run parser 1 or more times, as many times as possible, and then will run then. If then fails, the parser will undo runs of parser until then succeeds, otherwise will throw a ParseError. On success, returns a tuple with the resulting array from applying parser on the left and with the result of then on the right.

    Parameter

    Parameter parser: The parser to run repeatedly.

    Parameter

    Parameter then: The parser whose success is desired after applications of parser.

    Throws

    ParseError if then does not succeed for any number of times that parser succeeds.

    Declaration

    Swift

    @warn_unused_result public func many1<Token, ManyResult, ThenResult>(parser: Parser<Token, ManyResult>, then: Parser<Token, ThenResult>) -> Parser<Token, ([ManyResult], ThenResult)>

    Parameters

    parser

    The parser to run repeatedly.

    then

    The parser whose success is desired after applications of parser.

  • Constructs a Parser that will run parser 0 or more times, as few times as possible, such that then will succeed afterwards. If no number of successful applications of parser allows then to succeed, throws a ParseError. On success, returns a tuple with the resulting array from applying parser on the left and with the result of then on the right.

    Parameter

    Parameter parser: The parser to run repeatedly.

    Parameter

    Parameter then: The parser whose success is desired after applications of parser.

    Throws

    ParseError if then does not succeed for any number of times that parser succeeds.

    Declaration

    Swift

    @warn_unused_result public func few<Token, ManyResult, ThenResult>(parser: Parser<Token, ManyResult>, then: Parser<Token, ThenResult>) -> Parser<Token, ([ManyResult], ThenResult)>

    Parameters

    parser

    The parser to run repeatedly.

    then

    The parser whose success is desired after applications of parser.

  • Constructs a Parser that will run parser 1 or more times, as few times as possible, such that then will succeed afterwards. If no number of successful applications of parser allows then to succeed, throws a ParseError. On success, returns a tuple with the resulting array from applying parser on the left and with the result of then on the right.

    Parameter

    Parameter parser: The parser to run repeatedly.

    Parameter

    Parameter then: The parser whose success is desired after applications of parser.

    Throws

    ParseError if then does not succeed for any number of times that parser succeeds.

    Declaration

    Swift

    @warn_unused_result public func few1<Token, ManyResult, ThenResult>(parser: Parser<Token, ManyResult>, then: Parser<Token, ThenResult>) -> Parser<Token, ([ManyResult], ThenResult)>

    Parameters

    parser

    The parser to run repeatedly.

    then

    The parser whose success is desired after applications of parser.

  • Constructs a Parser that will run parser exactly count times and will result in an array of the results from each invocation.

    Parameter

    Parameter parser: The parser to run repeatedly.

    Parameter

    Parameter exactCount: The number of times to run parser.

    Declaration

    Swift

    @warn_unused_result public func repeated<Token, Result>(parser: Parser<Token, Result>, exactCount: Int) -> Parser<Token, [Result]>

    Parameters

    parser

    The parser to run repeatedly.

    exactCount

    The number of times to run parser.

  • Constructs a Parser that will run parser a maximum of count times and will result in an array of the results from each invocation.

    Parameter

    Parameter parser: The parser to run repeatedly.

    Parameter

    Parameter maxCount: The maximum number of times to run parser.

    Declaration

    Swift

    @warn_unused_result public func repeated<Token, Result>(parser: Parser<Token, Result>, maxCount: Int) -> Parser<Token, [Result]>

    Parameters

    parser

    The parser to run repeatedly.

    maxCount

    The maximum number of times to run parser.

  • Constructs a Parser that will run parser a minimum of count times and will result in an array of the results from each invocation.

    Parameter

    Parameter parser: The parser to run repeatedly.

    Parameter

    Parameter minCount: The minimum number of times to run parser.

    Declaration

    Swift

    @warn_unused_result public func repeated<Token, Result>(parser: Parser<Token, Result>, minCount: Int) -> Parser<Token, [Result]>

    Parameters

    parser

    The parser to run repeatedly.

    minCount

    The minimum number of times to run parser.

  • Constructs a Parser that will run parser a number of times within interval and will result in an array of the results from each invocation.

    Parameter

    Parameter parser: The parser to run repeatedly.

    Parameter

    Parameter interval: The interval describing the allowed number of times to run parser.

    Declaration

    Swift

    @warn_unused_result public func repeated<Token, Result>(parser: Parser<Token, Result>, betweenCount interval: ClosedInterval<Int>) -> Parser<Token, [Result]>

    Parameters

    parser

    The parser to run repeatedly.

    interval

    The interval describing the allowed number of times to run parser.

  • Constructs a Parser that will run parser a number of times within interval and will result in an array of the results from each invocation.

    Parameter

    Parameter parser: The parser to run repeatedly.

    Parameter

    Parameter interval: The interval describing the allowed number of times to run parser.

    Declaration

    Swift

    @warn_unused_result public func repeated<Token, Result>(parser: Parser<Token, Result>, betweenCount interval: HalfOpenInterval<Int>) -> Parser<Token, [Result]>

    Parameters

    parser

    The parser to run repeatedly.

    interval

    The interval describing the allowed number of times to run parser.

  • Constructs a Parser that will attempt to parse with parser, but will backtrack and return nil on failure

    Parameter

    Parameter parser: The parser to run.

    Declaration

    Swift

    @warn_unused_result public func optional<Token, Result>(parser: Parser<Token, Result>) -> Parser<Token, Result?>

    Parameters

    parser

    The parser to run.

  • Constructs a Parser that will attempt to parse with parser, but will backtrack and return nil on failure

    Parameter

    Parameter parser: The parser to run.

    Declaration

    Swift

    @warn_unused_result public func optional<Token, Result>(parser: Parser<Token, [Result]>) -> Parser<Token, [Result]>

    Parameters

    parser

    The parser to run.

  • Constructs a Parser that will attempt to parse with parser, but will backtrack and return otherwise on failure

    Parameter

    Parameter parser: The parser to run.

    Parameter

    Parameter otherwise: The default value to return on failure.

    Declaration

    Swift

    @warn_unused_result public func optional<Token, Result>(parser: Parser<Token, Result>, otherwise: Result) -> Parser<Token, Result>

    Parameters

    parser

    The parser to run.

    otherwise

    The default value to return on failure.

  • Construct a Parser that matches a given character.

    Parameter

    Parameter character: The character to match against.

    Declaration

    Swift

    @warn_unused_result public func character(character: Character) -> Parser<Character, Character>

    Parameters

    character

    The character to match against.

  • Constructs a Parser that matches a given string of text.

    Parameter

    Parameter text: The string of text to match against.

    Declaration

    Swift

    @warn_unused_result public func string(text: String) -> Parser<Character, [Character]>

    Parameters

    text

    The string of text to match against.

  • Constructs a Parser that consumes a single token and returns the token if it is within the string text.

    Parameter

    Parameter text: The String that the input is tested against.

    Declaration

    Swift

    @warn_unused_result public func within(text: String) -> Parser<Character, Character>

    Parameters

    text

    The String that the input is tested against.