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

gparse

v0.1.2

Published

A Generalised Parser Combinator library in Typescript that enables the creation of efficient and reliable parsers for any possible Context Free Grammar (CFG).

Downloads

3

Readme

gparse

gparse is a Parser Combinator library in Typescript that enables the creation of efficient and reliable parsers for any possible Context Free Grammar (CFG).

Feature list

  • 0 external dependencies.
  • Tested using BDD concepts.
  • Rich documentation using TSDoc and TypeDoc [here].
  • Minimal parser combinator set.
  • Best (known) time complexities on all parser combinator implementations.
  • Can create parsers for any CFG grammar.
  • Full support for semantic data and parse tree generation.
  • Includes error recovery capabilites.

Installation

gparse is available on npm and can be installed via:

    npm install gparse

Usage

A simple calculator in gparse can be implemented as follows:

import * as gparse from 'gparse';

// Error definitions
const eof = new gparse.StaticSemantics("EOF", null);
const match = new gparse.StaticSemantics("match", null);
const divideByZeroError = new gparse.StaticSemantics('', "DivideByZeroError");

// Token definitions

const number: gparse.SymbolParser<any, any> = gparse.SymbolParser.toSymbol(gparse.map(gparse.regex(/^[0-9]+/, (_) => eof, () => match), (state) => new gparse.StaticSemantics('', +state.result[state.result.length - 1]), (state) => state.error));
const lparen: gparse.SymbolParser<any, any> = gparse.SymbolParser.toSymbol(gparse.str("(", (_) => eof, (_) => match));
const rparen: gparse.SymbolParser<any, any> = gparse.SymbolParser.toSymbol(gparse.str(")", (_) => eof, (_) => match));
const addOp: gparse.SymbolParser<any, any> = gparse.SymbolParser.toSymbol(gparse.str("+", (_) => eof, (_) => match));
const minusOp: gparse.SymbolParser<any, any> = gparse.SymbolParser.toSymbol(gparse.str("-", (_) => eof, (_) => match));
const multiplyOp: gparse.SymbolParser<any, any> = gparse.SymbolParser.toSymbol(gparse.str("*", (_) => eof, (_) => match));
const divideOp: gparse.SymbolParser<any, any> = gparse.SymbolParser.toSymbol(gparse.str("/", (_) => eof, (_) => match));

// Symbols

const primary: gparse.SymbolParser<any, any> = gparse.SymbolParser.lazy(() => gparse.alternatives([
    number,
    gparse.chain([lparen, expression, rparen], (data) => {
        return new gparse.StaticSemantics('', data[2].value);
    }),
]));
const term: gparse.SymbolParser<any, any> = gparse.SymbolParser.lazy(() => gparse.alternatives([
    gparse.chain([term, multiplyOp, primary], (data) => {
        return new gparse.StaticSemantics('', data[0].value * data[2].value);
    }),
    gparse.chain([term, divideOp, gparse.assert(primary, (state) => {
                if (!state.isError && state.data.value === 0) {
                    return divideByZeroError;
                } else {
                    return null;
                }
            })], (data) => {
        return new gparse.StaticSemantics('', data[0].value / data[2].value);
    }),
    primary,
]));
const expression: gparse.SymbolParser<any, any> = gparse.SymbolParser.lazy(() => gparse.alternatives([
    gparse.chain([expression, addOp, term], (data) => {
        return new gparse.StaticSemantics('', data[0].value + data[2].value);
    }),
    gparse.chain([expression, minusOp, term], (data) => {
        return new gparse.StaticSemantics('', data[0].value - data[2].value);
    }),
    term,
]));

console.log("1+2-3+4*5-10/5");  // 18

Full documentation on the library can be found here.

Also, a Cookbook with many more examples is under developement.

Status

Status: Beta

It is suggested that it is NOT used in production before it reaches stable, as there may be critical bugs that I am unaware of.

Credits

gparse was heavily inspired by the work of arcsecond. The modified functional version of the GLL parsing algorithm is inspired by this.

Bibliography

License

gparse is made available under the MIT License. A copy of the license can be found in the project's repo.