Giter Club home page Giter Club logo

parsihax's Introduction

Parsihax

TravisCI Build Status

Parsihax is a small library for writing big parsers made up of lots of little parsers. The API is inspired by parsec and Parsimmon (originally, Parsihax was just supposed to be Parsimmon rewrite in Haxe).

Installation

Install the library via haxelib (library manager that comes with any Haxe distribution).

haxelib install parsihax

API Documentation

Haxe-generated API documentation is available at documentation website, or see the annotated source of parsihax.Parser.hx.

Examples

See the test directory for annotated examples of parsing JSON, simple Lisp-like structure and monad parser.

Basics

To use nice sugar syntax, simply add this to your Haxe file

import parsihax.*;
import parsihax.Parser.*;
using parsihax.Parser;

A ParseObject parser is an abstract 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, Parser.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,

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

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

~/[0-9]+/.regexp()
  .map(function(x) return Std.parseInt(x) * 2);

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

Also, Parsihax supports nice sugar syntax thanks to Haxe operator overloading. For example,

var a = "a".string() / "important letter a"
var b = "b".string() / "important letter b"
var c = "c".string() / "important letter c"

var result = a | b + c;

// Will succeed on "ac" and "bc"
// In case of failure, it will throw "expected important letter a|b|c"
// So, plus operator is alias to then, or operator to or and div
// operator to as

Getting apply from a ParseObject (or explicitly casting it to ParseFunction returns parsing function String -> ?Int -> Result<A> (or just ParseFunction), that parses the string and returns a Hax.Result 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 offset of the parse error, and a sorted, unique array of messages indicating what was expected.

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

Changing ParseObject.apply value changes ParseObject behaviour, but still keeps it's reference, what is really usefull in recursive parsers.

parsihax's People

Contributors

deathbeam avatar fponticelli avatar

Watchers

 avatar  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.