mirror of
https://github.com/twiglet/cs2j.git
synced 2025-01-18 13:15:17 +01:00
Collect search path in JavaMaker
This commit is contained in:
parent
a8fae67dc0
commit
b336ae4ffe
@ -290,16 +290,23 @@ namespace RusticiSoftware.Translator.CSharp
|
||||
javaMaker.TraceDestination = Console.Error;
|
||||
|
||||
javaMaker.Cfg = cfg;
|
||||
javaMaker.CUMap = new Dictionary<string, CommonTree>();
|
||||
javaMaker.CUMap = new Dictionary<string, CUnit>();
|
||||
javaMaker.CUKeys = new List<string>();
|
||||
|
||||
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);
|
||||
|
@ -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<string> inSearchKeys, List<string> inSearchValues) {
|
||||
Tree = inTree;
|
||||
SearchPathKeys = inSearchKeys;
|
||||
SearchPathValues = inSearchValues;
|
||||
}
|
||||
public CommonTree Tree {get; set;}
|
||||
public List<string> SearchPathKeys {get; set;}
|
||||
public List<string> SearchPathValues {get; set;}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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<string> aliases;
|
||||
List<string> 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<string, CommonTree> CUMap { get; set; }
|
||||
public IDictionary<string, CUnit> CUMap { get; set; }
|
||||
public IList<string> CUKeys { get; set; }
|
||||
|
||||
protected string ParentNameSpace {
|
||||
@ -48,6 +55,31 @@ scope TypeContext {
|
||||
}
|
||||
}
|
||||
|
||||
protected List<string> CollectAliases {
|
||||
get {
|
||||
List<string> ret = new List<string>();
|
||||
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<string> CollectNamespaces {
|
||||
get {
|
||||
List<string> ret = new List<string>();
|
||||
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<string> payloads) {
|
||||
@ -204,17 +236,25 @@ compilation_unit
|
||||
scope NSContext;
|
||||
@init {
|
||||
$NSContext::currentNS = "";
|
||||
$NSContext::aliases = new List<string>();
|
||||
$NSContext::namespaces = new List<string>();
|
||||
}
|
||||
:
|
||||
namespace_body;
|
||||
|
||||
namespace_declaration
|
||||
scope NSContext;
|
||||
:
|
||||
@init {
|
||||
$NSContext::currentNS = "";
|
||||
$NSContext::aliases = new List<string>();
|
||||
$NSContext::namespaces = new List<string>();
|
||||
}:
|
||||
'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<string> modList]
|
||||
@init {
|
||||
|
@ -39,7 +39,7 @@
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<Commandlineparameters>-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</Commandlineparameters>
|
||||
<Commandlineparameters> -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</Commandlineparameters>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
|
Loading…
x
Reference in New Issue
Block a user