Methods
-
bind(parser, fun)
-
Returns a parser that calls
fun
on the value resulting from runningparser
on the current parsing state. Fails without executingfun
ifparser
fails.Parameters:
Name Type Description parser
Parser The parser to execute.
fun
Function 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 constructor
Function 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
true
if 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 msg
String Message to report with the failure.
type
String A type to apply to the ParserError.
-
is(predicate, parser)
-
Returns a parser that succeeds if
predicate
returns true when called on a parser's result.Parameters:
Name Type Argument Default Description predicate
Function Tests a parser's result.
parser
Parser <optional>
token() Parser to run.
Example
parse(is(function(x) { return x === "a"; }), "a"); // => "a"
-
isNot(predicate, parser)
-
Returns a parser that succeeds if
predicate
returns false when called on a parser's result.Parameters:
Name Type Argument Default Description predicate
Function Tests a parser's result.
parser
Parser <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
parser
failure by replacing its error messages withmsg
.Parameters:
Name Type Description parser
Parser Parser whose errors to replace.
msg
String 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 parser
Parser Parser to wrap.
tag
String Tag to use when logging messages.
level
String <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 test
Parser 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 transformer
Function Function called on
parser
's value. Its return value will be used as themap
parser's value.parser
Parser 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 parser
Parser Parser whose value will be tagged.
tag
String 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 count
Integer <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"