public static void show_SourceCode_Ast_InTreeView(this TreeView treeView, string sourceCodeFile)
 {
     treeView.invokeOnThread(
         () =>
     {
         var ast = new Ast_CSharp(sourceCodeFile);
         treeView.show_SourceCode_Ast_InTreeView(ast.CompilationUnit);
     });
 }
 public void update(string sourceCode)
 {
     if (enabled)
     {
         var ast = new Ast_CSharp(sourceCode);
         ast_TreeView.show_Ast(ast);
         types_TreeView.show_List(ast.AstDetails.Types, "Text");
         usingDeclarations_TreeView.show_List(ast.AstDetails.UsingDeclarations, "Text");
         methods_TreeView.show_List(ast.AstDetails.Methods, "Text");
         fields_TreeView.show_List(ast.AstDetails.Fields, "Text");
         properties_TreeView.show_List(ast.AstDetails.Properties, "Text");
         comments_TreeView.show_List(ast.AstDetails.Comments, "Text");
         errors_TextBox.set_Text(ast.Errors);
     }
 }
 public void update(string sourceCode)
 {
     if (enabled)
     {
         var ast = new Ast_CSharp(sourceCode);
         ast_TreeView.show_Ast(ast);
         types_TreeView.show_List(ast.AstDetails.Types, "Text");
         usingDeclarations_TreeView.show_List(ast.AstDetails.UsingDeclarations, "Text");
         methods_TreeView.show_List(ast.AstDetails.Methods, "Text");
         fields_TreeView.show_List(ast.AstDetails.Fields, "Text");
         properties_TreeView.show_List(ast.AstDetails.Properties, "Text");
         comments_TreeView.show_List(ast.AstDetails.Comments, "Text");
         errors_TextBox.set_Text(ast.Errors);
     }
 }
 public void compileSourceCode(string sourceCode)
 {
     if (sourceCode.valid().isFalse())
     {
         "in CSharp_FastCompiler,compileSourceCode, provided sourceCode code was empty".error();
         if (onCompileFail != null)
         {
             onCompileFail();
         }
     }
     else
     {
         // we need to do make sure we include any extra references included in the code
         var astCSharp = new Ast_CSharp(sourceCode);
         mapCodeO2References(astCSharp);
         compileSourceCode(sourceCode, false);
     }
 }
 public static void show_Ast(this TreeView treeView, Ast_CSharp ast)
 {
     treeView.invokeOnThread(
         () => treeView.show_SourceCode_Ast_InTreeView(ast.CompilationUnit));
 }
		public void updateView(string sourceCode)
		{	
			AstCSharp 		= new Ast_CSharp(sourceCode);
			AstDetails 		= AstCSharp.AstDetails; 
			
			ast_TreeView.beginUpdate();
			ast_TreeView.show_Ast (AstCSharp);
			if (this.AutoExpand_AstTreeView)
				ast_TreeView.expandAll();
			ast_TreeView.endUpdate();
			
			types_TreeView				.show_List(AstDetails.Types, "Text");
			usingDeclarations_TreeView	.show_List(AstDetails.UsingDeclarations,"Text");
			methods_TreeView			.show_List(AstDetails.Methods,"Text");
			fields_TreeView				.show_List(AstDetails.Fields,"Text");
			properties_TreeView			.show_List(AstDetails.Properties,"Text");
			comments_TreeView			.show_List(AstDetails.Comments,"Text");
			errors_RichTextBox			.set_Text (AstCSharp.Errors);		
			
			rewritenCSharpCode_SourceCodeEditor.setDocumentContents (AstDetails.CSharpCode, ".cs");
			rewritenVBNet_SourceCodeEditor		.setDocumentContents(AstDetails.VBNetCode , ".vb");								
		}
        public void mapCodeO2References(Ast_CSharp astCSharp)
        {            
            bool onlyAddReferencedAssemblies = false;
			bool addExtraLocalO2ScriptFiles = true;
//            generateDebugSymbols = false; // default to not generating debug symbols and creating the assembly only in memory
            ExtraSourceCodeFilesToCompile = new List<string>();                                
        	var compilationUnit = astCSharp.CompilationUnit;
            ReferencedAssemblies = new List<string>();
            var FilesToDownload = new List<string>();

        	var currentUsingDeclarations = new List<string>();
        	foreach(var usingDeclaration in astCSharp.AstDetails.UsingDeclarations)
        		currentUsingDeclarations.Add(usingDeclaration.Text);
        	
        	
            foreach (var comment in astCSharp.AstDetails.Comments)
            {
                comment.Text.eq("O2Tag_OnlyAddReferencedAssemblies", () => onlyAddReferencedAssemblies = true);
				comment.Text.eq("O2Tag_DontAddExtraO2Files", () => addExtraLocalO2ScriptFiles = false);
                comment.Text.starts("using ", false, value => astCSharp.CompilationUnit.add_Using(value));
                comment.Text.starts(new [] {"ref ", "O2Ref:"}, false,  value => ReferencedAssemblies.Add(value));
                comment.Text.starts(new[] { "Download:","download:", "O2Download:" }, false, value => FilesToDownload.Add(value));
                comment.Text.starts(new[] { "include", "file ", "O2File:" }, false, value => ExtraSourceCodeFilesToCompile.Add(value));
                comment.Text.starts(new[] { "dir ", "O2Dir:" }, false, value => ExtraSourceCodeFilesToCompile.AddRange(value.files("*.cs",true))); 
               
                comment.Text.starts(new[] {"O2:debugSymbols",
                                        "generateDebugSymbols", 
                                        "debugSymbols"}, true, (value) => generateDebugSymbols = true);
                comment.Text.eq("StaThread", () => { ExecuteInStaThread = true; });
                comment.Text.eq("MtaThread", () => { ExecuteInMtaThread = true; });
                comment.Text.eq("WorkOffline", () => { WorkOffline = true; });
                comment.Text.eq("ClearAssembliesCheckedIfExists", () => { O2.Kernel.CodeUtils.O2Svn.clear_AssembliesCheckedIfExists(); });  
            }

			if (addExtraLocalO2ScriptFiles)
				ExtraSourceCodeFilesToCompile.Add(EXTRA_EXTENSION_METHODS_FILE); // add this one by default to make it easy to add new extension methods to the O2 Scripts
            //resolve location of ExtraSourceCodeFilesToCompile

            resolveFileLocationsOfExtraSourceCodeFilesToCompile();                        
                
            //use the same technique to download files that are needed for this script (for example *.zip files or other unmanaged/support files)
            CompileEngine.tryToResolveReferencesForCompilation(FilesToDownload, WorkOffline);            

            if (onlyAddReferencedAssemblies.isFalse())
            {
                foreach (var defaultRefAssembly in getDefaultReferencedAssemblies())
                    if (ReferencedAssemblies.Contains(defaultRefAssembly).isFalse())
                        ReferencedAssemblies.add(defaultRefAssembly);            
                foreach (var usingStatement in getDefaultUsingStatements())
                    if (false == currentUsingDeclarations.Contains(usingStatement))
                        compilationUnit.add_Using(usingStatement);
            }

			//make sure the referenced assemblies are in the current execution directory
			CompileEngine.tryToResolveReferencesForCompilation(ReferencedAssemblies, WorkOffline);            

        }
        public string tryToCreateCSharpCodeWith_Class_Method_WithMethodText(string code)
        {
            if (code.empty())
                return null;
			code = code.line();	// make sure there is an empty line at the end            
                      
            try
            {
                // handle special incudes in source code
                foreach(var originalLine in code.lines())
                    originalLine.starts("//include", (includeText) => 
                        {
                            if (includeText.fileExists())
                                code = code.Replace(originalLine, originalLine.line().add(includeText.contents()));
                        });  
            	var snippetParser = new SnippetParser(SupportedLanguage.CSharp);
                
                var parsedCode = snippetParser.Parse(code);
				AstErrors = snippetParser.errors();
                CompilationUnit = new CompilationUnit();

                if (parsedCode is BlockStatement || parsedCode is CompilationUnit)
                {
                    Ast_CSharp astCSharp;
                    if (parsedCode is BlockStatement)
                    {
                        // map parsedCode into a new type and method 

                        var blockStatement = (BlockStatement)parsedCode;
                        CompilationUnit.add_Type(default_TypeName)
                            .add_Method(default_MethodName, InvocationParameters, blockStatement);

                        astCSharp = new Ast_CSharp(CompilationUnit, snippetParser.Specials);
                        //astCSharp.AstDetails.mapSpecials();

                        // add references included in the original source code file
                        mapCodeO2References(astCSharp);

                        CreatedFromSnipptet = true;
                    }
                    else
                    {
                        CompilationUnit = (CompilationUnit)parsedCode;
                        if (CompilationUnit.Children.Count == 0)
                            return null;

                        astCSharp = new Ast_CSharp(CompilationUnit, snippetParser.Specials);
                        // add the comments from the original code                        

                        mapCodeO2References(astCSharp);
                        CreatedFromSnipptet = false;
                    }
                    
                    // create sourceCode using Ast_CSharp & AstDetails		
                    if(CompilationUnit.Children.Count > 0)
                    {                        
                        // reset the astCSharp.AstDetails object        	
                        astCSharp.mapAstDetails();
                        // add the comments from the original code
                        astCSharp.ExtraSpecials.AddRange(snippetParser.Specials);	                 

	                    SourceCode = astCSharp.AstDetails.CSharpCode;

                        //once we have the created SourceCode we need to create a new AST with it
                        var tempAstDetails = new Ast_CSharp(SourceCode).AstDetails;
                        //note we should try to add back the specials here (so that comments make it to the generated code
                        AstDetails = tempAstDetails;
	                    DebugMode.ifDebug("Ast parsing was OK");
	                    this.invoke(onAstOK);
	                    return SourceCode;
                    }
                }            
            }
            catch (Exception ex)
            {                            	
				DebugMode.ifError("in createCSharpCodeWith_Class_Method_WithMethodText:{0}", ex.Message);                
            }      			
			return null;                
        }        
		public void compileSourceCode(string sourceCode)
        {
            if (sourceCode.valid().isFalse())
            {
                "in CSharp_FastCompiler,compileSourceCode, provided sourceCode code was empty".error();
                if (onCompileFail !=null)
                    onCompileFail();
            }
            else
            {
				if (getCachedAssemblyForCode_and_RaiseEvents(sourceCode))
					return;
                // we need to do make sure we include any extra references included in the code
                var astCSharp = new Ast_CSharp(sourceCode);
                mapCodeO2References(astCSharp);
                compileSourceCode(sourceCode, false);
            }
       	}
        public void mapCodeO2References(Ast_CSharp astCSharp)
        {
            bool onlyAddReferencedAssemblies = false;

//            generateDebugSymbols = false; // default to not generating debug symbols and creating the assembly only in memory
            ExtraSourceCodeFilesToCompile = new List <string>();
            ExtraSourceCodeFilesToCompile.Add(EXTRA_EXTENSION_METHODS_FILE); // add this one by default to make it easy to add new extension methods to the O2 Scripts
            var compilationUnit = astCSharp.CompilationUnit;

            ReferencedAssemblies = new List <string>();
            var FilesToDownload = new List <string>();

            var currentUsingDeclarations = new List <string>();

            foreach (var usingDeclaration in astCSharp.AstDetails.UsingDeclarations)
            {
                currentUsingDeclarations.Add(usingDeclaration.Text);
            }


            foreach (var comment in astCSharp.AstDetails.Comments)
            {
                comment.Text.eq("O2Tag_OnlyAddReferencedAssemblies", () => onlyAddReferencedAssemblies = true);
                comment.Text.starts("using ", false, value => astCSharp.CompilationUnit.add_Using(value));
                comment.Text.starts(new [] { "ref ", "O2Ref:" }, false, value => ReferencedAssemblies.Add(value));
                comment.Text.starts(new[] { "Download:", "download:", "O2Download:" }, false, value => FilesToDownload.Add(value));
                comment.Text.starts(new[] { "include", "file ", "O2File:" }, false, value => ExtraSourceCodeFilesToCompile.Add(value));
                comment.Text.starts(new[] { "dir ", "O2Dir:" }, false, value => ExtraSourceCodeFilesToCompile.AddRange(value.files("*.cs", true)));

                comment.Text.starts(new[] { "O2:debugSymbols",
                                            "generateDebugSymbols",
                                            "debugSymbols" }, true, (value) => generateDebugSymbols = true);
                comment.Text.eq("StaThread", () => { ExecuteInStaThread = true; });
                comment.Text.eq("MtaThread", () => { ExecuteInMtaThread = true; });
                comment.Text.eq("WorkOffline", () => { WorkOffline = true; });
                comment.Text.eq("ClearAssembliesCheckedIfExists", () => { O2.Kernel.CodeUtils.O2Svn.clear_AssembliesCheckedIfExists(); });
            }

            //resolve location of ExtraSourceCodeFilesToCompile

            resolveFileLocationsOfExtraSourceCodeFilesToCompile();

            //make sure the referenced assemblies are in the current execution directory
            CompileEngine.tryToResolveReferencesForCompilation(ReferencedAssemblies, WorkOffline);
            //use the same technique to download files that are needed for this script (for example *.zip files or other unmanaged/support files)
            CompileEngine.tryToResolveReferencesForCompilation(FilesToDownload, WorkOffline);

            if (onlyAddReferencedAssemblies.isFalse())
            {
                foreach (var defaultRefAssembly in getDefaultReferencedAssemblies())
                {
                    if (ReferencedAssemblies.Contains(defaultRefAssembly).isFalse())
                    {
                        ReferencedAssemblies.add(defaultRefAssembly);
                    }
                }
                foreach (var usingStatement in getDefaultUsingStatements())
                {
                    if (false == currentUsingDeclarations.Contains(usingStatement))
                    {
                        compilationUnit.add_Using(usingStatement);
                    }
                }
            }
        }
        public string tryToCreateCSharpCodeWith_Class_Method_WithMethodText(string code)
        {
            if (code.empty())
            {
                return(null);
            }
            code = code.line();                 // make sure there is an empty line at the end

            try
            {
                // handle special incudes in source code
                foreach (var originalLine in code.lines())
                {
                    originalLine.starts("//include", (includeText) =>
                    {
                        if (includeText.fileExists())
                        {
                            code = code.Replace(originalLine, originalLine.line().add(includeText.contents()));
                        }
                    });
                }
                var snippetParser = new SnippetParser(SupportedLanguage.CSharp);

                var parsedCode = snippetParser.Parse(code);
                AstErrors       = snippetParser.errors();
                CompilationUnit = new CompilationUnit();

                if (parsedCode is BlockStatement || parsedCode is CompilationUnit)
                {
                    Ast_CSharp astCSharp;
                    if (parsedCode is BlockStatement)
                    {
                        // map parsedCode into a new type and method

                        var blockStatement = (BlockStatement)parsedCode;
                        CompilationUnit.add_Type(default_TypeName)
                        .add_Method(default_MethodName, InvocationParameters, blockStatement);

                        astCSharp = new Ast_CSharp(CompilationUnit, snippetParser.Specials);
                        //astCSharp.AstDetails.mapSpecials();

                        // add references included in the original source code file
                        mapCodeO2References(astCSharp);

                        CreatedFromSnipptet = true;
                    }
                    else
                    {
                        CompilationUnit = (CompilationUnit)parsedCode;
                        if (CompilationUnit.Children.Count == 0)
                        {
                            return(null);
                        }

                        astCSharp = new Ast_CSharp(CompilationUnit, snippetParser.Specials);
                        // add the comments from the original code

                        mapCodeO2References(astCSharp);
                        CreatedFromSnipptet = false;
                    }

                    // create sourceCode using Ast_CSharp & AstDetails
                    if (CompilationUnit.Children.Count > 0)
                    {
                        // reset the astCSharp.AstDetails object
                        astCSharp.mapAstDetails();
                        // add the comments from the original code
                        astCSharp.ExtraSpecials.AddRange(snippetParser.Specials);

                        SourceCode = astCSharp.AstDetails.CSharpCode;

                        //once we have the created SourceCode we need to create a new AST with it
                        var tempAstDetails = new Ast_CSharp(SourceCode).AstDetails;
                        //note we should try to add back the specials here (so that comments make it to the generated code
                        AstDetails = tempAstDetails;
                        DebugMode.ifDebug("Ast parsing was OK");
                        this.invoke(onAstOK);
                        return(SourceCode);
                    }
                }
            }
            catch (Exception ex)
            {
                DebugMode.ifError("in createCSharpCodeWith_Class_Method_WithMethodText:{0}", ex.Message);
            }
            return(null);
        }