Browse Source

Add SnippetParser class.

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/2.1@2517 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Daniel Grunwald 18 years ago
parent
commit
2586fed6ff
  1. 1
      src/Libraries/NRefactory/Project/NRefactory.csproj
  2. 11
      src/Libraries/NRefactory/Project/Src/Parser/AbstractParser.cs
  3. 35
      src/Libraries/NRefactory/Project/Src/Parser/CSharp/CSharpParser.cs
  4. 1844
      src/Libraries/NRefactory/Project/Src/Parser/CSharp/Parser.cs
  5. 4
      src/Libraries/NRefactory/Project/Src/Parser/CSharp/cs.ATG
  6. 3
      src/Libraries/NRefactory/Project/Src/Parser/IParser.cs
  7. 112
      src/Libraries/NRefactory/Project/Src/Parser/VBNet/Parser.cs
  8. 4
      src/Libraries/NRefactory/Project/Src/Parser/VBNet/VBNET.ATG
  9. 22
      src/Libraries/NRefactory/Project/Src/Parser/VBNet/VBNetParser.cs
  10. 129
      src/Libraries/NRefactory/Project/Src/SnippetParser.cs
  11. 1
      src/Libraries/NRefactory/Project/Src/Visitors/CSharpToVBNetConvertVisitor.cs
  12. 1
      src/Libraries/NRefactory/Project/Src/Visitors/VBNetToCSharpConvertVisitor.cs
  13. 6
      src/Libraries/NRefactory/Test/General/UnitTest.cs
  14. 1
      src/Libraries/NRefactory/Test/NRefactoryTests.csproj
  15. 3
      src/Libraries/NRefactory/Test/Output/CSharp/VBToCSharpConverterTest.cs
  16. 107
      src/Libraries/NRefactory/Test/Output/SnippetConversion.cs
  17. 3
      src/Libraries/NRefactory/Test/Output/VBNet/CSharpToVBConverterTest.cs

1
src/Libraries/NRefactory/Project/NRefactory.csproj

@ -92,6 +92,7 @@ @@ -92,6 +92,7 @@
<Compile Include="Src\PrettyPrinter\IOutputAstVisitor.cs" />
<Compile Include="Src\PrettyPrinter\NodeInformVisitor.cs" />
<Compile Include="Src\PrettyPrinter\SpecialNodesInserter.cs" />
<Compile Include="Src\SnippetParser.cs" />
<Compile Include="Src\Visitors\AbstractAstTransformer.cs" />
<Compile Include="Src\Visitors\AbstractASTVisitor.cs" />
<Compile Include="Src\Visitors\CodeDOMOutputVisitor.cs" />

11
src/Libraries/NRefactory/Project/Src/Parser/AbstractParser.cs

