public static Expression<Func<TFrom, TTo>> AndLambdas<TFrom, TTo>(this Expression<Func<TFrom, TTo>> first, Expression<Func<TFrom, TTo>> second) { ParameterExpression paramToUse = first.Parameters[0]; Expression bodyLeft = first.Body; ConversionVisitor visitor = new ConversionVisitor(paramToUse, second.Parameters[0]); Expression bodyRight = visitor.Visit(second.Body); return Expression.Lambda<Func<TFrom, TTo>>(Expression.MakeBinary(ExpressionType.AndAlso, bodyLeft, bodyRight), first.Parameters); }
public static Expression <Func <TTo, TR> > Convert <TFrom, TR>( Expression <Func <TFrom, TR> > e ) { var oldParameter = e.Parameters[0]; var newParameter = Expression.Parameter(typeof(TTo), oldParameter.Name); var converter = new ConversionVisitor(newParameter, oldParameter); var newBody = converter.Visit(e.Body); return(Expression.Lambda <Func <TTo, TR> >(newBody, newParameter)); }
public CompilationUnit Parse(Stream stream) { lock (CompilerCallableEntryPoint.parseLock) { CompilerCompilationUnit top = CompilerCallableEntryPoint.ParseFile (new string[] { "-v", "-unsafe"}, stream, "parsed.cs", Console.Out); if (top == null) return null; CSharpParser.ConversionVisitor conversionVisitor = new ConversionVisitor (top.LocationsBag); top.UsingsBag.Global.Accept (conversionVisitor); InsertComments (top, conversionVisitor); return conversionVisitor.Unit; } }
public static Expression <Func <TTo, TR> > Convert <TFrom, TR>(Expression <Func <TFrom, TR> > e) { if (e == null) { throw new ArgumentNullException(nameof(e)); } var oldParameter = e.Parameters[0]; var newParameter = Expression.Parameter(typeof(TTo), oldParameter.Name); var converter = new ConversionVisitor(newParameter, oldParameter); var newBody = converter.Visit(e.Body); return(Expression.Lambda <Func <TTo, TR> >(newBody, newParameter)); }
/// <summary> /// Convert expression from type [TFrom] to type [TTo] /// </summary> /// <typeparam name="TFrom"></typeparam> /// <typeparam name="TTo"></typeparam> /// <typeparam name="TReturn"></typeparam> /// <param name="e"></param> /// <returns></returns> public static Expression <Func <TTo, TResult> > Convert <TFrom, TTo, TResult>(Expression <Func <TFrom, TResult> > e) { if (e == null) { return(null); } var oldParameter = e.Parameters[0]; var newParameter = Expression.Parameter(typeof(TTo), oldParameter.Name); var converter = new ConversionVisitor(newParameter, oldParameter); try { var newBody = converter.Visit(e.Body); return(Expression.Lambda <Func <TTo, TResult> >(newBody, newParameter)); } catch (ArgumentException) { return(null); } }
public ExpressionResolutionResult ResolveExpression(Type currentType, Expression instanceParameter) { Expression currentChild = instanceParameter; Type currentChildType = currentType; foreach (var resolver in GetSourceValueResolvers()) { var getter = resolver as IMemberGetter; if (getter != null) { var memberInfo = getter.MemberInfo; var propertyInfo = memberInfo as PropertyInfo; if (propertyInfo != null) { currentChild = Expression.Property(currentChild, propertyInfo); currentChildType = propertyInfo.PropertyType; } else { throw new NotImplementedException("Expressions mapping from methods not supported yet."); } } else { var oldParameter = CustomExpression.Parameters.Single(); var newParameter = instanceParameter; var converter = new ConversionVisitor(newParameter, oldParameter); currentChild = converter.Visit(CustomExpression.Body); currentChildType = currentChild.Type; } } return(new ExpressionResolutionResult(currentChild, currentChildType)); }
private static ExpressionResolutionResult ResolveExpression(PropertyMap propertyMap, Type currentType, Expression instanceParameter) { Expression currentChild = instanceParameter; Type currentChildType = currentType; foreach (var resolver in propertyMap.GetSourceValueResolvers()) { var getter = resolver as IMemberGetter; if (getter != null) { var memberInfo = getter.MemberInfo; var propertyInfo = memberInfo as PropertyInfo; if (propertyInfo != null) { currentChild = Expression.Property(currentChild, propertyInfo); currentChildType = propertyInfo.PropertyType; } else { currentChildType = currentChild.Type; } } else { var oldParameter = propertyMap.CustomExpression.Parameters.Single(); var newParameter = instanceParameter; var converter = new ConversionVisitor(newParameter, oldParameter); currentChild = converter.Visit(propertyMap.CustomExpression.Body); currentChildType = currentChild.Type; } } return(new ExpressionResolutionResult(currentChild, currentChildType)); }
static void InsertComments (CompilerCompilationUnit top, ConversionVisitor conversionVisitor) { var leaf = GetOuterLeft (conversionVisitor.Unit); for (int i = 0; i < top.SpecialsBag.Specials.Count; i++) { var special = top.SpecialsBag.Specials [i]; AstNode newLeaf = null; var comment = special as SpecialsBag.Comment; if (comment != null) { if (conversionVisitor.convertTypeSystemMode && (comment.CommentType != SpecialsBag.CommentType.Documentation)) continue; var type = (CommentType)comment.CommentType; var start = new TextLocation (comment.Line, comment.Col); var end = new TextLocation (comment.EndLine, comment.EndCol); newLeaf = new Comment (type, start, end) { StartsLine = comment.StartsLine, Content = comment.Content }; } else { var directive = special as SpecialsBag.PreProcessorDirective; if (directive != null) { newLeaf = new PreProcessorDirective ((ICSharpCode.NRefactory.CSharp.PreProcessorDirectiveType)((int)directive.Cmd & 0xF), new TextLocation (directive.Line, directive.Col), new TextLocation (directive.EndLine, directive.EndCol)) { Argument = directive.Arg, Take = directive.Take }; } } if (newLeaf == null) continue; while (true) { var nextLeaf = NextLeaf (leaf); // insert comment at begin if (newLeaf.StartLocation < leaf.StartLocation) { var node = leaf.Parent ?? conversionVisitor.Unit; while (node.Parent != null && node.FirstChild == leaf) { leaf = node; node = node.Parent; } if (newLeaf is Comment) { node.InsertChildBefore (leaf, (Comment)newLeaf, AstNode.Roles.Comment); } else { node.InsertChildBefore (leaf, (PreProcessorDirective)newLeaf, AstNode.Roles.PreProcessorDirective); } leaf = newLeaf; break; } // insert comment at the end if (nextLeaf == null) { var node = leaf.Parent ?? conversionVisitor.Unit; if (newLeaf is Comment) { node.AddChild ((Comment)newLeaf, AstNode.Roles.Comment); } else { node.AddChild ((PreProcessorDirective)newLeaf, AstNode.Roles.PreProcessorDirective); } leaf = newLeaf; break; } // comment is between 2 nodes if (leaf.EndLocation <= newLeaf.StartLocation && newLeaf.StartLocation <= nextLeaf.StartLocation) { var node = leaf.Parent ?? conversionVisitor.Unit; if (newLeaf is Comment) { node.InsertChildAfter (leaf, (Comment)newLeaf, AstNode.Roles.Comment); } else { node.InsertChildAfter (leaf, (PreProcessorDirective)newLeaf, AstNode.Roles.PreProcessorDirective); } leaf = newLeaf; break; } leaf = nextLeaf; } } }
private LambdaExpression CreateMapExpression( Type typeIn, Type typeOut) { var typeMap = ConfigurationProvider.FindTypeMapFor(typeIn, typeOut); // this is the input parameter of this expression with name <variableName> ParameterExpression instanceParameter = Expression.Parameter(typeIn); var bindings = new List <MemberBinding>(); foreach (var propertyMap in typeMap.GetPropertyMaps().Where(pm => pm.CanResolveValue())) { var destinationProperty = propertyMap.DestinationProperty; var destinationMember = destinationProperty.MemberInfo; Expression currentChild = instanceParameter; Type currentChildType = typeIn; foreach (var resolver in propertyMap.GetSourceValueResolvers()) { var getter = resolver as IMemberGetter; if (getter != null) { var memberInfo = getter.MemberInfo; var propertyInfo = memberInfo as PropertyInfo; if (propertyInfo != null) { currentChild = Expression.Property(currentChild, propertyInfo); currentChildType = propertyInfo.PropertyType; } } else { var oldParameter = ((LambdaExpression)propertyMap.CustomExpression).Parameters.Single(); var newParameter = instanceParameter; var converter = new ConversionVisitor(newParameter, oldParameter); currentChild = converter.Visit(((LambdaExpression)propertyMap.CustomExpression).Body); var propertyInfo = propertyMap.SourceMember as PropertyInfo; if (propertyInfo != null) { currentChildType = propertyInfo.PropertyType; } } } var prop = destinationProperty.MemberInfo as PropertyInfo; // next to lists, also arrays // and objects!!! if (prop != null && prop.PropertyType.GetInterface("IEnumerable", true) != null && prop.PropertyType != typeof(string)) { Type destinationListType = prop.PropertyType.GetGenericArguments().First(); Type sourceListType = null; // is list sourceListType = currentChildType.GetGenericArguments().First(); //var newVariableName = "t" + (i++); var transformedExpression = CreateMapExpression(sourceListType, destinationListType); MethodCallExpression selectExpression = Expression.Call( typeof(Enumerable), "Select", new[] { sourceListType, destinationListType }, currentChild, transformedExpression); if (typeof(IList).IsAssignableFrom(prop.PropertyType)) { MethodCallExpression toListCallExpression = Expression.Call( typeof(Enumerable), "ToList", new Type[] { destinationListType }, selectExpression); // todo .ToArray() bindings.Add(Expression.Bind(destinationMember, toListCallExpression)); } else { // destination type implements ienumerable, but is not an ilist. allow deferred enumeration bindings.Add(Expression.Bind(destinationMember, selectExpression)); } } else { // does of course not work for subclasses etc./generic ... if (currentChildType != prop.PropertyType && // avoid nullable etc. prop.PropertyType.BaseType != typeof(ValueType) && prop.PropertyType.BaseType != typeof(Enum)) { var transformedExpression = CreateMapExpression(currentChildType, prop.PropertyType); var expr2 = Expression.Invoke( transformedExpression, currentChild ); bindings.Add(Expression.Bind(destinationMember, expr2)); } else { bindings.Add(Expression.Bind(destinationMember, currentChild)); } } } Expression total; if (typeOut.IsAbstract) { if (typeMap.CustomMapper == null) { throw new AutoMapperMappingException( String.Format("Abstract type {0} can not be mapped without custom mapper (tip: use ConvertUsing)", typeOut.Name)); } // We are going to return roughly following expression // typeOut (typeIn)x => (typeOut)(typeMap.CustomMapper.Invoke(new ResolutionContext(typeMap, x, typeIn, typeOut, options))) // This expression generates a new ResolutionContext // for the custom mapper (ResolveCore) var context = Expression.MemberInit( Expression.New( typeof(ResolutionContext).GetConstructor(BindingFlags.Instance | BindingFlags.Public, null, new Type[] { typeof(TypeMap), typeof(object), typeof(Type), typeof(Type), typeof(MappingOperationOptions) }, null), new List <Expression> { Expression.Constant(typeMap), instanceParameter, // Using the original parameter Expression.Constant(typeIn), Expression.Constant(typeOut), Expression.Constant(new MappingOperationOptions()) }) ); // This expression gets the CustomMapper from the typeMap Expression <Func <TypeMap, Func <ResolutionContext, object> > > method = x => x.CustomMapper; var customMapper = Expression.Invoke(method, Expression.Constant(typeMap)); // This expression calls the Invoke method from the CustomMapper func var invoke = Expression.Call(customMapper, typeof(Func <ResolutionContext, object>).GetMethod("Invoke"), context); // We have to convert the object from Invoke to typeOut total = Expression.Convert(invoke, typeOut); } else { total = Expression.MemberInit( Expression.New(typeOut), bindings.ToArray() ); } return(Expression.Lambda(total, instanceParameter)); }
public CompilationUnit Parse(CompilerCompilationUnit top, string fileName, int lineModifier = 0) { if (top == null) { return null; } CSharpParser.ConversionVisitor conversionVisitor = new ConversionVisitor (GenerateTypeSystemMode, top.LocationsBag); top.ModuleCompiled.Accept(conversionVisitor); InsertComments(top, conversionVisitor); if (CompilationUnitCallback != null) { CompilationUnitCallback(top); } if (top.LastYYValue is Mono.CSharp.Expression) { conversionVisitor.Unit.TopExpression = ((Mono.CSharp.Expression)top.LastYYValue).Accept(conversionVisitor) as AstNode; } conversionVisitor.Unit.FileName = fileName; return conversionVisitor.Unit; }
/// <summary> /// Converts a Mono.CSharp syntax tree into an NRefactory syntax tree. /// </summary> public SyntaxTree Parse(CompilerCompilationUnit top, string fileName) { if (top == null) { return null; } CSharpParser.ConversionVisitor conversionVisitor = new ConversionVisitor(GenerateTypeSystemMode, top.LocationsBag); top.ModuleCompiled.Accept(conversionVisitor); InsertComments(top, conversionVisitor); if (CompilationUnitCallback != null) { CompilationUnitCallback(top); } var expr = top.LastYYValue as Mono.CSharp.Expression; if (expr != null) conversionVisitor.Unit.TopExpression = expr.Accept(conversionVisitor) as AstNode; conversionVisitor.Unit.FileName = fileName; var conditionals = new List<string>(); foreach (var settings in compilerSettings.ConditionalSymbols) { if (top.Conditionals.ContainsKey(settings) && !top.Conditionals [settings]) continue; conditionals.Add(settings); } foreach (var kv in top.Conditionals) { if (!kv.Value || compilerSettings.ConditionalSymbols.Contains(kv.Key)) continue; conditionals.Add(kv.Key); } conversionVisitor.Unit.ConditionalSymbols = conditionals; return conversionVisitor.Unit; }
public MonoDevelop.CSharp.Dom.CompilationUnit Parse (TextEditorData data) { lock (CompilerCallableEntryPoint.parseLock) { CompilerCompilationUnit top; using (Stream stream = data.OpenStream ()) { top = CompilerCallableEntryPoint.ParseFile (new string[] { "-v", "-unsafe"}, stream, data.Document.FileName, Console.Out); } if (top == null) return null; CSharpParser.ConversionVisitor conversionVisitor = new ConversionVisitor (top.LocationsBag); top.UsingsBag.Global.Accept (conversionVisitor); return conversionVisitor.Unit; } }
/// <summary> /// Converts a Mono.CSharp syntax tree into an NRefactory syntax tree. /// </summary> public SyntaxTree Parse(CompilerCompilationUnit top, string fileName) { if (top == null) { return null; } CSharpParser.ConversionVisitor conversionVisitor = new ConversionVisitor (GenerateTypeSystemMode, top.LocationsBag); top.ModuleCompiled.Accept(conversionVisitor); InsertComments(top, conversionVisitor); if (CompilationUnitCallback != null) { CompilationUnitCallback(top); } if (top.LastYYValue is Mono.CSharp.Expression) { conversionVisitor.Unit.TopExpression = ((Mono.CSharp.Expression)top.LastYYValue).Accept(conversionVisitor) as AstNode; } conversionVisitor.Unit.FileName = fileName; conversionVisitor.Unit.ConditionalSymbols = top.Conditionals.Concat (compilerSettings.ConditionalSymbols).ToArray (); return conversionVisitor.Unit; }
public override ParsedDocument Parse (ProjectDom dom, string fileName, string content) { var result = new ParsedDocument (fileName); var unit = new MonoDevelop.Projects.Dom.CompilationUnit (fileName); result.CompilationUnit = unit; if (string.IsNullOrEmpty (content)) return result; lock (CompilerCallableEntryPoint.parseLock) { var tagComments = ProjectDomService.SpecialCommentTags.GetNames (); List<string > compilerArguments = new List<string> (); if (dom != null && dom.Project != null && MonoDevelop.Ide.IdeApp.Workspace != null) { DotNetProjectConfiguration configuration = dom.Project.GetConfiguration (MonoDevelop.Ide.IdeApp.Workspace.ActiveConfiguration) as DotNetProjectConfiguration; CSharpCompilerParameters par = configuration != null ? configuration.CompilationParameters as CSharpCompilerParameters : null; if (par != null) { if (!string.IsNullOrEmpty (par.DefineSymbols)) { compilerArguments.Add ("-define:" + string.Join (";", par.DefineSymbols.Split (';', ',', ' ', '\t').Where (s => !string.IsNullOrWhiteSpace (s)))); } if (par.UnsafeCode) compilerArguments.Add ("-unsafe"); if (par.TreatWarningsAsErrors) compilerArguments.Add ("-warnaserror"); if (!string.IsNullOrEmpty (par.NoWarnings)) compilerArguments.Add ("-nowarn:" + string.Join (",", par.NoWarnings.Split (';', ',', ' ', '\t'))); compilerArguments.Add ("-warn:" + par.WarningLevel); compilerArguments.Add ("-langversion:" + GetLangString (par.LangVersion)); if (par.GenerateOverflowChecks) compilerArguments.Add ("-checked"); } } CompilerCompilationUnit top; ErrorReportPrinter errorReportPrinter = new ErrorReportPrinter (); using (var stream = new MemoryStream (Encoding.UTF8.GetBytes (content))) { top = CompilerCallableEntryPoint.ParseFile (compilerArguments.ToArray (), stream, fileName, errorReportPrinter); } if (top == null) return null; foreach (var special in top.SpecialsBag.Specials) { var comment = special as SpecialsBag.Comment; if (comment != null) { VisitComment (result, comment, tagComments); } else { VisitPreprocessorDirective (result, special as SpecialsBag.PreProcessorDirective); } } // convert DOM var conversionVisitor = new ConversionVisitor (top.LocationsBag); try { conversionVisitor.Dom = dom; conversionVisitor.ParsedDocument = result; conversionVisitor.Unit = unit; top.UsingsBag.Global.Accept (conversionVisitor); top.ModuleCompiled.Accept (conversionVisitor); } catch (Exception ex) { System.Console.WriteLine (ex); } result.LanguageAST = new ICSharpCode.NRefactory.CSharp.CSharpParser().Parse (top, 0); // parser errorse errorReportPrinter.Errors.ForEach (e => conversionVisitor.ParsedDocument.Add (e)); return result; } }
private LambdaExpression CreateMapExpression( Type typeIn, Type typeOut) { var typeMap = ConfigurationProvider.FindTypeMapFor(typeIn, typeOut); if (typeMap == null) { throw new ArgumentException(string.Format("Type map for converting from \"{0}\" to \"{1}\" was not found. Please use Mapper.CreateMap<{0}, {1}>() to register a type map.", typeIn.Name, typeOut.Name)); } // this is the input parameter of this expression with name <variableName> ParameterExpression instanceParameter = Expression.Parameter(typeIn); var bindings = new List <MemberBinding>(); foreach (var propertyMap in typeMap.GetPropertyMaps().Where(pm => pm.CanResolveValue())) { var destinationProperty = propertyMap.DestinationProperty; var destinationMember = destinationProperty.MemberInfo; Expression currentChild = instanceParameter; Type currentChildType = typeIn; foreach (var resolver in propertyMap.GetSourceValueResolvers()) { var getter = resolver as IMemberGetter; if (getter != null) { var memberInfo = getter.MemberInfo; var propertyInfo = memberInfo as PropertyInfo; if (propertyInfo != null) { currentChild = Expression.Property(currentChild, propertyInfo); currentChildType = propertyInfo.PropertyType; } } else { var oldParameter = propertyMap.CustomExpression.Parameters.Single(); var newParameter = instanceParameter; var converter = new ConversionVisitor(newParameter, oldParameter); currentChild = converter.Visit(propertyMap.CustomExpression.Body); var propertyInfo = propertyMap.SourceMember as PropertyInfo; if (propertyInfo != null) { currentChildType = propertyInfo.PropertyType; } else { currentChildType = currentChild.Type; } } } var prop = destinationProperty.MemberInfo as PropertyInfo; // next to lists, also arrays // and objects!!! if (prop != null && prop.PropertyType.GetInterface("IEnumerable", true) != null && prop.PropertyType != typeof(string)) { Type destinationListType = prop.PropertyType.GetGenericArguments().FirstOrDefault() ?? prop.PropertyType.GetElementType();
internal static CompilationUnit Parse (bool convertTypeSystemMode, CompilerCompilationUnit top) { if (top == null) return null; CSharpParser.ConversionVisitor conversionVisitor = new ConversionVisitor (convertTypeSystemMode, top.LocationsBag); top.UsingsBag.Global.Accept (conversionVisitor); conversionVisitor.AddAttributeSection (conversionVisitor.Unit, top.ModuleCompiled); InsertComments (top, conversionVisitor); return conversionVisitor.Unit; }
public MonoDevelop.CSharp.Dom.CompilationUnit Parse (TextEditorData data) { lock (CompilerCallableEntryPoint.parseLock) { CompilerCompilationUnit top; using (Stream stream = data.OpenStream ()) { top = CompilerCallableEntryPoint.ParseFile (new string[] { "-v", "-unsafe"}, stream, data.Document.FileName ?? "empty.cs", Console.Out); } if (top == null) return null; CSharpParser.ConversionVisitor conversionVisitor = new ConversionVisitor (top.LocationsBag); top.UsingsBag.Global.Accept (conversionVisitor); foreach (var special in top.SpecialsBag.Specials) { var comment = special as SpecialsBag.Comment; if (comment != null) { var type = (MonoDevelop.CSharp.Dom.CommentType)comment.CommentType; var start = new DomLocation (comment.Line, comment.Col); var end = new DomLocation (comment.EndLine, comment.EndCol); var domComment = new MonoDevelop.CSharp.Dom.Comment (type, start, end); domComment.StartsLine = comment.StartsLine; domComment.Content = comment.Content; InsertComment (conversionVisitor.Unit, domComment); } } return conversionVisitor.Unit; } }
void InsertComments (CompilerCompilationUnit top, ConversionVisitor conversionVisitor) { foreach (var special in top.SpecialsBag.Specials) { var comment = special as SpecialsBag.Comment; if (comment != null) { var type = (CommentType)comment.CommentType; var start = new AstLocation (comment.Line, comment.Col); var end = new AstLocation (comment.EndLine, comment.EndCol); var domComment = new Comment (type, start, end); domComment.StartsLine = comment.StartsLine; domComment.Content = comment.Content; InsertComment (conversionVisitor.Unit, domComment); } } }
internal static CompilationUnit Parse (CompilerCompilationUnit top) { if (top == null) return null; CSharpParser.ConversionVisitor conversionVisitor = new ConversionVisitor (top.LocationsBag); top.UsingsBag.Global.Accept (conversionVisitor); InsertComments (top, conversionVisitor); return conversionVisitor.Unit; }
public CompilationUnit Parse (CompilerCompilationUnit top, int line) { if (top == null) return null; CSharpParser.ConversionVisitor conversionVisitor = new ConversionVisitor (top.LocationsBag); conversionVisitor.AddAttributeSection (conversionVisitor.Unit, top.ModuleCompiled); top.UsingsBag.Global.Accept (conversionVisitor); InsertComments (top, conversionVisitor); if (line != 0) AdjustLineLocations (conversionVisitor.Unit, line); return conversionVisitor.Unit; }
public BaseValue ConvertTo(BaseValue value, TypeId to) { var convertor = new ConversionVisitor(to); return(value.Accept(convertor)); }
public CompilationUnit Parse (CompilerCompilationUnit top, string fileName, int lineModifier = 0) { if (top == null) return null; CSharpParser.ConversionVisitor conversionVisitor = new ConversionVisitor (GenerateTypeSystemMode, top.LocationsBag); top.UsingsBag.Global.Accept (conversionVisitor); conversionVisitor.AddAttributeSection (conversionVisitor.Unit, top.ModuleCompiled); InsertComments (top, conversionVisitor); if (CompilationUnitCallback != null) CompilationUnitCallback (top); if (lineModifier != 0) AdjustLineLocations (conversionVisitor.Unit, lineModifier); if (top.LastYYValue is Mono.CSharp.Expression) conversionVisitor.Unit.TopExpression = ((Mono.CSharp.Expression)top.LastYYValue).Accept (conversionVisitor) as AstNode; conversionVisitor.Unit.FileName = fileName; return conversionVisitor.Unit; }
internal static MonoDevelop.CSharp.Ast.CompilationUnit Parse (CompilerCompilationUnit top) { if (top == null) return null; CSharpParser.ConversionVisitor conversionVisitor = new ConversionVisitor (top.LocationsBag); top.UsingsBag.Global.Accept (conversionVisitor); foreach (var special in top.SpecialsBag.Specials) { var comment = special as SpecialsBag.Comment; if (comment != null) { var type = (MonoDevelop.CSharp.Ast.CommentType)comment.CommentType; var start = new DomLocation (comment.Line, comment.Col); var end = new DomLocation (comment.EndLine, comment.EndCol); var domComment = new MonoDevelop.CSharp.Ast.Comment (type, start, end); domComment.StartsLine = comment.StartsLine; domComment.Content = comment.Content; InsertComment (conversionVisitor.Unit, domComment); } } return conversionVisitor.Unit; }
static void InsertComments(CompilerCompilationUnit top, ConversionVisitor conversionVisitor) { var leaf = GetOuterLeft (conversionVisitor.Unit); for (int i = 0; i < top.SpecialsBag.Specials.Count; i++) { var special = top.SpecialsBag.Specials [i]; AstNode newLeaf = null; var comment = special as SpecialsBag.Comment; if (comment != null) { // HACK: multiline documentation comment detection; better move this logic into the mcs tokenizer bool isMultilineDocumentationComment = ( comment.CommentType == SpecialsBag.CommentType.Multi && comment.Content.StartsWith("*", StringComparison.Ordinal) && !comment.Content.StartsWith("**", StringComparison.Ordinal) ); if (conversionVisitor.convertTypeSystemMode && !(comment.CommentType == SpecialsBag.CommentType.Documentation || isMultilineDocumentationComment)) continue; var type = isMultilineDocumentationComment ? CommentType.MultiLineDocumentation : (CommentType)comment.CommentType; var start = new TextLocation (comment.Line, comment.Col); var end = new TextLocation (comment.EndLine, comment.EndCol); newLeaf = new Comment (type, start, end) { StartsLine = comment.StartsLine, Content = isMultilineDocumentationComment ? comment.Content.Substring(1) : comment.Content }; } else { var directive = special as SpecialsBag.PreProcessorDirective; if (directive != null) { newLeaf = new PreProcessorDirective ((ICSharpCode.NRefactory.CSharp.PreProcessorDirectiveType)((int)directive.Cmd & 0xF), new TextLocation (directive.Line, directive.Col), new TextLocation (directive.EndLine, directive.EndCol)) { Argument = directive.Arg, Take = directive.Take }; } else { /* var newLine = special as SpecialsBag.NewLineToken; if (newLine != null) { if (newLine.NewLine == SpecialsBag.NewLine.Unix) { newLeaf = new UnixNewLine (new TextLocation (newLine.Line, newLine.Col)); } else { newLeaf = new WindowsNewLine (new TextLocation (newLine.Line, newLine.Col)); } }*/ } } if (newLeaf == null) continue; while (true) { var nextLeaf = NextLeaf (leaf); // insert comment at begin if (newLeaf.StartLocation < leaf.StartLocation) { var node = leaf.Parent ?? conversionVisitor.Unit; while (node.Parent != null && node.FirstChild == leaf) { leaf = node; node = node.Parent; } if (newLeaf is NewLineNode) { node.InsertChildBefore (leaf, (NewLineNode)newLeaf, Roles.NewLine); } else if (newLeaf is Comment) { node.InsertChildBefore (leaf, (Comment)newLeaf, Roles.Comment); } else { node.InsertChildBefore (leaf, (PreProcessorDirective)newLeaf, Roles.PreProcessorDirective); } leaf = newLeaf; break; } // insert comment at the end if (nextLeaf == null) { var node = leaf.Parent ?? conversionVisitor.Unit; if (newLeaf is NewLineNode) { node.AddChild ((NewLineNode)newLeaf, Roles.NewLine); } else if (newLeaf is Comment) { node.AddChild ((Comment)newLeaf, Roles.Comment); } else { node.AddChild ((PreProcessorDirective)newLeaf, Roles.PreProcessorDirective); } leaf = newLeaf; break; } // comment is between 2 nodes if (leaf.EndLocation <= newLeaf.StartLocation && newLeaf.StartLocation <= nextLeaf.StartLocation) { var node = leaf.Parent ?? conversionVisitor.Unit; if (newLeaf is NewLineNode) { node.InsertChildAfter (leaf, (NewLineNode)newLeaf, Roles.NewLine); } else if (newLeaf is Comment) { node.InsertChildAfter (leaf, (Comment)newLeaf, Roles.Comment); } else { node.InsertChildAfter (leaf, (PreProcessorDirective)newLeaf, Roles.PreProcessorDirective); } leaf = newLeaf; break; } leaf = nextLeaf; } } }
public override ParsedDocument Parse (ProjectDom dom, string fileName, string content) { lock (CompilerCallableEntryPoint.parseLock) { if (string.IsNullOrEmpty (content)) return null; List<string> compilerArguments = new List<string> (); var unit = new MonoDevelop.Projects.Dom.CompilationUnit (fileName);; var result = new ParsedDocument (fileName); result.CompilationUnit = unit; ICSharpCode.NRefactory.Parser.CSharp.Lexer lexer = new ICSharpCode.NRefactory.Parser.CSharp.Lexer (new StringReader (content)); lexer.SpecialCommentTags = ProjectDomService.SpecialCommentTags.GetNames (); lexer.EvaluateConditionalCompilation = true; if (dom != null && dom.Project != null && MonoDevelop.Ide.IdeApp.Workspace != null) { DotNetProjectConfiguration configuration = dom.Project.GetConfiguration (MonoDevelop.Ide.IdeApp.Workspace.ActiveConfiguration) as DotNetProjectConfiguration; CSharpCompilerParameters par = configuration != null ? configuration.CompilationParameters as CSharpCompilerParameters : null; if (par != null) { lexer.SetConditionalCompilationSymbols (par.DefineSymbols); if (!string.IsNullOrEmpty (par.DefineSymbols)) { compilerArguments.Add ("-define:" + string.Join (";", par.DefineSymbols.Split (';', ',', ' ', '\t'))); } if (par.UnsafeCode) compilerArguments.Add ("-unsafe"); if (par.TreatWarningsAsErrors) compilerArguments.Add ("-warnaserror"); if (!string.IsNullOrEmpty (par.NoWarnings)) compilerArguments.Add ("-nowarn:"+ string.Join (",", par.NoWarnings.Split (';', ',', ' ', '\t'))); compilerArguments.Add ("-warn:" + par.WarningLevel); compilerArguments.Add ("-langversion:" + GetLangString (par.LangVersion)); if (par.GenerateOverflowChecks) compilerArguments.Add ("-checked"); } } // compilerArguments.ForEach (arg => Console.WriteLine (arg)); while (lexer.NextToken ().Kind != ICSharpCode.NRefactory.Parser.CSharp.Tokens.EOF) ; CompilerCompilationUnit top; ErrorReportPrinter errorReportPrinter = new ErrorReportPrinter (); using (var stream = new MemoryStream (Encoding.Default.GetBytes (content))) { top = CompilerCallableEntryPoint.ParseFile (compilerArguments.ToArray (), stream, fileName, errorReportPrinter); } if (top == null) return null; SpecialTracker tracker = new SpecialTracker (result); foreach (ICSharpCode.NRefactory.ISpecial special in lexer.SpecialTracker.CurrentSpecials) { special.AcceptVisitor (tracker, null); } // convert DOM var conversionVisitor = new ConversionVisitor (top.LocationsBag, lexer.SpecialTracker.CurrentSpecials); conversionVisitor.Dom = dom; conversionVisitor.Unit = unit; conversionVisitor.ParsedDocument = result; top.UsingsBag.Global.Accept (conversionVisitor); unit.Tag = top; // parser errors errorReportPrinter.Errors.ForEach (e => conversionVisitor.ParsedDocument.Add (e)); return result; } }
void InsertComments(CompilerCompilationUnit top, ConversionVisitor conversionVisitor) { AstNode insertionPoint = conversionVisitor.Unit.FirstChild; foreach (var special in top.SpecialsBag.Specials) { AstNode newLeaf = null; Role role = null; bool isDocumentationComment = false; var comment = special as SpecialsBag.Comment; if (comment != null) { // HACK: multiline documentation comment detection; better move this logic into the mcs tokenizer bool isMultilineDocumentationComment = (comment.CommentType == SpecialsBag.CommentType.Multi && comment.Content.StartsWith("*", StringComparison.Ordinal) && !comment.Content.StartsWith("**", StringComparison.Ordinal)); isDocumentationComment = comment.CommentType == SpecialsBag.CommentType.Documentation || isMultilineDocumentationComment; if (conversionVisitor.convertTypeSystemMode && !isDocumentationComment) continue; var type = isMultilineDocumentationComment ? CommentType.MultiLineDocumentation : (CommentType)comment.CommentType; var start = new TextLocation(comment.Line, comment.Col); var end = new TextLocation(comment.EndLine, comment.EndCol); newLeaf = new Comment(type, start, end) { StartsLine = comment.StartsLine, Content = isMultilineDocumentationComment ? comment.Content.Substring(1) : comment.Content }; role = Roles.Comment; } else if (!GenerateTypeSystemMode) { var pragmaDirective = special as SpecialsBag.PragmaPreProcessorDirective; if (pragmaDirective != null) { var pragma = new PragmaWarningPreprocessorDirective(new TextLocation(pragmaDirective.Line, pragmaDirective.Col), new TextLocation(pragmaDirective.EndLine, pragmaDirective.EndCol)); pragma.AddChild(new CSharpTokenNode(new TextLocation(pragmaDirective.Line, pragmaDirective.Col), PragmaWarningPreprocessorDirective.PragmaKeywordRole), PragmaWarningPreprocessorDirective.PragmaKeywordRole); pragma.AddChild(new CSharpTokenNode(new TextLocation(pragmaDirective.Line, pragmaDirective.WarningColumn), PragmaWarningPreprocessorDirective.WarningKeywordRole), PragmaWarningPreprocessorDirective.WarningKeywordRole); var pragmaRole = pragmaDirective.Disalbe ? PragmaWarningPreprocessorDirective.DisableKeywordRole : PragmaWarningPreprocessorDirective.RestoreKeywordRole; pragma.AddChild(new CSharpTokenNode(new TextLocation(pragmaDirective.Line, pragmaDirective.DisableRestoreColumn), pragmaRole), pragmaRole); foreach (var code in pragmaDirective.Codes) { pragma.AddChild((PrimitiveExpression)conversionVisitor.Visit(code), PragmaWarningPreprocessorDirective.WarningRole); } newLeaf = pragma; role = Roles.PreProcessorDirective; goto end; } var lineDirective = special as SpecialsBag.LineProcessorDirective; if (lineDirective != null) { var pragma = new LinePreprocessorDirective(new TextLocation(lineDirective.Line, lineDirective.Col), new TextLocation(lineDirective.EndLine, lineDirective.EndCol)); pragma.LineNumber = lineDirective.LineNumber; pragma.FileName = lineDirective.FileName; newLeaf = pragma; role = Roles.PreProcessorDirective; goto end; } var directive = special as SpecialsBag.PreProcessorDirective; if (directive != null) { newLeaf = new PreProcessorDirective((PreProcessorDirectiveType)((int)directive.Cmd & 0xF), new TextLocation(directive.Line, directive.Col), new TextLocation(directive.EndLine, directive.EndCol)) { Argument = directive.Arg, Take = directive.Take }; role = Roles.PreProcessorDirective; } end: ; } if (newLeaf != null) { InsertComment(ref insertionPoint, newLeaf, role, isDocumentationComment, conversionVisitor.Unit); } } if (!GenerateTypeSystemMode) { // We cannot insert newlines in the same loop as comments/preprocessor directives // because they are not correctly ordered in the specials bag insertionPoint = conversionVisitor.Unit.FirstChild; for (int i = 0; i < top.SpecialsBag.Specials.Count; i++) { var newLine = top.SpecialsBag.Specials [i] as SpecialsBag.NewLineToken; if (newLine != null) { var newLeaf = new NewLineNode(new TextLocation(newLine.Line, newLine.Col + 1)); newLeaf.NewLineType = newLine.NewLine == SpecialsBag.NewLine.Unix ? UnicodeNewline.LF : UnicodeNewline.CRLF; InsertComment(ref insertionPoint, newLeaf, Roles.NewLine, false, conversionVisitor.Unit); } } } }
static void InsertComments (CompilerCompilationUnit top, ConversionVisitor conversionVisitor) { var leaf = GetOuterLeft(conversionVisitor.Unit); foreach (var special in top.SpecialsBag.Specials) { var comment = special as SpecialsBag.Comment; if (comment == null) continue; if (conversionVisitor.convertTypeSystemMode && (comment.CommentType != SpecialsBag.CommentType.Documentation)) continue; var type = (CommentType)comment.CommentType; var start = new AstLocation (comment.Line, comment.Col); var end = new AstLocation (comment.EndLine, comment.EndCol); var domComment = new Comment (type, start, end); domComment.StartsLine = comment.StartsLine; domComment.Content = comment.Content; while (true) { var nextLeaf = NextLeaf(leaf); // instert comment at the end if (nextLeaf == null) { var node = leaf.Parent ?? conversionVisitor.Unit; node.AddChild(domComment, AstNode.Roles.Comment); leaf = domComment; break; } // comment is between 2 nodes if (leaf.EndLocation <= domComment.StartLocation && domComment.StartLocation <= nextLeaf.StartLocation) { var node = leaf.Parent ?? conversionVisitor.Unit; node.InsertChildAfter(leaf, domComment, AstNode.Roles.Comment); leaf = domComment; break; } leaf = nextLeaf; } } }
/* /// <summary> /// Parses a file snippet; guessing what the code snippet represents (whole file, type members, block, type reference, expression). /// </summary> public AstNode ParseSnippet (string code) { // TODO: add support for parsing a part of a file throw new NotImplementedException (); } */ public DocumentationReference ParseDocumentationReference(string cref) { // see Mono.CSharp.DocumentationBuilder.HandleXrefCommon if (cref == null) throw new ArgumentNullException("cref"); // Additional symbols for < and > are allowed for easier XML typing cref = cref.Replace('{', '<').Replace('}', '>'); lock (parseLock) { errorReportPrinter = new ErrorReportPrinter(""); var ctx = new CompilerContext(compilerSettings.ToMono(), errorReportPrinter); ctx.Settings.TabSize = 1; var reader = new SeekableStreamReader(new StringTextSource(cref)); var file = new SourceFile("", "", 0); Location.Initialize(new List<SourceFile>(new [] { file })); var module = new ModuleContainer(ctx); module.DocumentationBuilder = new DocumentationBuilder(module); var source_file = new CompilationSourceFile(module); var report = new Report(ctx, errorReportPrinter); var session = new ParserSession(); session.LocationsBag = new LocationsBag(); var parser = new Mono.CSharp.CSharpParser(reader, source_file, report, session); parser.Lexer.Line += initialLocation.Line - 1; parser.Lexer.Column += initialLocation.Column - 1; parser.Lexer.putback_char = Tokenizer.DocumentationXref; parser.Lexer.parsing_generic_declaration_doc = true; parser.parse(); if (report.Errors > 0) { // Report.Warning (1584, 1, mc.Location, "XML comment on `{0}' has syntactically incorrect cref attribute `{1}'", // mc.GetSignatureForError (), cref); } var conversionVisitor = new ConversionVisitor(false, session.LocationsBag); var docRef = conversionVisitor.ConvertXmlDoc(module.DocumentationBuilder); CompilerCallableEntryPoint.Reset(); return docRef; } }
static void InsertComments (CompilerCompilationUnit top, ConversionVisitor conversionVisitor) { var leaf = GetOuterLeft (conversionVisitor.Unit); for (int i = 0; i < top.SpecialsBag.Specials.Count; i++) { var special = top.SpecialsBag.Specials [i]; Comment newLeaf = null; var comment = special as SpecialsBag.Comment; if (comment != null) { if (conversionVisitor.convertTypeSystemMode && (comment.CommentType != SpecialsBag.CommentType.Documentation)) continue; var type = (CommentType)comment.CommentType; var start = new AstLocation (comment.Line, comment.Col); var end = new AstLocation (comment.EndLine, comment.EndCol); newLeaf = new Comment (type, start, end) { StartsLine = comment.StartsLine, Content = comment.Content }; } else { // TODO: Proper handling of pre processor directives (atm got treated as comments Ast wise) var directive = special as SpecialsBag.PreProcessorDirective; if (directive != null) { newLeaf = new Comment (CommentType.SingleLine, new AstLocation (directive.Line, directive.Col), new AstLocation (directive.EndLine, directive.EndCol + 1)); if (!directive.Take) { SpecialsBag.PreProcessorDirective endif = null; int endifLevel = 0; for (int j = i + 1; j < top.SpecialsBag.Specials.Count; j++) { var s = top.SpecialsBag.Specials [j] as SpecialsBag.PreProcessorDirective; if (s == null) continue; if (s.Cmd == Tokenizer.PreprocessorDirective.If) { endifLevel++; continue; } if (s.Cmd == Tokenizer.PreprocessorDirective.Endif || endifLevel == 0 && s.Cmd == Tokenizer.PreprocessorDirective.Else) { if (endifLevel == 0) { endif = s; i = j; break; } endifLevel--; } } if (endif != null) newLeaf = new Comment (CommentType.SingleLine, new AstLocation (directive.Line, directive.Col), new AstLocation (endif.EndLine, endif.EndCol)); } } } if (newLeaf == null) continue; while (true) { var nextLeaf = NextLeaf (leaf); // insert comment at begin if (newLeaf.StartLocation < leaf.StartLocation) { var node = leaf.Parent ?? conversionVisitor.Unit; while (node.Parent != null && node.FirstChild == leaf) { leaf = node; node = node.Parent; } node.InsertChildBefore (leaf, newLeaf, AstNode.Roles.Comment); leaf = newLeaf; break; } // insert comment at the end if (nextLeaf == null) { var node = leaf.Parent ?? conversionVisitor.Unit; node.AddChild (newLeaf, AstNode.Roles.Comment); leaf = newLeaf; break; } // comment is between 2 nodes if (leaf.EndLocation <= newLeaf.StartLocation && newLeaf.StartLocation <= nextLeaf.StartLocation) { var node = leaf.Parent ?? conversionVisitor.Unit; node.InsertChildAfter (leaf, newLeaf, AstNode.Roles.Comment); leaf = newLeaf; break; } leaf = nextLeaf; } } }
private LambdaExpression CreateMapExpression( Type typeIn, Type typeOut) { var typeMap = ConfigurationProvider.FindTypeMapFor(typeIn, typeOut); // this is the input parameter of this expression with name <variableName> ParameterExpression instanceParameter = Expression.Parameter(typeIn); var bindings = new List <MemberBinding>(); foreach (var propertyMap in typeMap.GetPropertyMaps().Where(pm => pm.CanResolveValue())) { var destinationProperty = propertyMap.DestinationProperty; var destinationMember = destinationProperty.MemberInfo; Expression currentChild = instanceParameter; Type currentChildType = typeIn; foreach (var resolver in propertyMap.GetSourceValueResolvers()) { var getter = resolver as IMemberGetter; if (getter != null) { var memberInfo = getter.MemberInfo; var propertyInfo = memberInfo as PropertyInfo; if (propertyInfo != null) { currentChild = Expression.Property(currentChild, propertyInfo); currentChildType = propertyInfo.PropertyType; } } else { var oldParameter = ((LambdaExpression)propertyMap.CustomExpression).Parameters.Single(); var newParameter = instanceParameter; var converter = new ConversionVisitor(newParameter, oldParameter); currentChild = converter.Visit(((LambdaExpression)propertyMap.CustomExpression).Body); var propertyInfo = propertyMap.SourceMember as PropertyInfo; if (propertyInfo != null) { currentChildType = propertyInfo.PropertyType; } } } var prop = destinationProperty.MemberInfo as PropertyInfo; // next to lists, also arrays // and objects!!! if (prop != null && prop.PropertyType.GetInterface("IEnumerable", true) != null && prop.PropertyType != typeof(string)) { Type destinationListType = prop.PropertyType.GetGenericArguments().First(); Type sourceListType = null; // is list sourceListType = currentChildType.GetGenericArguments().First(); //var newVariableName = "t" + (i++); var transformedExpression = CreateMapExpression(sourceListType, destinationListType); MethodCallExpression selectExpression = Expression.Call( typeof(Enumerable), "Select", new[] { sourceListType, destinationListType }, currentChild, transformedExpression); if (typeof(IList).IsAssignableFrom(prop.PropertyType)) { MethodCallExpression toListCallExpression = Expression.Call( typeof(Enumerable), "ToList", new Type[] { destinationListType }, selectExpression); // todo .ToArray() bindings.Add(Expression.Bind(destinationMember, toListCallExpression)); } else { // destination type implements ienumerable, but is not an ilist. allow deferred enumeration bindings.Add(Expression.Bind(destinationMember, selectExpression)); } } else { // does of course not work for subclasses etc./generic ... if (currentChildType != prop.PropertyType && // avoid nullable etc. prop.PropertyType.BaseType != typeof(ValueType) && prop.PropertyType.BaseType != typeof(Enum)) { var transformedExpression = CreateMapExpression(currentChildType, prop.PropertyType); var expr2 = Expression.Invoke( transformedExpression, currentChild ); bindings.Add(Expression.Bind(destinationMember, expr2)); } else { bindings.Add(Expression.Bind(destinationMember, currentChild)); } } } var total = Expression.MemberInit( Expression.New(typeOut), bindings.ToArray() ); return(Expression.Lambda(total, instanceParameter)); }
void InsertComments(CompilerCompilationUnit top, ConversionVisitor conversionVisitor) { AstNode insertionPoint = conversionVisitor.Unit.FirstChild; for (int i = 0; i < top.SpecialsBag.Specials.Count; i++) { var special = top.SpecialsBag.Specials [i]; AstNode newLeaf = null; Role role = null; bool isDocumentationComment = false; var comment = special as SpecialsBag.Comment; if (comment != null) { // HACK: multiline documentation comment detection; better move this logic into the mcs tokenizer bool isMultilineDocumentationComment = ( comment.CommentType == SpecialsBag.CommentType.Multi && comment.Content.StartsWith("*", StringComparison.Ordinal) && !comment.Content.StartsWith("**", StringComparison.Ordinal) ); isDocumentationComment = comment.CommentType == SpecialsBag.CommentType.Documentation || isMultilineDocumentationComment; if (conversionVisitor.convertTypeSystemMode && !isDocumentationComment) continue; var type = isMultilineDocumentationComment ? CommentType.MultiLineDocumentation : (CommentType)comment.CommentType; var start = new TextLocation (comment.Line, comment.Col); var end = new TextLocation (comment.EndLine, comment.EndCol); newLeaf = new Comment (type, start, end) { StartsLine = comment.StartsLine, Content = isMultilineDocumentationComment ? comment.Content.Substring(1) : comment.Content }; role = Roles.Comment; } else if (!GenerateTypeSystemMode) { var pragmaDirective = special as SpecialsBag.PragmaPreProcessorDirective; if (pragmaDirective != null) { var pragma = new PragmaWarningPreprocssorDirective(new TextLocation(pragmaDirective.Line, pragmaDirective.Col), new TextLocation(pragmaDirective.EndLine, pragmaDirective.EndCol)); pragma.Disable = pragmaDirective.Disalbe; pragma.AddWarnings(pragmaDirective.Codes); newLeaf = pragma; role = Roles.PreProcessorDirective; } else { var directive = special as SpecialsBag.PreProcessorDirective; if (directive != null) { newLeaf = new PreProcessorDirective ((ICSharpCode.NRefactory.CSharp.PreProcessorDirectiveType)((int)directive.Cmd & 0xF), new TextLocation (directive.Line, directive.Col), new TextLocation (directive.EndLine, directive.EndCol)) { Argument = directive.Arg, Take = directive.Take }; role = Roles.PreProcessorDirective; } } } if (newLeaf != null) { InsertComment(ref insertionPoint, newLeaf, role, isDocumentationComment, conversionVisitor.Unit); } } if (!GenerateTypeSystemMode) { // We cannot insert newlines in the same loop as comments/preprocessor directives // because they are not correctly ordered in the specials bag insertionPoint = conversionVisitor.Unit.FirstChild; for (int i = 0; i < top.SpecialsBag.Specials.Count; i++) { var newLine = top.SpecialsBag.Specials[i] as SpecialsBag.NewLineToken; if (newLine != null) { AstNode newLeaf; if (newLine.NewLine == SpecialsBag.NewLine.Unix) { newLeaf = new UnixNewLine (new TextLocation (newLine.Line, newLine.Col + 1)); } else { newLeaf = new WindowsNewLine (new TextLocation (newLine.Line, newLine.Col + 1)); } InsertComment(ref insertionPoint, newLeaf, Roles.NewLine, false, conversionVisitor.Unit); } } } }