Skip to content

Instantly share code, notes, and snippets.

@ccorcos
Created March 2, 2017 04:35
Show Gist options
  • Save ccorcos/a7f3f9590c606a8b6e1f0cc737e15742 to your computer and use it in GitHub Desktop.
Save ccorcos/a7f3f9590c606a8b6e1f0cc737e15742 to your computer and use it in GitHub Desktop.
const sequence = parsers => input => {
let next = input
for (var i = 0; i < parsers.length; i++) {
const parser = parsers[i]
const {success, rest} = parser(next)
if (!success) {
return {success, rest}
}
next = rest
}
return {success: true, rest: next}
}
sequence([char('a'), char('b'), char('c')])('abc')
// => { success: true, rest: '' }
sequence([char('a'), char('b'), char('a')])('abc')
// => { success: false, rest: 'c' }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment