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

support for straightforward enums

This commit is contained in:
Kevin Glynn 2010-11-16 13:51:43 +01:00
parent 01c71e3852
commit f9e748242c
6 changed files with 96 additions and 44 deletions

View File

@ -456,7 +456,7 @@ namespace RusticiSoftware.Translator.CSharp
//System.Console.Out.WriteLine(writer.ToString());
System.Console.Out.WriteLine("");
System.Console.Out.WriteLine("");
System.Console.Out.WriteLine("Pretty-printed {0} in: {1} seconds.", fullName, elapsedTime);
System.Console.Out.WriteLine("Processed {0} in: {1} seconds.", fullName, elapsedTime);
}
}
}

View File

@ -86,19 +86,20 @@ using_namespace_directive:
'using' namespace_name ';' ;
namespace_member_declarations:
namespace_member_declaration+ ;
namespace_member_declaration:
namespace_declaration
| attributes? modifiers? type_declaration_pkg ;
// 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_pkg
@init { string ns = $NSContext::currentNS; }
namespace_member_declaration
@init { string ns = $NSContext::currentNS;
bool isCompUnit = false;}
@after {
CUMap.Add(ns+"."+$pkg.name, $type_declaration_pkg.tree);
CUKeys.Add(ns+"."+$pkg.name);
if (isCompUnit) {
CUMap.Add(ns+"."+$ty.name, $namespace_member_declaration.tree);
CUKeys.Add(ns+"."+$ty.name);
};
}
:
pkg=type_declaration -> ^(PACKAGE PAYLOAD[ns] $pkg);
namespace_declaration
| attributes? modifiers? ty=type_declaration { isCompUnit = true; } -> ^(PACKAGE[$ty.start.Token] PAYLOAD[ns] modifiers? 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]
:
('partial') => p='partial'! { Warning($p.line, "[UNSUPPORTED] 'partial' definition"); }
@ -120,8 +121,8 @@ namespace_name
modifiers:
modifier+ ;
modifier:
'new' | 'public' | 'protected' | 'private' | 'internal' | 'unsafe' | 'abstract' | 'sealed' | 'static'
| 'readonly' | 'volatile' | 'extern' | 'virtual' | 'override';
'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?
@ -699,11 +700,30 @@ enum_declaration returns [string name]:
enum_base:
':' integral_type ;
enum_body:
'{' (enum_member_declarations ','?)? '}' ;
enum_member_declarations:
enum_member_declaration (',' enum_member_declaration)* ;
enum_member_declaration:
attributes? identifier ('=' expression)? ;
'{' (enum_member_declarations ','?)? '}' -> ^(ENUM_BODY enum_member_declarations) ;
enum_member_declarations
@init {
SortedList<int,CommonTree> members = new SortedList<int,CommonTree>();
int next = 0;
}
@after{
$enum_member_declarations.tree = (CommonTree)adaptor.Nil;
int dummyCounter = 0;
for (int i = 0; i < next; i++) {
if (members.ContainsKey(i)) {
adaptor.AddChild($enum_member_declarations.tree, members[i]);
}
else {
adaptor.AddChild($enum_member_declarations.tree, adaptor.Create(IDENTIFIER, $e.start.Token, "__dummyEnum__" + dummyCounter++));
}
};
}
:
e=enum_member_declaration[members,ref next] (',' enum_member_declaration[members, ref next])*
->
;
enum_member_declaration[ SortedList<int,CommonTree> members, ref int next]:
attributes? identifier { $members[next] = $identifier.tree; $next++; } (('=' expression)?)! ;
//enum_modifiers:
// enum_modifier+ ;
//enum_modifier:

View File

