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

cmbjs

v1.1.0

Published

**A parsing library for JavaScript using parser combinators**

Downloads

8

Readme

cmb.js

A parsing library for JavaScript using parser combinators

cmb.js is a set of tools which can create a function that can parse a string and output a parse tree. It basically generates a scannerless recursive descent parser by encapsulating the boilerplate code in a way that you can combine the parts directly into the parsing function merely by specifying the grammar. Memoization ensures that the parsing happens in roughly O(n) time and space.

Usage

var parse = cmb(config);
var parseTree = parse("abcdefg");

Example

// Produces a parser that recognizes 0 or more a's
// followed by 1 or more b's
var parse = cmb({
  grammar: {
    "sentence": cmb.all("a's", "b's"),
    "a's": cmb.many(cmb.term("a")),
    "b's": cmb.several(cmb.term("b"))
  },
  startRule: "sentence",
  ignore: cmb.empty,
  transforms: {
    "sentence": function(value) {
      return value.map(function(node) {
          return node.value;
      });
    },
    "a's": function(value) {
      return value.map(function(node) {
          return node.value;
      });
    },
    "b's": function(value) {
      return value.map(function(node) {
          return node.value;
      });
    }
  }
});
var result = parse("aaabbbbb");
// output:
// {
//   value: [["a", "a", "a"], ["b", "b", "b", "b", "b"]],
//   state: ...,
//   name: "sentence"
// }

API

  • cmb: The parser generator.
    • term: produces a parselet that matches a string literal or a regular expression.
    • empty: a parselet that does nothing and produces an empty parse tree.
    • whitespace: a parselet that matches a string of contiguous whitespace. Shorthand for cmb.term(/\s+/).
    • maybe: a combinator that takes a parselet and returns its result on a match, or the resukt of cmb.empty otherwise. Analogous to the ? operator in regular expressions.
    • many: a combinator that takes a parselet and returns 0 or more consecutive matches in an array. Analogous to the * operator in regular expressions.
    • any: a combinator that takes a number of parselets in order and returns the results of the first one that matches or an error if none match. Analogous to the | operator in regular expressions.
    • all: a combinator that takes a number of parselets and matches them in sequence, returning an array of results on success and the first error it encounters otherwise. Analogous to adjacent rules in regular expressions.
    • several: a combinator that takes a parselet and returns 1 or more matches in an array or an error on no matches. Analogous to the + operator in regular expressions.
  • config: The language grammar specification fed to cmb() to produce a parsing function. config is an object which can have several optional fields:
    • grammar: (optional) an object representing name-value pairs of production rules. Specifying names are useful for defining a recursive rule, naming a parse tree node, or applying a transform to the parse tree's output at that level.
    • startRule: (optional) a name or parselet which will act as the starting point of the top-down parsing (the "top" of the grammar). If no rule is specified, it assumes that it will start at a node named "root".
    • ignore: (optional) a name or parselet that the parser will discard before trying to find a production rule. Useful for discarding whitespace, for example. It will not ignored the pattern in unnamed rules or subrules, so if it is required to ignore space between subrules, it's best to name the rules in the grammar. It defaults it cmb.empty, which matches nothing.
    • transforms: (optional) an object of name-value pairs of transforms to perform on a generated parse tree of a given production rule. Useful for flattening a parse tree or removing noise from the output to make the output easier for processing.

Parse Tree Format

{
	value: <any value>,
	state: <obj>,
	err: <string or array>,
	name: <string>
}
  • value: (optional) The value found parsing the text. Can contain text, other parse tree nodes, an array of results, or anything a transform function puts in the node.
  • state: The state of the parser as of the last thing parsed. Useful for debugging as it shows you text representation of a parse subtree.
  • err: (optional) An error or array of errors produced when a parselet cannot parse the given string.
  • name: (optional) A string representing the name of a production rule. This is included in all production rule output, but not in the intermediate parselets.

Transforms

The default parse tree format is designed to be generic and informative in the cmb.js internals and therefore it can be a bit noisy. To make them easier to work with, each named production rule can optionally come with a helper function to keep just the bare essentials in the tree structure.

Each transform is a function which takes a node's old value as its parameter, and its output is set as the node's new value. Note that only the node's value gets modified, resulting in a parse tree node like so:

{
    value: transforms[originalName](originalValue),
    state: originalState,
    err: originalErr,
    name: originalName
}

One last thing to note is that while parsing is done top-down, transforms are done bottom-up, meaning that any child nodes (contained in the value field) have already been subjected to transforms. This also makes possible any bottom-up syntax analysis before the value is returned.

Changes

  • 1.1.0: added the several combinator.

License

The MIT License (MIT)

Copyright (c) 2015 Sean Wolcott

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.