Browse Source

implement Attribute conversion

newNRvisualizers
Siegfried Pammer 14 years ago
parent
commit
b9dc346330
  1. 78
      ICSharpCode.NRefactory.VB/OutputVisitor/OutputVisitor.cs
  2. 58
      ICSharpCode.NRefactory.VB/Visitors/CSharpToVBConverterVisitor.cs

78
ICSharpCode.NRefactory.VB/OutputVisitor/OutputVisitor.cs

@ -6,7 +6,6 @@ using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using ICSharpCode.NRefactory.PatternMatching; using ICSharpCode.NRefactory.PatternMatching;
using ICSharpCode.NRefactory.VB.Ast; using ICSharpCode.NRefactory.VB.Ast;
@ -95,12 +94,28 @@ namespace ICSharpCode.NRefactory.VB
public object VisitAttribute(ICSharpCode.NRefactory.VB.Ast.Attribute attribute, object data) public object VisitAttribute(ICSharpCode.NRefactory.VB.Ast.Attribute attribute, object data)
{ {
throw new NotImplementedException(); StartNode(attribute);
if (!attribute.Target.IsNull) {
attribute.Target.AcceptVisitor(this, data);
WriteToken(":", VB.Ast.Attribute.TargetRole);
}
attribute.Type.AcceptVisitor(this, data);
WriteCommaSeparatedListInParenthesis(attribute.Arguments, false);
return EndNode(attribute);
} }
public object VisitAttributeBlock(AttributeBlock attributeBlock, object data) public object VisitAttributeBlock(AttributeBlock attributeBlock, object data)
{ {
throw new NotImplementedException(); StartNode(attributeBlock);
WriteToken("<", AttributeBlock.Roles.LChevron);
WriteCommaSeparatedList(attributeBlock.Attributes);
WriteToken(">", AttributeBlock.Roles.RChevron);
NewLine();
return EndNode(attributeBlock);
} }
public object VisitImportsStatement(ImportsStatement importsStatement, object data) public object VisitImportsStatement(ImportsStatement importsStatement, object data)
@ -136,6 +151,17 @@ namespace ICSharpCode.NRefactory.VB
node.AcceptVisitor(this, null); node.AcceptVisitor(this, null);
} }
NewLine(); NewLine();
Indent();
isFirst = true;
foreach (var member in namespaceDeclaration.Members) {
if (isFirst) {
isFirst = false;
} else {
NewLine();
}
member.AcceptVisitor(this, data);
}
Unindent();
WriteKeyword("End"); WriteKeyword("End");
WriteKeyword("Namespace"); WriteKeyword("Namespace");
NewLine(); NewLine();
@ -149,7 +175,40 @@ namespace ICSharpCode.NRefactory.VB
public object VisitTypeDeclaration(TypeDeclaration typeDeclaration, object data) public object VisitTypeDeclaration(TypeDeclaration typeDeclaration, object data)
{ {
throw new NotImplementedException(); StartNode(typeDeclaration);
WriteAttributes(typeDeclaration.Attributes);
WriteModifiers(typeDeclaration.ModifierTokens);
WriteClassTypeKeyword(typeDeclaration);
WriteIdentifier(typeDeclaration.Name.Name);
NewLine();
WriteKeyword("End");
WriteClassTypeKeyword(typeDeclaration);
NewLine();
return EndNode(typeDeclaration);
}
void WriteClassTypeKeyword(TypeDeclaration typeDeclaration)
{
switch (typeDeclaration.ClassType) {
case ICSharpCode.NRefactory.TypeSystem.ClassType.Class:
WriteKeyword("Class");
break;
case ICSharpCode.NRefactory.TypeSystem.ClassType.Enum:
break;
case ICSharpCode.NRefactory.TypeSystem.ClassType.Interface:
break;
case ICSharpCode.NRefactory.TypeSystem.ClassType.Struct:
WriteKeyword("Structure");
break;
case ICSharpCode.NRefactory.TypeSystem.ClassType.Delegate:
break;
case ICSharpCode.NRefactory.TypeSystem.ClassType.Module:
WriteKeyword("Module");
break;
default:
throw new Exception("Invalid value for ClassType");
}
} }
public object VisitXmlNamespaceImportsClause(XmlNamespaceImportsClause xmlNamespaceImportsClause, object data) public object VisitXmlNamespaceImportsClause(XmlNamespaceImportsClause xmlNamespaceImportsClause, object data)
@ -362,7 +421,6 @@ namespace ICSharpCode.NRefactory.VB
void Comma(AstNode nextNode, bool noSpaceAfterComma = false) void Comma(AstNode nextNode, bool noSpaceAfterComma = false)
{ {
WriteSpecialsUpToRole(AstNode.Roles.Comma, nextNode); WriteSpecialsUpToRole(AstNode.Roles.Comma, nextNode);
Space(); // TODO: Comma policy has changed.
formatter.WriteToken(","); formatter.WriteToken(",");
lastWritten = LastWritten.Other; lastWritten = LastWritten.Other;
Space(!noSpaceAfterComma); // TODO: Comma policy has changed. Space(!noSpaceAfterComma); // TODO: Comma policy has changed.
@ -577,6 +635,16 @@ namespace ICSharpCode.NRefactory.VB
formatter.NewLine(); formatter.NewLine();
lastWritten = LastWritten.Whitespace; lastWritten = LastWritten.Whitespace;
} }
void Indent()
{
formatter.Indent();
}
void Unindent()
{
formatter.Unindent();
}
#endregion #endregion
#region IsKeyword Test #region IsKeyword Test

