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

configurable throws Throwable/Exception

This commit is contained in:
Kevin Glynn 2011-02-07 14:49:20 +01:00
parent 0eb19d14a5
commit 4cb4a2bfd5
5 changed files with 41 additions and 29 deletions

View File

@ -1,7 +1,7 @@
package RusticiSoftware.System; package RusticiSoftware.System;
public interface IDisposable { public interface IDisposable {
void Dispose() throws Throwable; void Dispose() throws Exception;
void close() throws Throwable; void close() throws Exception;
} }

View File

@ -43,6 +43,11 @@ namespace RusticiSoftware.Translator.CSharp
get; set; get; set;
} }
public bool TranslatorExceptionIsThrowable
{
get; set;
}
public CS2JSettings () public CS2JSettings ()
{ {
@ -68,10 +73,11 @@ namespace RusticiSoftware.Translator.CSharp
Verbosity = 0; Verbosity = 0;
DebugTemplateExtraction = true; DebugTemplateExtraction = true;
DebugLevel = 0; DebugLevel = 0;
Warnings = false; Warnings = true;
TranslatorKeepParens = true; TranslatorKeepParens = true;
TranslatorAddTimeStamp = true; TranslatorAddTimeStamp = true;
TranslatorExceptionIsThrowable = false;
} }
} }
} }

View File

@ -105,6 +105,7 @@ namespace RusticiSoftware.Translator.CSharp
.Add ("exclude=", dirs => addDirectories(cfg.Exclude, dirs)) .Add ("exclude=", dirs => addDirectories(cfg.Exclude, dirs))
.Add ("translator-keep-parens=", v => cfg.TranslatorKeepParens = Boolean.Parse(v)) .Add ("translator-keep-parens=", v => cfg.TranslatorKeepParens = Boolean.Parse(v))
.Add ("translator-timestamp-files=", v => cfg.TranslatorAddTimeStamp = Boolean.Parse(v)) .Add ("translator-timestamp-files=", v => cfg.TranslatorAddTimeStamp = Boolean.Parse(v))
.Add ("translator-exception-is-throwable=", v => cfg.TranslatorExceptionIsThrowable = Boolean.Parse(v))
; ;
//TODO: fix enum dump //TODO: fix enum dump

View File