@ -6,6 +6,7 @@ @@ -6,6 +6,7 @@
// </file>
using System;
using System.Collections.Generic;
using ICSharpCode.NRefactory.Ast;
namespace ICSharpCode.NRefactory.Parser
@ -53,7 +54,7 @@ namespace ICSharpCode.NRefactory.Parser @@ -53,7 +54,7 @@ namespace ICSharpCode.NRefactory.Parser
}
}
protected AbstractParser(ILexer lexer)
internal AbstractParser(ILexer lexer)
{
this.errors = lexer.Errors;
this.lexer = lexer;
@ -63,9 +64,11 @@ namespace ICSharpCode.NRefactory.Parser @@ -63,9 +64,11 @@ namespace ICSharpCode.NRefactory.Parser
public abstract void Parse();
public abstract Expression ParseExpression();
public abstract BlockStatement ParseBlock();
public abstract List<INode> ParseTypeMembers();
protected abstract void SynErr(int line, int col, int errorNumber);
protected void SynErr(int n)
{
if (errDist >= MinErrDist) {
@ -96,7 +99,9 @@ namespace ICSharpCode.NRefactory.Parser @@ -96,7 +99,9 @@ namespace ICSharpCode.NRefactory.Parser
public void Dispose()
{
errors = null;
lexer.Dispose();
if (lexer != null) {
lexer.Dispose();
}
lexer = null;
}
#endregion

35
src/Libraries/NRefactory/Project/Src/Parser/CSharp/CSharpParser.cs

@ -53,6 +53,41 @@ namespace ICSharpCode.NRefactory.Parser.CSharp @@ -53,6 +53,41 @@ namespace ICSharpCode.NRefactory.Parser.CSharp
Expr(out expr);
return expr;
}
public override BlockStatement ParseBlock()
{
lexer.NextToken();
compilationUnit = new CompilationUnit();
BlockStatement blockStmt = new BlockStatement();
blockStmt.StartLocation = la.Location;
compilationUnit.BlockStart(blockStmt);
while (la.kind != Tokens.EOF) {
Token oldLa = la;
Statement();
if (la == oldLa) {
// did not advance lexer position, we cannot parse this as a statement block
return null;
}
}
compilationUnit.BlockEnd();
return blockStmt;
}
public override List<INode> ParseTypeMembers()
{
lexer.NextToken();
compilationUnit = new CompilationUnit();
TypeDeclaration newType = new TypeDeclaration(Modifiers.None, null);
compilationUnit.BlockStart(newType);
ClassBody();
compilationUnit.BlockEnd();
return newType.Children;
}
// Begin ISTypeCast
bool IsTypeCast()
{

1844
src/Libraries/NRefactory/Project/Src/Parser/CSharp/Parser.cs

File diff suppressed because it is too large Load Diff

4
src/Libraries/NRefactory/Project/Src/Parser/CSharp/cs.ATG

@ -343,7 +343,7 @@ TypeDecl<ModifierList m, List<AttributeSection> attributes> @@ -343,7 +343,7 @@ TypeDecl<ModifierList m, List<AttributeSection> attributes>
{ IF (IdentIsWhere()) TypeParameterConstraintsClause<templates> }
(. newType.BodyStartLocation = t.EndLocation; .)
ClassBody
"{" ClassBody "}"
[ ";" ] (. newType.EndLocation = t.Location;
compilationUnit.BlockEnd();
.)
@ -456,7 +456,6 @@ ClassBase<out List<TypeReference> names> @@ -456,7 +456,6 @@ ClassBase<out List<TypeReference> names>
ClassBody
(. AttributeSection section; .)
=
"{"
{ (.List<AttributeSection> attributes = new List<AttributeSection>();
ModifierList m = new ModifierList();
.)
@ -464,7 +463,6 @@ ClassBody @@ -464,7 +463,6 @@ ClassBody
MemberModifiers<m>
ClassMemberDecl<m, attributes>
}
"}"
.
StructInterfaces<out List<TypeReference> names>

3
src/Libraries/NRefactory/Project/Src/Parser/IParser.cs

@ -6,6 +6,7 @@ @@ -6,6 +6,7 @@
// </file>
using System;
using System.Collections.Generic;
using ICSharpCode.NRefactory.Ast;
namespace ICSharpCode.NRefactory
@ -34,5 +35,7 @@ namespace ICSharpCode.NRefactory @@ -34,5 +35,7 @@ namespace ICSharpCode.NRefactory
void Parse();
Expression ParseExpression();
BlockStatement ParseBlock();
List<INode> ParseTypeMembers();
}
}

112
src/Libraries/NRefactory/Project/Src/Parser/VBNet/Parser.cs

@ -562,8 +562,14 @@ out baseInterfaces); @@ -562,8 +562,14 @@ out baseInterfaces);
ClassBody(
#line 425 "VBNET.ATG"
newType);
Expect(88);
Expect(67);
#line 426 "VBNET.ATG"
newType.EndLocation = t.EndLocation;
Expect(1);
#line 427 "VBNET.ATG"
#line 429 "VBNET.ATG"
compilationUnit.BlockEnd();
break;
@ -571,7 +577,7 @@ newType); @@ -571,7 +577,7 @@ newType);
case 121: {
lexer.NextToken();
#line 431 "VBNET.ATG"
#line 433 "VBNET.ATG"
m.Check(Modifiers.VBModules);
TypeDeclaration newType = new TypeDeclaration(m.Modifier, attributes);
compilationUnit.AddChild(newType);
@ -581,17 +587,17 @@ newType); @@ -581,17 +587,17 @@ newType);
Identifier();
#line 438 "VBNET.ATG"
#line 440 "VBNET.ATG"
newType.Name = t.val;
Expect(1);
#line 440 "VBNET.ATG"
#line 442 "VBNET.ATG"
newType.BodyStartLocation = t.Location;
ModuleBody(
#line 441 "VBNET.ATG"
#line 443 "VBNET.ATG"
newType);
#line 443 "VBNET.ATG"
#line 445 "VBNET.ATG"
compilationUnit.BlockEnd();
break;
@ -599,7 +605,7 @@ newType); @@ -599,7 +605,7 @@ newType);
case 166: {
lexer.NextToken();
#line 447 "VBNET.ATG"
#line 449 "VBNET.ATG"
m.Check(Modifiers.VBStructures);
TypeDeclaration newType = new TypeDeclaration(m.Modifier, attributes);
compilationUnit.AddChild(newType);
@ -609,28 +615,28 @@ newType); @@ -609,28 +615,28 @@ newType);
Identifier();
#line 454 "VBNET.ATG"
#line 456 "VBNET.ATG"
newType.Name = t.val;
TypeParameterList(
#line 455 "VBNET.ATG"
#line 457 "VBNET.ATG"
newType.Templates);
Expect(1);
#line 457 "VBNET.ATG"
#line 459 "VBNET.ATG"
newType.BodyStartLocation = t.Location;
while (la.kind == 107) {
TypeImplementsClause(
#line 458 "VBNET.ATG"
#line 460 "VBNET.ATG"
out baseInterfaces);
#line 458 "VBNET.ATG"
#line 460 "VBNET.ATG"
newType.BaseTypes.AddRange(baseInterfaces);
}
StructureBody(
#line 459 "VBNET.ATG"
#line 461 "VBNET.ATG"
newType);
#line 461 "VBNET.ATG"
#line 463 "VBNET.ATG"
compilationUnit.BlockEnd();
break;
@ -638,7 +644,7 @@ newType); @@ -638,7 +644,7 @@ newType);
case 90: {
lexer.NextToken();
#line 466 "VBNET.ATG"
#line 468 "VBNET.ATG"
m.Check(Modifiers.VBEnums);
TypeDeclaration newType = new TypeDeclaration(m.Modifier, attributes);
newType.StartLocation = m.GetDeclarationLocation(t.Location);
@ -649,26 +655,26 @@ newType); @@ -649,26 +655,26 @@ newType);
Identifier();
#line 474 "VBNET.ATG"
#line 476 "VBNET.ATG"
newType.Name = t.val;
if (la.kind == 48) {
lexer.NextToken();
NonArrayTypeName(
#line 475 "VBNET.ATG"
#line 477 "VBNET.ATG"
out typeRef, false);
#line 475 "VBNET.ATG"
#line 477 "VBNET.ATG"
newType.BaseTypes.Add(typeRef);
}
Expect(1);
#line 477 "VBNET.ATG"
#line 479 "VBNET.ATG"
newType.BodyStartLocation = t.Location;
EnumBody(
#line 478 "VBNET.ATG"
#line 480 "VBNET.ATG"
newType);
#line 480 "VBNET.ATG"
#line 482 "VBNET.ATG"
compilationUnit.BlockEnd();
break;
@ -676,7 +682,7 @@ newType); @@ -676,7 +682,7 @@ newType);
case 112: {
lexer.NextToken();
#line 485 "VBNET.ATG"
#line 487 "VBNET.ATG"
m.Check(Modifiers.VBInterfacs);
TypeDeclaration newType = new TypeDeclaration(m.Modifier, attributes);
newType.StartLocation = m.GetDeclarationLocation(t.Location);
@ -686,28 +692,28 @@ newType); @@ -686,28 +692,28 @@ newType);
Identifier();
#line 492 "VBNET.ATG"
#line 494 "VBNET.ATG"
newType.Name = t.val;
TypeParameterList(
#line 493 "VBNET.ATG"
#line 495 "VBNET.ATG"
newType.Templates);
EndOfStmt();
#line 495 "VBNET.ATG"
#line 497 "VBNET.ATG"
newType.BodyStartLocation = t.Location;
while (la.kind == 110) {
InterfaceBase(
#line 496 "VBNET.ATG"
#line 498 "VBNET.ATG"
out baseInterfaces);
#line 496 "VBNET.ATG"
#line 498 "VBNET.ATG"
newType.BaseTypes.AddRange(baseInterfaces);
}
InterfaceBody(
#line 497 "VBNET.ATG"
#line 499 "VBNET.ATG"
newType);
#line 499 "VBNET.ATG"
#line 501 "VBNET.ATG"
compilationUnit.BlockEnd();
break;
@ -715,7 +721,7 @@ newType); @@ -715,7 +721,7 @@ newType);
case 80: {
lexer.NextToken();
#line 504 "VBNET.ATG"
#line 506 "VBNET.ATG"
m.Check(Modifiers.VBDelegates);
DelegateDeclaration delegateDeclr = new DelegateDeclaration(m.Modifier, attributes);
delegateDeclr.ReturnType = new TypeReference("", "System.Void");
@ -726,63 +732,63 @@ newType); @@ -726,63 +732,63 @@ newType);
lexer.NextToken();
Identifier();
#line 511 "VBNET.ATG"
#line 513 "VBNET.ATG"
delegateDeclr.Name = t.val;
TypeParameterList(
#line 512 "VBNET.ATG"
#line 514 "VBNET.ATG"
delegateDeclr.Templates);
if (la.kind == 24) {
lexer.NextToken();
if (StartOf(4)) {
FormalParameterList(
#line 513 "VBNET.ATG"
#line 515 "VBNET.ATG"
p);
}
Expect(25);
#line 513 "VBNET.ATG"
#line 515 "VBNET.ATG"
delegateDeclr.Parameters = p;
}
} else if (la.kind == 100) {
lexer.NextToken();
Identifier();
#line 515 "VBNET.ATG"
#line 517 "VBNET.ATG"
delegateDeclr.Name = t.val;
TypeParameterList(
#line 516 "VBNET.ATG"
#line 518 "VBNET.ATG"
delegateDeclr.Templates);
if (la.kind == 24) {
lexer.NextToken();
if (StartOf(4)) {
FormalParameterList(
#line 517 "VBNET.ATG"
#line 519 "VBNET.ATG"
p);
}
Expect(25);
#line 517 "VBNET.ATG"
#line 519 "VBNET.ATG"
delegateDeclr.Parameters = p;
}
if (la.kind == 48) {
lexer.NextToken();
#line 518 "VBNET.ATG"
#line 520 "VBNET.ATG"
TypeReference type;
TypeName(
#line 518 "VBNET.ATG"
#line 520 "VBNET.ATG"
out type);
#line 518 "VBNET.ATG"
#line 520 "VBNET.ATG"
delegateDeclr.ReturnType = type;
}
} else SynErr(214);
#line 520 "VBNET.ATG"
#line 522 "VBNET.ATG"
delegateDeclr.EndLocation = t.EndLocation;
Expect(1);
#line 523 "VBNET.ATG"
#line 525 "VBNET.ATG"
compilationUnit.AddChild(delegateDeclr);
break;
@ -1003,40 +1009,34 @@ out type); @@ -1003,40 +1009,34 @@ out type);
}
void ClassBody(
#line 533 "VBNET.ATG"
#line 535 "VBNET.ATG"
TypeDeclaration newType) {
#line 534 "VBNET.ATG"
#line 536 "VBNET.ATG"
AttributeSection section;
while (StartOf(7)) {
#line 536 "VBNET.ATG"
#line 538 "VBNET.ATG"
List<AttributeSection> attributes = new List<AttributeSection>();
ModifierList m = new ModifierList();
while (la.kind == 27) {
AttributeSection(
#line 539 "VBNET.ATG"
#line 541 "VBNET.ATG"
out section);
#line 539 "VBNET.ATG"
#line 541 "VBNET.ATG"
attributes.Add(section);
}
while (StartOf(8)) {
MemberModifier(
#line 540 "VBNET.ATG"
#line 542 "VBNET.ATG"
m);
}
ClassMemberDecl(
#line 541 "VBNET.ATG"
#line 543 "VBNET.ATG"
m, attributes);
}
Expect(88);
Expect(67);
#line 543 "VBNET.ATG"
newType.EndLocation = t.EndLocation;
Expect(1);
}
void ModuleBody(

4
src/Libraries/NRefactory/Project/Src/Parser/VBNet/VBNET.ATG

@ -423,6 +423,8 @@ NonModuleDeclaration<ModifierList m, List<AttributeSection> attributes> @@ -423,6 +423,8 @@ NonModuleDeclaration<ModifierList m, List<AttributeSection> attributes>
[ ClassBaseType<out typeRef> (. newType.BaseTypes.Add(typeRef); .) ]
{ TypeImplementsClause<out baseInterfaces> (. newType.BaseTypes.AddRange(baseInterfaces); .) }
ClassBody<newType>
"End" "Class" (. newType.EndLocation = t.EndLocation; .)
EOL
(.
compilationUnit.BlockEnd();
.)
@ -540,8 +542,6 @@ ClassBody<TypeDeclaration newType> @@ -540,8 +542,6 @@ ClassBody<TypeDeclaration newType>
{ MemberModifier<m> }
ClassMemberDecl<m, attributes>
}
"End" "Class" (. newType.EndLocation = t.EndLocation; .)
EOL
.
StructureBody<TypeDeclaration newType>

