From 21851337c56aab5cb645825c84588cc6e2f2dc55 Mon Sep 17 00:00:00 2001 From: Kevin Glynn Date: Thu, 26 Aug 2010 13:37:44 +0200 Subject: [PATCH] Add CSharpTranslator to rustici/trunk branch. Synched to rev 13553 --- CSharpTranslator/antlr2/.project | 11 +++ CSharpTranslator/antlr2/CSharpTranslator.sln | 5 - CSharpTranslator/antlr2/README.txt | 28 ------ CSharpTranslator/antlr2/Translator/.gitignore | 30 ------ CSharpTranslator/antlr2/Translator/ASTNode.cs | 7 +- .../antlr2/Translator/CSharpParser.g | 7 +- .../antlr2/Translator/CSharpTranslator.g | 14 +-- .../antlr2/Translator/JavaPrettyPrinter.g | 17 ++-- CSharpTranslator/antlr2/Translator/Main.cs | 16 +++- .../antlr2/Translator/NetTranslator.g | 7 +- CSharpTranslator/antlr2/Translator/TypeRep.cs | 9 +- CSharpTranslator/antlr2/build.properties | 28 ++++-- CSharpTranslator/antlr2/build.xml | 91 ++++--------------- 13 files changed, 87 insertions(+), 183 deletions(-) create mode 100644 CSharpTranslator/antlr2/.project delete mode 100644 CSharpTranslator/antlr2/README.txt delete mode 100644 CSharpTranslator/antlr2/Translator/.gitignore diff --git a/CSharpTranslator/antlr2/.project b/CSharpTranslator/antlr2/.project new file mode 100644 index 0000000..a565c18 --- /dev/null +++ b/CSharpTranslator/antlr2/.project @@ -0,0 +1,11 @@ + + + CSharpTranslator + + + + + + + + diff --git a/CSharpTranslator/antlr2/CSharpTranslator.sln b/CSharpTranslator/antlr2/CSharpTranslator.sln index 3d31999..bc3f6cd 100644 --- a/CSharpTranslator/antlr2/CSharpTranslator.sln +++ b/CSharpTranslator/antlr2/CSharpTranslator.sln @@ -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 diff --git a/CSharpTranslator/antlr2/README.txt b/CSharpTranslator/antlr2/README.txt deleted file mode 100644 index 9020c38..0000000 --- a/CSharpTranslator/antlr2/README.txt +++ /dev/null @@ -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 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/antlr2/Translator/.gitignore b/CSharpTranslator/antlr2/Translator/.gitignore deleted file mode 100644 index 97f8bd6..0000000 --- a/CSharpTranslator/antlr2/Translator/.gitignore +++ /dev/null @@ -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 diff --git a/CSharpTranslator/antlr2/Translator/ASTNode.cs b/CSharpTranslator/antlr2/Translator/ASTNode.cs index 3b63800..56b3a7b 100644 --- a/CSharpTranslator/antlr2/Translator/ASTNode.cs +++ b/CSharpTranslator/antlr2/Translator/ASTNode.cs @@ -114,11 +114,8 @@ 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; - } + Line = other.Line; + Column = other.Column; } //--------------------------------------------------------------------- diff --git a/CSharpTranslator/antlr2/Translator/CSharpParser.g b/CSharpTranslator/antlr2/Translator/CSharpParser.g index 93dffd7..5af6b48 100644 --- a/CSharpTranslator/antlr2/Translator/CSharpParser.g +++ b/CSharpTranslator/antlr2/Translator/CSharpParser.g @@ -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 ); } ; diff --git a/CSharpTranslator/antlr2/Translator/CSharpTranslator.g b/CSharpTranslator/antlr2/Translator/CSharpTranslator.g index 78963a7..0d7a6bf 100644 --- a/CSharpTranslator/antlr2/Translator/CSharpTranslator.g +++ b/CSharpTranslator/antlr2/Translator/CSharpTranslator.g @@ -1424,18 +1424,12 @@ staticConstructorBody [Object w] : body[w] ; -destructorDeclaration! [Object w] +destructorDeclaration [Object w] : #( DTOR_DECL attributes[w] modifiers[w] identifier[w] - b: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[w] + ) ; - + destructorBody [Object w] : body[w] ; diff --git a/CSharpTranslator/antlr2/Translator/JavaPrettyPrinter.g b/CSharpTranslator/antlr2/Translator/JavaPrettyPrinter.g index feaa5b3..5884ddc 100644 --- a/CSharpTranslator/antlr2/Translator/JavaPrettyPrinter.g +++ b/CSharpTranslator/antlr2/Translator/JavaPrettyPrinter.g @@ -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 */"); } diff --git a/CSharpTranslator/antlr2/Translator/Main.cs b/CSharpTranslator/antlr2/Translator/Main.cs index 3f07b02..347af08 100644 --- a/CSharpTranslator/antlr2/Translator/Main.cs +++ b/CSharpTranslator/antlr2/Translator/Main.cs @@ -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 diff --git a/CSharpTranslator/antlr2/Translator/NetTranslator.g b/CSharpTranslator/antlr2/Translator/NetTranslator.g index d94cdc8..fcd45e2 100644 --- a/CSharpTranslator/antlr2/Translator/NetTranslator.g +++ b/CSharpTranslator/antlr2/Translator/NetTranslator.g @@ -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 ); } diff --git a/CSharpTranslator/antlr2/Translator/TypeRep.cs b/CSharpTranslator/antlr2/Translator/TypeRep.cs index c447633..5d39e31 100644 --- a/CSharpTranslator/antlr2/Translator/TypeRep.cs +++ b/CSharpTranslator/antlr2/Translator/TypeRep.cs @@ -424,14 +424,7 @@ namespace RusticiSoftware.Translator 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?)"); - } + FieldsD.Add(ft.Name, FieldRep.newInstance(ft, uPath)); } Casts = new CastRep[template.Casts.Length]; for (int i = 0; i < template.Casts.Length; i++) diff --git a/CSharpTranslator/antlr2/build.properties b/CSharpTranslator/antlr2/build.properties index 6bbfd33..2a474dc 100644 --- a/CSharpTranslator/antlr2/build.properties +++ b/CSharpTranslator/antlr2/build.properties @@ -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 diff --git a/CSharpTranslator/antlr2/build.xml b/CSharpTranslator/antlr2/build.xml index fa71b31..89b9824 100644 --- a/CSharpTranslator/antlr2/build.xml +++ b/CSharpTranslator/antlr2/build.xml @@ -1,74 +1,20 @@ - + This script builds the cs2j translator and translates C# code - + + + + + - - - - - - - - - - - - - - - -