Ejemplo n.º 1
0
        public override object VisitAnonymousMethodExpression(AnonymousMethodExpression anonymousMethodExpression, object data)
        {
            AnonymousMethodReturnType amrt = new AnonymousMethodReturnType(resolver.CompilationUnit);

            foreach (ParameterDeclarationExpression param in anonymousMethodExpression.Parameters)
            {
                amrt.MethodParameters.Add(NRefactoryASTConvertVisitor.CreateParameter(param, resolver.CallingMember as IMethod, resolver.CallingClass, resolver.CompilationUnit));
            }
            return(amrt);
        }
 public ICompilationUnit setCurrentCompilationUnit(CompilationUnit compilationUnit)
 {
     if (compilationUnit == null)
         return null;
     NRefactoryASTConvertVisitor converter;
     converter = new NRefactoryASTConvertVisitor(myProjectContent);
     compilationUnit.AcceptVisitor(converter, null);
     var newCompilationUnit = converter.Cu;
     parseInformation.SetCompilationUnit(newCompilationUnit);
     return newCompilationUnit;            
 }
		INode Parse(SupportedLanguage sourceLanguage, string sourceCode, out string error)
		{
			project = new DefaultProjectContent();
			project.ReferencedContents.AddRange(ReferencedContents);
			if (sourceLanguage == SupportedLanguage.VBNet) {
				project.DefaultImports = new DefaultUsing(project);
				project.DefaultImports.Usings.AddRange(DefaultImportsToAdd);
			}
			SnippetParser parser = new SnippetParser(sourceLanguage);
			INode result = parser.Parse(sourceCode);
			error = parser.Errors.ErrorOutput;
			specials = parser.Specials;
			if (parser.Errors.Count != 0)
				return null;
			
			// now create a dummy compilation unit around the snippet result
			switch (parser.SnippetType) {
				case SnippetType.CompilationUnit:
					compilationUnit = (CompilationUnit)result;
					break;
				case SnippetType.Expression:
					compilationUnit = MakeCompilationUnitFromTypeMembers(
						MakeMethodFromBlock(
							MakeBlockFromExpression(
								(Expression)result
							)));
					break;
				case SnippetType.Statements:
					compilationUnit = MakeCompilationUnitFromTypeMembers(
						MakeMethodFromBlock(
							(BlockStatement)result
						));
					break;
				case SnippetType.TypeMembers:
					compilationUnit = MakeCompilationUnitFromTypeMembers(result.Children);
					break;
				default:
					throw new NotSupportedException("Unknown snippet type: " + parser.SnippetType);
			}
			
			// convert NRefactory CU in DOM CU
			NRefactoryASTConvertVisitor visitor = new NRefactoryASTConvertVisitor(project);
			visitor.VisitCompilationUnit(compilationUnit, null);
			visitor.Cu.FileName = sourceLanguage == SupportedLanguage.CSharp ? "a.cs" : "a.vb";
			
			// and register the compilation unit in the DOM
			foreach (IClass c in visitor.Cu.Classes) {
				project.AddClassToNamespaceList(c);
			}
			parseInfo = new ParseInformation();
			parseInfo.SetCompilationUnit(visitor.Cu);
			
			return result;
		}
		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);
			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;
		}
		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", ""));
		}
Ejemplo n.º 6
0
        internal LambdaReturnType(AnonymousMethodExpression expression, NRefactoryResolver resolver)
            : base(resolver.CompilationUnit)
        {
            this.resolver = resolver;

            if (expression.HasParameterList)
            {
                base.MethodParameters = new List <IParameter>();
                foreach (ParameterDeclarationExpression param in expression.Parameters)
                {
                    base.MethodParameters.Add(NRefactoryASTConvertVisitor.CreateParameter(param, resolver.CallingMember as IMethod, resolver.CallingClass, resolver.CompilationUnit));
                }
            }
            expression.Body.AcceptVisitor(new ReturnStatementFinder(returnExpressions), null);
        }
		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;
		}
		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;
		}
Ejemplo n.º 9
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, ICSharpCode.NRefactory.SupportedLanguage.VBNet);
			if (projectContent.Project != null) {
				visitor.VBRootNamespace = ((IProject)projectContent.Project).RootNamespace;
			}
			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;
		}
