A monadic LL(infinity) parser combinator library
Parsimmon supports IE7 and newer browsers, along with Node.js. It can be used as a standard Node module through npm (named
parsimmon), or directly in the browser through a script tag, where it exports a global variable called
Parsimmon. To download the latest browser build, use the npmcdn version. For more information on how to use npmcdn, see the npmcdn homepage.
See the examples directory for annotated examples of parsing JSON, Lisp, and math.
A Parsimmon parser is an object that represents an action on a stream of text, and the promise of either an object yielded by that action on success or a message in case of failure. For example,
Parsimmon.string('foo') yields the string
'foo' if the beginning of the stream is
'foo', and otherwise fails.
.map is used to transform the yielded value. For example,
'foobar' if the stream starts with
'foo'. The parser
will yield the number
24 when it encounters the string
.parse(string) on a parser parses the string and returns an object with a boolean
status flag, indicating whether the parse succeeded. If it succeeded, the
value attribute will contain the yielded value. Otherwise, the
expected attributes will contain the index of the parse error (with
column properties), and a sorted, unique array of messages indicating what was expected.
The error object can be passed along with the original source to
Parsimmon.formatError(source, error) to obtain a human-readable error string.
Thanks to @bd82 we have a good benchmark comparing Parsimmon CPU performance to several other parser libraries with a simple JSON parser example.
Parsimmon is also compatible with fantasyland. It is a Semigroup, an Applicative Functor, and a Monad.