Code Monkey home page Code Monkey logo

parsimmon's Introduction

Parsimmon

Parsimmon

Authors: @jneen and @laughinghan

Maintainer: @wavebeem

Parsimmon is a small library for writing big parsers made up of lots of little parsers. The API is inspired by parsec and Promises/A+.

Parsimmon supports IE7 and newer browsers, along with Node.js. It can be used as a standard Node module through npm (named parsimmon), or directly in the browser through a script tag, where it exports a global variable called Parsimmon. To download the latest browser build, use the unpkg version. For more information on how to use unpkg, see the unpkg homepage.

API Documentation

Full API documentation in API.md.

Examples

See the examples directory for annotated examples of parsing JSON, Lisp, a Python-ish language, and math.

Basics

A Parsimmon parser is an object that represents an action on a stream of text, and the promise of either an object yielded by that action on success or a message in case of failure. For example, Parsimmon.string('foo') yields the string 'foo' if the beginning of the stream is 'foo', and otherwise fails.

The method .map is used to transform the yielded value. For example,

Parsimmon.string('foo')
  .map(function(x) { return x + 'bar'; })

will yield 'foobar' if the stream starts with 'foo'. The parser

Parsimmon.regexp(/[0-9]+/)
  .map(function(x) { return Number(x) * 2; })

will yield the number 24 when it encounters the string '12'.

Calling .parse(string) on a parser parses the string and returns an object with a boolean status flag, indicating whether the parse succeeded. If it succeeded, the value attribute will contain the yielded value. Otherwise, the index and expected attributes will contain the index of the parse error (with offset, line and column properties), and a sorted, unique array of messages indicating what was expected.

The error object can be passed along with the original source to Parsimmon.formatError(source, error) to obtain a human-readable error string.

Note: If you prefer throwing an error when the parse failed, call .tryParse(string) instead. The returned value will be the parsing result itself when succeeded.

Common Functions

Questions

Feel free to ask a question by filing a GitHub Issue. I'm happy to help point you in the right direction with the library, and hopefully improve the documentation so less people get confused in the future.

Contributing

Contributions are not just pull requests.

Issues clearly describing bugs or confusing parts of Parsimmon are welcome! Also, documentation enhancements and examples are very desirable.

Feeling overwhelmed about contributing? Open an issue about what you want to contribute and I'm happy to help you through to completion!

Performance

Thanks to @bd82 we have a good benchmark comparing Parsimmon CPU performance to several other parser libraries with a simple JSON parser example.

Fantasy Land

Fantasyland

Parsimmon is also compatible with fantasyland. It implements Semigroup, Apply, Applicative, Functor, Chain, and Monad.

parsimmon's People

Contributors

jneen avatar laughinghan avatar anko avatar leonardfactory avatar hughfdjackson avatar mcapodici avatar minamorl avatar pbevin avatar michaelficarra avatar leonard-thieu avatar jwmerrill avatar grandsong avatar ahupp avatar alhimik45 avatar modernserf avatar raynos avatar pelotom avatar nlko avatar npmcdn-to-unpkg-bot avatar

Watchers

Mitsuki Ogasahara avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.