vbnetoutputvisitor.cs
来自「SharpDevelop2.0.0 c#开发免费工具」· CS 代码 · 共 2,086 行 · 第 1/5 页
CS
2,086 行
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="none" email=""/>
// <version>$Revision: 1434 $</version>
// </file>
using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using ICSharpCode.NRefactory.Parser;
using ICSharpCode.NRefactory.Parser.VB;
using ICSharpCode.NRefactory.Parser.AST;
namespace ICSharpCode.NRefactory.PrettyPrinter
{
public class VBNetOutputVisitor : IOutputASTVisitor
{
Errors errors = new Errors();
VBNetOutputFormatter outputFormatter;
VBNetPrettyPrintOptions prettyPrintOptions = new VBNetPrettyPrintOptions();
NodeTracker nodeTracker;
TypeDeclaration currentType;
Stack<int> exitTokenStack = new Stack<int>();
public string Text {
get {
return outputFormatter.Text;
}
}
public Errors Errors {
get {
return errors;
}
}
public object Options {
get {
return prettyPrintOptions;
}
set {
prettyPrintOptions = value as VBNetPrettyPrintOptions;
}
}
public NodeTracker NodeTracker {
get {
return nodeTracker;
}
}
public IOutputFormatter OutputFormatter {
get {
return outputFormatter;
}
}
public VBNetOutputVisitor()
{
outputFormatter = new VBNetOutputFormatter(prettyPrintOptions);
nodeTracker = new NodeTracker(this);
}
#region ICSharpCode.NRefactory.Parser.IASTVisitor interface implementation
public object Visit(INode node, object data)
{
errors.Error(-1, -1, String.Format("Visited INode (should NEVER HAPPEN), node is : {0}", node.ToString()));
return node.AcceptChildren(this, data);
}
public object Visit(CompilationUnit compilationUnit, object data)
{
nodeTracker.TrackedVisitChildren(compilationUnit, data);
outputFormatter.EndFile();
return null;
}
string ConvertTypeString(string typeString)
{
switch (typeString) {
case "System.Boolean":
return "Boolean";
case "System.String":
return "String";
case "System.Char":
return "Char";
case "System.Double":
return "Double";
case "System.Single":
return "Single";
case "System.Decimal":
return "Decimal";
case "System.DateTime":
return "Date";
case "System.Int64":
return "Long";
case "System.Int32":
return "Integer";
case "System.Int16":
return "Short";
case "System.Byte":
return "Byte";
case "System.Void":
return "Void";
case "System.Object":
return "Object";
case "System.UInt64":
return "ULong";
case "System.UInt32":
return "UInt";
case "System.UInt16":
return "UShort";
case "System.SByte":
return "SByte";
}
return null;
}
public object Visit(TypeReference typeReference, object data)
{
PrintTypeReferenceWithoutArray(typeReference);
if (typeReference.IsArrayType) {
PrintArrayRank(typeReference.RankSpecifier, 0);
}
return null;
}
void PrintTypeReferenceWithoutArray(TypeReference typeReference)
{
if (typeReference.IsGlobal) {
outputFormatter.PrintToken(Tokens.Global);
outputFormatter.PrintToken(Tokens.Dot);
}
if (typeReference.Type == null || typeReference.Type.Length ==0) {
outputFormatter.PrintText("Void");
} else {
string shortTypeName = ConvertTypeString(typeReference.SystemType);
if (shortTypeName != null) {
outputFormatter.PrintText(shortTypeName);
} else {
outputFormatter.PrintIdentifier(typeReference.Type);
}
}
if (typeReference.GenericTypes != null && typeReference.GenericTypes.Count > 0) {
outputFormatter.PrintToken(Tokens.OpenParenthesis);
outputFormatter.PrintToken(Tokens.Of);
outputFormatter.Space();
AppendCommaSeparatedList(typeReference.GenericTypes);
outputFormatter.PrintToken(Tokens.CloseParenthesis);
}
for (int i = 0; i < typeReference.PointerNestingLevel; ++i) {
outputFormatter.PrintToken(Tokens.Times);
}
}
void PrintArrayRank(int[] rankSpecifier, int startRank)
{
for (int i = startRank; i < rankSpecifier.Length; ++i) {
outputFormatter.PrintToken(Tokens.OpenParenthesis);
for (int j = 0; j < rankSpecifier[i]; ++j) {
outputFormatter.PrintToken(Tokens.Comma);
}
outputFormatter.PrintToken(Tokens.CloseParenthesis);
}
}
public object Visit(InnerClassTypeReference typeReference, object data)
{
nodeTracker.TrackedVisit(typeReference.BaseType, data);
outputFormatter.PrintToken(Tokens.Dot);
return Visit((TypeReference)typeReference, data); // call Visit(TypeReference, object)
}
#region Global scope
public object Visit(AttributeSection attributeSection, object data)
{
outputFormatter.Indent();
outputFormatter.PrintText("<");
if (attributeSection.AttributeTarget != null && attributeSection.AttributeTarget != String.Empty) {
outputFormatter.PrintIdentifier(attributeSection.AttributeTarget);
outputFormatter.PrintToken(Tokens.Colon);
outputFormatter.Space();
}
Debug.Assert(attributeSection.Attributes != null);
AppendCommaSeparatedList(attributeSection.Attributes);
if ("assembly".Equals(attributeSection.AttributeTarget, StringComparison.InvariantCultureIgnoreCase)
|| "module".Equals(attributeSection.AttributeTarget, StringComparison.InvariantCultureIgnoreCase)) {
outputFormatter.PrintText(">");
} else {
outputFormatter.PrintText("> _");
}
outputFormatter.NewLine();
return null;
}
public object Visit(ICSharpCode.NRefactory.Parser.AST.Attribute attribute, object data)
{
outputFormatter.PrintIdentifier(attribute.Name);
outputFormatter.PrintToken(Tokens.OpenParenthesis);
AppendCommaSeparatedList(attribute.PositionalArguments);
if (attribute.NamedArguments != null && attribute.NamedArguments.Count > 0) {
if (attribute.PositionalArguments.Count > 0) {
outputFormatter.PrintToken(Tokens.Comma);
outputFormatter.Space();
}
AppendCommaSeparatedList(attribute.NamedArguments);
}
outputFormatter.PrintToken(Tokens.CloseParenthesis);
return null;
}
public object Visit(NamedArgumentExpression namedArgumentExpression, object data)
{
outputFormatter.PrintIdentifier(namedArgumentExpression.Name);
outputFormatter.Space();
outputFormatter.PrintToken(Tokens.Colon);
outputFormatter.PrintToken(Tokens.Assign);
outputFormatter.Space();
nodeTracker.TrackedVisit(namedArgumentExpression.Expression, data);
return null;
}
public object Visit(Using u, object data)
{
Debug.Fail("Should never be called. The usings should be handled in Visit(UsingDeclaration)");
return null;
}
public object Visit(UsingDeclaration usingDeclaration, object data)
{
outputFormatter.Indent();
outputFormatter.PrintToken(Tokens.Imports);
outputFormatter.Space();
for (int i = 0; i < usingDeclaration.Usings.Count; ++i) {
outputFormatter.PrintIdentifier(((Using)usingDeclaration.Usings[i]).Name);
if (((Using)usingDeclaration.Usings[i]).IsAlias) {
outputFormatter.Space();
outputFormatter.PrintToken(Tokens.Assign);
outputFormatter.Space();
nodeTracker.TrackedVisit(((Using)usingDeclaration.Usings[i]).Alias, data);
}
if (i + 1 < usingDeclaration.Usings.Count) {
outputFormatter.PrintToken(Tokens.Comma);
outputFormatter.Space();
}
}
outputFormatter.NewLine();
return null;
}
public object Visit(NamespaceDeclaration namespaceDeclaration, object data)
{
outputFormatter.Indent();
outputFormatter.PrintToken(Tokens.Namespace);
outputFormatter.Space();
outputFormatter.PrintIdentifier(namespaceDeclaration.Name);
outputFormatter.NewLine();
++outputFormatter.IndentationLevel;
nodeTracker.TrackedVisitChildren(namespaceDeclaration, data);
--outputFormatter.IndentationLevel;
outputFormatter.Indent();
outputFormatter.PrintToken(Tokens.End);
outputFormatter.Space();
outputFormatter.PrintToken(Tokens.Namespace);
outputFormatter.NewLine();
return null;
}
int GetTypeToken(TypeDeclaration typeDeclaration)
{
switch (typeDeclaration.Type) {
case ClassType.Class:
return Tokens.Class;
case ClassType.Enum:
return Tokens.Enum;
case ClassType.Interface:
return Tokens.Interface;
case ClassType.Struct:
return Tokens.Structure;
default:
return Tokens.Class;
}
}
void PrintTemplates(List<TemplateDefinition> templates)
{
if (templates != null && templates.Count > 0) {
outputFormatter.PrintToken(Tokens.OpenParenthesis);
outputFormatter.PrintToken(Tokens.Of);
outputFormatter.Space();
AppendCommaSeparatedList(templates);
outputFormatter.PrintToken(Tokens.CloseParenthesis);
}
}
public object Visit(TypeDeclaration typeDeclaration, object data)
{
VisitAttributes(typeDeclaration.Attributes, data);
outputFormatter.Indent();
OutputModifier(typeDeclaration.Modifier);
int typeToken = GetTypeToken(typeDeclaration);
outputFormatter.PrintToken(typeToken);
outputFormatter.Space();
outputFormatter.PrintIdentifier(typeDeclaration.Name);
PrintTemplates(typeDeclaration.Templates);
if (typeDeclaration.Type == ClassType.Enum
&& typeDeclaration.BaseTypes != null && typeDeclaration.BaseTypes.Count > 0)
{
outputFormatter.Space();
outputFormatter.PrintToken(Tokens.As);
outputFormatter.Space();
foreach (TypeReference baseTypeRef in typeDeclaration.BaseTypes) {
nodeTracker.TrackedVisit(baseTypeRef, data);
}
}
outputFormatter.NewLine();
if (typeDeclaration.BaseTypes != null && typeDeclaration.Type != ClassType.Enum) {
foreach (TypeReference baseTypeRef in typeDeclaration.BaseTypes) {
outputFormatter.Indent();
string baseType = baseTypeRef.Type;
bool baseTypeIsInterface = baseType.StartsWith("I") && (baseType.Length <= 1 || Char.IsUpper(baseType[1]));
if (!baseTypeIsInterface || typeDeclaration.Type == ClassType.Interface) {
outputFormatter.PrintToken(Tokens.Inherits);
} else {
outputFormatter.PrintToken(Tokens.Implements);
}
outputFormatter.Space();
nodeTracker.TrackedVisit(baseTypeRef, data);
outputFormatter.NewLine();
}
}
++outputFormatter.IndentationLevel;
TypeDeclaration oldType = currentType;
currentType = typeDeclaration;
if (typeDeclaration.Type == ClassType.Enum) {
OutputEnumMembers(typeDeclaration, data);
} else {
nodeTracker.TrackedVisitChildren(typeDeclaration, data);
}
currentType = oldType;
--outputFormatter.IndentationLevel;
outputFormatter.Indent();
outputFormatter.PrintToken(Tokens.End);
outputFormatter.Space();
outputFormatter.PrintToken(typeToken);
outputFormatter.NewLine();
return null;
}
void OutputEnumMembers(TypeDeclaration typeDeclaration, object data)
{
foreach (FieldDeclaration fieldDeclaration in typeDeclaration.Children) {
nodeTracker.BeginNode(fieldDeclaration);
VariableDeclaration f = (VariableDeclaration)fieldDeclaration.Fields[0];
VisitAttributes(fieldDeclaration.Attributes, data);
outputFormatter.Indent();
outputFormatter.PrintIdentifier(f.Name);
if (f.Initializer != null && !f.Initializer.IsNull) {
outputFormatter.Space();
outputFormatter.PrintToken(Tokens.Assign);
outputFormatter.Space();
nodeTracker.TrackedVisit(f.Initializer, data);
}
outputFormatter.NewLine();
nodeTracker.EndNode(fieldDeclaration);
}
}
public object Visit(TemplateDefinition templateDefinition, object data)
{
outputFormatter.PrintIdentifier(templateDefinition.Name);
if (templateDefinition.Bases.Count > 0) {
outputFormatter.PrintText(" As ");
if (templateDefinition.Bases.Count == 1) {
nodeTracker.TrackedVisit(templateDefinition.Bases[0], data);
} else {
outputFormatter.PrintToken(Tokens.OpenCurlyBrace);
AppendCommaSeparatedList(templateDefinition.Bases);
outputFormatter.PrintToken(Tokens.CloseCurlyBrace);
}
}
return null;
}
public object Visit(DelegateDeclaration delegateDeclaration, object data)
{
VisitAttributes(delegateDeclaration.Attributes, data);
OutputModifier(delegateDeclaration.Modifier);
outputFormatter.PrintToken(Tokens.Delegate);
outputFormatter.Space();
bool isFunction = (delegateDeclaration.ReturnType.Type != "void");
if (isFunction) {
outputFormatter.PrintToken(Tokens.Function);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?