private ExpressionUnparser(ExpressionUnparser that, Unparser newUnparser) { this.unparser = newUnparser; this.expressionsThatCanCauseOthersBeingParenthesized = that.expressionsThatCanCauseOthersBeingParenthesized; this.expressionThatMayNeedParenthesesToParentheses = that.expressionThatMayNeedParenthesesToParentheses; this.bnfTermToBnfTermKind = that.bnfTermToBnfTermKind; this.direction = that.direction; }
public IEnumerable <UtokenBase> Unparse(UnparsableAst self, IEnumerable <UnparsableAst> children, Unparser.Direction direction) { if (ongoingExpressionUnparseLevel == 0) { ResetMutableState(); this.direction = direction; } using (ongoingExpressionUnparseLevel.IncrAutoDecr()) return(Unparse(self, children, initialization: false)); }
IEnumerable <UnparsableAst> IUnparsableNonTerminal.GetChildren(Unparser.ChildBnfTerms childBnfTerms, object astValue, Unparser.Direction direction) { return(GetChildren(childBnfTerms, astValue, direction)); }
private FormatYielder(FormatYielder that) { this.formatter = that.formatter; this.direction = that.direction; }
protected override int?GetChildrenPriority(IUnparser unparser, object astValue, Unparser.Children children, Unparser.Direction direction) { System.Collections.IEnumerable collection = (System.Collections.IEnumerable)astValue; if (collection != null || this.EmptyCollectionHandling == EmptyCollectionHandling.ReturnNull) { return(1); } else { return(null); } }
protected override IEnumerable <UnparsableAst> GetChildren(Unparser.ChildBnfTerms childBnfTerms, object astValue, Unparser.Direction direction) { System.Collections.IEnumerable astCollection = (System.Collections.IEnumerable)astValue; if (astCollection == null && this.EmptyCollectionHandling == EmptyCollectionHandling.ReturnNull) { yield break; // this null value should be handled as an empty collection } if (direction == Unparser.Direction.RightToLeft) { astCollection = astCollection.ReverseNonGenericOptimized(); } bool firstElement = true; foreach (object astElement in astCollection) { if (!firstElement && this.delimiter != null) { yield return(new UnparsableAst(this.delimiter, astCollection)); } yield return(new UnparsableAst(this.element, astElement)); firstElement = false; } }
/// <exception cref="UnparsableAst.NonCalculatedException"> /// If topLeft is non-calculated or thrown out. /// </exception> private static IEnumerable <UtokenBase> _YieldIndentation(UnparsableAst self, BlockIndentation blockIndentationParameter, Unparser.Direction direction, FormatYielder formatYielder, bool left) { #if DEBUG BlockIndentation originalBlockIndentationParameter = blockIndentationParameter; #endif var utokens = _YieldIndentation(self, ref blockIndentationParameter, direction, formatYielder, left); #if DEBUG Debug.Assert(object.ReferenceEquals(blockIndentationParameter, originalBlockIndentationParameter), string.Format("unwanted change of blockIndentationParameter reference in _YieldIndentation ({0})", left ? "left" : "right")); #endif return(utokens); }
protected abstract int?GetChildrenPriority(IUnparser unparser, object astValue, Unparser.Children childrenAtRule, Unparser.Direction direction);
protected override int?GetChildrenPriority(IUnparser unparser, object astValue, Unparser.Children children, Unparser.Direction direction) { if (this.isOptionalValue && object.Equals(astValue, this.defaultValue)) { return(0); } else if (this.value != null) { return(this.value.Equals(astValue) ? (int?)1 : null); } else if (this.UtokenizerForUnparse != null) { return(1); } else { return(unparser.GetPriority(GetMainChild(astValue, children))); } }
private static bool IsNextStronger(InsertedUtokens prevInsertedUtokens, InsertedUtokens nextInsertedUtokens, Unparser.Direction direction) { int compareResult = InsertedUtokens.Compare(prevInsertedUtokens, nextInsertedUtokens); if (compareResult == 0) { if (direction == Unparser.Direction.LeftToRight) { return(prevInsertedUtokens.kind == InsertedUtokens.Kind.Right); } else { return(nextInsertedUtokens.kind != InsertedUtokens.Kind.Right); } } else { return(compareResult < 0); } }
protected override int?GetChildrenPriority(IUnparser unparser, object astValue, Unparser.Children childrenAtRule, Unparser.Direction direction) { return(childrenAtRule .SumIncludingNullValues( (childAtRule, childIndexAtRule) => IsMemberAtRule(new ReferredBnfTermEI(childrenAtRule.ContentIndex, childAtRule.BnfTerm, childIndexAtRule)) ? GetBnfTermPriorityForMember(unparser, childAtRule) : unparser.GetPriority(childAtRule) )); }
protected override IEnumerable <UnparsableAst> GetChildren(Unparser.ChildBnfTerms childBnfTerms, object astValue, Unparser.Direction direction) { foreach (var childRuleReferredBnfTerm in childBnfTerms.Select((childBnfTerm, index) => new ReferredBnfTermEI(childBnfTerms.ContentIndex, childBnfTerm, direction == Unparser.Direction.LeftToRight ? index : childBnfTerms.Count - 1 - index) )) { object childAstValue; Member member; if (IsMemberAtRule(childRuleReferredBnfTerm)) { member = GetMemberByAtRule(childRuleReferredBnfTerm); childAstValue = GetValue(member.MemberInfo, astValue); } else if (childRuleReferredBnfTerm.BnfTerm is BnfiTermCopy) { member = null; childAstValue = astValue; } else { member = null; childAstValue = astValue; } if (childAstValue == null && childRuleReferredBnfTerm.BnfTerm is BnfiTermConversion && ((BnfiTermConversion)childRuleReferredBnfTerm.BnfTerm).isOptionalValue) { continue; } yield return(new UnparsableAst(childRuleReferredBnfTerm.BnfTerm, childAstValue, member)); } }
protected override IEnumerable <UnparsableAst> GetChildren(Unparser.ChildBnfTerms childBnfTerms, object astValue, Unparser.Direction direction) { return(childBnfTerms.Select(childBnfTerm => new UnparsableAst(childBnfTerm, astValue: valueCreatorFromNoAst != null ? valueCreatorFromNoAst() : null))); }
protected override int?GetChildrenPriority(IUnparser unparser, object astValue, Unparser.Children children, Unparser.Direction direction) { return(0); }
int?IUnparsableNonTerminal.GetChildrenPriority(IUnparser unparser, object astValue, Unparser.Children childrenAtRule, Unparser.Direction direction) { return(GetChildrenPriority(unparser, astValue, childrenAtRule, direction)); }
protected abstract IEnumerable <UnparsableAst> GetChildren(Unparser.ChildBnfTerms childBnfTerms, object astValue, Unparser.Direction direction);
protected override int?GetChildrenPriority(IUnparser unparser, object astValue, Unparser.Children children, Unparser.Direction direction) { UnparsableAst mainChild = children.Single(childValue => IsMainChild(childValue.BnfTerm)); if (astValue.GetType() == this.domainType) { return(unparser.GetPriority(mainChild)); } else { IBnfiTerm mainChildWithDomainType = mainChild.BnfTerm as IBnfiTerm; if (mainChildWithDomainType == null || mainChildWithDomainType.DomainType == null) { throw new UnparseException(string.Format("Cannot unparse '{0}' (type: '{1}'). BnfTerm '{2}' is not an IBnfiTerm or it has no domain type.", astValue, astValue.GetType().Name, mainChild.BnfTerm)); } int?priority = mainChildWithDomainType.DomainType == typeof(object) ? int.MinValue : 0 - mainChildWithDomainType.DomainType.GetInheritanceDistance(astValue); Unparser.tsPriorities.Indent(); priority.DebugWriteLinePriority(Unparser.tsPriorities, mainChild); Unparser.tsPriorities.Unindent(); return(priority); } }
protected override int?GetChildrenPriority(IUnparser unparser, object astValue, Unparser.Children children, Unparser.Direction direction) { return(children.SumIncludingNullValues(child => unparser.GetPriority(child))); }
/// <exception cref="UnparsableAst.NonCalculatedException"> /// If topLeft is non-calculated or thrown out. /// </exception> private static IEnumerable <UtokenBase> _YieldIndentation(UnparsableAst self, ref BlockIndentation blockIndentationParameter, Unparser.Direction direction, FormatYielder formatYielder, bool left) { if (!left && direction == Unparser.Direction.RightToLeft && blockIndentationParameter.IsDeferred() && blockIndentationParameter.LeftDeferred != null) { /* * We are in a right-to-left unparse and this deferred right indentation depends on a deferred left indentation, * so let's try to calculate the deferred left indentation, which - if succeed - will change the shared blockIndentation * object state from deferred to a valid indentation. * * (If the left indentation wasn't deferred then it would be calculated which means that the shared blockIndentation * object won't be deferred.) * */ blockIndentationParameter.LeftDeferred.CalculateUtokens(); // either CalculateUtokens succeeded, and blockIndentation is not deferred, or CalculateUtokens threw a NonCalculatedException exception Debug.Assert(!blockIndentationParameter.IsDeferred(), "CalculateUtokens succeeded, but blockIndentation remained deferred"); } if (blockIndentationParameter == BlockIndentation.ToBeSet || blockIndentationParameter.IsDeferred()) { UnparsableAst leftObject = GetLeftTerminalLeave(self); BlockIndentation blockIndentation = formatYielder._GetBlockIndentation(leftObject, self); // NOTE: topAncestorCacheForLeft gets updated by GetUsedLeftsFromTopToBottomB if (blockIndentation != BlockIndentation.IndentNotNeeded) { Unparser.tsUnparse.Debug("blockindentation {0} for leftTarget '{1}' and for target '{2}'", blockIndentation, leftObject, self); } Debug.Assert(!blockIndentation.IsDeferred()); if (blockIndentationParameter == BlockIndentation.ToBeSet) { blockIndentationParameter = blockIndentation; } else { blockIndentationParameter.CopyKindFrom(blockIndentation); } } if (direction == Unparser.Direction.LeftToRight) { return(left ? BlockIndentationToUtokenControlBefore(blockIndentationParameter) : BlockIndentationToUtokenControlAfter(blockIndentationParameter)); } else { return(left ? BlockIndentationToUtokenControlAfter(blockIndentationParameter) : BlockIndentationToUtokenControlBefore(blockIndentationParameter)); } }
protected override IEnumerable <UnparsableAst> GetChildren(Unparser.ChildBnfTerms childBnfTerms, object astValue, Unparser.Direction direction) { return(childBnfTerms.Select(childBnfTerm => new UnparsableAst(childBnfTerm, astValue))); }