📄 prettyprintvisitor.cs
字号:
// PrettyPrintVisitor.cs
// Copyright (C) 2003 Mike Krueger (mike@icsharpcode.net)
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
using System;
using System.Text;
using System.Collections;
using System.Diagnostics;
using ICSharpCode.SharpRefactory.Parser;
using ICSharpCode.SharpRefactory.Parser.AST;
namespace ICSharpCode.SharpRefactory.PrettyPrinter
{
public class PrettyPrintVisitor : AbstractASTVisitor
{
Errors errors = new Errors();
OutputFormatter outputFormatter;
PrettyPrintOptions prettyPrintOptions = new PrettyPrintOptions();
public string Text {
get {
return outputFormatter.Text;
}
}
public Errors Errors {
get {
return errors;
}
}
public PrettyPrintOptions PrettyPrintOptions {
get {
return prettyPrintOptions;
}
}
public PrettyPrintVisitor(string originalSourceFile)
{
outputFormatter = new OutputFormatter(originalSourceFile, prettyPrintOptions);
}
public override object Visit(INode node, object data)
{
errors.Error(-1, -1, String.Format("Visited INode (should NEVER HAPPEN)"));
Console.WriteLine("Visitor was: " + this.GetType());
Console.WriteLine("Node was : " + node.GetType());
return node.AcceptChildren(this, data);
}
public override object Visit(AttributeSection section, object data)
{
outputFormatter.Indent();
outputFormatter.PrintToken(Tokens.OpenSquareBracket);
if (section.AttributeTarget != null && section.AttributeTarget != String.Empty) {
outputFormatter.PrintIdentifier(section.AttributeTarget);
outputFormatter.PrintToken(Tokens.Colon);
outputFormatter.Space();
}
Debug.Assert(section.Attributes != null);
for (int j = 0; j < section.Attributes.Count; ++j) {
ICSharpCode.SharpRefactory.Parser.AST.Attribute a = (ICSharpCode.SharpRefactory.Parser.AST.Attribute)section.Attributes[j];
outputFormatter.PrintIdentifier(a.Name);
outputFormatter.PrintToken(Tokens.OpenParenthesis);
this.AppendCommaSeparatedList(a.PositionalArguments);
if (a.NamedArguments != null && a.NamedArguments.Count > 0) {
if (a.PositionalArguments.Count > 0) {
outputFormatter.PrintToken(Tokens.Comma);
outputFormatter.Space();
}
for (int i = 0; i < a.NamedArguments.Count; ++i) {
NamedArgument n = (NamedArgument)a.NamedArguments[i];
outputFormatter.PrintIdentifier(n.Name);
outputFormatter.Space();
outputFormatter.PrintToken(Tokens.Assign);
outputFormatter.Space();
n.Expr.AcceptVisitor(this, data);
if (i + 1 < a.NamedArguments.Count) {
outputFormatter.PrintToken(Tokens.Comma);
outputFormatter.Space();
}
}
}
outputFormatter.PrintToken(Tokens.CloseParenthesis);
if (j + 1 < section.Attributes.Count) {
outputFormatter.PrintToken(Tokens.Comma);
outputFormatter.Space();
}
}
outputFormatter.PrintToken(Tokens.CloseSquareBracket);
outputFormatter.NewLine();
return null;
}
public override object Visit(CompilationUnit compilationUnit, object data)
{
compilationUnit.AcceptChildren(this, data);
outputFormatter.EndFile();
return null;
}
public override object Visit(UsingDeclaration usingDeclaration, object data)
{
outputFormatter.Indent();
outputFormatter.PrintToken(Tokens.Using);
outputFormatter.Space();
outputFormatter.PrintIdentifier(usingDeclaration.Namespace);
outputFormatter.PrintToken(Tokens.Semicolon);
outputFormatter.NewLine();
return null;
}
public override object Visit(UsingAliasDeclaration usingAliasDeclaration, object data)
{
outputFormatter.Indent();
outputFormatter.PrintToken(Tokens.Using);
outputFormatter.Space();
outputFormatter.PrintIdentifier(usingAliasDeclaration.Alias);
outputFormatter.Space();
outputFormatter.PrintToken(Tokens.Assign);
outputFormatter.Space();
outputFormatter.PrintIdentifier(usingAliasDeclaration.Namespace);
outputFormatter.PrintToken(Tokens.Semicolon);
outputFormatter.NewLine();
return null;
}
public override object Visit(NamespaceDeclaration namespaceDeclaration, object data)
{
outputFormatter.Indent();
outputFormatter.PrintToken(Tokens.Namespace);
outputFormatter.Space();
outputFormatter.PrintIdentifier(namespaceDeclaration.NameSpace);
outputFormatter.BeginBrace(this.prettyPrintOptions.NameSpaceBraceStyle);
namespaceDeclaration.AcceptChildren(this, data);
outputFormatter.EndBrace();
return null;
}
object VisitModifier(Modifier modifier)
{
ArrayList tokenList = new ArrayList();
if ((modifier & Modifier.Unsafe) != 0) {
tokenList.Add(Tokens.Unsafe);
}
if ((modifier & Modifier.Public) != 0) {
tokenList.Add(Tokens.Public);
}
if ((modifier & Modifier.Private) != 0) {
tokenList.Add(Tokens.Private);
}
if ((modifier & Modifier.Protected) != 0) {
tokenList.Add(Tokens.Protected);
}
if ((modifier & Modifier.Static) != 0) {
tokenList.Add(Tokens.Static);
}
if ((modifier & Modifier.Internal) != 0) {
tokenList.Add(Tokens.Internal);
}
if ((modifier & Modifier.Override) != 0) {
tokenList.Add(Tokens.Override);
}
if ((modifier & Modifier.Abstract) != 0) {
tokenList.Add(Tokens.Abstract);
}
if ((modifier & Modifier.Virtual) != 0) {
tokenList.Add(Tokens.Virtual);
}
if ((modifier & Modifier.New) != 0) {
tokenList.Add(Tokens.New);
}
if ((modifier & Modifier.Sealed) != 0) {
tokenList.Add(Tokens.Sealed);
}
if ((modifier & Modifier.Extern) != 0) {
tokenList.Add(Tokens.Extern);
}
if ((modifier & Modifier.Const) != 0) {
tokenList.Add(Tokens.Const);
}
if ((modifier & Modifier.Readonly) != 0) {
tokenList.Add(Tokens.Readonly);
}
if ((modifier & Modifier.Volatile) != 0) {
tokenList.Add(Tokens.Volatile);
}
outputFormatter.PrintTokenList(tokenList);
return null;
}
object VisitParamModifiers(ParamModifiers modifier)
{
switch (modifier) {
case ParamModifiers.Out:
outputFormatter.PrintToken(Tokens.Out);
break;
case ParamModifiers.Params:
outputFormatter.PrintToken(Tokens.Params);
break;
case ParamModifiers.Ref:
outputFormatter.PrintToken(Tokens.Ref);
break;
}
outputFormatter.Space();
return null;
}
object VisitAttributes(ArrayList attributes, object data)
{
if (attributes == null || attributes.Count <= 0) {
return null;
}
foreach (AttributeSection section in attributes) {
Visit(section, data);
}
return null;
}
object Visit(TypeReference type, object data)
{
outputFormatter.PrintIdentifier(type.Type);
for (int i = 0; i < type.PointerNestingLevel; ++i) {
outputFormatter.PrintToken(Tokens.Times);
}
if (type.IsArrayType) {
for (int i = 0; i < type.RankSpecifier.Length; ++i) {
outputFormatter.PrintToken(Tokens.OpenSquareBracket);
for (int j = 1; j < type.RankSpecifier[i]; ++j) {
outputFormatter.PrintToken(Tokens.Comma);
}
outputFormatter.PrintToken(Tokens.CloseSquareBracket);
}
}
return null;
}
object VisitEnumMembers(TypeDeclaration typeDeclaration, object data)
{
foreach (FieldDeclaration fieldDeclaration in typeDeclaration.Children) {
VariableDeclaration f = (VariableDeclaration)fieldDeclaration.Fields[0];
VisitAttributes(fieldDeclaration.Attributes, data);
outputFormatter.Indent();
outputFormatter.PrintIdentifier(f.Name);
if (f.Initializer != null) {
outputFormatter.Space();
outputFormatter.PrintToken(Tokens.Assign);
outputFormatter.Space();
f.Initializer.AcceptVisitor(this, data);
}
outputFormatter.PrintToken(Tokens.Comma);
outputFormatter.NewLine();
}
return null;
}
public override object Visit(TypeDeclaration typeDeclaration, object data)
{
VisitAttributes(typeDeclaration.Attributes, data);
outputFormatter.Indent();
VisitModifier(typeDeclaration.Modifier);
switch (typeDeclaration.Type) {
case Types.Class:
outputFormatter.PrintToken(Tokens.Class);
break;
case Types.Enum:
outputFormatter.PrintToken(Tokens.Enum);
break;
case Types.Interface:
outputFormatter.PrintToken(Tokens.Interface);
break;
case Types.Struct:
outputFormatter.PrintToken(Tokens.Struct);
break;
}
outputFormatter.Space();
outputFormatter.PrintIdentifier(typeDeclaration.Name);
if (typeDeclaration.BaseTypes != null && typeDeclaration.BaseTypes.Count > 0) {
outputFormatter.Space();
outputFormatter.PrintToken(Tokens.Colon);
for (int i = 0; i < typeDeclaration.BaseTypes.Count; ++i) {
outputFormatter.Space();
outputFormatter.PrintIdentifier(typeDeclaration.BaseTypes[i]);
if (i + 1 < typeDeclaration.BaseTypes.Count) {
outputFormatter.PrintToken(Tokens.Comma);
outputFormatter.Space();
}
}
}
switch (typeDeclaration.Type) {
case Types.Class:
outputFormatter.BeginBrace(this.prettyPrintOptions.ClassBraceStyle);
break;
case Types.Enum:
outputFormatter.BeginBrace(this.prettyPrintOptions.EnumBraceStyle);
break;
case Types.Interface:
outputFormatter.BeginBrace(this.prettyPrintOptions.InterfaceBraceStyle);
break;
case Types.Struct:
outputFormatter.BeginBrace(this.prettyPrintOptions.StructBraceStyle);
break;
}
if (typeDeclaration.Type == Types.Enum) {
VisitEnumMembers(typeDeclaration, data);
} else {
typeDeclaration.AcceptChildren(this, data);
}
outputFormatter.EndBrace();
return null;
}
public override object Visit(ParameterDeclarationExpression parameterDeclarationExpression, object data)
{
VisitAttributes(parameterDeclarationExpression.Attributes, data);
VisitParamModifiers(parameterDeclarationExpression.ParamModifiers);
Visit(parameterDeclarationExpression.TypeReference, data);
outputFormatter.Space();
outputFormatter.PrintIdentifier(parameterDeclarationExpression.ParameterName);
return null;
}
public override object Visit(DelegateDeclaration delegateDeclaration, object data)
{
VisitAttributes(delegateDeclaration.Attributes, data);
VisitModifier(delegateDeclaration.Modifier);
outputFormatter.PrintToken(Tokens.Delegate);
outputFormatter.Space();
Visit(delegateDeclaration.ReturnType, data);
outputFormatter.Space();
outputFormatter.PrintIdentifier(delegateDeclaration.Name);
outputFormatter.PrintToken(Tokens.OpenParenthesis);
AppendCommaSeparatedList(delegateDeclaration.Parameters);
outputFormatter.PrintToken(Tokens.CloseParenthesis);
outputFormatter.PrintToken(Tokens.Semicolon);
outputFormatter.NewLine();
return null;
}
public override object Visit(VariableDeclaration variableDeclaration, object data)
{
outputFormatter.PrintIdentifier(variableDeclaration.Name);
if (variableDeclaration.Initializer != null) {
outputFormatter.Space();
outputFormatter.PrintToken(Tokens.Assign);
outputFormatter.Space();
variableDeclaration.Initializer.AcceptVisitor(this, data);
}
return null;
}
public void AppendCommaSeparatedList(IList list)
{
if (list != null) {
for (int i = 0; i < list.Count; ++i) {
((INode)list[i]).AcceptVisitor(this, null);
if (i + 1 < list.Count) {
outputFormatter.PrintToken(Tokens.Comma);
outputFormatter.Space();
}
if ((i + 1) % 10 == 0) {
outputFormatter.NewLine();
outputFormatter.Indent();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -