Exemplo n.º 1
0
        ICompilationUnit ParseVB(string fileName, string fileContent)
        {
            ICSharpCode.NRefactory.IParser p = ICSharpCode.NRefactory.ParserFactory.CreateParser(ICSharpCode.NRefactory.SupportedLanguage.VBNet, new StringReader(fileContent));
            p.ParseMethodBodies = false;
            p.Parse();
            DefaultProjectContent pc = new DefaultProjectContent();

            HostCallback.GetCurrentProjectContent = delegate {
                return(pc);
            };
            pc.ReferencedContents.Add(projectContentRegistry.Mscorlib);
            pc.ReferencedContents.Add(projectContentRegistry.GetProjectContentForReference("System.Windows.Forms", "System.Windows.Forms"));
            pc.Language = LanguageProperties.VBNet;
            lastPC      = pc;
            NRefactoryASTConvertVisitor visitor = new NRefactoryASTConvertVisitor(pc);

            visitor.VisitCompilationUnit(p.CompilationUnit, null);
            visitor.Cu.FileName = fileName;
            Assert.AreEqual(0, p.Errors.Count, "Parse error preparing compilation unit");
            visitor.Cu.ErrorsDuringCompile = p.Errors.Count > 0;
            foreach (IClass c in visitor.Cu.Classes)
            {
                pc.AddClassToNamespaceList(c);
            }

            return(visitor.Cu);
        }
        void TestProgram(SupportedLanguage sourceLanguage, string sourceCode, string expectedOutput)
        {
            DefaultProjectContent pc = new DefaultProjectContent();

            pc.ReferencedContents.Add(projectContentRegistry.Mscorlib);
            pc.ReferencedContents.Add(projectContentRegistry.GetProjectContentForReference("System.Windows.Forms", "System.Windows.Forms"));
            if (sourceLanguage == SupportedLanguage.VBNet)
            {
                pc.ReferencedContents.Add(projectContentRegistry.GetProjectContentForReference("Microsoft.VisualBasic", "Microsoft.VisualBasic"));
                pc.DefaultImports = new DefaultUsing(pc);
                pc.DefaultImports.Usings.Add("System");
                pc.DefaultImports.Usings.Add("Microsoft.VisualBasic");
            }
            pc.Language = sourceLanguage == SupportedLanguage.CSharp ? LanguageProperties.CSharp : LanguageProperties.VBNet;
            HostCallback.GetCurrentProjectContent = delegate {
                return(pc);
            };

            ICSharpCode.NRefactory.IParser parser = ParserFactory.CreateParser(sourceLanguage, new StringReader(sourceCode));
            parser.Parse();
            Assert.AreEqual("", parser.Errors.ErrorOutput);

            NRefactoryASTConvertVisitor visitor = new NRefactoryASTConvertVisitor(pc);

            visitor.VisitCompilationUnit(parser.CompilationUnit, null);
            visitor.Cu.FileName = sourceLanguage == SupportedLanguage.CSharp ? "a.cs" : "a.vb";
            foreach (IClass c in visitor.Cu.Classes)
            {
                pc.AddClassToNamespaceList(c);
            }

            ParseInformation parseInfo = new ParseInformation();

            parseInfo.SetCompilationUnit(visitor.Cu);

            if (sourceLanguage == SupportedLanguage.CSharp)
            {
                CSharpToVBNetConvertVisitor convertVisitor = new CSharpToVBNetConvertVisitor(pc, parseInfo);
                convertVisitor.RootNamespaceToRemove = "RootNamespace";
                parser.CompilationUnit.AcceptVisitor(convertVisitor, null);
            }
            else
            {
                VBNetToCSharpConvertVisitor convertVisitor = new VBNetToCSharpConvertVisitor(pc, parseInfo);
                parser.CompilationUnit.AcceptVisitor(convertVisitor, null);
            }

            IOutputAstVisitor outputVisitor = sourceLanguage == SupportedLanguage.CSharp ? (IOutputAstVisitor) new VBNetOutputVisitor() : new CSharpOutputVisitor();

            outputVisitor.Options.IndentationChar = ' ';
            outputVisitor.Options.IndentSize      = 2;
            using (SpecialNodesInserter.Install(parser.Lexer.SpecialTracker.RetrieveSpecials(),
                                                outputVisitor)) {
                outputVisitor.VisitCompilationUnit(parser.CompilationUnit, null);
            }
            Assert.AreEqual("", outputVisitor.Errors.ErrorOutput);
            Assert.AreEqual(expectedOutput.Replace("\r", ""), outputVisitor.Text.Trim().Replace("\r", ""));
        }
        ICompilationUnit ParseCSharp(ICompilationUnit oldUnit, string fileContent)
        {
            ICSharpCode.NRefactory.IParser p = ICSharpCode.NRefactory.ParserFactory.CreateParser(ICSharpCode.NRefactory.SupportedLanguage.CSharp, new StringReader(fileContent));
            p.ParseMethodBodies = false;
            p.Parse();
            NRefactoryASTConvertVisitor visitor = new NRefactoryASTConvertVisitor(pc, ICSharpCode.NRefactory.SupportedLanguage.CSharp);

            visitor.VisitCompilationUnit(p.CompilationUnit, null);
            Assert.AreEqual(0, p.Errors.Count, String.Format("Parse error preparing compilation unit: {0}", p.Errors.ErrorOutput));
            visitor.Cu.ErrorsDuringCompile = p.Errors.Count > 0;
            pc.UpdateCompilationUnit(oldUnit, visitor.Cu, null);

            return(visitor.Cu);
        }
