Module: mona/combinators

mona/combinators

Parser combinators for higher-order interaction between parsers.

Source:

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.

Source:
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.

Source:
Example
parse(between(string("("), string(")"), token()), "(a)"); // => "a"

collect(parser, opts)

Returns a parser that results in an array of min to max matches of parser

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.

Source:
Example
parse(collect(token()), "abcd"); // => ["a", "b", "c", "d"]

exactly(parser, n)

Returns a parser that results in an array of exactly n results for parser.

Parameters:
Name Type Description
parser Parser

The parser to collect results for.

n Integer

exact number of results to collect.

Source:
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.

Source:
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 of undefined without consuming input.

Parameters:
Name Type Description
parser Parser

Parser to try.

Source:
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.

Source:
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.

Source:
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 and end.

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

Source:
Example
parse(range("a", "z"), "d"); // => "d"

sequence(fun)

Returns a parser that will execute fun while handling the parserState internally, allowing the body of fun to be written sequentially. The purpose of this parser is to simulate do notation and prevent the need for heavily-nested bind calls.

The fun callback will receive a function s 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.

Source:
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.

Source:
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 by separator.

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.

Source:
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 by separator.

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.

Source:
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. Like and, 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.

Source:
Example
parse(unless(string("a"), token()), "b"); // => "b"

Type Definitions

SequenceFn(s) → {Parser}

Called by sequence to handle sequential syntax for parsing. Called with an s() function that must be called each time a parser should be applied. The s() function will return the unwrapped value returned by the parser. If any of the s() 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 a Parser.

Parameters:
Name Type Description
s Function

Sequencing function. Must be wrapped around a parser.

Source:
Returns:

parser - The final parser to apply before resolving sequence.

Type
Parser