in src/grammar.ts [946:995]
function _checkWhileConditions(grammar: Grammar, lineText: OnigString, isFirstLine: boolean, linePos: number, stack: StackElement, lineTokens: LineTokens): IWhileCheckResult {
let anchorPosition = (stack.beginRuleCapturedEOL ? 0 : -1);
const whileRules: IWhileStack[] = [];
for (let node: StackElement | null = stack; node; node = node.pop()) {
const nodeRule = node.getRule(grammar);
if (nodeRule instanceof BeginWhileRule) {
whileRules.push({
rule: nodeRule,
stack: node
});
}
}
for (let whileRule = whileRules.pop(); whileRule; whileRule = whileRules.pop()) {
const { ruleScanner, findOptions } = prepareRuleWhileSearch(whileRule.rule, grammar, whileRule.stack.endRule, isFirstLine, linePos === anchorPosition);
const r = ruleScanner.scanner.findNextMatchSync(lineText, linePos, findOptions);
if (DebugFlags.InDebugMode) {
console.log(' scanning for while rule');
console.log(debugCompiledRuleToString(ruleScanner));
}
if (r) {
const matchedRuleId = ruleScanner.rules[r.index];
if (matchedRuleId !== -2) {
// we shouldn't end up here
stack = whileRule.stack.pop()!;
break;
}
if (r.captureIndices && r.captureIndices.length) {
lineTokens.produce(whileRule.stack, r.captureIndices[0].start);
handleCaptures(grammar, lineText, isFirstLine, whileRule.stack, lineTokens, whileRule.rule.whileCaptures, r.captureIndices);
lineTokens.produce(whileRule.stack, r.captureIndices[0].end);
anchorPosition = r.captureIndices[0].end;
if (r.captureIndices[0].end > linePos) {
linePos = r.captureIndices[0].end;
isFirstLine = false;
}
}
} else {
if (DebugFlags.InDebugMode) {
console.log(' popping ' + whileRule.rule.debugName + ' - ' + whileRule.rule.debugWhileRegExp);
}
stack = whileRule.stack.pop()!;
break;
}
}
return { stack: stack, linePos: linePos, anchorPosition: anchorPosition, isFirstLine: isFirstLine };
}