Module: mona/api

mona/api

Parser execution api

Source:

Methods

parse(parser, string, opts)

Executes a parser and returns the result.

Parameters:
Name Type Argument Description
parser Function

The parser to execute.

string String

String to parse.

opts Object <optional>

Options object.

Properties
Name Type Argument Default Description
throwOnError Boolean <optional>
true

If truthy, throws a ParserError if the parser fails and returns ParserState instead of its value.

fileName String <optional>

filename to use for error messages.

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

parseAsync(parser, callback, opts)

Executes a parser asynchronously, returning an object that can be used to manage the parser state. Unless the parser given tries to match eof(), parsing will continue until the parser's done() function is called.

Parameters:
Name Type Argument Description
parser Function

The parser to execute.

callback AsyncParserCallback

node-style 2-arg callback executed once per successful application of parser.

opts Object <optional>

Options object.

Properties
Name Type Argument Description
fileName String <optional>

filename to use for error messages.

Source:
Example
var handle = parseAsync(token(), function(tok) {
 console.log("Got a token: ", tok);
});
handle.data("foobarbaz");

Type Definitions

ParserError

Information about a parsing failure.

Type:
  • Object
Properties:
Name Type Description
position api.SourcePosition

Source position for the error.

messages Array

Array containing relevant error messages.

type String

The type of parsing error.

Source:

SourcePosition

Represents a source location.

Type:
  • Object
Properties:
Name Type Description
name String

Optional sourcefile name.

line Integer

Line number, starting from 1.

column Integer

Column number in the line, starting from 1.

Source: