Skip to content

Instantly share code, notes, and snippets.

@wavebeem
Created June 23, 2016 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wavebeem/2b42f68b8721fd7bbc9882a98759f8d4 to your computer and use it in GitHub Desktop.
Save wavebeem/2b42f68b8721fd7bbc9882a98759f8d4 to your computer and use it in GitHub Desktop.
const P = require('parsimmon');
const Chalk = require('chalk');
function wants(x) {
process.stdout.write(Chalk.bold.red('( wants ' + Chalk.cyan(x) + ' ) '));
return x;
}
function got(x) {
console.log(Chalk.bold.green('( GOT ' + Chalk.cyan(x) + ' )'));
return x;
}
function log(x) {
console.log(Chalk.bold(x));
return x;
}
const parser =
P.seq(
P.of('\nPARSE START').map(log),
P.of('===========').map(log),
P.of('[').map(wants).then(P.string('[').map(got)),
P.alt(
P.of('a').map(wants).then(P.string('a').map(got)),
P.of('b').map(wants).then(P.string('b').map(got)),
P.of('c').map(wants).then(P.string('c').map(got))
),
P.of(']').map(wants).then(P.string(']').map(got))
);
parser.parse('[a]');
parser.parse('[b]');
parser.parse('[c]');
parser.parse('[]');
console.log();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment