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); } }
public override void GetUtokensAround(UnparsableAst target, out InsertedUtokens leftInsertedUtokens, out InsertedUtokens rightInsertedUtokens) { base.GetUtokensAround(target, out leftInsertedUtokens, out rightInsertedUtokens); if (target.BnfTerm == B.OBJECT_BEGIN) { rightInsertedUtokens = UtokenInsert.NewLine(); } else if (target.BnfTerm == B.OBJECT_END) { leftInsertedUtokens = rightInsertedUtokens = UtokenInsert.NewLine(); } else if (target.BnfTerm == B.ARRAY_BEGIN) { rightInsertedUtokens = UtokenInsert.NewLine(); } else if (target.BnfTerm == B.ARRAY_END) { leftInsertedUtokens = rightInsertedUtokens = UtokenInsert.NewLine(); } else if (target.BnfTerm == B.COMMA) { leftInsertedUtokens = UtokenInsert.NoWhitespace(); rightInsertedUtokens = UtokenInsert.NewLine(); } else if (target.BnfTerm == B.COLON && !CompactFormat) { rightInsertedUtokens = UtokenInsert.NewLine(); } }
protected override bool TryGetUtokensDirectly(IUnparser unparser, UnparsableAst self, out IEnumerable <UtokenValue> utokens) { if (this.UtokenizerForUnparse != null) { utokens = this.UtokenizerForUnparse(unparser.FormatProvider, self, self.AstValue); return(true); } else if (this.isOptionalValue && object.Equals(self.AstValue, this.defaultValue)) { utokens = Enumerable.Empty <UtokenValue>(); return(true); } else if (this.inverseValueConverterForUnparse != null) { /* * NOTE that we *don't have to* check for "this.inverseValueConverterForUnparse != noUnparseByInverse" here because * IUnparsableNonTerminal.GetChildren will call this.inverseValueConverterForUnparse anyway and it will throw an * UnparseException if it equals to NoUnparseByInverse(). * * Moreover, we *cannot* check for "this.inverseValueConverterForUnparse != noUnparseByInverse" here properly because * of the usages of the generic versions of NoUnparseByInverse<...>() methods which wrap the original noUnparseByInverse * by calling CastValueConverter. * */ utokens = null; return(false); } else { throw new UnparseException(string.Format("Cannot unparse. '{0}' has neither UtokenizerForUnparse nor ValueConverterForUnparse", this.Name)); } }
private void NormalSyntaxHighlight(Utoken utoken, UnparsableAst target, IDecoration decoration) { if (target.AstValue is D.Color && SpecialSyntaxHighlightForColorLiterals) { Color color; if (Enum.TryParse <Color>(target.AstValue.ToString(), out color)) { decoration .Add(DecorationKey.Background, color) ; if (color.EqualToAny(Color.Black, Color.Blue, Color.Green, Color.Red)) { decoration .Add(DecorationKey.Foreground, Color.White) ; } else { decoration .Add(DecorationKey.Foreground, Color.Black) ; } } } else if (utoken.Discriminator.EqualToAny(CommentContent, CommentStartSymbol, CommentEndSymbol)) { decoration .Add(DecorationKey.Foreground, ForeColorOfComment) ; } else if (target.BnfTerm is KeyTerm) { if (target.BnfTerm.IsOperator() || target.BnfTerm.IsBrace()) { decoration .Add(DecorationKey.Foreground, ForeColorOfOperator) ; } else { decoration .Add(DecorationKey.Foreground, ForeColorOfKeyword) ; } } else if (target.AstValue is D.Type) { decoration .Add(DecorationKey.Foreground, ForeColorOfType) ; } else if (target.BnfTerm.IsLiteral() || target.BnfTerm.IsConstant()) { decoration .Add(DecorationKey.Foreground, ForeColorOfLiteral) ; } }
public override void GetUtokensAround(UnparsableAst target, out InsertedUtokens leftInsertedUtokens, out InsertedUtokens rightInsertedUtokens) { base.GetUtokensAround(target, out leftInsertedUtokens, out rightInsertedUtokens); if (target.BnfTerm == B.Statement) { leftInsertedUtokens = new[] { UtokenInsert.NewLine(), UtokenInsert.NewLine() } } ; } }
public override InsertedUtokens GetUtokensBetween(UnparsableAst leftTerminalLeaveTarget, UnparsableAst rightTarget) { if (leftTerminalLeaveTarget.BnfTerm is KeyTerm && rightTarget.BnfTerm == B.LEFT_PAREN) { return(UtokenInsert.NoWhitespace()); } else { return(base.GetUtokensBetween(leftTerminalLeaveTarget, rightTarget)); } }
private void NormalSyntaxHighlight(Utoken utoken, UnparsableAst target, IDecoration decoration) { if (target.BnfTerm is KeyTerm) { decoration .Add(DecorationKey.Foreground, ForeColorOfOperator) ; } else if (target.BnfTerm.IsLiteral() || target.BnfTerm.IsConstant()) { decoration .Add(DecorationKey.Foreground, ForeColorOfLiteral) ; } }
public override IDecoration GetDecoration(Utoken utoken, UnparsableAst target) { var decoration = base.GetDecoration(utoken, target); decoration.Add(DecorationKey.FontFamily, FontFamily.GenericMonospace); if (target != null) { if (SyntaxHighlight == GrammarPrefix.SyntaxHighlight.Color) { NormalSyntaxHighlight(utoken, target, decoration); } } return(decoration); }
public override IDecoration GetDecoration(Utoken utoken, UnparsableAst target) { var decoration = base.GetDecoration(utoken, target); decoration.Add(DecorationKey.FontFamily, FontFamily.GenericMonospace); if (utoken.Discriminator.EqualToAny(CommentContent, CommentStartSymbol, CommentEndSymbol)) { decoration .Add(DecorationKey.Foreground, ForeColorOfComment) ; return(decoration); } if (target != null) { if (target.BnfTerm.EqualToAny(B.BOOLEAN, B.NULL) || target.BnfTerm is NumberLiteral) { decoration.Add(DecorationKey.Foreground, ForeColorOfLiteral); } else if (target.AstParentMember != null && target.AstParentMember.BnfTerm == B.Key) { if (target.AstValue is string && ((string)target.AstValue).EqualToAny(TYPE_KEYWORD, COLLECTION_VALUES_KEYWORD, PRIMITIVE_VALUE_KEYWORD)) { decoration.Add(DecorationKey.Foreground, ForeColorOfKeyword); } else { decoration.Add(DecorationKey.Foreground, ForeColorOfKey); } } else if (target.AstParentMember != null && target.AstParentMember.BnfTerm == B.Value) { if (target.AstParent != null && target.AstParent.AstValue is KeyValuePair <string, object> && ((KeyValuePair <string, object>)target.AstParent.AstValue).IsTypeInfo) { decoration.Add(DecorationKey.Foreground, ForeColorOfType); } else { decoration.Add(DecorationKey.Foreground, ForeColorOfValue); } } } return(decoration); }
public override BlockIndentation GetBlockIndentation(UnparsableAst leftTerminalLeaveIfAny, UnparsableAst target) { if (target.BnfTerm == B.KeyValuePairs) { return(BlockIndentation.Indent); } else if (target.BnfTerm == B.ArrayElements) { return(BlockIndentation.Indent); } else { return(base.GetBlockIndentation(leftTerminalLeaveIfAny, target)); } }
private static int?GetBnfTermPriorityForMember(IUnparser unparser, UnparsableAst unparsableAst) { if (unparsableAst.AstValue != null) { return(1); } else if (unparsableAst.BnfTerm is BnfiTermCollection && ((BnfiTermCollection)unparsableAst.BnfTerm).EmptyCollectionHandling == EmptyCollectionHandling.ReturnNull) { return(0); } else if (unparsableAst.BnfTerm is BnfiTermConversion && ((BnfiTermConversion)unparsableAst.BnfTerm).isOptionalValue) { return(0); } else { return(null); } }
public override void GetUtokensAround(UnparsableAst target, out InsertedUtokens leftInsertedUtokens, out InsertedUtokens rightInsertedUtokens) { base.GetUtokensAround(target, out leftInsertedUtokens, out rightInsertedUtokens); if (target.BnfTerm == B.RIGHT_PAREN) { leftInsertedUtokens = UtokenInsert.NoWhitespace(); } else if (target.BnfTerm == B.LEFT_PAREN) { rightInsertedUtokens = UtokenInsert.NoWhitespace(); } else if (target.BnfTerm == B.UnaryOperator) { rightInsertedUtokens = UtokenInsert.NoWhitespace(); } }
public override InsertedUtokens GetUtokensBetween(UnparsableAst leftTerminalLeaveTarget, UnparsableAst rightTarget) { if (leftTerminalLeaveTarget.AstImage != null && leftTerminalLeaveTarget.AstImage.AstValue is DC.Name && rightTarget.BnfTerm == B.LEFT_PAREN) { return(UtokenInsert.NoWhitespace()); } else if (leftTerminalLeaveTarget.AstImage != null && leftTerminalLeaveTarget.AstImage.AstValue is DC.NameRef && rightTarget.BnfTerm == B.LEFT_PAREN) { return(UtokenInsert.NoWhitespace()); } else if (leftTerminalLeaveTarget.BnfTerm == B.WRITE && rightTarget.BnfTerm == B.LEFT_PAREN) { return(UtokenInsert.NoWhitespace()); } else if (leftTerminalLeaveTarget.BnfTerm == B.WRITELN && rightTarget.BnfTerm == B.LEFT_PAREN) { return(UtokenInsert.NoWhitespace()); } // alternative ways to handle "else if" spacing else if (FlattenIfHierarchy && leftTerminalLeaveTarget.BnfTerm == B.ELSE && rightTarget.BnfTerm == B.If) { return(UtokenInsert.Space().SetPriority(10)); } //else if (FlattenIfHierarchy && leftTerminalLeaveTarget.BnfTerm == B.ELSE && rightTarget.BnfTerm == B.IF) // return UtokenInsert.Space.SetPriority(10); //else if (FlattenIfHierarchy && leftTerminalLeaveTarget.BnfTerm == B.ELSE && rightTarget.AstValue is D.If) // return UtokenInsert.Space.SetPriority(10); else if (leftTerminalLeaveTarget.BnfTerm == B.END && rightTarget.BnfTerm == B.DOT) { return(UtokenInsert.NoWhitespace().SetPriority(10)); } else { return(base.GetUtokensBetween(leftTerminalLeaveTarget, rightTarget)); } }
public override InsertedUtokens GetUtokensBetween(UnparsableAst leftTerminalLeaveTarget, UnparsableAst rightTarget) { if (leftTerminalLeaveTarget.AstImage != null && leftTerminalLeaveTarget.AstImage.AstValue is DC.Name && rightTarget.BnfTerm == B.LEFT_PAREN) { return(UtokenInsert.NoWhitespace()); } else if (leftTerminalLeaveTarget.AstImage != null && leftTerminalLeaveTarget.AstImage.AstValue is DC.NameRef && rightTarget.BnfTerm == B.LEFT_PAREN) { return(UtokenInsert.NoWhitespace()); } else if (leftTerminalLeaveTarget.BnfTerm == B.WRITE && rightTarget.BnfTerm == B.LEFT_PAREN) { return(UtokenInsert.NoWhitespace()); } else if (leftTerminalLeaveTarget.BnfTerm == B.WRITELN && rightTarget.BnfTerm == B.LEFT_PAREN) { return(UtokenInsert.NoWhitespace()); } else if (FlattenIfHierarchy && leftTerminalLeaveTarget.BnfTerm == B.ELSE && rightTarget.BnfTerm == B.If) { return(UtokenInsert.Space().SetPriority(10)); } else if (rightTarget.BnfTerm == B.ELSE && leftTerminalLeaveTarget.BnfTerm == B.END && IndentStyle == GrammarC.IndentStyle.KernighanAndRitchie) { return(UtokenInsert.Space().SetPriority(10)); } else if (leftTerminalLeaveTarget.BnfTerm == B.END && rightTarget.BnfTerm == B.DOT) { return(UtokenInsert.NoWhitespace().SetPriority(10)); } else { return(base.GetUtokensBetween(leftTerminalLeaveTarget, rightTarget)); } }
public override BlockIndentation GetBlockIndentation(UnparsableAst leftTerminalLeaveIfAny, UnparsableAst target) { if (target.BnfTerm == B.Statement && !(target.AstValue is D.StatementList)) { return(BlockIndentation.Indent); } // alternative ways to handle "else if" indentation else if (FlattenIfHierarchy && leftTerminalLeaveIfAny != null && leftTerminalLeaveIfAny.BnfTerm == B.ELSE && target.BnfTerm == B.If) { return(BlockIndentation.Unindent); } //else if (FlattenIfHierarchy && leftTerminalLeaveIfAny != null && leftTerminalLeaveIfAny.BnfTerm == B.ELSE && target.AstValue is D.If) // return BlockIndentation.Unindent; else { return(base.GetBlockIndentation(leftTerminalLeaveIfAny, target)); } }
public override BlockIndentation GetBlockIndentation(UnparsableAst leftTerminalLeaveIfAny, UnparsableAst target) { if (target.BnfTerm == B.Statement && !(target.AstValue is D.StatementList)) { return(BlockIndentation.Indent); } else if (IndentStyle == GrammarC.IndentStyle.Whitesmiths && target.BnfTerm.EqualToAny(B.BEGIN, B.END)) { return(BlockIndentation.Indent); } else if (FlattenIfHierarchy && leftTerminalLeaveIfAny != null && leftTerminalLeaveIfAny.BnfTerm == B.ELSE && target.BnfTerm == B.If) { return(BlockIndentation.Unindent); } else { return(base.GetBlockIndentation(leftTerminalLeaveIfAny, target)); } }
public IEnumerable <UtokenValue> NumberLiteralToText(UnparsableAst reference, IFormatProvider formatProvider) { INumberLiteral numberLiteral = (INumberLiteral)reference.AstValue; string prefix = BaseToPrefix(numberLiteral.Base); string body = NumberToText(numberLiteral.Value, (int)numberLiteral.Base, formatProvider, numberLiteral.HasExplicitTypeModifier); string suffix = TypeToSuffix(numberLiteral); if (!string.IsNullOrEmpty(prefix)) { yield return(UtokenValue.CreateText(prefix, reference).SetDiscriminator(Formatter.NumberLiteralBasePrefix)); yield return(UtokenValue.NoWhitespace()); } yield return(UtokenValue.CreateText(body, reference).SetDiscriminator(Formatter.NumberLiteralContent)); if (!string.IsNullOrEmpty(suffix)) { yield return(UtokenValue.NoWhitespace()); yield return(UtokenValue.CreateText(suffix, reference).SetDiscriminator(Formatter.NumberLiteralTypeModifierSuffix)); } }
internal static Unparser.Priority DebugWriteLinePriority(this Unparser.Priority priority, TraceSource ts, UnparsableAst unparsableAst, string messageBefore = "", string messageAfter = "", string messageInside = "") { ts.Debug( "{0}{1}{2} obj: {3}; priority = {4}{5}{6}", messageBefore, unparsableAst.BnfTerm, messageInside != "" ? " " + messageInside : messageInside, unparsableAst.AstValue != null ? unparsableAst.AstValue.ToString() : "<<NULL>>", priority.Kind, priority.Value.HasValue ? priority.Value.ToString() : "NULL", messageAfter ); return(priority); }
public override void GetUtokensAround(UnparsableAst target, out InsertedUtokens leftInsertedUtokens, out InsertedUtokens rightInsertedUtokens) { base.GetUtokensAround(target, out leftInsertedUtokens, out rightInsertedUtokens); if (target.BnfTerm == B.DOT) { leftInsertedUtokens = rightInsertedUtokens = UtokenInsert.NoWhitespace(); } else if (target.BnfTerm == B.RIGHT_PAREN) { leftInsertedUtokens = UtokenInsert.NoWhitespace(); } else if (target.BnfTerm == B.LEFT_PAREN) { rightInsertedUtokens = UtokenInsert.NoWhitespace(); } else if (target.BnfTerm == B.SEMICOLON) { leftInsertedUtokens = UtokenInsert.NoWhitespace(); } else if (target.BnfTerm == B.COMMA) { leftInsertedUtokens = UtokenInsert.NoWhitespace(); } else if (target.BnfTerm == B.Statement) { leftInsertedUtokens = rightInsertedUtokens = UtokenInsert.NewLine(); } else if (target.BnfTerm == B.BEGIN) { rightInsertedUtokens = UtokenInsert.NewLine(); if (IndentStyle.EqualToAny(GrammarC.IndentStyle.Allman, GrammarC.IndentStyle.Whitesmiths)) { leftInsertedUtokens = UtokenInsert.NewLine(); } else if (IndentStyle.EqualToAny(GrammarC.IndentStyle.Stroustrup, GrammarC.IndentStyle.KernighanAndRitchie)) { leftInsertedUtokens = UtokenInsert.Space(); } } else if (target.BnfTerm == B.END) { leftInsertedUtokens = rightInsertedUtokens = UtokenInsert.NewLine(); if (target.AstValue is D.Function) { rightInsertedUtokens = UtokenInsert.EmptyLine().SetPriority(10); } } else if (target.BnfTerm == B.UnaryOperator) { rightInsertedUtokens = UtokenInsert.NoWhitespace(); } else if (target.BnfTerm == B.Name && target.AstParent != null && target.AstParent.AstValue is D.Program) { rightInsertedUtokens = UtokenInsert.EmptyLine(); } else if (target.BnfTerm == B.NamespaceName && target.AstParent != null && target.AstParent.AstValue is D.Program) { rightInsertedUtokens = UtokenInsert.EmptyLine(); } }
protected override bool TryGetUtokensDirectly(IUnparser unparser, UnparsableAst self, out IEnumerable <UtokenValue> utokens) { utokens = null; return(false); }
bool IUnparsableNonTerminal.TryGetUtokensDirectly(IUnparser unparser, UnparsableAst self, out IEnumerable <UtokenValue> utokens) { return(TryGetUtokensDirectly(unparser, self, out utokens)); }
protected abstract bool TryGetUtokensDirectly(IUnparser unparser, UnparsableAst self, out IEnumerable <UtokenValue> utokens);
public override void GetUtokensAround(UnparsableAst target, out InsertedUtokens leftInsertedUtokens, out InsertedUtokens rightInsertedUtokens) { base.GetUtokensAround(target, out leftInsertedUtokens, out rightInsertedUtokens); #if false // alternative way to handle "else if" spacing if (target.AstValue is D.If && target.AstParent != null && target.AstParent.AstValue is D.If && // target.ParentMember != null && target.ParentMember.MemberInfo == Util.GetType<D.If>().GetMember(@if => @if.ElseBody)) target.ParentMember != null && target.ParentMember.MemberInfo == Util.GetMember(() => Util.GetType <D.If>().ElseBody) && target.BnfTerm == B.ELSE) { var foo = target.BnfTerm; rightInsertedUtokens = UtokenInsert.Space.SetPriority(10); return; } #endif if (target.BnfTerm == B.DOT) { leftInsertedUtokens = rightInsertedUtokens = UtokenInsert.NoWhitespace(); } else if (target.BnfTerm == B.RIGHT_PAREN) { leftInsertedUtokens = UtokenInsert.NoWhitespace(); } else if (target.BnfTerm == B.LEFT_PAREN) { rightInsertedUtokens = UtokenInsert.NoWhitespace(); } else if (target.BnfTerm == B.SEMICOLON) { leftInsertedUtokens = UtokenInsert.NoWhitespace(); } else if (target.BnfTerm == B.COMMA) { leftInsertedUtokens = UtokenInsert.NoWhitespace(); } else if (target.BnfTerm == B.Statement) { leftInsertedUtokens = rightInsertedUtokens = UtokenInsert.NewLine(); } else if (target.BnfTerm == B.BEGIN) { leftInsertedUtokens = rightInsertedUtokens = UtokenInsert.NewLine(); } else if (target.BnfTerm == B.END) { leftInsertedUtokens = rightInsertedUtokens = UtokenInsert.NewLine(); if (target.AstValue is D.Function) { rightInsertedUtokens = UtokenInsert.EmptyLine().SetPriority(10); } } else if (target.BnfTerm == B.UnaryOperator) { rightInsertedUtokens = UtokenInsert.NoWhitespace(); } else if (target.BnfTerm == B.Name && target.AstParent != null && target.AstParent.AstValue is D.Program) { rightInsertedUtokens = UtokenInsert.EmptyLine(); } else if (target.BnfTerm == B.NamespaceName && target.AstParent != null && target.AstParent.AstValue is D.Program) { rightInsertedUtokens = UtokenInsert.EmptyLine(); } }
private void CrazySyntaxHighlight(Utoken utoken, UnparsableAst target, IDecoration decoration) { if (target.AstValue is D.Color) { Color color; if (Enum.TryParse <Color>(target.AstValue.ToString(), out color)) { decoration .Add(DecorationKey.Background, color) ; if (color.EqualToAny(Color.Black, Color.Blue, Color.Green, Color.Red)) { decoration .Add(DecorationKey.Foreground, Color.White) ; } else { decoration .Add(DecorationKey.Foreground, Color.Black) ; } } } else if (utoken.Discriminator == CommentContent) { decoration .Add(DecorationKey.Foreground, Color.Pink) .Add(DecorationKey.TextDecoration, TextDecoration.Strikethrough) .Add(DecorationKey.FontStyle, FontStyle.Italic) ; } else if (utoken.Discriminator == CommentStartSymbol) { decoration .Add(DecorationKey.Foreground, Color.Yellow) .Add(DecorationKey.Background, Color.Violet) ; } else if (utoken.Discriminator == CommentEndSymbol) { decoration .Add(DecorationKey.Foreground, Color.Blue) .Add(DecorationKey.Background, Color.Yellow) ; } else if (utoken.Discriminator == StringLiteralStartSymbol) { decoration .Add(DecorationKey.FontSizeRelativePercent, 2) .Add(DecorationKey.Foreground, Color.Red) .Add(DecorationKey.Background, Color.Blue) ; } else if (target.AstValue is D.If) { if (target.BnfTerm == B.LEFT_PAREN) { decoration .Add(DecorationKey.FontWeight, FontWeight.Bold) .Add(DecorationKey.FontSizeRelativePercent, 2) .Add(DecorationKey.Foreground, Color.Blue) ; } else { decoration .Add(DecorationKey.FontWeight, FontWeight.Bold) .Add(DecorationKey.TextDecoration, TextDecoration.Underline) ; } } else if (target.BnfTerm == B.PROGRAM) { decoration .Add(DecorationKey.FontStyle, FontStyle.Italic) .Add(DecorationKey.Foreground, Color.Red) .Add(DecorationKey.Background, Color.Yellow) .Add(DecorationKey.FontSize, 30); } else if (target.AstValue is D.Type) { decoration .Add(DecorationKey.BaselineAlignment, BaselineAlignment.Subscript) .Add(DecorationKey.FontSizeRelativePercent, 0.75); } else if (target.AstValue is INumberLiteral) { INumberLiteral number = (INumberLiteral)target.AstValue; if (number.Value is int) { if (((int)number.Value) % 2 == 0) { decoration .Add(DecorationKey.Foreground, Color.Red) ; } else { decoration .Add(DecorationKey.Foreground, Color.Green) .Add(DecorationKey.Background, Color.Yellow) ; } } } }