mattfenwick / UnParse-js

https://www.npmjs.com/package/unparse-js
10 stars 1 forks source link

add `and` combinator #37

Open mattfenwick opened 8 years ago

mattfenwick commented 8 years ago

Run a bunch of parsers, checking that they all succeed, without advancing the position. Something like:

// [Parser e s (m t) a] -> Parser e s (m t) [a]
// or [forall a. Parser e s (m t) a] -> Parser e s (m t) ()
function and(parsers) {
  parsers.forEach(checkParser.bind(null, 'and'));
  function f(xs, s) {
    var values = [];
    for (var i = 0; i < parsers.length; i++) {
      var r = parsers[i].parse(xs, s);
      if ( r.status !== 'success' ) {
        return r;
      } else {
        values.push(r.result.value);
      }
    }
    return good(values, xs, s);
  }
  return new Parser(f);
}
mattfenwick commented 8 years ago

Is this different from:

// [Parser e s (m t) a] -> Parser e s (m t) [a]
function and(parsers) {
    parsers.forEach(checkParser.bind(null, 'and'));
    return seq(parsers.map(lookahead));
}

?

mattfenwick commented 8 years ago

Is there a better name than and? Perhaps all?