diff --git a/CSharpTranslator/antlr3/src/cs2j/CLR/cs2j.cs b/CSharpTranslator/antlr3/src/cs2j/CLR/cs2j.cs index b9ade91..c097c5c 100644 --- a/CSharpTranslator/antlr3/src/cs2j/CLR/cs2j.cs +++ b/CSharpTranslator/antlr3/src/cs2j/CLR/cs2j.cs @@ -290,16 +290,23 @@ namespace RusticiSoftware.Translator.CSharp javaMaker.TraceDestination = Console.Error; javaMaker.Cfg = cfg; - javaMaker.CUMap = new Dictionary(); + javaMaker.CUMap = new Dictionary(); javaMaker.CUKeys = new List(); if (cfg.DebugLevel > 5) Console.Out.WriteLine("Translating {0} to Java", fullName); - JavaMaker.compilation_unit_return java = javaMaker.compilation_unit(); - int saveEmittedCommentTokenIdx = 0; + + 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]; + CommonTree typeAST = javaMaker.CUMap[typeName].Tree; + + for (int j = 0; j < javaMaker.CUMap[typeName].SearchPathKeys.Count; j++) + { + Console.Out.WriteLine("{0} => {1}", javaMaker.CUMap[typeName].SearchPathKeys[j], javaMaker.CUMap[typeName].SearchPathValues[j]); + } string claName = typeName.Substring(typeName.LastIndexOf('.')+1); string nsDir = typeName.Substring(0,typeName.LastIndexOf('.')).Replace('.', Path.DirectorySeparatorChar); diff --git a/CSharpTranslator/antlr3/src/cs2j/CSharp/CommonWalker.cs b/CSharpTranslator/antlr3/src/cs2j/CSharp/CommonWalker.cs index d593048..60dda89 100644 --- a/CSharpTranslator/antlr3/src/cs2j/CSharp/CommonWalker.cs +++ b/CSharpTranslator/antlr3/src/cs2j/CSharp/CommonWalker.cs @@ -76,4 +76,18 @@ namespace RusticiSoftware.Translator.CSharp return buf.ToString(); } } + + // Wraps a compilation unit with its imports search path + public class CUnit { + + public CUnit(CommonTree inTree, List inSearchKeys, List inSearchValues) { + Tree = inTree; + SearchPathKeys = inSearchKeys; + SearchPathValues = inSearchValues; + } + public CommonTree Tree {get; set;} + public List SearchPathKeys {get; set;} + public List SearchPathValues {get; set;} + } + } diff --git a/CSharpTranslator/antlr3/src/cs2j/CSharp/JavaMaker.g b/CSharpTranslator/antlr3/src/cs2j/CSharp/JavaMaker.g index d8ac4d9..d1263e4 100644 --- a/CSharpTranslator/antlr3/src/cs2j/CSharp/JavaMaker.g +++ b/CSharpTranslator/antlr3/src/cs2j/CSharp/JavaMaker.g @@ -19,6 +19,12 @@ options { scope NSContext { int filler; string currentNS; + + // all namespaces in scope, these two lists are actually a map from alias to namespace + // so aliases[i] -> namespaces[i] + // for namespaces without an alias we just map them to themselves + List aliases; + List namespaces; } // A scope to keep track of the current type context @@ -35,11 +41,12 @@ scope TypeContext { @members { + // Since a CS file may comtain multiple top level types (and so generate multiple Java // files) we build a map from type name to AST for each top level type // We also build a lit of type names so that we can maintain the order (so comments // at the end of the file will get included when we emit the java for the last type) - public IDictionary CUMap { get; set; } + public IDictionary CUMap { get; set; } public IList CUKeys { get; set; } protected string ParentNameSpace { @@ -48,6 +55,31 @@ scope TypeContext { } } + protected List CollectAliases { + get { + List ret = new List(); + Object[] nsCtxtArr = $NSContext.ToArray(); + for (int i = nsCtxtArr.Length - 1; i >= 0; i--) { + foreach (string v in ((NSContext_scope)nsCtxtArr[i]).aliases) { + ret.Add(v); + } + } + return ret; + } + } + + protected List CollectNamespaces { + get { + List ret = new List(); + Object[] nsCtxtArr = $NSContext.ToArray(); + for (int i = nsCtxtArr.Length - 1; i >= 0; i--) { + foreach (string v in ((NSContext_scope)nsCtxtArr[i]).namespaces) { + ret.Add(v); + } + } + return ret; + } + } // TREE CONSTRUCTION protected CommonTree mkPayloadList(List payloads) { @@ -204,17 +236,25 @@ compilation_unit scope NSContext; @init { $NSContext::currentNS = ""; + $NSContext::aliases = new List(); + $NSContext::namespaces = new List(); } : namespace_body; namespace_declaration scope NSContext; -: +@init { + $NSContext::currentNS = ""; + $NSContext::aliases = new List(); + $NSContext::namespaces = new List(); +}: 'namespace' qi=qualified_identifier { // extend parent namespace $NSContext::currentNS = this.ParentNameSpace + $qi.thetext; + $NSContext::aliases.Add($NSContext::currentNS); + $NSContext::namespaces.Add($NSContext::currentNS); } namespace_block ';'? ; namespace_block: @@ -231,17 +271,18 @@ using_directive: (using_alias_directive | using_namespace_directive) ; using_alias_directive: - 'using' identifier '=' namespace_or_type_name ';' ; + 'using' identifier '=' namespace_or_type_name ';' {$NSContext::aliases.Add($identifier.text);$NSContext::namespaces.Add($namespace_or_type_name.thetext); } ; using_namespace_directive: - 'using' namespace_name ';' ; + 'using' namespace_name ';' {$NSContext::aliases.Add($namespace_name.thetext);$NSContext::namespaces.Add($namespace_name.thetext); }; namespace_member_declarations: namespace_member_declaration+ ; namespace_member_declaration @init { string ns = $NSContext::currentNS; - bool isCompUnit = false;} + bool isCompUnit = false; +} @after { if (isCompUnit) { - CUMap.Add(ns+"."+$ty.name, $namespace_member_declaration.tree); + CUMap.Add(ns+"."+$ty.name, new CUnit($namespace_member_declaration.tree,CollectAliases,CollectNamespaces)); CUKeys.Add(ns+"."+$ty.name); }; } @@ -265,8 +306,8 @@ type_declaration returns [string name] // Identifiers qualified_identifier returns [string thetext]: i1=identifier { $thetext = $i1.text; } ('.' ip=identifier { $thetext += "." + $ip.text; } )*; -namespace_name - : namespace_or_type_name ; +namespace_name returns [string thetext] + : namespace_or_type_name { $thetext = $namespace_or_type_name.thetext; }; modifiers returns [List modList] @init { diff --git a/CSharpTranslator/antlr3/src/cs2j/cs2j.csproj b/CSharpTranslator/antlr3/src/cs2j/cs2j.csproj index b7a10ff..5b77611 100644 --- a/CSharpTranslator/antlr3/src/cs2j/cs2j.csproj +++ b/CSharpTranslator/antlr3/src/cs2j/cs2j.csproj @@ -39,7 +39,7 @@ DEBUG;TRACE prompt 4 - -warnings -debug 10 -netdir=/Users/keving/gitrepos/cs2j/CS2JLibrary/NetFramework/ -dumpxmls -xmldir=/tmp/xml/se -odir=/tmp/java/se /Users/keving/svnrepos/ScormEngineNet/src/app/ScormEngine.Core/DataHelp/Db2OleDataHelper.cs + -translator-timestamp-files=false -translator-keep-parens=false -netdir=/Users/keving/gitrepos/cs2j/CS2JLibrary/NetFramework/ -dumpxmls -xmldir=/Users/keving/tmp/xml/se -odir=/Users/keving/tmp/java/se/src /Users/keving/svnrepos/ScormEngineNet/src/app/ScormEngine.Core/Logic/Integration/ExternalId.cs pdbonly