diff --git a/CSharpTranslator/CSharpTranslator.sln b/CSharpTranslator/CSharpTranslator.sln index bc3f6cd..da8477f 100644 --- a/CSharpTranslator/CSharpTranslator.sln +++ b/CSharpTranslator/CSharpTranslator.sln @@ -4,6 +4,9 @@ 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 + README.txt = README.txt + EndProjectSection EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/CSharpTranslator/README.txt b/CSharpTranslator/README.txt new file mode 100644 index 0000000..9020c38 --- /dev/null +++ b/CSharpTranslator/README.txt @@ -0,0 +1,28 @@ + 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 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) \ No newline at end of file diff --git a/CSharpTranslator/Translator/ASTNode.cs b/CSharpTranslator/Translator/ASTNode.cs index 56b3a7b..3b63800 100644 --- a/CSharpTranslator/Translator/ASTNode.cs +++ b/CSharpTranslator/Translator/ASTNode.cs @@ -114,8 +114,11 @@ namespace RusticiSoftware.Translator public void CopyPositionFrom(ASTNode other) { - Line = other.Line; - Column = other.Column; + if (other != null) // it might be if we are recovering from a parse error + { + Line = other.Line; + Column = other.Column; + } } //--------------------------------------------------------------------- diff --git a/CSharpTranslator/Translator/CSharpParser.g b/CSharpTranslator/Translator/CSharpParser.g index 5af6b48..93dffd7 100644 --- a/CSharpTranslator/Translator/CSharpParser.g +++ b/CSharpTranslator/Translator/CSharpParser.g @@ -346,7 +346,8 @@ type! ASTNode starsBase = #[STARS, "STARS"]; } : ( - ( p:predefinedTypeName { typeBase = #p; } | q:qualifiedIdentifier { typeBase = #q; } ) // typeName + ( p:predefinedTypeName { typeBase = #p; } | + q:qualifiedIdentifier { typeBase = #q; } (LTHAN type (COMMA type)* GTHAN)? ) // typeName ( s1:STAR // pointerType { @@ -1097,7 +1098,7 @@ modifier // classDeclaration! [AST attribs, AST modifiers] - : cl:CLASS id:identifier ba:classBase bo:classBody ( options { greedy = true; } : SEMI! )? + : cl:CLASS id:identifier (LTHAN type (COMMA type)* GTHAN)? ba:classBase bo:classBody ( options { greedy = true; } : SEMI! )? { ## = #( #cl, #attribs, #modifiers, #id, #ba, #bo ); } ; @@ -1660,7 +1661,7 @@ delegateDeclaration! [AST attribs, AST modifiers] typ1:voidAsType { typ = #typ1; } | typ2:type { typ = #typ2; } ) - id:identifier OPEN_PAREN! ( fp:formalParameterList )? CLOSE_PAREN! SEMI! + id:identifier (LTHAN type (COMMA type)* GTHAN)? OPEN_PAREN! ( fp:formalParameterList )? CLOSE_PAREN! SEMI! { ## = #( #dlg, #attribs, #modifiers, #typ, #id, #fp ); } ; diff --git a/CSharpTranslator/Translator/JavaPrettyPrinter.g b/CSharpTranslator/Translator/JavaPrettyPrinter.g index 5884ddc..b40e87a 100644 --- a/CSharpTranslator/Translator/JavaPrettyPrinter.g +++ b/CSharpTranslator/Translator/JavaPrettyPrinter.g @@ -36,6 +36,8 @@ 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) ) { @@ -277,8 +279,8 @@ importDefinition [TextWriter w] ; typeDefinition [TextWriter w] - : #(cl:CLASS - modifiers[w] + : #(cl:CLASS {inClassModifiers = true; } + modifiers[w] {inClassModifiers = false; } id:IDENTIFIER { Print(w, "class "); Print(w, #id, " "); } extendsClause[w] implementsClause[w] { PrintNL(w); Print(w, "{"); PrintNL(w); indentLevel++; } @@ -366,7 +368,7 @@ modifier [TextWriter w] : mpr:"private" { Print(w, #mpr); } | mpu:"public" { Print(w, #mpu); } | mpt:"protected" { Print(w, #mpt); } - | mst:"static" { Print(w, #mst); } + | mst:"static" { if (!inClassModifiers) {Print(w, #mst);} } | mtr:"transient" { Print(w, #mtr); } | mfi:FINAL { Print(w, #mfi); } | mab:ABSTRACT { Print(w, #mab); } @@ -749,7 +751,7 @@ constant [TextWriter w] | st:STRING_LITERAL { Print(w, #st); } | fl:NUM_FLOAT { Print(w, #fl); } | db:DOUBLE_LITERAL { Print(w, #db); } - | flr:FLOAT_LITERAL { Print(w, #flr); } + | flt:FLOAT_LITERAL { Print(w, #flt); } | 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 */"); } diff --git a/CSharpTranslator/build.xml b/CSharpTranslator/build.xml index 89b9824..4fcf505 100644 --- a/CSharpTranslator/build.xml +++ b/CSharpTranslator/build.xml @@ -1,10 +1,11 @@ - + This script builds the cs2j translator and translates C# code + @@ -12,6 +13,44 @@ + + + + + + + + + + + + + + +