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
CSharpTranslator/antlr3/src/cs2j

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

@ -84,21 +84,28 @@ namespace_member_declarations:
namespace_member_declaration+ ; namespace_member_declaration+ ;
namespace_member_declaration: namespace_member_declaration:
namespace_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 // 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) // here will become a Java compilation unit (and go to its own file)
type_declaration type_declaration_pkg
@init { string ns = $NSContext::currentNS; } @init { string ns = $NSContext::currentNS; }
@after {
CUs.Add(ns+"."+$pkg.name, $type_declaration_pkg.tree);
}
: :
('partial') => p='partial' { Warning($p.line, "[UNSUPPORTED] 'partial' definition"); } pkg=type_declaration -> ^(PACKAGE PAYLOAD[ns] $pkg);
(pc=class_declaration { CUs.Add(ns+"."+$pc.name, $pc.tree); } type_declaration returns [string name]
| ps=struct_declaration { CUs.Add(ns+"."+$ps.name, $ps.tree); } :
| pi=interface_declaration { CUs.Add(ns+"."+$pi.name, $pi.tree); }) ('partial') => p='partial'! { Warning($p.line, "[UNSUPPORTED] 'partial' definition"); }
| c=class_declaration { CUs.Add(ns+"."+$c.name, $c.tree); } (pc=class_declaration { $name=$pc.name; }
| s=struct_declaration { CUs.Add(ns+"."+$s.name, $s.tree); } | ps=struct_declaration { $name=$ps.name; }
| i=interface_declaration { CUs.Add(ns+"."+$i.name, $i.tree); } | pi=interface_declaration { $name=$pi.name; })
| e=enum_declaration { CUs.Add(ns+"."+$e.name, $e.tree); } | c=class_declaration { $name=$c.name; }
| d=delegate_declaration { CUs.Add(ns+"."+$d.name, $d.tree); } ; | 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 // Identifiers
qualified_identifier returns [string thetext]: qualified_identifier returns [string thetext]:
i1=identifier { $thetext = $i1.text; } ('.' ip=identifier { $thetext += "." + $ip.text; } )*; i1=identifier { $thetext = $i1.text; } ('.' ip=identifier { $thetext += "." + $ip.text; } )*;

@ -13,47 +13,32 @@ options {
@header @header
{ {
using System.Collections; using System.Collections;
using System.Text.RegularExpressions;
} }
@members @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: compilation_unit:
package; ^(PACKAGE nm=PAYLOAD type_declaration) ->
package: package(now = {DateTime.Now}, includeDate = {true}, packageName = {$nm.text}, comments = {collectComments($type_declaration.start.TokenStartIndex)}, type = {$type_declaration.st});
(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 ;
type_declaration: type_declaration:
('partial') => 'partial' (class_declaration class_declaration
| struct_declaration
| interface_declaration)
| class_declaration
| struct_declaration | struct_declaration
| interface_declaration | interface_declaration
| enum_declaration | enum_declaration

@ -19,37 +19,10 @@ options {
} }
compilation_unit: 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: type_declaration:
('partial') => 'partial' (class_declaration class_declaration
| struct_declaration
| interface_declaration)
| class_declaration
| struct_declaration | struct_declaration
| interface_declaration | interface_declaration
| enum_declaration | enum_declaration

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

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

@ -39,7 +39,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants> <DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport> <ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel> <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>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType> <DebugType>pdbonly</DebugType>