You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

29 lines
817 B
JavaScript

"use strict";
const { NoMatch, NotEnoughInput } = require("../symbols");
module.exports = function* until(instruction, state, context) {
while (state.currentIndex < state.currentInput.length) {
let attemptIndex = state.currentIndex;
let result = yield instruction.rule;
if (result === NoMatch) {
state.currentIndex += 1;
continue;
} else {
// Un-consume the terminator we've matched, and continue parsing other rules
state.currentIndex = attemptIndex;
if (state.currentIndex > context.startIndex) {
return state.currentInput.slice(context.startIndex, state.currentIndex);
} else {
// Zero-width match; that's a fail
return NoMatch;
}
}
}
// Reached the end of the currently available input, and we still haven't encountered our terminator
return NotEnoughInput;
};