Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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