Created
June 23, 2016 16:09
-
-
Save wavebeem/2b42f68b8721fd7bbc9882a98759f8d4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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