Skip to content

A simple parser combinator with usable error messages.

License

Notifications You must be signed in to change notification settings

luggage66/Sprache-js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

4b69e5d · Nov 30, 2023
Jun 26, 2021
Oct 31, 2020
Nov 30, 2023
May 13, 2019
Nov 2, 2020
Nov 1, 2020
May 11, 2019
Nov 6, 2020
Oct 31, 2020
Nov 30, 2023
Sep 19, 2017
Jun 26, 2021
Nov 1, 2020
Nov 30, 2023
Sep 24, 2017
Nov 1, 2020
Sep 17, 2017
Mar 2, 2021
Aug 31, 2021

Repository files navigation

Sprache.js

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

<Sprache> codecov

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 src/examples/

Building / Running examples

Requirements:

  • NodeJS
  • yarn
winget install OpenJS.NodeJS
winget install Yarn.Yarn
yarn install
yarn run build
yarn run test

To run as example

yarn run build && node dist/examples/sql

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