Beispiel #1
0
        public override void Run()
        {
            IWorkbenchWindow window = WorkbenchSingleton.Workbench.ActiveWorkbenchWindow;

            if (window != null && window.ViewContent is IEditable)
            {
                IParser p = ParserFactory.CreateParser(SupportedLanguage.CSharp, new StringReader(((IEditable)window.ViewContent).Text));
                p.Parse();
                if (p.Errors.Count > 0)
                {
                    MessageService.ShowError("${res:ICSharpCode.SharpDevelop.Commands.Convert.CorrectSourceCodeErrors}\n" + p.Errors.ErrorOutput);
                    return;
                }

                VBNetOutputVisitor vbv = new VBNetOutputVisitor();

                List <ISpecial> specials = p.Lexer.SpecialTracker.CurrentSpecials;
                PreprocessingDirective.CSharpToVB(specials);
                new CSharpToVBNetConvertVisitor().VisitCompilationUnit(p.CompilationUnit, null);
                using (SpecialNodesInserter.Install(specials, vbv)) {
                    vbv.VisitCompilationUnit(p.CompilationUnit, null);
                }

                FileService.NewFile("Generated.VB", "VBNET", vbv.Text);
            }
        }
        void CS2VB(string input, string expectedOutput)
        {
            SnippetParser parser = new SnippetParser(SupportedLanguage.CSharp);
            INode         node   = parser.Parse(input);

            // parser.Errors.ErrorOutput contains syntax errors, if any
            Assert.IsNotNull(node);
            Assert.AreEqual("", parser.Errors.ErrorOutput);
            // parser.Specials is the list of comments, preprocessor directives etc.
            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);
            }
            // output.Errors.ErrorOutput contains conversion errors/warnings, if any
            // output.Text contains the converted code
            Assert.AreEqual("", output.Errors.ErrorOutput);
            Assert.AreEqual(expectedOutput, output.Text);
        }
Beispiel #3
0
        public override void Run()
        {
            IViewContent content = WorkbenchSingleton.Workbench.ActiveViewContent;

            if (content != null && content.PrimaryFileName != null && content is IEditable)
            {
                IParser p = ParserFactory.CreateParser(SupportedLanguage.CSharp, ((IEditable)content).CreateSnapshot().CreateReader());
                p.Parse();
                if (p.Errors.Count > 0)
                {
                    MessageService.ShowError("${res:ICSharpCode.SharpDevelop.Commands.Convert.CorrectSourceCodeErrors}\n" + p.Errors.ErrorOutput);
                    return;
                }

                VBNetOutputVisitor vbv = new VBNetOutputVisitor();

                List <ISpecial> specials = p.Lexer.SpecialTracker.CurrentSpecials;
                PreprocessingDirective.CSharpToVB(specials);
                IAstVisitor v = new CSharpToVBNetConvertVisitor(ParserService.CurrentProjectContent,
                                                                ParserService.GetParseInformation(content.PrimaryFileName));
                v.VisitCompilationUnit(p.CompilationUnit, null);
                using (SpecialNodesInserter.Install(specials, vbv)) {
                    vbv.VisitCompilationUnit(p.CompilationUnit, null);
                }

                FileService.NewFile("Generated.vb", vbv.Text);
            }
        }
        protected override void ConvertAst(CompilationUnit compilationUnit, List <ISpecial> specials, FileProjectItem sourceItem)
        {
            PreprocessingDirective.CSharpToVB(specials);
            IProjectContent             pc      = ParserService.GetProjectContent(sourceItem.Project) ?? ParserService.CurrentProjectContent;
            CSharpToVBNetConvertVisitor visitor = new CSharpToVBNetConvertVisitor(pc, ParserService.GetParseInformation(sourceItem.FileName));

            visitor.RootNamespaceToRemove     = sourceItem.Project.RootNamespace;
            visitor.DefaultImportsToRemove    = defaultImports;
            visitor.StartupObjectToMakePublic = startupObject;
            compilationUnit.AcceptVisitor(visitor, null);
        }
