1
0
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:
Kevin Glynn 2010-11-15 11:47:32 +01:00
parent 4a4a905eb5
commit 01c71e3852
8 changed files with 55 additions and 27 deletions

View File

@ -258,8 +258,11 @@ namespace RusticiSoftware.Translator.CSharp
TemplateExtracter templateWalker = new TemplateExtracter(csTree);
templateWalker.Filename = fullName;
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)
{
// Make java compilation units from C# file
Dictionary<string, CommonTree> cus = new Dictionary<string, CommonTree>();
JavaMaker javaMaker = new JavaMaker(csTree);
javaMaker.Filename = fullName;
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);
string nsDir = package.Key.Substring(0,package.Key.LastIndexOf('.')).Replace('.', Path.DirectorySeparatorChar);
javaMaker.Cfg = cfg;
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 != "")
{
@ -314,12 +324,14 @@ namespace RusticiSoftware.Translator.CSharp
}
// Translate calls to .Net to calls to Java libraries
CommonTreeNodeStream javaSyntaxNodes = new CommonTreeNodeStream(package.Value);
CommonTreeNodeStream javaSyntaxNodes = new CommonTreeNodeStream(typeAST);
javaSyntaxNodes.TokenStream = csTree.TokenStream;
NetMaker netMaker = new NetMaker(javaSyntaxNodes);
netMaker.Filename = fullName;
netMaker.TraceDestination = Console.Error;
netMaker.Cfg = cfg;
NetMaker.compilation_unit_return javaCompilationUnit = netMaker.compilation_unit();
@ -331,6 +343,9 @@ namespace RusticiSoftware.Translator.CSharp
outputMaker.Filename = fullName;
outputMaker.TraceDestination = Console.Error;
outputMaker.TemplateLib = templates;
outputMaker.Cfg = cfg;
outputMaker.IsLast = i == (javaMaker.CUKeys.Count - 1);
StreamWriter javaW = new StreamWriter(javaFName);
javaW.Write(outputMaker.compilation_unit().ToString());

View File

@ -9,7 +9,7 @@ namespace RusticiSoftware.Translator.CSharp
{
public class CommonWalker : TreeParser
{
protected CS2JSettings Cfg { get; set; }
public CS2JSettings Cfg { get; set; }
public string Filename { get; set; }
protected CommonWalker(ITreeNodeStream input, RecognizerSharedState state)

View File

@ -30,7 +30,12 @@ scope NSContext {
@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 {
get {
@ -45,10 +50,9 @@ scope NSContext {
///////////////////////////////////////////////////////
compilation_unit[CS2JSettings inCfg, IDictionary<string, CommonTree> inCus /*, DirectoryHT<TypeRepTemplate> inAppEnv*/]
compilation_unit
scope NSContext;
@init {
CUs = inCus;
$NSContext::currentNS = "";
}
:
@ -90,7 +94,8 @@ namespace_member_declaration:
type_declaration_pkg
@init { string ns = $NSContext::currentNS; }
@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);

View File

@ -18,7 +18,12 @@ options {
@members
{
public bool IsLast { get; set; }
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) {
List<string> rets = new List<string>();
List<IToken> toks = ((CommonTokenStream)this.GetTreeNodeStream().TokenStream).GetTokens(emittedCommentTokenIdx,endIdx);
@ -30,12 +35,17 @@ options {
emittedCommentTokenIdx = endIdx+1;
return rets;
}
protected List<string> collectComments() {
return collectComments(((CommonTokenStream)this.GetTreeNodeStream().TokenStream).GetTokens().Count - 1);
}
}
compilation_unit:
^(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:
class_declaration

View File

@ -12,10 +12,6 @@ options {
@members
{
protected bool is_class_modifier()
{
return false;
}
}
compilation_unit:

View File

@ -37,7 +37,7 @@ scope NSContext {
// This is the environment that we are building, it maps fully qualified type names to their
// translation templates
protected DirectoryHT<TypeRepTemplate> AppEnv {get; set;}
public DirectoryHT<TypeRepTemplate> AppEnv {get; set;}
protected UseRepTemplate[] NameSpaceContext {
get {
@ -75,11 +75,9 @@ scope NSContext {
///////////////////////////////////////////////////////
compilation_unit[CS2JSettings inCfg, DirectoryHT<TypeRepTemplate> inAppEnv]
compilation_unit
scope NSContext;
@init{
Cfg = inCfg;
AppEnv = inAppEnv;
// For initial, file level scope
$NSContext::nss = new List<UseRepTemplate>();
$NSContext::currentNS = "";

View File

@ -53,7 +53,9 @@ namespace RusticiSoftware.Translator.CSharp
nodes.TokenStream = tokens;
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();
}
}

View File

@ -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)>
package <packageName>;
<comments; separator="\n">
<type>
<endComments; separator="\n">
>>