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

nearley-there

v1.0.0

Published

A nicer programmatic interface for Nearley.js

Downloads

2

Readme

nearley-there

Nearley.js is a fantastic parser toolkit for Javascript. However I've found it's programmatic interface a bit lacking. nearley-there fills in the gaps and make it easy to programmtically test and compile Nearley grammars into functional code.

Note: nearley-there is a development tool and you should compile your grammars and not use this lib for production.

install

npm install nearley
npm install --save-dev nearley-there

The compiled grammars produced by nearley-there require nearley to run, but nearley-there shouldn't be used in production.

usage

const N = require('nearley-there');
const fs = require('fs');

const colorGrammar = fs.readFileSync('./csscolor.ne', 'utf8');

//N.parse takes a grammar string and target
const color = N.parse(colorGrammar, '#333');

//If can also take file paths to grammars
const four = N.parse('./calculator.ne', '1+3');


//N.compile returns a compiled module for your grammar, ready-to-use
const compiled = N.compile('./calculator.ne');
fs.writeFileSync('./calculator.js', compiled);

//If you provide a filepath, it'll write the compiled grammar there
N.compile(colorGrammar, './color.js');

api

.compile(grammar|path, [filepath])

Takes a grammar or a path to a grammar, compiles it, and returns the compiled parser as a string. If a filepath is provided it will write the result to a new file there.

const compiled = N.compile('./calculator.ne');
fs.writeFileSync('./calculator.js', compiled);

N.compile(colorGrammar, './color.js');
const ColorParser = require('./color.js');
const color = ColorParser('#345');

.parse(grammar|path, target)

Takes a grammar or a path to a grammar, compiles it, then parses the target through the compiled grammar and returns the result. This function will not throw errors if the target does not match the grammar, it will instead just return the parsing errors. It will throw errors if the grammar is invalid.

Warning: Uses eval(). Don't use this other than for testing.

const color = N.parse(colorGrammar, '#333');

const four = N.parse('./calculator.ne', '1+3');

.unparse(grammar|path, [depth=5])

Nearley also has the ability to do unparsing, where it can take a grammar and produce random strings that conform to that grammar.

.unparse() takes a grammar or a path to a grammar, compiles it, the runs an unparser on it executing to the depth. This function may throw an error if the depth provided is too shallow ofr it to generate any results.

const randomColor = N.unparse(colorGrammar, 10);

//randomColor -> '#3471a3';

.generator(grammar|path, [filepath])

Takes a grammar or a path to a grammar, compiles it, integrates in the unparser and returns the generator as a string. If a filepath is provided it will write the result to a new file there.

N.generator('./color.ne', './color.rand.js');

const RandColor = require('./color.rand.js');
const randomColor = RandColor(7);

//randomColor -> '#3471a3';

compiled parsers

Compiled parsers are ready-to-use js files that can be required and used as a parsing function. This is what is used to produce a compiled parser.

const Nearley = require('nearley');

/** Generated by Nearley.js **/
[NEARLEY COMPILED GRAMMAR]
/** End **/

const CompiledGrammar = new Nearley.Parser(grammar.ParserRules,grammar.ParserStart).grammar;

module.exports = (input)=>{
	return (new Nearley.Parser(CompiledGrammar))
		.feed(input)
		.results[0];
};

usage

//On build
const N = require('nearley-there');
N.compile('./calculator.ne', './calculator.js');

//After build
const Calculator = require('./calculator.js');

Calculator('1+4') // -> 5