Exemplo n.º 4
0
        ICompilationUnit Parse(ICSharpCode.NRefactory.IParser p, string fileName, IProjectContent projectContent)
        {
            p.Lexer.SpecialCommentTags = lexerTags;
            p.ParseMethodBodies        = false;
            p.Parse();

            NRefactoryASTConvertVisitor visitor = new NRefactoryASTConvertVisitor(projectContent);

            visitor.Specials = p.Lexer.SpecialTracker.CurrentSpecials;
            visitor.VisitCompilationUnit(p.CompilationUnit, null);
            visitor.Cu.FileName            = fileName;
            visitor.Cu.ErrorsDuringCompile = p.Errors.Count > 0;
            RetrieveRegions(visitor.Cu, p.Lexer.SpecialTracker);
            AddCommentTags(visitor.Cu, p.Lexer.TagComments);
            return(visitor.Cu);
        }
        ICompilationUnit Parse(string code, SupportedLanguage language, bool referenceMscorlib)
        {
            DefaultProjectContent pc = new DefaultProjectContent();

            if (referenceMscorlib)
            {
                pc.AddReferencedContent(SharedProjectContentRegistryForTests.Instance.Mscorlib);
            }
            NRefactoryASTConvertVisitor visitor = new NRefactoryASTConvertVisitor(pc);

            using (IParser p = ParserFactory.CreateParser(language, new StringReader(code))) {
                p.ParseMethodBodies = false;
                p.Parse();

                visitor.Specials = p.Lexer.SpecialTracker.CurrentSpecials;
                visitor.VisitCompilationUnit(p.CompilationUnit, null);
            }
            return(visitor.Cu);
        }
        ICompilationUnit Parse(string code, SupportedLanguage language, params IProjectContent[] references)
        {
            DefaultProjectContent pc = new DefaultProjectContent();

            foreach (var reference in references)
            {
                pc.AddReferencedContent(reference);
            }
            NRefactoryASTConvertVisitor visitor = new NRefactoryASTConvertVisitor(pc, language);

            using (IParser p = ParserFactory.CreateParser(language, new StringReader(code))) {
                p.ParseMethodBodies = false;
                p.Parse();

                visitor.Specials = p.Lexer.SpecialTracker.CurrentSpecials;
                visitor.VisitCompilationUnit(p.CompilationUnit, null);
            }
            return(visitor.Cu);
        }
        private static ICompilationUnit Parse(IParser p, string fileName, IProjectContent projectContent)
        {
            p.Lexer.SpecialCommentTags = LexerTags;
            p.ParseMethodBodies        = false;
            p.Parse();

            var visitor = new NRefactoryASTConvertVisitor(projectContent,
                                                          SupportedLanguage.CSharp)
            {
                Specials = p.Lexer.SpecialTracker.CurrentSpecials
            };

            visitor.VisitCompilationUnit(p.CompilationUnit, null);
            visitor.Cu.FileName            = fileName;
            visitor.Cu.ErrorsDuringCompile = p.Errors.Count > 0;
            RetrieveRegions(visitor.Cu, p.Lexer.SpecialTracker);
            AddCommentTags(visitor.Cu, p.Lexer.TagComments);
            return(visitor.Cu);
        }
Exemplo n.º 8
0
        private void UpdateParseInfo(CSParserResults results)
        {
            var filename           = Location.LocalPath;
            var oldCompilationUnit = FParseInfo.MostRecentCompilationUnit;
            var projectContent     = GetProjectContent();

            var visitor = new NRefactoryASTConvertVisitor(projectContent);

            visitor.Specials = results.Specials;
            visitor.VisitCompilationUnit(results.CompilationUnit, null);

            var newCompilationUnit = visitor.Cu;

            newCompilationUnit.ErrorsDuringCompile = results.HasErrors;
            newCompilationUnit.FileName            = filename;

            UpdateFoldingRegions(newCompilationUnit, results);
            AddCommentTags(newCompilationUnit, results);

            projectContent.UpdateCompilationUnit(oldCompilationUnit, newCompilationUnit, filename);
            FParseInfo.SetCompilationUnit(newCompilationUnit);
        }
Exemplo n.º 9
0
        public void Parse(bool parseMethodBodies)
        {
            try
            {
                lock (FParseLock)
                {
                    FParserResults = CSParser.Parse(TextContent, parseMethodBodies);

                    var filename           = Location.LocalPath;
                    var oldCompilationUnit = FParseInfo.MostRecentCompilationUnit;
                    var projectContent     = GetProjectContent();

                    var visitor = new NRefactoryASTConvertVisitor(projectContent);
                    visitor.Specials = FParserResults.Specials;
                    visitor.VisitCompilationUnit(FParserResults.CompilationUnit, null);

                    var newCompilationUnit = visitor.Cu;
                    newCompilationUnit.ErrorsDuringCompile = FParserResults.HasErrors;
                    newCompilationUnit.FileName            = filename;

                    UpdateFoldingRegions(newCompilationUnit, FParserResults);
                    AddCommentTags(newCompilationUnit, FParserResults);

                    // Remove information from lastCompilationUnit and add information from newCompilationUnit.
                    projectContent.UpdateCompilationUnit(oldCompilationUnit, newCompilationUnit, filename);
                    FParseInfo.SetCompilationUnit(newCompilationUnit);
                }
            }
            catch (Exception e)
            {
                Shell.Instance.Logger.Log(e);
            } finally
            {
                FParsingDone.Set();
            }
        }