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.

25 lines
947 B
JavaScript

"use strict";
const { NoMatch } = require("../symbols");
module.exports = function* either(instruction, state, context) {
let { options } = instruction;
for (let option of options) {
let result = yield option;
if (result === NoMatch) {
// Restore index and try again with the next option
state.currentIndex = context.startIndex;
continue;
} else {
// Don't restore index; the match has been consumed
// FIXME: This includes NotEnoughInput! As it warrants an immediate abort. Handling of NotEnoughInput markers should be moved to a centralized place instead. Also, we should figure out exactly how to retain the current parsing position when one is encountered, and whether eg. individual core operations need to manage cursor resets for this purpose, or whether the core can centrally handle that as well, eg. by retaining the parsing stack.
return result;
}
}
// None of the options matched
return NoMatch;
};