Skip to content

Latest commit

 

History

History
58 lines (39 loc) · 1.43 KB

README.md

File metadata and controls

58 lines (39 loc) · 1.43 KB

Parsnip - Monadic parsing combinators for javascript

This library is intended to make it easy to write correct parsers for the javascript platform, by composing simple and correct parsers into larger parsers.

var readDigits = Parsnip.Parser
    .from(/\d+/).convert(Number)
    .separatedBy(' ')
    .surroundedBy('{', '}')
    .toFunction();

var result = readDigits('{1 546 233}');

console.log(result); 
// [1, 546, 233]

readDigits('{324324 33'); //!!! Syntax Error

For a more involved example, see the JSON parser in json/src.

You'll need coffeescript, and GNU Make to build and vows to run the tests

make       # makes build/parsnip.js
make test  # runs the test suite
make clean # deletes files generated by the build

Example JSON Parser

There's a complete JSON parser defined in json/ require './json/Json' to get it. The module provides the function parseJSON and an instance of Parser called JSONParser.

parseJSON takes a single string parameter and will either return a javascript value, or throw a SyntaxError.

Run the JSON tests with vows:

> vows --spec json/spec/*

Documentation

There will be some soon. The source is commented and the tests describe the intent of most methods.

License

This software is free to use and distribute under the MIT License as specified in the file LICENSE.txt