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 parsers
Parser <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 open
Parser Opening parser.
close
Parser Closing parser.
parser
Parser 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
min
tomax
matches ofparser
Parameters:
Name Type Argument Description parser
Parser Parser to match.
opts
Object <optional>
Properties
Name Type Argument Default Description min
Integer <optional>
0 Minimum number of matches.
max
Integer <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
n
results forparser
.Parameters:
Name Type Description parser
Parser The parser to collect results for.
n
Integer 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 parser
Parser The value of this parser is returned if it succeeds.
moreParsers
Parser <repeatable>
These parsers must succeed in order for
followedBy
to succeed.Example
parse(followedBy(string("a"), string("b")), "ab"); // => "a"
-
maybe(parser)
-
Returns a parser that returns the result of
parser
if it succeeds, otherwise succeeds with a value ofundefined
without consuming input.Parameters:
Name Type Description parser
Parser Parser to try.
Example
parse(maybe(token()), ""); // => undefined
-
not(parser)
-
Returns a parser that succeeds if
parser
fails. Does not consume.Parameters:
Name Type Description parser
Parser 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 parsers
Parser <repeatable>
One or more parsers to execute.
label
String <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
start
andend
.Parameters:
Name Type Argument Default Description start
* lower bound of the range to accept.
end
* higher bound of the range to accept.
parser
Parser <optional>
token() parser whose results to test
predicate
Function <optional>
function(x,y){return x<=y; } Tests range
Example
parse(range("a", "z"), "d"); // => "d"
-
sequence(fun)
-
Returns a parser that will execute
fun
while handling the parserState internally, allowing the body offun
to be written sequentially. The purpose of this parser is to simulatedo
notation and prevent the need for heavily-nestedbind
calls.The
fun
callback will receive a functions
which 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 fun
SequenceFn 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
parser
stops matching.Parameters:
Name Type Description parser
Parser 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 parser
Parser Parser for matching and collecting results.
separator
Parser Parser for the separator
opts
Object <optional>
Properties
Name Type Argument Default Description min
Integer <optional>
0 Minimum length of the resulting array.
max
Integer <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 parser
Parser Parser for matching and collecting results.
separator
Parser Parser for the separator
opts
Object <optional>
Properties
Name Type Argument Default Description enforceEnd
Integer <optional>
true If true,
separator
must be at the end of the parse.min
Integer <optional>
0 Minimum length of the resulting array.
max
Integer <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 notParser
Parser If this parser succeeds,
unless
will fail.moreParsers
Parser <repeatable>
Rest of the parses to test.
Example
parse(unless(string("a"), token()), "b"); // => "b"
Type Definitions
-
SequenceFn(s) → {Parser}
-
Called by
sequence
to 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
sequence
callback must return aParser
.Parameters:
Name Type Description s
Function Sequencing function. Must be wrapped around a parser.
Returns:
parser - The final parser to apply before resolving
sequence
.- Type
- Parser