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

Add CS2JSettings to simplify passing command line arguments to phases

Update cs.g to use recommended one from upstream UnitTests/cs.g
This commit is contained in:
Kevin Glynn 2010-10-26 16:32:48 +02:00
parent 70a3f53b38
commit bd7faabd37
7 changed files with 1606 additions and 1529 deletions

View File

@ -0,0 +1,60 @@
using System;
using System.Collections.Generic;
using RusticiSoftware.Translator.Utils;
using System.IO;
namespace RusticiSoftware.Translator.CSharp
{
public class CS2JSettings
{
public bool DisplayTokens { get; set; }
// dump parse trees to stdout
public bool DumpCSharp { get; set; }
public bool DumpJavaSyntax { get; set; }
public bool DumpJava { get; set; }
public bool DumpXmls { get; set; }
public bool DumpEnums { get; set; }
public string OutDir { get; set; }
public string CheatDir { get; set; }
public IList<string> NetRoot { get; set; }
public IList<string> ExNetRoot { get; set; }
public IList<string> AppRoot { get; set; }
public IList<string> ExAppRoot { get; set; }
public IList<string> Exclude { get; set; }
public IList<string> MacroDefines { get; set; }
public string XmlDir { get; set; }
public string EnumDir { get; set; }
public int Verbosity { get; set; }
public int DebugLevel { get; set; }
public CS2JSettings ()
{
DisplayTokens = false;
// dump parse trees to stdout
DumpCSharp = false;
DumpJavaSyntax = false;
DumpJava = false;
DumpXmls = false;
DumpEnums = false;
OutDir = Directory.GetCurrentDirectory();
CheatDir = "";
NetRoot = new List<string>();
ExNetRoot = new List<string>();
AppRoot = new List<string>();
ExAppRoot = new List<string>();
Exclude = new List<string>();
MacroDefines = new List<string>();
XmlDir = Path.Combine(Directory.GetCurrentDirectory(), "tmpXMLs");
EnumDir = Path.Combine(Directory.GetCurrentDirectory(), "enums");
Verbosity = 0;
DebugLevel = 0;
}
}
}

View File

