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

elenpi

v0.5.1

Published

Small DSL for LL(1) parser

Downloads

24

Readme

elenpi

Travis branch bitHound Overall Score Coverage Status npm-downloads dependecies dev-dependencies Conventional Commits npm licence

Small javascript LL(1) Parser generator through simple and expressive DSL.

Allow to describe parser rules with structured sentences based on Method Chaining.

  • really short, clear, clean and maintenable parser rules
  • less than 2ko gzip/minified
  • easy DSL prototyping
  • quite fast

Take a look at examples in ./examples.

Esdoc references

Rule's Api

Create a rule instance :

var rule = new Rule();

or using shortcut :

var  r = Rule.initializer;
var rule = r.oneOf(...);

The Atom :

The fundamental atom of elenpi : a simple handler that take an object (env) containing the rest of the string to parse and the errors report, and the current descriptor where to store catched information.

.done(function(env, descriptor){
	// place information in current descriptor
	descriptor.foo = env.string...;
	// when information catched : your responsible to replace THE REST of the string in env.
	env.string = env.string.substring(...);
}) : Rule

Recognize a terminal (aka try to match a regexp at beginning of current string) :

.terminal(RegExp, ?String || ?function(env, descriptor, captured){
	descriptor.something = captured[1]; // example
}) : Rule

Second argument :

  • could be the name of the property in descriptor where elenpi will store first value of the regexp matching output ("captured" below, which is an array with captured parts)
  • could be a function that receive the "env" object, the current descriptor, and the "captured" regexp output. You're free to do what you want. You should not manage the rest of the string (it will be done by elenpi).

Recognize a single char, that will not be stored in descriptor :

.char( String ) : Rule

Need the end of string there :

.end() : Rule

Use a rule, either by name (will be found in parser's rules), or by providing a rule instance (that will be "inserted" there) :

.use(ruleName:String||rule:Rule) : Rule

Skip an element : Place a skip:true in current descriptor and so elenpi will ignored it :

.skip() : Rule

Recognized an optional space (/^\s+/ : one or more) :

.space(needed = false) : Rule

Force error if parser execute it (Only aimed to be used in .oneOf() as last rule - so if no other rule have been matched -> force error) :

.error(msg): Rule

Match elements in strings through one rule (maybe optional) :

.one(rule || { 
	rule:rule, 
	?as:function(){ return Instance }, 
	?set:'name' || function(env, parent, descriptor){ ... } 
}) : Rule
.maybeOne(rule || { 
	rule:rule, 
	?as:function(){ return Instance }, 
	?set:'name' || function(env, parent, descriptor){ ... } 
}) : Rule
  • rule : the rule to apply (ruleName:String||rule:Rule)
  • as : optional, a function that return a descriptor instance to use with the provided rule
  • set : optional,
    • either a string that give the property's name where to store descriptor (the one created by the "as" function) in parent descriptor
    • either a function that receive "env", the parent descriptor, and the descriptor.

Match elements in strings through one of provided rules :

.oneOf(...rules || { 
	rules:[rules], 
	?as:function(){ return Instance }, 
	?set:'name' || function(env, parent, descriptor){ ... } 
}) : Rule
.maybeOneOf(...rules || {
	rules:[rules], 
	?as:function(){ return Instance }, 
	?set:'name' || function(env, parent, descriptor){ ... } 
})

Match x or more elements in string with provided rule (and maybe a separator rule) :

.xOrMore({ 
	rule:rule,
	minimum:int = 0,
	?as:function(){ return Instance }, 
	?pushTo:'name' || function(env, parent, descriptor){ ... },
	?separator:rule,
	?maximum:int = Infinity
}) : Rule
  • rule : the rule to apply (ruleName:String||rule:Rule)
  • minimum: the minimum number of elements to recognize (default 0)
  • as : optional, a function that return a descriptor instance to use with the provided rule
  • pushTo : optional,
    • either a string that give the property's name where to PUSH descriptor (the one created by the "as" function) in parent descriptor
    • either a function that receive "env", the parent descriptor, and the descriptor.
  • separator: optional, a rule that express the possible separator between recognized elements. (by example a coma between items in an array)
  • maximum: optional, the maximum number of elements to recognize (default Infinity)

xOrMore shortcuts :

.zeroOrMore(opt /* as above in xOrMore */) : Rule
.oneOrMore(opt /* as above in xOrMore (minimum = 1) */) : Rule

Parser's API

Constructor :

var parser = new Parser(rulesObject, 'defaultRulesName');
var  r = parser.parse('a string to parse', ?ruleToApply); // parse until the end of string
// r is false if parsing failed
// Otherwise, r is a descriptor object containing catched properties
var descriptor = {};
var  r = parser.parse('a string to parse', ruleToApply = null, objectWhereStoreTokens = null);

// r is false if parsing failed
// Otherwise, r is the string that still to be parsed

// If parsing succeed, descriptor has been decorated with catched properties

see parsers examples in ./examples

Licence

The MIT License

Copyright (c) 2015-2017 Gilles Coomans [email protected]

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.