📄 csharpoutputvisitor.cs
字号:
public object VisitFixedStatement(FixedStatement fixedStatement, object data)
{
outputFormatter.PrintToken(Tokens.Fixed);
if (this.prettyPrintOptions.FixedParentheses) {
outputFormatter.Space();
}
outputFormatter.PrintToken(Tokens.OpenParenthesis);
nodeTracker.TrackedVisit(fixedStatement.TypeReference, data);
outputFormatter.Space();
AppendCommaSeparatedList(fixedStatement.PointerDeclarators);
outputFormatter.PrintToken(Tokens.CloseParenthesis);
WriteEmbeddedStatement(fixedStatement.EmbeddedStatement);
return null;
}
public object VisitUnsafeStatement(UnsafeStatement unsafeStatement, object data)
{
outputFormatter.PrintToken(Tokens.Unsafe);
WriteEmbeddedStatement(unsafeStatement.Block);
return null;
}
public object VisitCheckedStatement(CheckedStatement checkedStatement, object data)
{
outputFormatter.PrintToken(Tokens.Checked);
WriteEmbeddedStatement(checkedStatement.Block);
return null;
}
public object VisitUncheckedStatement(UncheckedStatement uncheckedStatement, object data)
{
outputFormatter.PrintToken(Tokens.Unchecked);
WriteEmbeddedStatement(uncheckedStatement.Block);
return null;
}
public object VisitExitStatement(ExitStatement exitStatement, object data)
{
if (exitStatement.ExitType == ExitType.Function || exitStatement.ExitType == ExitType.Sub || exitStatement.ExitType == ExitType.Property) {
outputFormatter.PrintToken(Tokens.Return);
} else {
outputFormatter.PrintToken(Tokens.Break);
}
outputFormatter.PrintToken(Tokens.Semicolon);
outputFormatter.PrintText(" // TODO: might not be correct. Was : Exit " + exitStatement.ExitType);
outputFormatter.NewLine();
return null;
}
public object VisitForNextStatement(ForNextStatement forNextStatement, object data)
{
outputFormatter.PrintToken(Tokens.For);
outputFormatter.Space();
outputFormatter.PrintToken(Tokens.OpenParenthesis);
if (!forNextStatement.TypeReference.IsNull) {
nodeTracker.TrackedVisit(forNextStatement.TypeReference, data);
outputFormatter.Space();
}
outputFormatter.PrintIdentifier(forNextStatement.VariableName);
outputFormatter.Space();
outputFormatter.PrintToken(Tokens.Assign);
outputFormatter.Space();
nodeTracker.TrackedVisit(forNextStatement.Start, data);
outputFormatter.PrintToken(Tokens.Semicolon);
outputFormatter.Space();
outputFormatter.PrintIdentifier(forNextStatement.VariableName);
outputFormatter.Space();
PrimitiveExpression pe = forNextStatement.Step as PrimitiveExpression;
if ((pe == null || !(pe.Value is int) || ((int)pe.Value) >= 0)
&& !(forNextStatement.Step is UnaryOperatorExpression))
outputFormatter.PrintToken(Tokens.LessEqual);
else
outputFormatter.PrintToken(Tokens.GreaterEqual);
outputFormatter.Space();
nodeTracker.TrackedVisit(forNextStatement.End, data);
outputFormatter.PrintToken(Tokens.Semicolon);
outputFormatter.Space();
outputFormatter.PrintIdentifier(forNextStatement.VariableName);
if (forNextStatement.Step.IsNull) {
outputFormatter.PrintToken(Tokens.Increment);
} else {
outputFormatter.Space();
outputFormatter.PrintToken(Tokens.PlusAssign);
outputFormatter.Space();
nodeTracker.TrackedVisit(forNextStatement.Step, data);
}
outputFormatter.PrintToken(Tokens.CloseParenthesis);
WriteEmbeddedStatement(forNextStatement.EmbeddedStatement);
return null;
}
#endregion
#region Expressions
public object VisitClassReferenceExpression(ClassReferenceExpression classReferenceExpression, object data)
{
NotSupported(classReferenceExpression);
return null;
}
static string ConvertCharLiteral(char ch)
{
if (ch == '\'') return "\\'";
return ConvertChar(ch);
}
static string ConvertChar(char ch)
{
switch (ch) {
case '\\':
return "\\\\";
case '\0':
return "\\0";
case '\a':
return "\\a";
case '\b':
return "\\b";
case '\f':
return "\\f";
case '\n':
return "\\n";
case '\r':
return "\\r";
case '\t':
return "\\t";
case '\v':
return "\\v";
default:
if (char.IsControl(ch)) {
return "\\u" + (int)ch;
} else {
return ch.ToString();
}
}
}
static string ConvertString(string str)
{
StringBuilder sb = new StringBuilder();
foreach (char ch in str) {
if (ch == '"')
sb.Append("\\\"");
else
sb.Append(ConvertChar(ch));
}
return sb.ToString();
}
public object VisitPrimitiveExpression(PrimitiveExpression primitiveExpression, object data)
{
if (primitiveExpression.Value == null) {
outputFormatter.PrintToken(Tokens.Null);
return null;
}
object val = primitiveExpression.Value;
if (val is bool) {
if ((bool)val) {
outputFormatter.PrintToken(Tokens.True);
} else {
outputFormatter.PrintToken(Tokens.False);
}
return null;
}
if (val is string) {
outputFormatter.PrintText('"' + ConvertString(val.ToString()) + '"');
return null;
}
if (val is char) {
outputFormatter.PrintText("'" + ConvertCharLiteral((char)val) + "'");
return null;
}
if (val is decimal) {
outputFormatter.PrintText(((decimal)val).ToString(NumberFormatInfo.InvariantInfo) + "m");
return null;
}
if (val is float) {
outputFormatter.PrintText(((float)val).ToString(NumberFormatInfo.InvariantInfo) + "f");
return null;
}
if (val is double) {
string text = ((double)val).ToString(NumberFormatInfo.InvariantInfo);
if (text.IndexOf('.') < 0 && text.IndexOf('E') < 0)
outputFormatter.PrintText(text + ".0");
else
outputFormatter.PrintText(text);
return null;
}
if (val is IFormattable) {
outputFormatter.PrintText(((IFormattable)val).ToString(null, NumberFormatInfo.InvariantInfo));
if (val is uint || val is ulong) {
outputFormatter.PrintText("u");
}
if (val is long || val is ulong) {
outputFormatter.PrintText("l");
}
} else {
outputFormatter.PrintText(val.ToString());
}
return null;
}
static bool IsNullLiteralExpression(Expression expr)
{
PrimitiveExpression pe = expr as PrimitiveExpression;
if (pe == null) return false;
return pe.Value == null;
}
public object VisitBinaryOperatorExpression(BinaryOperatorExpression binaryOperatorExpression, object data)
{
// VB-operators that require special representation:
switch (binaryOperatorExpression.Op) {
case BinaryOperatorType.ReferenceEquality:
case BinaryOperatorType.ReferenceInequality:
if (IsNullLiteralExpression(binaryOperatorExpression.Left) || IsNullLiteralExpression(binaryOperatorExpression.Right)) {
// prefer a == null to object.ReferenceEquals(a, null)
break;
}
if (binaryOperatorExpression.Op == BinaryOperatorType.ReferenceInequality)
outputFormatter.PrintToken(Tokens.Not);
outputFormatter.PrintText("object.ReferenceEquals");
if (prettyPrintOptions.BeforeMethodCallParentheses) {
outputFormatter.Space();
}
outputFormatter.PrintToken(Tokens.OpenParenthesis);
nodeTracker.TrackedVisit(binaryOperatorExpression.Left, data);
PrintFormattedComma();
nodeTracker.TrackedVisit(binaryOperatorExpression.Right, data);
outputFormatter.PrintToken(Tokens.CloseParenthesis);
return null;
case BinaryOperatorType.Power:
outputFormatter.PrintText("Math.Pow");
if (prettyPrintOptions.BeforeMethodCallParentheses) {
outputFormatter.Space();
}
outputFormatter.PrintToken(Tokens.OpenParenthesis);
nodeTracker.TrackedVisit(binaryOperatorExpression.Left, data);
PrintFormattedComma();
nodeTracker.TrackedVisit(binaryOperatorExpression.Right, data);
outputFormatter.PrintToken(Tokens.CloseParenthesis);
return null;
}
nodeTracker.TrackedVisit(binaryOperatorExpression.Left, data);
switch (binaryOperatorExpression.Op) {
case BinaryOperatorType.Add:
case BinaryOperatorType.Concat: // translate Concatenation to +
if (prettyPrintOptions.AroundAdditiveOperatorParentheses) {
outputFormatter.Space();
}
outputFormatter.PrintToken(Tokens.Plus);
if (prettyPrintOptions.AroundAdditiveOperatorParentheses) {
outputFormatter.Space();
}
break;
case BinaryOperatorType.Subtract:
if (prettyPrintOptions.AroundAdditiveOperatorParentheses) {
outputFormatter.Space();
}
outputFormatter.PrintToken(Tokens.Minus);
if (prettyPrintOptions.AroundAdditiveOperatorParentheses) {
outputFormatter.Space();
}
break;
case BinaryOperatorType.Multiply:
if (prettyPrintOptions.AroundMultiplicativeOperatorParentheses) {
outputFormatter.Space();
}
outputFormatter.PrintToken(Tokens.Times);
if (prettyPrintOptions.AroundMultiplicativeOperatorParentheses) {
outputFormatter.Space();
}
break;
case BinaryOperatorType.Divide:
case BinaryOperatorType.DivideInteger:
if (prettyPrintOptions.AroundMultiplicativeOperatorParentheses) {
outputFormatter.Space();
}
outputFormatter.PrintToken(Tokens.Div);
if (prettyPrintOptions.AroundMultiplicativeOperatorParentheses) {
outputFormatter.Space();
}
break;
case BinaryOperatorType.Modulus:
if (prettyPrintOptions.AroundMultiplicativeOperatorParentheses) {
outputFormatter.Space();
}
outputFormatter.PrintToken(Tokens.Mod);
if (prettyPrintOptions.AroundMultiplicativeOperatorParentheses) {
outputFormatter.Space();
}
break;
case BinaryOperatorType.ShiftLeft:
if (prettyPrintOptions.AroundShiftOperatorParentheses) {
outputFormatter.Space();
}
outputFormatter.PrintToken(Tokens.ShiftLeft);
if (prettyPrintOptions.AroundShiftOperatorParentheses) {
outputFormatter.Space();
}
break;
case BinaryOperatorType.ShiftRight:
if (prettyPrintOptions.AroundShiftOperatorParentheses) {
outputFormatter.Space();
}
outputFormatter.PrintToken(Tokens.GreaterThan);
outputFormatter.PrintToken(Tokens.GreaterThan);
if (prettyPrintOptions.AroundShiftOperatorParentheses) {
outputFormatter.Space();
}
break;
case BinaryOperatorType.BitwiseAnd:
if (prettyPrintOptions.AroundBitwiseOperatorParentheses) {
outputFormatter.Space();
}
outputFormatter.PrintToken(Tokens.BitwiseAnd);
if (prettyPrintOptions.AroundBitwiseOperatorParentheses) {
outputFormatter.Space();
}
break;
case BinaryOperatorType.BitwiseOr:
if (prettyPrintOptions.AroundBitwiseOperatorParentheses) {
outputFormatter.Space();
}
outputFormatter.PrintToken(Tokens.BitwiseOr);
if (prettyPrintOptions.AroundBitwiseOperatorParentheses) {
outputFormatter.Space();
}
break;
case BinaryOperatorType.ExclusiveOr:
if (prettyPrintOptions.AroundBitwiseOperatorParentheses) {
outputFormatter.Space();
}
outputFormatter.PrintToken(Tokens.Xor);
if (prettyPrintOptions.AroundBitwiseOperatorParentheses) {
outputFormatter.Space();
}
break;
case BinaryOperatorType.LogicalAnd:
if (prettyPrintOptions.AroundLogicalOperatorParentheses) {
outputFormatter.Space();
}
outputFormatter.PrintToken(Tokens.LogicalAnd);
if (prettyPrintOptions.AroundLogicalOperatorParentheses) {
outputFormatter.Space();
}
break;
case BinaryOperatorType.LogicalOr:
if (prettyPrintOptions.AroundLogicalOperatorParentheses) {
outputFormatter.Space();
}
outputFormatter.PrintToken(Tokens.LogicalOr);
if (prettyPrintOptions.AroundLogicalOperatorParentheses) {
outputFormatter.Space();
}
break;
case BinaryOperatorType.Equality:
case BinaryOperatorType.ReferenceEquality:
if (prettyPrintOp
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -