Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
parser-combinator library (using GLL) for coffeescript / javascript
JavaScript
Branch: master
Failed to load latest commit information.
docs add implicit conversions.
src add toString.
test/src allow an error message to be passed to `filter`.
.gitignore more ignores.
.travis.yml ?
CHANGES.md 3.0.1
LICENSE.txt update copyright.
README.md clarify docs.
TODO.md to-done.
build.plz stupid coffee-script.
icon.jpg icon for tower.
package.json 4.0!

README.md

packrattle

Build Status

This is a simple GLL-based parser-combinator library for javascript. It lets you write parsing code without the use of an external tool like lex or antlr: the parser is written in javascript just like the rest of your code!

An example, from the unit tests:

var packrattle = require("packrattle");

var csv = packrattle.repeatSeparated(
  packrattle.regex(/([^,]*)/).map(match => match[0]),
  /,/
);

csv.run("this,is,csv");
// [ "this", "is", "csv" ]

Docs

What?

Parser-combinators start from a simple idea: A "parser" is a function that takes a string and a position within that string, and either fails to match, or succeeds, returning the matched value and moving the position forward. In other words, a parser does:

position => { newPosition, value }

on success, or

position => error

on failure.

You can start with a few basic parsers which match a string or regular expression, and build more complex parsers out of functions that combine them: "a or b", "a then b", "repeat(a)", and so on.

Being "GLL-based" means that a work queue and memoization is used to avoid loops and make backtracking fast. This lets you parse almost any grammar, even if it's left recursive or ambiguous. For example, the grammar

expr ::= (expr "+" expr) | /\d+/

would need to be restructured to work in most parser libraries. It can be expressed in packrattle as

var expr = packrattle.alt(
  [ () => expr, "+", () => expr ],
  packrattle.regex(/\d+/).map(match => match[0])
);

and it actually matches strings:

expr.run("3+10+200");
// [ [ '3', '+', '10' ], '+', '200' ]

The nested functions (() => expr) on line 2 allow javascript to handle recursive definitions by delaying evaluation. The functions will only be called once (when first invoked) and then cached.

Further reading

License

Apache 2 (open-source) license, included in 'LICENSE.txt'.

Author

Credit and blame: Robey Pointer robeypointer@gmail.com

Special thanks to Daniel Spiewak, Brian McKenna, and Vegard Øye for sharing info about GLL.

Something went wrong with that request. Please try again.