Functions
The following functions are available globally.
-
Constructs a
Parser
that runs attempts to runparser
and backtracks on failure. On successful parse, this parser returns the result ofparse
; on failure, this parser catches the error, rewinds theStream
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 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
parsers
The sequence of parsers to sequentially run.
-
Constructs a
Parser
that 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
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 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
parsers
The sequence of parsers to sequentially run.
-
Constructs a
Parser
that 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
parsers
The variadic list of parsers to sequentially run.
-
Constructs a
Parser
that 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 +<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 parsefirst
and then will parseothers
, prepending the result offirst
to 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
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 parseothers
and then will parselast
, appending the result oflast
to 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
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 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
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 runparser
and 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
parser
The parser to be run.
-
Constructs a
Parser
that 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
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 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
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 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
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 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
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 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
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 itsParser
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 inparsers
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 ofparsers
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 withrightParser
if and only ifleftParser
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.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 withrightParser
wheneverleftParser
fails. Note that the two parsers need not be the same type as anEither
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 runleftParser
followed byrightParser
, discarding the result fromrightParser
and 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
leftParser
The parser whose result will be propagated.
rightParser
The parser whose result will be discarded.
-
Constructs a
Parser
that will runleftParser
followed byrightParser
, discarding the result fromleftParser
and 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
leftParser
The parser whose result will be discarded.
rightParser
The parser whose result will be propagated.
-
Constructs a
Parser
that will runparser
1 or more times, as many times as possible parsingparser
separated 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
parser
The parser to run repeatedly.
delimiter
The parser to separate each occurance of
parser
. -
Constructs a
Parser
that will runparser
0 or more times, as many times as possible parsingparser
separated 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
parser
The parser to run repeatedly.
delimiter
The parser to separate each occurance of
parser
. -
Constructs a
Parser
that will runleft
followed byparser
followed byright
, discarding the result fromleft
andright
and 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
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 runleft
followed byparser
followed byright
, discarding the result fromleft
andright
and 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
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 runparser
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 runparser
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 runparser
0 or more times, as many times as possible, and then will runthen
. Ifthen
fails, the parser will undo runs ofparser
untilthen
succeeds, otherwise will throw aParseError
. On success, returns a tuple with the resulting array from applyingparser
on the left and with the result ofthen
on 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
parser
The parser to run repeatedly.
then
The parser whose success is desired after applications of
parser
. -
Constructs a
Parser
that will runparser
1 or more times, as many times as possible, and then will runthen
. Ifthen
fails, the parser will undo runs ofparser
untilthen
succeeds, otherwise will throw aParseError
. On success, returns a tuple with the resulting array from applyingparser
on the left and with the result ofthen
on 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
parser
The parser to run repeatedly.
then
The parser whose success is desired after applications of
parser
. -
Constructs a
Parser
that will runparser
0 or more times, as few times as possible, such thatthen
will succeed afterwards. If no number of successful applications ofparser
allowsthen
to succeed, throws aParseError
. On success, returns a tuple with the resulting array from applyingparser
on the left and with the result ofthen
on 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
parser
The parser to run repeatedly.
then
The parser whose success is desired after applications of
parser
. -
Constructs a
Parser
that will runparser
1 or more times, as few times as possible, such thatthen
will succeed afterwards. If no number of successful applications ofparser
allowsthen
to succeed, throws aParseError
. On success, returns a tuple with the resulting array from applyingparser
on the left and with the result ofthen
on 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
parser
The parser to run repeatedly.
then
The parser whose success is desired after applications of
parser
. -
Constructs a
Parser
that will runparser
exactlycount
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 runparser
.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 runparser
a maximum ofcount
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 runparser
.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 runparser
a minimum ofcount
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 runparser
.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 runparser
a number of times withininterval
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 runparser
a number of times withininterval
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 withparser
, but will backtrack and returnnil
on 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
parser
The parser to run.
-
Constructs a
Parser
that will attempt to parse withparser
, but will backtrack and returnnil
on 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
parser
The parser to run.
-
Constructs a
Parser
that will attempt to parse withparser
, but will backtrack and returnotherwise
on 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
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 stringtext
.Parameter
Parameter text: TheString
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.