Functions
The following functions are available globally.
-
Constructs a
Parserthat runs attempts to runparserand backtracks on failure. On successful parse, this parser returns the result ofparse; on failure, this parser catches the error, rewinds theStreamback 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
attemptThe parser to run with backtracking on failure.
-
Constructs a
Parserthat will parse will each element ofparsers, 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
parsersThe sequence of parsers to sequentially run.
-
Constructs a
Parserthat will parse will each element ofparsers, 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
parsersThe variadic list of parsers to sequentially run.
-
Constructs a
Parserthat 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
Parserthat will parse each element ofparsers, 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
parsersThe sequence of parsers to sequentially run.
-
Constructs a
Parserthat will parse each element ofparsers, 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
parsersThe variadic list of parsers to sequentially run.
-
Constructs a
Parserthat will parse each element ofparsers, sequentially. Parsing only succeeds if every parser succeeds, and the resulting parser returns an array of the results.See moreParameter
Parameter parsers: The variadic list of parsers to sequentially run.Declaration
Swift
public func +<Token, Result>(left: Parser<Token, [Result]>, right: Parser<Token, [Result]>) -> Parser<Token, [Result]>Parameters
parsersThe variadic list of parsers to sequentially run.
-
Constructs a
Parserthat will parsefirstand then will parseothers, prepending the result offirstto the result ofothers.Parameter
Parameter first: The first parser to run whose result will be prepended to the result ofothers.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
firstThe first parser to run whose result will be prepended to the result of
others.othersThe second parser to run. The result should be an array.
-
Constructs a
Parserthat will parseothersand then will parselast, appending the result oflastto the result ofothers.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 ofothers.Declaration
Swift
public func append<Token, Result>(others: Parser<Token, [Result]>, _ last: Parser<Token, Result>) -> Parser<Token, [Result]>Parameters
othersThe first parser to run. The result should be an array.
lastThe last parser to run whose result will be appended to the result of
others.
-
Constructs a
Parserthat consumes no input and returnsvalue.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
valueThe value to return on parse.
-
Constructs a
Parserthat consumes no input and returns nothing.Declaration
Swift
@warn_unused_result public func none<Token>() -> Parser<Token, ()> -
Constructs a
Parserthat consumes a single token and returns it.Declaration
Swift
@warn_unused_result public func any<Token>() -> Parser<Token, Token> -
Constructs a
Parserthat 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
Parserthat will runparserand ensure that no input remains uponparser’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
parserThe parser to be run.
-
Constructs a
Parserthat consumes a single token and returns the token if it satisfiescondition; otherwise, it throws aParseError.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
conditionThe condition that the token must satisfy.
-
Constructs a
Parserthat consumes a single token and returns the token if it is equal to the argumenttoken.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
tokenThe token that the input is tested against.
-
Constructs a
Parserthat consumes a single token and returns the token if it is within the intervalinterval.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
intervalThe interval that the input is tested against.
-
Constructs a
Parserthat consumes a single token and returns the token if it is within the sequencesequence.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
sequenceThe sequence that the input is tested against.
-
Constructs a
Parserthat consumes a single token and returns the token if it is within the listtokens.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
tokensThe list that the input is tested against.
-
Constructs a
Parserthat is able to recurse on itself.Parameter
Parameter recurse: A function that receives itsParserreturn 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
recurseA function that receives its
Parserreturn value as an argument.
-
Constructs a
Parserthat will parse with the first parser inparsersthat 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
parsersThe sequence of parsers to attempt.
-
Constructs a
Parserthat will parse with the first element ofparsersthat 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
parsersA variadic list of parsers to attempt.
-
Constructs a
Parserthat will parse withrightParserif and only ifleftParserfails. Note that the construct parser will result in the same type as both other parsers.Parameter
Parameter leftParser: The parser to run first.See moreParameter
Parameter rightParser: The parser to run whenever the first parser fails.Declaration
Swift
@warn_unused_result public func ??<Token, Result>(leftParser: Parser<Token, Result>, rightParser: Parser<Token, Result>) -> Parser<Token, Result>Parameters
leftParserThe parser to run first.
rightParserThe parser to run whenever the first parser fails.
-
Constructs a
Parserthat will parse withrightParserwheneverleftParserfails. Note that the two parsers need not be the same type as anEithertype 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
leftParserThe parser to run first.
rightParserThe parser to run whenever the first parser fails.
-
Constructs a
Parserthat will runleftParserfollowed byrightParser, discarding the result fromrightParserand returning the result fromleftParser.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
leftParserThe parser whose result will be propagated.
rightParserThe parser whose result will be discarded.
-
Constructs a
Parserthat will runleftParserfollowed byrightParser, discarding the result fromleftParserand returning the result fromrightParser.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
leftParserThe parser whose result will be discarded.
rightParserThe parser whose result will be propagated.
-
Constructs a
Parserthat will runparser1 or more times, as many times as possible parsingparserseparated bydelimiter.Parameter
Parameter parser: The parser to run repeatedly.Parameter
Parameter delimiter: The parser to separate each occurance ofparser.Declaration
Swift
@warn_unused_result public func separatedBy1<Token, Result, Discard>(parser: Parser<Token, Result>, delimiter: Parser<Token, Discard>) -> Parser<Token, [Result]>Parameters
parserThe parser to run repeatedly.
delimiterThe parser to separate each occurance of
parser. -
Constructs a
Parserthat will runparser0 or more times, as many times as possible parsingparserseparated bydelimiter.Parameter
Parameter parser: The parser to run repeatedly.Parameter
Parameter delimiter: The parser to separate each occurance ofparser.Declaration
Swift
@warn_unused_result public func separatedBy<Token, Result, Discard>(parser: Parser<Token, Result>, delimiter: Parser<Token, Discard>) -> Parser<Token, [Result]>Parameters
parserThe parser to run repeatedly.
delimiterThe parser to separate each occurance of
parser. -
Constructs a
Parserthat will runleftfollowed byparserfollowed byright, discarding the result fromleftandrightand returning the result fromparser.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
leftThe first parser whose result will be discarded.
rightThe second parser whose result will be discarded.
parserThe parser that will be run between the other two parsers.
-
Constructs a
Parserthat will runleftfollowed byparserfollowed byright, discarding the result fromleftandrightand returning the result fromparser.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
sideThe first and last parser whose result will be discarded.
parserThe parser that will be run between the other two parsers.
-
Constructs a
Parserthat will runparser0 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
parserThe parser to run repeatedly.
-
Constructs a
Parserthat will runparser1 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
parserThe parser to run repeatedly.
-
Constructs a
Parserthat will runparser0 or more times, as many times as possible, and then will runthen. Ifthenfails, the parser will undo runs ofparseruntilthensucceeds, otherwise will throw aParseError. On success, returns a tuple with the resulting array from applyingparseron the left and with the result ofthenon the right.Parameter
Parameter parser: The parser to run repeatedly.Parameter
Parameter then: The parser whose success is desired after applications ofparser.Declaration
Swift
@warn_unused_result public func many<Token, ManyResult, ThenResult>(parser: Parser<Token, ManyResult>, then: Parser<Token, ThenResult>) -> Parser<Token, ([ManyResult], ThenResult)>Parameters
parserThe parser to run repeatedly.
thenThe parser whose success is desired after applications of
parser. -
Constructs a
Parserthat will runparser1 or more times, as many times as possible, and then will runthen. Ifthenfails, the parser will undo runs ofparseruntilthensucceeds, otherwise will throw aParseError. On success, returns a tuple with the resulting array from applyingparseron the left and with the result ofthenon the right.Parameter
Parameter parser: The parser to run repeatedly.Parameter
Parameter then: The parser whose success is desired after applications ofparser.Declaration
Swift
@warn_unused_result public func many1<Token, ManyResult, ThenResult>(parser: Parser<Token, ManyResult>, then: Parser<Token, ThenResult>) -> Parser<Token, ([ManyResult], ThenResult)>Parameters
parserThe parser to run repeatedly.
thenThe parser whose success is desired after applications of
parser. -
Constructs a
Parserthat will runparser0 or more times, as few times as possible, such thatthenwill succeed afterwards. If no number of successful applications ofparserallowsthento succeed, throws aParseError. On success, returns a tuple with the resulting array from applyingparseron the left and with the result ofthenon the right.Parameter
Parameter parser: The parser to run repeatedly.Parameter
Parameter then: The parser whose success is desired after applications ofparser.Declaration
Swift
@warn_unused_result public func few<Token, ManyResult, ThenResult>(parser: Parser<Token, ManyResult>, then: Parser<Token, ThenResult>) -> Parser<Token, ([ManyResult], ThenResult)>Parameters
parserThe parser to run repeatedly.
thenThe parser whose success is desired after applications of
parser. -
Constructs a
Parserthat will runparser1 or more times, as few times as possible, such thatthenwill succeed afterwards. If no number of successful applications ofparserallowsthento succeed, throws aParseError. On success, returns a tuple with the resulting array from applyingparseron the left and with the result ofthenon the right.Parameter
Parameter parser: The parser to run repeatedly.Parameter
Parameter then: The parser whose success is desired after applications ofparser.Declaration
Swift
@warn_unused_result public func few1<Token, ManyResult, ThenResult>(parser: Parser<Token, ManyResult>, then: Parser<Token, ThenResult>) -> Parser<Token, ([ManyResult], ThenResult)>Parameters
parserThe parser to run repeatedly.
thenThe parser whose success is desired after applications of
parser. -
Constructs a
Parserthat will runparserexactlycounttimes 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 runparser.Declaration
Swift
@warn_unused_result public func repeated<Token, Result>(parser: Parser<Token, Result>, exactCount: Int) -> Parser<Token, [Result]>Parameters
parserThe parser to run repeatedly.
exactCountThe number of times to run
parser. -
Constructs a
Parserthat will runparsera maximum ofcounttimes 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 runparser.Declaration
Swift
@warn_unused_result public func repeated<Token, Result>(parser: Parser<Token, Result>, maxCount: Int) -> Parser<Token, [Result]>Parameters
parserThe parser to run repeatedly.
maxCountThe maximum number of times to run
parser. -
Constructs a
Parserthat will runparsera minimum ofcounttimes 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 runparser.Declaration
Swift
@warn_unused_result public func repeated<Token, Result>(parser: Parser<Token, Result>, minCount: Int) -> Parser<Token, [Result]>Parameters
parserThe parser to run repeatedly.
minCountThe minimum number of times to run
parser. -
Constructs a
Parserthat will runparsera number of times withinintervaland 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
parserThe parser to run repeatedly.
intervalThe interval describing the allowed number of times to run parser.
-
Constructs a
Parserthat will runparsera number of times withinintervaland 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
parserThe parser to run repeatedly.
intervalThe interval describing the allowed number of times to run parser.
-
Constructs a
Parserthat will attempt to parse withparser, but will backtrack and returnnilon failureParameter
Parameter parser: The parser to run.Declaration
Swift
@warn_unused_result public func optional<Token, Result>(parser: Parser<Token, Result>) -> Parser<Token, Result?>Parameters
parserThe parser to run.
-
Constructs a
Parserthat will attempt to parse withparser, but will backtrack and returnnilon failureParameter
Parameter parser: The parser to run.Declaration
Swift
@warn_unused_result public func optional<Token, Result>(parser: Parser<Token, [Result]>) -> Parser<Token, [Result]>Parameters
parserThe parser to run.
-
Constructs a
Parserthat will attempt to parse withparser, but will backtrack and returnotherwiseon failureParameter
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
parserThe parser to run.
otherwiseThe default value to return on failure.
-
Construct a
Parserthat 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
characterThe character to match against.
-
Constructs a
Parserthat 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
textThe string of text to match against.
-
Constructs a
Parserthat consumes a single token and returns the token if it is within the stringtext.Parameter
Parameter text: TheStringthat the input is tested against.Declaration
Swift
@warn_unused_result public func within(text: String) -> Parser<Character, Character>Parameters
textThe
Stringthat the input is tested against.
Functions Reference