1
0
mirror of https://github.com/twiglet/cs2j.git synced 2025-01-18 13:15:17 +01:00

Add CSharpTranslator to rustici/trunk branch. Synched to rev 13553

This commit is contained in:
Kevin Glynn 2010-08-26 13:37:44 +02:00
parent ff8ef87f54
commit 21851337c5
13 changed files with 87 additions and 183 deletions

View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>CSharpTranslator</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
</buildSpec>
<natures>
</natures>
</projectDescription>

View File

@ -4,11 +4,6 @@ Microsoft Visual Studio Solution File, Format Version 9.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Translator", "Translator\Translator.csproj", "{D33074E4-1525-4F22-A1DB-A7F30989D8FE}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{8ECA4801-3F48-4FD1-91B4-4DFB567D913B}"
ProjectSection(SolutionItems) = preProject
build.properties = build.properties
build.xml = build.xml
README.txt = README.txt
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution

View File

@ -1,28 +0,0 @@
Welcome to CS2J, a C# to Java translator
In this directory you will find:
Translator: A C# project that can be built in Visual Studio 2005. It requires a version of Antlr that can generate C# to be on
your path. It builds to a command line application. Run it without arguments to see how to use it to translate C# projects to Java.
Although the project knows to use antlr to build the grammar files VS2005 has trouble with getting the dependencies straight. For
a trouble free development I suggest you run MSBuild directly in the project directory, e.g.:
kevin.glynn@D11624C1 ~/winhome/My Documents/Visual Studio 2005/Projects/Translator/Translator
$ /cygdrive/c/WINDOWS/Microsoft.NET/Framework/v2.0.50727/MSBuild.exe
works for me under cygwin.
In order that the -show<etc> options work you will need to add antlr.astframe.dll and antlr.runtime.dll (from the antlr distribution)
to your project references.
CS2JLibrary: These are the XML translation files for the .NET library. Copy them into your filesystem, this location will be passed
as your translator's -netdir argument.
CS2JLibrary: This is a Java project containing supporting code that will be required by the translated code to build and
run. Load it into a project in your Java environment and make your translated project depend on it.
Disclaimer: By the way, the translator doesn't quite work yet ....
Kevin (kevin.glynn@scorm.com)

View File

@ -1,30 +0,0 @@
CSharpEnvBuilder.cs
CSharpEnvBuilderTokenTypes.cs
CSharpEnvBuilderTokenTypes.txt
CSharpHooverTokenTypes.cs
CSharpHooverTokenTypes.txt
CSharpJavaTokenTypes.cs
CSharpJavaTokenTypes.txt
CSharpLexer.cs
CSharpLexerBase.cs
CSharpLexerBaseTokenTypes.cs
CSharpLexerBaseTokenTypes.txt
CSharpLexerTokenTypes.cs
CSharpLexerTokenTypes.txt
CSharpParser.cs
CSharpPreprocessTokenTypes.cs
CSharpPreprocessTokenTypes.txt
CSharpPreprocessorHooverLexer.cs
CSharpPreprocessorLexer.cs
CSharpTranslator.cs
CSharpTranslatorTokenTypes.cs
CSharpTranslatorTokenTypes.txt
JavaPrettyPrinter.cs
JavaPrettyPrinterTokenTypes.cs
JavaPrettyPrinterTokenTypes.txt
NetTranslator.cs
NetTranslatorTokenTypes.cs
NetTranslatorTokenTypes.txt
UnicodeLexerBase.cs
UnicodeLexerBaseTokenTypes.cs
UnicodeLexerBaseTokenTypes.txt

View File

@ -113,12 +113,9 @@ namespace RusticiSoftware.Translator
}
public void CopyPositionFrom(ASTNode other)
{
if (other != null) // it might be if we are recovering from a parse error
{
Line = other.Line;
Column = other.Column;
}
}
//---------------------------------------------------------------------

View File

