Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

parsimmon

Package Overview
Dependencies
Maintainers
2
Versions
49
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

parsimmon

A monadic LL(infinity) parser combinator library

  • 1.18.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
2
Created

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

Keywords

FAQs

Package last updated on 20 Dec 2021

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc