Methods
-
bind(parser, fun)
-
Returns a parser that calls
funon the value resulting from runningparseron the current parsing state. Fails without executingfunifparserfails.Parameters:
Name Type Description parserParser The parser to execute.
funFunction Function called with the resulting value of
parser. Must return a parser.Example
parse(bind(token(), function(x) { return value(x+"!"); }), "a"); // => "a!" -
delay(constructor, args)
-
Delays calling of a parser constructor function until parse-time. Useful for recursive parsers that would otherwise blow the stack at construction time.
Parameters:
Name Type Argument Description constructorFunction A function that returns a Parser.
args* <repeatable>
Arguments to apply to the constructor.
Example
// The following would usually result in an infinite loop: function foo() { return or(x(), foo()); } // But you can use delay() to remedy this... function foo() { return or(x(), delay(foo)); } -
eof()
-
Returns a parser that succeeds with a value of
trueif there is no more input to consume.Example
parse(eof(), ""); // => true
-
fail(msg, type)
-
Returns a parser that always fails without consuming input. Automatically includes the line and column positions in the final ParserError.
Parameters:
Name Type Description msgString Message to report with the failure.
typeString A type to apply to the ParserError.
-
is(predicate, parser)
-
Returns a parser that succeeds if
predicatereturns true when called on a parser's result.Parameters:
Name Type Argument Default Description predicateFunction Tests a parser's result.
parserParser <optional>
token() Parser to run.
Example
parse(is(function(x) { return x === "a"; }), "a"); // => "a" -
isNot(predicate, parser)
-
Returns a parser that succeeds if
predicatereturns false when called on a parser's result.Parameters:
Name Type Argument Default Description predicateFunction Tests a parser's result.
parserParser <optional>
token() Parser to run.
Example
parse(isNot(function(x) { return x === "a"; }), "b"); // => "b" -
label(parser, msg)
-
Returns a parser that will label a
parserfailure by replacing its error messages withmsg.Parameters:
Name Type Description parserParser Parser whose errors to replace.
msgString Error message to replace errors with.
Example
parse(token(), ""); // => unexpected eof parse(label(token(), "thing"), ""); // => expected thing
-
log(parser, tag, level)
-
Debugger parser that logs the ParserState with a tag.
Parameters:
Name Type Argument Default Description parserParser Parser to wrap.
tagString Tag to use when logging messages.
levelString <optional>
"log" 'log', 'info', 'debug', 'warn', 'error'.
-
lookAhead(test)
-
Returns a parser that runs a given parser without consuming input, while still returning a success or failure.
Parameters:
Name Type Description testParser Parser to execute.
Example
parse(and(lookAhead(token()), token()), "a"); // => "a"
-
map(transformer, parser)
-
Returns a parser that transforms the resulting value of a successful application of its given parser. This function is a lot like
bind, except it always succeeds if its parser succeeds, and is expected to return a transformed value, instead of another parser.Parameters:
Name Type Description transformerFunction Function called on
parser's value. Its return value will be used as themapparser's value.parserParser Parser that will yield the input value.
Example
parse(map(parseFloat, text()), "1234.5"); // => 1234.5
-
tag(parser, tag)
-
Returns a parser that returns an object with a single key whose value is the result of the given parser.
Parameters:
Name Type Description parserParser Parser whose value will be tagged.
tagString String to use as the object's key.
Example
parse(tag(token(), "myToken"), "a"); // => {myToken: "a"} -
token(count)
-
Returns a parser that consumes a single item from the input, or fails with an unexpected eof error if there is no input left.
Parameters:
Name Type Argument Default Description countInteger <optional>
1 number of tokens to consume. Must be > 0.
Example
parse(token(), "a"); // => "a"
-
value(val)
-
Returns a parser that always succeeds without consuming input.
Parameters:
Name Type Argument Default Description val<optional>
undefined value to use as this parser's value.
Example
parse(value("foo"), ""); // => "foo"