public override string ToString() { return(ProductionRule.ToString() + (ProductionRule.Rhs.Count > 1 ? $"( {ProductionRule.Rhs[RulePartIndex].ToString()} )" : string.Empty) + $" {Position}" + $" :{Content}"); }
/// <summary> /// Applies the suppled production rule starting at token index /// This will recursively call further production rules /// </summary> /// <returns><c>true</c>, if rule was applied, <c>false</c> otherwise.</returns> /// <param name="r">The red component.</param> /// <param name="tokenIndex">Token index.</param> /// <param name="indent">Indent.</param> bool ApplyRule(ProductionRule r, ref int tokenIndex, int indent = 0) { Log("Applying " + r.ToString() + " to " + string.Join(", ", _tokens.GetRange(tokenIndex, _tokens.Count - tokenIndex).Select(t => $"{t.TokenType.Name} ({t.Value})")), indent); _rulesStack.Push(r.Name); var incomingTokenIndex = tokenIndex; // In order to pass this rule one of the RuleParts must evaluate bool evaluatedRuleSuccessfully = false; ProductionRulePart successfullyEvaluatedRulePart = null; for (int i = 0; i < r.Rhs.Count; i++) { // Take a copy of tokens. if (ApplyRulePart(r.Rhs[i], ref tokenIndex, indent)) { // We have passed - so can break out. // Otherwise, try the next rule part evaluatedRuleSuccessfully = true; successfullyEvaluatedRulePart = r.Rhs[i]; break; } } if (evaluatedRuleSuccessfully) { var nodePosition = GetRuleNodePosition(tokenIndex, incomingTokenIndex); _treeNodeStack.Push( new AbstractSyntaxRuleNode( r, r.Rhs.IndexOf(successfullyEvaluatedRulePart), nodePosition, _source.Substring(nodePosition.Index, nodePosition.Length) ) ); } else { // restore the index - we need to try a different rule tokenIndex = incomingTokenIndex; } _rulesStack.Pop(); return(evaluatedRuleSuccessfully); }