Methods
-
and(parsers)
-
Returns a parser that succeeds if all the parsers given to it succeed. The returned parser uses the value of the last successful parser.
Parameters:
Name Type Argument Description parsersParser <repeatable>
One or more parsers to execute.
Example
parse(and(token(), token()), "ab"); // => "b"
-
between(open, close, parser)
-
Returns a parser that results in a value between an opening and closing parser.
Parameters:
Name Type Description openParser Opening parser.
closeParser Closing parser.
parserParser Parser to return the value of.
Example
parse(between(string("("), string(")"), token()), "(a)"); // => "a" -
collect(parser, opts)
-
Returns a parser that results in an array of
mintomaxmatches ofparserParameters:
Name Type Argument Description parserParser Parser to match.
optsObject <optional>
Properties
Name Type Argument Default Description minInteger <optional>
0 Minimum number of matches.
maxInteger <optional>
Infinity Maximum number of matches.
Example
parse(collect(token()), "abcd"); // => ["a", "b", "c", "d"]
-
exactly(parser, n)
-
Returns a parser that results in an array of exactly
nresults forparser.Parameters:
Name Type Description parserParser The parser to collect results for.
nInteger exact number of results to collect.
Example
parse(exactly(token(), 4), "abcd"); // => ["a", "b", "c", "d"]
-
followedBy(parser, moreParsers)
-
Returns a parser that returns the result of its first parser if it succeeds, but fails if any of the following parsers fail.
Parameters:
Name Type Argument Description parserParser The value of this parser is returned if it succeeds.
moreParsersParser <repeatable>
These parsers must succeed in order for
followedByto succeed.Example
parse(followedBy(string("a"), string("b")), "ab"); // => "a" -
maybe(parser)
-
Returns a parser that returns the result of
parserif it succeeds, otherwise succeeds with a value ofundefinedwithout consuming input.Parameters:
Name Type Description parserParser Parser to try.
Example
parse(maybe(token()), ""); // => undefined
-
not(parser)
-
Returns a parser that succeeds if
parserfails. Does not consume.Parameters:
Name Type Description parserParser parser to test.
Example
parse(and(not(string("a")), token()), "b"); // => "b" -
or(parsers, label)
-
Returns a parser that succeeds if one of the parsers given to it suceeds. Uses the value of the first successful parser.
Parameters:
Name Type Argument Description parsersParser <repeatable>
One or more parsers to execute.
labelString <optional>
Label to replace the full message with.
Example
parse(or(string("foo"), string("bar")), "bar"); // => "bar" -
range(start, end, parser, predicate)
-
Returns a parser that accepts a parser if its result is within range of
startandend.Parameters:
Name Type Argument Default Description start* lower bound of the range to accept.
end* higher bound of the range to accept.
parserParser <optional>
token() parser whose results to test
predicateFunction <optional>
function(x,y){return x<=y; } Tests range
Example
parse(range("a", "z"), "d"); // => "d" -
sequence(fun)
-
Returns a parser that will execute
funwhile handling the parserState internally, allowing the body offunto be written sequentially. The purpose of this parser is to simulatedonotation and prevent the need for heavily-nestedbindcalls.The
funcallback will receive a functionswhich should be called with each parser that will be executed, which will update the internal parserState. The return value of the callback must be a parser.If any of the parsers fail, sequence will exit immediately, and the entire sequence will fail with that parser's reason.
Parameters:
Name Type Description funSequenceFn A sequence callback function to execute.
Example
mona.sequence(function(s) { var x = s(mona.token()); var y = s(mona.string('b')); return mona.value(x+y); }); -
skip(parser)
-
Returns a parser that skips input until
parserstops matching.Parameters:
Name Type Description parserParser Determines whether to continue skipping.
Example
parse(and(skip(string("a")), token()), "aaaab"); // => "b" -
split(parser, separator, opts)
-
Returns a parser that returns an array of results that have been successfully parsed by
parser, which were separated byseparator.Parameters:
Name Type Argument Description parserParser Parser for matching and collecting results.
separatorParser Parser for the separator
optsObject <optional>
Properties
Name Type Argument Default Description minInteger <optional>
0 Minimum length of the resulting array.
maxInteger <optional>
0 Maximum length of the resulting array.
Example
parse(split(token(), space()), "a b c d"); // => ["a","b","c","d"]
-
splitEnd(parser, separator, opts)
-
Returns a parser that returns an array of results that have been successfully parsed by
parser, separated and ended byseparator.Parameters:
Name Type Argument Description parserParser Parser for matching and collecting results.
separatorParser Parser for the separator
optsObject <optional>
Properties
Name Type Argument Default Description enforceEndInteger <optional>
true If true,
separatormust be at the end of the parse.minInteger <optional>
0 Minimum length of the resulting array.
maxInteger <optional>
Infinity Maximum length of the resulting array.
Example
parse(splitEnd(token(), space()), "a b c "); // => ["a", "b", "c"]
-
unless(notParser, moreParsers)
-
Returns a parser that works like
and, but fails if the first parser given to it succeeds. Likeand, it returns the value of the last successful parser.Parameters:
Name Type Argument Description notParserParser If this parser succeeds,
unlesswill fail.moreParsersParser <repeatable>
Rest of the parses to test.
Example
parse(unless(string("a"), token()), "b"); // => "b"
Type Definitions
-
SequenceFn(s) → {Parser}
-
Called by
sequenceto handle sequential syntax for parsing. Called with ans()function that must be called each time a parser should be applied. Thes()function will return the unwrapped value returned by the parser. If any of thes()calls fail, this callback will exit with an appropriate failure message, and none of the subsequent code will execute.Note that this callback may be called multiple times during parsing, and many of those calls might partially fail, so side-effects should be done with care.
A
sequencecallback must return aParser.Parameters:
Name Type Description sFunction Sequencing function. Must be wrapped around a parser.
Returns:
parser - The final parser to apply before resolving
sequence.- Type
- Parser