Ejemplo n.º 10
0
        internal LambdaReturnType(LambdaExpression expression, NRefactoryResolver resolver)
            : base(resolver.CompilationUnit)
        {
            this.resolver         = resolver;
            this.lambdaExpression = expression;

            base.MethodParameters = new List <IParameter>();
            foreach (ParameterDeclarationExpression param in expression.Parameters)
            {
                base.MethodParameters.Add(NRefactoryASTConvertVisitor.CreateParameter(param, resolver.CallingMember as IMethod, resolver.CallingClass, resolver.CompilationUnit));
            }
            if (expression.ExpressionBody.IsNull)
            {
                expression.StatementBody.AcceptVisitor(new ReturnStatementFinder(returnExpressions), null);
            }
            else
            {
                returnExpressions.Add(expression.ExpressionBody);
            }
        }
        INode Parse(SupportedLanguage sourceLanguage, string sourceCode, out string error)
        {
            project = new DefaultProjectContent();
            project.ReferencedContents.AddRange(ReferencedContents);
            if (sourceLanguage == SupportedLanguage.VBNet)
            {
                project.Language       = LanguageProperties.VBNet;
                project.DefaultImports = new DefaultUsing(project);
                project.DefaultImports.Usings.AddRange(DefaultImportsToAdd);
            }
            else
            {
                project.Language = LanguageProperties.CSharp;
            }
            SnippetParser parser = new SnippetParser(sourceLanguage);
            INode         result = parser.Parse(sourceCode);

            error    = parser.Errors.ErrorOutput;
            specials = parser.Specials;
            if (parser.Errors.Count != 0)
            {
                return(null);
            }

            wasExpression = parser.SnippetType == SnippetType.Expression;
            if (wasExpression)
            {
                // Special case 'Expression': expressions may be replaced with other statements in the AST by the ConvertVisitor,
                // but we need to return a 'stable' node so that the correct transformed AST is returned.
                // Thus, we wrap any expressions into a statement block.
                result = MakeBlockFromExpression((Expression)result);
            }

            // now create a dummy compilation unit around the snippet result
            switch (parser.SnippetType)
            {
            case SnippetType.CompilationUnit:
                compilationUnit = (CompilationUnit)result;
                break;

            case SnippetType.Expression:
            case SnippetType.Statements:
                compilationUnit = MakeCompilationUnitFromTypeMembers(
                    MakeMethodFromBlock(
                        (BlockStatement)result
                        ));
                break;

            case SnippetType.TypeMembers:
                compilationUnit = MakeCompilationUnitFromTypeMembers(result.Children);
                break;

            default:
                throw new NotSupportedException("Unknown snippet type: " + parser.SnippetType);
            }

            // convert NRefactory CU in DOM CU
            NRefactoryASTConvertVisitor visitor = new NRefactoryASTConvertVisitor(project);

            visitor.VisitCompilationUnit(compilationUnit, null);
            visitor.Cu.FileName = sourceLanguage == SupportedLanguage.CSharp ? "a.cs" : "a.vb";

            // and register the compilation unit in the DOM
            foreach (IClass c in visitor.Cu.Classes)
            {
                project.AddClassToNamespaceList(c);
            }
            parseInfo = new ParseInformation();
            parseInfo.SetCompilationUnit(visitor.Cu);

            return(result);
        }
