npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

parselmouth

v1.0.0

Published

A combinatory parser library

Downloads

3

Readme

ParselMouth

A combinatory parser library

This is my attempt at creating a combinatory parser library that feels easy to use and allows for easy usage. It attempts to maximize DX (Developer exprience) while staying coherent to its style.

How do I start?

Simply import the module and call its main function, creating a new parselMouth instance:

import parselMouth from "parselMouth";

const pm = parselMouth();

You can add parsers through the pm.addParser method. All parsers are methods of the parselMouth instance, which can be used alone to create more complex parsers or used inside pm.addParser to finally be parser when you run the parselMouth instance with pm.run. To retrieve results or errors, you can use pm.getResults and pm.getErrors.

Parsers

Here an example of some parsers:

  • pm.char: Validates, that the parsed char is the same as the char given as argument.
  • pm.string: Validates, that the parsed string equals the string given as argument.
  • pm.digit: Validates, that the parsed char is a digit.
  • pm.letter: Validates, that the parsed char is a letter. There is also pm.upperCaseLetter and pm.lowerCaseLetter for easier use.
  • pm.not: Validates, that the parsed char or string does not valiate the parser given as argument.
  • pm.many: Validates the parsed string with a given parser as many times as it can.
  • pm.and: Validates the current position of the code with each given parser. It does not travel along the code
  • pm.chain: Validates the parsed code with each of the parsers applied one after another, moving on in the parsed code.
  • pm.same: Validates, that the parsed position in the code is the same as the captured results of another given parser.
  • pm.ahead: Validates, that the position in front of the current one fits the given parser.
  • pm.back: Validates, that the position behind the current one fits the given parser.

Many more, feel free to explore!

With this, you could build something like that:

import parselMouth from "parselMouth";

const pm = parselMouth();

const usedQuote = pm.choice(pm.char("'"), pm.char("\""), pm.char("´"));
const endQuote = pm.same(usedQuote);
const stringInsides = pm.many(pm.not(endQuote));

const parsedString = pm.chain(usedQuote, stringInsides, endQuote);

pm.addParser(parsedString);
pm.run("'Hello world'");

console.log(pm.getResults);

Parser methods

Parsers also have their own methods, such as:

  • parser.join: Joins the captured group into an array with a single concatenated string. Returns the parser for chaining.
  • parser.map: Allows you to set a mapping function that will be run on the result of the next parsed code. Returns the parser for chaining.
  • parser.error: Sets the error message of a parser. Returns the parser for chaining.
  • parser.getError: Returns the errormessage set for the partser.
  • parser.getResult: Returns the result from the last parsed code.

Let's revisit our code!

import parselMouth from "parselMouth";

const pm = parselMouth();

const usedQuote = pm.choice(pm.char("'"), pm.char("\""), pm.char("´"));
const endQuote = pm.same(usedQuote).error("Error: Did not find matching quotes at end of string.");
const stringInsides = pm.many(pm.not(endQuote));

const parsedString = pm.chain(usedQuote, stringInsides, endQuote).join();

pm.addParser(parsedString);
pm.run("'Hello world'");

console.log(pm.getResults);