@ -19,36 +19,11 @@ namespace RusticiSoftware.Translator.CSharp
class CS2J
{
private const string VERSION = "2009.1.1.x";
private static DirectoryHT AppEnv { get; set; }
private static CS2JSettings cfg = new CS2JSettings();
public delegate void FileProcessor(string fName);
// show gui explorer of parse tree
internal static bool showTree = false;
internal static bool showCSharp = false;
internal static bool showJavaSyntax = false;
internal static bool showJava = false;
internal static bool displayTokens = false;
// dump parse tree to stdout
internal static bool dumpCSharp = false;
internal static bool dumpJavaSyntax = false;
internal static bool dumpJava = false;
internal static bool dumpXmls = false;
internal static bool dumpEnums = false;
internal static string outDir = Directory.GetCurrentDirectory();
internal static string cheatDir = "";
internal static IList<string> netRoot = new List<string>();
internal static IList<string> exNetRoot = new List<string>();
internal static IList<string> appRoot = new List<string>();
internal static IList<string> exAppRoot = new List<string>();
internal static IList<string> exclude = new List<string>();
internal static DirectoryHT appEnv = new DirectoryHT(null);
internal static IList<string> macroDefines = new List<string>();
internal static string xmlDir = Path.Combine(Directory.GetCurrentDirectory(), "tmpXMLs");
internal static string enumDir = Path.Combine(Directory.GetCurrentDirectory(), "enums");
internal static int verbosity = 0;
private static void showVersion()
{
Console.Out.WriteLine(Path.GetFileNameWithoutExtension(System.Environment.GetCommandLineArgs()[0]) + ": " + VERSION);
@ -61,7 +36,6 @@ namespace RusticiSoftware.Translator.CSharp
Console.Out.WriteLine(" [-v] (be [somewhat more] verbose, repeat for more verbosity)");
Console.Out.WriteLine(" [-D <macroVariable>] (define <macroVariable>, option can be repeated)");
Console.Out.WriteLine(" [-showtokens] (the lexer prints the tokenized input to the console)");
Console.Out.WriteLine(" [-showtree] [-showcsharp] [-showjavasyntax] [-showjava] (show parse tree at various stages of the translation)");
Console.Out.WriteLine(" [-dumpcsharp] [-dumpjavasyntax] [-dumpjava] (show parse tree at various stages of the translation)");
Console.Out.WriteLine(" [-dumpxml] [-xmldir <directory to dump xml database>] (dump the translation repository as xml files)");
Console.Out.WriteLine(" [-dumpenums <enum xml file>] (create an xml file documenting enums)");
@ -86,7 +60,9 @@ namespace RusticiSoftware.Translator.CSharp
{
long startTime = DateTime.Now.Ticks;
IList<string> remArgs = new List<string>();
XmlTextWriter enumXmlWriter = null;
XmlTextWriter enumXmlWriter = null;
AppEnv = new DirectoryHT(null);
// Use a try/catch block for parser exceptions
try
{
@ -94,32 +70,29 @@ namespace RusticiSoftware.Translator.CSharp
if (args.Length > 0)
{
if (verbosity >= 2) Console.Error.WriteLine("Parsing Command Line Arguments...");
if (cfg.Verbosity >= 2) Console.Error.WriteLine("Parsing Command Line Arguments...");
OptionSet p = new OptionSet ()
.Add ("v", v => verbosity++)
.Add ("v", v => cfg.Verbosity++)
.Add ("debug=", v => cfg.DebugLevel = Int32.Parse(v))
.Add ("version", v => showVersion())
.Add ("help|h|?", v => showUsage())
.Add ("showtree", v => showTree = true)
.Add ("showcsharp", v => showCSharp = true)
.Add ("showjava", v => showJava = true)
.Add ("showjavasyntax", v => showJavaSyntax = true)
.Add ("dumpcsharp", v => dumpCSharp = true)
.Add ("dumpjava", v => dumpJava = true)
.Add ("dumpjavasyntax", v => dumpJavaSyntax = true)
.Add ("tokens", v => displayTokens = true)
.Add ("D=", def => macroDefines.Add(def))
.Add ("dumpenums", v => dumpEnums = true)
.Add ("enumdir=", dir => enumDir = Path.Combine(Directory.GetCurrentDirectory(), dir))
.Add ("dumpxmls", v => dumpXmls = true)
.Add ("xmldir=", dir => xmlDir = Path.Combine(Directory.GetCurrentDirectory(), dir))
.Add ("odir=", dir => outDir = dir)
.Add ("cheatdir=", dir => cheatDir = dir)
.Add ("netdir=", dirs => addDirectories(netRoot, dirs))
.Add ("exnetdir=", dirs => addDirectories(exNetRoot, dirs))
.Add ("appdir=", dirs => addDirectories(appRoot, dirs))
.Add ("exappdir=", dirs => addDirectories(exAppRoot, dirs))
.Add ("exclude=", dirs => addDirectories(exclude, dirs))
.Add ("dumpcsharp", v => cfg.DumpCSharp = true)
.Add ("dumpjava", v => cfg.DumpJava = true)
.Add ("dumpjavasyntax", v => cfg.DumpJavaSyntax = true)
.Add ("dumptokens", v => cfg.DisplayTokens = true)
.Add ("D=", def => cfg.MacroDefines.Add(def))
.Add ("dumpenums", v => cfg.DumpEnums = true)
.Add ("enumdir=", dir => cfg.EnumDir = Path.Combine(Directory.GetCurrentDirectory(), dir))
.Add ("dumpxmls", v => cfg.DumpXmls = true)
.Add ("xmldir=", dir => cfg.XmlDir = Path.Combine(Directory.GetCurrentDirectory(), dir))
.Add ("odir=", dir => cfg.OutDir = dir)
.Add ("cheatdir=", dir => cfg.CheatDir = dir)
.Add ("netdir=", dirs => addDirectories(cfg.NetRoot, dirs))
.Add ("exnetdir=", dirs => addDirectories(cfg.ExNetRoot, dirs))
.Add ("appdir=", dirs => addDirectories(cfg.AppRoot, dirs))
.Add ("exappdir=", dirs => addDirectories(cfg.ExAppRoot, dirs))
.Add ("exclude=", dirs => addDirectories(cfg.Exclude, dirs))
;
//TODO: fix enum dump
@ -128,24 +101,24 @@ namespace RusticiSoftware.Translator.CSharp
// Load .Net templates
foreach (string r in netRoot)
doFile(r, ".xml", addNetTranslation, exNetRoot);
foreach (string r in cfg.NetRoot)
doFile(r, ".xml", addNetTranslation, cfg.ExNetRoot);
// Load Application Class Signatures (i.e. generate templates)
if (appRoot.Count == 0)
if (cfg.AppRoot.Count == 0)
// By default translation target is application root
appRoot.Add(remArgs[0]);
foreach (string r in appRoot)
doFile(r, ".cs", addAppSigTranslation, exAppRoot); // parse it
if (dumpEnums) {
enumXmlWriter = new XmlTextWriter(enumDir, System.Text.Encoding.UTF8);
cfg.AppRoot.Add(remArgs[0]);
foreach (string r in cfg.AppRoot)
doFile(r, ".cs", addAppSigTranslation, cfg.ExAppRoot); // parse it
if (cfg.DumpEnums) {
enumXmlWriter = new XmlTextWriter(cfg.EnumDir, System.Text.Encoding.UTF8);
}
if (dumpXmls)
if (cfg.DumpXmls)
{
// Get package name and convert to directory name
foreach (DictionaryEntry de in appEnv)
foreach (DictionaryEntry de in AppEnv)
{
String xmlFName = Path.Combine(xmlDir,
String xmlFName = Path.Combine(cfg.XmlDir,
((string)de.Key).Replace('.', Path.DirectorySeparatorChar) + ".xml");
String xmlFDir = Path.GetDirectoryName(xmlFName);
if (!Directory.Exists(xmlFDir))
@ -158,8 +131,8 @@ namespace RusticiSoftware.Translator.CSharp
w.Close();
}
}
doFile(remArgs[0], ".cs", translateFile, exclude); // parse it
if (dumpEnums)
doFile(remArgs[0], ".cs", translateFile, cfg.Exclude); // parse it
if (cfg.DumpEnums)
{
enumXmlWriter.WriteEndElement();
enumXmlWriter.Close();
@ -176,7 +149,7 @@ namespace RusticiSoftware.Translator.CSharp
Console.Error.WriteLine(e.StackTrace); // so we can get stack trace
}
double elapsedTime = ((DateTime.Now.Ticks - startTime) / TimeSpan.TicksPerMillisecond) / 1000.0;
if (verbosity >= 1)
if (cfg.Verbosity >= 1)
{
System.Console.Out.WriteLine("");
System.Console.Out.WriteLine("");
@ -200,7 +173,7 @@ namespace RusticiSoftware.Translator.CSharp
}
else if ((Path.GetFileName(canonicalPath).Length > ext.Length) && canonicalPath.Substring(canonicalPath.Length - ext.Length).Equals(ext))
{
if (verbosity >= 2) Console.WriteLine(" " + canonicalPath);
if (cfg.Verbosity >= 2) Console.WriteLine(" " + canonicalPath);
try
{
@ -215,15 +188,16 @@ namespace RusticiSoftware.Translator.CSharp
}
}
public static BufferedTreeNodeStream parseFile(string fullName)
public static CommonTreeNodeStream parseFile(string fullName)
{
CommonTokenStream tokens = null;
if (verbosity > 2) Console.WriteLine("Parsing " + Path.GetFileName(fullName));
if (cfg.Verbosity > 2) Console.WriteLine("Parsing " + Path.GetFileName(fullName));
PreProcessor lex = new PreProcessor();;
ICharStream input = new ANTLRFileStream(fullName);
lex.AddDefine(cfg.MacroDefines);
ICharStream input = new ANTLRFileStream(fullName);
lex.CharStream = input;
tokens = new CommonTokenStream(lex);
@ -231,12 +205,9 @@ namespace RusticiSoftware.Translator.CSharp
csParser.compilation_unit_return parser_rt;
parser_rt = p.compilation_unit();
ITree parse_tree = (ITree)parser_rt.Tree;
if (verbosity > 2) Console.Out.WriteLine(parse_tree.ToStringTree());
BufferedTreeNodeStream nodes = new BufferedTreeNodeStream(parse_tree);
if (nodes == null)
if (parser_rt == null || parser_rt.Tree == null)
{
if (tokens.Count > 0)
{
@ -248,7 +219,9 @@ namespace RusticiSoftware.Translator.CSharp
}
}
return nodes;
CommonTreeNodeStream nodes = new CommonTreeNodeStream(parser_rt.Tree);
return nodes;
}
@ -257,27 +230,26 @@ namespace RusticiSoftware.Translator.CSharp
{
Stream s = new FileStream(fullName, FileMode.Open, FileAccess.Read);
TypeRepTemplate t = TypeRepTemplate.newInstance(s);
appEnv[t.TypeName] = t;
AppEnv[t.TypeName] = t;
}
// Here's where we do the real work...
public static void addAppSigTranslation(string fullName)
{
BufferedTreeNodeStream nodes = parseFile(fullName);
if (nodes != null)
ITreeNodeStream csTree = parseFile(fullName);
if (csTree != null)
{
TemplateExtracter templateWalker = new TemplateExtracter(nodes);
templateWalker.DebugLevel = 10;
templateWalker.compilation_unit();
TemplateExtracter templateWalker = new TemplateExtracter(csTree);
templateWalker.compilation_unit(cfg);
}
}
// Here's where we do the real work...
public static void translateFile(string fullName)
{
BufferedTreeNodeStream nodes = parseFile(fullName);
if (dumpCSharp) AntlrUtils.AntlrUtils.DumpNodes(new CommonTreeNodeStream(nodes.TreeSource));
CommonTreeNodeStream csTree = parseFile(fullName);
if (cfg.DumpCSharp) AntlrUtils.AntlrUtils.DumpNodes(csTree);
// ASTNode t = parseFile(f, s);
// if (t != null)

View File

@ -9,22 +9,12 @@ namespace RusticiSoftware.Translator.CSharp
{
public class CommonWalker : TreeParser
{
protected CS2JSettings Cfg { get; set; }
protected CommonWalker(ITreeNodeStream input, RecognizerSharedState state)
: base(input, state)
{ }
/// <summary>
/// Debug Routines
/// </summary>
private int debugLevel = 0;
public int DebugLevel
{
get { return debugLevel; }
set { debugLevel = value; }
}
protected void Debug(String s)
{
Debug(1, s);
@ -37,7 +27,7 @@ namespace RusticiSoftware.Translator.CSharp
protected void Debug(int level, String s)
{
if (level <= DebugLevel)
if (level <= Cfg.DebugLevel)
{
Console.Out.WriteLine(s);
}

View File

@ -11,9 +11,13 @@ tree grammar TemplateExtracter;
options {
tokenVocab=cs;
ASTLabelType=CommonTree;
language=CSharp2;
superClass='RusticiSoftware.Translator.CSharp.CommonWalker';
//backtrack=true;
language=CSharp2;
superClass='RusticiSoftware.Translator.CSharp.CommonWalker';
//backtrack=true;
}
scope UseScope {
IList<String> usePath;
}
@namespace { RusticiSoftware.Translator.CSharp }
@ -21,20 +25,17 @@ options {
@header
{
using System.Text;
using System.Collections.Generic;
using RusticiSoftware.Translator.CLR;
}
@members
{
// As we scan the AST we collect these features until
// we reach the end, then calculate the TypeRep and insert it into
// the TypeEnv
private IList<PropRepTemplate> Properties = new List<PropRepTemplate>();
private IList<MethodRepTemplate> Methods = new List<MethodRepTemplate>();
private IList<ConstructorRepTemplate> Constructors = new List<ConstructorRepTemplate>();
private IList<FieldRepTemplate> Fields = new List<FieldRepTemplate>();
private IList<CastRepTemplate> Casts = new List<CastRepTemplate>();
// This is a global 'magic' string used to return strings from within complex productions.
// For example a namespace rule will set the string that represents the namespace, saves
// passing a whole load of returns through intermediate rules.
protected string Capture {get; set;}
}
/********************************************************************************************
@ -42,24 +43,125 @@ options {
*********************************************************************************************/
///////////////////////////////////////////////////////
compilation_unit:
{ Debug("start"); } using_directives
compilation_unit[CS2JSettings inCfg]
scope UseScope;
@init {
Cfg = inCfg;
$UseScope::usePath = new List<String>();
}
:
{ Debug("start template extraction"); }
namespace_body
{ Debug("end template extraction"); }
;
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' namespace_name ';' { Debug("using " + $namespace_name.namespaceText); })
;
namespace_name returns [string namespaceText]:
^(NAMESPACE_OR_TYPE_NAME nsc=namespace_component { namespaceText = $nsc.idText; }
(nscp=namespace_component { namespaceText = namespaceText + "." + $nscp.idText; } )* )
;
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
| struct_declaration
| interface_declaration
| enum_declaration
| delegate_declaration ;
namespace_component returns [string idText]:
^(NSTN identifier { idText=$identifier.idText; } )
// ad-hoc
///////////////////////////////////////////////////////
// Type Section
///////////////////////////////////////////////////////
class_declaration
: 'class' identifier ';' ;
struct_declaration
: 'struct' identifier ';' ;
interface_declaration
: 'interface' identifier ';' ;
enum_declaration
: 'enum' identifier ';' ;
delegate_declaration
: 'delegate' identifier ';' ;
type_name:
namespace_or_type_name ;
namespace_or_type_name:
type_or_generic ('::' type_or_generic)? ('.' type_or_generic)* ;
type_or_generic:
(identifier '<') => identifier generic_argument_list
| identifier
;
generic_argument_list:
'<' type_arguments '>' ;
type_arguments:
type (',' type)* ;
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' '*'+
;
identifier returns [string idText]:
^(ID IDENTIFIER { idText = $IDENTIFIER.Text; Debug("Identifier: " + $IDENTIFIER.Text); } )
;
qualified_identifier:
identifier ('.' identifier)* ;
namespace_name
: namespace_or_type_name ;
modifiers:
modifier+ ;
modifier:
'new' | 'public' | 'protected' | 'private' | 'internal' | 'unsafe' | 'abstract' | 'sealed' | 'static'
| 'readonly' | 'volatile' | 'extern' | 'virtual' | 'override';
rank_specifiers:
rank_specifier+ ;
rank_specifier:
'[' dim_separators? ']' ;
dim_separators:
','+ ;
global_attributes:
global_attribute+ ;
global_attribute:
'[' 'fred' ','? ']' ;
attributes:
attribute_sections ;
attribute_sections:
attribute_section+ ;
attribute_section:
'[' 'jim' ','? ']' ;
predefined_type:
'bool' | 'byte' | 'char' | 'decimal' | 'double' | 'float' | 'int' | 'long' | 'object' | 'sbyte'
| 'short' | 'string' | 'uint' | 'ulong' | 'ushort' ;
identifier:
IDENTIFIER { Debug($IDENTIFIER.Text); } ;

File diff suppressed because it is too large Load Diff

View File

@ -51,8 +51,7 @@ namespace RusticiSoftware.Translator.CSharp
TemplateExtracter templateWalker = new TemplateExtracter(nodes);
templateWalker.DebugLevel = 10;
templateWalker.compilation_unit();
templateWalker.compilation_unit(new CS2JSettings());
}
}

View File

@ -39,7 +39,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Commandlineparameters>-genTemplate /Users/keving/gitrepos/cs2j/CSharpTranslator/antlr3/src/cs2j/bin/Debug/Antlr3-2.Runtime.DotNet20.dll Antlr.Runtime.CommonTokenStream</Commandlineparameters>
<Commandlineparameters>-dumpcsharp /Users/keving/gitrepos/cs2j/CSharpTranslator/antlr3/cs2jTest/TestDLLs/Various.cs</Commandlineparameters>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
@ -54,7 +54,6 @@
<Compile Include="CLR\cs2j.cs" />
<Compile Include="CLR\TypeRep.cs" />
<Compile Include="CSharp\CommonWalker.cs" />
<Compile Include="CSharp\csCrawl.cs" />
<Compile Include="CSharp\csLexer.cs" />
<Compile Include="CSharp\csParser.cs" />
<Compile Include="CSharp\Main.cs" />
@ -66,6 +65,7 @@
<Compile Include="Utils\Constants.cs" />
<Compile Include="Utils\TypeHelper.cs" />
<Compile Include="CSharp\TemplateExtracter.cs" />
<Compile Include="CLR\CS2JSettings.cs" />
</ItemGroup>
<ItemGroup>
<None Include="CSharp\csCrawl.g" />