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

kakapo-js

v0.1.6

Published

Javascript parser combinator library

Downloads

20

Readme

kakapo-js

npm version Kakapo is a Javascript parser combinator library designed for creating complex grammars. Kakapo is an API, not a code generation tool.

Kakapo has no run-time dependencies. The Kakapo module is designed to be able to be run from the browser or from Node.js. There are several examples of using Kakapo in the "examples" folder.

Load

It can be loaded using a script tag in an HTML document for the browser

<script src="https://raw.githubusercontent.com/nirvanasupermind/kakapo-js/main/src/kakapo.min.js"></script>

or as a Node.js module using require.

var kakapo = require("kakapo.js");

For Node, the library is available from the npm registry.

$ npm install kakapo-js

API

kakapo.char(chars)

Succeeds if one of the characters are present.

Example

var parser = kakapo.char("0123456789abc");
console.log(parser.parse("a")); //=> "a"
console.log(parser.parse("bob")); //=> { error: { index: 0 } }

kakapo.delay(fn)

Creates a parser that is defined from a function that generates the parser. This is useful for referencing parsers that haven't yet been defined, and for implementing recursive parsers.

Example

var parser1 = kakapo.delay(() => kakapo.text("a").then(parser2));
var parser2 = kakapo.text("b");
console.log(parser1.parse("ab")); //=> ["a","b"]

kakapo.failure(index)

Generates an object of the form {"error":{"index":index}}. Used in kakapo.Parser(fn).

Example

console.log(kakapo.failure(2)); //=> { error: { index: 2 } }

kakapo.success(value,start,end)

Generates an object of the form {"value":value,"start":start,"end":end}. Used inside kakapo.Parser(fn).

Example

console.log(kakapo.success("a",0,1)); //=> { value: 'a', start: 0, end: 1 }

kakapo.Parser(fn)

Creates a primitive parser.

Example

var parser = new kakapo.Parser(function (input) {
   if(input === "a") {
       return kakapo.success("a",0,1);
   } else if(input === "b") {
       return kakapo.success("b",0,1);
   } else {
       return kakapo.failure(0);
   }
});

console.log(parser.parse("a")); //=> "a"
console.log(parser.parse("b")); //=> "b"
console.log(parser.parse("c")); //=> { error: { index: 0 } }

kakapo.text(text)

Create a parser that matches the text.

Example

var parser = kakapo.text("Hello World");
console.log(parser.parse("Hello World")); //=> "Hello World"
console.log(parser.parse("nice")); //=> { error: { index: 0 } }

Parser methods

parser.delimited(delimiter)

Example

var parser = kakapo.text("a").delimited(kakapo.text(","));
console.log(parser.parse("")); //=> []
console.log(parser.parse("a")); //=> ["a"]
console.log(parser.parse("a,a")); //=> ["a","a"]
console.log(parser.parse("b")); //=> { error: { index: 0 } }

parser.oneOrMore()

Attempts to apply the parser 1 or more times.

Example

var parser = kakapo.char("yz").oneOrMore();
console.log(parser.parse("")); //=> { error: { index: 0 } }
console.log(parser.parse("y")); //=> ["y"]
console.log(parser.parse("zy")); //=> ["z","y"]
console.log(parser.parse("wy")); //=> { error: { index: 0 } }

parser.opt()

Attempts to match a parser 0 or 1 times.

Example

var parser = kakapo.text("a").opt();
console.log(parser.parse("")); //=> ""
console.log(parser.parse("a")); //=> "a"
console.log(parser.parse("ab")); //=> { error: { index: 1 } }

parser.or(other)

Returns a new parser which tries parser and, if it fails, tries other.

Example

var parser = kakapo.text("foo")
                   .or(kakapo.text("bar"));
console.log(parser.parse("foo")); //=> "foo"
console.log(parser.parse("bar")); //=> "bar"
console.log(parser.parse("baz")); //=> { error: { index: 2 } }

parser.parse(input)

Parses a string. If successful, outputs the parsed result. If unsuccessful, outputs an error object of the form {"error":{"index":index}}.

parser.quantified(min,max)

Attempts to apply a parser between min and max number of times inclusive.

Example

var parser = kakapo.text("no").quantified(1,3);
console.log(parser.parse("")); //=> { error: { index: 0 } }
console.log(parser.parse("no")); //=> ["no"]
console.log(parser.parse("nono")); //=> ["no","no"]
console.log(parser.parse("nonono")); //=> ["no","no","no"]
console.log(parser.parse("nononono")); //=> { error: { index: 2 } }
console.log(parser.parse("mo")); //=> { error: { index: 0 } }

parser.repeat(count)

Attempts to apply a parser a precise number of times.

Example

var parser = kakapo.text("a").repeat(2);
console.log(parser.parse("aa")); //=> ["a","a"]
console.log(parser.parse("ab")); //=> { error: { index: 1 } }

parser.then(other)

Matches two parsers in order.

Example

var parser = kakapo.text("b").then(kakapo.text("c"));
console.log(parser.parse("bc")); //=> ["b","c"]
console.log(parser.parse("bd")); //=> { error: { index: 1 } }
console.log(parser.parse("cb")); //=> { error: { index: 0 } }

parser.transform(fn)

Transforms the output of parser with the given function.

Example

var parser = kakapo.text("050").transform(parseFloat);
console.log(parser.parse("050")); //=> 50
console.log(parser.parse("foo")); //=> { error: { index: 0 } }

parser.zeroOrMore()

Attempts to apply the parser 0 or more times.

Example

var parser = kakapo.char("ac").zeroOrMore();
console.log(parser.parse("")); //=> []
console.log(parser.parse("a")); //=> ["a"]
console.log(parser.parse("ac")); //=> ["a","c"]
console.log(parser.parse("ad")); //=> { error: { index: 0 } }

Minification

For convenience I am providing a minified/obfuscated version src/kakapo.min.js that is being generated with uglify.js.

License

Kakapo is licensed under the MIT License.