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

parsle

v1.2.4

Published

A parser toolkit for JavaScript

Downloads

2

Readme

parsle

A parser toolkit for JavaScript based off of RegExps

Installation

$ npm install parsle

Getting Started

Parsle is a Req module, which means you can use Req to require it:

Req.module("myModule",
  $$ => $$.depend("parsle", "a parsing toolkit"),
  $$ => {

  const Parsle = $$.get("parsle", {});

  // ...
})

Or, if you prefer, you can require it normally like so:

const Parsle = require("parsle")();

// ...

Usage

To create a parser, use the Parsle constructor:

var parser = new Parsle($ => {
	// ...
});

In the above code, $ is a Matcher - a class which performs matching operations on text.

Matcher API

Matcher.MATCH(regex, transform, gate)

The Matcher.MATCH function matches a regex against the current string. It takes the regex to match, a transform function, and a gate.

The regex must match at the first character (i.e. as if it had ^ prepended to it). If this behavior is not desired then use Matcher.MATCH_AHEAD.

The transform function takes an info object with keys:

  • pos: The 0-based index of the character the match started at.
  • line, col: The 1-based line and column of the character the match started at.
  • match: The result of calling string.match(regex) where string is the current string ...and returns an object to be added to the results. Think of it as a post-proccesor function. The default behavior is to return the match property of the info object. If the transform function is the special Matcher.IGNORED or Matcher.prototype.IGNORED, or if the function returns either of those, then the results aren't even added to the this.result array

The gate function takes an info object as described above, and if it returns false, the match is discarded and a parse error is thrown.

After the match is successful, the matched string is removed from the current string.

Matcher.MATCH_AHEAD(regex, transform, gate)

Does the same as above, but does not require the match to be on the first character and instead removes all the way up to the part of the string actually matched.

Matcher.PUSHSTATE()

Saves the state of the matcher on the stack to be loaded later with Matcher.POPSTATE() (or accessed with the Matcher.states property)

Matcher.POPSTATE()

Pops a state from the stack and loads it.

Matcher.OPTIONAL(fn)

Runs fn. If a parse error occurs, set this.error to the error, go back to before fn was called and return false. Otherwise, return true.

Matcher.TRY(fn)

Returns whether fn would succeed (i.e. not throw a parse error when run). If any error occurs, it is also saved in this.error.

Matcher.GROUP(fn, transform)

Runs fn, but instead of pushing the results to the Matcher.result array, it passes an array of results* to the transform function and the return value of that is pushed instead.

* Those results are still obtained by using the transform function on Matcher.MATCH / Matcher.MATCH_AHEAD, so they might be info objects and might not.

Matcher.OR(fnArray)

Try to match each function in fnArray one at a time, in order. If none work, throw a parse error.

Using the parser

When you create your parser, call its parse(input) method to parse text. The result of parsing is stored in this.result as well as returned.

License

MIT