@thi.ng/parse
Version:
Purely functional parser combinators & AST generation for generic inputs
18 lines (17 loc) • 426 B
JavaScript
const lookahead = (parser, ahead, capture = false, id = "lookahead") => (ctx) => {
if (ctx.done) return false;
ctx.start(id);
let pass = false;
while (true) {
const state = capture ? null : ctx.state.copy();
if (ahead(ctx)) {
!capture && (ctx.state = state);
return pass ? ctx.end() : ctx.discard();
}
if (!parser(ctx)) return ctx.discard();
pass = true;
}
};
export {
lookahead
};