@ -27,12 +27,14 @@ options {
protected List<string> collectComments(int endIdx) {
List<string> rets = new List<string>();
List<IToken> toks = ((CommonTokenStream)this.GetTreeNodeStream().TokenStream).GetTokens(emittedCommentTokenIdx,endIdx);
foreach (IToken tok in toks) {
if (tok.Channel == TokenChannels.Hidden) {
rets.Add(new Regex("(\\n|\\r)+").Replace(tok.Text, Environment.NewLine).Trim());
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;
}
protected List<string> collectComments() {
@ -40,10 +42,14 @@ options {
}
}
compilation_unit:
^(PACKAGE nm=PAYLOAD type_declaration) ->
compilation_unit
@init {
List<string> preComments = null;
}:
^(PACKAGE { preComments = collectComments($PACKAGE.TokenStartIndex); } nm=PAYLOAD modifiers? type_declaration) ->
package(now = {DateTime.Now}, includeDate = {true}, packageName = {$nm.text},
comments = {collectComments($type_declaration.start.TokenStartIndex)},
comments = { preComments },
modifiers = {$modifiers.st},
type = {$type_declaration.st},
endComments = {IsLast ? collectComments() : null});
@ -51,7 +57,7 @@ type_declaration:
class_declaration
| struct_declaration
| interface_declaration
| enum_declaration
| enum_declaration -> { $enum_declaration.st }
| delegate_declaration ;
// Identifiers
qualified_identifier:
@ -60,10 +66,12 @@ namespace_name
: namespace_or_type_name ;
modifiers:
modifier+ ;
modifier:
'new' | 'public' | 'protected' | 'private' | 'internal' | 'unsafe' | 'abstract' | 'sealed' | 'static'
| 'readonly' | 'volatile' | 'extern' | 'virtual' | 'override';
ms+=modifier+ -> modifiers(mods={$ms});
modifier
:
(m='new' | m='public' | m='protected' | m='private' | m='abstract' | m='sealed' | m='static'
| m='readonly' | m='volatile' | m='extern' | m='virtual' | m='override' | m=FINAL)
-> string(payload={$m.text});
class_member_declaration:
attributes?
@ -621,15 +629,14 @@ remove_accessor_declaration:
// enum declaration
///////////////////////////////////////////////////////
enum_declaration:
'enum' identifier enum_base? enum_body ';'? ;
'enum' identifier enum_base? enum_body ';'?
-> enum(name={$identifier.text}, body={$enum_body.st}) ;
enum_base:
':' integral_type ;
enum_body:
'{' (enum_member_declarations ','?)? '}' ;
enum_member_declarations:
enum_member_declaration (',' enum_member_declaration)* ;
^(ENUM_BODY es+=enum_member_declaration+) -> enum_body(values={$es});
enum_member_declaration:
attributes? identifier ('=' expression)? ;
attributes? identifier -> enum_member(comments = {collectComments($identifier.start.TokenStartIndex)}, value={ $identifier.st });
//enum_modifiers:
// enum_modifier+ ;
//enum_modifier:
@ -1015,7 +1022,7 @@ predefined_type:
| 'short' | 'string' | 'uint' | 'ulong' | 'ushort' ;
identifier:
IDENTIFIER | also_keyword;
IDENTIFIER -> template(v= { $IDENTIFIER.text }) "<v>" | also_keyword -> template(v= { $also_keyword.text }) "<v>";
keyword:
'abstract' | 'as' | 'base' | 'bool' | 'break' | 'byte' | 'case' | 'catch' | 'char' | 'checked' | 'class' | 'const' | 'continue' | 'decimal' | 'default' | 'delegate' | 'do' | 'double' | 'else' | 'enum' | 'event' | 'explicit' | 'extern' | 'false' | 'finally' | 'fixed' | 'float' | 'for' | 'foreach' | 'goto' | 'if' | 'implicit' | 'in' | 'int' | 'interface' | 'internal' | 'is' | 'lock' | 'long' | 'namespace' | 'new' | 'null' | 'object' | 'operator' | 'out' | 'override' | 'params' | 'private' | 'protected' | 'public' | 'readonly' | 'ref' | 'return' | 'sbyte' | 'sealed' | 'short' | 'sizeof' | 'stackalloc' | 'static' | 'string' | 'struct' | 'switch' | 'this' | 'throw' | 'true' | 'try' | 'typeof' | 'uint' | 'ulong' | 'unchecked' | 'unsafe' | 'ushort' | 'using' | 'virtual' | 'void' | 'volatile' ;

View File

@ -15,7 +15,7 @@ options {
}
compilation_unit:
^(PACKAGE PAYLOAD type_declaration);
^(PACKAGE PAYLOAD modifiers? type_declaration);
type_declaration:
class_declaration
@ -32,8 +32,8 @@ namespace_name
modifiers:
modifier+ ;
modifier:
'new' | 'public' | 'protected' | 'private' | 'internal' | 'unsafe' | 'abstract' | 'sealed' | 'static'
| 'readonly' | 'volatile' | 'extern' | 'virtual' | 'override';
'new' | 'public' | 'protected' | 'private' | 'abstract' | 'sealed' | 'static'
| 'readonly' | 'volatile' | 'extern' | 'virtual' | 'override' | FINAL ;
class_member_declaration:
attributes?
@ -595,11 +595,11 @@ enum_declaration:
enum_base:
':' integral_type ;
enum_body:
'{' (enum_member_declarations ','?)? '}' ;
^(ENUM_BODY enum_member_declarations) ;
enum_member_declarations:
enum_member_declaration (',' enum_member_declaration)* ;
enum_member_declaration+ ;
enum_member_declaration:
attributes? identifier ('=' expression)? ;
attributes? identifier ;
//enum_modifiers:
// enum_modifier+ ;
//enum_modifier:

View File

@ -8,6 +8,10 @@ options {
tokens {
PACKAGE;
ENUM_BODY;
FINAL; /* final modifier */
PAYLOAD; // carries arbitrary text for the output file
}

View File

@ -16,13 +16,34 @@ itsmine(now, includeDate) ::= <<
>>
package(now, includeDate, packageName, comments, type, endComments) ::= <<
package(now, includeDate, packageName, comments, modifiers, type, endComments) ::= <<
<itsmine(now=now,includeDate=includeDate)>
package <packageName>;
<comments; separator="\n">
<type>
<modifiers> <type>
<endComments; separator="\n">
>>
enum(comments, attributes, name, body) ::= <<
<comments; separator="\n">
enum <name>
{
<body>
}
>>
enum_body(values) ::= "<values; separator=\",\n\">"
enum_member(comments, value) ::= <<
<comments; separator="\n">
<value>
>>
modifiers(mods) ::= "<mods; separator=\" \">"
// utility
string(payload) ::= "<payload>"