Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / Open Source Projects > Code Converter > Really open source?

Not answered Really open source?

Feed from this thread
  • Fabien avatar

    Posted on May 1, 2009 (permalink)

    Hi,

    I've tested your code converter and I really liked it! I was just wondering if it was really open source (as the category of the forum suggests it)? If yes, where can I found the sources? I'm really interrested in how you managed to wrap the code snippets into classes and methods (or not if there was already one) and I'd like to see more details about it.

    Thanks

    Reply

  • Todd Todd admin's avatar

    Posted on May 4, 2009 (permalink)

    Hi Fabien,

    Thanks for the note. You are correct that the project is really a bit mis-named as it is not truly "open source." That said, it should be "Telerik Open Source," so we should be able to make the code available for your consumption.

    In the mean time, the wrapping and unwrapping code is pretty basic. The general idea is to simply ensure that the minimum requirements are met for NRefactory to "do its thing" with the code snippet. And that means that the snippet needs to be a complete class- not just a statement or method. To accomplish that, we use two methods- wrap and unwrap, one for VBtoCS and one for CStoVB. Here's an example of the VBtoCS wrap method:

    private string wrapVbCode(string vbCode) 
        { 
            //If the VB code is already wrapped in a Class 
            //then we do not need to add our wrapper 
            if (vbCode.Contains("Class") || vbCode.Contains("Namespace")) 
            { 
                return vbCode; 
            } 
            else 
            { 
                this.isClassWrapped = true
     
                if (!vbCode.Contains("Sub") && !vbCode.Contains("Function") && !vbCode.Contains("Property") && !vbCode.Contains("Enum")) 
                { 
                    this.isWrapped = true
                    //We need to wrap the code in a dummy method for converter to work 
                    vbCode = String.Format("Public Sub DummyWrapper(){0}{1}{0}End Sub",Environment.NewLine, vbCode); 
                } 
     
                //We need to wrap code in dummy class to work 
                return String.Format("Public Class DummyClassWrapper{0}{1}{0}End Class", Environment.NewLine, vbCode); 
            } 
        } 

    Then, after the conversion, assuming there are no errors, we try to unwrap the code, like this:

    private string unwrapVbCode(string newVbCode) 
            if (checkForError(newVbCode)) 
            { 
                return formatError(newVbCode); 
            } 
     
            //NEW HACK: Fix region conversion problems with NRefactory (TMA 7/9/2007) 
            Regex r1 = new Regex(@"^\s*#endregion", RegexOptions.Multiline); 
            newVbCode = r1.Replace(newVbCode, "#End Region"); 
            //END HACK 
     
            if (!this.isClassWrapped) 
            { 
                return addFooter(newVbCode,true); 
            } 
            else 
            { 
                //Keep track of how many tabs to remove from code 
                int tabsToRemove = 0; 
     
                System.Text.StringBuilder sb = new System.Text.StringBuilder(newVbCode); 
     
                if (this.isWrapped) 
                { 
                    sb.Replace("Public Sub DummyWrapper()\r\n\t""").Replace("End Sub\r\n"""); 
                    tabsToRemove++; 
                } 
                 
                sb.Replace("Public Class DummyClassWrapper\r\n""").Replace("End Class\r\n"""); 
     
                tabsToRemove++; 
     
                return addFooter(removeTabs(sb.ToString(), tabsToRemove),true); 
            } 
        } 

    We will work on cleaning-up the code and package everything together and make it available on Telerik.com soon. Hope that helps!

    -Todd

    Reply

  • Fabien avatar

    Posted on May 5, 2009 (permalink)

    Hi Todd, and thanks for your reply.

    I've been drilling through NRefactory source code over the weekend and I've found something interresting that avoid having to wrap the code snippet at all.

    With the latest version of NRefactory you can use the following method to convert C# to VB.Net:

    private string ConvertCSharpToVB(string input)  
            {  
                SnippetParser parser = new SnippetParser(SupportedLanguage.CSharp);  
                INode node = parser.Parse(tbInput.Text);  
     
                if (parser.Errors.Count > 0)  
                {  
                    MessageBox.Show("Invalid C# code.");  
                    return string.Empty;  
                }  
                  
                PreprocessingDirective.CSharpToVB(parser.Specials);  
     
                // Convert C# constructs to VB.NET:  
                node.AcceptVisitor(new CSharpConstructsConvertVisitor(), null);  
                node.AcceptVisitor(new ToVBNetConvertVisitor(), null);  
     
                VBNetOutputVisitor output = new VBNetOutputVisitor();  
                using (SpecialNodesInserter.Install(parser.Specials, output))  
                {  
                    node.AcceptVisitor(output, null);  
                }  
     
                return output.Text;  
            } 

    As you can see this is pretty easy and it works well with code snippets, full methods or full classes.

    Also, I've got a question about your implementation of the converter. How come when trying to convert the following C# statement:
    i++; 

    I get the following result:
    System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1) 

    I don't think it's an issue with NRefactory as it is returning me "i +=1" when I try with my implementation (which is nothing more than the method posted above).

    Anyway, thanks for your time and your quick reply!

    Reply

  • Posted on May 5, 2009 (permalink)

    Thanks for the insight, Fabien! It seems NRefactory may have new support for converting snippets that wasn't around when the Converter was originally built. We'll definitely re-evaluate our approach in light of this and try to leverage the internal NRefactory methods.

    As for the syntax conversion error, I think that's also due to new NRefactory support. In the past, NRefactory always convtered "i++" in this strange way. I appears the newest builds no longer do. I'll try to update the NRefactory source we're using ASAP and that should hopefully address the problem.

    Thanks again for all your input!
    -Todd

    Reply

Back to Top

Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / Open Source Projects > Code Converter > Really open source?