Ejemplo n.º 12
0
		INode Parse(SupportedLanguage sourceLanguage, string sourceCode, out string error)
		{
			project = new DefaultProjectContent();
			project.ReferencedContents.AddRange(ReferencedContents);
			if (sourceLanguage == SupportedLanguage.VBNet) {
				project.Language = LanguageProperties.VBNet;
				project.DefaultImports = new DefaultUsing(project);
				project.DefaultImports.Usings.AddRange(DefaultImportsToAdd);
			} else {
				project.Language = LanguageProperties.CSharp;
			}
			SnippetParser parser = new SnippetParser(sourceLanguage);
			INode result = parser.Parse(sourceCode);
			error = parser.Errors.ErrorOutput;
			specials = parser.Specials;
			if (parser.Errors.Count != 0)
				return null;
			
			wasExpression = parser.SnippetType == SnippetType.Expression;
			if (wasExpression) {
				// Special case 'Expression': expressions may be replaced with other statements in the AST by the ConvertVisitor,
				// but we need to return a 'stable' node so that the correct transformed AST is returned.
				// Thus, we wrap any expressions into a statement block.
				result = MakeBlockFromExpression((Expression)result);
			}
			
			// now create a dummy compilation unit around the snippet result
			switch (parser.SnippetType) {
				case SnippetType.CompilationUnit:
					compilationUnit = (CompilationUnit)result;
					break;
				case SnippetType.Expression:
				case SnippetType.Statements:
					compilationUnit = MakeCompilationUnitFromTypeMembers(
						MakeMethodFromBlock(
							(BlockStatement)result
						));
					break;
				case SnippetType.TypeMembers:
					compilationUnit = MakeCompilationUnitFromTypeMembers(result.Children);
					break;
				default:
					throw new NotSupportedException("Unknown snippet type: " + parser.SnippetType);
			}
			
			// convert NRefactory CU in DOM CU
			NRefactoryASTConvertVisitor visitor = new NRefactoryASTConvertVisitor(project, sourceLanguage);
			visitor.VisitCompilationUnit(compilationUnit, null);
			visitor.Cu.FileName = sourceLanguage == SupportedLanguage.CSharp ? "a.cs" : "a.vb";
			
			// and register the compilation unit in the DOM
			foreach (IClass c in visitor.Cu.Classes) {
				project.AddClassToNamespaceList(c);
			}
			parseInfo = new ParseInformation(visitor.Cu);
			
			return result;
		}
 public ICompilationUnit ConvertCompilationUnit(CompilationUnit compilationUnit)
 {
     var converter = new NRefactoryASTConvertVisitor(myProjectContent);
     compilationUnit.AcceptVisitor(converter, null);            
     return converter.Cu;
 }
Ejemplo n.º 14
0
		private ICompilationUnit ConvertCompilationUnit(CompilationUnit cu)
		{
			var supportedLanguage = SupportedLanguage == SupportedLanguage.CSharp
				? ICSharpCode.NRefactory.SupportedLanguage.CSharp
				: ICSharpCode.NRefactory.SupportedLanguage.VBNet;
			var converter = new NRefactoryASTConvertVisitor(ProjectContent, supportedLanguage);
			cu.AcceptVisitor(converter, null);
			return converter.Cu;
		}
Ejemplo n.º 15
0
 private ICompilationUnit ConvertCompilationUnit(CompilationUnit cu)
 {
     NRefactoryASTConvertVisitor converter;
     converter = new NRefactoryASTConvertVisitor(myProjectContent);
     cu.AcceptVisitor(converter, null);
     return converter.Cu;
 }
Ejemplo n.º 16
0
        INode Parse(SupportedLanguage sourceLanguage, string sourceCode, out string error)
        {
            project = new DefaultProjectContent();
            project.ReferencedContents.AddRange(ReferencedContents);
            if (sourceLanguage == SupportedLanguage.VBNet)
            {
                project.DefaultImports = new DefaultUsing(project);
                project.DefaultImports.Usings.AddRange(DefaultImportsToAdd);
            }
            SnippetParser parser = new SnippetParser(sourceLanguage);
            INode         result = parser.Parse(sourceCode);

            error    = parser.Errors.ErrorOutput;
            specials = parser.Specials;
            if (parser.Errors.Count != 0)
            {
                return(null);
            }

            // now create a dummy compilation unit around the snippet result
            switch (parser.SnippetType)
            {
            case SnippetType.CompilationUnit:
                compilationUnit = (CompilationUnit)result;
                break;

            case SnippetType.Expression:
                compilationUnit = MakeCompilationUnitFromTypeMembers(
                    MakeMethodFromBlock(
                        MakeBlockFromExpression(
                            (Expression)result
                            )));
                break;

            case SnippetType.Statements:
                compilationUnit = MakeCompilationUnitFromTypeMembers(
                    MakeMethodFromBlock(
                        (BlockStatement)result
                        ));
                break;

            case SnippetType.TypeMembers:
                compilationUnit = MakeCompilationUnitFromTypeMembers(result.Children);
                break;

            default:
                throw new NotSupportedException("Unknown snippet type: " + parser.SnippetType);
            }

            // convert NRefactory CU in DOM CU
            NRefactoryASTConvertVisitor visitor = new NRefactoryASTConvertVisitor(project);

            visitor.VisitCompilationUnit(compilationUnit, null);
            visitor.Cu.FileName = sourceLanguage == SupportedLanguage.CSharp ? "a.cs" : "a.vb";

            // and register the compilation unit in the DOM
            foreach (IClass c in visitor.Cu.Classes)
            {
                project.AddClassToNamespaceList(c);
            }
            parseInfo = new ParseInformation();
            parseInfo.SetCompilationUnit(visitor.Cu);

            return(result);
        }