/// <summary> /// Feeds all tokens to the parser and returns the last result. /// </summary> private RuleResult FeedAll(IRuleParser parser, IEnumerable <Token> tokens) { RuleResult lastResult = default; foreach (Token t in tokens) { lastResult = parser.FeedToken(t); } return(lastResult); }
public RuleResult FeedToken(Token t) { if (_isFinished) { throw new Exception("Tried to feed a token to an already-finished ThenChainParser."); } var currentCallabck = _ruleSequence[_currentRuleIndex].onMatched; // HACK: Create the first rule, if it hasn't been already. if (_currentRule == null) { _currentRule = _ruleSequence[_currentRuleIndex].ruleFactory(); } // Feed the token to the current rule var result = _currentRule.FeedToken(t); // If it failed, cascade that failure upwards if (result.status == RuleStatus.Failed) { return(result); } // If it succeeded, invoke the callback and move to the next rule if (result.status == RuleStatus.Complete) { currentCallabck(result.node); _currentRuleIndex++; // If the last rule just succeeded, the whole chain is complete. if (_currentRuleIndex >= _ruleSequence.Count) { _isFinished = true; return(RuleResult.Complete(_node)); } _currentRule = _ruleSequence[_currentRuleIndex].ruleFactory(); } return(RuleResult.GoodSoFar()); }