@ -346,8 +346,7 @@ type!
ASTNode starsBase = #[STARS, "STARS"];
}
: (
( p:predefinedTypeName { typeBase = #p; } |
q:qualifiedIdentifier { typeBase = #q; } (LTHAN type (COMMA type)* GTHAN)? ) // typeName
( p:predefinedTypeName { typeBase = #p; } | q:qualifiedIdentifier { typeBase = #q; } ) // typeName
(
s1:STAR // pointerType
{
@ -1098,7 +1097,7 @@ modifier
//
classDeclaration! [AST attribs, AST modifiers]
: cl:CLASS id:identifier (LTHAN type (COMMA type)* GTHAN)? ba:classBase bo:classBody ( options { greedy = true; } : SEMI! )?
: cl:CLASS id:identifier ba:classBase bo:classBody ( options { greedy = true; } : SEMI! )?
{ ## = #( #cl, #attribs, #modifiers, #id, #ba, #bo ); }
;
@ -1661,7 +1660,7 @@ delegateDeclaration! [AST attribs, AST modifiers]
typ1:voidAsType { typ = #typ1; }
| typ2:type { typ = #typ2; }
)
id:identifier (LTHAN type (COMMA type)* GTHAN)? OPEN_PAREN! ( fp:formalParameterList )? CLOSE_PAREN! SEMI!
id:identifier OPEN_PAREN! ( fp:formalParameterList )? CLOSE_PAREN! SEMI!
{ ## = #( #dlg, #attribs, #modifiers, #typ, #id, #fp ); }
;

View File

@ -1424,16 +1424,10 @@ staticConstructorBody [Object w]
: body[w]
;
destructorDeclaration! [Object w]
destructorDeclaration [Object w]
: #( DTOR_DECL attributes[w] modifiers[w] identifier[w]
b:destructorBody[w]
destructorBody[w]
)
{ ## = #( [METHOD_DECL],
#( [MODIFIERS], [PROTECTED, "protected"] ),
#( [TYPE], [VOID, "void"], #( [ARRAY_RANKS] ) ),
#( [IDENTIFIER, "finalize"]) ,
#( [FORMAL_PARAMETER_LIST] ), #( [THROWS, "throws"], [IDENTIFIER, "Throwable"] ),
astFactory.dupTree(#b) ); }
;
destructorBody [Object w]

View File

@ -36,8 +36,6 @@ options {
private XmlTextWriter enumXmlWriter;
private ArrayList enumMembers = new ArrayList();
private bool inClassModifiers = false; // Filter out static, this isn't the right place but pending rewrite this is quickest.
/** walk list of hidden tokens in order, printing them out */
public void dumpHidden(TextWriter w, antlr.IHiddenStreamToken t) {
for ( ; t!=null ; t=filter.getHiddenAfter(t) ) {
@ -279,8 +277,8 @@ importDefinition [TextWriter w]
;
typeDefinition [TextWriter w]
: #(cl:CLASS {inClassModifiers = true; }
modifiers[w] {inClassModifiers = false; }
: #(cl:CLASS
modifiers[w]
id:IDENTIFIER { Print(w, "class "); Print(w, #id, " "); }
extendsClause[w]
implementsClause[w] { PrintNL(w); Print(w, "{"); PrintNL(w); indentLevel++; }
@ -368,7 +366,7 @@ modifier [TextWriter w]
: mpr:"private" { Print(w, #mpr); }
| mpu:"public" { Print(w, #mpu); }
| mpt:"protected" { Print(w, #mpt); }
| mst:"static" { if (!inClassModifiers) {Print(w, #mst);} }
| mst:"static" { Print(w, #mst); }
| mtr:"transient" { Print(w, #mtr); }
| mfi:FINAL { Print(w, #mfi); }
| mab:ABSTRACT { Print(w, #mab); }
@ -538,7 +536,7 @@ stat [TextWriter w]
typeDefinition[w]
| variableDef[w] { Print(w, ";"); }
| #(EXPR_STMT expression[w]) { Print(w, ";"); }
| #(LABEL_STMT id:IDENTIFIER { Print(w, #id, ": "); } stat[w])
| #(LABELED_STAT id:IDENTIFIER { Print(w, #id, ": "); } stat[w])
| #(lif:IF { Print(w, #lif, " ("); }
expression[w] { Print(w, ")"); PrintNL(w); }
stat[w]
@ -565,9 +563,8 @@ stat [TextWriter w]
stat[w] { Print(w, "while ("); }
expression[w] { Print(w, ");"); }
)
| #(gt:"goto" { Print(w, "// TODO: CS2J: goto is not supported by Java."); PrintNL(w); Print(w, "continue "); } gid:IDENTIFIER { Print(w, #gid); Print(w, ";"); } )
| #(br:"break" { Print(w, #br); } ( { Print(w, " "); } bid:IDENTIFIER { Print(w, #bid); } )? { Print(w, ";"); } )
| #(co:"continue" { Print(w, #co); } ( { Print(w, " "); } cid:IDENTIFIER { Print(w, #cid); } )? { Print(w, ";"); } )
| #(br:"break" { Print(w, #br); } ( { Print(w, " "); } IDENTIFIER)? { Print(w, ";"); } )
| #(co:"continue" { Print(w, #co); } ( { Print(w, " "); } IDENTIFIER)? { Print(w, ";"); } )
| #(re:"return" { Print(w, #re); } ( { Print(w, " "); } expression[w])? { Print(w, ";"); } )
| #(sw:"switch" { Print(w, #sw, " ("); }
expression[w] { Print(w, ")"); PrintNL(w); Print(w, "{"); indentLevel++; PrintNL(w); }
@ -752,7 +749,7 @@ constant [TextWriter w]
| st:STRING_LITERAL { Print(w, #st); }
| fl:NUM_FLOAT { Print(w, #fl); }
| db:DOUBLE_LITERAL { Print(w, #db); }
| flt:FLOAT_LITERAL { Print(w, #flt); }
| flr:FLOAT_LITERAL { Print(w, #flr); }
| lo:LONG_LITERAL { Print(w, #lo); Print(w, "L"); }
| ul:ULONG_LITERAL { Print(w, #ul); Print(w, "L"); }
| de:DECIMAL_LITERAL { Print(w, #de, "/* Unsupported Decimal Literal */"); }

View File

@ -206,7 +206,7 @@ namespace RusticiSoftware.Translator
i++;
string[] argDirs = args[i].Split(';');
for (int j = 0; j < argDirs.Length; j++)
argDirs[j] = Path.GetFullPath(argDirs[j]).ToLower();
argDirs[j] = Path.GetFullPath(argDirs[j]);
netRoot.AddRange(argDirs);
}
else if (args[i].ToLower().Equals("-exnetdir") && i < (args.Length - 1))
@ -259,6 +259,20 @@ namespace RusticiSoftware.Translator
Console.Out.WriteLine(String.Format("\nFound {0} .Net template files ({1} processed successfully)\n", numFilesProcessed, numFilesSuccessfullyProcessed));
// Sanity Checks
if (numFilesProcessed == 0)
{
Console.Error.WriteLine("Can't find any templates, aborting.");
Environment.Exit(1);
}
const double MIN_SUCCESS_RATIO = .9d;
if (((Double)numFilesSuccessfullyProcessed / (Double)numFilesProcessed) < MIN_SUCCESS_RATIO)
{
Console.Error.WriteLine("Success ratio below " + MIN_SUCCESS_RATIO + " aborting.");
Environment.Exit(1);
}
// Load Application Class Signatures (i.e. generate templates)
if (appRoot.Count == 0)
// By default translation target is application root

View File

@ -762,7 +762,7 @@ stat [object w]
: typeDefinition[w]
| variableDef[w, true]
| #(EXPR_STMT expression[w])
| #(LABEL_STMT IDENTIFIER stat[w])
| #(LABELED_STAT IDENTIFIER stat[w])
| #(IF
expression[w]
stat[w]
@ -829,7 +829,6 @@ stat [object w]
stat[w]
expression[w]
)
| #("goto" IDENTIFIER )
| #("break" ( IDENTIFIER)? )
| #("continue" ( IDENTIFIER)? )
| #("return" ( expression[w])? )
@ -1259,7 +1258,7 @@ primaryExpression [object w]
{
ASTNode kosherInp = #( [INVOCATION_EXPR], #e, #args);
ASTNode retAST = null;
if (#e != null && #e.Type == IDENTIFIER && symtab[#e.getText()] == null)
if (#e.Type == IDENTIFIER && symtab[#e.getText()] == null)
{
// Is it a local method call?
ASTNode thisNode = #( [THIS, "DUMMYTHIS"] );
@ -1267,7 +1266,7 @@ primaryExpression [object w]
retAST = ResolveMethod( #( [INVOCATION_EXPR], #( [MEMBER_ACCESS_EXPR], thisNode, astFactory.dupTree(#e)),
astFactory.dupTree(#args)) );
}
else if (#e != null && #e.Type == MEMBER_ACCESS_EXPR && #e.getFirstChild().getNextSibling().Type == IDENTIFIER)
else if (#e.Type == MEMBER_ACCESS_EXPR && #e.getFirstChild().getNextSibling().Type == IDENTIFIER)
{ // resolve method call
retAST = ResolveMethod( kosherInp );
}

View File

@ -423,16 +423,9 @@ namespace RusticiSoftware.Translator
Implements = (TypeRep[]) TmpImplements.ToArray(typeof(TypeRep));
FieldsD = new Hashtable();
foreach (FieldRepTemplate ft in template.Fields)
{
try
{
FieldsD.Add(ft.Name, FieldRep.newInstance(ft, uPath));
}
catch (Exception x)
{
Console.Out.WriteLine("Ignore duplicate field (#iffery?)");
}
}
Casts = new CastRep[template.Casts.Length];
for (int i = 0; i < template.Casts.Length; i++)
{

View File

@ -25,16 +25,32 @@ cs2jtx.build.dir=${build.dir}/cs2jtx
cs2jtx.exe= ${cs2jtx.bin.dir}/Translator.exe
#cs2jtx.exe= ${basedir}/../bin/Translator.exe
# by default, sibling directory to Translator
cs2j.dir=${basedir}/../../CS2JLibrary/NetTranslations
cheats.dir=${basedir}/../../Cheats
#whichbranch=AmazonBranch
whichbranch=TrunkBranch
# Sources, by default these are sibling directories of this script
#cs2j.dir=${basedir}/../../AmazonBranch/CS2JLibrary
#cheats.dir=${basedir}/../AmazonBranch/Cheats
cs2j.dir=${basedir}/../../${whichbranch}/CS2JLibrary
cheats.dir=${basedir}/../${whichbranch}/Cheats
# Directories to build into, against
cs.app.name=ScormEngineNetTrunk
#cs.app.name=Amazonsimpledb4thed
#cs.app.dir=${user.home}/My Documents/gitrepos/${cs.app.name}/src/app/ScormEngine.Core
cs.app.dir=${user.home}/My Documents/Visual Studio 2005/Projects/${cs.app.name}/src/app/ScormEngine.Core
# If no cs.app.dir set make it equal to cs.tx.dir
cs.app.dir=${cs.tx.dir}
# Set cs.tx.project if you want to translate a subset of the application
#cs.tx.project=ScormEngine.Core/Logic
cs.tx.project=Logic
cs.tx.dir=${cs.app.dir}/${cs.tx.project}
# Object of the translation, by default in the build area
java.output.dir=${build.dir}/javaproject/src
#java.output.dir=${user.home}/My Documents/AmazonBranch/RusticiSoftware.ScormContentPlayer.Logic/src
java.output.dir=${user.home}/My Documents/${whichbranch}/RusticiSoftware.ScormContentPlayer.Logic/src

View File

@ -1,74 +1,20 @@
<?xml-stylesheet type="text/xsl" href="file://C:\ant\etc\antex\antprettybuild\antprettybuild-3.1.1.xsl"?>
<project name="cs2jTranslator" default="launch" basedir="." xmlns:dn="antlib:org.apache.ant.dotnet">
<project name="cs2jTranslator" default="" basedir="." xmlns:dn="antlib:org.apache.ant.dotnet">
<description>
This script builds the cs2j translator and translates C# code
</description>
<property name="builder.ant.lib" value="${basedir}/lib"/>
<property file="${user.home}/${ant.project.name}.build.properties" />
<property file="${user.home}/build.properties" />
<property file="build.properties" />
<!-- load the ant-dotnet task, see http://ant.apache.org/antlibs/dotnet/index.html -->
<taskdef uri="antlib:org.apache.ant.dotnet" resource="org/apache/ant/dotnet/antlib.xml" classpath="${builder.ant.lib}/ant-dotnet-1.0.jar" />
<!-- Load AntForm tasks -->
<taskdef name="antform" classname="com.sardak.antform.AntForm" classpath="${builder.ant.lib}/antform.jar"/>
<taskdef name="antmenu" classname="com.sardak.antform.AntMenu" classpath="${builder.ant.lib}/antform.jar"/>
<target name="launch" depends="launch-form,run" description="Simple Graphical Launch"/>
<target name="launch-full" depends="launch-full-form,run" description="Graphical Launch with all options"/>
<target name="launch-form">
<property file="${basedir}/launch-simple.properties" />
<antform title="C# to Java Translation" save="${basedir}/launch-simple.properties">
<label>Configure the source and target locations for your translation:</label>
<fileSelectionProperty label="C# Translation Root : " property="cs.tx.dir" directoryChooser="true" />
<fileSelectionProperty label="Java output directory : " property="java.output.dir" directoryChooser="true" />
<separator />
<controlbar>
<button label="Cancel" type="cancel" target="cancel" newproject="false"/>
<button label="Reset" type="reset" />
<button label="OK" type="ok" focus="true" newproject="false"/>
</controlbar>
</antform>
</target>
<target name="launch-full-form">
<property file="${basedir}/launch-full.properties" />
<antform title="C# to Java Translation" save="${basedir}/launch-full.properties">
<label>Configure the source and target locations for your translation:</label>
<fileSelectionProperty label="C# Application Root : " property="cs.app.dir" directoryChooser="true" />
<fileSelectionProperty label="C# Translation Root : " property="cs.tx.dir" directoryChooser="true" />
<fileSelectionProperty label="Java output directory : " property="java.output.dir" directoryChooser="true" />
<fileSelectionProperty label="CS2J Translation Files : " property="cs2j.dir" directoryChooser="true" />
<separator />
<!-- <label>Enter the locations of the branches you want to translate from / to</label>
<textProperty label="ScormEngineNet branch : " property="csharp.project.full" />
<textProperty label="ScormEngineJava branch : " property="java.project.full" />
<separator />
<booleanProperty label="Commit Java code after translation " property="is.commit.javacode" />
<booleanProperty label="Refresh and Rebuild cs2j executable " property="is.rebuild.cs2j" />
-->
<controlbar>
<button label="Cancel" type="cancel" target="cancel" newproject="false"/>
<button label="Reset" type="reset" />
<button label="OK" type="ok" focus="true" newproject="false"/>
</controlbar>
</antform>
</target>
<target name="cancel">
<echo message="Translation cancelled by user" />
<property name="is.run.cancelled" value="true" />
</target>
<target name="ok">
</target>
<target name="run" depends="init,translateCS2J" unless="is.run.cancelled">
</target>
<!-- We could have left this in the msbuild script fed to buildTranslator, but it is clearer in Ant :) -->
<target name="antlrTranslator" depends="init" unless="is.run.cancelled">
<target name="antlrTranslator" depends="init">
<antlr target="${cs2jtx.src.dir}/UnicodeLexerBase.g">
<classpath> <pathelement location="${builder.ant.lib}/antlr.jar"/> </classpath>
</antlr>
@ -109,7 +55,7 @@ ork\v2.0.50727\System.dll /reference:C:\WINDOWS\Microsoft.NET\Framework\v2.0.507
/target:exe
-->
<target name="buildTranslator" depends="antlrTranslator" description="Builds CSharp to Java Translator" unless="is.run.cancelled">
<target name="buildTranslator" depends="antlrTranslator" description="Builds CSharp to Java Translator">
<mkdir dir="${cs2jtx.bin.dir}/"/>
<dn:msbuild>
<dn:build>
@ -184,10 +130,9 @@ Settings\kevin.glynn\eclipsews\RusticiSoftware.ScormUntethered.Logic\src"
"c:\Documents and Settings\kevin.glynn\My Documents\Cheats"
"c:\docume~1\kevin~1.gly\locals~1\temp\tmpk6_pcn\UntetheredLogic" -->
<target name="translateCS2J" depends="init" description="run translator" unless="is.run.cancelled">
<target name="translateCS2J" depends="init" description="run translator">
<dn:dotnetexec executable="${cs2jtx.exe}">
<arg value="-version"/>
<arg value="-v"/>
<arg value="-dumpxml"/>
<arg value="-xmldir"/>
<arg file="${cs2jtx.build.dir}/xmls"/>
@ -207,15 +152,17 @@ Settings\kevin.glynn\eclipsews\RusticiSoftware.ScormUntethered.Logic\src"
</dn:dotnetexec>
</target>
<target name="dumpProperties" depends="init" description="output all set properties, a useful sanity check">
<target name="echoTranslateCS2J" depends="" description="echo run translator command line">
<echo>
"${cs2jtx.exe} -dumpxml -odir ${java.output.dir} -netdir ${cs2j.dir} -appdir ${cs.app.dir} -cheatdir ${cheats.dir} ${cs.tx.dir}"
</echo>
</target>
<target name="dumpProperties" description="output all set properties, a useful sanity check">
<echoproperties />
</target>
<target name="init" depends="" unless="is.run.cancelled">
<property file="${user.home}/${ant.project.name}.build.properties" />
<property file="${user.home}/build.properties" />
<property file="build.properties" />
<target name="init" depends="">
<mkdir dir="${cs2jtx.build.dir}" />
</target>