mirror of
https://github.com/twiglet/cs2j.git
synced 2025-01-18 13:15:17 +01:00
Pass information between driver and parsers via public properties. Pass isLast flag to Pretty Print to print all remaining comments. Be sure to process file sin the same order as C# source
This commit is contained in:
parent
4a4a905eb5
commit
01c71e3852
@ -258,8 +258,11 @@ namespace RusticiSoftware.Translator.CSharp
|
|||||||
TemplateExtracter templateWalker = new TemplateExtracter(csTree);
|
TemplateExtracter templateWalker = new TemplateExtracter(csTree);
|
||||||
templateWalker.Filename = fullName;
|
templateWalker.Filename = fullName;
|
||||||
templateWalker.TraceDestination = Console.Error;
|
templateWalker.TraceDestination = Console.Error;
|
||||||
|
|
||||||
templateWalker.compilation_unit(cfg, AppEnv);
|
templateWalker.Cfg = cfg;
|
||||||
|
templateWalker.AppEnv = AppEnv;
|
||||||
|
|
||||||
|
templateWalker.compilation_unit();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -274,17 +277,24 @@ namespace RusticiSoftware.Translator.CSharp
|
|||||||
if (csTree != null)
|
if (csTree != null)
|
||||||
{
|
{
|
||||||
// Make java compilation units from C# file
|
// Make java compilation units from C# file
|
||||||
Dictionary<string, CommonTree> cus = new Dictionary<string, CommonTree>();
|
|
||||||
JavaMaker javaMaker = new JavaMaker(csTree);
|
JavaMaker javaMaker = new JavaMaker(csTree);
|
||||||
javaMaker.Filename = fullName;
|
javaMaker.Filename = fullName;
|
||||||
javaMaker.TraceDestination = Console.Error;
|
javaMaker.TraceDestination = Console.Error;
|
||||||
|
|
||||||
JavaMaker.compilation_unit_return java = javaMaker.compilation_unit(cfg, cus);
|
|
||||||
foreach (KeyValuePair<string, CommonTree> package in cus) {
|
|
||||||
Console.WriteLine (package.Key);
|
|
||||||
|
|
||||||
string claName = package.Key.Substring(package.Key.LastIndexOf('.')+1);
|
javaMaker.Cfg = cfg;
|
||||||
string nsDir = package.Key.Substring(0,package.Key.LastIndexOf('.')).Replace('.', Path.DirectorySeparatorChar);
|
javaMaker.CUMap = new Dictionary<string, CommonTree>();
|
||||||
|
javaMaker.CUKeys = new List<string>();
|
||||||
|
|
||||||
|
JavaMaker.compilation_unit_return java = javaMaker.compilation_unit();
|
||||||
|
|
||||||
|
for (int i = 0; i < javaMaker.CUKeys.Count; i++)
|
||||||
|
{
|
||||||
|
string typeName = javaMaker.CUKeys[i];
|
||||||
|
CommonTree typeAST = javaMaker.CUMap[typeName];
|
||||||
|
Console.WriteLine (typeName);
|
||||||
|
|
||||||
|
string claName = typeName.Substring(typeName.LastIndexOf('.')+1);
|
||||||
|
string nsDir = typeName.Substring(0,typeName.LastIndexOf('.')).Replace('.', Path.DirectorySeparatorChar);
|
||||||
|
|
||||||
if (cfg.CheatDir != "")
|
if (cfg.CheatDir != "")
|
||||||
{
|
{
|
||||||
@ -314,12 +324,14 @@ namespace RusticiSoftware.Translator.CSharp
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Translate calls to .Net to calls to Java libraries
|
// Translate calls to .Net to calls to Java libraries
|
||||||
CommonTreeNodeStream javaSyntaxNodes = new CommonTreeNodeStream(package.Value);
|
CommonTreeNodeStream javaSyntaxNodes = new CommonTreeNodeStream(typeAST);
|
||||||
javaSyntaxNodes.TokenStream = csTree.TokenStream;
|
javaSyntaxNodes.TokenStream = csTree.TokenStream;
|
||||||
|
|
||||||
NetMaker netMaker = new NetMaker(javaSyntaxNodes);
|
NetMaker netMaker = new NetMaker(javaSyntaxNodes);
|
||||||
netMaker.Filename = fullName;
|
netMaker.Filename = fullName;
|
||||||
netMaker.TraceDestination = Console.Error;
|
netMaker.TraceDestination = Console.Error;
|
||||||
|
|
||||||
|
netMaker.Cfg = cfg;
|
||||||
|
|
||||||
NetMaker.compilation_unit_return javaCompilationUnit = netMaker.compilation_unit();
|
NetMaker.compilation_unit_return javaCompilationUnit = netMaker.compilation_unit();
|
||||||
|
|
||||||
@ -331,6 +343,9 @@ namespace RusticiSoftware.Translator.CSharp
|
|||||||
outputMaker.Filename = fullName;
|
outputMaker.Filename = fullName;
|
||||||
outputMaker.TraceDestination = Console.Error;
|
outputMaker.TraceDestination = Console.Error;
|
||||||
outputMaker.TemplateLib = templates;
|
outputMaker.TemplateLib = templates;
|
||||||
|
|
||||||
|
outputMaker.Cfg = cfg;
|
||||||
|
outputMaker.IsLast = i == (javaMaker.CUKeys.Count - 1);
|
||||||
|
|
||||||
StreamWriter javaW = new StreamWriter(javaFName);
|
StreamWriter javaW = new StreamWriter(javaFName);
|
||||||
javaW.Write(outputMaker.compilation_unit().ToString());
|
javaW.Write(outputMaker.compilation_unit().ToString());
|
||||||
|
@ -9,7 +9,7 @@ namespace RusticiSoftware.Translator.CSharp
|
|||||||
{
|
{
|
||||||
public class CommonWalker : TreeParser
|
public class CommonWalker : TreeParser
|
||||||
{
|
{
|
||||||
protected CS2JSettings Cfg { get; set; }
|
public CS2JSettings Cfg { get; set; }
|
||||||
public string Filename { get; set; }
|
public string Filename { get; set; }
|
||||||
|
|
||||||
protected CommonWalker(ITreeNodeStream input, RecognizerSharedState state)
|
protected CommonWalker(ITreeNodeStream input, RecognizerSharedState state)
|
||||||
|
@ -30,7 +30,12 @@ scope NSContext {
|
|||||||
|
|
||||||
@members
|
@members
|
||||||
{
|
{
|
||||||
private IDictionary<string, CommonTree> CUs { get; set; }
|
// 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 IList<string> CUKeys { get; set; }
|
||||||
|
|
||||||
protected string ParentNameSpace {
|
protected string ParentNameSpace {
|
||||||
get {
|
get {
|
||||||
@ -45,10 +50,9 @@ scope NSContext {
|
|||||||
|
|
||||||
///////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////
|
||||||
|
|
||||||
compilation_unit[CS2JSettings inCfg, IDictionary<string, CommonTree> inCus /*, DirectoryHT<TypeRepTemplate> inAppEnv*/]
|
compilation_unit
|
||||||
scope NSContext;
|
scope NSContext;
|
||||||
@init {
|
@init {
|
||||||
CUs = inCus;
|
|
||||||
$NSContext::currentNS = "";
|
$NSContext::currentNS = "";
|
||||||
}
|
}
|
||||||
:
|
:
|
||||||
@ -90,7 +94,8 @@ namespace_member_declaration:
|
|||||||
type_declaration_pkg
|
type_declaration_pkg
|
||||||
@init { string ns = $NSContext::currentNS; }
|
@init { string ns = $NSContext::currentNS; }
|
||||||
@after {
|
@after {
|
||||||
CUs.Add(ns+"."+$pkg.name, $type_declaration_pkg.tree);
|
CUMap.Add(ns+"."+$pkg.name, $type_declaration_pkg.tree);
|
||||||
|
CUKeys.Add(ns+"."+$pkg.name);
|
||||||
}
|
}
|
||||||
:
|
:
|
||||||
pkg=type_declaration -> ^(PACKAGE PAYLOAD[ns] $pkg);
|
pkg=type_declaration -> ^(PACKAGE PAYLOAD[ns] $pkg);
|
||||||
|
@ -18,7 +18,12 @@ options {
|
|||||||
|
|
||||||
@members
|
@members
|
||||||
{
|
{
|
||||||
|
|
||||||
|
public bool IsLast { get; set; }
|
||||||
protected int emittedCommentTokenIdx = 0;
|
protected int emittedCommentTokenIdx = 0;
|
||||||
|
|
||||||
|
// Collect all comments from previous position to endIdx
|
||||||
|
// comments are the text from tokens on the Hidden channel
|
||||||
protected List<string> collectComments(int endIdx) {
|
protected List<string> collectComments(int endIdx) {
|
||||||
List<string> rets = new List<string>();
|
List<string> rets = new List<string>();
|
||||||
List<IToken> toks = ((CommonTokenStream)this.GetTreeNodeStream().TokenStream).GetTokens(emittedCommentTokenIdx,endIdx);
|
List<IToken> toks = ((CommonTokenStream)this.GetTreeNodeStream().TokenStream).GetTokens(emittedCommentTokenIdx,endIdx);
|
||||||
@ -30,12 +35,17 @@ options {
|
|||||||
emittedCommentTokenIdx = endIdx+1;
|
emittedCommentTokenIdx = endIdx+1;
|
||||||
return rets;
|
return rets;
|
||||||
}
|
}
|
||||||
|
protected List<string> collectComments() {
|
||||||
|
return collectComments(((CommonTokenStream)this.GetTreeNodeStream().TokenStream).GetTokens().Count - 1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
compilation_unit:
|
compilation_unit:
|
||||||
^(PACKAGE nm=PAYLOAD type_declaration) ->
|
^(PACKAGE nm=PAYLOAD type_declaration) ->
|
||||||
package(now = {DateTime.Now}, includeDate = {true}, packageName = {$nm.text}, comments = {collectComments($type_declaration.start.TokenStartIndex)}, type = {$type_declaration.st});
|
package(now = {DateTime.Now}, includeDate = {true}, packageName = {$nm.text},
|
||||||
|
comments = {collectComments($type_declaration.start.TokenStartIndex)},
|
||||||
|
type = {$type_declaration.st},
|
||||||
|
endComments = {IsLast ? collectComments() : null});
|
||||||
|
|
||||||
type_declaration:
|
type_declaration:
|
||||||
class_declaration
|
class_declaration
|
||||||
|
@ -12,10 +12,6 @@ options {
|
|||||||
|
|
||||||
@members
|
@members
|
||||||
{
|
{
|
||||||
protected bool is_class_modifier()
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
compilation_unit:
|
compilation_unit:
|
||||||
|
@ -37,7 +37,7 @@ scope NSContext {
|
|||||||
|
|
||||||
// This is the environment that we are building, it maps fully qualified type names to their
|
// This is the environment that we are building, it maps fully qualified type names to their
|
||||||
// translation templates
|
// translation templates
|
||||||
protected DirectoryHT<TypeRepTemplate> AppEnv {get; set;}
|
public DirectoryHT<TypeRepTemplate> AppEnv {get; set;}
|
||||||
|
|
||||||
protected UseRepTemplate[] NameSpaceContext {
|
protected UseRepTemplate[] NameSpaceContext {
|
||||||
get {
|
get {
|
||||||
@ -75,11 +75,9 @@ scope NSContext {
|
|||||||
|
|
||||||
///////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////
|
||||||
|
|
||||||
compilation_unit[CS2JSettings inCfg, DirectoryHT<TypeRepTemplate> inAppEnv]
|
compilation_unit
|
||||||
scope NSContext;
|
scope NSContext;
|
||||||
@init{
|
@init{
|
||||||
Cfg = inCfg;
|
|
||||||
AppEnv = inAppEnv;
|
|
||||||
// For initial, file level scope
|
// For initial, file level scope
|
||||||
$NSContext::nss = new List<UseRepTemplate>();
|
$NSContext::nss = new List<UseRepTemplate>();
|
||||||
$NSContext::currentNS = "";
|
$NSContext::currentNS = "";
|
||||||
|
@ -53,7 +53,9 @@ namespace RusticiSoftware.Translator.CSharp
|
|||||||
nodes.TokenStream = tokens;
|
nodes.TokenStream = tokens;
|
||||||
|
|
||||||
TemplateExtracter templateWalker = new TemplateExtracter(nodes);
|
TemplateExtracter templateWalker = new TemplateExtracter(nodes);
|
||||||
templateWalker.compilation_unit(new CS2JSettings(), new DirectoryHT<TypeRepTemplate>());
|
templateWalker.Cfg = new CS2JSettings();
|
||||||
|
templateWalker.AppEnv = new DirectoryHT<TypeRepTemplate>();
|
||||||
|
templateWalker.compilation_unit();
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,11 +16,13 @@ itsmine(now, includeDate) ::= <<
|
|||||||
|
|
||||||
>>
|
>>
|
||||||
|
|
||||||
package(now, includeDate, packageName, comments, type) ::= <<
|
package(now, includeDate, packageName, comments, type, endComments) ::= <<
|
||||||
<itsmine(now=now,includeDate=includeDate)>
|
<itsmine(now=now,includeDate=includeDate)>
|
||||||
package <packageName>;
|
package <packageName>;
|
||||||
|
|
||||||
<comments; separator="\n">
|
<comments; separator="\n">
|
||||||
|
|
||||||
<type>
|
<type>
|
||||||
|
|
||||||
|
<endComments; separator="\n">
|
||||||
>>
|
>>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user