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

parser-generator

v0.2.0

Published

A dynamic recursive descent LL(k) parser for creating grammar parsing functions in JavaScript.

Downloads

21

Readme

parser-generator.js

A dynamic recursive descent LL(k) parser for creating grammar parsing functions in JavaScript. This library is based on the work of Dan Yoder in the Cruiser.Parse library.

Note: The following was adapted from this wiki page

Introduction

When you reach the limits of regular expressions in JavaScript, you have two choices:

  1. Realize that what you are trying to do is probably not a good idea.
  2. Write yourself a parser generator and keep on going.

The addition of CSS 3 selector support broke our regex-based parser for Behaviors. And there were so many cool things to do with them, too. So we went ahead with option 2.

What this library gives you is the ability to write parsers very easily. We don’t use the YACC-like approach of generating JavaScript code – instead we just build up a big function. It isn’t as fast as code generation, but it is faster than regex, at least for non-trivial parsers (i.e., those that require multiple passes with different regex).

Example

Here's the grammar for CSS (probably not perfect, but good enough for our purposes):

var g = {}, o = require("parser-generator").Operators, t = require("./Translator");

// basic tokens
g.lbrace = o.token("{");
g.rbrace = o.token("}");
g.lparen = o.token(/\(/);
g.rparen = o.token(/\)/);
g.colon = o.token(":");
g.semicolon = o.token(";");
// comments
g.inlineComment = o.token(/\x2F\x2F[^\n]*\n/);
g.multilineComment = o.token(/\x2F\x2A(.|\n)*?\x2A\x2F/);
g.comments = o.ignore(o.any(g.inlineComment, g.multilineComment));
// attributes
g.attrName = o.token(/[\w\-\d]+/);
g.attrValue = o.token(/[^;\}]+/);
g.attr = o.each(g.attrName, g.colon, g.attrValue, g.semicolon);
g.attrList = o.many(o.any(g.comments, g.attr));
// style rules
g.style = o.process(o.between(g.lbrace, g.attrList, g.rbrace), t.style);
g.selector = o.token(/[^\{]+/);
g.rule = o.each(g.selector, g.style);
g.rules = o.process(o.many(o.any(g.comments, g.rule)), t.rules);

Most of it is pretty self-explanatory. The t.* functions (style and rules) transform the output tree from associative arrays to hashes, since, by default, the parser simply puts each matched token into an array.

For example, consider the following translator functions:

var Translator = {
  /**
   * Translate style attributes to an associative array.
   *
   * @param ax The attributes parse tree
   */
  style: function (ax) {
    return _.reduce(ax, function (h, a) {
      if (a) {
        h[a[0]] = a[2];
      }

      return h;
    }, {});
  },
  /**
   * Translate rules to an associative array.
   *
   * @param rx The rules parse tree
   */
  rules: function (rx) {
    return _.reduce(rx, function (h, r) {
      if (r) {
        h[r[0]] = _.extend(h[r[0]] || {}, r[1]);
      }

      return h;
    }, {});
  }
};

Also, functions like pair and between give the parser some additional intelligence beyond just sequences of tokens and alternate paths. The details of each function are provided below.

Functions

Each of the functions described below return a function that will match based on the given criteria. You can then call the returned function with a string to parse. If it parses, it will return an array with the first element being the result of the parsing and the second element being the remaining string. Otherwise, it will throw a Parser.Exception.

  • token( token )
    • Matches the given string or regex.
    • Note that you don’t need to pre-pend your regex with ^ since that is done for you.
  • any( rule1, rule2, ... )
    • Matches any of the given rules, attempting them in the order given.
  • each( rule1, rule2, ... )
    • Maches all of the given rules, in the order given.
  • many( rule )
    • Matches the given rule as many times as possible.
  • list( rule, [ delim, trailing ] )
    • Matches a list whose elements are the given rule, delimited by the given delimiter (defaults to ’,’).
    • Also takes a boolean to indicate whether a trailing delimiter is allowed (defaults to false).
    • Only returns the elements matching the rule.
  • between( ldelim, rule, rdelim )
    • Matches a rule between two delimiters.
    • Useful for things like matching a block between braces or an argument list between parenthesis.
    • Returns only the matched rule.
  • pair( rule1, rule2, [ delim ] )
    • Matches two rules separated by a delimiter (defaults to ’,’).
    • Returns the two matched rules.
  • process( rule, fn )
    • Matches the given rule and passes the result to the given function for processing.
    • Useful for transforming the parser output into something more semantically meaningful.
  • ignore( rule)
    • Discard the matched input (returns an empty array on match).
    • Useful for matching things like comments.

Extensibility

You can easily add additional operators. Just add them to Parser.Operators. We will provide more guidance on this topic in a future release. Also, please let us know if you have added any new operators!