From 11624cc6746fddfd1cb13925d880ce4754e9004d Mon Sep 17 00:00:00 2001 From: Kevin Glynn Date: Tue, 21 Dec 2010 12:03:24 +0100 Subject: [PATCH] option to omit timestamps, static constructors, indexers --- .../antlr3/src/cs2j/CLR/CS2JSettings.cs | 7 ++- CSharpTranslator/antlr3/src/cs2j/CLR/cs2j.cs | 1 + .../antlr3/src/cs2j/CSharp/JavaMaker.g | 46 +++++++++++++++---- .../antlr3/src/cs2j/CSharp/JavaPrettyPrint.g | 4 +- .../antlr3/src/cs2j/CSharp/NetMaker.g | 1 + CSharpTranslator/antlr3/src/cs2j/CSharp/cs.g | 1 + .../src/cs2j/bin/Debug/templates/java.stg | 8 ++++ 7 files changed, 58 insertions(+), 10 deletions(-) diff --git a/CSharpTranslator/antlr3/src/cs2j/CLR/CS2JSettings.cs b/CSharpTranslator/antlr3/src/cs2j/CLR/CS2JSettings.cs index ac94b8f..a64cfb0 100644 --- a/CSharpTranslator/antlr3/src/cs2j/CLR/CS2JSettings.cs +++ b/CSharpTranslator/antlr3/src/cs2j/CLR/CS2JSettings.cs @@ -32,12 +32,16 @@ namespace RusticiSoftware.Translator.CSharp public bool Warnings { get; set; } - private bool translatorKeepParens = true; public bool TranslatorKeepParens { get; set; } + public bool TranslatorAddTimeStamp + { + get; set; + } + public CS2JSettings () { @@ -65,6 +69,7 @@ namespace RusticiSoftware.Translator.CSharp Warnings = false; TranslatorKeepParens = true; + TranslatorAddTimeStamp = true; } } } diff --git a/CSharpTranslator/antlr3/src/cs2j/CLR/cs2j.cs b/CSharpTranslator/antlr3/src/cs2j/CLR/cs2j.cs index d0620e4..b282db8 100644 --- a/CSharpTranslator/antlr3/src/cs2j/CLR/cs2j.cs +++ b/CSharpTranslator/antlr3/src/cs2j/CLR/cs2j.cs @@ -98,6 +98,7 @@ namespace RusticiSoftware.Translator.CSharp .Add ("exappdir=", dirs => addDirectories(cfg.ExAppRoot, dirs)) .Add ("exclude=", dirs => addDirectories(cfg.Exclude, dirs)) .Add ("translator-keep-parens=", v => cfg.TranslatorKeepParens = Boolean.Parse(v)) + .Add ("translator-timestamp-files=", v => cfg.TranslatorAddTimeStamp = Boolean.Parse(v)) ; //TODO: fix enum dump diff --git a/CSharpTranslator/antlr3/src/cs2j/CSharp/JavaMaker.g b/CSharpTranslator/antlr3/src/cs2j/CSharp/JavaMaker.g index 5511ab0..cccf120 100644 --- a/CSharpTranslator/antlr3/src/cs2j/CSharp/JavaMaker.g +++ b/CSharpTranslator/antlr3/src/cs2j/CSharp/JavaMaker.g @@ -302,7 +302,7 @@ class_member_declaration: | e3=enum_declaration -> ^(ENUM[$e3.start.Token, "ENUM"] $a? $m? $e3) | d3=delegate_declaration -> ^(DELEGATE[$d3.start.Token, "DELEGATE"] $a? $m? $d3) | co3=conversion_operator_declaration -> ^(CONVERSION_OPERATOR[$co3.start.Token, "CONVERSION"] $a? $m? $co3) - | con3=constructor_declaration -> ^(CONSTRUCTOR[$con3.start.Token, "CONSTRUCTOR"] $a? $m? $con3) + | con3=constructor_declaration[$a.tree, $m.tree, $m.modList] -> $con3 | de3=destructor_declaration -> ^(DESTRUCTOR[$de3.start.Token, "DESTRUCTOR"] $a? $m? $de3) ) ; @@ -1152,7 +1152,7 @@ interface_event_declaration [CommonTree atts, CommonTree mods]: 'event' type identifier ';' ; interface_indexer_declaration [CommonTree atts, CommonTree mods, CommonTree type]: // attributes? 'new'? type - 'this' '[' formal_parameter_list ']' '{' interface_accessor_declarations[atts,mods,type, "INDEX"] '}' ; + 'this' '[' formal_parameter_list ']' '{' indexer_accessor_declarations[atts,mods,type,$formal_parameter_list.tree] '}' ; interface_accessor_declarations [CommonTree atts, CommonTree mods, CommonTree type, String propName]: interface_accessor_declaration[atts, mods, type, propName]+ ; @@ -1211,11 +1211,26 @@ scope TypeContext; /////////////////////////////////////////////////////// indexer_declaration [CommonTree atts, CommonTree mods, CommonTree type]: - indexer_declarator '{' accessor_declarations[atts, mods, type, "INDEX", "INDEX"] '}' ; -indexer_declarator: + 'this' '[' formal_parameter_list ']' '{' indexer_accessor_declarations[atts, mods, type, $formal_parameter_list.tree] '}' ; +//indexer_declarator: //(type_name '.')? - 'this' '[' formal_parameter_list ']' ; +// 'this' '[' formal_parameter_list ']' ; + +indexer_accessor_declarations [CommonTree atts, CommonTree mods, CommonTree type, CommonTree idxparams]: + indexer_accessor_declaration[atts, mods, type, idxparams]+; + +indexer_accessor_declaration [CommonTree atts, CommonTree mods, CommonTree type, CommonTree idxparams] +@init { + CommonTree idxBlock = null; +}: + la=attributes? lm=accessor_modifier? + (g='get' ((';')=> gbe=';' { idxBlock = $gbe.tree; } + | gb=block { idxBlock = $gb.tree; } ) geti=magicIdxGetter[atts, $la.tree, mods, $lm.tree, type, $g.token, idxBlock, idxparams] -> $geti + | s='set' ((';')=> sbe=';' { idxBlock = $sbe.tree; } + | sb=block { idxBlock = $sb.tree; } ) seti=magicIdxSetter[atts, $la.tree, mods, $lm.tree, type, $s.token, idxBlock, idxparams] -> $seti) + ; + /////////////////////////////////////////////////////// operator_declaration: operator_declarator operator_body ; @@ -1242,9 +1257,10 @@ operator_body: block ; /////////////////////////////////////////////////////// -constructor_declaration: - i=identifier '(' p=formal_parameter_list? ')' init=constructor_initializer? b=constructor_body[$init.tree] - -> $i $p? $b; +constructor_declaration[CommonTree atts, CommonTree mods, List modList]: + i=identifier '(' p=formal_parameter_list? ')' init=constructor_initializer? b=constructor_body[$init.tree] sb=magicSmotherExceptions[$b.tree] + -> {modList.Contains("static")}? ^(STATIC_CONSTRUCTOR[$i.tree.Token, "CONSTRUCTOR"] { dupTree($atts) } { dupTree($mods) } $sb) + -> ^(CONSTRUCTOR[$i.tree.Token, "CONSTRUCTOR"] { dupTree($atts) } { dupTree($mods) } $i $p? $b); constructor_initializer: ':' tok='this' '(' argument_list? ')' -> ^(APPLY[$tok.token, "APPLY"] $tok argument_list?) SEMI[$tok.token, ";"] @@ -1567,6 +1583,20 @@ magicGetterBody[IToken getTok, String varName]: magicSetterBody[IToken setTok, String varName]: -> OPEN_BRACE[setTok,"{"] IDENTIFIER[setTok, varName] ASSIGN[setTok,"="] IDENTIFIER[setTok, "value"] SEMI[setTok, ";"] CLOSE_BRACE[setTok,"}"] ; +magicIdxGetter[CommonTree atts, CommonTree localatts, CommonTree mods, CommonTree localmods, CommonTree type, IToken getTok, CommonTree body, CommonTree idxparams] +: + -> ^(METHOD[$type.token, "METHOD"] { dupTree(mods) } { dupTree(type)} IDENTIFIER[getTok, "get___idx"] { dupTree(idxparams) } { dupTree(body) } EXCEPTION[getTok, "Throwable"]) + ; +magicIdxSetter[CommonTree atts, CommonTree localatts, CommonTree mods, CommonTree localmods, CommonTree type, IToken setTok, CommonTree body, CommonTree idxparams] +@init { + CommonTree augParams = dupTree(idxparams); + adaptor.AddChild(augParams, dupTree($type)); + adaptor.AddChild(augParams, (CommonTree)adaptor.Create(IDENTIFIER, setTok, "value")); +} +: + -> ^(METHOD[$type.token, "METHOD"] { dupTree(mods) } ^(TYPE[setTok, "TYPE"] IDENTIFIER[setTok, "void"] ) IDENTIFIER[setTok, "set___idx"] { augParams } { dupTree(body) } EXCEPTION[setTok, "Throwable"] ) + ; + // keving: can't get this to work reasonably //magicMkConstModifiers[IToken tok, List filter]: // ({ !filter.Contains("static") }?=> -> STATIC[tok, "static"] ) ( { !filter.Contains("public") }?=> -> $magicMkConstModifiers FINAL[tok, "final"] ); diff --git a/CSharpTranslator/antlr3/src/cs2j/CSharp/JavaPrettyPrint.g b/CSharpTranslator/antlr3/src/cs2j/CSharp/JavaPrettyPrint.g index b920d45..d05b575 100644 --- a/CSharpTranslator/antlr3/src/cs2j/CSharp/JavaPrettyPrint.g +++ b/CSharpTranslator/antlr3/src/cs2j/CSharp/JavaPrettyPrint.g @@ -226,7 +226,7 @@ compilation_unit } : ^(PACKAGE nm=PAYLOAD modifiers? type_declaration[$modifiers.st] { if (IsLast) collectComments(); }) -> - package(now = {DateTime.Now}, includeDate = {true}, packageName = {($nm.text != null && $nm.text.Length > 0 ? $nm.text : null)}, + package(now = {DateTime.Now}, includeDate = {Cfg.TranslatorAddTimeStamp}, packageName = {($nm.text != null && $nm.text.Length > 0 ? $nm.text : null)}, type = {$type_declaration.st}, endComments = { CollectedComments }); @@ -269,6 +269,8 @@ class_member_declaration returns [List preComments]: | ^(CONVERSION_OPERATOR attributes? modifiers? conversion_operator_declaration) | ^(CONSTRUCTOR attributes? modifiers? identifier formal_parameter_list? { $preComments = CollectedComments; } block) -> constructor(modifiers={$modifiers.st}, name={ $identifier.st }, params={ $formal_parameter_list.st }, bodyIsSemi = { $block.isSemi }, body={ $block.st }) + | ^(STATIC_CONSTRUCTOR attributes? modifiers? block) + -> static_constructor(modifiers={$modifiers.st}, bodyIsSemi = { $block.isSemi }, body={ $block.st }) | ^(DESTRUCTOR attributes? modifiers? destructor_declaration) ; diff --git a/CSharpTranslator/antlr3/src/cs2j/CSharp/NetMaker.g b/CSharpTranslator/antlr3/src/cs2j/CSharp/NetMaker.g index 936d860..8853365 100644 --- a/CSharpTranslator/antlr3/src/cs2j/CSharp/NetMaker.g +++ b/CSharpTranslator/antlr3/src/cs2j/CSharp/NetMaker.g @@ -47,6 +47,7 @@ class_member_declaration: | ^(DELEGATE attributes? modifiers? delegate_declaration) | ^(CONVERSION_OPERATOR attributes? modifiers? conversion_operator_declaration) | ^(CONSTRUCTOR attributes? modifiers? identifier formal_parameter_list? block) + | ^(STATIC_CONSTRUCTOR attributes? modifiers? block) | ^(DESTRUCTOR attributes? modifiers? destructor_declaration) ; // class_member_declaration: diff --git a/CSharpTranslator/antlr3/src/cs2j/CSharp/cs.g b/CSharpTranslator/antlr3/src/cs2j/CSharp/cs.g index 91dda58..16058d2 100644 --- a/CSharpTranslator/antlr3/src/cs2j/CSharp/cs.g +++ b/CSharpTranslator/antlr3/src/cs2j/CSharp/cs.g @@ -46,6 +46,7 @@ tokens { APPLY; ARGS; NEW; + STATIC_CONSTRUCTOR; RETURN = 'return'; PRIVATE = 'private'; diff --git a/CSharpTranslator/antlr3/src/cs2j/bin/Debug/templates/java.stg b/CSharpTranslator/antlr3/src/cs2j/bin/Debug/templates/java.stg index 0d95372..5d7aa9b 100644 --- a/CSharpTranslator/antlr3/src/cs2j/bin/Debug/templates/java.stg +++ b/CSharpTranslator/antlr3/src/cs2j/bin/Debug/templates/java.stg @@ -61,6 +61,14 @@ constructor(modifiers, name, params, exception="Throwable", body, bodyIsSemi) :: <\n> >> +static_constructor(modifiers, body, bodyIsSemi) ::= << +; + + + +<\n> +>> + method(modifiers, typeparams, type, name, params, exceptions, body, bodyIsSemi) ::= << () throws ;