void ParseStep() { string code = null; Invoke(new MethodInvoker(delegate { code = textEditorControl1.Text; })); TextReader textReader = new StringReader(code); Dom.ICompilationUnit newCompilationUnit; if (Language == "C#") { using (NRefactory.IParser p = NRefactory.ParserFactory.CreateParser(NRefactory.SupportedLanguage.CSharp, textReader)) { p.Parse(); newCompilationUnit = ConvertCompilationUnit(p.CompilationUnit); } } else { using (NRefactory.IParser p = NRefactory.ParserFactory.CreateParser(NRefactory.SupportedLanguage.VBNet, textReader)) { p.Parse(); newCompilationUnit = ConvertCompilationUnit(p.CompilationUnit); } } // Remove information from lastCompilationUnit and add information from newCompilationUnit. myProjectContent.UpdateCompilationUnit(lastCompilationUnit, newCompilationUnit, DummyFileName); lastCompilationUnit = newCompilationUnit; }
void ParseStep() { string code = null; Invoke(new MethodInvoker(delegate { code = textEditorControl1.Text; })); TextReader textReader = new StringReader(code); Dom.ICompilationUnit newCompilationUnit; NRefactory.SupportedLanguage supportedLanguage; if (IsVisualBasic) { supportedLanguage = NRefactory.SupportedLanguage.VBNet; } else { supportedLanguage = NRefactory.SupportedLanguage.CSharp; } using (NRefactory.IParser p = NRefactory.ParserFactory.CreateParser(supportedLanguage, textReader)) { // we only need to parse types and method definitions, no method bodies // so speed up the parser and make it more resistent to syntax // errors in methods p.ParseMethodBodies = false; p.Parse(); newCompilationUnit = ConvertCompilationUnit(p.CompilationUnit); } // Remove information from lastCompilationUnit and add information from newCompilationUnit. myProjectContent.UpdateCompilationUnit(lastCompilationUnit, newCompilationUnit, DummyFileName); lastCompilationUnit = newCompilationUnit; parseInformation.SetCompilationUnit(newCompilationUnit); }
/// <summary> /// Fix type names and remove unused methods. /// </summary> void FixTypeNames(object o, ICSharpCode.SharpDevelop.Dom.ICompilationUnit domCu, ref bool foundInitMethod) { if (domCu == null) { return; } CompilationUnit cu = o as CompilationUnit; if (cu != null) { foreach (object c in cu.Children) { FixTypeNames(c, domCu, ref foundInitMethod); } return; } NamespaceDeclaration namespaceDecl = o as NamespaceDeclaration; if (namespaceDecl != null) { foreach (object c in namespaceDecl.Children) { FixTypeNames(c, domCu, ref foundInitMethod); } return; } TypeDeclaration typeDecl = o as TypeDeclaration; if (typeDecl != null) { for (int i = 0; i < typeDecl.BaseTypes.Count; i++) { typeDecl.BaseTypes[i] = FixTypeReference(typeDecl.BaseTypes[i], typeDecl.StartLocation, domCu, ReturnTypeOptions.BaseTypeReference); } for (int i = 0; i < typeDecl.Children.Count; i++) { object child = typeDecl.Children[i]; MethodDeclaration method = child as MethodDeclaration; if (method != null) { // remove all methods except InitializeComponents if ((method.Name == "InitializeComponents" || method.Name == "InitializeComponent") && method.Parameters.Count == 0) { method.Name = "InitializeComponent"; if (foundInitMethod) { throw new FormsDesignerLoadException("There are multiple InitializeComponent methods in the class. Designer cannot be loaded."); } foundInitMethod = true; } else { typeDecl.Children.RemoveAt(i--); } } else if (child is TypeDeclaration || child is FieldDeclaration) { FixTypeNames(child, domCu, ref foundInitMethod); } else { // child is property, event etc. typeDecl.Children.RemoveAt(i--); } } return; } FieldDeclaration fieldDecl = o as FieldDeclaration; if (fieldDecl != null) { fieldDecl.TypeReference = FixTypeReference(fieldDecl.TypeReference, fieldDecl.StartLocation, domCu, ReturnTypeOptions.None); foreach (VariableDeclaration var in fieldDecl.Fields) { if (var != null) { var.TypeReference = FixTypeReference(var.TypeReference, fieldDecl.StartLocation, domCu, ReturnTypeOptions.None); } } } }
void ParseStep() { string code = null; Invoke(new MethodInvoker(delegate { code = sqlEditor.Text; })); TextReader textReader = new StringReader(code); Dom.ICompilationUnit newCompilationUnit; // using (NRefactory.IParser p = NRefactory.ParserFactory.CreateParser(NRefactory.SupportedLanguage.CSharp, textReader)) { using (NRefactory.IParser p = NRefactory.ParserFactory.CreateParser(NRefactory.SupportedLanguage.VBNet, textReader)) { p.Parse(); newCompilationUnit = ConvertCompilationUnit(p.CompilationUnit); } // Remove information from lastCompilationUnit and add information from newCompilationUnit. myProjectContent.UpdateCompilationUnit(lastCompilationUnit, newCompilationUnit, DummyFileName); lastCompilationUnit = newCompilationUnit; }
void ParseStep() { try { string code = null; Invoke(new MethodInvoker(delegate { code = textEditorControl1.Text; })); TextReader textReader = new StringReader(code); Dom.ICompilationUnit newCompilationUnit; NRefactory.SupportedLanguage supportedLanguage; if (IsVisualBasic) supportedLanguage = NRefactory.SupportedLanguage.VBNet; else supportedLanguage = NRefactory.SupportedLanguage.CSharp; using (NRefactory.IParser p = NRefactory.ParserFactory.CreateParser(supportedLanguage, textReader)) { // we only need to parse types and method definitions, no method bodies // so speed up the parser and make it more resistent to syntax // errors in methods p.ParseMethodBodies = false; p.Parse(); newCompilationUnit = ConvertCompilationUnit(p.CompilationUnit); } // Remove information from lastCompilationUnit and add information from newCompilationUnit. myProjectContent.UpdateCompilationUnit(lastCompilationUnit, newCompilationUnit, DummyFileName); lastCompilationUnit = newCompilationUnit; parseInformation.SetCompilationUnit(newCompilationUnit); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(" in MainForm: " + ex.Message); // DC } }