public CommonTree parseString(string startRule, string inStr, bool includeNet) { if (Cfg.Verbosity > 5) { Console.WriteLine("Parsing fragment "); } ICharStream input = new ANTLRStringStream(inStr); PreProcessor lex = new PreProcessor(); lex.AddDefine(Cfg.MacroDefines); lex.CharStream = input; lex.TraceDestination = Console.Error; CommonTokenStream tokens = new CommonTokenStream(lex); csParser p = new csParser(tokens); p.TraceDestination = Console.Error; p.IsJavaish = true; // Try and call a rule like CSParser.namespace_body() // Use reflection to find the rule to use. MethodInfo mi = p.GetType().GetMethod(startRule); if (mi == null) { throw new Exception("Could not find start rule " + startRule + " in csParser"); } ParserRuleReturnScope csRet = (ParserRuleReturnScope)mi.Invoke(p, new object[0]); CommonTreeNodeStream csTreeStream = new CommonTreeNodeStream(csRet.Tree); csTreeStream.TokenStream = tokens; JavaMaker javaMaker = new JavaMaker(csTreeStream); javaMaker.TraceDestination = Console.Error; javaMaker.Cfg = Cfg; javaMaker.IsJavaish = true; javaMaker.InitParser(); // Try and call a rule like CSParser.namespace_body() // Use reflection to find the rule to use. mi = javaMaker.GetType().GetMethod(startRule); if (mi == null) { throw new Exception("Could not find start rule " + startRule + " in javaMaker"); } TreeRuleReturnScope javaSyntaxRet = (TreeRuleReturnScope)mi.Invoke(javaMaker, new object[0]); CommonTree retAST = (CommonTree)javaSyntaxRet.Tree; if (includeNet) { CommonTreeNodeStream javaSyntaxNodes = new CommonTreeNodeStream(retAST); javaSyntaxNodes.TokenStream = csTreeStream.TokenStream; NetMaker netMaker = new NetMaker(javaSyntaxNodes); netMaker.TraceDestination = Console.Error; netMaker.Cfg = Cfg; netMaker.AppEnv = AppEnv; netMaker.IsJavaish = true; netMaker.InitParser(); retAST = (CommonTree)netMaker.class_member_declarations().Tree; // snaffle additional imports this.AddToImports(netMaker.Imports); } return(retAST); }
// 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(""); }
// 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().Replace("//CS2J:Java code:", "")); 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(""); }
public CommonTree parseString(string startRule, string inStr, bool includeNet) { if (Cfg.Verbosity > 5) Console.WriteLine("Parsing fragment "); ICharStream input = new ANTLRStringStream(inStr); PreProcessor lex = new PreProcessor(); lex.AddDefine(Cfg.MacroDefines); lex.CharStream = input; lex.TraceDestination = Console.Error; CommonTokenStream tokens = new CommonTokenStream(lex); csParser p = new csParser(tokens); p.TraceDestination = Console.Error; p.IsJavaish = true; // Try and call a rule like CSParser.namespace_body() // Use reflection to find the rule to use. MethodInfo mi = p.GetType().GetMethod(startRule); if (mi == null) { throw new Exception("Could not find start rule " + startRule + " in csParser"); } ParserRuleReturnScope csRet = (ParserRuleReturnScope) mi.Invoke(p, new object[0]); CommonTreeNodeStream csTreeStream = new CommonTreeNodeStream(csRet.Tree); csTreeStream.TokenStream = tokens; JavaMaker javaMaker = new JavaMaker(csTreeStream); javaMaker.TraceDestination = Console.Error; javaMaker.Cfg = Cfg; javaMaker.IsJavaish = true; javaMaker.InitParser(); // Try and call a rule like CSParser.namespace_body() // Use reflection to find the rule to use. mi = javaMaker.GetType().GetMethod(startRule); if (mi == null) { throw new Exception("Could not find start rule " + startRule + " in javaMaker"); } TreeRuleReturnScope javaSyntaxRet = (TreeRuleReturnScope) mi.Invoke(javaMaker, new object[0]); CommonTree retAST = (CommonTree)javaSyntaxRet.Tree; if (includeNet) { CommonTreeNodeStream javaSyntaxNodes = new CommonTreeNodeStream(retAST); javaSyntaxNodes.TokenStream = csTreeStream.TokenStream; NetMaker netMaker = new NetMaker(javaSyntaxNodes); netMaker.TraceDestination = Console.Error; netMaker.Cfg = Cfg; netMaker.AppEnv = AppEnv; netMaker.IsJavaish = true; netMaker.InitParser(); retAST = (CommonTree)netMaker.class_member_declarations().Tree; // snaffle additional imports this.AddToImports(netMaker.Imports); } return retAST; }