Beispiel #5
0
        private void Generate(SupportedLanguage language, TextReader inputstream, OutputClass output)
        {
            IParser parser = ParserFactory.CreateParser(language, inputstream);

            parser.Parse();
            if (parser.Errors.Count > 0)
            {
                new ExceptionDialog(null, "Error Parsing Input Code").ShowDialog();
            }
            else if (output.CodeDomProvider != null)
            {
                CodeDomVisitor visitor = new CodeDomVisitor();
                visitor.VisitCompilationUnit(parser.CompilationUnit, null);
                for (int i = visitor.codeCompileUnit.Namespaces.Count - 1; i >= 0; i--)
                {
                    if (visitor.codeCompileUnit.Namespaces[i].Types.Count == 0)
                    {
                        visitor.codeCompileUnit.Namespaces.RemoveAt(i);
                    }
                }
                CodeGeneratorOptions options = new CodeGeneratorOptions();
                options.BlankLinesBetweenMembers = true;
                StringWriter writer = new StringWriter();
                output.CodeDomProvider.GenerateCodeFromCompileUnit(visitor.codeCompileUnit, writer, options);
                this.scintillaOutput.Text = writer.ToString();
                writer.Close();
            }
            else
            {
                AbstractAstTransformer transformer     = output.CreateTransformer();
                List <ISpecial>        currentSpecials = parser.Lexer.SpecialTracker.CurrentSpecials;
                if ((language == SupportedLanguage.CSharp) && (transformer is ToVBNetConvertVisitor))
                {
                    PreprocessingDirective.CSharpToVB(currentSpecials);
                }
                else if ((language == SupportedLanguage.VBNet) && (transformer is ToCSharpConvertVisitor))
                {
                    PreprocessingDirective.VBToCSharp(currentSpecials);
                }
                parser.CompilationUnit.AcceptVisitor(transformer, null);
                IOutputAstVisitor outputVisitor = output.CreatePrettyPrinter();
                using (SpecialNodesInserter.Install(currentSpecials, outputVisitor))
                {
                    outputVisitor.VisitCompilationUnit(parser.CompilationUnit, null);
                }
                this.scintillaOutput.Text = outputVisitor.Text;
            }
        }
Beispiel #6
0
        public string CSharpToVB(string input, out string errors)
        {
            INode node = Parse(SupportedLanguage.CSharp, input, out errors);

            if (node == null)
            {
                return(null);
            }
            // apply conversion logic:
            var visitor = new CSharpToVBNetConvertVisitor(project, parseInfo);

            visitor.DefaultImportsToRemove = DefaultImportsToRemove;
            compilationUnit.AcceptVisitor(visitor, null);
            PreprocessingDirective.CSharpToVB(specials);
            return(CreateCode(UnpackExpression(node), new VBNetOutputVisitor()));
        }
Beispiel #7
0
        void TestProgramCS2VB(string programCS, string programVB)
        {
            IParser parser = ParserFactory.CreateParser(SupportedLanguage.CSharp, new StringReader(programCS));

            parser.Parse();
            Assert.AreEqual("", parser.Errors.ErrorOutput);
            VBNetOutputVisitor outputVisitor = new VBNetOutputVisitor();
            List <ISpecial>    specials      = parser.Lexer.SpecialTracker.RetrieveSpecials();

            PreprocessingDirective.CSharpToVB(specials);
            outputVisitor.Options.IndentationChar = ' ';
            outputVisitor.Options.IndentSize      = 2;
            using (SpecialNodesInserter.Install(specials, outputVisitor)) {
                outputVisitor.VisitCompilationUnit(parser.CompilationUnit, null);
            }
            Assert.AreEqual("", outputVisitor.Errors.ErrorOutput);
            Assert.AreEqual(programVB.Replace("\r", ""), outputVisitor.Text.TrimEnd().Replace("\r", ""));
            parser.Dispose();
        }
        public void TestProgram(string input, string expectedOutput)
        {
            IParser parser = ParserFactory.CreateParser(SupportedLanguage.CSharp, new StringReader(input));

            parser.Parse();
            Assert.AreEqual("", parser.Errors.ErrorOutput);
            var specials = parser.Lexer.SpecialTracker.RetrieveSpecials();

            PreprocessingDirective.CSharpToVB(specials);
            parser.CompilationUnit.AcceptVisitor(new CSharpConstructsConvertVisitor(), null);
            parser.CompilationUnit.AcceptVisitor(new ToVBNetConvertVisitor(), null);
            VBNetOutputVisitor outputVisitor = new VBNetOutputVisitor();

            outputVisitor.Options.IndentationChar     = ' ';
            outputVisitor.Options.IndentSize          = 2;
            outputVisitor.Options.OutputByValModifier = true;
            using (SpecialNodesInserter.Install(specials, outputVisitor)) {
                outputVisitor.VisitCompilationUnit(parser.CompilationUnit, null);
            }
            Assert.AreEqual("", outputVisitor.Errors.ErrorOutput);
            Assert.AreEqual(expectedOutput.Replace("\r", ""), outputVisitor.Text.Replace("\r", ""));
        }
        private static string ConvertCSharpCodeToVb(string Code)
        {
            IParser p = ParserFactory.CreateParser(SupportedLanguage.CSharp, new StringReader(Code));

            p.Parse();

            if (p.Errors.Count > 0)
            {
                return(Code);
            }
            NRefactory.PrettyPrinter.VBNetOutputVisitor output = new NRefactory.PrettyPrinter.VBNetOutputVisitor();
            List <ISpecial> specials = p.Lexer.SpecialTracker.CurrentSpecials;

            PreprocessingDirective.CSharpToVB(specials);
            p.CompilationUnit.AcceptVisitor(new NRefactory.Visitors.CSharpConstructsVisitor(), null);
            p.CompilationUnit.AcceptVisitor(new NRefactory.Visitors.ToVBNetConvertVisitor(), null);

            using (NRefactory.PrettyPrinter.SpecialNodesInserter.Install(specials, output))
            {
                output.VisitCompilationUnit(p.CompilationUnit, null);
            }
            return(output.Text);
        }