Security News
Namecheap Takes Down Polyfill.io Service Following Supply Chain Attack
Polyfill.io has been serving malware for months via its CDN, after the project's open source maintainer sold the service to a company based in China.
parsimmon
Advanced tools
Package description
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.
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 } }
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.
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 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.
Changelog
1.18.1 (2021-12-19)
Readme
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.
Everyone is expected to abide by the Contributor Covenant. Please send reports to brian@wavebeem.com.
See the examples directory for annotated examples of parsing JSON, Lisp, a Python-ish language, and math.
.createLanguage(parsers)
.string(string)
.regexp(regexp)
.seq(p1, p2, ...pn)
.sepBy(content, separator)
.alt(p1, p2, ...pn)
.whitespace
.index
parser.map(fn)
parser.node(name)
parser.skip(otherParser)
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.
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!
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 implements Semigroup, Apply, Applicative, Functor, Chain, and Monad.
FAQs
A monadic LL(infinity) parser combinator library
The npm package parsimmon receives a total of 94,870 weekly downloads. As such, parsimmon popularity was classified as popular.
We found that parsimmon demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Polyfill.io has been serving malware for months via its CDN, after the project's open source maintainer sold the service to a company based in China.
Security News
OpenSSF is warning open source maintainers to stay vigilant against reputation farming on GitHub, where users artificially inflate their status by manipulating interactions on closed issues and PRs.
Security News
A JavaScript library maintainer is under fire after merging a controversial PR to support legacy versions of Node.js.