diff --git a/CSharpTranslator/antlr3/src/cs2j/CLR/cs2j.cs b/CSharpTranslator/antlr3/src/cs2j/CLR/cs2j.cs index 09939e2..34a9e5a 100644 --- a/CSharpTranslator/antlr3/src/cs2j/CLR/cs2j.cs +++ b/CSharpTranslator/antlr3/src/cs2j/CLR/cs2j.cs @@ -286,7 +286,7 @@ namespace RusticiSoftware.Translator.CSharp javaMaker.CUKeys = new List(); JavaMaker.compilation_unit_return java = javaMaker.compilation_unit(); - + int saveEmittedCommentTokenIdx = 0; for (int i = 0; i < javaMaker.CUKeys.Count; i++) { string typeName = javaMaker.CUKeys[i]; @@ -345,11 +345,13 @@ namespace RusticiSoftware.Translator.CSharp outputMaker.TemplateLib = templates; outputMaker.Cfg = cfg; + outputMaker.EmittedCommentTokenIdx = saveEmittedCommentTokenIdx; outputMaker.IsLast = i == (javaMaker.CUKeys.Count - 1); StreamWriter javaW = new StreamWriter(javaFName); javaW.Write(outputMaker.compilation_unit().ToString()); javaW.Close(); + saveEmittedCommentTokenIdx = outputMaker.EmittedCommentTokenIdx; } // ITreeNodeStream javaTree = java.Tree; } diff --git a/CSharpTranslator/antlr3/src/cs2j/CSharp/JavaMaker.g b/CSharpTranslator/antlr3/src/cs2j/CSharp/JavaMaker.g index 8e8a349..ef37fe3 100644 --- a/CSharpTranslator/antlr3/src/cs2j/CSharp/JavaMaker.g +++ b/CSharpTranslator/antlr3/src/cs2j/CSharp/JavaMaker.g @@ -1,4 +1,4 @@ - // JavaMaker.g +// JavaMaker.g // // Convert C# parse tree to a Java parse tree // @@ -53,6 +53,18 @@ scope NSContext { } return root; } + + protected CommonTree mangleModifiersForType(CommonTree modifiers) { + if (modifiers == null || modifiers.Children == null) + return modifiers; + CommonTree stripped = (CommonTree)modifiers.DupNode(); + for (int i = 0; i < modifiers.Children.Count; i++) { + if (((CommonTree)modifiers.Children[i]).Token.Text != "static") { + adaptor.AddChild(stripped, modifiers.Children[i]); + } + } + return stripped; + } } /******************************************************************************************** @@ -108,7 +120,7 @@ namespace_member_declaration } : namespace_declaration - | attributes? modifiers? ty=type_declaration { isCompUnit = true; } -> ^(PACKAGE[$ty.start.Token] PAYLOAD[ns] modifiers? type_declaration); + | attributes? modifiers? ty=type_declaration { isCompUnit = true; } -> ^(PACKAGE[$ty.start.Token] PAYLOAD[ns] { mangleModifiersForType($modifiers.tree) } type_declaration); // type_declaration is only called at the top level, so each of the types declared // here will become a Java compilation unit (and go to its own file) type_declaration returns [string name] @@ -134,10 +146,9 @@ modifiers: modifier: 'new' | 'public' | 'protected' | 'private' | 'internal' -> /* translate to package-private */| 'unsafe' -> | 'abstract' | 'sealed' -> FINAL["final"] | 'static' | 'readonly' -> FINAL["final"] | 'volatile' | 'extern' | 'virtual' | 'override'; - + class_member_declaration: attributes? - // TODO: Don't emit private m=modifiers? ( 'const' type constant_declarators ';' | event_declaration // 'event' @@ -630,15 +641,13 @@ attribute_argument_expression: class_declaration returns [string name]: c='class' type_or_generic { $name = mkTypeName($type_or_generic.type, $type_or_generic.generic_arguments); } class_base? type_parameter_constraints_clauses? class_body ';'? - -> ^(CLASS[$c.Token] PAYLOAD[$type_or_generic.type] ^(PAYLOAD_LIST { mkPayloadList($type_or_generic.generic_arguments) } ) - class_base? type_parameter_constraints_clauses? class_body ); + -> ^(CLASS[$c.Token] type_or_generic class_base? type_parameter_constraints_clauses? class_body ); class_base: - // syntactically base class vs interface name is the same - //':' class_type (',' interface_type_list)? ; - ':' interface_type_list ; + // just put all types in a single list. In NetMaker we will extract the base class if necessary + ':' interface_type_list -> ^(IMPLEMENTS interface_type_list); interface_type_list: - type (',' type)* ; + ts+=type (',' ts+=type)* -> $ts+; class_body: '{' class_member_declarations? '}' ; @@ -825,12 +834,15 @@ parameter_array: /////////////////////////////////////////////////////// interface_declaration returns [string name]: - 'interface' identifier { $name = $identifier.text; } variant_generic_parameter_list? - interface_base? type_parameter_constraints_clauses? interface_body ';'? ; + c='interface' identifier { $name = $identifier.text; } variant_generic_parameter_list? + interface_base? type_parameter_constraints_clauses? interface_body ';'? + -> ^(INTERFACE[$c.Token] identifier variant_generic_parameter_list? interface_base? type_parameter_constraints_clauses? interface_body ); + +interface_base: + ':' interface_type_list -> ^(EXTENDS interface_type_list); + interface_modifiers: modifier+ ; -interface_base: - ':' interface_type_list ; interface_body: '{' interface_member_declarations? '}' ; interface_member_declarations: @@ -868,7 +880,10 @@ method_modifiers: /////////////////////////////////////////////////////// struct_declaration returns [string name]: - 'struct' type_or_generic { $name = mkTypeName($type_or_generic.type, $type_or_generic.generic_arguments); } struct_interfaces? type_parameter_constraints_clauses? struct_body ';'? ; + c='struct' type_or_generic { $name = mkTypeName($type_or_generic.type, $type_or_generic.generic_arguments); } class_base? type_parameter_constraints_clauses? class_body ';'? + -> ^(CLASS[$c.Token] type_or_generic class_base? type_parameter_constraints_clauses? class_body ); + +// UNUSED, HOPEFULLY struct_modifiers: struct_modifier+ ; struct_modifier: @@ -907,7 +922,7 @@ struct_member_declaration: | constructor_declaration // | static_constructor_declaration ) ; - +// UNUSED END /////////////////////////////////////////////////////// indexer_declaration: diff --git a/CSharpTranslator/antlr3/src/cs2j/CSharp/JavaPrettyPrint.g b/CSharpTranslator/antlr3/src/cs2j/CSharp/JavaPrettyPrint.g index e3c027d..97e49a0 100644 --- a/CSharpTranslator/antlr3/src/cs2j/CSharp/JavaPrettyPrint.g +++ b/CSharpTranslator/antlr3/src/cs2j/CSharp/JavaPrettyPrint.g @@ -20,20 +20,20 @@ options { { public bool IsLast { get; set; } - protected int emittedCommentTokenIdx = 0; + public int EmittedCommentTokenIdx { get; set; } // Collect all comments from previous position to endIdx // comments are the text from tokens on the Hidden channel protected List collectComments(int endIdx) { List rets = new List(); - List toks = ((CommonTokenStream)this.GetTreeNodeStream().TokenStream).GetTokens(emittedCommentTokenIdx,endIdx); + List toks = ((CommonTokenStream)this.GetTreeNodeStream().TokenStream).GetTokens(EmittedCommentTokenIdx,endIdx); if (toks != null) { foreach (IToken tok in toks) { if (tok.Channel == TokenChannels.Hidden) { rets.Add(new Regex("(\\n|\\r)+").Replace(tok.Text, Environment.NewLine).Trim()); } } - emittedCommentTokenIdx = endIdx+1; + EmittedCommentTokenIdx = endIdx+1; } return rets; } @@ -52,7 +52,7 @@ compilation_unit type_declaration [StringTemplate modifiersST]: class_declaration[modifiersST] -> { $class_declaration.st } | struct_declaration - | interface_declaration + | interface_declaration[modifiersST] -> { $interface_declaration.st } | enum_declaration[modifiersST] -> { $enum_declaration.st } | delegate_declaration ; // Identifiers @@ -75,10 +75,10 @@ class_member_declaration: ( 'const' type constant_declarators ';' | event_declaration // 'event' | 'partial' (method_declaration - | interface_declaration + | interface_declaration[$m.st] | class_declaration[$m.st] | struct_declaration) - | interface_declaration // 'interface' + | interface_declaration[$m.st] // 'interface' | 'void' method_declaration | type ( (member_name '(') => method_declaration | (member_name '{') => property_declaration @@ -294,12 +294,13 @@ commas: /////////////////////////////////////////////////////// type_name: - namespace_or_type_name ; + namespace_or_type_name -> { $namespace_or_type_name.st }; namespace_or_type_name: - type_or_generic ('::' type_or_generic)? ('.' type_or_generic)* ; + + t1=type_or_generic ('::' t2=type_or_generic)? ('.' ts+=type_or_generic)* -> namespace_or_type(type1={$t1.st}, type2={$t2.st}, types={$ts}); type_or_generic: - (identifier generic_argument_list) => identifier generic_argument_list - | identifier ; + (identifier generic_argument_list) => gi=identifier generic_argument_list -> template(name={ $gi.st }, args={ $generic_argument_list.st }) "" + | i=identifier -> { $i.st }; qid: // qualified_identifier v2 qid_start qid_part* @@ -318,15 +319,20 @@ qid_part: access_identifier ; generic_argument_list: - '<' type_arguments '>' ; + '<' type_arguments '>' -> template(args={ $type_arguments.st }) "\<\>"; type_arguments: - type (',' type)* ; + ts+=type (',' ts+=type)* -> template(types = { $ts }) ""; -type: - ((predefined_type | type_name) rank_specifiers) => (predefined_type | type_name) rank_specifiers '*'* - | ((predefined_type | type_name) ('*'+ | '?')) => (predefined_type | type_name) ('*'+ | '?') - | (predefined_type | type_name) - | 'void' '*'+ +type +@init { + StringTemplate nm = null; + List stars = new List(); + string opt = null; +}: + ((predefined_type | type_name) rank_specifiers) => (t1p=predefined_type {nm=$t1p.st;} | t1t=type_name {nm=$t1t.st;} ) rank_specifiers ('*' { stars.Add("*"); })* -> type(name={ nm }, stars={ stars }, rs={ $rank_specifiers.st }) + | ((predefined_type | type_name) ('*'+ | '?')) => (t2p=predefined_type {nm=$t2p.st;} | t2t=type_name {nm=$t2t.st;} ) (('*' { stars.Add("*"); })+ | '?' { opt = "?"; }) -> type(name={ nm }, stars={ stars }, opt={ opt }) + | (t3p=predefined_type {nm=$t3p.st;} | t3t=type_name {nm=$t3t.st;} ) -> type(name={ nm }) + | 'void' ('*' { stars.Add("*"); })+ -> type(name={ "void" }, stars={ stars }) ; non_nullable_type: (predefined_type | type_name) @@ -550,16 +556,18 @@ class_declaration[StringTemplate modifiersST] @init { List preComments = null; }: - ^(c=CLASS { preComments = collectComments($c.TokenStartIndex); } nm=PAYLOAD ^(PAYLOAD_LIST PAYLOAD* ) - class_base? type_parameter_constraints_clauses? class_body ) - -> class(modifiers = {modifiersST}, name={ $nm }, comments = { preComments } ) ; -class_base: - // syntactically base class vs interface name is the same - //':' class_type (',' interface_type_list)? ; - ':' interface_type_list ; + ^(c=CLASS { preComments = collectComments($c.TokenStartIndex); } type_or_generic + class_extends? class_implements? type_parameter_constraints_clauses? class_body ) + -> class(modifiers = {modifiersST}, name={ $type_or_generic.st }, comments = { preComments }, + extends = { $class_extends.st }, imps = { $class_implements.st }) ; + +class_extends: + ^(EXTENDS ts+=type*) -> extends(types = { $ts }) ; +class_implements: + ^(IMPLEMENTS ts+=type*) -> imps(types = { $ts }) ; interface_type_list: - type (',' type)* ; + ts+=type (',' ts+=type)* -> template(types={ $ts }) ""; class_body: '{' class_member_declarations? '}' ; @@ -707,9 +715,14 @@ parameter_array: 'params' type identifier ; /////////////////////////////////////////////////////// -interface_declaration: - 'interface' identifier variant_generic_parameter_list? - interface_base? type_parameter_constraints_clauses? interface_body ';'? ; +interface_declaration[StringTemplate modifiersST] +@init { + List preComments = null; +}: + ^(c=INTERFACE { preComments = collectComments($c.TokenStartIndex); } identifier variant_generic_parameter_list? + class_extends? type_parameter_constraints_clauses? interface_body ) + -> iface(modifiers = {modifiersST}, name={ $identifier.st }, comments = { preComments }, + imps = { $class_extends.st }) ; interface_modifiers: modifier+ ; interface_base: @@ -767,11 +780,11 @@ struct_member_declaration: ( 'const' type constant_declarators ';' | event_declaration // 'event' | 'partial' (method_declaration - | interface_declaration + | interface_declaration[$m.st] | class_declaration[$m.st] | struct_declaration) - | interface_declaration // 'interface' + | interface_declaration[$m.st] // 'interface' | class_declaration[$m.st] // 'class' | 'void' method_declaration | type ( (member_name '(') => method_declaration @@ -1022,8 +1035,8 @@ yield_statement: /////////////////////////////////////////////////////// predefined_type: - 'bool' | 'byte' | 'char' | 'decimal' | 'double' | 'float' | 'int' | 'long' | 'object' | 'sbyte' - | 'short' | 'string' | 'uint' | 'ulong' | 'ushort' ; + (t='bool' | t='byte' | t='char' | t='decimal' | t='double' | t='float' | t='int' | t='long' | t='object' | t='sbyte' + | t='short' | t='string' | t='uint' | t='ulong' | t='ushort') -> string(payload={$t.text}); identifier: IDENTIFIER -> template(v= { $IDENTIFIER.text }) "" | also_keyword -> template(v= { $also_keyword.text }) ""; diff --git a/CSharpTranslator/antlr3/src/cs2j/CSharp/NetMaker.g b/CSharpTranslator/antlr3/src/cs2j/CSharp/NetMaker.g index 42b38ff..13f070e 100644 --- a/CSharpTranslator/antlr3/src/cs2j/CSharp/NetMaker.g +++ b/CSharpTranslator/antlr3/src/cs2j/CSharp/NetMaker.g @@ -513,12 +513,12 @@ attribute_argument_expression: /////////////////////////////////////////////////////// class_declaration: - ^(CLASS PAYLOAD ^(PAYLOAD_LIST PAYLOAD* ) - class_base? type_parameter_constraints_clauses? class_body ) ; -class_base: - // syntactically base class vs interface name is the same - //':' class_type (',' interface_type_list)? ; - ':' interface_type_list ; + ^(CLASS type_or_generic + class_implements? type_parameter_constraints_clauses? class_body ) ; +class_extends: + ^(EXTENDS type*) ; +class_implements: + ^(IMPLEMENTS type*) ; interface_type_list: type (',' type)* ; @@ -668,8 +668,8 @@ parameter_array: /////////////////////////////////////////////////////// interface_declaration: - 'interface' identifier variant_generic_parameter_list? - interface_base? type_parameter_constraints_clauses? interface_body ';'? ; + ^(INTERFACE identifier variant_generic_parameter_list? + class_extends? type_parameter_constraints_clauses? interface_body ) ; interface_modifiers: modifier+ ; interface_base: diff --git a/CSharpTranslator/antlr3/src/cs2j/CSharp/cs.g b/CSharpTranslator/antlr3/src/cs2j/CSharp/cs.g index 2f4f585..cf376e6 100644 --- a/CSharpTranslator/antlr3/src/cs2j/CSharp/cs.g +++ b/CSharpTranslator/antlr3/src/cs2j/CSharp/cs.g @@ -10,7 +10,9 @@ tokens { PACKAGE; ENUM_BODY; CLASS; - + EXTENDS; + IMPLEMENTS; + INTERFACE; FINAL; /* final modifier */ PAYLOAD; // carries arbitrary text for the output file diff --git a/CSharpTranslator/antlr3/src/cs2j/bin/Debug/templates/java.stg b/CSharpTranslator/antlr3/src/cs2j/bin/Debug/templates/java.stg index 2ea0d8f..ce7a092 100644 --- a/CSharpTranslator/antlr3/src/cs2j/bin/Debug/templates/java.stg +++ b/CSharpTranslator/antlr3/src/cs2j/bin/Debug/templates/java.stg @@ -28,9 +28,17 @@ package ; // ******* CLASSES *********** -class(modifiers, comments, attributes, name, body) ::= << +class(modifiers, comments, attributes, name, inherits, body) ::= << -class +class +{ + +} +>> + +iface(modifiers, comments, attributes, name, imps, body) ::= << + +interface { } @@ -53,8 +61,13 @@ enum_member(comments, value) ::= << >> +type(name, stars, opt) ::= "" +namespace_or_type(type1, type2, types) ::= "::." + modifiers(mods) ::= " " -//modifiers(mods) ::= "" + +extends(types) ::= "extends " +imps(types) ::= "implements " // ******* UTILITY *********** -string(payload) ::= "" +string(payload) ::= "" \ No newline at end of file diff --git a/CSharpTranslator/antlr3/src/cs2j/cs2j.csproj b/CSharpTranslator/antlr3/src/cs2j/cs2j.csproj index 5c51455..52a617b 100644 --- a/CSharpTranslator/antlr3/src/cs2j/cs2j.csproj +++ b/CSharpTranslator/antlr3/src/cs2j/cs2j.csproj @@ -39,7 +39,7 @@ DEBUG;TRACE prompt 4 - -warnings -debug 10 -dumpxmls -xmldir=/tmp/xml/se -odir=/tmp/java/se /Users/keving/svnrepos/ScormEngineNet/src/app/ScormEngine.Core/Logic/Types/Mode.cs + -warnings -debug 10 -dumpxmls -xmldir=/tmp/xml/se -odir=/tmp/java/se /Users/keving/svnrepos/ScormEngineNet/src/app/ScormEngine.Core/DataHelp/IDataHelper.cs pdbonly