void CSharpGenerateCodeButtonClick(object sender, EventArgs e) { StringWriter w = new StringWriter(); OutputVisitor output = new OutputVisitor(w, new CSharpFormattingPolicy()); compilationUnit.AcceptVisitor(output, null); csharpCodeTextBox.Text = w.ToString(); }
public String ToCSharp2() { IOutputAstVisitor outputVisitor = new CSharpOutputVisitor(); SetPrettyPrintOptions(outputVisitor); using (SpecialNodesInserter.Install(specials, outputVisitor)) { unit.AcceptVisitor(outputVisitor, null); } return(outputVisitor.Text); }
public static void OptimizeCompilationUnit(CompilationUnit tree) { tree.AcceptVisitor(new InlineTemporaryCVariablesVisitor()); var visitors = GetVisitors(); string oldText = null; while (tree.GetText() != oldText) { oldText = tree.GetText(); foreach (var visitor in visitors) tree.AcceptVisitor(visitor); } }
public override object VisitCompilationUnit(CompilationUnit compilationUnit, object data) { base.VisitCompilationUnit(compilationUnit, data); ToVBNetConvertVisitor v = new ToVBNetConvertVisitor(); compilationUnit.AcceptVisitor(v, data); return null; }
public static string GenerateText(TypeDeclaration type, AbstractDynamicCompilationExtension[] extensions) { var unit = new CompilationUnit(); var namespaces = new HashSet <string> { typeof(AbstractViewGenerator).Namespace, typeof(Enumerable).Namespace, typeof(int).Namespace, typeof(LinqOnDynamic).Namespace }; foreach (var extension in extensions) { foreach (var ns in extension.GetNamespacesToImport()) { namespaces.Add(ns); } } foreach (var ns in namespaces) { unit.AddChild(new Using(ns)); } unit.AddChild(type); var output = new CSharpOutputVisitor(); unit.AcceptVisitor(output, null); return(output.Text); }
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())); }
public static IEnumerable <TypeDeclaration> FindAllClasses(this CompilationUnit inode) { IndexableClassFinderVisitor v = new IndexableClassFinderVisitor(); inode.AcceptVisitor(v, null); return(v.AllClasses); }
public string Convert() { TextReader Reader = this.GetReader(); IParser Parser = ParserFactory.CreateParser(this.From, Reader); Parser.Parse(); if (Parser.Errors.Count > 0) { Log(Level.Error, Parser.Errors.ErrorOutput); throw new BuildException("Errors parsing code.", this.Location); } CompilationUnit Tree = Parser.CompilationUnit; IOutputAstVisitor OutputVisitor = null; switch (this.To) { case SupportedLanguage.CSharp: OutputVisitor = new CSharpOutputVisitor(); break; case SupportedLanguage.VBNet: OutputVisitor = new VBNetOutputVisitor(); break; } Tree.AcceptVisitor(OutputVisitor, null); return(OutputVisitor.Text); }
private ICompilationUnit ConvertCompilationUnit(CompilationUnit cu) { var converter = new NRefactoryASTConvertVisitor(MyProjectContent); cu.AcceptVisitor(converter, null); return(converter.Cu); }
public ICompilationUnit ConvertCompilationUnit(CompilationUnit compilationUnit) { var converter = new NRefactoryASTConvertVisitor(myProjectContent); compilationUnit.AcceptVisitor(converter, null); return(converter.Cu); }
public override object VisitCompilationUnit(CompilationUnit compilationUnit, object data) { base.VisitCompilationUnit(compilationUnit, data); if (!string.IsNullOrEmpty(NamespacePrefixToAdd)) { for (int i = 0; i < compilationUnit.Children.Count; i++) { NamespaceDeclaration ns = compilationUnit.Children[i] as NamespaceDeclaration; if (ns != null) { ns.Name = NamespacePrefixToAdd + "." + ns.Name; } if (compilationUnit.Children[i] is TypeDeclaration || compilationUnit.Children[i] is DelegateDeclaration) { ns = new NamespaceDeclaration(NamespacePrefixToAdd); ns.AddChild(compilationUnit.Children[i]); compilationUnit.Children[i] = ns; } } } ToCSharpConvertVisitor v = new ToCSharpConvertVisitor(); compilationUnit.AcceptVisitor(v, data); if (projectContent != null && projectContent.DefaultImports != null) { int index = 0; foreach (string u in projectContent.DefaultImports.Usings) { compilationUnit.Children.Insert(index++, new UsingDeclaration(u)); } } return(null); }
public static string IsTypeMovedToNamespaceError(CompilationUnit cu, int line, int column) { InvalidTypeOrNamespaceErrorTypeMapper invalidTypeOrNamespaceErrorTypeMapper = new InvalidTypeOrNamespaceErrorTypeMapper(line, column); cu.AcceptVisitor(invalidTypeOrNamespaceErrorTypeMapper, null); return(invalidTypeOrNamespaceErrorTypeMapper.Found); }
void CSharpGenerateCodeButtonClick(object sender, EventArgs e) { var w = new StringWriter(); var output = new CSharpOutputVisitor(w, new CSharpFormattingOptions()); unit.AcceptVisitor(output, null); editor.Text = w.ToString(); }
protected AstNode FindNode(CompilationUnit cu, TextLocation start, TextLocation end) { FindNodeVisitor fnv = new FindNodeVisitor(start, end); cu.AcceptVisitor(fnv); Assert.IsNotNull(fnv.ResultNode, "Did not find DOM node at the specified location"); return(fnv.ResultNode); }
void CSharpGenerateCodeButtonClick(object sender, EventArgs e) { StringWriter w = new StringWriter(); OutputVisitor output = new OutputVisitor(w, new VBFormattingOptions()); compilationUnit.AcceptVisitor(output, null); codeView.Text = w.ToString(); }
ICompilationUnit ConvertCompilationUnit(CompilationUnit cu) { NRefactoryASTConvertVisitor converter; SupportedLanguage supportedLanguage = SupportedLanguage == ESupportedLanguage.CSharp ? ICSharpCode.NRefactory.SupportedLanguage.CSharp : ICSharpCode.NRefactory.SupportedLanguage.VBNet; converter = new NRefactoryASTConvertVisitor(ProjectContent, supportedLanguage); cu.AcceptVisitor(converter, null); return(converter.Cu); }
void HandleDocumentParsed(object sender, EventArgs e) { if (src != null) { src.Cancel(); } resolver = null; if (guiDocument != null && MonoDevelop.Core.PropertyService.Get("EnableSemanticHighlighting", true)) { var parsedDocument = guiDocument.ParsedDocument; if (parsedDocument != null) { unit = parsedDocument.GetAst <CompilationUnit> (); parsedFile = parsedDocument.ParsedFile as CSharpParsedFile; if (guiDocument.Project != null && guiDocument.IsCompileableInProject) { src = new CancellationTokenSource(); var cancellationToken = src.Token; System.Threading.Tasks.Task.Factory.StartNew(delegate { Thread.Sleep(100); compilation = guiDocument.Compilation; var newResolver = new CSharpAstResolver(compilation, unit, parsedFile); var visitor = new QuickTaskVisitor(newResolver, cancellationToken); unit.AcceptVisitor(visitor); if (!cancellationToken.IsCancellationRequested) { Gtk.Application.Invoke(delegate { if (cancellationToken.IsCancellationRequested) { return; } var editorData = guiDocument.Editor; if (editorData == null) { return; } resolver = newResolver; quickTasks = visitor.QuickTasks; OnTasksUpdated(EventArgs.Empty); var textEditor = editorData.Parent; if (textEditor != null) { var margin = textEditor.TextViewMargin; if (!parsedDocument.HasErrors) { highlightedSegmentCache.Clear(); margin.PurgeLayoutCache(); textEditor.QueueDraw(); } } }); } }, cancellationToken); } } } }
public void rewriteCode_CSharp(CompilationUnit unit, IList <ISpecial> specials) { var outputVisitor = new CSharpOutputVisitor(); using (SpecialNodesInserter.Install(specials, outputVisitor)) { unit.AcceptVisitor(outputVisitor, null); } //codeTextBox.Text = outputVisitor.Text.Replace("\t", " "); CSharpCode = outputVisitor.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); }
protected override void ConvertAst(CompilationUnit compilationUnit, List <ISpecial> specials, FileProjectItem sourceItem) { PreprocessingDirective.VBToCSharp(specials); CompilableProject project = (CompilableProject)sourceItem.Project; RemoveWindowsFormsSpecificCode(compilationUnit, specials, project.OutputType == OutputType.WinExe); IProjectContent pc = ParserService.GetProjectContent(sourceItem.Project) ?? ParserService.CurrentProjectContent; VBNetToCSharpConvertVisitor visitor = new VBNetToCSharpConvertVisitorWithMyFormsSupport(pc, ParserService.GetParseInformation(sourceItem.FileName), sourceItem.Project.RootNamespace); compilationUnit.AcceptVisitor(visitor, null); }
public void rewriteCode_VBNet(CompilationUnit unit, IList <ISpecial> specials) { var outputVisitor = new VBNetOutputVisitor(); using (SpecialNodesInserter.Install(specials, outputVisitor)) { unit.AcceptVisitor(outputVisitor, null); } //codeTextBox.Text = outputVisitor.Text.Replace("\t", " "); VBNetCode = outputVisitor.Text; // PublicDI.log.debug(recreatedCode); }
void RunButtonClick(object sender, EventArgs e) { StringReader input = new StringReader(inputTextBox.Text); IParser parser = ParserFactory.CreateParser(SupportedLanguage.CSharp, input); parser.Parse(); if (parser.Errors.Count > 0) { outputTextBox.Text = parser.Errors.ErrorOutput; return; } CompilationUnit cu = parser.CompilationUnit; cu.AcceptVisitor(new WrapperGeneratorVisitor(), null); IOutputAstVisitor output = new CSharpOutputVisitor(); //new VBNetOutputVisitor(); // SpecialNodesInserter will re-insert the comments into the generated code using (SpecialNodesInserter.Install(parser.Lexer.SpecialTracker.RetrieveSpecials(), output)) { cu.AcceptVisitor(output, null); } outputTextBox.Text = output.Text; }
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); }
private void FindBadCatchBlock(string program, int findCount, string msg) { using (TextReader text = new StringReader(program)) { IParser parser = ParserFactory.CreateParser(SupportedLanguage.CSharp, text); parser.Parse(); Assert.AreEqual(0, parser.Errors.Count, "expected no errors in the input code"); CompilationUnit cu = parser.CompilationUnit; BadCatchBlockFinder visitor = new BadCatchBlockFinder(); cu.AcceptVisitor(visitor, null); Assert.AreEqual(findCount, visitor.Found, msg); } }
public static string GenerateText(TypeDeclaration type) { var unit = new CompilationUnit(); unit.AddChild(new Using(typeof(AbstractViewGenerator).Namespace)); unit.AddChild(new Using(typeof(Enumerable).Namespace)); unit.AddChild(new Using(typeof(int).Namespace)); unit.AddChild(new Using(typeof(LinqOnDynamic).Namespace)); unit.AddChild(type); var output = new CSharpOutputVisitor(); unit.AcceptVisitor(output, null); return(output.Text); }
void RemoveWindowsFormsSpecificCode(CompilationUnit compilationUnit, List <ISpecial> specials, bool keepCode) { for (int i = 0; i < specials.Count; i++) { PreprocessingDirective ppd = specials[i] as PreprocessingDirective; if (ppd != null && ppd.Cmd == "#if") { if (ppd.Arg == "_MyType = \"WindowsForms\"") { int depth = 1; for (int j = i + 1; j < specials.Count; j++) { ppd = specials[j] as PreprocessingDirective; if (ppd != null) { if (ppd.Cmd == "#if") { depth++; } else if (ppd.Cmd == "#endif") { depth--; if (depth == 0) { if (keepCode) { // keep code, remove only the ifdef specials.RemoveAt(j); specials.RemoveAt(i); } else { // remove ifdef including the code compilationUnit.AcceptVisitor(new RemoveMembersInRangeVisitor( DomRegion.FromLocation(specials[i].StartPosition, specials[j].EndPosition)), null); specials.RemoveRange(i, j - i + 1); } i--; break; } } } } } } } }
/// <summary> /// Tries to determine the resource set which is referenced by the /// resource manager which is assigned to the specified member. /// </summary> /// <param name="member">The referenced member to examine.</param> /// <returns> /// The ResourceSetReference, if successful, or a null reference, if the /// specified member is not a resource manager or if the /// resource file cannot be determined. /// </returns> static ResourceSetReference ResolveResourceSet(IMember member) { if (member != null && member.ReturnType != null && member.DeclaringType != null && member.DeclaringType.CompilationUnit != null) { ResourceSetReference rsr; if (!NRefactoryAstCacheService.CacheEnabled || !cachedResourceSetReferenceMappings.TryGetValue(member, out rsr)) { string declaringFileName = member.DeclaringType.CompilationUnit.FileName; if (declaringFileName != null) { if (IsResourceManager(member.ReturnType, declaringFileName)) { SupportedLanguage?language = NRefactoryResourceResolver.GetFileLanguage(declaringFileName); if (language == null) { return(null); } CompilationUnit cu = NRefactoryAstCacheService.GetFullAst(language.Value, declaringFileName, ResourceResolverService.GetParsableFileContent(declaringFileName)); if (cu != null) { ResourceManagerInitializationFindVisitor visitor = new ResourceManagerInitializationFindVisitor(member); cu.AcceptVisitor(visitor, null); if (visitor.FoundResourceSet != null) { rsr = visitor.FoundResourceSet; if (NRefactoryAstCacheService.CacheEnabled) { cachedResourceSetReferenceMappings.Add(member, rsr); } return(rsr); } } } } return(null); } return(rsr); } return(null); }
protected override void ConvertAst(CompilationUnit compilationUnit, List <ISpecial> specials, FileProjectItem sourceItem) { PreprocessingDirective.VBToCSharp(specials); CompilableProject project = (CompilableProject)sourceItem.Project; RemoveWindowsFormsSpecificCode(compilationUnit, specials, project.OutputType == OutputType.WinExe); IProjectContent pc = ParserService.GetProjectContent(sourceItem.Project) ?? ParserService.CurrentProjectContent; VBNetToCSharpConvertVisitor visitor = new VBNetToCSharpConvertVisitorWithMyFormsSupport(pc, ParserService.GetParseInformation(sourceItem.FileName), sourceItem.Project.RootNamespace); // set project options visitor.OptionInfer = (project.GetEvaluatedProperty("OptionInfer") ?? "Off") .Equals("On", StringComparison.OrdinalIgnoreCase); visitor.OptionStrict = (project.GetEvaluatedProperty("OptionStrict") ?? "Off") .Equals("On", StringComparison.OrdinalIgnoreCase); compilationUnit.AcceptVisitor(visitor, null); }
public static void InsertSpecials(CompilationUnit unit, IList <ISpecial> specials) { IOutputAstVisitor outputVisitor = new CSharpOutputVisitor(); using (SpecialNodesUserDataInserter.Install(specials, outputVisitor)) { unit.AcceptVisitor(outputVisitor, null); } /* * List<ISpecial> collectedSpecials = new List<ISpecial>(); * CollectSpecials(collectedSpecials, unit.Children); * * if (collectedSpecials.Count != specials.Count) { * Console.Out.WriteLine(collectedSpecials.Count); * Console.Out.WriteLine(specials.Count); * * }*/ }
public void mapAstDetails(CompilationUnit unit) { try { CompilationUnit = unit; AstDetails = new AstDetails(); var specials = Parser.Lexer.SpecialTracker.RetrieveSpecials(); specials.AddRange(ExtraSpecials); AstDetails.mapSpecials(specials); AstDetails.rewriteCode_CSharp(CompilationUnit, specials); AstDetails.rewriteCode_VBNet(CompilationUnit, specials); CompilationUnit.AcceptVisitor(AstDetails, null); } catch (Exception ex) { PublicDI.log.error("in mapAstDetails: {0}", ex.Message); } }
IEnumerable <FoldingRegion> GenerateFoldings(CompilationUnit unit, ParsedDocument doc) { foreach (var fold in doc.ConditionalRegions.ToFolds()) { yield return(fold); } foreach (var fold in doc.Comments.ToFolds()) { yield return(fold); } var visitor = new FoldingVisitor(); unit.AcceptVisitor(visitor, null); foreach (var fold in visitor.Foldings) { yield return(fold); } }
static void ConvertToVBNet(CompilationUnit cu) { ToVBNetConvertVisitor converter = new ToVBNetConvertVisitor(); cu.AcceptVisitor(converter, null); }
static void ConvertToPascalCase(CompilationUnit cu) { cu.AcceptVisitor(new PascalCaseConverter(), null); }
public bool ProcessAndReturnTrueIfEverythingIsSupported(CompilationUnit cu) { _result = true; cu.AcceptVisitor(this); return _result; }
public static ISet<string> Gather(CompilationUnit cu, IEnumerable<string> predefinedSymbols) { var obj = new DefinedSymbolsGatherer(predefinedSymbols); cu.AcceptVisitor(obj); return obj._definedSymbols; }