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

Build packages from each top level type in the file. Emit comments :)

This commit is contained in:
Kevin Glynn 2010-11-13 19:11:47 +01:00
parent 16babbd494
commit 4a4a905eb5
7 changed files with 48 additions and 76 deletions

View File

@ -321,7 +321,7 @@ namespace RusticiSoftware.Translator.CSharp
netMaker.Filename = fullName;
netMaker.TraceDestination = Console.Error;
NetMaker.type_declaration_return javaCompilationUnit = netMaker.type_declaration();
NetMaker.compilation_unit_return javaCompilationUnit = netMaker.compilation_unit();
CommonTreeNodeStream javaCompilationUnitNodes = new CommonTreeNodeStream(javaCompilationUnit.Tree);
javaCompilationUnitNodes.TokenStream = csTree.TokenStream;
@ -333,7 +333,7 @@ namespace RusticiSoftware.Translator.CSharp
outputMaker.TemplateLib = templates;
StreamWriter javaW = new StreamWriter(javaFName);
javaW.Write(outputMaker.type_declaration().ToString());
javaW.Write(outputMaker.compilation_unit().ToString());
javaW.Close();
}
// ITreeNodeStream javaTree = java.Tree;

View File

@ -84,21 +84,28 @@ namespace_member_declarations:
namespace_member_declaration+ ;
namespace_member_declaration:
namespace_declaration
| attributes? modifiers? type_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
type_declaration_pkg
@init { string ns = $NSContext::currentNS; }
@after {
CUs.Add(ns+"."+$pkg.name, $type_declaration_pkg.tree);
}
:
('partial') => p='partial' { Warning($p.line, "[UNSUPPORTED] 'partial' definition"); }
(pc=class_declaration { CUs.Add(ns+"."+$pc.name, $pc.tree); }
| ps=struct_declaration { CUs.Add(ns+"."+$ps.name, $ps.tree); }
| pi=interface_declaration { CUs.Add(ns+"."+$pi.name, $pi.tree); })
| c=class_declaration { CUs.Add(ns+"."+$c.name, $c.tree); }
| s=struct_declaration { CUs.Add(ns+"."+$s.name, $s.tree); }
| i=interface_declaration { CUs.Add(ns+"."+$i.name, $i.tree); }
| e=enum_declaration { CUs.Add(ns+"."+$e.name, $e.tree); }
| d=delegate_declaration { CUs.Add(ns+"."+$d.name, $d.tree); } ;
pkg=type_declaration -> ^(PACKAGE PAYLOAD[ns] $pkg);
type_declaration returns [string name]
:
('partial') => p='partial'! { Warning($p.line, "[UNSUPPORTED] 'partial' definition"); }
(pc=class_declaration { $name=$pc.name; }
| ps=struct_declaration { $name=$ps.name; }
| pi=interface_declaration { $name=$pi.name; })
| c=class_declaration { $name=$c.name; }
| s=struct_declaration { $name=$s.name; }
| i=interface_declaration { $name=$i.name; }
| e=enum_declaration { $name=$e.name; }
| d=delegate_declaration { $name=$d.name; }
;
// Identifiers
qualified_identifier returns [string thetext]:
i1=identifier { $thetext = $i1.text; } ('.' ip=identifier { $thetext += "." + $ip.text; } )*;

View File

@ -13,47 +13,32 @@ options {
@header
{
using System.Collections;
using System.Text.RegularExpressions;
}
@members
{
protected int emittedCommentTokenIdx = 0;
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());
}
}
emittedCommentTokenIdx = endIdx+1;
return rets;
}
}
compilation_unit:
package;
package:
(PACKAGE string type_declaration) ->
package(now = {DateTime.Now}, includeDate = {true}, packageName = {$string}, type = {$type_declaration});
namespace_declaration:
'namespace' qualified_identifier namespace_block ';'? ;
namespace_block:
'{' namespace_body '}' ;
namespace_body:
extern_alias_directives? using_directives? global_attributes? namespace_member_declarations? ;
extern_alias_directives:
extern_alias_directive+ ;
extern_alias_directive:
'extern' 'alias' identifier ';' ;
using_directives:
using_directive+ ;
using_directive:
(using_alias_directive
| using_namespace_directive) ;
using_alias_directive:
'using' identifier '=' namespace_or_type_name ';' ;
using_namespace_directive:
'using' namespace_name ';' ;
namespace_member_declarations:
namespace_member_declaration+ ;
namespace_member_declaration:
namespace_declaration
| attributes? modifiers? type_declaration ;
^(PACKAGE nm=PAYLOAD type_declaration) ->
package(now = {DateTime.Now}, includeDate = {true}, packageName = {$nm.text}, comments = {collectComments($type_declaration.start.TokenStartIndex)}, type = {$type_declaration.st});
type_declaration:
('partial') => 'partial' (class_declaration
| struct_declaration
| interface_declaration)
| class_declaration
class_declaration
| struct_declaration
| interface_declaration
| enum_declaration

View File

@ -19,37 +19,10 @@ options {
}
compilation_unit:
namespace_body[true];
^(PACKAGE PAYLOAD type_declaration);
namespace_declaration:
'namespace' qualified_identifier namespace_block ';'? ;
namespace_block:
'{' namespace_body[false] '}' ;
namespace_body[bool bGlobal]:
extern_alias_directives? using_directives? global_attributes? namespace_member_declarations? ;
extern_alias_directives:
extern_alias_directive+ ;
extern_alias_directive:
'extern' 'alias' identifier ';' ;
using_directives:
using_directive+ ;
using_directive:
(using_alias_directive
| using_namespace_directive) ;
using_alias_directive:
'using' identifier '=' namespace_or_type_name ';' ;
using_namespace_directive:
'using' namespace_name ';' ;
namespace_member_declarations:
namespace_member_declaration+ ;
namespace_member_declaration:
namespace_declaration
| attributes? modifiers? type_declaration ;
type_declaration:
('partial') => 'partial' (class_declaration
| struct_declaration
| interface_declaration)
| class_declaration
class_declaration
| struct_declaration
| interface_declaration
| enum_declaration

View File

@ -6,6 +6,11 @@ options {
language=CSharp2;
}
tokens {
PACKAGE;
PAYLOAD; // carries arbitrary text for the output file
}
@namespace { RusticiSoftware.Translator.CSharp }
@lexer::header

View File

@ -16,9 +16,11 @@ itsmine(now, includeDate) ::= <<
>>
package(now, includeDate, packageName, type) ::= <<
package(now, includeDate, packageName, comments, type) ::= <<
<itsmine(now=now,includeDate=includeDate)>
package <packageName>;
<comments; separator="\n">
<type>
>>

View File

@ -39,7 +39,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Commandlineparameters>-v -v -v -dumpcsharp -debug 10 /Users/keving/gitrepos/cs2j/CSharpTranslator/antlr3/cs2jTest/TestDLLs/Various.cs</Commandlineparameters>
<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>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>