58
ICSharpCode.NRefactory.VB/Visitors/CSharpToVBConverterVisitor.cs

@ -3,15 +3,31 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using ICSharpCode.NRefactory.TypeSystem;
using ICSharpCode.NRefactory.VB.Ast; using ICSharpCode.NRefactory.VB.Ast;
namespace ICSharpCode.NRefactory.VB.Visitors namespace ICSharpCode.NRefactory.VB.Visitors
{ {
public interface IEnvironmentProvider
{
string RootNamespace { get; }
string GetTypeNameForAttribute(CSharp.Attribute attribute);
}
/// <summary> /// <summary>
/// Description of CSharpToVBConverterVisitor. /// Description of CSharpToVBConverterVisitor.
/// </summary> /// </summary>
public class CSharpToVBConverterVisitor : CSharp.IAstVisitor<object, VB.AstNode> public class CSharpToVBConverterVisitor : CSharp.IAstVisitor<object, VB.AstNode>
{ {
IEnvironmentProvider provider;
public CSharpToVBConverterVisitor(IEnvironmentProvider provider)
{
this.provider = provider;
}
public AstNode VisitAnonymousMethodExpression(CSharp.AnonymousMethodExpression anonymousMethodExpression, object data) public AstNode VisitAnonymousMethodExpression(CSharp.AnonymousMethodExpression anonymousMethodExpression, object data)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
@ -234,12 +250,21 @@ namespace ICSharpCode.NRefactory.VB.Visitors
public AstNode VisitAttribute(CSharp.Attribute attribute, object data) public AstNode VisitAttribute(CSharp.Attribute attribute, object data)
{ {
throw new NotImplementedException(); var attr = new VB.Ast.Attribute();
// TODO : attribute targets
attr.Type = (AstType)attribute.Type.AcceptVisitor(this, data);
// ConvertNodes(attribute.Arguments, attr.Arguments);
return EndNode(attribute, attr);
} }
public AstNode VisitAttributeSection(CSharp.AttributeSection attributeSection, object data) public AstNode VisitAttributeSection(CSharp.AttributeSection attributeSection, object data)
{ {
throw new NotImplementedException(); AttributeBlock block = new AttributeBlock();
ConvertNodes(attributeSection.Attributes, block.Attributes);
return EndNode(attributeSection, block);
} }
public AstNode VisitDelegateDeclaration(CSharp.DelegateDeclaration delegateDeclaration, object data) public AstNode VisitDelegateDeclaration(CSharp.DelegateDeclaration delegateDeclaration, object data)
@ -261,6 +286,23 @@ namespace ICSharpCode.NRefactory.VB.Visitors
{ {
var type = new TypeDeclaration(); var type = new TypeDeclaration();
CSharp.Attribute stdModAttr;
if (typeDeclaration.ClassType == ClassType.Class && HasAttribute(typeDeclaration.Attributes, "Microsoft.VisualBasic.CompilerServices.StandardModuleAttribute", out stdModAttr)) {
type.ClassType = ClassType.Module;
// remove AttributeSection if only one attribute is present
var attrSec = (CSharp.AttributeSection)stdModAttr.Parent;
if (attrSec.Attributes.Count == 1)
attrSec.Remove();
else
stdModAttr.Remove();
} else
type.ClassType = typeDeclaration.ClassType;
ConvertNodes(typeDeclaration.Attributes, type.Attributes);
type.Name = new Identifier(typeDeclaration.Name, AstLocation.Empty);
return EndNode(typeDeclaration, type); return EndNode(typeDeclaration, type);
} }
@ -620,5 +662,17 @@ namespace ICSharpCode.NRefactory.VB.Visitors
{ {
return result; return result;
} }
bool HasAttribute(CSharp.AstNodeCollection<CSharp.AttributeSection> attributes, string name, out CSharp.Attribute foundAttribute)
{
foreach (var attr in attributes.SelectMany(a => a.Attributes)) {
if (provider.GetTypeNameForAttribute(attr) == name) {
foundAttribute = attr;
return true;
}
}
foundAttribute = null;
return false;
}
} }
} }

Loading…
Cancel
Save