Пример #1
0
        private void buttonGenerate_Click(object sender, EventArgs e)
        {
            this.tabControl1.SelectedTab = this.tabOutputCode;
            SupportedLanguage cSharp = SupportedLanguage.CSharp;

            if (this.radioVB.Checked)
            {
                cSharp = SupportedLanguage.VBNet;
            }
            TextReader inputstream = new StringReader(this.scintillaInput.Text);

            try
            {
                OutputClass selectedItem = (OutputClass)this.cbOutput.SelectedItem;
                this.Generate(cSharp, inputstream, selectedItem);
                this.buttonTestCodeDom.Enabled = selectedItem.CanTestCodeDom;
            }
            catch (Exception exception)
            {
                new ExceptionDialog(exception, "Error Generating CodeDom").ShowDialog();
            }
            finally
            {
                inputstream.Close();
            }
        }
Пример #2
0
 private void CompileTestCodeDom(string code)
 {
     try
     {
         OutputClass     selectedItem    = (OutputClass)this.cbCodeDom.SelectedItem;
         CodeDomProvider codeDomProvider = selectedItem.CodeDomProvider;
         CSharpSnippet   snippet         = new CSharpSnippet("\r\n    public string GenerateCode()\r\n    {\r\n        " + code + "\r\n\r\n        CodeGeneratorOptions codegenopt = new CodeGeneratorOptions();\r\n        codegenopt.BlankLinesBetweenMembers = true;\r\n\r\n        using (System.IO.StringWriter sw = new System.IO.StringWriter())\r\n        {\r\n            " + codeDomProvider.GetType().Name + " provider = new " + codeDomProvider.GetType().Name + "();\r\n\r\n            provider.GenerateCodeFromCompileUnit(_compileunit1, sw, codegenopt);\r\n\r\n            return sw.ToString();\r\n        }\r\n    }   \r\n");
         snippet.Namespaces.Add("System.CodeDom");
         snippet.Namespaces.Add("System.CodeDom.Compiler");
         snippet.Namespaces.Add(codeDomProvider.GetType().Namespace);
         Assembly assembly = codeDomProvider.GetType().Assembly;
         if (!assembly.GlobalAssemblyCache)
         {
             snippet.ReferencedAssemblies.Add(assembly.Location);
         }
         object[] args = new object[0];
         object   obj2 = snippet.Execute("GenerateCode", args);
         if (obj2 != null)
         {
             this.scintillaTestCodeDom.Text = obj2.ToString();
         }
     }
     catch (Exception exception)
     {
         new ExceptionDialog(exception, "Error Generating CodeDom").ShowDialog();
     }
 }
Пример #3
0
 private void LoadLists()
 {
     this.savefilters.Clear();
     this.cbOutput.Items.Clear();
     this.cbCodeDom.Items.Clear();
     this.savefilters.Add("CS|*.cs");
     this.cbOutput.Items.Add(new OutputClass("C#", new ToCSharpConvertVisitor(), new CSharpOutputVisitor(), 0));
     this.savefilters.Add("VB|*.vb");
     this.cbOutput.Items.Add(new OutputClass("VB", new ToVBNetConvertVisitor(), new VBNetOutputVisitor(), 1));
     this.LoadCodeDomAssemblies();
     foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
     {
         foreach (System.Type type in assembly.GetExportedTypes())
         {
             if (type.IsSubclassOf(typeof(CodeDomProvider)))
             {
                 CodeDomProvider codedomprovider = (CodeDomProvider)assembly.CreateInstance(type.FullName);
                 string          fileExtension   = codedomprovider.FileExtension;
                 string          item            = fileExtension.ToUpper() + "|*." + fileExtension.ToLower();
                 int             savefilterindex = -1;
                 for (int i = 0; i < this.savefilters.Count; i++)
                 {
                     if (this.savefilters[i] == item)
                     {
                         savefilterindex = i;
                         break;
                     }
                 }
                 if (savefilterindex == -1)
                 {
                     savefilterindex = this.savefilters.Count;
                     this.savefilters.Add(item);
                 }
                 OutputClass class2 = new OutputClass(type.Name, codedomprovider, savefilterindex);
                 if (type == typeof(CodeDomCodeProvider))
                 {
                     class2.CanTestCodeDom = true;
                 }
                 else
                 {
                     this.cbCodeDom.Items.Add(class2);
                 }
                 this.cbOutput.Items.Add(class2);
             }
         }
     }
     this.cbOutput.SelectedIndex  = 0;
     this.cbCodeDom.SelectedIndex = 0;
 }
Пример #4
0
        private void Generate(SupportedLanguage language, TextReader inputstream, OutputClass output)
        {
            IParser parser = ParserFactory.CreateParser(language, inputstream);

            parser.Parse();
            if (parser.Errors.Count > 0)
            {
                new ExceptionDialog(null, "Error Parsing Input Code").ShowDialog();
            }
            else if (output.CodeDomProvider != null)
            {
                CodeDomVisitor visitor = new CodeDomVisitor();
                visitor.VisitCompilationUnit(parser.CompilationUnit, null);
                for (int i = visitor.codeCompileUnit.Namespaces.Count - 1; i >= 0; i--)
                {
                    if (visitor.codeCompileUnit.Namespaces[i].Types.Count == 0)
                    {
                        visitor.codeCompileUnit.Namespaces.RemoveAt(i);
                    }
                }
                CodeGeneratorOptions options = new CodeGeneratorOptions();
                options.BlankLinesBetweenMembers = true;
                StringWriter writer = new StringWriter();
                output.CodeDomProvider.GenerateCodeFromCompileUnit(visitor.codeCompileUnit, writer, options);
                this.scintillaOutput.Text = writer.ToString();
                writer.Close();
            }
            else
            {
                AbstractAstTransformer transformer     = output.CreateTransformer();
                List <ISpecial>        currentSpecials = parser.Lexer.SpecialTracker.CurrentSpecials;
                if ((language == SupportedLanguage.CSharp) && (transformer is ToVBNetConvertVisitor))
                {
                    PreprocessingDirective.CSharpToVB(currentSpecials);
                }
                else if ((language == SupportedLanguage.VBNet) && (transformer is ToCSharpConvertVisitor))
                {
                    PreprocessingDirective.VBToCSharp(currentSpecials);
                }
                parser.CompilationUnit.AcceptVisitor(transformer, null);
                IOutputAstVisitor outputVisitor = output.CreatePrettyPrinter();
                using (SpecialNodesInserter.Install(currentSpecials, outputVisitor))
                {
                    outputVisitor.VisitCompilationUnit(parser.CompilationUnit, null);
                }
                this.scintillaOutput.Text = outputVisitor.Text;
            }
        }