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

fastparse

v1.1.2

Published

A very simple and stupid parser, based on a statemachine and regular expressions.

Downloads

11,136,757

Readme

fastparse

A very simple and stupid parser, based on a statemachine and regular expressions.

It's not intended for complex languages. It's intended to easily write a simple parser for a simple language.

Usage

Pass a description of statemachine to the constructor. The description must be in this form:

new Parser(description)

description is {
	// The key is the name of the state
	// The value is an object containing possible transitions
	"state-name": {
		// The key is a regular expression
		// If the regular expression matches the transition is executed
		// The value can be "true", a other state name or a function

		"a": true,
		// true will make the parser stay in the current state
		
		"b": "other-state-name",
		// a string will make the parser transit to a new state
		
		"[cde]": function(match, index, matchLength) {
			// "match" will be the matched string
			// "index" will be the position in the complete string
			// "matchLength" will be "match.length"
			
			// "this" will be the "context" passed to the "parse" method"
			
			// A new state name (string) can be returned
			return "other-state-name";
		},
		
		"([0-9]+)(\\.[0-9]+)?": function(match, first, second, index, matchLength) {
			// groups can be used in the regular expression
			// they will match to arguments "first", "second"
		},
		
		// the parser stops when it cannot match the string anymore
		
		// order of keys is the order in which regular expressions are matched
		// if the javascript runtime preserves the order of keys in an object
		// (this is not standardized, but it's a de-facto standard)
	}
}

The statemachine is compiled down to a single regular expression per state. So basically the parsing work is delegated to the (native) regular expression logic of the javascript runtime.

Parser.prototype.parse(initialState: String, parsedString: String, context: Object)

initialState: state where the parser starts to parse.

parsedString: the string which should be parsed.

context: an object which can be used to save state and results. Available as this in transition functions.

returns context

Example

var Parser = require("fastparse");

// A simple parser that extracts @licence ... from comments in a JS file
var parser = new Parser({
	// The "source" state
	"source": {
		// matches comment start
		"/\\*": "comment",
		"//": "linecomment",
		
		// this would be necessary for a complex language like JS
		// but omitted here for simplicity
		// "\"": "string1",
		// "\'": "string2",
		// "\/": "regexp"
		
	},
	// The "comment" state
	"comment": {
		"\\*/": "source",
		"@licen[cs]e\\s((?:[^*\n]|\\*+[^*/\n])*)": function(match, licenseText) {
			this.licences.push(licenseText.trim());
		}
	},
	// The "linecomment" state
	"linecomment": {
		"\n": "source",
		"@licen[cs]e\\s(.*)": function(match, licenseText) {
			this.licences.push(licenseText.trim());
		}
	}
});

var licences = parser.parse("source", sourceCode, { licences: [] }).licences;

console.log(licences);

License

MIT (http://www.opensource.org/licenses/mit-license.php)