What is parsimmon?
Parsimmon is a library for writing simple, elegant parsers in JavaScript. It allows you to construct parsers for custom languages or data formats using a combinator-based approach.
What are parsimmon's main functionalities?
Basic Parsing
This feature allows you to create a basic parser that matches a specific string. In this example, the parser is set to match the string 'hello'.
const P = require('parsimmon');
const parser = P.string('hello');
console.log(parser.parse('hello')); // { status: true, value: 'hello' }
console.log(parser.parse('world')); // { status: false, expected: ['hello'], index: { offset: 0, line: 1, column: 1 } }
Sequence Parsing
This feature allows you to create a parser that matches a sequence of patterns. In this example, the parser matches the sequence 'hello', whitespace, and 'world'.
const P = require('parsimmon');
const parser = P.seq(P.string('hello'), P.whitespace, P.string('world'));
console.log(parser.parse('hello world')); // { status: true, value: ['hello', ' ', 'world'] }
console.log(parser.parse('hello there')); // { status: false, expected: ['world'], index: { offset: 6, line: 1, column: 7 } }
Custom Parsers
This feature allows you to create custom parsers using regular expressions and combinators. In this example, the parser matches a sequence of digits.
const P = require('parsimmon');
const digit = P.regexp(/[0-9]/).desc('a digit');
const digits = digit.many().map(digits => digits.join(''));
console.log(digits.parse('12345')); // { status: true, value: '12345' }
console.log(digits.parse('abc')); // { status: false, expected: ['a digit'], index: { offset: 0, line: 1, column: 1 } }
Error Handling
This feature allows you to handle parsing errors gracefully by providing alternative parsing options. In this example, the parser matches either 'hello' or 'world'.
const P = require('parsimmon');
const parser = P.string('hello').or(P.string('world'));
console.log(parser.parse('hello')); // { status: true, value: 'hello' }
console.log(parser.parse('world')); // { status: true, value: 'world' }
console.log(parser.parse('foo')); // { status: false, expected: ['hello', 'world'], index: { offset: 0, line: 1, column: 1 } }
Other packages similar to parsimmon
nearley
Nearley is a fast, feature-rich parser toolkit for JavaScript. It uses a different approach called Earley parsing, which can handle more complex grammars compared to Parsimmon. Nearley is more powerful but can be more complex to use.
pegjs
PEG.js is a simple parser generator for JavaScript based on the Parsing Expression Grammar (PEG) formalism. It allows you to define your grammar in a concise syntax and generates a parser from it. PEG.js is more declarative compared to Parsimmon's combinator-based approach.
jison
Jison is a parser generator that converts a context-free grammar into a JavaScript parser. It is similar to tools like Bison or Yacc but for JavaScript. Jison is more suitable for complex language parsing tasks compared to Parsimmon.
Parsimmon
Authors: @jneen and @laughinghan
Maintainer: @wavebeem
Parsimmon is a small library for writing big parsers made up of lots of little parsers. The API is inspired by parsec and Promises/A+.
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 unpkg version. For more information on how to use unpkg, see the unpkg homepage.
Code of Conduct
Everyone is expected to abide by the Contributor Covenant. Please send reports to brian@wavebeem.com.
API Documentation
Examples
See the examples directory for annotated examples of parsing JSON, Lisp, a Python-ish language, and math.
Common Functions
Questions
Feel free to ask a question by filing a GitHub Issue. I'm happy to help point you in the right direction with the library, and hopefully improve the documentation so less people get confused in the future.
Contributing
Contributions are not just pull requests.
Issues clearly describing bugs or confusing parts of Parsimmon are welcome! Also, documentation enhancements and examples are very desirable.
Feeling overwhelmed about contributing? Open an issue about what you want to contribute and I'm happy to help you through to completion!
Performance
Thanks to @bd82 we have a good benchmark comparing Parsimmon CPU performance to several other parser libraries with a simple JSON parser example.
Fantasyland
Parsimmon is also compatible with fantasyland. It implements Semigroup, Apply, Applicative, Functor, Chain, and Monad.