📄 csharpoutputvisitor.cs
字号:
outputFormatter.PrintToken(Tokens.For);
if (this.prettyPrintOptions.ForParentheses) {
outputFormatter.Space();
}
outputFormatter.PrintToken(Tokens.OpenParenthesis);
outputFormatter.DoIndent = false;
outputFormatter.DoNewLine = false;
outputFormatter.EmitSemicolon = false;
for (int i = 0; i < forStatement.Initializers.Count; ++i) {
INode node = (INode)forStatement.Initializers[i];
nodeTracker.TrackedVisit(node, data);
if (i + 1 < forStatement.Initializers.Count) {
outputFormatter.PrintToken(Tokens.Comma);
}
}
outputFormatter.EmitSemicolon = true;
outputFormatter.PrintToken(Tokens.Semicolon);
outputFormatter.EmitSemicolon = false;
if (!forStatement.Condition.IsNull) {
if (this.prettyPrintOptions.SpacesAfterSemicolon) {
outputFormatter.Space();
}
nodeTracker.TrackedVisit(forStatement.Condition, data);
}
outputFormatter.EmitSemicolon = true;
outputFormatter.PrintToken(Tokens.Semicolon);
outputFormatter.EmitSemicolon = false;
if (forStatement.Iterator != null && forStatement.Iterator.Count > 0) {
if (this.prettyPrintOptions.SpacesAfterSemicolon) {
outputFormatter.Space();
}
for (int i = 0; i < forStatement.Iterator.Count; ++i) {
INode node = (INode)forStatement.Iterator[i];
nodeTracker.TrackedVisit(node, data);
if (i + 1 < forStatement.Iterator.Count) {
outputFormatter.PrintToken(Tokens.Comma);
}
}
}
outputFormatter.PrintToken(Tokens.CloseParenthesis);
outputFormatter.EmitSemicolon = true;
outputFormatter.DoNewLine = true;
outputFormatter.DoIndent = true;
WriteEmbeddedStatement(forStatement.EmbeddedStatement);
return null;
}
void WriteEmbeddedStatement(Statement statement)
{
if (statement is BlockStatement) {
nodeTracker.TrackedVisit(statement, prettyPrintOptions.StatementBraceStyle);
} else {
++outputFormatter.IndentationLevel;
outputFormatter.NewLine();
nodeTracker.TrackedVisit(statement, null);
outputFormatter.NewLine();
--outputFormatter.IndentationLevel;
}
}
public object VisitLabelStatement(LabelStatement labelStatement, object data)
{
outputFormatter.PrintIdentifier(labelStatement.Label);
outputFormatter.PrintToken(Tokens.Colon);
return null;
}
public object VisitGotoStatement(GotoStatement gotoStatement, object data)
{
outputFormatter.PrintToken(Tokens.Goto);
outputFormatter.Space();
outputFormatter.PrintIdentifier(gotoStatement.Label);
outputFormatter.PrintToken(Tokens.Semicolon);
return null;
}
public object VisitSwitchStatement(SwitchStatement switchStatement, object data)
{
outputFormatter.PrintToken(Tokens.Switch);
if (this.prettyPrintOptions.SwitchParentheses) {
outputFormatter.Space();
}
outputFormatter.PrintToken(Tokens.OpenParenthesis);
nodeTracker.TrackedVisit(switchStatement.SwitchExpression, data);
outputFormatter.PrintToken(Tokens.CloseParenthesis);
outputFormatter.Space();
outputFormatter.PrintToken(Tokens.OpenCurlyBrace);
outputFormatter.NewLine();
++outputFormatter.IndentationLevel;
foreach (SwitchSection section in switchStatement.SwitchSections) {
nodeTracker.TrackedVisit(section, data);
}
--outputFormatter.IndentationLevel;
outputFormatter.Indent();
outputFormatter.PrintToken(Tokens.CloseCurlyBrace);
return null;
}
public object VisitSwitchSection(SwitchSection switchSection, object data)
{
foreach (CaseLabel label in switchSection.SwitchLabels) {
nodeTracker.TrackedVisit(label, data);
}
++outputFormatter.IndentationLevel;
foreach (Statement stmt in switchSection.Children) {
outputFormatter.Indent();
nodeTracker.TrackedVisit(stmt, data);
outputFormatter.NewLine();
}
// Check if a 'break' should be auto inserted.
if (switchSection.Children.Count == 0 ||
!(switchSection.Children[switchSection.Children.Count - 1] is BreakStatement ||
switchSection.Children[switchSection.Children.Count - 1] is ContinueStatement ||
switchSection.Children[switchSection.Children.Count - 1] is ReturnStatement)) {
outputFormatter.Indent();
outputFormatter.PrintToken(Tokens.Break);
outputFormatter.PrintToken(Tokens.Semicolon);
outputFormatter.NewLine();
}
--outputFormatter.IndentationLevel;
return null;
}
public object VisitCaseLabel(CaseLabel caseLabel, object data)
{
outputFormatter.Indent();
if (caseLabel.IsDefault) {
outputFormatter.PrintToken(Tokens.Default);
} else {
outputFormatter.PrintToken(Tokens.Case);
outputFormatter.Space();
if (caseLabel.BinaryOperatorType != BinaryOperatorType.None) {
Error(caseLabel, String.Format("Case labels with binary operators are unsupported : {0}", caseLabel.BinaryOperatorType));
}
nodeTracker.TrackedVisit(caseLabel.Label, data);
}
outputFormatter.PrintToken(Tokens.Colon);
if (!caseLabel.ToExpression.IsNull) {
PrimitiveExpression pl = caseLabel.Label as PrimitiveExpression;
PrimitiveExpression pt = caseLabel.ToExpression as PrimitiveExpression;
if (pl != null && pt != null && pl.Value is int && pt.Value is int) {
int plv = (int)pl.Value;
int prv = (int)pt.Value;
if (plv < prv && plv + 12 > prv) {
for (int i = plv + 1; i <= prv; i++) {
outputFormatter.NewLine();
outputFormatter.Indent();
outputFormatter.PrintToken(Tokens.Case);
outputFormatter.Space();
outputFormatter.PrintText(i.ToString(NumberFormatInfo.InvariantInfo));
outputFormatter.PrintToken(Tokens.Colon);
}
} else {
outputFormatter.PrintText(" // TODO: to ");
nodeTracker.TrackedVisit(caseLabel.ToExpression, data);
}
} else {
outputFormatter.PrintText(" // TODO: to ");
nodeTracker.TrackedVisit(caseLabel.ToExpression, data);
}
}
outputFormatter.NewLine();
return null;
}
public object VisitBreakStatement(BreakStatement breakStatement, object data)
{
outputFormatter.PrintToken(Tokens.Break);
outputFormatter.PrintToken(Tokens.Semicolon);
return null;
}
public object VisitStopStatement(StopStatement stopStatement, object data)
{
outputFormatter.PrintText("System.Diagnostics.Debugger.Break()");
outputFormatter.PrintToken(Tokens.Semicolon);
return null;
}
public object VisitResumeStatement(ResumeStatement resumeStatement, object data)
{
NotSupported(resumeStatement);
return null;
}
public object VisitEndStatement(EndStatement endStatement, object data)
{
outputFormatter.PrintText("System.Environment.Exit(0)");
outputFormatter.PrintToken(Tokens.Semicolon);
return null;
}
public object VisitContinueStatement(ContinueStatement continueStatement, object data)
{
outputFormatter.PrintToken(Tokens.Continue);
outputFormatter.PrintToken(Tokens.Semicolon);
return null;
}
public object VisitGotoCaseStatement(GotoCaseStatement gotoCaseStatement, object data)
{
outputFormatter.PrintToken(Tokens.Goto);
outputFormatter.Space();
if (gotoCaseStatement.IsDefaultCase) {
outputFormatter.PrintToken(Tokens.Default);
} else {
outputFormatter.PrintToken(Tokens.Case);
outputFormatter.Space();
nodeTracker.TrackedVisit(gotoCaseStatement.Expression, data);
}
outputFormatter.PrintToken(Tokens.Semicolon);
return null;
}
void PrintLoopCheck(DoLoopStatement doLoopStatement)
{
outputFormatter.PrintToken(Tokens.While);
if (this.prettyPrintOptions.WhileParentheses) {
outputFormatter.Space();
}
outputFormatter.PrintToken(Tokens.OpenParenthesis);
if (doLoopStatement.ConditionType == ConditionType.Until) {
outputFormatter.PrintToken(Tokens.Not);
outputFormatter.PrintToken(Tokens.OpenParenthesis);
}
if (doLoopStatement.Condition.IsNull) {
outputFormatter.PrintToken(Tokens.True);
} else {
nodeTracker.TrackedVisit(doLoopStatement.Condition, null);
}
if (doLoopStatement.ConditionType == ConditionType.Until) {
outputFormatter.PrintToken(Tokens.CloseParenthesis);
}
outputFormatter.PrintToken(Tokens.CloseParenthesis);
}
public object VisitDoLoopStatement(DoLoopStatement doLoopStatement, object data)
{
if (doLoopStatement.ConditionPosition == ConditionPosition.None) {
Error(doLoopStatement, String.Format("Unknown condition position for loop : {0}.", doLoopStatement));
}
if (doLoopStatement.ConditionPosition == ConditionPosition.Start) {
PrintLoopCheck(doLoopStatement);
} else {
outputFormatter.PrintToken(Tokens.Do);
}
WriteEmbeddedStatement(doLoopStatement.EmbeddedStatement);
if (doLoopStatement.ConditionPosition == ConditionPosition.End) {
outputFormatter.Indent();
PrintLoopCheck(doLoopStatement);
outputFormatter.PrintToken(Tokens.Semicolon);
outputFormatter.NewLine();
}
return null;
}
public object VisitForeachStatement(ForeachStatement foreachStatement, object data)
{
outputFormatter.PrintToken(Tokens.Foreach);
if (this.prettyPrintOptions.ForeachParentheses) {
outputFormatter.Space();
}
outputFormatter.PrintToken(Tokens.OpenParenthesis);
nodeTracker.TrackedVisit(foreachStatement.TypeReference, data);
outputFormatter.Space();
outputFormatter.PrintIdentifier(foreachStatement.VariableName);
outputFormatter.Space();
outputFormatter.PrintToken(Tokens.In);
outputFormatter.Space();
nodeTracker.TrackedVisit(foreachStatement.Expression, data);
outputFormatter.PrintToken(Tokens.CloseParenthesis);
WriteEmbeddedStatement(foreachStatement.EmbeddedStatement);
return null;
}
public object VisitLockStatement(LockStatement lockStatement, object data)
{
outputFormatter.PrintToken(Tokens.Lock);
if (this.prettyPrintOptions.LockParentheses) {
outputFormatter.Space();
}
outputFormatter.PrintToken(Tokens.OpenParenthesis);
nodeTracker.TrackedVisit(lockStatement.LockExpression, data);
outputFormatter.PrintToken(Tokens.CloseParenthesis);
WriteEmbeddedStatement(lockStatement.EmbeddedStatement);
return null;
}
public object VisitUsingStatement(UsingStatement usingStatement, object data)
{
outputFormatter.PrintToken(Tokens.Using);
if (this.prettyPrintOptions.UsingParentheses) {
outputFormatter.Space();
}
outputFormatter.PrintToken(Tokens.OpenParenthesis);
outputFormatter.DoIndent = false;
outputFormatter.DoNewLine = false;
outputFormatter.EmitSemicolon = false;
nodeTracker.TrackedVisit(usingStatement.ResourceAcquisition, data);
outputFormatter.DoIndent = true;
outputFormatter.DoNewLine = true;
outputFormatter.EmitSemicolon = true;
outputFormatter.PrintToken(Tokens.CloseParenthesis);
WriteEmbeddedStatement(usingStatement.EmbeddedStatement);
return null;
}
public object VisitWithStatement(WithStatement withStatement, object data)
{
NotSupported(withStatement);
return null;
}
public object VisitTryCatchStatement(TryCatchStatement tryCatchStatement, object data)
{
outputFormatter.PrintToken(Tokens.Try);
WriteEmbeddedStatement(tryCatchStatement.StatementBlock);
foreach (CatchClause catchClause in tryCatchStatement.CatchClauses) {
nodeTracker.TrackedVisit(catchClause, data);
}
if (!tryCatchStatement.FinallyBlock.IsNull) {
outputFormatter.Indent();
outputFormatter.PrintToken(Tokens.Finally);
WriteEmbeddedStatement(tryCatchStatement.FinallyBlock);
}
return null;
}
public object VisitCatchClause(CatchClause catchClause, object data)
{
outputFormatter.Indent();
outputFormatter.PrintToken(Tokens.Catch);
if (!catchClause.TypeReference.IsNull) {
if (this.prettyPrintOptions.CatchParentheses) {
outputFormatter.Space();
}
outputFormatter.PrintToken(Tokens.OpenParenthesis);
outputFormatter.PrintIdentifier(catchClause.TypeReference.Type);
if (catchClause.VariableName.Length > 0) {
outputFormatter.Space();
outputFormatter.PrintIdentifier(catchClause.VariableName);
}
outputFormatter.PrintToken(Tokens.CloseParenthesis);
}
WriteEmbeddedStatement(catchClause.StatementBlock);
return null;
}
public object VisitThrowStatement(ThrowStatement throwStatement, object data)
{
outputFormatter.PrintToken(Tokens.Throw);
if (!throwStatement.Expression.IsNull) {
outputFormatter.Space();
nodeTracker.TrackedVisit(throwStatement.Expression, data);
}
outputFormatter.PrintToken(Tokens.Semicolon);
return null;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -