Really open source?

4 posts, 0 answers
  1. Fabien
    Fabien avatar
    2 posts
    Member since:
    May 2009

    Posted 01 May 2009 Link to this post

    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

  2. Todd
    Admin
    Todd avatar
    65 posts
    Member since:
    Sep 2012

    Posted 04 May 2009 Link to this post

    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

  3. Fabien
    Fabien avatar
    2 posts
    Member since:
    May 2009

    Posted 05 May 2009 Link to this post

    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!

  4. Todd Anglin
    Todd Anglin avatar
    2040 posts
    Member since:
    Aug 2005

    Posted 05 May 2009 Link to this post

    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

Back to Top