IOU with Context-free Grammar.
JavaScript
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
src
test
.gitignore
LICENSE
README.md
package.json
sample.bnf
server.js

README.md

Scrip

Scrip is a tiny language described using Parsing Expression Grammar (PEG) with a parser (generated by pegjs) that understands IOU (and only IOU).

Syntax

  • At the top level exists a sponsorships rule, which is either a single sponsorship rule or a collection of sponsorship rules separated by the and conjunction.

  • Each sponsorship is of the format:

    sponsor paid expense on behalf of sponsees

  • sponsor is the name of the sponsor (Example: Duff).

  • expense is an integer (Example: 500).

  • sponsees is a single name or comma-separated list of names (Example: Buff or Buff,Gruff).

  • So, a sponsorship rule looks like this:

Duff paid 500 on behalf of Buff,Gruff
  • Multiple sponsorships look like this:
Duff paid 500 on behalf of Buff,Gruff and Buff paid 1000 on behalf of Duff,Gruff

Parser results

The parser takes in a scrip of the above syntax and returns a JSON response with debtors as keys, and {debtee: amount} as values.

For instance,

Duff paid 500 on behalf of Buff,Gruff and Buff paid 600 on behalf of Duff

returns

{"Duff":{"Buff":100},"Gruff":{"Duff":500}}

This indicates that Duff owes Buff 100 and Gruff owes Duff 500.

###Install

$ git clone https://github.com/pranavrc/scrip.git
$ cd scrip/
$ npm install

Running tests

Run mocha or npm test from the project directory.

$ mocha

  Parser tests
All tests passed. Here's the result: 

{ A: { C: 800, B: 100 }, B: { C: 700 } }
    ✓ Should compute debts without errors

  GET /
All server tests passed.
    ✓ Should return valid result data


  2 passing (26ms)

As a service

Run node server.js to start the server on port 8000.

$ curl -G "http://localhost:8000/" --data-urlencode "scrip=Duff paid 500 on behalf of Buff,Gruff and Buff paid 600 on behalf of Duff"
{"Duff":{"Buff":100},"Gruff":{"Duff":500}}