Skip to content

A simple parser combinator with usable error messages (ported from C#).

License

Notifications You must be signed in to change notification settings

LawnCable/Sprache-js

 
 

Repository files navigation

Sprache.js

sprache.js is a TypeScript port of Sprache, a simple parser for C#

Build Status Coverage Status

npm install sprache --save

Usage

Unlike most parser-building frameworks, you use Sprache from your program code, and don't need to set up any build-time code generation tasks.

A simple parser might parse a sequence of characters:

import { Parse } from 'sprache';

// Parse any number of capital 'A's in a row
var parseA = Parse.char('A').atLeastOnce();

Sprache provides a number of built-in functions that can make bigger parsers from smaller ones, often callable via generators:

import { Parse } from 'sprache';

const identifier = Parse.query(function*() {
    const leading  = yield Parse.whiteSpace.many();
    const first    = yield Parse.letter.once();
    const rest     = yield Parse.letterOrDigit.many();
    const trailing = yield Parse.whiteSpace.many();

    return Parse.return([first].concat(rest).join(''));
});

var id = identifier.parse(" abc123  ");

Assert.isEqual("abc123", id);

More Examples

More examples are available in examples/

Building / Running examples

npm install
npm run build
npm run test

To run as example

npm run build && node dist/examples/sql

Is VSCode, just run task "npm: install", then F5 to run.

About

A simple parser combinator with usable error messages (ported from C#).

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 99.3%
  • JavaScript 0.7%