예제 #1
0
파일: CS2JMain.cs 프로젝트: exaphaser/cs2j
       public static void emitPartialType(string name, ClassDescriptorSerialized serTy)
       {
          JavaPrettyPrint outputMaker = new JavaPrettyPrint(null);
          outputMaker.Filename = serTy.FileName;
          outputMaker.TraceDestination = Console.Error;
          outputMaker.TemplateLib = templates;
          outputMaker.Cfg = cfg;

          StringTemplate pkgST = outputMaker.emitPackage(serTy);

          if (cfg.DebugLevel >= 1) Console.Out.WriteLine("Writing out {0}", serTy.FileName);
          StreamWriter javaW = new StreamWriter(serTy.FileName);
          javaW.Write(pkgST.ToString());
          javaW.Close();
       }
예제 #2
0
파일: CS2JMain.cs 프로젝트: exaphaser/cs2j
        // Here's where we do the real work...		
        public static void translateFile(string fullName)
        {
            long startTime = DateTime.Now.Ticks;
            if (cfg.DebugLevel > 3) Console.Out.WriteLine("Translating file {0}", fullName);
            if (cfg.DebugLevel > 5) Console.Out.WriteLine("Parsing file {0}", fullName);
            CommonTreeNodeStream csTree = parseFile(fullName);
            if (cfg.DumpCSharp && csTree != null)
            {
                AntlrUtils.DumpNodesFlat(csTree, "C Sharp Parse Tree");
                csTree.Reset();
            }

            if (csTree != null)
            {
                // Make java compilation units from C# file
                JavaMaker javaMaker = new JavaMaker(csTree);
                javaMaker.Filename = fullName;
                javaMaker.TraceDestination = Console.Error;

                javaMaker.Cfg = cfg;
                javaMaker.CUMap = new Dictionary<string, CUnit>();
                javaMaker.CUKeys = new List<string>();
                javaMaker.IsJavaish = cfg.InternalIsJavaish;
	    
		javaMaker.EnumXmlWriter = enumXmlWriter;

                if (cfg.DebugLevel >= 1) Console.Out.WriteLine("Translating {0} to Java", fullName);
                
                javaMaker.compilation_unit();
                
                int saveEmittedCommentTokenIdx = 0;
                for (int i = 0; i < javaMaker.CUKeys.Count; i++)
                {
                    string typeName = javaMaker.CUKeys[i];
                    CommonTree typeAST = javaMaker.CUMap[typeName].Tree;

                    string claName = typeName.Substring(typeName.LastIndexOf('.')+1); 
                    string nsDir = typeName.LastIndexOf('.') >= 0 ? typeName.Substring(0,typeName.LastIndexOf('.')).Replace('.', Path.DirectorySeparatorChar) : "";
                    
                    if (cfg.CheatDir != "")
                    {
                        String ignoreMarker = Path.Combine(cfg.CheatDir, Path.Combine(nsDir, claName + ".none"));
                        if (File.Exists(ignoreMarker))
                        {
                            // Don't generate this class
                            continue;
                        }
                    }
                    // Make sure parent directory exists
                    String javaFDir = Path.Combine(cfg.OutDir, nsDir);
                    String javaFName = Path.Combine(javaFDir, claName + ".java");
                    if (!Directory.Exists(javaFDir))
                    {
                        Directory.CreateDirectory(javaFDir);
                    }
                    if (cfg.CheatDir != "")
                    {
                        String cheatFile = Path.Combine(cfg.CheatDir, Path.Combine(nsDir, claName + ".java"));
                        if (File.Exists(cheatFile))
                        {
                            // the old switcheroo
                            File.Copy(cheatFile, javaFName,true);
                            continue;
                        }
                    }

                    // Translate calls to .Net to calls to Java libraries
                    CommonTreeNodeStream javaSyntaxNodes = new CommonTreeNodeStream(typeAST);            
                    if (cfg.DumpJavaSyntax && javaSyntaxNodes != null)
                    {
                        AntlrUtils.DumpNodesFlat(javaSyntaxNodes, "Java Syntax Parse Tree for " + claName);
                        javaSyntaxNodes.Reset();    
                    }
                    javaSyntaxNodes.TokenStream = csTree.TokenStream;
                    
                    NetMaker netMaker = new NetMaker(javaSyntaxNodes);
                    netMaker.Filename = fullName;
                    netMaker.TraceDestination = Console.Error;

                    netMaker.Cfg = cfg;
                    netMaker.AppEnv = AppEnv;

                    netMaker.SearchPath = javaMaker.CUMap[typeName].SearchPath;
                    netMaker.AliasKeys = javaMaker.CUMap[typeName].NameSpaceAliasKeys;
                    netMaker.AliasNamespaces = javaMaker.CUMap[typeName].NameSpaceAliasValues;

                    netMaker.IsJavaish = cfg.InternalIsJavaish;
                    netMaker.Imports = new Set<String>();
                    netMaker.AddToImports(javaMaker.Imports);

                    if (cfg.DebugLevel > 5) Console.Out.WriteLine("Translating {0} Net Calls to Java", javaFName);
                    NetMaker.compilation_unit_return javaCompilationUnit = netMaker.compilation_unit();

                    CommonTreeNodeStream javaCompilationUnitNodes = new CommonTreeNodeStream(javaCompilationUnit.Tree);            
                    javaCompilationUnitNodes.TokenStream = csTree.TokenStream;
                    
                    if (cfg.DumpJava && javaCompilationUnitNodes != null)
                    {
                        AntlrUtils.DumpNodesFlat(javaCompilationUnitNodes, "Final Java Parse Tree for " + claName);
                        javaCompilationUnitNodes.Reset();    
                    }
                    // Pretty print java parse tree as text
                    JavaPrettyPrint outputMaker = new JavaPrettyPrint(javaCompilationUnitNodes);
                    outputMaker.Filename = fullName;
                    outputMaker.TraceDestination = Console.Error;
                    outputMaker.TemplateLib = templates;

                    outputMaker.Cfg = cfg;
                    outputMaker.EmittedCommentTokenIdx = saveEmittedCommentTokenIdx;
                    bool isPartial = javaMaker.CUMap[typeName].IsPartial;
                    if (isPartial)
                    {
                       if (!partialTypes.ContainsKey(typeName))
                       {
                          partialTypes[typeName] = new ClassDescriptorSerialized(claName);
                          partialTypes[typeName].FileName = javaFName;
                       }
                       outputMaker.PartialDescriptor = partialTypes[typeName];
                    }

                    outputMaker.IsLast = i == (javaMaker.CUKeys.Count - 1);
                    
                    if (!isPartial)
                    {
                       if (cfg.DebugLevel >= 1) Console.Out.WriteLine("Writing out {0}", javaFName);
                       StreamWriter javaW = new StreamWriter(javaFName);
                       javaW.Write(outputMaker.compilation_unit().ToString());
                       javaW.Close();
                    }
                    else
                    {
                       // fill out partialTypes[typeName]
                       outputMaker.compilation_unit();
                    }
                    saveEmittedCommentTokenIdx = outputMaker.EmittedCommentTokenIdx;
                }
            }

            double elapsedTime = ((DateTime.Now.Ticks - startTime) / TimeSpan.TicksPerMillisecond) / 1000.0;
            System.Console.Out.WriteLine("Processed {0} in: {1} seconds.", fullName, elapsedTime);
            System.Console.Out.WriteLine("");
            System.Console.Out.WriteLine("");
        }