22
src/Libraries/NRefactory/Project/Src/Parser/VBNet/VBNetParser.cs

@ -65,6 +65,28 @@ namespace ICSharpCode.NRefactory.Parser.VB @@ -65,6 +65,28 @@ namespace ICSharpCode.NRefactory.Parser.VB
Expr(out expr);
return expr;
}
public override BlockStatement ParseBlock()
{
lexer.NextToken();
compilationUnit = new CompilationUnit();
Statement st;
Block(out st);
return st as BlockStatement;
}
public override List<INode> ParseTypeMembers()
{
lexer.NextToken();
compilationUnit = new CompilationUnit();
TypeDeclaration newType = new TypeDeclaration(Modifiers.None, null);
compilationUnit.BlockStart(newType);
ClassBody(newType);
compilationUnit.BlockEnd();
return newType.Children;
}
bool LeaveBlock()
{

129
src/Libraries/NRefactory/Project/Src/SnippetParser.cs

@ -0,0 +1,129 @@ @@ -0,0 +1,129 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Daniel Grunwald"/>
// <version>$Revision$</version>
// </file>
using System;
using System.Collections.Generic;
using System.IO;
using ICSharpCode.NRefactory.Ast;
using ICSharpCode.NRefactory.Parser;
namespace ICSharpCode.NRefactory
{
/// <summary>
/// The snippet parser supports parsing code snippets that are not valid as a full compilation unit.
/// </summary>
public class SnippetParser
{
readonly SupportedLanguage language;
public SnippetParser(SupportedLanguage language)
{
this.language = language;
}
Errors errors;
List<ISpecial> specials;
/// <summary>
/// Gets the errors of the last call to Parse(). Returns null if parse was not yet called.
/// </summary>
public Errors Errors {
get { return errors; }
}
/// <summary>
/// Gets the specials of the last call to Parse(). Returns null if parse was not yet called.
/// </summary>
public List<ISpecial> Specials {
get { return specials; }
}
/// <summary>
/// Parse the code. The result may be a CompilationUnit, an Expression, a BlockStatement or a list of class
/// members.
/// </summary>
public INode Parse(string code)
{
IParser parser = ParserFactory.CreateParser(language, new StringReader(code));
parser.Parse();
errors = parser.Errors;
specials = parser.Lexer.SpecialTracker.RetrieveSpecials();
INode result = parser.CompilationUnit;
if (errors.Count > 0) {
parser = ParserFactory.CreateParser(language, new StringReader(code));
Expression expression = parser.ParseExpression();
if (expression != null && parser.Errors.Count < errors.Count) {
errors = parser.Errors;
specials = parser.Lexer.SpecialTracker.RetrieveSpecials();
result = expression;
}
}
if (errors.Count > 0) {
parser = ParserFactory.CreateParser(language, new StringReader(code));
BlockStatement block = parser.ParseBlock();
if (block != null && parser.Errors.Count < errors.Count) {
errors = parser.Errors;
specials = parser.Lexer.SpecialTracker.RetrieveSpecials();
result = block;
}
}
if (errors.Count > 0) {
parser = ParserFactory.CreateParser(language, new StringReader(code));
List<INode> members = parser.ParseTypeMembers();
if (members != null && members.Count > 0 && parser.Errors.Count < errors.Count) {
errors = parser.Errors;
specials = parser.Lexer.SpecialTracker.RetrieveSpecials();
result = new MemberListNode(members);
}
}
return result;
}
sealed class MemberListNode : INode
{
List<INode> members;
public MemberListNode(List<INode> members)
{
this.members = members;
}
public INode Parent {
get { return null; }
set { throw new NotSupportedException(); }
}
public List<INode> Children {
get { return members; }
}
public Location StartLocation {
get { return Location.Empty; }
set { throw new NotSupportedException(); }
}
public Location EndLocation {
get { return Location.Empty; }
set { throw new NotSupportedException(); }
}
public object AcceptChildren(IAstVisitor visitor, object data)
{
foreach (INode n in members) {
n.AcceptVisitor(visitor, data);
}
return null;
}
public object AcceptVisitor(IAstVisitor visitor, object data)
{
return AcceptChildren(visitor, data);
}
}
}
}

1
src/Libraries/NRefactory/Project/Src/Visitors/CSharpToVBNetConvertVisitor.cs

@ -13,6 +13,7 @@ namespace ICSharpCode.NRefactory.Visitors @@ -13,6 +13,7 @@ namespace ICSharpCode.NRefactory.Visitors
/// <summary>
/// This class converts C# constructs to their VB.NET equivalents.
/// </summary>
[Obsolete("Use CSharpConstructsVisitor + ToVBNetConvertVisitor instead")]
public class CSharpToVBNetConvertVisitor : CSharpConstructsVisitor
{
public override object VisitCompilationUnit(CompilationUnit compilationUnit, object data)

1
src/Libraries/NRefactory/Project/Src/Visitors/VBNetToCSharpConvertVisitor.cs

@ -15,6 +15,7 @@ namespace ICSharpCode.NRefactory.Visitors @@ -15,6 +15,7 @@ namespace ICSharpCode.NRefactory.Visitors
/// Applying the VBNetToCSharpConvertVisitor on a CompilationUnit has the same effect
/// as applying the VBNetConstructsConvertVisitor and ToCSharpConvertVisitor.
/// </summary>
[Obsolete("Use VBNetConstructsConvertVisitor + ToCSharpConvertVisitor instead")]
public class VBNetToCSharpConvertVisitor : VBNetConstructsConvertVisitor
{
public override object VisitCompilationUnit(CompilationUnit compilationUnit, object data)

6
src/Libraries/NRefactory/Test/General/UnitTest.cs

@ -23,7 +23,7 @@ namespace ICSharpCode.NRefactory.Tests @@ -23,7 +23,7 @@ namespace ICSharpCode.NRefactory.Tests
Type[] allTypes = typeof(INode).Assembly.GetTypes();
foreach (Type type in allTypes) {
if (type.IsClass && !type.IsAbstract && type.GetInterface(typeof(INode).FullName) != null) {
if (type.IsClass && !type.IsAbstract && !type.IsNested && type.GetInterface(typeof(INode).FullName) != null) {
MethodInfo methodInfo = type.GetMethod("ToString", BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public);
Assert.IsNotNull(methodInfo, "ToString() not found in " + type.FullName);
}
@ -68,7 +68,7 @@ namespace ICSharpCode.NRefactory.Tests @@ -68,7 +68,7 @@ namespace ICSharpCode.NRefactory.Tests
Type visitor = typeof(IAstVisitor);
foreach (Type type in allTypes) {
if (type.IsClass && !type.IsAbstract && type.GetInterface(typeof(INode).FullName) != null && !type.Name.StartsWith("Null")) {
if (type.IsClass && !type.IsAbstract && !type.IsNested && type.GetInterface(typeof(INode).FullName) != null && !type.Name.StartsWith("Null")) {
MethodInfo methodInfo = visitor.GetMethod("Visit" + type.Name, BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.ExactBinding, null, new Type[] {type, typeof(object)}, null);
Assert.IsNotNull(methodInfo, "Visit with parameter " + type.FullName + " not found");
Assert.AreEqual(2, methodInfo.GetParameters().Length);
@ -89,7 +89,7 @@ namespace ICSharpCode.NRefactory.Tests @@ -89,7 +89,7 @@ namespace ICSharpCode.NRefactory.Tests
Type visitor = typeof(AbstractAstVisitor);
foreach (Type type in allTypes) {
if (type.IsClass && !type.IsAbstract && type.GetInterface(typeof(INode).FullName) != null && !type.Name.StartsWith("Null")) {
if (type.IsClass && !type.IsAbstract && !type.IsNested && type.GetInterface(typeof(INode).FullName) != null && !type.Name.StartsWith("Null")) {
MethodInfo methodInfo = visitor.GetMethod("Visit" + type.Name, BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.ExactBinding, null, new Type[] {type, typeof(object)}, null);
Assert.IsNotNull(methodInfo, "Visit with parameter " + type.FullName + " not found");

1
src/Libraries/NRefactory/Test/NRefactoryTests.csproj

@ -46,6 +46,7 @@ @@ -46,6 +46,7 @@
<Compile Include="AssemblyInfo.cs" />
<Compile Include="Lexer\CSharp\LexerTests.cs" />
<Compile Include="General\UnitTest.cs" />
<Compile Include="Output\SnippetConversion.cs" />
<Compile Include="Parser\GlobalScope\AttributeSectionTests.cs" />
<Compile Include="Output\CSharp\CSharpOutputTest.cs" />
<Compile Include="Parser\Expressions\PrimitiveExpressionTests.cs" />

3
src/Libraries/NRefactory/Test/Output/CSharp/VBToCSharpConverterTest.cs

@ -24,7 +24,8 @@ namespace ICSharpCode.NRefactory.Tests.PrettyPrinter @@ -24,7 +24,8 @@ namespace ICSharpCode.NRefactory.Tests.PrettyPrinter
IParser parser = ParserFactory.CreateParser(SupportedLanguage.VBNet, new StringReader(input));
parser.Parse();
Assert.AreEqual("", parser.Errors.ErrorOutput);
parser.CompilationUnit.AcceptVisitor(new VBNetToCSharpConvertVisitor(), null);
parser.CompilationUnit.AcceptVisitor(new VBNetConstructsConvertVisitor(), null);
parser.CompilationUnit.AcceptVisitor(new ToCSharpConvertVisitor(), null);
CSharpOutputVisitor outputVisitor = new CSharpOutputVisitor();
outputVisitor.VisitCompilationUnit(parser.CompilationUnit, null);
Assert.AreEqual("", outputVisitor.Errors.ErrorOutput);

107
src/Libraries/NRefactory/Test/Output/SnippetConversion.cs

@ -0,0 +1,107 @@ @@ -0,0 +1,107 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Daniel Grunwald"/>
// <version>$Revision$</version>
// </file>
using System;
using NUnit.Framework;
using ICSharpCode.NRefactory.Ast;
using ICSharpCode.NRefactory.Visitors;
using ICSharpCode.NRefactory.PrettyPrinter;
namespace ICSharpCode.NRefactory.Tests.Output
{
[TestFixture]
public class SnippetConversion
{
void CS2VB(string input, string expectedOutput)
{
SnippetParser parser = new SnippetParser(SupportedLanguage.CSharp);
INode node = parser.Parse(input);
Assert.IsNotNull(node);
Assert.AreEqual("", parser.Errors.ErrorOutput);
PreprocessingDirective.CSharpToVB(parser.Specials);
node.AcceptVisitor(new CSharpConstructsVisitor(), null);
node.AcceptVisitor(new ToVBNetConvertVisitor(), null);
VBNetOutputVisitor output = new VBNetOutputVisitor();
using (SpecialNodesInserter.Install(parser.Specials, output)) {
node.AcceptVisitor(output, null);
}
Assert.AreEqual("", output.Errors.ErrorOutput);
Assert.AreEqual(expectedOutput, output.Text);
}
[Test]
public void CompilationUnitCS2VB()
{
CS2VB(
@"using System;
public class MyClass
{
string abc;
public string Abc { get { return abc; } }
// This is a test method
static void M<T>(params T[] args) where T : IDisposable
{
Console.WriteLine(""Hello!"");
}
}",
@"Imports System
Public Class [MyClass]
Private m_abc As String
Public ReadOnly Property Abc() As String
Get
Return m_abc
End Get
End Property
' This is a test method
Private Shared Sub M(Of T As IDisposable)(ParamArray args As T())
Console.WriteLine(""Hello!"")
End Sub
End Class
"
);
}
[Test]
public void TypeMembersCS2VB()
{
CS2VB(
"void Test() {}\n" +
"void Test2() {}",
@"Private Sub Test()
End Sub
Private Sub Test2()
End Sub
"
);
}
[Test]
public void StatementsCS2VB()
{
CS2VB(
"int a = 3;\n" +
"a++;",
@"Dim a As Integer = 3
a += 1
"
);
}
}
}

3
src/Libraries/NRefactory/Test/Output/VBNet/CSharpToVBConverterTest.cs

@ -24,7 +24,8 @@ namespace ICSharpCode.NRefactory.Tests.PrettyPrinter @@ -24,7 +24,8 @@ namespace ICSharpCode.NRefactory.Tests.PrettyPrinter
IParser parser = ParserFactory.CreateParser(SupportedLanguage.CSharp, new StringReader(input));
parser.Parse();
Assert.AreEqual("", parser.Errors.ErrorOutput);
parser.CompilationUnit.AcceptVisitor(new CSharpToVBNetConvertVisitor(), null);
parser.CompilationUnit.AcceptVisitor(new CSharpConstructsVisitor(), null);
parser.CompilationUnit.AcceptVisitor(new ToVBNetConvertVisitor(), null);
VBNetOutputVisitor outputVisitor = new VBNetOutputVisitor();
outputVisitor.VisitCompilationUnit(parser.CompilationUnit, null);
Assert.AreEqual("", outputVisitor.Errors.ErrorOutput);

Loading…
Cancel
Save