protected void ContractTo(BnfiTermCollection target) { if (!this.IsContractible) { GrammarHelper.ThrowGrammarErrorException(GrammarErrorLevel.Error, "This collection should not be a right-value: {0}", this.Name); } if (!this.listKind.HasValue) { GrammarHelper.ThrowGrammarErrorException(GrammarErrorLevel.Error, "Right-value collection has not been initialized: {0}", this.Name); } // note: target.RuleRaw is set and target.SetState is called by _MakePlusRule/_MakeStarRule if (this.listKind == ListKind.Plus) { _MakePlusRule(target, this.delimiter, this.element); } else if (this.listKind == ListKind.Star) { _MakeStarRule(target, this.delimiter, this.element); } else { throw new InvalidOperationException(string.Format("Unknown listKind: {0}", this.listKind)); } this.RuleRaw = null; this.ClearState(); this.hasBeenContracted = true; }
protected void ContractTo(BnfiTermConversion target) { if (!this.IsContractible) { GrammarHelper.ThrowGrammarErrorException(GrammarErrorLevel.Error, "This value should not be a right-value: {0}", this.Name); } target.RuleRaw = this.RuleRaw; target.SetState(this); this.RuleRaw = null; this.ClearState(); this.hasBeenContracted = true; }
protected void CheckAfterRuleHasBeenSetThatChildrenAreNotContracted() { if (Rule != null) { var children = Rule.Data .SelectMany(_children => _children) .OfType <BnfiTermNonTerminal>(); if (children.Any(child => child.hasBeenContracted)) { GrammarHelper.ThrowGrammarErrorException( GrammarErrorLevel.Error, "NonTerminal '{0}' has been contracted. You should use MakeUncontractible() on it.", children.First(child => child.hasBeenContracted) ); } } }
private void ProcessUnparseHints(BnfExpression rule) { if (rule != null) { for (int childBnfTermListIndex = 0; childBnfTermListIndex < rule.Data.Count; childBnfTermListIndex++) { BnfTermList bnfTermList = rule.Data[childBnfTermListIndex]; try { UnparseHint unparseHint = (UnparseHint)bnfTermList.SingleOrDefault(bnfTerm => bnfTerm is UnparseHint); childBnfTermListIndexToUnparseHint.Add(childBnfTermListIndex, unparseHint); } catch (InvalidOperationException) { GrammarHelper.ThrowGrammarErrorException( GrammarErrorLevel.Error, "NonTerminal '{0}' has more than one UnparseHint on its {1}. childrenlist. Only one UnparseHint is allowed per childrenlist.", this, childBnfTermListIndex + 1 ); } } } }
private void RegisterNonTerminalOperator(NonTerminal nonTerminalOperator, int precedence, Associativity?associativity, bool recurse) { if (nonTerminalOperator.Precedence != BnfTerm.NoPrecedence) { throw new InvalidOperationException(string.Format("Double call of RegisterOperators on non-terminal '{0}'", nonTerminalOperator)); } if (nonTerminalOperator.Rule == null) { GrammarHelper.ThrowGrammarErrorException(GrammarErrorLevel.Error, "Rule is needed to have been set for nonterminal operator '{0}' before calling RegisterOperators", nonTerminalOperator); } if (recurse) { foreach (var bnfTerms in nonTerminalOperator.Rule.GetBnfTermsList()) { foreach (var bnfTerm in bnfTerms) { if (bnfTerm is Terminal) { RegisterTerminalOperator((Terminal)bnfTerm, precedence, associativity, recurse); } else if (bnfTerm is NonTerminal && !bnfTerm.Flags.IsSet(TermFlags.NoAstNode)) { RegisterNonTerminalOperator((NonTerminal)bnfTerm, precedence, associativity, recurse); } } } nonTerminalOperator.SetFlag(TermFlags.InheritPrecedence); } else { BaseRegisterOperator(nonTerminalOperator, precedence, associativity); } }