public override void WriteKeyword(Role role, string keyword) { if (lastWritten == LastWritten.KeywordOrIdentifier) { Space(); } base.WriteKeyword(role, keyword); lastWritten = LastWritten.KeywordOrIdentifier; }
public override void WriteKeyword(Role role, string keyword) { if (lastWritten == LastWritten.KeywordOrIdentifier) { Space(); } base.WriteKeyword(role, keyword); lastWritten = LastWritten.KeywordOrIdentifier; }
public override void WritePrimitiveValue(object value, string literalValue = null) { if (lastWritten == LastWritten.KeywordOrIdentifier) { Space(); } base.WritePrimitiveValue(value, literalValue); if (value == null || value is bool) { return; } if (value is string) { lastWritten = LastWritten.Other; } else if (value is char) { lastWritten = LastWritten.Other; } else if (value is decimal) { lastWritten = LastWritten.Other; } else if (value is float) { float f = (float)value; if (float.IsInfinity(f) || float.IsNaN(f)) { return; } lastWritten = LastWritten.Other; } else if (value is double) { double f = (double)value; if (double.IsInfinity(f) || double.IsNaN(f)) { return; } // needs space if identifier follows number; // this avoids mistaking the following identifier as type suffix lastWritten = LastWritten.KeywordOrIdentifier; } else if (value is IFormattable) { // needs space if identifier follows number; // this avoids mistaking the following identifier as type suffix lastWritten = LastWritten.KeywordOrIdentifier; } else { lastWritten = LastWritten.Other; } }
public override void WriteComment(CommentType commentType, string content) { if (lastWritten == LastWritten.Division) { // When there's a comment starting after a division operator // "1.0 / /*comment*/a", then we need to insert a space in front of the comment. base.Space(); } base.WriteComment(commentType, content); lastWritten = LastWritten.Whitespace; }
public override void WriteIdentifier(Identifier identifier, TextTokenType tokenType) { if (identifier.IsVerbatim || CSharpOutputVisitor.IsKeyword(identifier.Name, identifier)) { if (lastWritten == LastWritten.KeywordOrIdentifier) { // this space is not strictly required, so we call Space() Space(); } } else if (lastWritten == LastWritten.KeywordOrIdentifier) { // this space is strictly required, so we directly call the formatter base.Space(); } base.WriteIdentifier(identifier, tokenType); lastWritten = LastWritten.KeywordOrIdentifier; }
public override void WriteIdentifier(Identifier identifier) { if (identifier.IsVerbatim) { if (lastWritten == LastWritten.KeywordOrIdentifier) { // this space is not strictly required, so we call Space() Space(); } } else if (lastWritten == LastWritten.KeywordOrIdentifier) { // this space is strictly required, so we directly call the formatter base.Space(); } base.WriteIdentifier(identifier); lastWritten = LastWritten.KeywordOrIdentifier; }
public override void WriteIdentifier(Identifier ident) { if(ident.IsVerbatim || ExpressoOutputWalker.IsKeyword(ident.Name, ident)){ if(last_written == LastWritten.KeywordOrIdentifier){ // This space is not strictly required, so we delegate to Space() Space(); } }else if(last_written == LastWritten.KeywordOrIdentifier){ // This space is strictly required, so we directly call the formatter base.Space(); } base.WriteIdentifier(ident); last_written = LastWritten.KeywordOrIdentifier; }
public override void WritePrimitiveType(string type) { if (lastWritten == LastWritten.KeywordOrIdentifier) { Space(); } base.WritePrimitiveType(type); if (type == "new") { lastWritten = LastWritten.Other; } else { lastWritten = LastWritten.KeywordOrIdentifier; } }
public override void WriteIdentifier(Identifier identifier) { if (identifier.IsVerbatim || CSharpOutputVisitor.IsKeyword(identifier.Name, identifier)) { if (lastWritten == LastWritten.KeywordOrIdentifier) { // this space is not strictly required, so we call Space() Space(); } } else if (lastWritten == LastWritten.KeywordOrIdentifier) { // this space is strictly required, so we directly call the formatter base.Space(); } base.WriteIdentifier(identifier); lastWritten = LastWritten.KeywordOrIdentifier; }
public override void WriteToken(Role role, string token) { // Avoid that two +, - or ? tokens are combined into a ++, -- or ?? token. // Note that we don't need to handle tokens like = because there's no valid // C# program that contains the single token twice in a row. // (for +, - and &, this can happen with unary operators; // for ?, this can happen in "a is int? ? b : c" or "a as int? ?? 0"; // and for /, this can happen with "1/ *ptr" or "1/ //comment".) if (lastWritten == LastWritten.Plus && token[0] == '+' || lastWritten == LastWritten.Minus && token[0] == '-' || lastWritten == LastWritten.Ampersand && token[0] == '&' || lastWritten == LastWritten.QuestionMark && token[0] == '?' || lastWritten == LastWritten.Division && token[0] == '*') { base.Space(); } base.WriteToken(role, token); if (token == "+") { lastWritten = LastWritten.Plus; } else if (token == "-") { lastWritten = LastWritten.Minus; } else if (token == "&") { lastWritten = LastWritten.Ampersand; } else if (token == "?") { lastWritten = LastWritten.QuestionMark; } else if (token == "/") { lastWritten = LastWritten.Division; } else { lastWritten = LastWritten.Other; } }
public void VisitComment(Comment comment) { if (lastWritten == LastWritten.Division) { // When there's a comment starting after a division operator // "1.0 / /*comment*/a", then we need to insert a space in front of the comment. formatter.Space(); } formatter.StartNode(comment); formatter.WriteComment(comment.CommentType, comment.Content); formatter.EndNode(comment); lastWritten = LastWritten.Whitespace; }
public void VisitPreProcessorDirective(PreProcessorDirective preProcessorDirective) { formatter.StartNode(preProcessorDirective); formatter.WritePreProcessorDirective(preProcessorDirective.Type, preProcessorDirective.Argument); formatter.EndNode(preProcessorDirective); lastWritten = LastWritten.Whitespace; }
/// <summary> /// Writes a comma. /// </summary> /// <param name="nextNode">The next node after the comma.</param> /// <param name="noSpaceAfterComma">When set prevents printing a space after comma.</param> void Comma(AstNode nextNode, bool noSpaceAfterComma = false) { WriteSpecialsUpToRole(Roles.Comma, nextNode); Space(policy.SpaceBeforeBracketComma); // TODO: Comma policy has changed. formatter.WriteToken(","); lastWritten = LastWritten.Other; Space(!noSpaceAfterComma && policy.SpaceAfterBracketComma); // TODO: Comma policy has changed. }
public void VisitArraySpecifier(ArraySpecifier arraySpecifier) { StartNode(arraySpecifier); WriteToken(Roles.LBracket); foreach (var comma in arraySpecifier.GetChildrenByRole(Roles.Comma)) { WriteSpecialsUpToNode(comma); formatter.WriteToken(","); lastWritten = LastWritten.Other; } WriteToken(Roles.RBracket); EndNode(arraySpecifier); }
void CloseBrace(BraceStyle style) { WriteSpecialsUpToRole(Roles.RBrace); formatter.CloseBrace(style); lastWritten = LastWritten.Other; }
public override void WritePrimitiveValue(object value, TextTokenType? tokenType = null, string literalValue = null) { base.WritePrimitiveValue(value, tokenType, literalValue); if (value == null || value is bool) return; if (value is string) { lastWritten = LastWritten.Other; } else if (value is char) { lastWritten = LastWritten.Other; } else if (value is decimal) { lastWritten = LastWritten.Other; } else if (value is float) { float f = (float)value; if (float.IsInfinity(f) || float.IsNaN(f)) return; lastWritten = LastWritten.Other; } else if (value is double) { double f = (double)value; if (double.IsInfinity(f) || double.IsNaN(f)) return; // needs space if identifier follows number; // this avoids mistaking the following identifier as type suffix lastWritten = LastWritten.KeywordOrIdentifier; } else if (value is IFormattable) { // needs space if identifier follows number; // this avoids mistaking the following identifier as type suffix lastWritten = LastWritten.KeywordOrIdentifier; } else { lastWritten = LastWritten.Other; } }
void WriteToken(string token, Role tokenRole) { WriteSpecialsUpToRole(tokenRole); // Avoid that two +, - or ? tokens are combined into a ++, -- or ?? token. // Note that we don't need to handle tokens like = because there's no valid // C# program that contains the single token twice in a row. // (for +, - and &, this can happen with unary operators; // for ?, this can happen in "a is int? ? b : c" or "a as int? ?? 0"; // and for /, this can happen with "1/ *ptr" or "1/ //comment".) // If a destructor has modifiers then there should be a space before ~ if (lastWritten == LastWritten.Plus && token [0] == '+' || lastWritten == LastWritten.Minus && token [0] == '-' || lastWritten == LastWritten.Ampersand && token [0] == '&' || lastWritten == LastWritten.QuestionMark && token [0] == '?' || lastWritten == LastWritten.Division && token [0] == '*' || lastWritten == LastWritten.KeywordOrIdentifier && token[0] == '~') { formatter.Space(); } formatter.WriteToken(token); if (token == "+") { lastWritten = LastWritten.Plus; } else if (token == "-") { lastWritten = LastWritten.Minus; } else if (token == "&") { lastWritten = LastWritten.Ampersand; } else if (token == "?") { lastWritten = LastWritten.QuestionMark; } else if (token == "/") { lastWritten = LastWritten.Division; } else { lastWritten = LastWritten.Other; } }
public override void WritePreProcessorDirective(PreProcessorDirectiveType type, string argument) { base.WritePreProcessorDirective(type, argument); lastWritten = LastWritten.Whitespace; }
public override void WritePreProcessorDirective(PreProcessorDirectiveType type, string argument) { base.WritePreProcessorDirective(type, argument); lastWritten = LastWritten.Whitespace; }
public override void NewLine() { base.NewLine(); lastWritten = LastWritten.Whitespace; }
public override void WriteComment(CommentType commentType, string content, CommentReference[] refs) { if (lastWritten == LastWritten.Division) { // When there's a comment starting after a division operator // "1.0 / /*comment*/a", then we need to insert a space in front of the comment. base.Space(); } base.WriteComment(commentType, content, refs); lastWritten = LastWritten.Whitespace; }
public override void NewLine() { base.NewLine(); lastWritten = LastWritten.Whitespace; }
public override void Space() { base.Space(); lastWritten = LastWritten.Whitespace; }
void WriteKeyword(string token, Role tokenRole = null) { if (tokenRole != null) { WriteSpecialsUpToRole(tokenRole); } if (lastWritten == LastWritten.KeywordOrIdentifier) { formatter.Space(); } formatter.WriteKeyword(token); lastWritten = LastWritten.KeywordOrIdentifier; }
void NewLine() { formatter.NewLine(); lastWritten = LastWritten.Whitespace; }
/* void WriteKeyword (string keyword, Role tokenRole) { WriteSpecialsUpToRole (tokenRole); if (lastWritten == LastWritten.KeywordOrIdentifier) formatter.Space (); formatter.WriteKeyword (keyword); lastWritten = LastWritten.KeywordOrIdentifier; }*/ void WriteIdentifier(string identifier, Role<Identifier> identifierRole = null) { WriteSpecialsUpToRole(identifierRole ?? Roles.Identifier); if (IsKeyword(identifier, containerStack.Peek())) { if (lastWritten == LastWritten.KeywordOrIdentifier) { Space(); } // this space is not strictly required, so we call Space() formatter.WriteToken("@"); } else if (lastWritten == LastWritten.KeywordOrIdentifier) { formatter.Space(); // this space is strictly required, so we directly call the formatter } formatter.WriteIdentifier(identifier); lastWritten = LastWritten.KeywordOrIdentifier; }
public override void WriteToken(Role role, string token, TextTokenType tokenType) { // Avoid that two +, - or ? tokens are combined into a ++, -- or ?? token. // Note that we don't need to handle tokens like = because there's no valid // C# program that contains the single token twice in a row. // (for +, - and &, this can happen with unary operators; // for ?, this can happen in "a is int? ? b : c" or "a as int? ?? 0"; // and for /, this can happen with "1/ *ptr" or "1/ //comment".) if (lastWritten == LastWritten.Plus && token[0] == '+' || lastWritten == LastWritten.Minus && token[0] == '-' || lastWritten == LastWritten.Ampersand && token[0] == '&' || lastWritten == LastWritten.QuestionMark && token[0] == '?' || lastWritten == LastWritten.Division && token[0] == '*') { base.Space(); } base.WriteToken(role, token, tokenType); if (token == "+") { lastWritten = LastWritten.Plus; } else if (token == "-") { lastWritten = LastWritten.Minus; } else if (token == "&") { lastWritten = LastWritten.Ampersand; } else if (token == "?") { lastWritten = LastWritten.QuestionMark; } else if (token == "/") { lastWritten = LastWritten.Division; } else { lastWritten = LastWritten.Other; } }
/// <summary> /// Writes a space depending on policy. /// </summary> void Space(bool addSpace = true) { if (addSpace) { formatter.Space(); lastWritten = LastWritten.Whitespace; } }
public override void Space() { base.Space(); lastWritten = LastWritten.Whitespace; }
void OpenBrace(BraceStyle style) { WriteSpecialsUpToRole(Roles.LBrace); formatter.OpenBrace(style); lastWritten = LastWritten.Other; }
void WritePrimitiveValue(object val) { if (val == null) { // usually NullReferenceExpression should be used for this, but we'll handle it anyways WriteKeyword("null"); return; } if (val is bool) { if ((bool)val) { WriteKeyword("true"); } else { WriteKeyword("false"); } return; } if (val is string) { formatter.WriteToken("\"" + ConvertString(val.ToString()) + "\""); lastWritten = LastWritten.Other; } else if (val is char) { formatter.WriteToken("'" + ConvertCharLiteral((char)val) + "'"); lastWritten = LastWritten.Other; } else if (val is decimal) { formatter.WriteToken(((decimal)val).ToString(NumberFormatInfo.InvariantInfo) + "m"); lastWritten = LastWritten.Other; } else if (val is float) { float f = (float)val; if (float.IsInfinity(f) || float.IsNaN(f)) { // Strictly speaking, these aren't PrimitiveExpressions; // but we still support writing these to make life easier for code generators. WriteKeyword("float"); WriteToken(Roles.Dot); if (float.IsPositiveInfinity(f)) { WriteIdentifier("PositiveInfinity"); } else if (float.IsNegativeInfinity(f)) { WriteIdentifier("NegativeInfinity"); } else { WriteIdentifier("NaN"); } return; } if (f == 0 && 1 / f == double.NegativeInfinity) { // negative zero is a special case // (again, not a primitive expression, but it's better to handle // the special case here than to do it in all code generators) formatter.WriteToken("-"); } formatter.WriteToken(f.ToString("R", NumberFormatInfo.InvariantInfo) + "f"); lastWritten = LastWritten.Other; } else if (val is double) { double f = (double)val; if (double.IsInfinity(f) || double.IsNaN(f)) { // Strictly speaking, these aren't PrimitiveExpressions; // but we still support writing these to make life easier for code generators. WriteKeyword("double"); WriteToken(Roles.Dot); if (double.IsPositiveInfinity(f)) { WriteIdentifier("PositiveInfinity"); } else if (double.IsNegativeInfinity(f)) { WriteIdentifier("NegativeInfinity"); } else { WriteIdentifier("NaN"); } return; } if (f == 0 && 1 / f == double.NegativeInfinity) { // negative zero is a special case // (again, not a primitive expression, but it's better to handle // the special case here than to do it in all code generators) formatter.WriteToken("-"); } string number = f.ToString("R", NumberFormatInfo.InvariantInfo); if (number.IndexOf('.') < 0 && number.IndexOf('E') < 0) { number += ".0"; } formatter.WriteToken(number); // needs space if identifier follows number; this avoids mistaking the following identifier as type suffix lastWritten = LastWritten.KeywordOrIdentifier; } else if (val is IFormattable) { StringBuilder b = new StringBuilder (); // if (primitiveExpression.LiteralFormat == LiteralFormat.HexadecimalNumber) { // b.Append("0x"); // b.Append(((IFormattable)val).ToString("x", NumberFormatInfo.InvariantInfo)); // } else { b.Append(((IFormattable)val).ToString(null, NumberFormatInfo.InvariantInfo)); // } if (val is uint || val is ulong) { b.Append("u"); } if (val is long || val is ulong) { b.Append("L"); } formatter.WriteToken(b.ToString()); // needs space if identifier follows number; this avoids mistaking the following identifier as type suffix lastWritten = LastWritten.KeywordOrIdentifier; } else { formatter.WriteToken(val.ToString()); lastWritten = LastWritten.Other; } }
void WriteQualifiedIdentifier(IEnumerable<Identifier> identifiers) { bool first = true; foreach (Identifier ident in identifiers) { if (first) { first = false; if (lastWritten == LastWritten.KeywordOrIdentifier) { formatter.Space(); } } else { WriteSpecialsUpToRole(Roles.Dot, ident); formatter.WriteToken("."); lastWritten = LastWritten.Other; } WriteSpecialsUpToNode(ident); formatter.WriteIdentifier(ident.Name); lastWritten = LastWritten.KeywordOrIdentifier; } }
public override void WritePrimitiveType(string type) { if (lastWritten == LastWritten.KeywordOrIdentifier) { Space(); } base.WritePrimitiveType(type); if (type == "new") { lastWritten = LastWritten.Other; } else { lastWritten = LastWritten.KeywordOrIdentifier; } }