@ -976,7 +976,7 @@ method_declaration [CommonTree atts, CommonTree mods, List<string> modList, Comm
member_name type_parameter_constraints_clauses? type_parameter_list? formal_parameter_list? $b { exceptions }); member_name type_parameter_constraints_clauses? type_parameter_list? formal_parameter_list? $b { exceptions });
method_body [bool smotherExceptions] returns [CommonTree exceptionList]: method_body [bool smotherExceptions] returns [CommonTree exceptionList]:
b=block nb=magicSmotherExceptions[ dupTree($b.tree) ] el=magicThrowable { if (!smotherExceptions) $exceptionList=$el.tree; } b=block nb=magicSmotherExceptions[ dupTree($b.tree) ] el=magicThrowsException[true,$b.tree.Token] { if (!smotherExceptions) $exceptionList=$el.tree; }
-> {smotherExceptions}? $nb -> {smotherExceptions}? $nb
-> $b -> $b
; ;
@ -1205,9 +1205,9 @@ interface_property_declaration [CommonTree atts, CommonTree mods, CommonTree typ
i=identifier '{' iads=interface_accessor_declarations[atts, mods, type, $i.text] '}' -> $iads ; i=identifier '{' iads=interface_accessor_declarations[atts, mods, type, $i.text] '}' -> $iads ;
interface_method_declaration [CommonTree atts, CommonTree mods, CommonTree type]: interface_method_declaration [CommonTree atts, CommonTree mods, CommonTree type]:
identifier generic_argument_list? identifier generic_argument_list?
'(' formal_parameter_list? ')' type_parameter_constraints_clauses? magicThrowable ';' '(' formal_parameter_list? ')' type_parameter_constraints_clauses? s=';' magicThrowsException[true,$s.token]
-> ^(METHOD { dupTree($atts) } { dupTree($mods) } { dupTree($type) } -> ^(METHOD { dupTree($atts) } { dupTree($mods) } { dupTree($type) }
identifier type_parameter_constraints_clauses? generic_argument_list? formal_parameter_list? magicThrowable); identifier type_parameter_constraints_clauses? generic_argument_list? formal_parameter_list? magicThrowsException);
interface_event_declaration [CommonTree atts, CommonTree mods]: interface_event_declaration [CommonTree atts, CommonTree mods]:
//attributes? 'new'? //attributes? 'new'?
'event' type identifier ';' ; 'event' type identifier ';' ;
@ -1282,9 +1282,9 @@ operator_body:
/////////////////////////////////////////////////////// ///////////////////////////////////////////////////////
constructor_declaration[CommonTree atts, CommonTree mods, List<String> modList]: constructor_declaration[CommonTree atts, CommonTree mods, List<String> modList]:
i=identifier '(' p=formal_parameter_list? ')' init=constructor_initializer? b=constructor_body[$init.tree] sb=magicSmotherExceptionsThrow[$b.tree, "ExceptionInInitializerError"] magicThrowable i=identifier '(' p=formal_parameter_list? s=')' init=constructor_initializer? b=constructor_body[$init.tree] sb=magicSmotherExceptionsThrow[$b.tree, "ExceptionInInitializerError"] magicThrowsException[true,$s.token]
-> {modList.Contains("static")}? ^(STATIC_CONSTRUCTOR[$i.tree.Token, "CONSTRUCTOR"] { dupTree($atts) } { dupTree($mods) } $sb) -> {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 magicThrowable); -> ^(CONSTRUCTOR[$i.tree.Token, "CONSTRUCTOR"] { dupTree($atts) } { dupTree($mods) } $i $p? $b magicThrowsException);
constructor_initializer: constructor_initializer:
':' tok='this' '(' argument_list? ')' ':' tok='this' '(' argument_list? ')'
-> ^(APPLY[$tok.token, "APPLY"] $tok argument_list?) SEMI[$tok.token, ";"] -> ^(APPLY[$tok.token, "APPLY"] $tok argument_list?) SEMI[$tok.token, ";"]
@ -1475,7 +1475,7 @@ scope { CommonTree throwVar; }
CommonTree ty = null, var = null; CommonTree ty = null, var = null;
}: }:
c='catch' ('(' given_t=class_type { ty = $given_t.tree; } (given_v=identifier { var = $given_v.tree; } | magic_v=magicCatchVar { var = $magic_v.tree; } ) ')' c='catch' ('(' given_t=class_type { ty = $given_t.tree; } (given_v=identifier { var = $given_v.tree; } | magic_v=magicCatchVar { var = $magic_v.tree; } ) ')'
| magic_t=magicThrowableType magic_v=magicCatchVar { ty = $magic_t.tree; var = $magic_v.tree; }) { $catch_clause::throwVar = var; } block | magic_t=magicThrowableType[true,$c.token] magic_v=magicCatchVar { ty = $magic_t.tree; var = $magic_v.tree; }) { $catch_clause::throwVar = var; } block
-> ^($c { ty } { var } block) -> ^($c { ty } { var } block)
; ;
finally_clause: finally_clause:
@ -1572,8 +1572,10 @@ literal:
void_type: void_type:
v='void' -> ^(TYPE[$v.token, "TYPE"] $v); v='void' -> ^(TYPE[$v.token, "TYPE"] $v);
magicThrowableType: magicThrowableType[bool isOn, IToken tok]:
-> ^(TYPE["TYPE"] IDENTIFIER["Throwable"]); -> {isOn}? ^(TYPE[tok, "TYPE"] IDENTIFIER[tok, Cfg.TranslatorExceptionIsThrowable ? "Throwable" : "Exception"])
->
;
magicCatchVar: magicCatchVar:
-> IDENTIFIER["__dummyCatchVar" + dummyCatchVarCtr++]; -> IDENTIFIER["__dummyCatchVar" + dummyCatchVarCtr++];
@ -1583,7 +1585,7 @@ magicPropGetter[CommonTree atts, CommonTree localatts, CommonTree mods, CommonTr
CommonTree realBody = body; CommonTree realBody = body;
CommonTree exceptionList = null; CommonTree exceptionList = null;
}: }:
( { mkBody }? => b=magicGetterBody[getTok,varName] { realBody = $b.tree; } | e=magicException { exceptionList = $e.tree; }) ( { mkBody }? => b=magicGetterBody[getTok,varName] { realBody = $b.tree; } | e=magicThrowsException[true,getTok] { exceptionList = $e.tree; })
-> ^(METHOD[$type.token, "METHOD"] { dupTree(mods) } { dupTree(type)} IDENTIFIER[getTok, "get"+propName] { dupTree(realBody) } { exceptionList }) -> ^(METHOD[$type.token, "METHOD"] { dupTree(mods) } { dupTree(type)} IDENTIFIER[getTok, "get"+propName] { dupTree(realBody) } { exceptionList })
; ;
magicPropSetter[CommonTree atts, CommonTree localatts, CommonTree mods, CommonTree localmods, CommonTree type, IToken setTok, CommonTree body, String propName, bool mkBody, String varName] magicPropSetter[CommonTree atts, CommonTree localatts, CommonTree mods, CommonTree localmods, CommonTree type, IToken setTok, CommonTree body, String propName, bool mkBody, String varName]
@ -1591,7 +1593,7 @@ magicPropSetter[CommonTree atts, CommonTree localatts, CommonTree mods, CommonTr
CommonTree realBody = body; CommonTree realBody = body;
CommonTree exceptionList = null; CommonTree exceptionList = null;
}: }:
( { mkBody }? => b=magicSetterBody[setTok,varName] { realBody = $b.tree; }| e=magicException { exceptionList = $e.tree; } ) ( { mkBody }? => b=magicSetterBody[setTok,varName] { realBody = $b.tree; }| e=magicThrowsException[true,setTok] { exceptionList = $e.tree; } )
-> ^(METHOD[$type.token, "METHOD"] { dupTree(mods) } ^(TYPE[setTok, "TYPE"] IDENTIFIER[setTok, "void"] ) IDENTIFIER[setTok, "set"+propName] ^(PARAMS[setTok, "PARAMS"] { dupTree(type)} IDENTIFIER[setTok, "value"]) { dupTree(realBody) } { exceptionList } ) -> ^(METHOD[$type.token, "METHOD"] { dupTree(mods) } ^(TYPE[setTok, "TYPE"] IDENTIFIER[setTok, "void"] ) IDENTIFIER[setTok, "set"+propName] ^(PARAMS[setTok, "PARAMS"] { dupTree(type)} IDENTIFIER[setTok, "value"]) { dupTree(realBody) } { exceptionList } )
; ;
@ -1609,7 +1611,8 @@ magicSetterBody[IToken setTok, String varName]:
magicIdxGetter[CommonTree atts, CommonTree localatts, CommonTree mods, CommonTree localmods, CommonTree type, IToken getTok, CommonTree body, CommonTree idxparams] 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"]) magicThrowsException[true,getTok]
-> ^(METHOD[$type.token, "METHOD"] { dupTree(mods) } { dupTree(type)} IDENTIFIER[getTok, "get___idx"] { dupTree(idxparams) } { dupTree(body) } magicThrowsException)
; ;
magicIdxSetter[CommonTree atts, CommonTree localatts, CommonTree mods, CommonTree localmods, CommonTree type, IToken setTok, CommonTree body, CommonTree idxparams] magicIdxSetter[CommonTree atts, CommonTree localatts, CommonTree mods, CommonTree localmods, CommonTree type, IToken setTok, CommonTree body, CommonTree idxparams]
@init { @init {
@ -1617,36 +1620,30 @@ magicIdxSetter[CommonTree atts, CommonTree localatts, CommonTree mods, CommonTre
adaptor.AddChild(augParams, dupTree($type)); adaptor.AddChild(augParams, dupTree($type));
adaptor.AddChild(augParams, (CommonTree)adaptor.Create(IDENTIFIER, setTok, "value")); 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"] ) magicThrowsException[true,setTok]
-> ^(METHOD[$type.token, "METHOD"] { dupTree(mods) } ^(TYPE[setTok, "TYPE"] IDENTIFIER[setTok, "void"] ) IDENTIFIER[setTok, "set___idx"] { augParams } { dupTree(body) } magicThrowsException )
; ;
// keving: can't get this to work reasonably // keving: can't get this to work reasonably
//magicMkConstModifiers[IToken tok, List<string> filter]: //magicMkConstModifiers[IToken tok, List<string> filter]:
// ({ !filter.Contains("static") }?=> -> STATIC[tok, "static"] ) ( { !filter.Contains("public") }?=> -> $magicMkConstModifiers FINAL[tok, "final"] ); // ({ !filter.Contains("static") }?=> -> STATIC[tok, "static"] ) ( { !filter.Contains("public") }?=> -> $magicMkConstModifiers FINAL[tok, "final"] );
magicException:
-> EXCEPTION["Throwable"]
;
magicSmotherExceptions[CommonTree body]: magicSmotherExceptions[CommonTree body]:
magicSmotherExceptionsThrow[body, "RuntimeException"] magicSmotherExceptionsThrow[body, "RuntimeException"]
; ;
magicSmotherExceptionsThrow[CommonTree body, String exception]: magicSmotherExceptionsThrow[CommonTree body, String exception]:
v=magicCatchVar v=magicCatchVar magicThrowableType[true, body.Token]
-> OPEN_BRACE["{"] -> OPEN_BRACE["{"]
^(TRY["try"] ^(TRY["try"]
{ dupTree(body) } { dupTree(body) }
^(CATCH["catch"] ^(TYPE["TYPE"] IDENTIFIER["Throwable"]) { dupTree($v.tree) } ^(CATCH["catch"] magicThrowableType { dupTree($v.tree) }
OPEN_BRACE["{"] ^(THROW["throw"] ^(NEW["new"] ^(TYPE["TYPE"] IDENTIFIER[exception]) ^(ARGS["ARGS"] { dupTree($v.tree) }))) CLOSE_BRACE["}"])) OPEN_BRACE["{"] ^(THROW["throw"] ^(NEW["new"] ^(TYPE["TYPE"] IDENTIFIER[exception]) ^(ARGS["ARGS"] { dupTree($v.tree) }))) CLOSE_BRACE["}"]))
CLOSE_BRACE["}"] CLOSE_BRACE["}"]
; ;
magicThrowable:
-> EXCEPTION["Throwable"]
;
// METHOD{ public static TYPE{ void } main PARAMS{ TYPE{ String [ ] } args } { APPLY{ .{ System exit } ARGS{ APPLY{ .{ Program Main } } } } ; // METHOD{ public static TYPE{ void } main PARAMS{ TYPE{ String [ ] } args } { APPLY{ .{ System exit } ARGS{ APPLY{ .{ Program Main } } } } ;
magicMainArgs[bool isOn, IToken tok]: magicMainArgs[bool isOn, IToken tok]:
@ -1671,13 +1668,14 @@ magicMainExit[bool isOn, bool retInt, IToken tok, CommonTree body]:
magicMainWrapper[bool isOn, IToken tok, CommonTree body]: magicMainWrapper[bool isOn, IToken tok, CommonTree body]:
magicThrowsException[isOn,tok]
-> { isOn }? -> { isOn }?
^(METHOD[tok, "METHOD"] ^(METHOD[tok, "METHOD"]
PUBLIC[tok, "public"] STATIC[tok,"static"] PUBLIC[tok, "public"] STATIC[tok,"static"]
^(TYPE[tok, "TYPE"] IDENTIFIER[tok, "void"]) ^(TYPE[tok, "TYPE"] IDENTIFIER[tok, "void"])
IDENTIFIER[tok, "main"] ^(PARAMS[tok, "PARAMS"] ^(TYPE[tok, "TYPE"] IDENTIFIER[tok,"String"] OPEN_BRACKET[tok, "["] CLOSE_BRACKET[tok, "]"]) IDENTIFIER[tok, "args"]) IDENTIFIER[tok, "main"] ^(PARAMS[tok, "PARAMS"] ^(TYPE[tok, "TYPE"] IDENTIFIER[tok,"String"] OPEN_BRACKET[tok, "["] CLOSE_BRACKET[tok, "]"]) IDENTIFIER[tok, "args"])
OPEN_BRACE[tok, "{"] { dupTree(body) } SEMI[tok, ";"] CLOSE_BRACE[tok, "}"] OPEN_BRACE[tok, "{"] { dupTree(body) } SEMI[tok, ";"] CLOSE_BRACE[tok, "}"]
EXCEPTION[tok, "Throwable"]) magicThrowsException)
-> ->
; ;
@ -1697,6 +1695,7 @@ magicFinally[IToken tok, CommonTree statement_list]:
; ;
magicFinalize[IToken tok, CommonTree body]: magicFinalize[IToken tok, CommonTree body]:
magicThrowsException[true,tok]
-> ->
^(METHOD[tok, "METHOD"] ^(METHOD[tok, "METHOD"]
PROTECTED[tok, "protected"] PROTECTED[tok, "protected"]
@ -1704,8 +1703,9 @@ magicFinalize[IToken tok, CommonTree body]:
OPEN_BRACE[tok, "{"] OPEN_BRACE[tok, "{"]
^(TRY[tok, "try"] { dupTree(body) } ^(TRY[tok, "try"] { dupTree(body) }
^(FINALLY[tok, "finally"] OPEN_BRACE[tok, "{"] ^(APPLY[tok, "APPLY"] ^(DOT[tok,"."] SUPER[tok,"super"] IDENTIFIER[tok,"finalize"])) SEMI[tok, ";"] CLOSE_BRACE[tok, "}"])) ^(FINALLY[tok, "finally"] OPEN_BRACE[tok, "{"] ^(APPLY[tok, "APPLY"] ^(DOT[tok,"."] SUPER[tok,"super"] IDENTIFIER[tok,"finalize"])) SEMI[tok, ";"] CLOSE_BRACE[tok, "}"]))
CLOSE_BRACE[tok, "}"] CLOSE_BRACE[tok, "}"]
EXCEPTION[tok, "Throwable"]) // Always throws Throwable to match Object.finalize()
EXCEPTION[tok, "Throwable"])
; ;
magicDefaultConstructor[IToken tok, String name]: magicDefaultConstructor[IToken tok, String name]:
@ -1718,3 +1718,8 @@ magicDefaultConstructor[IToken tok, String name]:
) )
; ;
magicThrowsException[bool isOn, IToken tok]:
-> {isOn}? EXCEPTION[tok, Cfg.TranslatorExceptionIsThrowable ? "Throwable" : "Exception"]
->
;

View File

@ -1981,7 +1981,7 @@ magicCastOperator[CommonTree mods, String methodName, CommonTree header, CommonT
{ dupTree($mods) } { dupTree($mods) }
{ toType } IDENTIFIER[tok, $methodName] ^(PARAMS[tok, "PARAMS"] { fromType } { paramName}) { toType } IDENTIFIER[tok, $methodName] ^(PARAMS[tok, "PARAMS"] { fromType } { paramName})
{ dupTree(body) } { dupTree(body) }
EXCEPTION[tok, "Throwable"]) EXCEPTION[tok, Cfg.TranslatorExceptionIsThrowable ? "Throwable" : "Exception"])
; ;
magicAnnotation [CommonTree mods, CommonTree name, CommonTree body, IToken tok]: magicAnnotation [CommonTree mods, CommonTree name, CommonTree body, IToken tok]: