Browse Source
git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/2.1@2517 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61shortcuts
17 changed files with 1296 additions and 991 deletions
File diff suppressed because it is too large
Load Diff
@ -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); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -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 |
||||||
|
"
|
||||||
|
); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue