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

Emit interfaces and structs (as class)

Don't emit all comments again when multiple classes in a file
This commit is contained in:
Kevin Glynn 2010-11-18 14:23:17 +01:00
parent ba1bf1cb4c
commit 765e18384e
7 changed files with 108 additions and 63 deletions

View File

@ -286,7 +286,7 @@ namespace RusticiSoftware.Translator.CSharp
javaMaker.CUKeys = new List<string>();
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;
}

View File

@ -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:

View File

@ -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<string> collectComments(int endIdx) {
List<string> rets = new List<string>();
List<IToken> toks = ((CommonTokenStream)this.GetTreeNodeStream().TokenStream).GetTokens(emittedCommentTokenIdx,endIdx);
List<IToken> 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 }) "<name><args>"
| 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 }) "\<<args>\>";
type_arguments:
type (',' type)* ;
ts+=type (',' ts+=type)* -> template(types = { $ts }) "<types; separator=\",\">";
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<string> stars = new List<string>();
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<string> 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 }) "<types; separator=\",\">";
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<string> 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 }) "<v>" | also_keyword -> template(v= { $also_keyword.text }) "<v>";

View File

@ -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:

View File

@ -10,7 +10,9 @@ tokens {
PACKAGE;
ENUM_BODY;
CLASS;
EXTENDS;
IMPLEMENTS;
INTERFACE;
FINAL; /* final modifier */
PAYLOAD; // carries arbitrary text for the output file

View File

@ -28,9 +28,17 @@ package <packageName>;
// ******* CLASSES ***********
class(modifiers, comments, attributes, name, body) ::= <<
class(modifiers, comments, attributes, name, inherits, body) ::= <<
<comments; separator="\n">
<modifiers(modifiers)>class <name>
<modifiers(modifiers)>class <name> <inherits>
{
<body>
}
>>
iface(modifiers, comments, attributes, name, imps, body) ::= <<
<comments; separator="\n">
<modifiers(modifiers)>interface <name> <imps>
{
<body>
}
@ -53,8 +61,13 @@ enum_member(comments, value) ::= <<
<value>
>>
type(name, stars, opt) ::= "<name><stars><opt>"
namespace_or_type(type1, type2, types) ::= "<type1><if(type2)>::<type2><endif><if(types)>.<types; separator=\".\"><endif>"
modifiers(mods) ::= "<mods; separator=\" \"><if(mods)> <endif>"
//modifiers(mods) ::= "<mods; separator=\" \">"
extends(types) ::= "<if(types)>extends <types; separator=\",\"><endif>"
imps(types) ::= "<if(types)>implements <types; separator=\",\"><endif>"
// ******* UTILITY ***********
string(payload) ::= "<payload>"
string(payload) ::= "<payload>"

View File

@ -39,7 +39,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Commandlineparameters>-warnings -debug 10 -dumpxmls -xmldir=/tmp/xml/se -odir=/tmp/java/se /Users/keving/svnrepos/ScormEngineNet/src/app/ScormEngine.Core/Logic/Types/Mode.cs</Commandlineparameters>
<Commandlineparameters>-warnings -debug 10 -dumpxmls -xmldir=/tmp/xml/se -odir=/tmp/java/se /Users/keving/svnrepos/ScormEngineNet/src/app/ScormEngine.Core/DataHelp/IDataHelper.cs</Commandlineparameters>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>